From b4f3850e493e631fd86920d5f0b8fdfc1db7af54 Mon Sep 17 00:00:00 2001 From: Frames White Date: Fri, 13 Dec 2024 10:53:32 +0800 Subject: [PATCH] Store the compartment for created metabolites --- src/cobra/core/reaction.py | 2 +- tests/test_core/test_core_reaction.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/cobra/core/reaction.py b/src/cobra/core/reaction.py index 50b6b63ca..e02702bdb 100644 --- a/src/cobra/core/reaction.py +++ b/src/cobra/core/reaction.py @@ -1615,7 +1615,7 @@ def build_reaction_from_string( except KeyError: if verbose: print(f"unknown metabolite '{met_id}' created") - met = Metabolite(met_id) + met = Metabolite(met_id, compartment=compartment or None) self.add_metabolites({met: num}) def summary( diff --git a/tests/test_core/test_core_reaction.py b/tests/test_core/test_core_reaction.py index a0e10a012..0a98977e3 100644 --- a/tests/test_core/test_core_reaction.py +++ b/tests/test_core/test_core_reaction.py @@ -380,6 +380,18 @@ def test_build_from_string(model: Model) -> None: assert pgi.bounds == (0, 1000) +def test_build_from_string_creating_metabolites() -> None: + # 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.a.compartment == "c" + assert model.metabolites.b.compartment == "c" + assert model.reaction.R1.compartments == set(["c"]) + + def test_bounds_setter(model: Model) -> None: """Test reaction bounds setter.""" rxn = model.reactions.get_by_id("PGI")