Adding reactions using a string representation

570 views
Skip to first unread message

Afif Elghraoui

unread,
Jan 27, 2015, 3:49:44 AM1/27/15
to cobr...@googlegroups.com
Hello,
I know there used to be a function in cobrapy like the one in the cobra
toolbox where you could add a reaction using a string representation (or
there was at least the parser to make this easy). I actually copied out
the reaction string parser before it was removed from the cobrapy
repository and I use it all the time. I was wondering why it was taken
out and also if there's a chance of this functionality being reinstated.
I load reaction data from a file where they're represented as strings by
their formulas. I thought this might be a common use case and that it'd
be useful to have the functionality in cobrapy.

Thanks and regards,
Afif

Ali Ebrahim

unread,
Jan 27, 2015, 1:41:59 PM1/27/15
to Afif Elghraoui, cobra-pie
Hey Afif,

That function was removed because it was untested and didn't do somethings quite right. In practice though, there is no reason such a function shouldn't exist. I have a rewritten version of it which I have been using as well. If there are other users I can add it back to the codebase with some tests. Would you mind filing a bug on the issue tracker for this?

Here is the function I use now: https://gist.github.com/aebrahim/fdc493840b73e628d4b0

-Ali




--
You received this message because you are subscribed to the Google Groups "cobra pie" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cobra-pie+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Afif Elghraoui

unread,
Jan 27, 2015, 4:38:10 PM1/27/15
to Ali Ebrahim, cobra-pie
Sure thing. Thanks for the information!

Afif
To unsubscribe from this group and stop receiving emails from it, send an email to cobra-pie+...@googlegroups.com.

Ali Ebrahim

unread,
Jan 27, 2015, 10:15:38 PM1/27/15
to Afif Elghraoui, cobra-pie
This should be resolved in commit 092d11e

Afif Elghraoui

unread,
Jan 27, 2015, 10:38:53 PM1/27/15
to cobr...@googlegroups.com
Many thanks. So from taking a glance at the code, it looks to me like this is roughly how it should be used:

from cobra import Reaction
my_reaction = Reaction('3OAS140')
my_reaction.name = '3 oxoacyl acyl carrier protein synthase n C140 '
my_reaction.subsystem = 'Cell Envelope Biosynthesis'
my_reaction.reaction = 'malACP_c + h_c + ddcaACP_c --> co2_c + 3omrsACP_c + ACP_c'

with no direct call to add_metabolites necessary.

Thanks again
Afif

Rodrigo Colpo

unread,
May 13, 2018, 10:00:39 PM5/13/18
to cobra pie
Hi,

I tried the exemple above (my_reaction.reaction = 'malACP_c + h_c + ddcaACP_c --> co2_c + 3omrsACP_c + ACP_c') and got an error.
Is there any way to add reactions using their formula?

Thanks.

Moritz Beber

unread,
May 14, 2018, 3:49:00 AM5/14/18
to cobra pie
Dear Rodrigo,

I'm not sure which example you are referring to. At the moment adding reactions by their string is a little awkward but assuming that my_reaction in your example is a newly instantiation cobra.Reaction object then it should be fine. Please send us a copy of the error otherwise it is hard to help you.

Best,
Moritz
--

Rodrigo Colpo

unread,
May 14, 2018, 7:54:02 AM5/14/18
to cobra pie
Hi Moritz,

I tried the exemple bellow:

from cobra import Reaction
my_reaction = Reaction('3OAS140')
my_reaction.name
 = '3 oxoacyl acyl carrier protein synthase n C140 '
my_reaction.subsystem = 'Cell Envelope Biosynthesis'
my_reaction.reaction = 'malACP_c + h_c + ddcaACP_c --> co2_c + 3omrsACP_c + ACP_c'

when I entered the line "my_reaction.reaction = 'malACP_c + h_c + ddcaACP_c --> co2_c + 3omrsACP_c + ACP_c' " into the GUI I received an error message:

Warning (from warnings module):
  File "C:\Python36\lib\site-packages\cobra\core\reaction.py", line 982
    warn("no model found")
UserWarning: no model found
Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    my_reaction.reaction = 'malACP_c + h_c + ddcaACP_c --> co2_c + 3omrsACP_c + ACP_c'
  File "C:\Python36\lib\site-packages\cobra\core\reaction.py", line 852, in reaction
    return self.build_reaction_from_string(value)
  File "C:\Python36\lib\site-packages\cobra\core\reaction.py", line 1033, in build_reaction_from_string
    met = model.metabolites.get_by_id(met_id)
AttributeError: 'NoneType' object has no attribute 'metabolites'


Do you have any idea of what is going on?

Thanks,
Rodrigo.

Moritz Beber

unread,
May 14, 2018, 8:21:01 AM5/14/18
to cobr...@googlegroups.com
Thank you for providing the full example. You've hit one of the spots where reactions are tightly linked to models. We should make it work for isolated reactions but the assumption is that the reaction belongs to a model so that we can look up the metabolites or add them to the model when they're missing. So in order to fix your problem:

In [1]: import cobra

In [7]: m = cobra.Model()

In [8]: m.add_reactions([my_reaction])

In [9]: my_reaction.reaction = 'malACP_c + h_c + ddcaACP_c --> co2_c + 3omrsACP_c + ACP_c'
unknown metabolite 'malACP_c' created
unknown metabolite 'h_c' created
unknown metabolite 'ddcaACP_c' created
unknown metabolite 'co2_c' created
unknown metabolite '3omrsACP_c' created
unknown metabolite 'ACP_c' created

You could also fully define the metabolites first on the model and then build the reaction.

Hope that helps,
Moritz
Reply all
Reply to author
Forward
0 new messages