Skip to content

Commit b3ae805

Browse files
authored
Do not error on duplicate boundary addition (#1401)
* Do not error on duplicate boundary addition * add log message when adding duplicate boundary reaction
1 parent 5bc58c9 commit b3ae805

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

release-notes/next-release.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Fixed a bug with SBML group parsing that affects the Debian package.
1010

1111
## Other
12+
* Adding a duplicate boundary reaction (with `add_boundary`) no longer errors, but instead just returns the existing reaction.
1213

1314
## Deprecated features
1415

src/cobra/core/model.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,9 @@ def add_boundary(
676676
"identifier. Please set the `reaction_id`."
677677
)
678678
if reaction_id in self.reactions:
679-
raise ValueError(f"Boundary reaction '{reaction_id}' already exists.")
679+
# It already exists so just retrieve it.
680+
logger.info(f"Boundary reaction '{reaction_id}' already exists.")
681+
return self.reactions.get_by_id(reaction_id)
680682
name = f"{metabolite.name} {type}"
681683
rxn = Reaction(id=reaction_id, name=name, lower_bound=lb, upper_bound=ub)
682684
rxn.add_metabolites({metabolite: -1})

tests/test_core/test_model.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,9 @@ def test_add_existing_boundary(
489489
The allowed types for boundary, see add_boundary() for types.
490490
"""
491491
for metabolite in metabolites:
492-
model.add_boundary(metabolite, reaction_type)
493-
with pytest.raises(ValueError):
494-
model.add_boundary(metabolite, reaction_type)
492+
rxn_added = model.add_boundary(metabolite, reaction_type)
493+
rxn_dup = model.add_boundary(metabolite, reaction_type)
494+
assert rxn_dup is rxn_added
495495

496496

497497
@pytest.mark.parametrize("solver", optlang_solvers)

0 commit comments

Comments
 (0)