In [1]:
from __future__ import print_function
import cobra.test
# "ecoli" and "salmonella" are also valid arguments
model = cobra.test.create_test_model("textbook")
The reactions, metabolites, and genes attributes of the cobrapy model are a special type of list called a DictList, and each one is made up of Reaction, Metabolite and Gene objects respectively.
In [2]:
print(len(model.reactions))
print(len(model.metabolites))
print(len(model.genes))
Just like a regular list, objects in the DictList can be retrived by index. For example, to get the 30th reaction in the model (at index 29 because of 0-indexing):
In [3]:
model.reactions[29]
Out[3]:
Addictionally, items can be retrived by their id using the get_by_id() function. For example, to get the cytosolic atp metabolite object (the id is "atp_c"), we can do the following:
In [4]:
model.metabolites.get_by_id("atp_c")
Out[4]:
As an added bonus, users with an interactive shell such as IPython will be able to tab-complete to list elements inside a list. While this is not recommended behavior for most code because of the possibility for characters like "-" inside ids, this is very useful while in an interactive prompt:
In [5]:
model.reactions.EX_glc__D_e.lower_bound
Out[5]:
We will consider the reaction glucose 6-phosphate isomerase, which interconverts glucose 6-phosphate and fructose 6-phosphate. The reaction id for this reaction in our test model is PGI.
In [6]:
pgi = model.reactions.get_by_id("PGI")
pgi
Out[6]:
We can view the full name and reaction catalyzed as strings
In [7]:
print(pgi.name)
print(pgi.reaction)
We can also view reaction upper and lower bounds. Because the pgi.lower_bound < 0, and pgi.upper_bound > 0, pgi is reversible
In [8]:
print(pgi.lower_bound, "< pgi <", pgi.upper_bound)
print(pgi.reversibility)
We can also ensure the reaction is mass balanced. This function will return elements which violate mass balance. If it comes back empty, then the reaction is mass balanced.
In [9]:
pgi.check_mass_balance()
Out[9]:
In order to add a metabolite, we pass in a dict with the metabolite object and its coefficient
In [10]:
pgi.add_metabolites({model.metabolites.get_by_id("h_c"): -1})
pgi.reaction
Out[10]:
The reaction is no longer mass balanced
In [11]:
pgi.check_mass_balance()
Out[11]:
We can remove the metabolite, and the reaction will be balanced once again.
In [12]:
pgi.pop(model.metabolites.get_by_id("h_c"))
print(pgi.reaction)
print(pgi.check_mass_balance())
It is also possible to build the reaction from a string. However, care must be taken when doing this to ensure reaction id's match those in the model. The direction of the arrow is also used to update the upper and lower bounds.
In [13]:
pgi.reaction = "g6p_c --> f6p_c + h_c + green_eggs + ham"
In [14]:
pgi.reaction
Out[14]:
In [15]:
pgi.reaction = "g6p_c <=> f6p_c"
pgi.reaction
Out[15]:
We will consider cytosolic atp as our metabolite, which has the id atp_c in our test model.
In [16]:
atp = model.metabolites.get_by_id("atp_c")
atp
Out[16]:
We can print out the metabolite name and compartment (cytosol in this case).
In [17]:
print(atp.name)
print(atp.compartment)
We can see that ATP is a charged molecule in our model.
In [18]:
atp.charge
Out[18]:
We can see the chemical formula for the metabolite as well.
In [19]:
print(atp.formula)
The reactions attribute gives a frozenset of all reactions using the given metabolite. We can use this to count the number of reactions which use atp.
In [20]:
len(atp.reactions)
Out[20]:
A metabolite like glucose 6-phosphate will participate in fewer reactions.
In [21]:
model.metabolites.get_by_id("g6p_c").reactions
Out[21]:
The gene_reaction_rule is a boolean representation of the gene requirements for this reaction to be active as described in Schellenberger et al 2011 Nature Protocols 6(9):1290-307.
The GPR is stored as the gene_reaction_rule for a Reaction object as a string.
In [22]:
gpr = pgi.gene_reaction_rule
gpr
Out[22]:
Corresponding gene objects also exist. These objects are tracked by the reactions itself, as well as by the model
In [23]:
pgi.genes
Out[23]:
In [24]:
pgi_gene = model.genes.get_by_id("b4025")
pgi_gene
Out[24]:
Each gene keeps track of the reactions it catalyzes
In [25]:
pgi_gene.reactions
Out[25]:
Altering the gene_reaction_rule will create new gene objects if necessary and update all relationships.
In [26]:
pgi.gene_reaction_rule = "(spam or eggs)"
pgi.genes
Out[26]:
In [27]:
pgi_gene.reactions
Out[27]:
Newly created genes are also added to the model
In [28]:
model.genes.get_by_id("spam")
Out[28]:
The delete_model_genes function will evaluate the gpr and set the upper and lower bounds to 0 if the reaction is knocked out. This function can preserve existing deletions or reset them using the cumulative_deletions flag.
In [29]:
len("%3d"% 0)
Out[29]:
In [30]:
cobra.manipulation.delete_model_genes(model, ["spam"],
cumulative_deletions=True)
print("after 1 KO: %4d < flux_PGI < %4d" %
(pgi.lower_bound, pgi.upper_bound))
cobra.manipulation.delete_model_genes(model, ["eggs"],
cumulative_deletions=True)
print("after 2 KO: %4d < flux_PGI < %4d" %
(pgi.lower_bound, pgi.upper_bound))
The undelete_model_genes can be used to reset a gene deletion
In [31]:
cobra.manipulation.undelete_model_genes(model)
print(pgi.lower_bound, "< pgi <", pgi.upper_bound)