Cobrapy - Documentation PDF
Cobrapy - Documentation PDF
Release 0.13.3
1 Getting Started 3
1.1 Loading a model and inspecting it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Reactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Metabolites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Genes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 Making changes reversibly using models as contexts . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Building a Model 9
5 Simulating Deletions 23
5.1 Knocking out single genes and reactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.2 Single Deletions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
5.3 Double Deletions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
6 Production envelopes 27
7 Flux sampling 29
7.1 Basic usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
7.2 Advanced usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
7.3 Adding constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
8 Loopless FBA 33
8.1 Loopless solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
8.2 Loopless model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
8.3 Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
9 Gapfillling 37
i
10 Growth media 39
10.1 Minimal media . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
10.2 Boundary reactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
11 Solvers 43
11.1 Internal solver interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
14 FAQ 51
14.1 How do I install cobrapy? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
14.2 How do I cite cobrapy? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
14.3 How do I rename reactions or metabolites? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
14.4 How do I delete a gene? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
14.5 How do I change the reversibility of a Reaction? . . . . . . . . . . . . . . . . . . . . . . . . . . 52
14.6 How do I generate an LP file from a COBRA model? . . . . . . . . . . . . . . . . . . . . . . . . 52
ii
cobra Documentation, Release 0.13.3
Contents 1
cobra Documentation, Release 0.13.3
2 Contents
CHAPTER 1
Getting Started
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]: from __future__ import print_function
import cobra
import cobra.test
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))
95
72
137
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")
3
cobra Documentation, Release 0.13.3
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
Out[6]: (-10.0, 1000.0)
1.2 Reactions
We will consider the reaction glucose 6-phosphate isomerase, which interconverts glucose 6-phosphate and fruc-
tose 6-phosphate. The reaction id for this reaction in our test model is PGI.
In [7]: pgi = model.reactions.get_by_id("PGI")
pgi
Out[7]: <Reaction PGI at 0x11b886a90>
We can view the full name and reaction catalyzed as strings
In [8]: print(pgi.name)
print(pgi.reaction)
glucose-6-phosphate isomerase
g6p_c <=> f6p_c
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)
-1000.0 < pgi < 1000.0
True
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 [10]: pgi.check_mass_balance()
Out[10]: {}
In order to add a metabolite, we pass in a dict with the metabolite object and its coefficient
In [11]: pgi.add_metabolites({model.metabolites.get_by_id("h_c"): -1})
pgi.reaction
Out[11]: 'g6p_c + h_c <=> f6p_c'
We can remove the metabolite, and the reaction will be balanced once again.
In [12]: pgi.subtract_metabolites({model.metabolites.get_by_id("h_c"): -1})
print(pgi.reaction)
print(pgi.check_mass_balance())
g6p_c <=> f6p_c
{}
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"
1.3 Metabolites
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]: <Metabolite atp_c at 0x11b7f82b0>
We can print out the metabolite name and compartment (cytosol in this case) directly as string.
In [17]: print(atp.name)
print(atp.compartment)
ATP
c
We can see that ATP is a charged molecule in our model.
In [18]: atp.charge
Out[18]: -4
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]: 13
1.4 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.
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]: 'b4025'
1.3. Metabolites 5
cobra Documentation, Release 0.13.3
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]: frozenset({<Gene b4025 at 0x11b844cc0>})
In [24]: pgi_gene = model.genes.get_by_id("b4025")
pgi_gene
Out[24]: <Gene b4025 at 0x11b844cc0>
Each gene keeps track of the reactions it catalyzes
In [25]: pgi_gene.reactions
Out[25]: frozenset({<Reaction PGI at 0x11b886a90>})
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]: frozenset({<Gene spam at 0x11b850908>, <Gene eggs at 0x11b850eb8>})
In [27]: pgi_gene.reactions
Out[27]: frozenset()
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]: 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))
after 1 KO: -1000 < flux_PGI < 1000
after 2 KO: 0 < flux_PGI < 0
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 [31]: model = cobra.test.create_test_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))
ACALD blocked (bounds: (0, 0)), new growth rate 0.873922
ACALDt blocked (bounds: (0, 0)), new growth rate 0.873922
ACKr blocked (bounds: (0, 0)), new growth rate 0.873922
ACONTa blocked (bounds: (0, 0)), new growth rate -0.000000
ACONTb blocked (bounds: (0, 0)), new growth rate -0.000000
If we look at those knocked reactions, see that their bounds have all been reverted.
In [32]: [reaction.bounds for reaction in model.reactions[:5]]
Out[32]: [(-1000.0, 1000.0),
(-1000.0, 1000.0),
(-1000.0, 1000.0),
(-1000.0, 1000.0),
(-1000.0, 1000.0)]
Building a Model
This simple example demonstrates how to create a model, create a reaction, and then add the reaction to the model.
We’ll use the ‘3OAS140’ reaction from the STM_1.0 model:
1.0 malACP[c] + 1.0 h[c] + 1.0 ddcaACP[c] → 1.0 co2[c] + 1.0 ACP[c] + 1.0 3omrsACP[c]
First, create the model and reaction.
In [1]: from __future__ import print_function
In [2]: from cobra import Model, Reaction, Metabolite
# Best practise: SBML compliant IDs
model = Model('example_model')
reaction = Reaction('3OAS140')
reaction.name = '3 oxoacyl acyl carrier protein synthase n C140 '
reaction.subsystem = 'Cell Envelope Biosynthesis'
reaction.lower_bound = 0. # This is the default
reaction.upper_bound = 1000. # This is the default
We need to create metabolites as well. If we were using an existing model, we could use Model.get_by_id
to get the appropriate Metabolite objects instead.
In [3]: ACP_c = Metabolite(
'ACP_c',
formula='C11H21N2O7PRS',
name='acyl-carrier-protein',
compartment='c')
omrsACP_c = Metabolite(
'3omrsACP_c',
formula='C25H45N2O9PRS',
name='3-Oxotetradecanoyl-acyl-carrier-protein',
compartment='c')
co2_c = Metabolite('co2_c', formula='CO2', name='CO2', compartment='c')
malACP_c = Metabolite(
'malACP_c',
formula='C14H22N2O10PRS',
name='Malonyl-acyl-carrier-protein',
compartment='c')
h_c = Metabolite('h_c', formula='H', name='H', compartment='c')
ddcaACP_c = Metabolite(
'ddcaACP_c',
9
cobra Documentation, Release 0.13.3
formula='C23H43N2O8PRS',
name='Dodecanoyl-ACP-n-C120ACP',
compartment='c')
Adding metabolites to a reaction requires using a dictionary of the metabolites and their stoichiometric coefficients.
A group of metabolites can be added all at once, or they can be added one at a time.
In [4]: reaction.add_metabolites({
malACP_c: -1.0,
h_c: -1.0,
ddcaACP_c: -1.0,
co2_c: 1.0,
ACP_c: 1.0,
omrsACP_c: 1.0
})
We will add the reaction to the model, which will also add all associated metabolites and genes
In [7]: model.add_reactions([reaction])
print("")
print("Metabolites")
print("-----------")
for x in model.metabolites:
print('%9s : %s' % (x.id, x.formula))
print("")
print("Genes")
print("-----")
for x in model.genes:
associated_ids = (i.id for i in x.reactions)
print("%s is associated with reactions: %s" %
(x.id, "{" + ", ".join(associated_ids) + "}"))
Reactions
---------
3OAS140 : ddcaACP_c + h_c + malACP_c --> 3omrsACP_c + ACP_c + co2_c
Metabolites
-----------
co2_c : CO2
malACP_c : C14H22N2O10PRS
h_c : H
3omrsACP_c : C25H45N2O9PRS
ddcaACP_c : C23H43N2O8PRS
ACP_c : C11H21N2O7PRS
Genes
-----
STM1197 is associated with reactions: {3OAS140}
STM2378 is associated with reactions: {3OAS140}
Last we need to set the objective of the model. Here, we just want this to be the maximization of the flux in the
single reaction we added and we do this by assigning the reaction’s identifier to the objective property of the
model.
In [9]: model.objective = '3OAS140'
The created objective is a symbolic algebraic expression and we can examine it by printing it
In [10]: print(model.objective.expression)
print(model.objective.direction)
-1.0*3OAS140_reverse_65ddc + 1.0*3OAS140
max
which here shows that the solver will maximize the flux in the forward direction.
11
cobra Documentation, Release 0.13.3
Cobrapy supports reading and writing models in SBML (with and without FBC), JSON, YAML, MAT, and pickle
formats. Generally, SBML with FBC version 2 is the preferred format for general use. The JSON format may be
more useful for cobrapy-specific functionality.
The package also ships with test models in various formats for testing purposes.
In [1]: import cobra.test
import os
from os.path import join
data_dir = cobra.test.data_dir
textbook_model = cobra.test.create_test_model("textbook")
ecoli_model = cobra.test.create_test_model("ecoli")
salmonella_model = cobra.test.create_test_model("salmonella")
mini test files:
mini.json, mini.mat, mini.pickle, mini.yml, mini_cobra.xml, mini_fbc1.xml, mini_fbc2.xml, mini_fbc
3.1 SBML
The Systems Biology Markup Language is an XML-based standard format for distributing models which has
support for COBRA models through the FBC extension version 2.
Cobrapy has native support for reading and writing SBML with FBCv2. Please note that all id’s in the model must
conform to the SBML SID requirements in order to generate a valid SBML file.
In [2]: cobra.io.read_sbml_model(join(data_dir, "mini_fbc2.xml"))
Out[2]: <Model mini_textbook at 0x1074fd080>
In [3]: cobra.io.write_sbml_model(textbook_model, "test_fbc2.xml")
There are other dialects of SBML prior to FBC 2 which have previously been use to encode COBRA models. The
primary ones is the “COBRA” dialect which used the “notes” fields in SBML files.
13
cobra Documentation, Release 0.13.3
Cobrapy can use libsbml, which must be installed separately (see installation instructions) to read and write these
files. When reading in a model, it will automatically detect whether FBC was used or not. When writing a model,
the use_fbc_package flag can be used can be used to write files in this legacy “cobra” format.
Consider having the lxml package installed as it can speed up parsing considerably.
In [4]: cobra.io.read_sbml_model(join(data_dir, "mini_cobra.xml"))
Out[4]: <Model mini_textbook at 0x112fa6b38>
In [5]: cobra.io.write_sbml_model(
textbook_model, "test_cobra.xml", use_fbc_package=False)
3.2 JSON
Cobrapy models have a JSON (JavaScript Object Notation) representation. This format was created for interoper-
ability with escher.
In [6]: cobra.io.load_json_model(join(data_dir, "mini.json"))
Out[6]: <Model mini_textbook at 0x113061080>
In [7]: cobra.io.save_json_model(textbook_model, "test.json")
3.3 YAML
Cobrapy models have a YAML (YAML Ain’t Markup Language) representation. This format was created for
more human readable model representations and automatic diffs between models.
In [8]: cobra.io.load_yaml_model(join(data_dir, "mini.yml"))
Out[8]: <Model mini_textbook at 0x113013390>
In [9]: cobra.io.save_yaml_model(textbook_model, "test.yml")
3.4 MATLAB
Often, models may be imported and exported solely for the purposes of working with the same models in cobrapy
and the MATLAB cobra toolbox. MATLAB has its own “.mat” format for storing variables. Reading and writing
to these mat files from python requires scipy.
A mat file can contain multiple MATLAB variables. Therefore, the variable name of the model in the MATLAB
file can be passed into the reading function:
In [10]: cobra.io.load_matlab_model(
join(data_dir, "mini.mat"), variable_name="mini_textbook")
Out[10]: <Model mini_textbook at 0x113000b70>
If the mat file contains only a single model, cobra can figure out which variable to read from, and the variable_name
parameter is unnecessary.
In [11]: cobra.io.load_matlab_model(join(data_dir, "mini.mat"))
Out[11]: <Model mini_textbook at 0x113758438>
3.5 Pickle
Cobra models can be serialized using the python serialization format, pickle.
Please note that use of the pickle format is generally not recommended for most use cases. JSON, SBML, and
MAT are generally the preferred formats.
3.5. Pickle 15
cobra Documentation, Release 0.13.3
Simulations using flux balance analysis can be solved using Model.optimize(). This will maximize or
minimize (maximizing is the default) flux through the objective reactions.
In [1]: import cobra.test
model = cobra.test.create_test_model("textbook")
The solvers that can be used with cobrapy are so fast that for many small to mid-size models computing the
solution can be even faster than it takes to collect the values from the solver and convert to them python objects.
With model.optimize, we gather values for all reactions and metabolites and that can take a significant amount
of time if done repeatedly. If we are only interested in the flux value of a single reaction or the objective, it is
faster to instead use model.slim_optimize which only does the optimization and returns the objective value
leaving it up to you to fetch other values that you may need.
In [4]: %%time
model.optimize().objective_value
17
cobra Documentation, Release 0.13.3
CPU times: user 3.84 ms, sys: 672 µs, total: 4.51 ms
Wall time: 6.16 ms
Out[4]: 0.8739215069684307
In [5]: %%time
model.slim_optimize()
CPU times: user 229 µs, sys: 19 µs, total: 248 µs
Wall time: 257 µs
Out[5]: 0.8739215069684307
Models solved using FBA can be further analyzed by using summary methods, which output printed text to give
a quick representation of model behavior. Calling the summary method on the entire model displays information
on the input and output behavior of the model, along with the optimized objective.
In [6]: model.summary()
IN FLUXES OUT FLUXES OBJECTIVES
--------------- ------------ ----------------------
o2_e 21.8 h2o_e 29.2 Biomass_Ecol... 0.874
glc__D_e 10 co2_e 22.8
nh4_e 4.77 h_e 17.5
pi_e 3.21
In addition, the input-output behavior of individual metabolites can also be inspected using summary methods.
For instance, the following commands can be used to examine the overall redox balance of the model
In [7]: model.metabolites.nadh_c.summary()
PRODUCING REACTIONS -- Nicotinamide adenine dinucleotide - reduced (nadh_c)
---------------------------------------------------------------------------
% FLUX RXN ID REACTION
---- ------ ---------- --------------------------------------------------
42% 16 GAPD g3p_c + nad_c + pi_c <=> 13dpg_c + h_c + nadh_c
24% 9.28 PDH coa_c + nad_c + pyr_c --> accoa_c + co2_c + nadh_c
13% 5.06 AKGDH akg_c + coa_c + nad_c --> co2_c + nadh_c + succ...
13% 5.06 MDH mal__L_c + nad_c <=> h_c + nadh_c + oaa_c
8% 3.1 Biomass... 1.496 3pg_c + 3.7478 accoa_c + 59.81 atp_c + 0...
The objective function is determined from the objective_coefficient attribute of the objective reaction(s). Gener-
ally, a “biomass” function which describes the composition of metabolites which make up a cell is used.
In [9]: biomass_rxn = model.reactions.get_by_id("Biomass_Ecoli_core")
Currently in the model, there is only one reaction in the objective (the biomass reaction), with an linear coefficient
of 1.
In [10]: from cobra.util.solver import linear_reaction_coefficients
linear_reaction_coefficients(model)
Out[10]: {<Reaction Biomass_Ecoli_core at 0x112eab4a8>: 1.0}
The objective function can be changed by assigning Model.objective, which can be a reaction object (or just it’s
name), or a dict of {Reaction: objective_coefficient}.
In [11]: # change the objective to ATPM
model.objective = "ATPM"
FBA will not give always give unique solution, because multiple flux states can achieve the same optimum. FVA
(or flux variability analysis) finds the ranges of each metabolic flux at the optimum.
In [13]: from cobra.flux_analysis import flux_variability_analysis
In [14]: flux_variability_analysis(model, model.reactions[:10])
Out[14]: maximum minimum
ACALD -2.208811e-30 -5.247085e-14
ACALDt 0.000000e+00 -5.247085e-14
ACKr 0.000000e+00 -8.024953e-14
ACONTa 2.000000e+01 2.000000e+01
ACONTb 2.000000e+01 2.000000e+01
ACt2r 0.000000e+00 -8.024953e-14
ADK1 3.410605e-13 0.000000e+00
AKGDH 2.000000e+01 2.000000e+01
AKGt2r 0.000000e+00 -2.902643e-14
ALCD2x 0.000000e+00 -4.547474e-14
Setting parameter fraction_of_optimium=0.90 would give the flux ranges for reactions at 90% optimal-
ity.
In [15]: cobra.flux_analysis.flux_variability_analysis(
model, model.reactions[:10], fraction_of_optimum=0.9)
The standard FVA may contain loops, i.e. high absolute flux values that only can be high if they are allowed to
participate in loops (a mathematical artifact that cannot happen in vivo). Use the loopless argument to avoid
such loops. Below, we can see that FRD7 and SUCDi reactions can participate in loops but that this is avoided
when using the looplesss FVA.
In [16]: loop_reactions = [model.reactions.FRD7, model.reactions.SUCDi]
flux_variability_analysis(model, reaction_list=loop_reactions, loopless=False)
Out[16]: maximum minimum
FRD7 980.0 0.0
SUCDi 1000.0 20.0
In [17]: flux_variability_analysis(model, reaction_list=loop_reactions, loopless=True)
Out[17]: maximum minimum
FRD7 0.0 0.0
SUCDi 20.0 20.0
Flux variability analysis can also be embedded in calls to summary methods. For instance, the expected variability
in substrate consumption and product formation can be quickly found by
In [18]: model.optimize()
model.summary(fva=0.95)
IN FLUXES OUT FLUXES OBJECTIVES
---------------------------- ---------------------------- ------------
id Flux Range id Flux Range ATPM 175
-------- ------ ---------- -------- ------ ----------
o2_e 60 [55.9, 60] co2_e 60 [54.2, 60]
glc__D_e 10 [9.5, 10] h2o_e 60 [54.2, 60]
nh4_e 0 [0, 0.673] for_e 0 [0, 5.83]
pi_e 0 [0, 0.171] h_e 0 [0, 5.83]
ac_e 0 [0, 2.06]
acald_e 0 [0, 1.35]
pyr_e 0 [0, 1.35]
etoh_e 0 [0, 1.17]
lac__D_e 0 [0, 1.13]
succ_e 0 [0, 0.875]
akg_e 0 [0, 0.745]
glu__L_e 0 [0, 0.673]
Similarly, variability in metabolite mass balances can also be checked with flux variability analysis.
In [19]: model.metabolites.pyr_c.summary(fva=0.95)
PRODUCING REACTIONS -- Pyruvate (pyr_c)
---------------------------------------
% FLUX RANGE RXN ID REACTION
---- ------ ------------ ---------- ----------------------------------------
50% 10 [1.25, 18.8] PYK adp_c + h_c + pep_c --> atp_c + pyr_c
50% 10 [9.5, 10] GLCpts glc__D_e + pep_c --> g6p_c + pyr_c
0% 0 [0, 8.75] ME1 mal__L_c + nad_c --> co2_c + nadh_c +...
In these summary methods, the values are reported as a the center point +/- the range of the FVA solution, calcu-
lated from the maximum and minimum values.
Parsimonious FBA (often written pFBA) finds a flux distribution which gives the optimal growth rate, but mini-
mizes the total sum of flux. This involves solving two sequential linear programs, but is handled transparently by
cobrapy. For more details on pFBA, please see Lewis et al. (2010).
In [20]: model.objective = 'Biomass_Ecoli_core'
fba_solution = model.optimize()
pfba_solution = cobra.flux_analysis.pfba(model)
Geometric FBA finds a unique optimal flux distribution which is central to the range of possible fluxes. For more
details on geometric FBA, please see K Smallbone, E Simeonidis (2009).
In [22]: geometric_fba_sol = cobra.flux_analysis.geometric_fba(model)
geometric_fba_sol
Out[22]: <Solution 0.000 at 0x116dfcc88>
Simulating Deletions
import cobra.test
from cobra.flux_analysis import (
single_gene_deletion, single_reaction_deletion, double_gene_deletion,
double_reaction_deletion)
cobra_model = cobra.test.create_test_model("textbook")
ecoli_model = cobra.test.create_test_model("ecoli")
A commonly asked question when analyzing metabolic models is what will happen if a certain reaction was not
allowed to have any flux at all. This can tested using cobrapy by
In [2]: print('complete model: ', cobra_model.optimize())
with cobra_model:
cobra_model.reactions.PFK.knock_out()
print('pfk knocked out: ', cobra_model.optimize())
complete model: <Solution 0.874 at 0x1118cc898>
pfk knocked out: <Solution 0.704 at 0x1118cc5c0>
For evaluating genetic manipulation strategies, it is more interesting to examine what happens if given genes
are knocked out as doing so can affect no reactions in case of redundancy, or more reactions if gene when is
participating in more than one reaction.
In [3]: print('complete model: ', cobra_model.optimize())
with cobra_model:
cobra_model.genes.b1723.knock_out()
print('pfkA knocked out: ', cobra_model.optimize())
cobra_model.genes.b3916.knock_out()
print('pfkB knocked out: ', cobra_model.optimize())
complete model: <Solution 0.874 at 0x1108b81d0>
pfkA knocked out: <Solution 0.874 at 0x1108b80b8>
pfkB knocked out: <Solution 0.704 at 0x1108b8128>
23
cobra Documentation, Release 0.13.3
Double deletions run in a similar way. Passing in return_frame=True will cause them to format the results
as a pandas.DataFrame.
In [7]: double_gene_deletion(
cobra_model, cobra_model.genes[-5:], return_frame=True).round(4)
Out[7]: b2464 b0008 b2935 b2465 b3919
b2464 0.8739 0.8648 0.8739 0.8739 0.704
b0008 0.8648 0.8739 0.8739 0.8739 0.704
b2935 0.8739 0.8739 0.8739 0.0000 0.704
b2465 0.8739 0.8739 0.0000 0.8739 0.704
b3919 0.7040 0.7040 0.7040 0.7040 0.704
By default, the double deletion function will automatically use multiprocessing, splitting the task over up to 4
cores if they are available. The number of cores can be manually specified as well. Setting use of a single core
will disable use of the multiprocessing library, which often aids debugging.
In [8]: start = time() # start timer()
double_gene_deletion(
ecoli_model, ecoli_model.genes[:300], number_of_processes=2)
t1 = time() - start
print("Double gene deletions for 200 genes completed in "
"%.2f sec with 2 cores" % t1)
Production envelopes
Production envelopes (aka phenotype phase planes) will show distinct phases of optimal growth with different use
of two different substrates. For more information, see Edwards et al.
Cobrapy supports calculating these production envelopes and they can easily be plotted using your favorite plotting
package. Here, we will make one for the “textbook” E. coli core model and demonstrate plotting using matplotlib.
In [1]: import cobra.test
from cobra.flux_analysis import production_envelope
model = cobra.test.create_test_model("textbook")
We want to make a phenotype phase plane to evaluate uptakes of Glucose and Oxygen.
In [2]: prod_env = production_envelope(model, ["EX_glc__D_e", "EX_o2_e"])
In [3]: prod_env.head()
Out[3]: carbon_source carbon_yield_maximum carbon_yield_minimum flux_maximum \
0 EX_glc__D_e 1.442300e-13 0.0 0.000000
1 EX_glc__D_e 1.310050e+00 0.0 0.072244
2 EX_glc__D_e 2.620100e+00 0.0 0.144488
3 EX_glc__D_e 3.930150e+00 0.0 0.216732
4 EX_glc__D_e 5.240200e+00 0.0 0.288975
EX_o2_e
0 -60.000000
1 -56.842105
2 -53.684211
3 -50.526316
4 -47.368421
If we specify the carbon source, we can also get the carbon and mass yield. For example, temporarily setting the
objective to produce acetate instead we could get production envelope as follows and pandas to quickly plot the
results.
27
cobra Documentation, Release 0.13.3
Previous versions of cobrapy included more tailored plots for phase planes which have now been dropped in order
to improve maintainability and enhance the focus of cobrapy. Plotting for cobra models is intended for another
package.
Flux sampling
The easiest way to get started with flux sampling is using the sample function in the flux_analysis sub-
module. sample takes at least two arguments: a cobra model and the number of samples you want to generate.
In [1]: from cobra.test import create_test_model
from cobra.flux_analysis import sample
model = create_test_model("textbook")
s = sample(model, 100)
s.head()
Out[1]: ACALD ACALDt ACKr ACONTa ACONTb ACt2r ADK1 \
0 -0.577302 -0.149662 -0.338001 10.090870 10.090870 -0.338001 0.997694
1 -0.639279 -0.505704 -0.031929 10.631865 10.631865 -0.031929 4.207702
2 -1.983410 -0.434676 -0.408318 11.046294 11.046294 -0.408318 5.510960
3 -1.893551 -0.618887 -0.612598 8.879426 8.879426 -0.612598 6.194372
4 -1.759520 -0.321021 -0.262520 10.801480 10.801480 -0.262520 4.815146
[5 rows x 95 columns]
By default sample uses the optgp method based on the method presented here as it is suited for larger models and
can run in parallel. By default the sampler uses a single process. This can be changed by using the processes
argument.
29
cobra Documentation, Release 0.13.3
In general setting up the sampler is expensive since initial search directions are generated by solving many linear
programming problems. Thus, we recommend to generate as many samples as possible in one go. However, this
might require finer control over the sampling procedure as described in the following section.
The sampling process can be controlled on a lower level by using the sampler classes directly.
In [4]: from cobra.flux_analysis.sampling import OptGPSampler, ACHRSampler
Both sampler classes have standardized interfaces and take some additional argument. For instance the
thinning factor. “Thinning” means only recording samples every n iterations. A higher thinning factors mean
less correlated samples but also larger computation times. By default the samplers use a thinning factor of 100
which creates roughly uncorrelated samples. If you want less samples but better mixing feel free to increase this
parameter. If you want to study convergence for your own model you might want to set it to 1 to obtain all iterates.
In [5]: achr = ACHRSampler(model, thinning=10)
OptGPSampler has an additional processes argument specifying how many processes are used to create
parallel sampling chains. This should be in the order of your CPU cores for maximum efficiency. As noted before
class initialization can take up to a few minutes due to generation of initial search directions. Sampling on the
other hand is quick.
In [6]: optgp = OptGPSampler(model, processes=4)
Both samplers have a sample function that generates samples from the initialized object and act like the sample
function described above, only that this time it will only accept a single argument, the number of samples. For
OptGPSampler the number of samples should be a multiple of the number of processes, otherwise it will be
increased to the nearest multiple automatically.
In [7]: s1 = achr.sample(100)
s2 = optgp.sample(100)
You can call sample repeatedly and both samplers are optimized to generate large amount of samples without
falling into “numerical traps”. All sampler objects have a validate function in order to check if a set of points
are feasible and give detailed information about feasibility violations in a form of a short code denoting feasibility.
Here the short code is a combination of any of the following letters:
• “v” - valid point
• “l” - lower bound violation
Even though most models are numerically stable enought that the sampler should only generate valid samples we
still urge to check this. validate is pretty fast and works quickly even for large models and many samples. If
you find invalid samples you do not necessarily have to rerun the entire sampling but can exclude them from the
sample DataFrame.
In [10]: s1_valid = s1[achr.validate(s1) == "v"]
len(s1_valid)
Out[10]: 100
Sampler objects are made for generating billions of samples, however using the sample function might quickly
fill up your RAM when working with genome-scale models. Here, the batch method of the sampler objects
might come in handy. batch takes two arguments, the number of samples in each batch and the number of
batches. This will make sense with a small example.
Let’s assume we want to quantify what proportion of our samples will grow. For that we might want to generate
10 batches of 50 samples each and measure what percentage of the individual 100 samples show a growth rate
larger than 0.1. Finally, we want to calculate the mean and standard deviation of those individual percentages.
In [11]: counts = [np.mean(s.Biomass_Ecoli_core > 0.1) for s in optgp.batch(100, 10)]
print("Usually {:.2f}% +- {:.2f}% grow...".format(
np.mean(counts) * 100.0, np.std(counts) * 100.0))
Usually 10.90% +- 3.83% grow...
Flux sampling will respect additional contraints defined in the model. For instance we can add a constraint
enforcing growth in asimilar manner as the section before.
In [12]: co = model.problem.Constraint(model.reactions.Biomass_Ecoli_core.flux_expression, lb=0.1)
model.add_cons_vars([co])
Note that this is only for demonstration purposes. usually you could set the lower bound of the reaction directly
instead of creating a new constraint.
In [13]: s = sample(model, 10)
print(s.Biomass_Ecoli_core)
0 0.118106
1 0.120205
2 0.206187
3 0.198633
4 0.206575
5 0.119032
6 0.119231
7 0.127219
8 0.120086
9 0.182586
Name: Biomass_Ecoli_core, dtype: float64
Loopless FBA
The goal of this procedure is identification of a thermodynamically consistent flux state without loops, as implied
by the name. You can find a more detailed description in the method section at the end of the notebook.
In [1]: %matplotlib inline
import plot_helper
import cobra.test
from cobra import Reaction, Metabolite, Model
from cobra.flux_analysis.loopless import add_loopless, loopless_solution
from cobra.flux_analysis import pfba
Classical loopless approaches as described below are computationally expensive to solve due to the added mixed-
integer constraints. A much faster, and pragmatic approach is instead to post-process flux distributions to simply
set fluxes to zero wherever they can be zero without changing the fluxes of any exchange reactions in the model.
CycleFreeFlux is an algorithm that can be used to achieve this and in cobrapy it is implemented in the cobra.
flux_analysis.loopless_solution function. loopless_solution will identify the closest flux
distribution (using only loopless elementary flux modes) to the original one. Note that this will not remove loops
which you explicitly requested, for instance by forcing a loop reaction to carry non-zero flux.
Using a larger model than the simple example above, this can be demonstrated as follows
In [2]: salmonella = cobra.test.create_test_model('salmonella')
nominal = salmonella.optimize()
loopless = loopless_solution(salmonella)
In [3]: import pandas
df = pandas.DataFrame(dict(loopless=loopless.fluxes, nominal=nominal.fluxes))
In [4]: df.plot.scatter(x='loopless', y='nominal')
Out[4]: <matplotlib.axes._subplots.AxesSubplot at 0x10f7cb3c8>
33
cobra Documentation, Release 0.13.3
30
20
10
nominal
0
10
20
30
30 20 10 0 10 20 30
loopless
This functionality can also be used in FVA by using the loopless=True argument to avoid getting high flux
ranges for reactions that essentially only can reach high fluxes if they are allowed to participate in loops (see the
simulation notebook) leading to much narrower flux ranges.
Cobrapy also includes the “classical” loopless formulation by Schellenberger et. al. implemented in cobra.
flux_analysis.add_loopless modify the model with additional mixed-integer constraints that make
thermodynamically infeasible loops impossible. This is much slower than the strategy provided above and should
only be used if one of the two following cases applies:
1. You want to combine a non-linear (e.g. quadratic) objective with the loopless condition
2. You want to force the model to be infeasible in the presence of loops independent of the set reaction bounds.
We will demonstrate this with a toy model which has a simple loop cycling A → B → C → A, with A allowed to
enter the system and C allowed to leave. A graphical view of the system is drawn below:
In [5]: plot_helper.plot_loop()
B
v1 v2
EX_A DM_C
A v3 C
In [6]: model = Model()
model.add_metabolites([Metabolite(i) for i in "ABC"])
model.add_reactions([Reaction(i) for i in ["EX_A", "DM_C", "v1", "v2", "v3"]])
model.reactions.EX_A.add_metabolites({"A": 1})
model.reactions.DM_C.add_metabolites({"C": -1})
model.objective = 'DM_C'
While this model contains a loop, a flux state exists which has no flux through reaction v3 , and is identified by
loopless FBA.
In [7]: with model:
add_loopless(model)
solution = model.optimize()
print("loopless solution: status = " + solution.status)
print("loopless solution flux: v3 = %.1f" % solution.fluxes["v3"])
loopless solution: status = optimal
loopless solution flux: v3 = 0.0
If there is no forced flux through a loopless reaction, parsimonious FBA will also have no flux through the loop.
In [8]: solution = pfba(model)
print("parsimonious solution: status = " + solution.status)
print("loopless solution flux: v3 = %.1f" % solution.fluxes["v3"])
parsimonious solution: status = optimal
loopless solution flux: v3 = 0.0
However, if flux is forced through v3 , then there is no longer a feasible loopless solution, but the parsimonious
solution will still exist.
In [9]: model.reactions.v3.lower_bound = 1
with model:
add_loopless(model)
try:
solution = model.optimize()
except:
print('model is infeasible')
model is infeasible
cobra/util/solver.py:398 UserWarning: solver status is 'infeasible'
8.3 Method
loopless_solution is based on a given reference flux distribution. It will look for a new flux distribution
with the following requirements:
1. The objective value is the same as in the reference fluxes.
2. All exchange fluxes have the same value as in the reference distribution.
3. All non-exchange fluxes have the same sign (flow in the same direction) as the reference fluxes.
4. The sum of absolute non-exchange fluxes is minimized.
As proven in the original publication this will identify the “least-loopy” solution closest to the reference fluxes.
If you are using add_loopless this will use the method described here. In summary, it will add 𝐺 ≈ ∆𝐺
proxy variables and make loops thermodynamically infeasible. This is achieved by the following formulation.
𝑡𝑜
maximize 𝑣𝑜𝑏𝑗
𝑠.𝑡.𝑆𝑣 = 0
𝑙𝑏𝑗 ≤ 𝑣𝑗 ≤ 𝑢𝑏𝑗
− 𝑀 · (1 − 𝑎𝑖 ) ≤ 𝑣𝑖 ≤ 𝑀 · 𝑎𝑖
− 1000𝑎𝑖 + (1 − 𝑎𝑖 ) ≤ 𝐺𝑖 ≤ −𝑎𝑖 + 1000(1 − 𝑎𝑖 )
𝑁𝑖𝑛𝑡 𝐺 = 0
𝑎𝑖 ∈ {0, 1}(8.1)
𝑆𝑣 = 0
−𝑀 · (1 − 𝑎𝑖 ) ≤ 𝑣𝑖 ≤ 𝑀 · 𝑎𝑖
𝑁𝑖𝑛𝑡 𝐺 = 0
Here the index j runs over all reactions and the index i only over internal ones. 𝑎𝑖 are indicator variables which
equal one if the reaction flux flows in hte forward direction and 0 otherwise. They are used to force the G proxies
to always carry the opposite sign of the flux (as it is the case for the “real” ∆𝐺 values). 𝑁𝑖𝑛𝑡 is the nullspace
matrix for internal reactions and is used to find thermodinamically “correct” values for G.
Gapfillling
Model gap filling is the task of figuring out which reactions have to be added to a model to make it feasible.
Several such algorithms have been reported e.g. Kumar et al. 2009 and Reed et al. 2006. Cobrapy has a gap
filling implementation that is very similar to that of Reed et al. where we use a mixed-integer linear program to
figure out the smallest number of reactions that need to be added for a user-defined collection of reactions, i.e. a
universal model. Briefly, the problem that we try to solve is
Minimize:
∑︁
𝑐𝑖 * 𝑧𝑖
𝑖
subject to
𝑆𝑣 = 0
𝑣⋆ ≥ 𝑡
𝑙𝑖 ≤ 𝑣𝑖 ≤ 𝑢𝑖
𝑣𝑖 = 0 if 𝑧𝑖 = 0
Where l, u are lower and upper bounds for reaction i and z is an indicator variable that is zero if the reaction is not
used and otherwise 1, c is a user-defined cost associated with using the ith reaction, 𝑣 ⋆ is the flux of the objective
and t a lower bound for that objective. To demonstrate, let’s take a model and remove some essential reactions
from it.
In [1]: import cobra.test
from cobra.flux_analysis import gapfill
model = cobra.test.create_test_model("salmonella")
In this model D-Fructose-6-phosphate is an essential metabolite. We will remove all the reactions using it, and at
them to a separate model.
In [2]: universal = cobra.Model("universal_reactions")
for i in [i.id for i in model.metabolites.f6p_c.reactions]:
reaction = model.reactions.get_by_id(i)
universal.add_reaction(reaction.copy())
model.remove_reactions([reaction])
37
cobra Documentation, Release 0.13.3
Out[3]: 0.0
We will use can use the model’s original objective, growth, to figure out which of the removed reactions are
required for the model be feasible again. This is very similar to making the ‘no-growth but growth (NGG)’
predictions of Kumar et al. 2009.
In [4]: solution = gapfill(model, universal, demand_reactions=False)
for reaction in solution[0]:
print(reaction.id)
GF6PTA
F6PP
TKT2
FBP
MAN6PI
We can obtain multiple possible reaction sets by having the algorithm go through multiple iterations.
In [5]: result = gapfill(model, universal, demand_reactions=False, iterations=4)
for i, entries in enumerate(result):
print("---- Run %d ----" % (i + 1))
for e in entries:
print(e.id)
---- Run 1 ----
GF6PTA
F6PP
TKT2
FBP
MAN6PI
---- Run 2 ----
GF6PTA
TALA
PGI
F6PA
MAN6PI
---- Run 3 ----
GF6PTA
F6PP
TKT2
FBP
MAN6PI
---- Run 4 ----
GF6PTA
TALA
PGI
F6PA
MAN6PI
We can also instead of using the original objective, specify a given metabolite that we want the model to be able
to produce.
In [6]: with model:
model.objective = model.add_boundary(model.metabolites.f6p_c, type='demand')
solution = gapfill(model, universal)
for reaction in solution[0]:
print(reaction.id)
FBP
Finally, note that using mixed-integer linear programming is computationally quite expensive and for larger mod-
els you may want to consider alternative gap filling methods and reconstruction methods.
38 Chapter 9. Gapfillling
CHAPTER 10
Growth media
The availability of nutrients has a major impact on metabolic fluxes and cobrapy provides some helpers to
manage the exchanges between the external environment and your metabolic model. In experimental settings
the “environment” is usually constituted by the growth medium, ergo the concentrations of all metabolites and
co-factors available to the modeled organism. However, constraint-based metabolic models only consider fluxes.
Thus, you will first have to translate your concentrations into fluxes. For instance by assuming that 1 gDW of
your organism cannot consume the entire concentration of a metabolite in 24h which gives you an estimate of the
upper exchange flux of concentration / (1 gDW * 24 h). If you have direct measurement of exchange
fluxes you can of course use those as well (and those will be much more accurate).
The current growth medium of a model is managed by the medium attribute.
In [1]: from cobra.test import create_test_model
model = create_test_model("textbook")
model.medium
Out[1]: {'EX_co2_e': 1000.0,
'EX_glc__D_e': 10.0,
'EX_h2o_e': 1000.0,
'EX_h_e': 1000.0,
'EX_nh4_e': 1000.0,
'EX_o2_e': 1000.0,
'EX_pi_e': 1000.0}
This will return a dictionary that contains all active exchange fluxes (the ones having non-zero flux bounds). Right
now we see that we have enabled aerobic growth. You can modify a growth medium of a model by assigning a
dictionary to model.medium that maps exchange reactions to their respective upper import bounds. For now let
us enforce anaerobic growth by shutting off the oxygen import.
In [2]: medium = model.medium
medium["EX_o2_e"] = 0.0
model.medium = medium
model.medium
Out[2]: {'EX_co2_e': 1000.0,
'EX_glc__D_e': 10.0,
'EX_h2o_e': 1000.0,
'EX_h_e': 1000.0,
'EX_nh4_e': 1000.0,
'EX_pi_e': 1000.0}
39
cobra Documentation, Release 0.13.3
As we can see oxygen import is now removed from the list of active exchanges and we can verify that this also
leads to a lower growth rate.
In [3]: model.slim_optimize()
Out[3]: 0.21166294973530736
Setting the growth medium also connects to the context manager, so you can set a specific growth medium in a
reversible manner.
In [4]: model = create_test_model("textbook")
with model:
medium = model.medium
medium["EX_o2_e"] = 0.0
model.medium = medium
print(model.slim_optimize())
print(model.slim_optimize())
model.medium
0.21166294973530736
0.8739215069684102
Out[4]: {'EX_co2_e': 1000.0,
'EX_glc__D_e': 10.0,
'EX_h2o_e': 1000.0,
'EX_h_e': 1000.0,
'EX_nh4_e': 1000.0,
'EX_o2_e': 1000.0,
'EX_pi_e': 1000.0}
So the medium change is only applied within the with block and reverted automatically.
In some cases you might be interested in the smallest growth medium that can maintain a specific growth rate,
the so called “minimal medium”. For this we provide the function minimal_medium which by default obtains
the medium with the lowest total import flux. This function needs two arguments: the model and the minimum
growth rate (or other objective) the model has to achieve.
In [5]: from cobra.medium import minimal_medium
max_growth = model.slim_optimize()
minimal_medium(model, max_growth)
Out[5]: EX_glc__D_e 10.000000
EX_nh4_e 4.765319
EX_o2_e 21.799493
EX_pi_e 3.214895
dtype: float64
alternative solutions. Let us try that with our model and also use the open_exchanges argument which will
assign a large upper bound to all import reactions in the model. The return type will be a pandas.DataFrame.
In [7]: minimal_medium(model, 0.8, minimize_components=8, open_exchanges=True)
Out[7]: 0 1 2 3 4 \
EX_fru_e 0.000000 0.000000 523.104557 0.000000 0.000000
EX_glc__D_e 0.000000 0.000000 0.000000 523.104557 521.357767
EX_gln__L_e 0.000000 0.000000 0.000000 0.000000 40.698058
EX_glu__L_e 23.468185 348.101944 83.995843 83.995843 0.000000
EX_mal__L_e 1000.000000 0.000000 0.000000 0.000000 0.000000
EX_nh4_e 0.000000 0.000000 0.000000 0.000000 0.000000
EX_o2_e 0.000000 500.000000 0.000000 0.000000 0.000000
EX_pi_e 15.667461 66.431529 56.667310 56.667310 54.913419
5
EX_fru_e 0.000000
EX_glc__D_e 519.750758
EX_gln__L_e 0.000000
EX_glu__L_e 0.000000
EX_mal__L_e 0.000000
EX_nh4_e 81.026921
EX_o2_e 0.000000
EX_pi_e 54.664344
So there are 4 alternative solutions in total. One aerobic and three anaerobic ones using different carbon sources.
Apart from exchange reactions there are other types of boundary reactions such as demand or sink reactions.
cobrapy uses various heuristics to identify those and they can be accessed by using the appropriate attribute.
For exchange reactions:
In [8]: ecoli = create_test_model("ecoli")
ecoli.exchanges[0:5]
Out[8]: [<Reaction EX_12ppd__R_e at 0x7f3921088fd0>,
<Reaction EX_12ppd__S_e at 0x7f3921078fd0>,
<Reaction EX_14glucan_e at 0x7f3921078f98>,
<Reaction EX_15dap_e at 0x7f3921078eb8>,
<Reaction EX_23camp_e at 0x7f392107e2b0>]
All boundary reactions (any reaction that consumes or introduces mass into the system) can be obtained with the
boundary attribute:
In [11]: ecoli.boundary[0:10]
Out[11]: [<Reaction DM_4CRSOL at 0x7f3921144b70>,
<Reaction DM_5DRIB at 0x7f3921078b38>,
Solvers
A constraint-based reconstruction and analysis model for biological systems is actually just an application of
a class of discrete optimization problems typically solved with linear, mixed integer or quadratic programming
techniques. Cobrapy does not implement any algorithm to find solutions to such problems but rather creates a
biologically motivated abstraction to these techniques to make it easier to think of how metabolic systems work
without paying much attention to how that formulates to an optimization problem.
The actual solving is instead done by tools such as the free software glpk or commercial tools gurobi and cplex
which are all made available as a common programmers interface via the optlang package.
When you have defined your model, you can switch solver backend by simply assigning to the model.solver
property.
In [1]: import cobra.test
model = cobra.test.create_test_model('textbook')
In [2]: model.solver = 'glpk'
# or if you have cplex installed
model.solver = 'cplex'
For information on how to configure and tune the solver, please see the documentation for optlang project and
note that model.solver is simply an optlang object of class Model.
In [3]: type(model.solver)
Out[3]: optlang.cplex_interface.Model
Cobrapy also contains its own solver interfaces but these are now deprecated and will be removed completely in
the near future. For documentation of how to use these, please refer to older documentation.
43
cobra Documentation, Release 0.13.3
Thanks to the use of symbolic expressions via the optlang mathematical modeling package, it is relatively straight-
forward to add new variables, constraints and advanced objectives that cannot be easily formulated as a combi-
nation of different reaction and their corresponding upper and lower bounds. Here we demonstrate this optlang
functionality which is exposed via the model.solver.interface.
12.1 Constraints
Suppose we want to ensure that two reactions have the same flux in our model. We can add this criteria as
constraint to our model using the optlang solver interface by simply defining the relevant expression as follows.
In [1]: import cobra.test
model = cobra.test.create_test_model('textbook')
In [2]: same_flux = model.problem.Constraint(
model.reactions.FBA.flux_expression - model.reactions.NH4t.flux_expression,
lb=0,
ub=0)
model.add_cons_vars(same_flux)
The flux for our reaction of interest is obtained by the model.reactions.FBA.flux_expression which
is simply the sum of the forward and reverse flux, i.e.,
In [3]: model.reactions.FBA.flux_expression
Out[3]: 1.0*FBA - 1.0*FBA_reverse_84806
Now I can maximize growth rate whilst the fluxes of reactions ‘FBA’ and ‘NH4t’ are constrained to be (near)
identical.
In [4]: solution = model.optimize()
print(solution.fluxes['FBA'], solution.fluxes['NH4t'],
solution.objective_value)
4.66274904774 4.66274904774 0.855110960926157
It is also possible to add many constraints at once. For large models, with constraints involving many reactions,
the efficient way to do this is to first build a dictionary of the linear coefficients for every flux, and then add the
constraint at once. For example, suppose we want to add a constrain on the sum of the absolute values of every
flux in the network to be less than 100:
45
cobra Documentation, Release 0.13.3
12.2 Objectives
Simple objective such as the maximization of the flux through one or more reactions can conveniently be done by
simply assigning to the model.objective property as we have seen in previous chapters, e.g.,
In [5]: model = cobra.test.create_test_model('textbook')
with model:
model.objective = {model.reactions.Biomass_Ecoli_core: 1}
model.optimize()
print(model.reactions.Biomass_Ecoli_core.flux)
0.8739215069684307
But suppose we need a more complicated objective, such as minimizing the Euclidean distance of the solution to
the origin minus another variable, while subject to additional linear constraints. This is an objective function with
both linear and quadratic components.
Consider the example problem:
min 12 𝑥2 + 𝑦 2 − 𝑦
(︀ )︀
subject to
𝑥+𝑦 =2
𝑥≥0
𝑦≥0
This (admittedly very artificial) problem can be visualized graphically where the optimum is indicated by the blue
dot on the line of feasible solutions.
In [7]: %matplotlib inline
import plot_helper
plot_helper.plot_qp2()
2.0
1.0
1.0 2.0
We return to the textbook model and set the solver to one that can handle quadratic objectives such as cplex. We
then add the linear constraint that the sum of our x and y reactions, that we set to FBA and NH4t, must equal 2.
In [8]: model.solver = 'cplex'
sum_two = model.problem.Constraint(
model.reactions.FBA.flux_expression + model.reactions.NH4t.flux_expression,
lb=2,
ub=2)
model.add_cons_vars(sum_two)
12.3 Variables
We can also create additional variables to facilitate studying the effects of new constraints and variables. Suppose
we want to study the difference in flux between nitrogen and carbon uptake whilst we block other reactions. For
this it will may help to add another variable representing this difference.
In [11]: model = cobra.test.create_test_model('textbook')
difference = model.problem.Variable('difference')
12.3. Variables 47
cobra Documentation, Release 0.13.3
model.reactions.EX_nh4_e.flux_expression - difference,
lb=0,
ub=0)
model.add_cons_vars([difference, constraint])
Now we can access that difference directly during our knock-out exploration by looking at its primal value.
In [13]: for reaction in model.reactions[:5]:
with model:
reaction.knock_out()
model.optimize()
print(model.solver.variables.difference.primal)
-5.234680806802543
-5.2346808068025386
-5.234680806802525
-1.8644444444444337
-1.8644444444444466
This example demonstrates using COBRA toolbox commands in MATLAB from python through pymatbridge.
In [1]: %load_ext pymatbridge
Starting MATLAB on ZMQ socket ipc:///tmp/pymatbridge-57ff5429-02d9-4e1a-8ed0-44e391fb0df7
Send 'exit' command to kill the server
...MATLAB started and connected!
In [2]: import cobra.test
m = cobra.test.create_test_model("textbook")
The model_to_pymatbridge function will send the model to the workspace with the given variable name.
In [3]: from cobra.io.mat import model_to_pymatbridge
model_to_pymatbridge(m, variable_name="model")
Now in the MATLAB workspace, the variable name ‘model’ holds a COBRA toolbox struct encoding the model.
In [4]: %%matlab
model
model =
49
cobra Documentation, Release 0.13.3
Commands from the COBRA toolbox can now be run on the model
In [6]: %%matlab
optimizeCbModel(model)
ans =
x: [95x1 double]
f: 0.8739
y: [71x1 double]
w: [95x1 double]
stat: 1
origStat: 5
solver: 'glpk'
time: 3.2911
FBA in the COBRA toolbox should give the same result as cobrapy (but maybe just a little bit slower :))
In [7]: %time
m.optimize().f
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 5.48 µs
Out[7]: 0.8739215069684909
FAQ
This document will address frequently asked questions not addressed in other pages of the documentation.
try:
model.metabolites.get_by_id(model.metabolites[0].id)
except KeyError as e:
print(repr(e))
The Model.repair function will rebuild the necessary indexes
In [2]: model.repair()
model.metabolites.get_by_id(model.metabolites[0].id)
Out[2]: <Metabolite test_dcaACP_c at 0x110f09630>
51
cobra Documentation, Release 0.13.3
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.
Reaction.reversibility is a property in cobra which is computed when it is requested from the lower
and upper bounds.
In [4]: model = cobra.test.create_test_model()
model.reactions.get_by_id("PGI").reversibility
Out[4]: True
The way to change the reversibility is to change the bounds to make the reaction irreversible.
In [6]: model.reactions.get_by_id("PGI").lower_bound = 10
model.reactions.get_by_id("PGI").reversibility
Out[6]: False
With optlang solvers, the LP formulation of a model is obtained by it’s string representation. All solvers behave
the same way.
In [7]: with open('test.lp', 'w') as out:
out.write(str(model.solver))
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.
cobrapy works well with the escher package, which is well suited to this purpose. Consult the escher documenta-
tion for examples.
This page is the top-level of your generated API documentation. Below is a list of all items that are documented
here.
15.1 cobra
15.1.1 Subpackages
cobra.core
Submodules
cobra.core.dictlist
Module Contents
class cobra.core.dictlist.DictList(*args)
A combined dict and list
This object behaves like a list, but has the O(1) speed benefits of a dict when looking up elements by their
id.
__init__(*args)
Instantiate a combined dict and list.
Parameters args (iterable) – iterable as single argument to create new DictList from
has_id(id)
_check(id)
make sure duplicate id’s are not added. This function is called before adding in elements.
_generate_index()
rebuild the _dict index
get_by_id(id)
return the element with a matching id
55
cobra Documentation, Release 0.13.3
list_attr(attribute)
return a list of the given attribute for every object
get_by_any(iterable)
Get a list of members using several different ways of indexing
Parameters iterable (list (if not, turned into single element
list)) – list where each element is either int (referring to an index in in this DictList),
string (a id of a member in this DictList) or member of this DictList for pass-through
Returns a list of members
Return type list
query(search_function, attribute=None)
Query the list
Parameters
• search_function (a string, regular expression or function)
– Used to find the matching elements in the list. - a regular expression (possibly
compiled), in which case the given attribute of the object should match the regular
expression. - a function which takes one argument and returns True for desired values
• attribute (string or None) – the name attribute of the object to passed as
argument to the search_function. If this is None, the object itself is used.
Returns a new list of objects which match the query
Return type DictList
Examples
_replace_on_id(new_object)
Replace an object by another with the same id.
append(object)
append object to end
union(iterable)
adds elements with id’s not already in the model
extend(iterable)
extend list by appending elements from the iterable
_extend_nocheck(iterable)
extends without checking for uniqueness
This function should only be used internally by DictList when it can guarantee elements are already
unique (as in when coming from self or other DictList). It will be faster because it skips these checks.
__sub__(other)
x.__sub__(y) <==> x - y
Parameters other (iterable) – other must contain only unique id’s present in the list
__isub__(other)
x.__sub__(y) <==> x -= y
Parameters other (iterable) – other must contain only unique id’s present in the list
__add__(other)
x.__add__(y) <==> x + y
Parameters other (iterable) – other must contain only unique id’s which do not inter-
sect with self
__iadd__(other)
x.__iadd__(y) <==> x += y
Parameters other (iterable) – other must contain only unique id’s whcih do not inter-
sect with self
__reduce__()
__getstate__()
gets internal state
This is only provided for backwards compatibility so older versions of cobrapy can load pickles gen-
erated with cobrapy. In reality, the “_dict” state is ignored when loading a pickle
__setstate__(state)
sets internal state
Ignore the passed in state and recalculate it. This is only for compatibility with older pickles which
did not correctly specify the initialization class
index(id, *args)
Determine the position in the list
id: A string or a Object
__contains__(object)
DictList.__contains__(object) <==> object in DictList
object: str or Object
__copy__()
insert(index, object)
insert object before index
pop(*args)
remove and return item at index (default last).
add(x)
Opposite of remove. Mirrors set.add
remove(x)
reverse()
reverse IN PLACE
sort(cmp=None, key=None, reverse=False)
stable sort IN PLACE
cmp(x, y) -> -1, 0, 1
__getitem__(i)
__setitem__(i, y)
__delitem__(index)
__getslice__(i, j)
__setslice__(i, j, y)
15.1. cobra 57
cobra Documentation, Release 0.13.3
__delslice__(i, j)
__getattr__(attr)
__dir__()
cobra.core.formula
Module Contents
class cobra.core.formula.Formula(formula=None)
Describes a Chemical Formula
Parameters formula (string) – A legal formula string contains only letters and numbers.
__init__(formula=None)
__add__(other_formula)
Combine two molecular formulas.
Parameters other_formula (Formula, str) – string for a chemical formula
Returns The combined formula
Return type Formula
parse_composition()
Breaks the chemical formula down by element.
weight()
Calculate the mol mass of the compound
Returns the mol mass
Return type float
cobra.core.gene
Module Contents
Returns True if the gene reaction rule is true with the given knockouts otherwise false
Return type bool
class cobra.core.gene.GPRCleaner
Parses compiled ast of a gene_reaction_rule and identifies genes
Parts of the tree are rewritten to allow periods in gene ID’s and bitwise boolean operations
__init__()
visit_Name(node)
visit_BinOp(node)
cobra.core.gene.parse_gpr(str_expr)
parse gpr into AST
Parameters str_expr (string) – string with the gene reaction rule to parse
Returns elements ast_tree and gene_ids as a set
Return type tuple
class cobra.core.gene.Gene(id=None, name="", functional=True)
A Gene in a cobra model
Parameters
• id (string) – The identifier to associate the gene with
• name (string) – A longer human readable name for the gene
• functional (bool) – Indicates whether the gene is functional. If it is not functional
then it cannot be used in an enzyme complex nor can its products be used.
__init__(id=None, name="", functional=True)
functional()
A flag indicating if the gene is functional.
Changing the flag is reverted upon exit if executed within the model as context.
functional(value)
knock_out()
Knockout gene by marking it as non-functional and setting all associated reactions bounds to zero.
The change is reverted upon exit if executed within the model as context.
remove_from_model(model=None, make_dependent_reactions_nonfunctional=True)
Removes the association
Parameters
• model (cobra model) – The model to remove the gene from
• make_dependent_reactions_nonfunctional (bool) – If True then re-
place the gene with ‘False’ in the gene association, else replace the gene with ‘True’
Deprecated since version 0.4: Use cobra.manipulation.delete_model_genes to simulate knockouts and
cobra.manipulation.remove_genes to remove genes from the model.
_repr_html_()
15.1. cobra 59
cobra Documentation, Release 0.13.3
cobra.core.metabolite
Module Contents
Warning:
• Accessing shadow prices through a Solution object is the safer, preferred, and only guaran-
teed to be correct way. You can see how to do so easily in the examples.
• Shadow price is retrieved from the currently defined self._model.solver. The solver status is
checked but there are no guarantees that the current solver state is the one you are looking
for.
• If you modify the underlying model after an optimization, you will retrieve the old optimiza-
tion values.
Raises
• RuntimeError – If the underlying model was never optimized beforehand or the
metabolite is not part of a model.
• OptimizationError – If the solver status is anything other than ‘optimal’.
Examples
remove_from_model(destructive=False)
Removes the association from self.model
The change is reverted upon exit when using the model as a context.
Parameters destructive (bool) – If False then the metabolite is removed from all
associated reactions. If True then all associated reactions are removed from the Model.
summary(solution=None, threshold=0.01, fva=None, names=False, floatfmt=".3g")
Print a summary of the production and consumption fluxes.
This method requires the model for which this metabolite is a part to be solved.
Parameters
• solution (cobra.Solution, optional) – A previously solved model so-
lution to use for generating the summary. If none provided (default), the summary
method will resolve the model. Note that the solution object must match the model,
i.e., changes to the model such as changed bounds, added or removed reactions are
not taken into account by this method.
• threshold (float, optional) – Threshold below which fluxes are not re-
ported.
• fva (pandas.DataFrame, float or None, optional) – Whether or not
to include flux variability analysis in the output. If given, fva should either be a pre-
vious FVA solution matching the model or a float between 0 and 1 representing the
fraction of the optimum objective to be searched.
• names (bool, optional) – Emit reaction and metabolite names rather than iden-
tifiers (default False).
• floatfmt (string, optional) – Format string for floats (default ‘.3g’).
_repr_html_()
cobra.core.model
Module Contents
15.1. cobra 61
cobra Documentation, Release 0.13.3
metabolites
DictList – A DictList where the key is the metabolite identifier and the value a Metabolite
genes
DictList – A DictList where the key is the gene identifier and the value a Gene
solution
Solution – The last obtained solution from optimizing the model.
__setstate__(state)
Make sure all cobra.Objects in the model point to the model.
__getstate__()
Get state for serialization.
Ensures that the context stack is cleared prior to serialization, since partial functions cannot be pickled
reliably.
__init__(id_or_model=None, name=None)
solver()
Get or set the attached solver instance.
The associated the solver object, which manages the interaction with the associated solver, e.g. glpk.
This property is useful for accessing the optimization problem directly and to define additional non-
metabolic constraints.
Examples
solver(value)
description()
description(value)
get_metabolite_compartments()
Return all metabolites’ compartments.
compartments()
compartments(value)
Get or set the dictionary of current compartment descriptions.
Assigning a dictionary to this property updates the model’s dictionary of compartment descriptions
with the new values.
Parameters value (dict) – Dictionary mapping compartments abbreviations to full
names.
Examples
medium()
medium(medium)
Get or set the constraints on the model exchanges.
model.medium returns a dictionary of the bounds for each of the boundary reactions, in the form of
{rxn_id: bound}, where bound specifies the absolute value of the bound in direction of metabolite
creation (i.e., lower_bound for met <–, upper_bound for met –>)
Parameters medium (dictionary-like) – The medium to initialize. medium should
be a dictionary defining {rxn_id: bound} pairs.
__add__(other_model)
Add the content of another model to this model (+).
The model is copied as a new object, with a new model identifier, and copies of all the reactions in the
other model are added to this model. The objective is the sum of the objective expressions for the two
models.
__iadd__(other_model)
Incrementally add the content of another model to this model (+=).
Copies of all the reactions in the other model are added to this model. The objective is the sum of the
objective expressions for the two models.
copy()
Provides a partial ‘deepcopy’ of the Model. All of the Metabolite, Gene, and Reaction objects are
created anew but in a faster fashion than deepcopy
add_metabolites(metabolite_list)
Will add a list of metabolites to the model object and add new constraints accordingly.
The change is reverted upon exit when using the model as a context.
Parameters metabolite_list (A list of cobra.core.Metabolite objects) –
remove_metabolites(metabolite_list, destructive=False)
Remove a list of metabolites from the the object.
The change is reverted upon exit when using the model as a context.
Parameters
• metabolite_list (list) – A list with cobra.Metabolite objects as elements.
• destructive (bool) – If False then the metabolite is removed from all associated
reactions. If True then all associated reactions are removed from the Model.
add_reaction(reaction)
Will add a cobra.Reaction object to the model, if reaction.id is not in self.reactions.
Parameters
• reaction (cobra.Reaction) – The reaction to add
• (0.6) Use ~cobra.Model.add_reactions instead (Deprecated) –
add_boundary(metabolite, type="exchange", reaction_id=None, lb=None, ub=1000.0)
Add a boundary reaction for a given metabolite.
There are three different types of pre-defined boundary reactions: exchange, demand, and sink reac-
tions. An exchange reaction is a reversible, imbalanced reaction that adds to or removes an extracellu-
lar metabolite from the extracellular compartment. A demand reaction is an irreversible reaction that
consumes an intracellular metabolite. A sink is similar to an exchange but specifically for intracellular
metabolites.
If you set the reaction type to something else, you must specify the desired identifier of the created
reaction along with its upper and lower bound. The name will be given by the metabolite name and
the given type.
Parameters
15.1. cobra 63
cobra Documentation, Release 0.13.3
Examples
add_reactions(reaction_list)
Add reactions to the model.
Reactions with identifiers identical to a reaction already in the model are ignored.
The change is reverted upon exit when using the model as a context.
Parameters reaction_list (list) – A list of cobra.Reaction objects
remove_reactions(reactions, remove_orphans=False)
Remove reactions from the model.
The change is reverted upon exit when using the model as a context.
Parameters
• reactions (list) – A list with reactions (cobra.Reaction), or their id’s, to remove
• remove_orphans (bool) – Remove orphaned genes and metabolites from the
model as well
add_cons_vars(what, **kwargs)
Add constraints and variables to the model’s mathematical problem.
Useful for variables and constraints that can not be expressed with reactions and simple lower and
upper bounds.
Additions are reversed upon exit if the model itself is used as context.
Parameters
15.1. cobra 65
cobra Documentation, Release 0.13.3
slim_optimize(error_value=float, message=None)
Optimize model without creating a solution object.
Creating a full solution object implies fetching shadow prices and flux values for all reactions and
metabolites from the solver object. This necessarily takes some time and in cases where only one
or two values are of interest, it is recommended to instead use this function which does not create a
solution object returning only the value of the objective. Note however that the optimize() function
uses efficient means to fetch values so if you need fluxes/shadow prices for more than say 4 reac-
tions/metabolites, then the total speed increase of slim_optimize versus optimize is expected to be
small or even negative depending on how you fetch the values after optimization.
Parameters
• error_value (float, None) – The value to return if optimization failed due to
e.g. infeasibility. If None, raise OptimizationError if the optimization fails.
• message (string) – Error message to use if the model optimization did not suc-
ceed.
Returns The objective value.
Return type float
optimize(objective_sense=None, raise_error=False)
Optimize the model using flux balance analysis.
Parameters
• objective_sense ({None, 'maximize' 'minimize'}, optional) –
Whether fluxes should be maximized or minimized. In case of None, the previous
direction is used.
• raise_error (bool) –
If true, raise an OptimizationError if solver status is not optimal.
Notes
Only the most commonly used parameters are presented here. Additional parameters for cobra.solvers
may be available and specified with the appropriate keyword argument.
repair(rebuild_index=True, rebuild_relationships=True)
Update all indexes and pointers in a model
Parameters
• rebuild_index (bool) – rebuild the indices kept in reactions, metabolites and
genes
• rebuild_relationships (bool) – reset all associations between genes,
metabolites, model and then re-add them.
objective()
Get or set the solver objective
Before introduction of the optlang based problems, this function returned the objective reactions as a
list. With optlang, the objective is not limited a simple linear summation of individual reaction fluxes,
making that return value ambiguous. Henceforth, use cobra.util.solver.linear_reaction_coefficients to
get a dictionary of reactions with their linear coefficients (empty if there are none)
The set value can be dictionary (reactions as keys, linear coefficients as values), string (reaction iden-
tifier), int (reaction index), Reaction or problem.Objective or sympy expression directly interpreted as
objectives.
When using a HistoryManager context, this attribute can be set temporarily, reversed when the exiting
the context.
objective(value)
objective_direction()
Get or set the objective direction.
When using a HistoryManager context, this attribute can be set temporarily, reversed when exiting the
context.
objective_direction(value)
summary(solution=None, threshold=1e-06, fva=None, names=False, floatfmt=".3g")
Print a summary of the input and output fluxes of the model.
Parameters
• solution (cobra.Solution, optional) – A previously solved model so-
lution to use for generating the summary. If none provided (default), the summary
method will resolve the model. Note that the solution object must match the model,
i.e., changes to the model such as changed bounds, added or removed reactions are
not taken into account by this method.
• threshold (float, optional) – Threshold below which fluxes are not re-
ported.
• fva (pandas.DataFrame, float or None, optional) – Whether or not
to include flux variability analysis in the output. If given, fva should either be a pre-
vious FVA solution matching the model or a float between 0 and 1 representing the
fraction of the optimum objective to be searched.
• names (bool, optional) – Emit reaction and metabolite names rather than iden-
tifiers (default False).
• floatfmt (string, optional) – Format string for floats (default ‘.3g’).
__enter__()
Record all future changes to the model, undoing them when a call to __exit__ is received
__exit__(type, value, traceback)
Pop the top context manager and trigger the undo functions
merge(right, prefix_existing=None, inplace=True, objective="left")
Merge two models to create a model with the reactions from both models.
Custom constraints and variables from right models are also copied to left model, however note that,
constraints and variables are assumed to be the same if they have the same name.
right [cobra.Model] The model to add reactions from
prefix_existing [string] Prefix the reaction identifier in the right that already exist in the left model
with this string.
inplace [bool] Add reactions from right directly to left model object. Otherwise, create a new model
leaving the left model untouched. When done within the model as context, changes to the models
are reverted upon exit.
objective [string] One of ‘left’, ‘right’ or ‘sum’ for setting the objective of the resulting model to that
of the corresponding model or the sum of both.
_repr_html_()
cobra.core.object
Module Contents
15.1. cobra 67
cobra Documentation, Release 0.13.3
__init__(id=None, name="")
A simple object with an identifier
Parameters id (None or a string) – the identifier to associate with the object
id()
id(value)
_set_id_with_model(value)
__getstate__()
To prevent excessive replication during deepcopy.
__repr__()
__str__()
cobra.core.reaction
Module Contents
objective_coefficient()
Get the coefficient for this reaction in a linear objective (float)
Assuming that the objective of the associated model is summation of fluxes from a set of reactions,
the coefficient for each reaction can be obtained individually using this property. A more general way
is to use the model.objective property directly.
objective_coefficient(value)
__copy__()
__deepcopy__(memo)
lower_bound()
Get or set the lower bound
Setting the lower bound (float) will also adjust the associated optlang variables associated with the
reaction. Infeasible combinations, such as a lower bound higher than the current upper bound will
update the other bound.
When using a HistoryManager context, this attribute can be set temporarily, reversed when the exiting
the context.
lower_bound(value)
upper_bound()
Get or set the upper bound
Setting the upper bound (float) will also adjust the associated optlang variables associated with the
reaction. Infeasible combinations, such as a upper bound lower than the current lower bound will
update the other bound.
When using a HistoryManager context, this attribute can be set temporarily, reversed when the exiting
the context.
upper_bound(value)
bounds()
Get or set the bounds directly from a tuple
Convenience method for setting upper and lower bounds in one line using a tuple of lower and upper
bound. Invalid bounds will raise an AssertionError.
When using a HistoryManager context, this attribute can be set temporarily, reversed when the exiting
the context.
bounds(value)
flux()
The flux value in the most recent solution.
Flux is the primal value of the corresponding variable in the model.
Warning:
• Accessing reaction fluxes through a Solution object is the safer, preferred, and only guaran-
teed to be correct way. You can see how to do so easily in the examples.
• Reaction flux is retrieved from the currently defined self._model.solver. The solver status is
checked but there are no guarantees that the current solver state is the one you are looking
for.
• If you modify the underlying model after an optimization, you will retrieve the old optimiza-
tion values.
Raises
15.1. cobra 69
cobra Documentation, Release 0.13.3
Examples
reduced_cost()
The reduced cost in the most recent solution.
Reduced cost is the dual value of the corresponding variable in the model.
Warning:
• Accessing reduced costs through a Solution object is the safer, preferred, and only guaran-
teed to be correct way. You can see how to do so easily in the examples.
• Reduced cost is retrieved from the currently defined self._model.solver. The solver status is
checked but there are no guarantees that the current solver state is the one you are looking
for.
• If you modify the underlying model after an optimization, you will retrieve the old optimiza-
tion values.
Raises
• RuntimeError – If the underlying model was never optimized beforehand or the
reaction is not part of a model.
• OptimizationError – If the solver status is anything other than ‘optimal’.
Examples
metabolites()
genes()
gene_reaction_rule()
gene_reaction_rule(new_rule)
gene_name_reaction_rule()
Display gene_reaction_rule with names intead.
Do NOT use this string for computation. It is intended to give a representation of the rule using more
familiar gene names instead of the often cryptic ids.
functional()
All required enzymes for reaction are functional.
Returns True if the gene-protein-reaction (GPR) rule is fulfilled for this reaction, or if reac-
tion is not associated to a model, otherwise False.
Return type bool
x()
The flux through the reaction in the most recent solution.
Flux values are computed from the primal values of the variables in the solution.
y()
The reduced cost of the reaction in the most recent solution.
Reduced costs are computed from the dual values of the variables in the solution.
reversibility()
Whether the reaction can proceed in both directions (reversible)
This is computed from the current upper and lower bounds.
reversibility(value)
boundary()
Whether or not this reaction is an exchange reaction.
Returns True if the reaction has either no products or reactants.
model()
returns the model the reaction is a part of
_update_awareness()
Make sure all metabolites and genes that are associated with this reaction are aware of it.
remove_from_model(remove_orphans=False)
Removes the reaction from a model.
This removes all associations between a reaction the associated model, metabolites and genes.
The change is reverted upon exit when using the model as a context.
Parameters remove_orphans (bool) – Remove orphaned genes and metabolites from
the model as well
delete(remove_orphans=False)
Removes the reaction from a model.
This removes all associations between a reaction the associated model, metabolites and genes.
The change is reverted upon exit when using the model as a context.
Deprecated, use reaction.remove_from_model instead.
Parameters remove_orphans (bool) – Remove orphaned genes and metabolites from
the model as well
__setstate__(state)
Probably not necessary to set _model as the cobra.Model that contains self sets the _model attribute
for all metabolites and genes in the reaction.
However, to increase performance speed we do want to let the metabolite and gene know that they are
employed in this reaction
copy()
Copy a reaction
The referenced metabolites and genes are also copied.
15.1. cobra 71
cobra Documentation, Release 0.13.3
__add__(other)
Add two reactions
The stoichiometry will be the combined stoichiometry of the two reactions, and the gene reaction rule
will be both rules combined by an and. All other attributes (i.e. reaction bounds) will match those of
the first reaction
__iadd__(other)
__sub__(other)
__isub__(other)
__imul__(coefficient)
Scale coefficients in a reaction by a given value
E.g. A -> B becomes 2A -> 2B.
If coefficient is less than zero, the reaction is reversed and the bounds are swapped.
__mul__(coefficient)
reactants()
Return a list of reactants for the reaction.
products()
Return a list of products for the reaction
get_coefficient(metabolite_id)
Return the stoichiometric coefficient of a metabolite.
Parameters metabolite_id (str or cobra.Metabolite) –
get_coefficients(metabolite_ids)
Return the stoichiometric coefficients for a list of metabolites.
Parameters metabolite_ids (iterable) – Containing str or ‘‘cobra.Metabolite‘‘s.
add_metabolites(metabolites_to_add, combine=True, reversibly=True)
Add metabolites and stoichiometric coefficients to the reaction. If the final coefficient for a metabolite
is 0 then it is removed from the reaction.
The change is reverted upon exit when using the model as a context.
Parameters
• metabolites_to_add (dict) – Dictionary with metabolite objects or metabolite
identifiers as keys and coefficients as values. If keys are strings (name of a metabolite)
the reaction must already be part of a model and a metabolite with the given name
must exist in the model.
• combine (bool) – Describes behavior a metabolite already exists in the reaction.
True causes the coefficients to be added. False causes the coefficient to be replaced.
• reversibly (bool) – Whether to add the change to the context to make the change
reversibly or not (primarily intended for internal use).
subtract_metabolites(metabolites, combine=True, reversibly=True)
Subtract metabolites from a reaction.
That means add the metabolites with -1*coefficient. If the final coefficient for a metabolite is 0 then
the metabolite is removed from the reaction.
Notes
Parameters
• metabolites (dict) – Dictionary where the keys are of class Metabolite and the
values are the coefficients. These metabolites will be added to the reaction.
• combine (bool) – Describes behavior a metabolite already exists in the reaction.
True causes the coefficients to be added. False causes the coefficient to be replaced.
• reversibly (bool) – Whether to add the change to the context to make the change
reversibly or not (primarily intended for internal use).
reaction()
Human readable reaction string
reaction(value)
build_reaction_string(use_metabolite_names=False)
Generate a human readable reaction string
check_mass_balance()
Compute mass and charge balance for the reaction
returns a dict of {element: amount} for unbalanced elements. “charge” is treated as an element in this
dict This should be empty for balanced reactions.
compartments()
lists compartments the metabolites are in
get_compartments()
lists compartments the metabolites are in
_associate_gene(cobra_gene)
Associates a cobra.Gene object with a cobra.Reaction.
Parameters cobra_gene (cobra.core.Gene.Gene) –
_dissociate_gene(cobra_gene)
Dissociates a cobra.Gene object with a cobra.Reaction.
Parameters cobra_gene (cobra.core.Gene.Gene) –
knock_out()
Knockout reaction by setting its bounds to zero.
build_reaction_from_string(reaction_str, verbose=True, fwd_arrow=None,
rev_arrow=None, reversible_arrow=None, term_split="+")
Builds reaction from reaction equation reaction_str using parser
Takes a string and using the specifications supplied in the optional arguments infers a set of metabo-
lites, metabolite compartments and stoichiometries for the reaction. It also infers the reversibility of
the reaction from the reaction arrow.
Changes to the associated model are reverted upon exit when using the model as a context.
Parameters
• reaction_str (string) – a string containing a reaction formula (equation)
• verbose (bool) – setting verbosity of function
• fwd_arrow (re.compile) – for forward irreversible reaction arrows
• rev_arrow (re.compile) – for backward irreversible reaction arrows
• reversible_arrow (re.compile) – for reversible reaction arrows
• term_split (string) – dividing individual metabolite entries
__str__()
_repr_html_()
15.1. cobra 73
cobra Documentation, Release 0.13.3
cobra.core.reaction.separate_forward_and_reverse_bounds(lower_bound, up-
per_bound)
Split a given (lower_bound, upper_bound) interval into a negative component and a positive component.
Negative components are negated (returns positive ranges) and flipped for usage with forward and reverse
reactions bounds
Parameters
• lower_bound (float) – The lower flux bound
• upper_bound (float) – The upper flux bound
cobra.core.reaction.update_forward_and_reverse_bounds(reaction, direc-
tion="both")
For the given reaction, update the bounds in the forward and reverse variable bounds.
Parameters
• reaction (cobra.Reaction) – The reaction to operate on
• direction (string) – Either ‘both’, ‘upper’ or ‘lower’ for updating the correspond-
ing flux bounds.
cobra.core.solution
Module Contents
Notes
Solution is meant to be constructed by get_solution please look at that function to fully understand the
Solution class.
objective_value
float – The (optimal) value for the objective function.
status
str – The solver status related to the solution.
fluxes
pandas.Series – Contains the reaction fluxes (primal values of variables).
reduced_costs
pandas.Series – Contains reaction reduced costs (dual values of variables).
shadow_prices
pandas.Series – Contains metabolite shadow prices (dual values of constraints).
Deprecated Attributes
---------------------
f
float – Use objective_value instead.
x
list – Use fluxes.values instead.
x_dict
pandas.Series – Use fluxes instead.
y
list – Use reduced_costs.values instead.
y_dict
pandas.Series – Use reduced_costs instead.
__init__(objective_value, status, fluxes, reduced_costs=None, shadow_prices=None, **kwargs)
Initialize a Solution from its components.
Parameters
• objective_value (float) – The (optimal) value for the objective function.
• status (str) – The solver status related to the solution.
• fluxes (pandas.Series) – Contains the reaction fluxes (primal values of vari-
ables).
• reduced_costs (pandas.Series) – Contains reaction reduced costs (dual val-
ues of variables).
• shadow_prices (pandas.Series) – Contains metabolite shadow prices (dual
values of constraints).
__repr__()
String representation of the solution instance.
_repr_html_()
__dir__()
Hide deprecated attributes and methods from the public interface.
__getitem__(reaction_id)
Return the flux of a reaction.
Parameters reaction (str) – A model reaction ID.
f()
Deprecated property for getting the objective value.
x_dict()
Deprecated property for getting fluxes.
x_dict(fluxes)
Deprecated property for setting fluxes.
x()
Deprecated property for getting flux values.
y_dict()
Deprecated property for getting reduced costs.
y_dict(costs)
Deprecated property for setting reduced costs.
y()
Deprecated property for getting reduced cost values.
to_frame()
Return the fluxes and reduced costs as a data frame
class cobra.core.solution.LegacySolution(f, x=None, x_dict=None, y=None,
y_dict=None, solver=None, the_time=0,
status="NA", **kwargs)
Legacy support for an interface to a cobra.Model optimization solution.
15.1. cobra 75
cobra Documentation, Release 0.13.3
f
float – The objective value
solver
str – A string indicating which solver package was used.
x
iterable – List or Array of the fluxes (primal values).
x_dict
dict – A dictionary of reaction IDs that maps to the respective primal values.
y
iterable – List or Array of the dual values.
y_dict
dict – A dictionary of reaction IDs that maps to the respective dual values.
Warning: deprecated
Note: This is only intended for the optlang solver interfaces and not the legacy solvers.
cobra.core.species
Module Contents
cobra.flux_analysis
Submodules
cobra.flux_analysis.deletion
Module Contents
cobra.flux_analysis.deletion._reactions_knockouts_with_restore(model, reac-
tions)
cobra.flux_analysis.deletion._get_growth(model)
cobra.flux_analysis.deletion._reaction_deletion(model, ids)
cobra.flux_analysis.deletion._gene_deletion(model, ids)
15.1. cobra 77
cobra Documentation, Release 0.13.3
cobra.flux_analysis.deletion._reaction_deletion_worker(ids)
cobra.flux_analysis.deletion._gene_deletion_worker(ids)
cobra.flux_analysis.deletion._init_worker(model)
cobra.flux_analysis.deletion._multi_deletion(model, entity, element_lists,
method="fba", solution=None, pro-
cesses=None, **kwargs)
Provide a common interface for single or multiple knockouts.
Parameters
• model (cobra.Model) – The metabolic model to perform deletions in.
• entity ('gene' or 'reaction') – The entity to knockout (cobra.Gene or
cobra.Reaction).
• element_lists (list) – List of iterables ‘‘cobra.Reaction‘‘s or ‘‘cobra.Gene‘‘s
(or their IDs) to be deleted.
• method ({"fba", "moma", "linear moma", "room", "linear
room"}, optional) – Method used to predict the growth rate.
• solution (cobra.Solution, optional) – A previous solution to use as a ref-
erence for (linear) MOMA or ROOM.
• processes (int, optional) – The number of parallel processes to run. Can
speed up the computations if the number of knockouts to perform is large. If not passed,
will be set to the number of CPUs found.
• kwargs – Passed on to underlying simulation functions.
Returns
A representation of all combinations of entity deletions. The columns are ‘growth’ and
‘status’, where
index [frozenset([str])] The gene or reaction identifiers that were knocked out.
growth [float] The growth rate of the adjusted model.
status [str] The solution’s status.
Return type pandas.DataFrame
cobra.flux_analysis.deletion._entities_ids(entities)
cobra.flux_analysis.deletion._element_lists(entities, *ids)
cobra.flux_analysis.deletion.single_reaction_deletion(model, reac-
tion_list=None,
method="fba", so-
lution=None, pro-
cesses=None, **kwargs)
Knock out each reaction from a given list.
Parameters
• model (cobra.Model) – The metabolic model to perform deletions in.
• reaction_list (iterable, optional) – ‘‘cobra.Reaction‘‘s to be deleted. If
not passed, all the reactions from the model are used.
• method ({"fba", "moma", "linear moma", "room", "linear
room"}, optional) – Method used to predict the growth rate.
• solution (cobra.Solution, optional) – A previous solution to use as a ref-
erence for (linear) MOMA or ROOM.
15.1. cobra 79
cobra Documentation, Release 0.13.3
cobra.flux_analysis.gapfilling
Module Contents
M. Knight, Stephen S. Fong, and Bernhard O. Palsson. “Systems Approach to Refining Genome Annotation.” Proceedings of the National
Academy of Sciences 103, no. 46 (2006): 17480–17484.
[2] Kumar, Vinay Satish, and Costas D. Maranas. “GrowMatch: An Automated Method for Reconciling In Silico/In Vivo
Growth Predictions.” Edited by Christos A. Ouzounis. PLoS Computational Biology 5, no. 3 (March 13, 2009): e1000308.
doi:10.1371/journal.pcbi.1000308.
[3] http://opencobra.github.io/cobrapy/tags/gapfilling/
[4] Schultz, André, and Amina A. Qutub. “Reconstruction of Tissue-Specific Metabolic Networks Using CORDA.” Edited by Costas D.
Maranas. PLOS Computational Biology 12, no. 3 (March 4, 2016): e1004808. doi:10.1371/journal.pcbi.1004808.
[5] Diener, Christian https://github.com/cdiener/corda
15.1. cobra 81
cobra Documentation, Release 0.13.3
References
• lower_bound (float) – The minimally accepted flux for the objective in the filled
model.
• penalties (dict, None) – A dictionary with keys being ‘universal’ (all reactions
included in the universal model), ‘exchange’ and ‘demand’ (all additionally added ex-
change and demand reactions) for the three reaction types. Can also have reaction iden-
tifiers for reaction specific costs. Defaults are 1, 100 and 1 respectively.
• iterations (int) – The number of rounds of gapfilling to perform. For every it-
eration, the penalty for every used reaction increases linearly. This way, the algorithm
is encouraged to search for alternative solutions which may include previously used
reactions. I.e., with enough iterations pathways including 10 steps will eventually be
reported even if the shortest pathway is a single reaction.
• exchange_reactions (bool) – Consider adding exchange (uptake) reactions for
all metabolites in the model.
• demand_reactions (bool) – Consider adding demand reactions for all metabo-
lites.
Returns list of lists with on set of reactions that completes the model per requested iteration.
Return type iterable
Examples
cobra.flux_analysis.geometric
Module Contents
311-5. 10.1016/j.jtbi.2009.01.027.
15.1. cobra 83
cobra Documentation, Release 0.13.3
References
cobra.flux_analysis.loopless
Module Contents
cobra.flux_analysis.loopless.add_loopless(model, zero_cutoff=1e-12)
Modify a model so all feasible flux distributions are loopless.
In most cases you probably want to use the much faster loopless_solution. May be used in cases where you
want to add complex constraints and objecives (for instance quadratic objectives) to the model afterwards
or use an approximation of Gibbs free energy directions in you model. Adds variables and constraints to
a model which will disallow flux distributions with loops. The used formulation is described in [1]_. This
function will modify your model.
Parameters
• model (cobra.Model) – The model to which to add the constraints.
• zero_cutoff (positive float, optional) – Cutoff used for null space.
Coefficients with an absolute value smaller than zero_cutoff are considered to be zero.
Returns
Return type Nothing
References
cobra.flux_analysis.loopless._add_cycle_free(model, fluxes)
Add constraints for CycleFreeFlux.
cobra.flux_analysis.loopless.loopless_solution(model, fluxes=None)
Convert an existing solution to a loopless one.
Removes as many loops as possible (see Notes). Uses the method from CycleFreeFlux [1]_ and is much
faster than add_loopless and should therefore be the preferred option to get loopless flux distributions.
Parameters
• model (cobra.Model) – The model to which to add the constraints.
• fluxes (dict) – A dictionary {rxn_id: flux} that assigns a flux to each reaction. If
not None will use the provided flux values to obtain a close loopless solution.
Returns A solution object containing the fluxes with the least amount of loops possible or None
if the optimization failed (usually happening if the flux distribution in fluxes is infeasible).
Return type cobra.Solution
Notes
References
3. the model contains an auxiliary variable called “fva_old_objective” denoting the previous objective
Parameters
• model (cobra.Model) – The model to be used.
• reaction (cobra.Reaction) – The reaction currently minimized/maximized.
• solution (boolean, optional) – Whether to return the entire solution or only
the minimum/maximum for reaction.
• zero_cutoff (positive float, optional) – Cutoff used for loop removal.
Fluxes with an absolute value smaller than zero_cutoff are considered to be zero.
Returns Returns the minimized/maximized flux through reaction if all_fluxes == False (de-
fault). Otherwise returns a loopless flux solution containing the minimum/maximum flux
for reaction.
Return type single float or dict
cobra.flux_analysis.loopless.construct_loopless_model(cobra_model)
Construct a loopless model.
This adds MILP constraints to prevent flux from proceeding in a loop, as done in http://dx.doi.org/10.1016/
j.bpj.2010.12.3707 Please see the documentation for an explanation of the algorithm.
This must be solved with an MILP capable solver.
cobra.flux_analysis.moma
Module Contents
15.1. cobra 85
cobra Documentation, Release 0.13.3
• linear (bool, optional) – Whether to use the linear MOMA formulation or not
(default True).
Returns A flux distribution that is at a minimal distance compared to the reference solution.
Return type cobra.Solution
See also:
Notes
In the original MOMA1 specification one looks for the flux distribution of the deletion (v^d) closest to the
fluxes without the deletion (v). In math this means:
minimize sum_i (v^d_i - v_i)^2 s.t. Sv^d = 0
lb_i <= v^d_i <= ub_i
Here, we use a variable transformation v^t := v^d_i - v_i. Substituting and using the fact that Sv = 0 gives:
minimize sum_i (v^t_i)^2 s.t. Sv^d = 0
v^t = v^d_i - v_i lb_i <= v^d_i <= ub_i
So basically we just re-center the flux space at the old solution and then find the flux distribution closest to
the new zero (center). This is the same strategy as used in cameo.
In the case of linear MOMA2 , we instead minimize sum_i abs(v^t_i). The linear MOMA is typically
significantly faster. Also quadratic MOMA tends to give flux distributions in which all fluxes deviate from
the reference fluxes a little bit whereas linear MOMA tends to give flux distributions where the majority of
fluxes are the same reference with few fluxes deviating a lot (typical effect of L2 norm vs L1 norm).
The former objective function is saved in the optlang solver interface as "moma_old_objective" and
this can be used to immediately extract the value of the former objective after MOMA optimization.
See also:
of Cellular Metabolism with Constraint-Based Models: The COBRA Toolbox.” Nature Protocols 2 (March 29, 2007): 727.
References
cobra.flux_analysis.parsimonious
Module Contents
cobra.flux_analysis.parsimonious.optimize_minimal_flux(*args, **kwargs)
cobra.flux_analysis.parsimonious.pfba(model, fraction_of_optimum=1.0, objec-
tive=None, reactions=None)
Perform basic pFBA (parsimonious Enzyme Usage Flux Balance Analysis) to minimize total flux.
pFBA [1] adds the minimization of all fluxes the the objective of the model. This approach is motivated by
the idea that high fluxes have a higher enzyme turn-over and that since producing enzymes is costly, the cell
will try to minimize overall flux while still maximizing the original objective function, e.g. the growth rate.
Parameters
• model (cobra.Model) – The model
• fraction_of_optimum (float, optional) – Fraction of optimum which
must be maintained. The original objective reaction is constrained to be greater than
maximal_value * fraction_of_optimum.
• objective (dict or model.problem.Objective) – A desired objective to
use during optimization in addition to the pFBA objective. Dictionaries (reaction as key,
coefficient as value) can be used for linear objectives.
• reactions (iterable) – List of reactions or reaction identifiers. Implies re-
turn_frame to be true. Only return fluxes for the given reactions. Faster than fetching
all fluxes if only a few are needed.
Returns The solution object to the optimized model with pFBA constraints added.
Return type cobra.Solution
References
Parameters
• model (cobra.Model) – The model to add the objective to
• objective – An objective to set in combination with the pFBA objective.
• fraction_of_optimum (float) – Fraction of optimum which must be main-
tained. The original objective reaction is constrained to be greater than maximal_value
* fraction_of_optimum.
15.1. cobra 87
cobra Documentation, Release 0.13.3
cobra.flux_analysis.phenotype_phase_plane
Module Contents
cobra.flux_analysis.phenotype_phase_plane.production_envelope(model, reac-
tions, objec-
tive=None,
car-
bon_sources=None,
points=20,
threshold=1e-
07)
Calculate the objective value conditioned on all combinations of fluxes for a set of chosen reactions
The production envelope can be used to analyze a model’s ability to produce a given compound conditional
on the fluxes for another set of reactions, such as the uptake rates. The model is alternately optimized
with respect to minimizing and maximizing the objective and the obtained fluxes are recorded. Ranges to
compute production is set to the effective bounds, i.e., the minimum / maximum fluxes that can be obtained
given current reaction bounds.
Parameters
• model (cobra.Model) – The model to compute the production envelope for.
• reactions (list or string) – A list of reactions, reaction identifiers or a single
reaction.
• objective (string, dict, model.solver.interface.Objective,
optional) – The objective (reaction) to use for the production envelope. Use the
model’s current objective if left missing.
• carbon_sources (list or string, optional) – One or more reactions or
reaction identifiers that are the source of carbon for computing carbon (mol carbon
in output over mol carbon in input) and mass yield (gram product over gram output).
Only objectives with a carbon containing input and output metabolite is supported. Will
identify active carbon sources in the medium if none are specified.
• points (int, optional) – The number of points to calculate production for.
• threshold (float, optional) – A cut-off under which flux values will be con-
sidered to be zero.
Returns
A data frame with one row per evaluated point and
• reaction id : one column per input reaction indicating the flux at each given point,
• carbon_source: identifiers of carbon exchange reactions
A column for the maximum and minimum each for the following types:
• flux: the objective flux
• carbon_yield: if carbon source is defined and the product is a single metabolite (mol
carbon product per mol carbon feeding source)
• mass_yield: if carbon source is defined and the product is a single metabolite (gram
product per 1 g of feeding source)
Return type pandas.DataFrame
Examples
15.1. cobra 89
cobra Documentation, Release 0.13.3
cobra.flux_analysis.reaction
Module Contents
cobra.flux_analysis.reaction.assess_precursors(model, reaction,
flux_coefficient_cutoff=0.001,
solver=None)
Assesses the ability of the model to provide sufficient precursors for a reaction operating at, or beyond, the
specified cutoff.
Deprecated: use assess_component instead
Parameters
• model (cobra.Model) – The cobra model to assess production capacity for
• reaction (reaction identifier or cobra.Reaction) – The reaction to
assess
• flux_coefficient_cutoff (float) – The minimum flux that reaction must
carry to be considered active.
• solver (basestring) – Solver name. If None, the default solver will be used.
Returns True if the precursors can be simultaneously produced at the specified cutoff. False, if
the model has the capacity to produce each individual precursor at the specified threshold but
not all precursors at the required level simultaneously. Otherwise a dictionary of the required
and the produced fluxes for each reactant that is not produced in sufficient quantities.
Return type bool or dict
cobra.flux_analysis.reaction.assess_products(model, reaction,
flux_coefficient_cutoff=0.001,
solver=None)
Assesses whether the model has the capacity to absorb the products of a reaction at a given flux rate.
Useful for identifying which components might be blocking a reaction from achieving a specific flux rate.
Deprecated: use assess_component instead
Parameters
• model (cobra.Model) – The cobra model to assess production capacity for
• reaction (reaction identifier or cobra.Reaction) – The reaction to
assess
• flux_coefficient_cutoff (float) – The minimum flux that reaction must
carry to be considered active.
• solver (basestring) – Solver name. If None, the default solver will be used.
Returns True if the model has the capacity to absorb all the reaction products being simul-
taneously given the specified cutoff. False, if the model has the capacity to absorb each
individual product but not all products at the required level simultaneously. Otherwise a
dictionary of the required and the capacity fluxes for each product that is not absorbed in
sufficient quantities.
Return type bool or dict
cobra.flux_analysis.room
Module Contents
15.1. cobra 91
cobra Documentation, Release 0.13.3
Compute a new flux distribution that minimizes the number of active reactions needed to accommodate a
previous reference solution. Regulatory on/off minimization (ROOM) is generally used to assess the impact
of knock-outs. Thus the typical usage is to provide a wildtype flux distribution as reference and a model in
knock-out state.
Parameters
• model (cobra.Model) – The model state to compute a ROOM-based solution for.
• solution (cobra.Solution, optional) – A (wildtype) reference solution.
• linear (bool, optional) – Whether to use the linear ROOM formulation or not
(default False).
• delta (float, optional) – The relative tolerance range (additive) (default 0.03).
• epsilon (float, optional) – The absolute tolerance range (multiplicative) (de-
fault 0.001).
Returns A flux distribution with minimal active reaction changes compared to the reference.
Return type cobra.Solution
See also:
Notes
The formulation used here is the same as stated in the original paper1 . The mathematical expression is given
below:
minimize sum_{i=1}^m y^i s.t. Sv = 0
v_min <= v <= v_max v_j = 0 j A for 1 <= i <= m v_i - y_i(v_{max,i} - w_i^u) <= w_i^u (1)
v_i - y_i(v_{min,i} - w_i^l) <= w_i^l (2) y_i {0,1} (3) w_i^u = w_i + delta|w_i| + epsilon w_i^l
= w_i - delta|w_i| - epsilon
So, for the linear version of the ROOM , constraint (3) is relaxed to 0 <= y_i <= 1.
See also:
1 Tomer Shlomi, Omer Berkman and Eytan Ruppin, “Regulatory on/off minimization of metabolic flux changes after genetic perturba-
tions”, PNAS 2005 102 (21) 7695-7700; doi:10.1073/pnas.0406346102
References
cobra.flux_analysis.sampling
Module Contents
cobra.flux_analysis.sampling.mp_init(obj)
Initialize the multiprocessing pool.
cobra.flux_analysis.sampling.shared_np_array(shape, data=None, integer=False)
Create a new numpy array that resides in shared memory.
Parameters
• shape (tuple of ints) – The shape of the new array.
• data (numpy.array) – Data to copy to the new array. Has to have the same shape.
• integer (boolean) – Whether to use an integer array. Defaults to False which
means float array.
cobra.flux_analysis.sampling._step(sampler, x, delta, fraction=None, tries=0)
Sample a new feasible point from the point x in direction delta.
class cobra.flux_analysis.sampling.HRSampler(model, thinning, nproj=None,
seed=None)
The abstract base class for hit-and-run samplers.
Parameters
• model (cobra.Model) – The cobra model from which to generate samples.
• thinning (int) – The thinning factor of the generated sampling chain. A thinning
of 10 means samples are returned every 10 steps.
• nproj (int > 0, optional) – How often to reproject the sampling point into the
feasibility space. Avoids numerical issues at the cost of lower sampling. If you observe
many equality constraint violations with sampler.validate you should lower this number.
• seed (int > 0, optional) – The random number seed that should be used.
model
cobra.Model – The cobra model from which the samples get generated.
thinning
int – The currently used thinning factor.
n_samples
int – The total number of samples that have been generated by this sampler instance.
retries
int – The overall of sampling retries the sampler has observed. Larger values indicate numerical
instabilities.
problem
collections.namedtuple – A python object whose attributes define the entire sampling problem in ma-
trix form. See docstring of Problem.
15.1. cobra 93
cobra Documentation, Release 0.13.3
warmup
a numpy matrix – A matrix of with as many columns as reactions in the model and more than 3 rows
containing a warmup sample in each row. None if no warmup points have been generated yet.
nproj
int – How often to reproject the sampling point into the feasibility space.
seed
positive integer, optional – Sets the random number seed. Initialized to the current time stamp if None.
fwd_idx
np.array – Has one entry for each reaction in the model containing the index of the respective forward
variable.
rev_idx
np.array – Has one entry for each reaction in the model containing the index of the respective reverse
variable.
__init__(model, thinning, nproj=None, seed=None)
Initialize a new sampler object.
__build_problem()
Build the matrix representation of the sampling problem.
generate_fva_warmup()
Generate the warmup points for the sampler.
Generates warmup points by setting each flux as the sole objective and minimizing/maximizing it.
Also caches the projection of the warmup points into the nullspace for non-homogeneous problems
(only if necessary).
_reproject(p)
Reproject a point into the feasibility region.
This function is guaranteed to return a new feasible point. However, no guarantees in terms of prox-
imity to the original point can be made.
Parameters p (numpy.array) – The current sample point.
Returns A new feasible point. If p was feasible it wil return p.
Return type numpy.array
_random_point()
Find an approximately random point in the flux cone.
_is_redundant(matrix, cutoff=None)
Identify rdeundant rows in a matrix that can be removed.
_bounds_dist(p)
Get the lower and upper bound distances. Negative is bad.
sample(n, fluxes=True)
Abstract sampling function.
Should be overwritten by child classes.
batch(batch_size, batch_num, fluxes=True)
Create a batch generator.
This is useful to generate n batches of m samples each.
Parameters
• batch_size (int) – The number of samples contained in each batch (m).
• batch_num (int) – The number of batches in the generator (n).
15.1. cobra 95
cobra Documentation, Release 0.13.3
seed
positive integer, optional – Sets the random number seed. Initialized to the current time stamp if None.
nproj
int – How often to reproject the sampling point into the feasibility space.
fwd_idx
np.array – Has one entry for each reaction in the model containing the index of the respective forward
variable.
rev_idx
np.array – Has one entry for each reaction in the model containing the index of the respective reverse
variable.
prev
numpy array – The current/last flux sample generated.
center
numpy array – The center of the sampling space as estimated by the mean of all previously generated
samples.
Notes
ACHR generates samples by choosing new directions from the sampling space’s center and the warmup
points. The implementation used here is the same as in the Matlab Cobra Toolbox [2]_ and uses only the
initial warmup points to generate new directions and not any other previous iterates. This usually gives
better mixing since the startup points are chosen to span the space in a wide manner. This also makes the
generated sampling chain quasi-markovian since the center converges rapidly.
Memory usage is roughly in the order of (2 * number reactions)^2 due to the required nullspace matrices
and warmup points. So large models easily take up a few GB of RAM.
References
Notes
Performance of this function linearly depends on the number of reactions in your model and the thin-
ning factor.
cobra.flux_analysis.sampling._sample_chain(args)
Sample a single chain for OptGPSampler.
center and n_samples are updated locally and forgotten afterwards.
class cobra.flux_analysis.sampling.OptGPSampler(model, processes, thinning=100,
nproj=None, seed=None)
A parallel optimized sampler.
A parallel sampler with fast convergence and parallel execution. See [1]_ for details.
Parameters
• model (cobra.Model) – The cobra model from which to generate samples.
• processes (int) – The number of processes used during sampling.
• thinning (int, optional) – The thinning factor of the generated sampling
chain. A thinning of 10 means samples are returned every 10 steps.
• nproj (int > 0, optional) – How often to reproject the sampling point into the
feasibility space. Avoids numerical issues at the cost of lower sampling. If you observe
many equality constraint violations with sampler.validate you should lower this number.
• seed (int > 0, optional) – Sets the random number seed. Initialized to the
current time stamp if None.
model
cobra.Model – The cobra model from which the samples get generated.
thinning
int – The currently used thinning factor.
n_samples
int – The total number of samples that have been generated by this sampler instance.
problem
collections.namedtuple – A python object whose attributes define the entire sampling problem in ma-
trix form. See docstring of Problem.
warmup
a numpy matrix – A matrix of with as many columns as reactions in the model and more than 3 rows
containing a warmup sample in each row. None if no warmup points have been generated yet.
retries
int – The overall of sampling retries the sampler has observed. Larger values indicate numerical
instabilities.
seed
positive integer, optional – Sets the random number seed. Initialized to the current time stamp if None.
nproj
int – How often to reproject the sampling point into the feasibility space.
fwd_idx
np.array – Has one entry for each reaction in the model containing the index of the respective forward
variable.
rev_idx
np.array – Has one entry for each reaction in the model containing the index of the respective reverse
variable.
prev
numpy.array – The current/last flux sample generated.
center
numpy.array – The center of the sampling space as estimated by the mean of all previously generated
samples.
15.1. cobra 97
cobra Documentation, Release 0.13.3
Notes
The sampler is very similar to artificial centering where each process samples its own chain. Initial points are
chosen randomly from the warmup points followed by a linear transformation that pulls the points towards
the a little bit towards the center of the sampling space.
If the number of processes used is larger than one the requested number of samples is adjusted to the smallest
multiple of the number of processes larger than the requested sample number. For instance, if you have 3
processes and request 8 samples you will receive 9.
Memory usage is roughly in the order of (2 * number reactions)^2 due to the required nullspace matrices
and warmup points. So large models easily take up a few GB of RAM. However, most of the large matrices
are kept in shared memory. So the RAM usage is independent of the number of processes.
References
Notes
Performance of this function linearly depends on the number of reactions in your model and the thin-
ning factor.
If the number of processes is larger than one, computation is split across as the CPUs of your machine.
This may shorten computation time. However, there is also overhead in setting up parallel computation
so we recommend to calculate large numbers of samples at once (n > 1000).
__getstate__()
Return the object for serialization.
cobra.flux_analysis.sampling.sample(model, n, method="optgp", thinning=100, pro-
cesses=1, seed=None)
Sample valid flux distributions from a cobra model.
The function samples valid flux distributions from a cobra model. Currently we support two methods:
1. ‘optgp’ (default) which uses the OptGPSampler that supports parallel sampling [1]_. Requires
large numbers of samples to be performant (n < 1000). For smaller samples ‘achr’ might be better
suited.
or
2. ‘achr’ which uses artificial centering hit-and-run. This is a single process method with good conver-
gence [2]_.
Parameters
Notes
The samplers have a correction method to ensure equality feasibility for long-running chains, however this
will only work for homogeneous models, meaning models with no non-zero fixed variables or constraints (
right-hand side of the equalities are zero).
References
cobra.flux_analysis.summary
Module Contents
15.1. cobra 99
cobra Documentation, Release 0.13.3
cobra.flux_analysis.variability
Module Contents
cobra.flux_analysis.variability.flux_variability_analysis(model, reac-
tion_list=None,
loop-
less=False, frac-
tion_of_optimum=1.0,
pfba_factor=None)
Determine the minimum and maximum possible flux value for each reaction.
Parameters
• model (cobra.Model) – The model for which to run the analysis. It will not be
modified.
• reaction_list (list of cobra.Reaction or str, optional) – The
reactions for which to obtain min/max fluxes. If None will use all reactions in the model
(default).
• loopless (boolean, optional) – Whether to return only loopless solutions.
This is significantly slower. Please also refer to the notes.
• fraction_of_optimum (float, optional) – Must be <= 1.0. Requires that
the objective value is at least the fraction times maximum objective value. A value
of 0.85 for instance means that the objective has to be at least at 85% percent of its
maximum.
• pfba_factor (float, optional) – Add an additional constraint to the model
that requires the total sum of absolute fluxes must not be larger than this value times
the smallest possible sum of absolute fluxes, i.e., by setting the value to 1.1 the to-
tal sum of absolute fluxes must not be more than 10% larger than the pFBA solution.
Since the pFBA solution is the one that optimally minimizes the total flux sum, the
pfba_factor should, if set, be larger than one. Setting this value may lead to more
realistic predictions of the effective flux bounds.
Returns A data frame with reaction identifiers as the index and two columns: - maximum:
indicating the highest possible flux - minimum: indicating the lowest possible flux
Return type pandas.DataFrame
Notes
This implements the fast version as described in1 . Please note that the flux distribution containing all mini-
mal/maximal fluxes does not have to be a feasible solution for the model. Fluxes are minimized/maximized
individually and a single minimal flux might require all others to be suboptimal.
Using the loopless option will lead to a significant increase in computation time (about a factor of 100 for
large models). However, the algorithm used here (see2 ) is still more than 1000x faster than the “naive”
version using add_loopless(model). Also note that if you have included constraints that force a loop
(for instance by setting all fluxes in a loop to be non-zero) this loop will be included in the solution.
References
cobra.flux_analysis.variability.find_blocked_reactions(model, reac-
tion_list=None,
zero_cutoff=1e-09,
open_exchanges=False)
Finds reactions that cannot carry a flux with the current exchange reaction settings for a cobra model, using
flux variability analysis.
Parameters
• model (cobra.Model) – The model to analyze
• reaction_list (list) – List of reactions to consider, use all if left missing
• zero_cutoff (float) – Flux value which is considered to effectively be zero.
• open_exchanges (bool) – If true, set bounds on exchange reactions to very high
values to avoid that being the bottle-neck.
Returns List with the blocked reactions
Return type list
cobra.flux_analysis.variability.find_essential_genes(model, threshold=None,
processes=None)
Return a set of essential genes.
A gene is considered essential if restricting the flux of all reactions that depend on it to zero causes the
objective, e.g., the growth rate, to also be zero, below the threshold, or infeasible.
Parameters
• model (cobra.Model) – The model to find the essential genes for.
• threshold (float, optional) – Minimal objective flux to be considered viable.
By default this is 1% of the maximal objective.
• processes (int, optional) – The number of parallel processes to run. Can
speed up the computations if the number of knockouts to perform is large. If not passed,
will be set to the number of CPUs found.
1 Computationally efficient flux variability analysis. Gudmundsson S, Thiele I. BMC Bioinformatics. 2010 Sep 29;11:489. doi:
cobra.io
Submodules
cobra.io.dict
Module Contents
cobra.io.dict._fix_type(value)
convert possible types to str, float, and bool
cobra.io.dict._update_optional(cobra_object, new_dict, optional_attribute_dict, or-
dered_keys)
update new_dict with optional attributes from cobra_object
cobra.io.dict.metabolite_to_dict(metabolite)
cobra.io.dict.metabolite_from_dict(metabolite)
cobra.io.dict.gene_to_dict(gene)
cobra.io.dict.gene_from_dict(gene)
cobra.io.dict.reaction_to_dict(reaction)
cobra.io.dict.reaction_from_dict(reaction, model)
cobra.io.dict.model_to_dict(model, sort=False)
Convert model to a dict.
Parameters
• model (cobra.Model) – The model to reformulate as a dict.
• sort (bool, optional) – Whether to sort the metabolites, reactions, and genes or
maintain the order defined in the model.
Returns A dictionary with elements, ‘genes’, ‘compartments’, ‘id’, ‘metabolites’, ‘notes’ and
‘reactions’; where ‘metabolites’, ‘genes’ and ‘metabolites’ are in turn lists with dictionaries
holding all attributes to form the corresponding object.
cobra.io.json
Module Contents
cobra.io.json.from_json(document)
Load a cobra model from a JSON document.
Parameters document (str) – The JSON document representation of a cobra model.
Returns The cobra model as represented in the JSON document.
Return type cobra.Model
See also:
cobra.io.json.load_json_model(filename)
Load a cobra model from a file in JSON format.
Parameters filename (str or file-like) – File path or descriptor that contains the
JSON document describing the cobra model.
Returns The cobra model as represented in the JSON document.
Return type cobra.Model
See also:
cobra.io.mat
Module Contents
cobra.io.mat._get_id_compartment(id)
extract the compartment from the id string
cobra.io.mat._cell(x)
translate an array x into a MATLAB cell array
cobra.io.mat.load_matlab_model(infile_path, variable_name=None, inf=inf )
Load a cobra model stored as a .mat file
Parameters
• infile_path (str) – path to the file to to read
• variable_name (str, optional) – The variable name of the model in the .mat
file. If this is not specified, then the first MATLAB variable which looks like a COBRA
model will be used
• inf (value) – The value to use for infinite bounds. Some solvers do not handle infinite
values so for using those, set this to a high numeric value.
Returns The resulting cobra model
Return type cobra.core.Model.Model
cobra.io.mat.save_matlab_model(model, file_name, varname=None)
Save the cobra model as a .mat file.
This .mat file can be used directly in the MATLAB version of COBRA.
Parameters
cobra.io.sbml
Module Contents
• old_sbml (bool) – Set to True if the XML file has metabolite formula appended to
metabolite names. This was a poorly designed artifact that persists in some models.
• legacy_metabolite (bool) –
If True then assume that the metabolite id has the compartment id appended after
an underscore (e.g. _c for cytosol). This has not been implemented but will be soon.
• print_time (bool) – deprecated
• use_hyphens (bool) – If True, double underscores (__) in an SBML ID will be
converted to hyphens
Returns Model
Return type The parsed cobra model
cobra.io.sbml.parse_legacy_sbml_notes(note_string, note_delimiter=":")
Deal with various legacy SBML format issues.
cobra.io.sbml.write_cobra_model_to_sbml_file(cobra_model, sbml_filename,
sbml_level=2, sbml_version=1,
print_time=False,
use_fbc_package=True)
Write a cobra.Model object to an SBML XML file.
Parameters
• cobra_model (cobra.core.Model.Model) – The model object to write
• sbml_filename (string) – The file to write the SBML XML to.
• sbml_level (int) – 2 is the only supported level.
• sbml_version (int) – 1 is the only supported version.
• print_time (bool) – deprecated
• use_fbc_package (bool) – Convert the model to the FBC package format to im-
prove portability. http://sbml.org/Documents/Specifications/SBML_Level_3/Packages/
Flux_Balance_Constraints_(flux)
Notes
TODO: Update the NOTES to match the SBML standard and provide support for Level 2 Version 4
cobra.io.sbml.get_libsbml_document(cobra_model, sbml_level=2, sbml_version=1,
print_time=False, use_fbc_package=True)
Return a libsbml document object for writing to a file. This function is used by
write_cobra_model_to_sbml_file().
cobra.io.sbml.add_sbml_species(sbml_model, cobra_metabolite, note_start_tag,
note_end_tag, boundary_metabolite=False)
A helper function for adding cobra metabolites to an sbml model.
Parameters
• sbml_model (sbml_model object) –
• cobra_metabolite (a cobra.Metabolite object) –
• note_start_tag (string) – the start tag for parsing cobra notes. this will even-
tually be supplanted when COBRA is worked into sbml.
• note_end_tag (string) – the end tag for parsing cobra notes. this will eventually
be supplanted when COBRA is worked into sbml.
• boundary_metabolite (bool) – if metabolite boundary condition should be set
or not
Returns string
Return type the created metabolite identifier
cobra.io.sbml.fix_legacy_id(id, use_hyphens=False, fix_compartments=False)
cobra.io.sbml.read_legacy_sbml(filename, use_hyphens=False)
read in an sbml file and fix the sbml id’s
cobra.io.sbml3
Module Contents
class cobra.io.sbml3.Basic
cobra.io.sbml3.ns(query)
replace prefixes with namespace
cobra.io.sbml3.extract_rdf_annotation(sbml_element, metaid)
class cobra.io.sbml3.CobraSBMLError
cobra.io.sbml3.get_attrib(tag, attribute, type=None, require=False)
cobra.io.sbml3.set_attrib(xml, attribute_name, value)
cobra.io.sbml3.parse_stream(filename)
parses filename or compressed stream to xml
cobra.io.sbml3.clip(string, prefix)
clips a prefix from the beginning of a string if it exists
cobra.io.sbml3.strnum(number)
Utility function to convert a number to a string
cobra.io.sbml3.construct_gpr_xml(parent, expression)
create gpr xml under parent node
cobra.io.sbml3.annotate_cobra_from_sbml(cobra_element, sbml_element)
cobra.io.sbml3.annotate_sbml_from_cobra(sbml_element, cobra_element)
cobra.io.sbml3.parse_xml_into_model(xml, number=float)
cobra.io.sbml3.model_to_xml(cobra_model, units=True)
cobra.io.sbml3.read_sbml_model(filename, number=float, **kwargs)
cobra.io.sbml3.validate_sbml_model(filename, check_model=True)
Returns the model along with a list of errors.
Parameters
• filename (str) – The filename of the SBML model to be validated.
• check_model (bool, optional) – Whether to also check some basic model
properties such as reaction boundaries and compartment formulas.
Returns
• model (Model object) – The cobra model if the file could be read succesfully or None
otherwise.
• errors (dict) – Warnings and errors grouped by their respective types.
Raises CobraSBMLError – If the file is not a valid SBML Level 3 file with FBC.
cobra.io.yaml
Module Contents
cobra.io.yaml.from_yaml(document)
Load a cobra model from a YAML document.
Parameters document (str) – The YAML document representation of a cobra model.
Returns The cobra model as represented in the YAML document.
Return type cobra.Model
See also:
cobra.io.yaml.load_yaml_model(filename)
Load a cobra model from a file in YAML format.
Parameters filename (str or file-like) – File path or descriptor that contains the
YAML document describing the cobra model.
Returns The cobra model as represented in the YAML document.
Return type cobra.Model
See also:
cobra.manipulation
Submodules
cobra.manipulation.annotate
Module Contents
cobra.manipulation.annotate.add_SBO(model)
adds SBO terms for demands and exchanges
This works for models which follow the standard convention for constructing and naming these reactions.
The reaction should only contain the single metabolite being exchanged, and the id should be EX_metid or
DM_metid
cobra.manipulation.delete
Module Contents
cobra.manipulation.delete.prune_unused_metabolites(cobra_model)
Remove metabolites that are not involved in any reactions
Parameters cobra_model (cobra.Model) – the model to remove unused metabolites from
Returns list of metabolites that were removed
Return type list
cobra.manipulation.delete.prune_unused_reactions(cobra_model)
Remove reactions that have no assigned metabolites
Parameters cobra_model (cobra.Model) – the model to remove unused reactions from
Returns list of reactions that were removed
Return type list
cobra.manipulation.delete.undelete_model_genes(cobra_model)
Undoes the effects of a call to delete_model_genes in place.
cobra_model: A cobra.Model which will be modified in place
cobra.manipulation.delete.get_compiled_gene_reaction_rules(cobra_model)
Generates a dict of compiled gene_reaction_rules
Any gene_reaction_rule expressions which cannot be compiled or do not evaluate after compiling will be
excluded. The result can be used in the find_gene_knockout_reactions function to speed up evaluation of
these rules.
cobra.manipulation.delete.find_gene_knockout_reactions(cobra_model,
gene_list, com-
piled_gene_reaction_rules=None)
identify reactions which will be disabled when the genes are knocked out
cobra_model: Model
gene_list: iterable of Gene
compiled_gene_reaction_rules: dict of {reaction_id: compiled_string} If provided, this gives pre-
compiled gene_reaction_rule strings. The compiled rule strings can be evaluated much faster. If a rule
is not provided, the regular expression evaluation will be used. Because not all gene_reaction_rule
strings can be evaluated, this dict must exclude any rules which can not be used with eval.
cobra.manipulation.delete.delete_model_genes(cobra_model, gene_list, cu-
mulative_deletions=True, dis-
able_orphans=False)
delete_model_genes will set the upper and lower bounds for reactions catalysed by the genes
in gene_list if deleting the genes means that the reaction cannot proceed according to co-
bra_model.reactions[:].gene_reaction_rule
cumulative_deletions: False or True. If True then any previous deletions will be maintained in the model.
class cobra.manipulation.delete._GeneRemover(target_genes)
__init__(target_genes)
visit_Name(node)
visit_BoolOp(node)
cobra.manipulation.delete.remove_genes(cobra_model, gene_list, re-
move_reactions=True)
remove genes entirely from the model
This will also simplify all gene_reaction_rules with this gene inactivated.
cobra.manipulation.modify
Module Contents
cobra.manipulation.modify._escape_str_id(id_str)
make a single string id SBML compliant
class cobra.manipulation.modify._GeneEscaper
visit_Name(node)
cobra.manipulation.modify.escape_ID(cobra_model)
makes all ids SBML compliant
cobra.manipulation.modify.rename_genes(cobra_model, rename_dict)
renames genes in a model from the rename_dict
cobra.manipulation.modify.convert_to_irreversible(cobra_model)
Split reversible reactions into two irreversible reactions
These two reactions will proceed in opposite directions. This guarentees that all reactions in the model will
only allow positive flux values, which is useful for some modeling problems.
cobra_model: A Model object which will be modified in place.
cobra.manipulation.modify.revert_to_reversible(cobra_model, up-
date_solution=True)
This function will convert an irreversible model made by convert_to_irreversible into a reversible model.
cobra.manipulation.validate
Module Contents
cobra.manipulation.validate.check_mass_balance(model)
cobra.manipulation.validate.check_reaction_bounds(model)
cobra.manipulation.validate.check_metabolite_compartment_formula(model)
cobra.medium
Submodules
cobra.medium.boundary_types
Module Contents
cobra.medium.boundary_types.find_external_compartment(model)
Find the external compartment in the model.
Uses a simple heuristic where the external compartment should be the one with the most exchange reactions.
Parameters model (cobra.Model) – A cobra model.
Returns The putative external compartment.
Return type str
cobra.medium.boundary_types.is_boundary_type(reaction, boundary_type, exter-
nal_compartment)
Check whether a reaction is an exchange reaction.
Parameters
• reaction (cobra.Reaction) – The reaction to check.
• boundary_type (str) – What boundary type to check for. Must be one of “ex-
change”, “demand”, or “sink”.
• external_compartment (str) – The id for the external compartment.
Returns Whether the reaction looks like the requested type. Might be based on a heuristic.
Return type boolean
cobra.medium.boundary_types.find_boundary_types(model, boundary_type, exter-
nal_compartment=None)
Find specific boundary reactions.
Parameters
• model (cobra.Model) – A cobra model.
• boundary_type (str) – What boundary type to check for. Must be one of “ex-
change”, “demand”, or “sink”.
• external_compartment (str or None) – The id for the external compartment.
If None it will be detected automatically.
Returns A list of likely boundary reactions of a user defined type.
Return type list of cobra.reaction
cobra.medium.minimal_medium
Module Contents
cobra.medium.minimal_medium.add_linear_obj(model)
Add a linear version of a minimal medium to the model solver.
Changes the optimization objective to finding the growth medium requiring the smallest total import flux:
minimize sum |r_i| for r_i in import_reactions
cobra.medium.minimal_medium.add_mip_obj(model)
Add a mixed-integer version of a minimal medium to the model.
Changes the optimization objective to finding the medium with the least components:
minimize size(R) where R part of import_reactions
Parameters
• model (cobra.model) – The model to modify.
• min_objective_value (positive float or array-like object) –
The minimum growth rate (objective) that has to be achieved.
• exports (boolean) – Whether to include export fluxes in the returned medium.
Defaults to False which will only return import fluxes.
• minimize_components (boolean or positive int) – Whether to mini-
mize the number of components instead of the total import flux. Might be more intuitive
if set to True but may also be slow to calculate for large communities. If set to a number
n will return up to n alternative solutions all with the same number of components.
• open_exchanges (boolean or number) – Whether to ignore currently set
bounds and make all exchange reactions in the model possible. If set to a number
all exchange reactions will be opened with (-number, number) as bounds.
Returns A series giving the import flux for each required import reaction and (optionally) the
associated export fluxes. All exchange fluxes are oriented into the import reaction e.g. posi-
tive fluxes denote imports and negative fluxes exports. If minimize_components is a number
larger 1 may return a DataFrame where each column is a minimal medium. Returns None if
the minimization is infeasible (for instance if min_growth > maximum growth rate).
Return type pandas.Series, pandas.DataFrame or None
Notes
Due to numerical issues the minimize_components option will usually only minimize the number of
“large” import fluxes. Specifically, the detection limit is given by integrality_tolerance *
max_bound where max_bound is the largest bound on an import reaction. Thus, if you are in-
terested in small import fluxes as well you may have to adjust the integrality tolerance at first with
model.solver.configuration.tolerances.integrality = 1e-7 for instance. However, this will be very slow for
large models especially with GLPK.
cobra.test
Submodules
cobra.test.conftest
Module Contents
cobra.test.conftest.pytest_addoption(parser)
cobra.test.conftest.data_directory()
cobra.test.conftest.empty_once()
cobra.test.conftest.empty_model(empty_once)
cobra.test.conftest.small_model()
cobra.test.conftest.model(small_model)
cobra.test.conftest.large_once()
cobra.test.conftest.large_model(large_once)
cobra.test.conftest.medium_model()
cobra.test.conftest.salmonella(medium_model)
cobra.test.conftest.solved_model(data_directory)
cobra.test.conftest.tiny_toy_model()
cobra.test.conftest.fva_results(data_directory)
cobra.test.conftest.pfba_fva_results(data_directory)
cobra.test.conftest.opt_solver(request)
cobra.test.conftest.metabolites(model, request)
cobra.test.test_flux_analysis
Module Contents
cobra.test.test_flux_analysis.construct_ll_test_model()
cobra.test.test_flux_analysis.ll_test_model(request)
cobra.test.test_flux_analysis.construct_room_model()
cobra.test.test_flux_analysis.construct_room_solution()
cobra.test.test_flux_analysis.construct_geometric_fba_model()
cobra.test.test_flux_analysis.captured_output()
A context manager to test the IO summary methods.
class cobra.test.test_flux_analysis.TestCobraFluxAnalysis
Test the simulation functions in cobra.flux_analysis.
test_pfba_benchmark(large_model, benchmark, solver)
test_pfba(model, solver)
test_geometric_fba_benchmark(model, benchmark, solver)
test_geometric_fba(solver)
test_single_gene_deletion_fba_benchmark(model, benchmark, solver)
test_single_gene_deletion_fba(model, solver)
test_single_gene_deletion_moma_benchmark(model, benchmark, solver)
test_single_gene_deletion_linear_moma_benchmark(model, benchmark, solver)
test_moma_sanity(model, solver)
Test optimization criterion and optimality.
test_single_gene_deletion_moma(model, solver)
test_single_gene_deletion_moma_reference(model, solver)
test_linear_moma_sanity(model, solver)
Test optimization criterion and optimality.
test_single_gene_deletion_linear_moma(model, solver)
test_single_gene_deletion_benchmark(model, benchmark, solver)
test_single_gene_deletion_room_benchmark(model, benchmark, solver)
test_single_gene_deletion_linear_room_benchmark(model, benchmark, solver)
test_room_sanity(model, solver)
test_linear_room_sanity(model, solver)
test_single_reaction_deletion_room(solver)
test_single_reaction_deletion_room_linear(solver)
test_single_reaction_deletion(model, solver)
compare_matrices(matrix1, matrix2, places=3)
test_double_gene_deletion_benchmark(large_model, benchmark)
test_double_gene_deletion(model)
test_double_reaction_deletion(model)
test_double_reaction_deletion_benchmark(large_model, benchmark)
test_flux_variability_benchmark(large_model, benchmark, solver)
test_flux_variability_loopless_benchmark(model, benchmark, solver)
test_pfba_flux_variability(model, pfba_fva_results, fva_results, solver)
test_flux_variability(model, fva_results, solver)
test_flux_variability_loopless(model, solver)
test_fva_data_frame(model)
test_fva_infeasible(model)
test_fva_minimization(model)
test_find_blocked_reactions_solver_none(model)
test_essential_genes(model)
test_essential_reactions(model)
test_find_blocked_reactions(model, solver)
test_loopless_benchmark_before(benchmark)
test_loopless_benchmark_after(benchmark)
test_loopless_solution(ll_test_model)
test_loopless_solution_fluxes(model)
test_add_loopless(ll_test_model)
test_gapfilling(salmonella)
check_line(output, expected_entries, pattern=compile)
Ensure each expected entry is in the output.
check_in_line(output, expected_entries, pattern=compile)
Ensure each expected entry is contained in the output.
test_model_summary_previous_solution(model, opt_solver, names)
test_model_summary(model, opt_solver, names)
test_model_summary_with_fva(model, opt_solver, fraction)
test_metabolite_summary_previous_solution(model, opt_solver, met)
test_metabolite_summary(model, opt_solver, met, names)
test_metabolite_summary_with_fva(model, opt_solver, fraction, met)
class cobra.test.test_flux_analysis.TestCobraFluxSampling
Tests and benchmark flux sampling.
test_single_achr(model)
test_single_optgp(model)
test_multi_optgp(model)
test_wrong_method(model)
test_validate_wrong_sample(model)
test_fixed_seed(model)
test_equality_constraint(model)
test_inequality_constraint(model)
setup_class()
test_achr_init_benchmark(model, benchmark)
test_optgp_init_benchmark(model, benchmark)
test_sampling()
test_achr_sample_benchmark(benchmark)
test_optgp_sample_benchmark(benchmark)
test_batch_sampling()
test_variables_samples()
test_inhomogeneous_sanity(model)
Test whether inhomogeneous sampling gives approximately the same standard deviation as a homo-
geneous version.
test_reproject()
test_complicated_model()
Difficult model since the online mean calculation is numerically unstable so many samples weakly
violate the equality constraints.
test_single_point_space(model)
Model where constraints reduce the sampling space to one point.
class cobra.test.test_flux_analysis.TestProductionEnvelope
Test the production envelope.
test_envelope_one(model)
test_envelope_multi_reaction_objective(model)
test_multi_variable_envelope(model, variables, num)
test_envelope_two(model)
class cobra.test.test_flux_analysis.TestReactionUtils
Test the assess_ functions in reactions.py.
test_assess(model, solver)
cobra.test.test_io
Module Contents
cobra.test.test_io.write_legacy_sbml_placeholder()
cobra.test.test_io.validate_json(filename)
cobra.test.test_io.read_pickle(filename, load_function=load)
cobra.test.test_io.write_pickle(model, filename, dump_function=dump)
cobra.test.test_io.raise_scipy_errors()
cobra.test.test_io.raise_libsbml_errors()
cobra.test.test_io.io_trial(request, data_directory)
class cobra.test.test_io.TestCobraIO
cobra.test.test_io_order
Module Contents
cobra.test.test_io_order.tmp_path(tmpdir_factory)
cobra.test.test_io_order.minimized_shuffle(small_model)
cobra.test.test_io_order.minimized_sorted(minimized_shuffle)
cobra.test.test_io_order.minimized_reverse(minimized_shuffle)
cobra.test.test_io_order.template(request, minimized_shuffle, minimized_reverse, mini-
mized_sorted)
cobra.test.test_io_order.attribute(request)
cobra.test.test_io_order.get_ids(iterable)
cobra.test.test_io_order.test_io_order(attribute, read, write, ext, template, tmp_path)
cobra.test.test_manipulation
Module Contents
class cobra.test.test_manipulation.TestManipulation
Test functions in cobra.manipulation
test_modify_reversible(model)
test_escape_ids(model)
test_rename_gene(model)
test_gene_knockout_computation(salmonella)
test_remove_genes()
test_sbo_annotation(model)
test_validate_formula_compartment(model)
test_validate_mass_balance(model)
test_prune_unused(model)
cobra.test.test_medium
Module Contents
class cobra.test.test_medium.TestModelMedium
test_model_medium(model)
class cobra.test.test_medium.TestTypeDetection
test_external_compartment(model)
test_exchange(model)
test_demand(model)
test_sink(model)
test_sbo_terms(model)
class cobra.test.test_medium.TestMinimalMedia
test_medium_linear(model)
test_medium_mip(model)
test_medium_alternative_mip(model)
test_benchmark_medium_linear(model, benchmark)
test_benchmark_medium_mip(model, benchmark)
test_medium_exports(model)
test_open_exchanges(model)
class cobra.test.test_medium.TestErrorsAndExceptions
test_no_boundary_reactions(empty_model)
test_no_boundary_reactions(empty_model)
cobra.test.test_model
Module Contents
class cobra.test.test_model.TestReactions
test_gpr()
test_gpr_modification(model)
test_gene_knock_out(model)
test_str()
test_add_metabolite_benchmark(model, benchmark, solver)
test_add_metabolite(model)
test_subtract_metabolite_benchmark(model, benchmark, solver)
test_subtract_metabolite(model, solver)
test_mass_balance(model)
test_build_from_string(model)
test_bounds_setter(model)
test_copy(model)
test_iadd(model)
test_add(model)
test_radd(model)
test_mul(model)
test_sub(model)
test_repr_html_(model)
class cobra.test.test_model.TestCobraMetabolites
test_metabolite_formula()
test_formula_element_setting(model)
test_repr_html_(model)
class cobra.test.test_model.TestCobraGenes
test_repr_html_(model)
class cobra.test.test_model.TestCobraModel
test core cobra functions
test_add_remove_reaction_benchmark(model, benchmark, solver)
test_add_metabolite(model)
test_remove_metabolite_subtractive(model)
test_remove_metabolite_destructive(model)
test_compartments(model)
test_add_reaction(model)
test_add_reaction_context(model)
test_add_reaction_from_other_model(model)
test_model_remove_reaction(model)
test_reaction_remove(model)
test_reaction_delete(model)
test_remove_gene(model)
test_exchange_reactions(model)
test_add_boundary(model, metabolites, reaction_type, prefix)
test_add_boundary_context(model, metabolites, reaction_type, prefix)
test_add_existing_boundary(model, metabolites, reaction_type)
test_copy_benchmark(model, solver, benchmark)
cobra.test.test_solver_model
Module Contents
cobra.test.test_solver_model.solved_model(request, model)
cobra.test.test_solver_model.same_ex(ex1, ex2)
Compare to expressions for mathematical equality.
class cobra.test.test_solver_model.TestSolution
test_solution_contains_only_reaction_specific_values()
class cobra.test.test_solver_model.TestReaction
test_str(model)
test_add_metabolite(solved_model)
test_removal_from_model_retains_bounds(model)
test_set_bounds_scenario_1(model)
test_set_bounds_scenario_3(model)
test_set_bounds_scenario_4(model)
test_set_upper_before_lower_bound_to_0(model)
test_set_bounds_scenario_2(model)
test_change_bounds(model)
test_make_irreversible(model)
test_make_reversible(model)
test_make_irreversible_irreversible_to_the_other_side(model)
test_make_lhs_irreversible_reversible(model)
test_model_less_reaction(model)
test_knockout(model)
test_reaction_without_model()
test_weird_left_to_right_reaction_issue(tiny_toy_model)
test_one_left_to_right_reaction_set_positive_ub(tiny_toy_model)
test_irrev_reaction_set_negative_lb(model)
test_twist_irrev_right_to_left_reaction_to_left_to_right(model)
test_set_lb_higher_than_ub_sets_ub_to_new_lb(model)
test_set_ub_lower_than_lb_sets_lb_to_new_ub(model)
test_add_metabolites_combine_true(model)
test_add_metabolites_combine_false(model)
test_reaction_imul(model)
test_remove_from_model(model)
test_change_id_is_reflected_in_solver(model)
class cobra.test.test_solver_model.TestSolverBasedModel
test_objective_coefficient_reflects_changed_objective(model)
test_change_objective_through_objective_coefficient(model)
test_transfer_objective(model)
test_model_from_other_model(model)
test_add_reactions(model)
test_add_reactions_single_existing(model)
test_add_reactions_duplicate(model)
test_add_cobra_reaction(model)
test_all_objects_point_to_all_other_correct_objects(model)
test_objects_point_to_correct_other_after_copy(model)
test_remove_reactions(model)
test_objective(model)
test_change_objective(model)
test_set_reaction_objective(model)
test_set_reaction_objective_str(model)
test_invalid_objective_raises(model)
test_solver_change(model)
test_no_change_for_same_solver(model)
test_invalid_solver_change_raises(model)
test_change_solver_to_cplex_and_check_copy_works(model)
test_copy_preserves_existing_solution(solved_model)
class cobra.test.test_solver_model.TestMetabolite
test_set_id(solved_model)
test_remove_from_model(solved_model)
cobra.test.test_solver_utils
Module Contents
class cobra.test.test_solver_utils.TestHelpers
test_solver_list()
test_interface_str()
test_solver_name()
test_choose_solver(model)
class cobra.test.test_solver_utils.TestObjectiveHelpers
test_linear_reaction_coefficients(model)
test_fail_non_linear_reaction_coefficients(model, solver)
class cobra.test.test_solver_utils.TestSolverMods
test_add_remove(model)
test_add_remove_in_context(model)
test_absolute_expression(model)
test_fix_objective_as_constraint(solver, model)
test_fix_objective_as_constraint_minimize(model, solver)
cobra.test.test_util
Module Contents
cobra.test.test_util.dict_list()
class cobra.test.test_util.TestDictList
test_contains(dict_list)
test_index(dict_list)
test_independent()
test_get_by_any(dict_list)
test_append(dict_list)
test_insert(dict_list)
test_extend(dict_list)
test_iadd(dict_list)
test_add(dict_list)
test_sub(dict_list)
test_isub(dict_list)
test_init_copy(dict_list)
test_slice(dict_list)
test_copy(dict_list)
test_deepcopy(dict_list)
test_pickle(dict_list)
test_query(dict_list)
test_removal()
test_set()
test_sort_and_reverse()
test_dir(dict_list)
test_union(dict_list)
cobra.test.test_util.test_show_versions(capsys)
Package Contents
cobra.test.create_test_model(model_name="salmonella")
Returns a cobra model for testing
model_name: str One of ‘ecoli’, ‘textbook’, or ‘salmonella’, or the path to a pickled cobra.Model
cobra.test.test_all(args=None)
alias for running all unit-tests on installed cobra
cobra.util
Submodules
cobra.util.array
Module Contents
cobra.util.array.create_stoichiometric_matrix(model, array_type="dense",
dtype=None)
Return a stoichiometric array representation of the given model.
The the columns represent the reactions and rows represent metabolites. S[i,j] therefore contains the quantity
of metabolite i produced (negative for consumed) by reaction j.
Parameters
• model (cobra.Model) – The cobra model to construct the matrix for.
Notes
Notes
To accomodate non-zero equalities the problem will add the variable “const_one” which is a variable that
equals one.
Parameters
• model (cobra.Model) – The model from which to obtain the LP problem.
• array_type (string) – The type of array to construct. if ‘dense’, return a standard
numpy.array, ‘dok’, or ‘lil’ will construct a sparse array using scipy of the correspond-
ing type and ‘DataFrame’ will give a pandas DataFrame with metabolite indices and
reaction columns.
• zero_tol (float) – The zero tolerance used to judge whether two bounds are the
same.
Returns
A named tuple consisting of 6 matrices and 2 vectors: - “equalities” is a matrix S such that
S*vars = b. It includes a row
for each constraint and one column for each variable.
• ”b” the right side of the equality equation such that S*vars = b.
• ”inequalities” is a matrix M such that lb <= M*vars <= ub. It contains a row for each
inequality and as many columns as variables.
• ”bounds” is a compound matrix [lb ub] containing the lower and upper bounds for the
inequality constraints in M.
• ”variable_fixed” is a boolean vector indicating whether the variable at that index is fixed
(lower bound == upper_bound) and is thus bounded by an equality constraint.
• ”variable_bounds” is a compound matrix [lb ub] containing the lower and upper bounds
for all variables.
cobra.util.context
Module Contents
class cobra.util.context.HistoryManager
Record a list of actions to be taken at a later time. Used to implement context managers that allow temporary
changes to a Model.
__init__()
__call__(operation)
Add the corresponding method to the history stack.
Parameters operation (function) – A function to be called at a later time
reset()
Trigger executions for all items in the stack in reverse order
cobra.util.context.get_context(obj)
Search for a context manager
cobra.util.context.resettable(f )
A decorator to simplify the context management of simple object attributes. Gets the value of the attribute
prior to setting it, and stores a function to set the value to the old value in the HistoryManager.
cobra.util.solver
Module Contents
cobra.util.solver.linear_reaction_coefficients(model, reactions=None)
Coefficient for the reactions in a linear objective.
Parameters
• model (cobra model) – the model object that defined the objective
• reactions (list) – an optional list for the reactions to get the coefficients for. All
reactions if left missing.
Returns A dictionary where the key is the reaction object and the value is the corresponding
coefficient. Empty dictionary if there are no linear terms in the objective.
Return type dict
cobra.util.solver._valid_atoms(model, expression)
Check whether a sympy expression references the correct variables.
Parameters
• model (cobra.Model) – The model in which to check for variables.
• expression (sympy.Basic) – A sympy expression.
Returns True if all referenced variables are contained in model, False otherwise.
Return type boolean
cobra.util.solver.set_objective(model, value, additive=False)
Set the model objective.
Parameters
• model (cobra model) – The model to set the objective for
• value (model.problem.Objective,) – e.g. optlang.glpk_interface.Objective,
sympy.Basic or dict
If the model objective is linear, the value can be a new Objective object or a dictionary
with linear coefficients where each key is a reaction and the element the new coefficient
(float).
If the objective is not linear and additive is true, only values of class Objective.
• additive (boolmodel.reactions.Biomass_Ecoli_core.bounds =
(0.1, 0.1)) – If true, add the terms to the current objective, otherwise start with an
empty objective.
cobra.util.solver.interface_to_str(interface)
Give a string representation for an optlang interface.
Parameters interface (string, ModuleType) – Full name of the interface in optlang
or cobra representation. For instance ‘optlang.glpk_interface’ or ‘optlang-glpk’.
Returns The name of the interface as a string
Return type string
cobra.util.solver.get_solver_name(mip=False, qp=False)
Select a solver for a given optimization problem.
Parameters
• mip (bool) – Does the solver require mixed integer linear programming capabilities?
• qp (bool) – Does the solver require quadratic programming capabilities?
Returns The name of feasible solver.
Return type string
cobra.util.util
Module Contents
cobra.util.util.format_long_string(string, max_length=50)
class cobra.util.util.AutoVivification
Implementation of perl’s autovivification feature. Checkout http://stackoverflow.com/a/652284/280182
__getitem__(item)
cobra.util.util.show_versions()
Print dependency information.
15.1.2 Submodules
cobra.exceptions
Module Contents
class cobra.exceptions.OptimizationError(message)
__init__(message)
class cobra.exceptions.Infeasible
class cobra.exceptions.Unbounded
class cobra.exceptions.FeasibleButNotOptimal
class cobra.exceptions.UndefinedSolution
class cobra.exceptions.SolverNotFound
A simple Exception when a solver can not be found.
• genindex
• modindex
• search
131
cobra Documentation, Release 0.13.3
c cobra.test.test_flux_analysis, 114
cobra, 55 cobra.test.test_io, 116
cobra.core, 55 cobra.test.test_io_order, 117
cobra.core.dictlist, 55 cobra.test.test_manipulation, 117
cobra.core.formula, 58 cobra.test.test_medium, 118
cobra.core.gene, 58 cobra.test.test_model, 118
cobra.core.metabolite, 60 cobra.test.test_solver_model, 120
cobra.core.model, 61 cobra.test.test_solver_utils, 122
cobra.core.object, 67 cobra.test.test_util, 122
cobra.core.reaction, 68 cobra.util, 123
cobra.core.solution, 74 cobra.util.array, 123
cobra.core.species, 77 cobra.util.context, 125
cobra.exceptions, 129 cobra.util.solver, 125
cobra.flux_analysis, 77 cobra.util.util, 128
cobra.flux_analysis.deletion, 77
cobra.flux_analysis.gapfilling, 81
cobra.flux_analysis.geometric, 83
cobra.flux_analysis.loopless, 84
cobra.flux_analysis.moma, 85
cobra.flux_analysis.parsimonious, 87
cobra.flux_analysis.phenotype_phase_plane,
88
cobra.flux_analysis.reaction, 90
cobra.flux_analysis.room, 91
cobra.flux_analysis.sampling, 93
cobra.flux_analysis.summary, 99
cobra.flux_analysis.variability, 100
cobra.io, 102
cobra.io.dict, 102
cobra.io.json, 103
cobra.io.mat, 104
cobra.io.sbml, 105
cobra.io.sbml3, 107
cobra.io.yaml, 108
cobra.manipulation, 109
cobra.manipulation.annotate, 109
cobra.manipulation.delete, 109
cobra.manipulation.modify, 110
cobra.manipulation.validate, 111
cobra.medium, 111
cobra.medium.boundary_types, 111
cobra.medium.minimal_medium, 112
cobra.test, 113
cobra.test.conftest, 113
133
cobra Documentation, Release 0.13.3
135
cobra Documentation, Release 0.13.3
136 Index
cobra Documentation, Release 0.13.3
Index 137
cobra Documentation, Release 0.13.3
138 Index
cobra Documentation, Release 0.13.3
Index 139
cobra Documentation, Release 0.13.3
140 Index
cobra Documentation, Release 0.13.3
Index 141
cobra Documentation, Release 0.13.3
142 Index
cobra Documentation, Release 0.13.3
Index 143
cobra Documentation, Release 0.13.3
144 Index
cobra Documentation, Release 0.13.3
Index 145
cobra Documentation, Release 0.13.3
146 Index
cobra Documentation, Release 0.13.3
Index 147
cobra Documentation, Release 0.13.3
U Y
y (cobra.core.solution.LegacySolution attribute), 76
Unbounded (class in cobra.exceptions), 129
y (cobra.core.solution.Solution attribute), 75
UndefinedSolution (class in cobra.exceptions), 129
y() (cobra.core.metabolite.Metabolite method), 60
undelete_model_genes() (in module co-
y() (cobra.core.reaction.Reaction method), 71
bra.manipulation.delete), 109
y() (cobra.core.solution.Solution method), 75
union() (cobra.core.dictlist.DictList method), 56
y_dict (cobra.core.solution.LegacySolution attribute),
update_costs() (cobra.flux_analysis.gapfilling.GapFiller
76
method), 82
y_dict (cobra.core.solution.Solution attribute), 75
update_forward_and_reverse_bounds() (in module co-
y_dict() (cobra.core.solution.Solution method), 75
bra.core.reaction), 74
upper_bound() (cobra.core.reaction.Reaction method),
69
V
validate() (cobra.flux_analysis.gapfilling.GapFiller
method), 82
validate() (cobra.flux_analysis.sampling.HRSampler
method), 95
validate_json() (in module cobra.test.test_io), 116
validate_sbml_model() (in module cobra.io.sbml3),
107
variables() (cobra.core.model.Model method), 65
visit_BinOp() (cobra.core.gene.GPRCleaner method),
59
visit_BoolOp() (cobra.manipulation.delete._GeneRemover
method), 110
visit_Name() (cobra.core.gene.GPRCleaner method),
59
visit_Name() (cobra.manipulation.delete._GeneRemover
method), 110
visit_Name() (cobra.manipulation.modify._GeneEscaper
method), 110
148 Index