Skip to content

Commit

Permalink
Do not error on duplicate boundary addition
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox committed Aug 23, 2024
1 parent 5bc58c9 commit 837b376
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions release-notes/next-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/cobra/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
6 changes: 3 additions & 3 deletions tests/test_core/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 837b376

Please sign in to comment.