This document will address frequently asked questions not addressed in other pages of the documentation.
13.3. How do I rename reactions or metabolites?¶
When renaming metabolites or reactions, there are issues because cobra indexes based off of ID’s, which can cause errors. For example:
from __future__ import print_function import cobra.test model = cobra.test.create_test_model() for metabolite in model.metabolites: metabolite.id = "test_" + metabolite.id try: model.metabolites.get_by_id(model.metabolites.id) except KeyError as e: print(repr(e))
/home/moritz/.virtualenvs/cobra/lib/python3.5/site-packages/optlang/gurobi_interface.py:26: UserWarning: Be careful! The GUROBI interface is still under construction ... warn("Be careful! The GUROBI interface is still under construction ...")
The Model.repair function will rebuild the necessary indexes
<Metabolite test_dcaACP_c at 0x7fa92b9257f0>
13.4. How do I delete a gene?¶
That depends on what precisely you mean by delete a gene.
If you want to simulate the model with a gene knockout, use the
cobra.manipulation.delete_model_genes function. The effects of this
function are reversed by
model = cobra.test.create_test_model() PGI = model.reactions.get_by_id("PGI") print("bounds before knockout:", (PGI.lower_bound, PGI.upper_bound)) cobra.manipulation.delete_model_genes(model, ["STM4221"]) print("bounds after knockouts", (PGI.lower_bound, PGI.upper_bound))
bounds before knockout: (-1000.0, 1000.0) bounds after knockouts (0.0, 0.0)
If you want to actually remove all traces of a gene from a model, this
is more difficult because this will require changing all the
gene_reaction_rule strings for reactions involving the gene.
13.5. How do I change the reversibility of a Reaction?¶
Reaction.reversibility is a property in cobra which is computed when
it is requested from the lower and upper bounds.
model = cobra.test.create_test_model() model.reactions.get_by_id("PGI").reversibility
Trying to set it directly will result in an error or warning:
try: model.reactions.get_by_id("PGI").reversibility = False except Exception as e: print(repr(e))
cobra/core/reaction.py:484 UserWarning: Setting reaction reversibility is ignored
The way to change the reversibility is to change the bounds to make the reaction irreversible.
model.reactions.get_by_id("PGI").lower_bound = 10 model.reactions.get_by_id("PGI").reversibility
13.6. How do I generate an LP file from a COBRA model?¶
13.6.1. For optlang based solvers¶
With optlang solvers, the LP formulation of a model is obtained by it’s string representation. All solvers behave the same way.
with open('test.lp', 'w') as out: out.write(str(model.solver))
13.6.2. For cobrapy’s internal solvers¶
With the internal solvers, we first create the problem and use functions bundled with the solver.
Please note that unlike the LP file format, the MPS file format does not specify objective direction and is always a minimization. Some (but not all) solvers will rewrite the maximization as a minimization.
model = cobra.test.create_test_model() # glpk through cglpk glpk = cobra.solvers.cglpk.create_problem(model) glpk.write("test.lp") glpk.write("test.mps") # will not rewrite objective # cplex cplex = cobra.solvers.cplex_solver.create_problem(model) cplex.write("test.lp") cplex.write("test.mps") # rewrites objective