Skip to content

Commit

Permalink
Store the compartment for created metabolites (#1420)
Browse files Browse the repository at this point in the history
* Store the compartment for created metabolites

* update release notes

* add docstring

Co-authored-by: Christian Diener <[email protected]>

* fix compartment logic in build_reaction_from_string

---------

Co-authored-by: Christian Diener <[email protected]>
  • Loading branch information
oxinabox and cdiener authored Jan 8, 2025
1 parent 0e99b1d commit e5fc6ed
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions release-notes/next-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

Fixes failures of GPR.copy() in Python 3.13.

Fix compartment not being stored for metabolites created during
reaction.build_reaction_from_string

## Other

## Deprecated features
Expand Down
9 changes: 5 additions & 4 deletions src/cobra/core/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

# This regular expression finds any single letter compartment enclosed in
# square brackets at the beginning of the string. For example [c] : foo --> bar
compartment_finder = re.compile(r"^\s*(\[[A-Za-z]\])\s*:*")
compartment_finder = re.compile(r"^\s*\[([A-Za-z])\]\s*:*")
# Regular expressions to match the arrows
_reversible_arrow_finder = re.compile("<(-+|=+)>")
_forward_arrow_finder = re.compile("(-+|=+)>")
Expand Down Expand Up @@ -1573,7 +1573,7 @@ def build_reaction_from_string(
compartment = found_compartments[0]
reaction_str = compartment_finder.sub("", reaction_str)
else:
compartment = ""
compartment = None

# reversible case
arrow_match = reversible_arrow_finder.search(reaction_str)
Expand Down Expand Up @@ -1609,13 +1609,14 @@ def build_reaction_from_string(
else:
met_id = term
num = factor
met_id += compartment
if compartment is not None:
met_id += f"[{compartment}]"
try:
met = model.metabolites.get_by_id(met_id)
except KeyError:
if verbose:
print(f"unknown metabolite '{met_id}' created")
met = Metabolite(met_id)
met = Metabolite(met_id, compartment=compartment)
self.add_metabolites({met: num})

def summary(
Expand Down
13 changes: 13 additions & 0 deletions tests/test_core/test_core_reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,19 @@ def test_build_from_string(model: Model) -> None:
assert pgi.bounds == (0, 1000)


def test_build_from_string_creating_metabolites() -> None:
"""Test that metabolites are created in the correct compartment."""
# https://github.com/opencobra/cobrapy/issues/1418
model = Model()
reaction = Reaction("R1")
model.add_reactions([reaction])
reaction.build_reaction_from_string("[c]: a --> b")
assert len(model.metabolites) == 2
assert model.metabolites.get_by_id("a[c]").compartment == "c"
assert model.metabolites.get_by_id("b[c]").compartment == "c"
assert model.reactions.R1.compartments == set(["c"])


def test_bounds_setter(model: Model) -> None:
"""Test reaction bounds setter."""
rxn = model.reactions.get_by_id("PGI")
Expand Down

0 comments on commit e5fc6ed

Please sign in to comment.