From 837b37621119f813035076238a39f7c6de68c1ae Mon Sep 17 00:00:00 2001 From: Frames White Date: Fri, 23 Aug 2024 11:52:07 +0800 Subject: [PATCH] Do not error on duplicate boundary addition --- release-notes/next-release.md | 1 + src/cobra/core/model.py | 3 ++- tests/test_core/test_model.py | 6 +++--- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/release-notes/next-release.md b/release-notes/next-release.md index 0d16de7c6..0cca0da86 100644 --- a/release-notes/next-release.md +++ b/release-notes/next-release.md @@ -9,6 +9,7 @@ * Fixed a bug with SBML group parsing that affects the Debian package. ## Other +* Adding a duplicate boundary reaction (with `add_boundary`) no longer errors, but instead just returns the existing reaction. ## Deprecated features diff --git a/src/cobra/core/model.py b/src/cobra/core/model.py index 4ef74df13..d183b4c26 100644 --- a/src/cobra/core/model.py +++ b/src/cobra/core/model.py @@ -676,7 +676,8 @@ def add_boundary( "identifier. Please set the `reaction_id`." ) if reaction_id in self.reactions: - raise ValueError(f"Boundary reaction '{reaction_id}' already exists.") + # It already exists so just retrieve it. + return self.reactions.get_by_id(reaction_id) name = f"{metabolite.name} {type}" rxn = Reaction(id=reaction_id, name=name, lower_bound=lb, upper_bound=ub) rxn.add_metabolites({metabolite: -1}) diff --git a/tests/test_core/test_model.py b/tests/test_core/test_model.py index e7e606ae1..c29a67ef6 100644 --- a/tests/test_core/test_model.py +++ b/tests/test_core/test_model.py @@ -489,9 +489,9 @@ def test_add_existing_boundary( The allowed types for boundary, see add_boundary() for types. """ for metabolite in metabolites: - model.add_boundary(metabolite, reaction_type) - with pytest.raises(ValueError): - model.add_boundary(metabolite, reaction_type) + rxn_added = model.add_boundary(metabolite, reaction_type) + rxn_dup = model.add_boundary(metabolite, reaction_type) + assert rxn_dup is rxn_added @pytest.mark.parametrize("solver", optlang_solvers)