Skip to content

Do not error on duplicate boundary addition #1401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 3 additions & 1 deletion src/cobra/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,9 @@ 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.
logger.info(f"Boundary reaction '{reaction_id}' already exists.")
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
Loading