opencobra / cobrapy

COBRApy is a package for constraint-based modeling of metabolic networks.
http://opencobra.github.io/cobrapy/
GNU General Public License v2.0
461 stars 216 forks source link

Adding new metabolites #1371

Closed amarsh26 closed 9 months ago

amarsh26 commented 9 months ago

Is there an existing issue for this?

Problem description

Hello,

I am trying to add new reactions with new metabolites to my models, but I am receiving errors such as the following (using the steps outlined in the documentation for building a model).

ValueError: Reaction 'ALPHNH' does not belong to a model. Either add the reaction to a model or use Metabolite objects instead of strings as keys.

Is there better way to add reactions to existing models?

Thanks for your help.

Code sample

Code run:

Traceback:

Environment

Anything else?

No response

Midnighter commented 9 months ago

Can you please share the code that you used (or something very similar)?

amarsh26 commented 9 months ago

I think the root of my issue is actually with using Model.get_by_id (see example below) as if I add all of the metabolites in the reactions like they are new, adding the reaction works.

iML1515_model.get_by_id("nh4_c")

`--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [30], in <cell line: 1>() ----> 1 iML1515_model.get_by_id("nh4_c")

AttributeError: 'Model' object has no attribute 'get_by_i`

I believe how I was referencing existing metabolites was causing the issue in the previous post. My updated question is regarding referencing existing metabolites. Below is an example of working code.

` co2_c = Metabolite('co2_c', formula='CO2', name='CO2', compartment='c')

h_c = Metabolite('h_c', formula='H', name='H', compartment='c')

h2o_c = Metabolite('h2o_c', formula='H2O', name='H2O', compartment='c')

nh4_c = Metabolite('nh4_c', formula='H4N', name='Ammonium', compartment='c')

allphn_c = Metabolite( 'allphn_c', formula='C2H3N2O3', name='Allophanate C2H3N2O3', compartment='c')

reaction.add_metabolites({

co2_c: 2.0,

h_c: -3.0,
h2o_c: -1.0,
nh4_c: 2.0,
allphn_c: -1.0

})

reaction.reaction`

Is there a method I can use that does not require creating a metabolite object for metabolites already in the model?

Thank you so much for your help!

Midnighter commented 9 months ago

As described in the method help, the dictionary with stoichiometric coefficients can also contain string keys that reference metabolite identifiers. For that to work, the reaction has to be added to a model beforehand. This is not well explained in the documentation.

Alternatively, you use the get_by_id method to fetch the metabolite objects from your model. You seem to have a typo with the method, hence the AttributeError.

amarsh26 commented 9 months ago

Thanks. I had not realized you need to add 'metabolite'. Solution: iML1515_model.metabolites.get_by_id("nh4_c")