#!/usr/bin/env python # coding: utf-8 # # Getting Started # ## Loading a model and inspecting it # To begin with, cobrapy comes with bundled models for _Salmonella_ and _E. coli_, as well as a "textbook" model of _E. coli_ core metabolism. To load a test model, type # In[1]: import cobra from cobra.io import load_model # "iJO1366" and "salmonella" are also valid arguments model = load_model("textbook") # The reactions, metabolites, and genes attributes of the cobrapy model are a special type of list called a `cobra.DictList`, and each one is made up of `cobra.Reaction`, `cobra.Metabolite` and `cobra.Gene` objects respectively. # In[2]: print(len(model.reactions)) print(len(model.metabolites)) print(len(model.genes)) # When using [Jupyter notebook](https://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/) this type of information is rendered as a table. # In[3]: model # Just like a regular list, objects in the `DictList` can be retrieved by index. For example, to get the 30th reaction in the model (at index 29 because of [0-indexing](https://en.wikipedia.org/wiki/Zero-based_numbering)): # In[4]: model.reactions[29] # Additionally, items can be retrieved by their `id` using the `DictList.get_by_id()` function. For example, to get the cytosolic atp metabolite object (the id is "atp_c"), we can do the following: # In[5]: model.metabolites.get_by_id("atp_c") # 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[6]: model.reactions.EX_glc__D_e.bounds # ## Reactions # 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[7]: pgi = model.reactions.get_by_id("PGI") pgi # We can view the full name and reaction catalyzed as strings # In[8]: 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[9]: print(pgi.lower_bound, "< pgi <", pgi.upper_bound) print(pgi.reversibility) # The lower and upper bound of reactions can also be modified, and the reversibility attribute will automatically be updated. The preferred method for manipulating bounds is using `reaction.bounds`, e.g. # In[10]: old_bounds = pgi.bounds pgi.bounds = (0, 1000.0) print(pgi.lower_bound, "< pgi <", pgi.upper_bound) print("Reversibility after modification:", pgi.reversibility) pgi.bounds = old_bounds print("Reversibility after resetting:", pgi.reversibility) # Bounds can also be modified one-at-a-time using `reaction.lower_bound` or `reaction.upper_bound`. This approach is less desirable than setting both bounds simultaneously with `reaction.bounds` because a user might accidently set a lower bound *higher* than an upper bound (or vice versa). If a lower bound is *higher* than upper bound (or vice versa), this will lead to an error. # In[11]: old_bounds = pgi.bounds print('Upper bound prior to setting new lower bound:', pgi.upper_bound) pgi.lower_bound = 1100 print('Upper bound after setting new lower bound:', pgi.upper_bound) pgi.bounds = old_bounds # 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[12]: pgi.check_mass_balance() # In order to add a metabolite, we pass in a `dict` with the metabolite object and its coefficient # In[13]: pgi.add_metabolites({model.metabolites.get_by_id("h_c"): -1}) pgi.reaction # The reaction is no longer mass balanced # In[14]: pgi.check_mass_balance() # We can remove the metabolite, and the reaction will be balanced once again. # In[15]: pgi.subtract_metabolites({model.metabolites.get_by_id("h_c"): -1}) 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[16]: pgi.reaction = "g6p_c --> f6p_c + h_c + green_eggs + ham" # In[17]: pgi.reaction # In[18]: pgi.reaction = "g6p_c <=> f6p_c" pgi.reaction # ## Metabolites # We will consider cytosolic atp as our metabolite, which has the id `"atp_c"` in our test model. # In[19]: atp = model.metabolites.get_by_id("atp_c") atp # We can print out the metabolite name and compartment (cytosol in this case) directly as string. # In[20]: print(atp.name) print(atp.compartment) # We can see that ATP is a charged molecule in our model. # In[21]: atp.charge # We can see the chemical formula for the metabolite as well. # In[22]: 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[23]: len(atp.reactions) # A metabolite like glucose 6-phosphate will participate in fewer reactions. # In[24]: model.metabolites.get_by_id("g6p_c").reactions # ## Genes # 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](http://dx.doi.org/doi:10.1038/nprot.2011.308). # # The GPR is stored as the GPR class in the gpr for a Reaction. A string representation of it is stored as the gene_reaction_rule for a Reaction object. # In[25]: gpr = pgi.gpr print(gpr) gpr_string = pgi.gene_reaction_rule print(gpr_string) # Corresponding gene objects also exist. These objects are tracked by the reactions itself, as well as by the model # In[26]: pgi.genes # In[27]: pgi_gene = model.genes.get_by_id("b4025") pgi_gene # Each gene keeps track of the reactions it catalyzes # In[28]: pgi_gene.reactions # Altering the gene_reaction_rule will create new gene objects if necessary and update all relationships. # In[29]: pgi.gene_reaction_rule = "(spam or eggs)" pgi.genes # In[30]: pgi_gene.reactions # Newly created genes are also added to the model # In[31]: model.genes.get_by_id("spam") # The `knock_out_model_genes` function will evaluate the GPR and set the upper and lower bounds to 0 if the reaction is knocked out. # In[32]: cobra.manipulation.knock_out_model_genes( model, ["spam"]) print("after 1 KO: %4d < flux_PGI < %4d" % (pgi.lower_bound, pgi.upper_bound)) cobra.manipulation.knock_out_model_genes( model, ["eggs"]) print("after 2 KO: %4d < flux_PGI < %4d" % (pgi.lower_bound, pgi.upper_bound)) # When knocking out model genes in a context, it is reversed when leaving the context. # Quite often, one wants to make small changes to a model and evaluate the impacts of these. For example, we may want to knock-out all reactions sequentially, and see what the impact of this is on the objective function. One way of doing this would be to create a new copy of the model before each knock-out with `model.copy()`. However, even with small models, this is a very slow approach as models are quite complex objects. Better then would be to do the knock-out, optimizing and then manually resetting the reaction bounds before proceeding with the next reaction. Since this is such a common scenario however, cobrapy allows us to use the model as a context, to have changes reverted automatically. # In[34]: model = load_model('textbook') for reaction in model.reactions[:5]: with model as model: reaction.knock_out() model.optimize() print('%s blocked (bounds: %s), new growth rate %f' % (reaction.id, str(reaction.bounds), model.objective.value)) # If we look at those knocked reactions, see that their bounds have all been reverted. # In[35]: [reaction.bounds for reaction in model.reactions[:5]] # Nested contexts are also supported # In[36]: print('original objective: ', model.objective.expression) with model: model.objective = 'ATPM' print('print objective in first context:', model.objective.expression) with model: model.objective = 'ACALD' print('print objective in second context:', model.objective.expression) print('objective after exiting second context:', model.objective.expression) print('back to original objective:', model.objective.expression) # Most methods that modify the model are supported like this including adding and removing reactions and metabolites and setting the objective. Supported methods and functions mention this in the corresponding documentation. # While it does not have any actual effect, for syntactic convenience it is also possible to refer to the model by a different name than outside the context. Such as # In[37]: with model as inner: inner.reactions.PFK.knock_out # In[ ]: