Skip to content

Commit e5fc6ed

Browse files
oxinaboxcdiener
andauthored
Store the compartment for created metabolites (#1420)
* 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]>
1 parent 0e99b1d commit e5fc6ed

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

release-notes/next-release.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

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

9+
Fix compartment not being stored for metabolites created during
10+
reaction.build_reaction_from_string
11+
912
## Other
1013

1114
## Deprecated features

src/cobra/core/reaction.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

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

15781578
# reversible case
15791579
arrow_match = reversible_arrow_finder.search(reaction_str)
@@ -1609,13 +1609,14 @@ def build_reaction_from_string(
16091609
else:
16101610
met_id = term
16111611
num = factor
1612-
met_id += compartment
1612+
if compartment is not None:
1613+
met_id += f"[{compartment}]"
16131614
try:
16141615
met = model.metabolites.get_by_id(met_id)
16151616
except KeyError:
16161617
if verbose:
16171618
print(f"unknown metabolite '{met_id}' created")
1618-
met = Metabolite(met_id)
1619+
met = Metabolite(met_id, compartment=compartment)
16191620
self.add_metabolites({met: num})
16201621

16211622
def summary(

tests/test_core/test_core_reaction.py

+13
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,19 @@ def test_build_from_string(model: Model) -> None:
380380
assert pgi.bounds == (0, 1000)
381381

382382

383+
def test_build_from_string_creating_metabolites() -> None:
384+
"""Test that metabolites are created in the correct compartment."""
385+
# https://github.com/opencobra/cobrapy/issues/1418
386+
model = Model()
387+
reaction = Reaction("R1")
388+
model.add_reactions([reaction])
389+
reaction.build_reaction_from_string("[c]: a --> b")
390+
assert len(model.metabolites) == 2
391+
assert model.metabolites.get_by_id("a[c]").compartment == "c"
392+
assert model.metabolites.get_by_id("b[c]").compartment == "c"
393+
assert model.reactions.R1.compartments == set(["c"])
394+
395+
383396
def test_bounds_setter(model: Model) -> None:
384397
"""Test reaction bounds setter."""
385398
rxn = model.reactions.get_by_id("PGI")

0 commit comments

Comments
 (0)