File tree 3 files changed +21
-4
lines changed
3 files changed +21
-4
lines changed Original file line number Diff line number Diff line change 6
6
7
7
Fixes failures of GPR.copy() in Python 3.13.
8
8
9
+ Fix compartment not being stored for metabolites created during
10
+ reaction.build_reaction_from_string
11
+
9
12
## Other
10
13
11
14
## Deprecated features
Original file line number Diff line number Diff line change 49
49
50
50
# This regular expression finds any single letter compartment enclosed in
51
51
# 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*:*" )
53
53
# Regular expressions to match the arrows
54
54
_reversible_arrow_finder = re .compile ("<(-+|=+)>" )
55
55
_forward_arrow_finder = re .compile ("(-+|=+)>" )
@@ -1573,7 +1573,7 @@ def build_reaction_from_string(
1573
1573
compartment = found_compartments [0 ]
1574
1574
reaction_str = compartment_finder .sub ("" , reaction_str )
1575
1575
else :
1576
- compartment = ""
1576
+ compartment = None
1577
1577
1578
1578
# reversible case
1579
1579
arrow_match = reversible_arrow_finder .search (reaction_str )
@@ -1609,13 +1609,14 @@ def build_reaction_from_string(
1609
1609
else :
1610
1610
met_id = term
1611
1611
num = factor
1612
- met_id += compartment
1612
+ if compartment is not None :
1613
+ met_id += f"[{ compartment } ]"
1613
1614
try :
1614
1615
met = model .metabolites .get_by_id (met_id )
1615
1616
except KeyError :
1616
1617
if verbose :
1617
1618
print (f"unknown metabolite '{ met_id } ' created" )
1618
- met = Metabolite (met_id )
1619
+ met = Metabolite (met_id , compartment = compartment )
1619
1620
self .add_metabolites ({met : num })
1620
1621
1621
1622
def summary (
Original file line number Diff line number Diff line change @@ -380,6 +380,19 @@ def test_build_from_string(model: Model) -> None:
380
380
assert pgi .bounds == (0 , 1000 )
381
381
382
382
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
+
383
396
def test_bounds_setter (model : Model ) -> None :
384
397
"""Test reaction bounds setter."""
385
398
rxn = model .reactions .get_by_id ("PGI" )
You can’t perform that action at this time.
0 commit comments