Documentation for COBRApy

For installation instructions, please see INSTALL.rst.

Many of the examples below are viewable as IPython notebooks, which can be viewed at nbviewer.

Getting Started

Loading a model and inspecting it

To begin with, cobrapy comes with bundled models for Salmonella and E. coli, as well as a “textbook” model of E. coli core metabolism. To load a test model, type

In [1]:
from __future__ import print_function

import cobra
import cobra.test

# "ecoli" and "salmonella" are also valid arguments
model = cobra.test.create_test_model("textbook")

The reactions, metabolites, and genes attributes of the cobrapy model are a special type of list called a 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

When using Jupyter notebook this type of information is rendered as a table.

In [3]:
model
Out[3]:
Name e_coli_core
Memory address 0x01116ea9e8
Number of metabolites 72
Number of reactions 95
Objective expression -1.0*Biomass_Ecoli_core_reverse_2cdba + 1.0*Biomass_Ecoli_core
Compartments cytosol, extracellular

Just like a regular list, objects in the DictList can be retrieved by index. For example, to get the 30th reaction in the model (at index 29 because of 0-indexing):

In [4]:
model.reactions[29]
Out[4]:
Reaction identifierEX_glu__L_e
NameL-Glutamate exchange
Memory address 0x011b8643c8
Stoichiometry

glu__L_e -->

L-Glutamate -->

GPR
Lower bound0.0
Upper bound1000.0

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")
Out[5]:
Metabolite identifieratp_c
NameATP
Memory address 0x011b7f82b0
FormulaC10H12N5O13P3
Compartmentc
In 13 reaction(s) PYK, GLNS, ATPS4r, SUCOAS, PPCK, GLNabc, ATPM, ACKr, Biomass_Ecoli_core, ADK1, PPS, PFK, PGK

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)

Reactions

We will consider the reaction glucose 6-phosphate isomerase, which interconverts glucose 6-phosphate and fructose 6-phosphate. The reaction id for this reaction in our test model is PGI.

In [7]:
pgi = model.reactions.get_by_id("PGI")
pgi
Out[7]:
Reaction identifierPGI
Nameglucose-6-phosphate isomerase
Memory address 0x011b886a90
Stoichiometry

g6p_c <=> f6p_c

D-Glucose 6-phosphate <=> D-Fructose 6-phosphate

GPRb4025
Lower bound-1000.0
Upper bound1000.0

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'

The reaction is no longer mass balanced

In [11]:
pgi.check_mass_balance()
Out[11]:
{'H': -1.0, 'charge': -1.0}

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"
unknown metabolite 'green_eggs' created
unknown metabolite 'ham' created
In [14]:
pgi.reaction
Out[14]:
'g6p_c --> f6p_c + green_eggs + h_c + ham'
In [15]:
pgi.reaction = "g6p_c <=> f6p_c"
pgi.reaction
Out[15]:
'g6p_c <=> f6p_c'

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 identifieratp_c
NameATP
Memory address 0x011b7f82b0
FormulaC10H12N5O13P3
Compartmentc
In 13 reaction(s) PYK, GLNS, ATPS4r, SUCOAS, PPCK, GLNabc, ATPM, ACKr, Biomass_Ecoli_core, ADK1, PPS, PFK, PGK

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

We can see the chemical formula for the metabolite as well.

In [19]:
print(atp.formula)
C10H12N5O13P3

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

A metabolite like glucose 6-phosphate will participate in fewer reactions.

In [21]:
model.metabolites.get_by_id("g6p_c").reactions
Out[21]:
frozenset({<Reaction G6PDH2r at 0x11b870c88>,
           <Reaction GLCpts at 0x11b870f98>,
           <Reaction PGI at 0x11b886a90>,
           <Reaction Biomass_Ecoli_core at 0x11b85a5f8>})

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'

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 identifierb4025
Namepgi
Memory address 0x011b844cc0
FunctionalTrue
In 1 reaction(s) PGI

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()

Newly created genes are also added to the model

In [28]:
model.genes.get_by_id("spam")
Out[28]:
Gene identifierspam
Name
Memory address 0x011b850908
FunctionalTrue
In 1 reaction(s) PGI

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

The undelete_model_genes can be used to reset a gene deletion

In [30]:
cobra.manipulation.undelete_model_genes(model)
print(pgi.lower_bound, "< pgi <", pgi.upper_bound)
-1000 < pgi < 1000

Making changes reversibly using models as contexts

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)]

Nested contexts are also supported

In [33]:
print('original objective: ', model.objective.expression)
with model:
    model.objective = 'ATPM'
    print('print objective in first context:', model.objective.expression)
    with model:
        model.objective = 'ACALD'
        print('print objective in second context:', model.objective.expression)
    print('objective after exiting second context:',
          model.objective.expression)
print('back to original objective:', model.objective.expression)
original objective:  -1.0*Biomass_Ecoli_core_reverse_2cdba + 1.0*Biomass_Ecoli_core
print objective in first context: -1.0*ATPM_reverse_5b752 + 1.0*ATPM
print objective in second context: 1.0*ACALD - 1.0*ACALD_reverse_fda2b
objective after exiting second context: -1.0*ATPM_reverse_5b752 + 1.0*ATPM
back to original objective: -1.0*Biomass_Ecoli_core_reverse_2cdba + 1.0*Biomass_Ecoli_core

Most methods that modify the model are supported like this including adding and removing reactions and metabolites and setting the objective. Supported methods and functions mention this in the corresponding documentation.

While it does not have any actual effect, for syntactic convenience it is also possible to refer to the model by a different name than outside the context. Such as

In [34]:
with model as inner:
    inner.reactions.PFK.knock_out

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] \(\rightarrow\) 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',
    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
})

reaction.reaction  # This gives a string representation of the reaction
Out[4]:
'ddcaACP_c + h_c + malACP_c --> 3omrsACP_c + ACP_c + co2_c'

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. We will assign the gene reaction rule string, which will automatically create the corresponding gene objects.

In [5]:
reaction.gene_reaction_rule = '( STM2378 or STM1197 )'
reaction.genes
Out[5]:
frozenset({<Gene STM1197 at 0x7f2d85786898>, <Gene STM2378 at 0x7f2dc45437f0>})

At this point in time, the model is still empty

In [6]:
print('%i reactions initially' % len(model.reactions))
print('%i metabolites initially' % len(model.metabolites))
print('%i genes initially' % len(model.genes))
0 reactions initially
0 metabolites initially
0 genes initially

We will add the reaction to the model, which will also add all associated metabolites and genes

In [7]:
model.add_reactions([reaction])

# Now there are things in the model
print('%i reaction' % len(model.reactions))
print('%i metabolites' % len(model.metabolites))
print('%i genes' % len(model.genes))
1 reaction
6 metabolites
2 genes

We can iterate through the model objects to observe the contents

In [8]:
# Iterate through the the objects in the model
print("Reactions")
print("---------")
for x in model.reactions:
    print("%s : %s" % (x.id, x.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.

Reading and Writing Models

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

print("mini test files: ")
print(", ".join(i for i in os.listdir(data_dir) if i.startswith("mini")))

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_fbc2.xml.bz2, mini_fbc2.xml.gz

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]:
Name mini_textbook
Memory address 0x01074fd080
Number of metabolites 23
Number of reactions 18
Objective expression -1.0*ATPM_reverse_5b752 - 1.0*PFK_reverse_d24a6 + 1.0*PFK + 1.0*ATPM
Compartments cytosol, extracellular
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.

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]:
Name mini_textbook
Memory address 0x0112fa6b38
Number of metabolites 23
Number of reactions 18
Objective expression -1.0*ATPM_reverse_5b752 - 1.0*PFK_reverse_d24a6 + 1.0*PFK + 1.0*ATPM
Compartments cytosol, extracellular
In [5]:
cobra.io.write_sbml_model(
    textbook_model, "test_cobra.xml", use_fbc_package=False)

JSON

Cobrapy models have a JSON (JavaScript Object Notation) representation. This format was created for interoperability with escher.

In [6]:
cobra.io.load_json_model(join(data_dir, "mini.json"))
Out[6]:
Name mini_textbook
Memory address 0x0113061080
Number of metabolites 23
Number of reactions 18
Objective expression -1.0*ATPM_reverse_5b752 - 1.0*PFK_reverse_d24a6 + 1.0*PFK + 1.0*ATPM
Compartments cytosol, extracellular
In [7]:
cobra.io.save_json_model(textbook_model, "test.json")

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]:
Name mini_textbook
Memory address 0x0113013390
Number of metabolites 23
Number of reactions 18
Objective expression -1.0*ATPM_reverse_5b752 - 1.0*PFK_reverse_d24a6 + 1.0*PFK + 1.0*ATPM
Compartments extracellular, cytosol
In [9]:
cobra.io.save_yaml_model(textbook_model, "test.yml")

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]:
Name mini_textbook
Memory address 0x0113000b70
Number of metabolites 23
Number of reactions 18
Objective expression -1.0*ATPM_reverse_5b752 - 1.0*PFK_reverse_d24a6 + 1.0*PFK + 1.0*ATPM
Compartments c, e

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]:
Name mini_textbook
Memory address 0x0113758438
Number of metabolites 23
Number of reactions 18
Objective expression -1.0*ATPM_reverse_5b752 - 1.0*PFK_reverse_d24a6 + 1.0*PFK + 1.0*ATPM
Compartments c, e

Saving models to mat files is also relatively straightforward

In [12]:
cobra.io.save_matlab_model(textbook_model, "test.mat")

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.

Simulating with FBA

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")

Running FBA

In [2]:
solution = model.optimize()
print(solution)
<Solution 0.874 at 0x112eb3d30>

The Model.optimize() function will return a Solution object. A solution object has several attributes:

  • objective_value: the objective value
  • status: the status from the linear programming solver
  • fluxes: a pandas series with flux indexed by reaction identifier. The flux for a reaction variable is the difference of the primal values for the forward and reverse reaction variables.
  • shadow_prices: a pandas series with shadow price indexed by the metabolite identifier.

For example, after the last call to model.optimize(), if the optimization succeeds it’s status will be optimal. In case the model is infeasible an error is raised.

In [3]:
solution.objective_value
Out[3]:
0.8739215069684307

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
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

Analyzing FBA solutions

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....

CONSUMING REACTIONS -- Nicotinamide adenine dinucleotide - reduced (nadh_c)
---------------------------------------------------------------------------
%       FLUX  RXN ID      REACTION
----  ------  ----------  --------------------------------------------------
100%   38.5   NADH16      4.0 h_c + nadh_c + q8_c --> 3.0 h_e + nad_c + q...

Or to get a sense of the main energy production and consumption reactions

In [8]:
model.metabolites.atp_c.summary()
PRODUCING REACTIONS -- ATP (atp_c)
----------------------------------
%      FLUX  RXN ID      REACTION
---  ------  ----------  --------------------------------------------------
67%  45.5    ATPS4r      adp_c + 4.0 h_e + pi_c <=> atp_c + h2o_c + 3.0 h_c
23%  16      PGK         3pg_c + atp_c <=> 13dpg_c + adp_c
7%    5.06   SUCOAS      atp_c + coa_c + succ_c <=> adp_c + pi_c + succoa_c
3%    1.76   PYK         adp_c + h_c + pep_c --> atp_c + pyr_c

CONSUMING REACTIONS -- ATP (atp_c)
----------------------------------
%      FLUX  RXN ID      REACTION
---  ------  ----------  --------------------------------------------------
76%  52.3    Biomass...  1.496 3pg_c + 3.7478 accoa_c + 59.81 atp_c + 0....
12%   8.39   ATPM        atp_c + h2o_c --> adp_c + h_c + pi_c
11%   7.48   PFK         atp_c + f6p_c --> adp_c + fdp_c + h_c
0%    0.223  GLNS        atp_c + glu__L_c + nh4_c --> adp_c + gln__L_c +...

Changing the Objectives

The objective function is determined from the objective_coefficient attribute of the objective reaction(s). Generally, 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"

# The upper bound should be 1000, so that we get
# the actual optimal value
model.reactions.get_by_id("ATPM").upper_bound = 1000.
linear_reaction_coefficients(model)
Out[11]:
{<Reaction ATPM at 0x112eab470>: 1.0}
In [12]:
model.optimize().objective_value
Out[12]:
174.99999999999966

We can also have more complicated objectives including quadratic terms.

Running FVA

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% optimality.

In [15]:
cobra.flux_analysis.flux_variability_analysis(
    model, model.reactions[:10], fraction_of_optimum=0.9)
Out[15]:
maximum minimum
ACALD 0.000000e+00 -2.692308
ACALDt 0.000000e+00 -2.692308
ACKr 6.635712e-30 -4.117647
ACONTa 2.000000e+01 8.461538
ACONTb 2.000000e+01 8.461538
ACt2r 0.000000e+00 -4.117647
ADK1 1.750000e+01 0.000000
AKGDH 2.000000e+01 2.500000
AKGt2r 2.651196e-16 -1.489362
ALCD2x 0.000000e+00 -2.333333

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

Running FVA in summary methods

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 +...
0%         0  [0, 8.75]     ME2         mal__L_c + nadp_c --> co2_c + nadph_c...

CONSUMING REACTIONS -- Pyruvate (pyr_c)
---------------------------------------
%       FLUX  RANGE         RXN ID      REACTION
----  ------  ------------  ----------  ----------------------------------------
100%      20  [13, 28.8]    PDH         coa_c + nad_c + pyr_c --> accoa_c + c...
0%         0  [0, 8.75]     PPS         atp_c + h2o_c + pyr_c --> amp_c + 2.0...
0%         0  [0, 5.83]     PFL         coa_c + pyr_c --> accoa_c + for_c
0%         0  [0, 1.35]     PYRt2       h_e + pyr_e <=> h_c + pyr_c
0%         0  [0, 1.13]     LDH_D       lac__D_c + nad_c <=> h_c + nadh_c + p...
0%         0  [0, 0.132]    Biomass...  1.496 3pg_c + 3.7478 accoa_c + 59.81 ...

In these summary methods, the values are reported as a the center point +/- the range of the FVA solution, calculated from the maximum and minimum values.

Running pFBA

Parsimonious FBA (often written pFBA) finds a flux distribution which gives the optimal growth rate, but minimizes 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)

These functions should give approximately the same objective value.

In [21]:
abs(fba_solution.fluxes["Biomass_Ecoli_core"] - pfba_solution.fluxes[
    "Biomass_Ecoli_core"])
Out[21]:
7.7715611723760958e-16

Running geometric FBA

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]:
Optimal solution with objective value 0.000
fluxes reduced_costs
ACALD 1.785214e-14 0.0
ACALDt 1.785214e-14 0.0
ACKr 0.000000e+00 0.0
ACONTa 6.007250e+00 0.0
ACONTb 6.007250e+00 0.0
... ... ...
TALA 1.496984e+00 0.0
THD2 1.522652e-14 0.0
TKT1 1.496984e+00 0.0
TKT2 1.181498e+00 0.0
TPI 7.477382e+00 0.0

95 rows × 2 columns

Simulating Deletions

In [1]:
import pandas
from time import time

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")

Knocking out single genes and reactions

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>

Single Deletions

Perform all single gene deletions on a model

In [4]:
deletion_results = single_gene_deletion(cobra_model)

These can also be done for only a subset of genes

In [5]:
single_gene_deletion(cobra_model, cobra_model.genes[:20])
Out[5]:
flux status
b0116 0.782351 optimal
b0118 0.873922 optimal
b0351 0.873922 optimal
b0356 0.873922 optimal
b0474 0.873922 optimal
b0726 0.858307 optimal
b0727 0.858307 optimal
b1241 0.873922 optimal
b1276 0.873922 optimal
b1478 0.873922 optimal
b1849 0.873922 optimal
b2296 0.873922 optimal
b2587 0.873922 optimal
b3115 0.873922 optimal
b3732 0.374230 optimal
b3733 0.374230 optimal
b3734 0.374230 optimal
b3735 0.374230 optimal
b3736 0.374230 optimal
s0001 0.211141 optimal

This can also be done for reactions

In [6]:
single_reaction_deletion(cobra_model, cobra_model.reactions[:20])
Out[6]:
flux status
ACALD 8.739215e-01 optimal
ACALDt 8.739215e-01 optimal
ACKr 8.739215e-01 optimal
ACONTa -5.039994e-13 optimal
ACONTb -1.477823e-12 optimal
ACt2r 8.739215e-01 optimal
ADK1 8.739215e-01 optimal
AKGDH 8.583074e-01 optimal
AKGt2r 8.739215e-01 optimal
ALCD2x 8.739215e-01 optimal
ATPM 9.166475e-01 optimal
ATPS4r 3.742299e-01 optimal
Biomass_Ecoli_core 0.000000e+00 optimal
CO2t 4.616696e-01 optimal
CS 1.129472e-12 optimal
CYTBD 2.116629e-01 optimal
D_LACt2 8.739215e-01 optimal
ENO 1.161773e-14 optimal
ETOHt2r 8.739215e-01 optimal
EX_ac_e 8.739215e-01 optimal

Double Deletions

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)

start = time()  # start timer()
double_gene_deletion(
    ecoli_model, ecoli_model.genes[:300], number_of_processes=1)
t2 = time() - start
print("Double gene deletions for 200 genes completed in "
      "%.2f sec with 1 core" % t2)

print("Speedup of %.2fx" % (t2 / t1))
Double gene deletions for 200 genes completed in 33.26 sec with 2 cores
Double gene deletions for 200 genes completed in 45.38 sec with 1 core
Speedup of 1.36x

Double deletions can also be run for reactions.

In [9]:
double_reaction_deletion(
    cobra_model, cobra_model.reactions[2:7], return_frame=True).round(4)
Out[9]:
ACKr ACONTa ACONTb ACt2r ADK1
ACKr 0.8739 0.0 0.0 0.8739 0.8739
ACONTa 0.0000 0.0 0.0 0.0000 0.0000
ACONTb 0.0000 0.0 0.0 0.0000 -0.0000
ACt2r 0.8739 0.0 0.0 0.8739 0.8739
ADK1 0.8739 0.0 -0.0 0.8739 0.8739

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 flux_minimum mass_yield_maximum mass_yield_minimum EX_glc__D_e EX_o2_e
0 EX_glc__D_e 1.442300e-13 0.0 0.000000 0.0 NaN NaN -10.0 -60.000000
1 EX_glc__D_e 1.310050e+00 0.0 0.072244 0.0 NaN NaN -10.0 -56.842105
2 EX_glc__D_e 2.620100e+00 0.0 0.144488 0.0 NaN NaN -10.0 -53.684211
3 EX_glc__D_e 3.930150e+00 0.0 0.216732 0.0 NaN NaN -10.0 -50.526316
4 EX_glc__D_e 5.240200e+00 0.0 0.288975 0.0 NaN NaN -10.0 -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.

In [4]:
prod_env = production_envelope(
    model, ["EX_o2_e"], objective="EX_ac_e", carbon_sources="EX_glc__D_e")
In [5]:
prod_env.head()
Out[5]:
carbon_source carbon_yield_maximum carbon_yield_minimum flux_maximum flux_minimum mass_yield_maximum mass_yield_minimum EX_o2_e
0 EX_glc__D_e 2.385536e-15 0.0 0.000000 0.0 2.345496e-15 0.0 -60.000000
1 EX_glc__D_e 5.263158e-02 0.0 1.578947 0.0 5.174819e-02 0.0 -56.842105
2 EX_glc__D_e 1.052632e-01 0.0 3.157895 0.0 1.034964e-01 0.0 -53.684211
3 EX_glc__D_e 1.578947e-01 0.0 4.736842 0.0 1.552446e-01 0.0 -50.526316
4 EX_glc__D_e 2.105263e-01 0.0 6.315789 0.0 2.069927e-01 0.0 -47.368421
In [6]:
%matplotlib inline
In [7]:
prod_env.plot(
    kind='line', x='EX_o2_e', y='carbon_yield_maximum');
_images/phenotype_phase_plane_10_0.png

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

Basic usage

The easiest way to get started with flux sampling is using the sample function in the flux_analysis submodule. 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 AKGDH AKGt2r ALCD2x ... RPI SUCCt2_2 SUCCt3 SUCDi SUCOAS TALA THD2 TKT1 TKT2 TPI
0 -0.577302 -0.149662 -0.338001 10.090870 10.090870 -0.338001 0.997694 4.717467 -0.070599 -0.427639 ... -2.255649 6.152278 6.692068 821.012351 -4.717467 2.230392 133.608893 2.230392 2.220236 5.263234
1 -0.639279 -0.505704 -0.031929 10.631865 10.631865 -0.031929 4.207702 6.763224 -0.024720 -0.133575 ... -0.769792 14.894313 14.929989 521.410118 -6.763224 0.755207 66.656770 0.755207 0.749341 7.135499
2 -1.983410 -0.434676 -0.408318 11.046294 11.046294 -0.408318 5.510960 7.240802 -0.501086 -1.548735 ... -0.088852 15.211972 15.807554 756.884622 -7.240802 0.065205 42.830676 0.065205 0.055695 8.109647
3 -1.893551 -0.618887 -0.612598 8.879426 8.879426 -0.612598 6.194372 5.382222 -0.563573 -1.274664 ... -1.728387 15.960829 17.404437 556.750972 -5.382222 1.714682 37.386748 1.714682 1.709171 7.003325
4 -1.759520 -0.321021 -0.262520 10.801480 10.801480 -0.262520 4.815146 9.236588 -0.359817 -1.438499 ... -2.840577 12.379023 13.141259 440.132011 -9.236588 2.822071 0.292885 2.822071 2.814629 6.205245

5 rows × 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.

In [2]:
print("One process:")
%time s = sample(model, 1000)
print("Two processes:")
%time s = sample(model, 1000, processes=2)
One process:
CPU times: user 7.91 s, sys: 4.09 s, total: 12 s
Wall time: 6.52 s
Two processes:
CPU times: user 288 ms, sys: 495 ms, total: 783 ms
Wall time: 3.52 s

Alternatively you can also user Artificial Centering Hit-and-Run for sampling by setting the method to achr. achr does not support parallel execution but has good convergence and is almost Markovian.

In [3]:
s = sample(model, 100, method="achr")

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.

Advanced usage

Sampler objects

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)

Sampling and validation

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
  • “u” - upper bound violation
  • “e” - equality violation (meaning the point is not a steady state)

For instance for a random flux distribution (should not be feasible):

In [8]:
import numpy as np

bad = np.random.uniform(-1000, 1000, size=len(model.reactions))
achr.validate(np.atleast_2d(bad))
Out[8]:
array(['le'], dtype='<U3')

And for our generated samples:

In [9]:
achr.validate(s1)
Out[9]:
array(['v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v',
       'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v',
       'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v',
       'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v',
       'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v',
       'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v',
       'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v',
       'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v'], dtype='<U3')

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

Batch sampling

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...

Adding constraints

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

As we can see our new constraint was respected.

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

Loopless solution

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>
_images/loopless_7_1.png

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.

Loopless model

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 \(\rightarrow\) B \(\rightarrow\) C \(\rightarrow\) 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()
_images/loopless_11_0.png
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.reactions.v1.add_metabolites({"A": -1, "B": 1})
model.reactions.v2.add_metabolites({"B": -1, "C": 1})
model.reactions.v3.add_metabolites({"C": -1, "A": 1})

model.objective = 'DM_C'

While this model contains a loop, a flux state exists which has no flux through reaction v\(_3\), 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 v\(_3\), 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'
In [10]:
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 = 1.0

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 \(G \approx \Delta G\) proxy variables and make loops thermodynamically infeasible. This is achieved by the following formulation.

\[\begin{split}\begin{eqnarray} &\text{maximize } v_{obj} \\ s.t. & Sv = 0\\ & lb_j \leq v_j \leq ub_j \\ & -M\cdot (1 - a_i) \leq v_i \leq M\cdot a_i\\ & -1000a_i + (1 - a_i) \leq G_i \leq -a_i + 1000(1 - a_i)\\ & N_{int}G = 0\\ & a_i \in \{0, 1\} \end{eqnarray}\end{split}\]

Here the index j runs over all reactions and the index i only over internal ones. \(a_i\) 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” \(\Delta G\) values). \(N_{int}\) 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:

\[\sum_i c_i * z_i\]

subject to

\[Sv = 0\]
\[v^\star \geq t\]
\[l_i\leq v_i \leq u_i\]
\[v_i = 0 \textrm{ if } z_i = 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, \(v^\star\) 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])

Now, because of these gaps, the model won’t grow.

In [3]:
model.optimize().objective_value
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 models you may want to consider alternative gap filling methods and reconstruction methods.

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}

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.

Minimal media

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

So we see that growth is actually limited by glucose import.

Alternatively you might be interested in a minimal medium with the smallest number of active imports. This can be achieved by using the minimize_components argument (note that this uses a MIP formulation and will therefore be much slower).

In [6]:
minimal_medium(model, 0.1, minimize_components=True)
Out[6]:
EX_glc__D_e    10.000000
EX_nh4_e        1.042503
EX_pi_e         0.703318
dtype: float64

When minimizing the number of import fluxes there may be many alternative solutions. To obtain several of those you can also pass a positive integer to minimize_components which will give you at most that many 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 5
EX_fru_e 0.000000 0.000000 523.104557 0.000000 0.000000 0.000000
EX_glc__D_e 0.000000 0.000000 0.000000 523.104557 521.357767 519.750758
EX_gln__L_e 0.000000 0.000000 0.000000 0.000000 40.698058 0.000000
EX_glu__L_e 23.468185 348.101944 83.995843 83.995843 0.000000 0.000000
EX_mal__L_e 1000.000000 0.000000 0.000000 0.000000 0.000000 0.000000
EX_nh4_e 0.000000 0.000000 0.000000 0.000000 0.000000 81.026921
EX_o2_e 0.000000 500.000000 0.000000 0.000000 0.000000 0.000000
EX_pi_e 15.667461 66.431529 56.667310 56.667310 54.913419 54.664344

So there are 4 alternative solutions in total. One aerobic and three anaerobic ones using different carbon sources.

Boundary reactions

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>]

For demand reactions:

In [9]:
ecoli.demands
Out[9]:
[<Reaction DM_4CRSOL at 0x7f3921144b70>,
 <Reaction DM_5DRIB at 0x7f3921078b38>,
 <Reaction DM_AACALD at 0x7f3921078be0>,
 <Reaction DM_AMOB at 0x7f3921078c50>,
 <Reaction DM_MTHTHF at 0x7f3921078cf8>,
 <Reaction DM_OXAM at 0x7f3921078d68>]

For sink reactions:

In [10]:
ecoli.sinks
Out[10]:
[]

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>,
 <Reaction DM_AACALD at 0x7f3921078be0>,
 <Reaction DM_AMOB at 0x7f3921078c50>,
 <Reaction DM_MTHTHF at 0x7f3921078cf8>,
 <Reaction DM_OXAM at 0x7f3921078d68>,
 <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>]

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

Internal solver interfaces

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.

Tailored constraints, variables and objectives

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 combination of different reaction and their corresponding upper and lower bounds. Here we demonstrate this optlang functionality which is exposed via the model.solver.interface.

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:

In [5]:
coefficients = dict()
for rxn in model.reactions:
    coefficients[rxn.forward_variable] = 1.
    coefficients[rxn.reverse_variable] = 1.
constraint = model.problem.Constraint(0, lb=0, ub=100)
model.add_cons_vars(constraint)
model.solver.update()
constraint.set_linear_coefficients(coefficients=coefficients)

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

The objectives mathematical expression is seen by

In [6]:
model.objective.expression
Out[6]:
-1.0*Biomass_Ecoli_core_reverse_2cdba + 1.0*Biomass_Ecoli_core

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 \(\frac{1}{2}\left(x^2 + y^2 \right) - y\)

subject to

\(x + y = 2\)

\(x \ge 0\)

\(y \ge 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()
_images/constraints_objectives_19_0.png

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)

Next we add the quadratic objective

In [9]:
quadratic_objective = model.problem.Objective(
    0.5 * model.reactions.NH4t.flux_expression**2 + 0.5 *
    model.reactions.FBA.flux_expression**2 -
    model.reactions.FBA.flux_expression,
    direction='min')
model.objective = quadratic_objective
solution = model.optimize(objective_sense=None)
In [10]:
print(solution.fluxes['NH4t'], solution.fluxes['FBA'])
0.5 1.5

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')

We use constraints to define what values this variable shall take

In [12]:
constraint = model.problem.Constraint(
    model.reactions.EX_glc__D_e.flux_expression -
    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

Using the COBRA toolbox with cobrapy

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 =

            rev: [95x1 double]
       metNames: {72x1 cell}
              b: [72x1 double]
      metCharge: [72x1 double]
              c: [95x1 double]
         csense: [72x1 char]
          genes: {137x1 cell}
    metFormulas: {72x1 cell}
           rxns: {95x1 cell}
        grRules: {95x1 cell}
       rxnNames: {95x1 cell}
    description: [11x1 char]
              S: [72x95 double]
             ub: [95x1 double]
             lb: [95x1 double]
           mets: {72x1 cell}
     subSystems: {95x1 cell}


First, we have to initialize the COBRA toolbox in MATLAB.

In [5]:
%%matlab --silent
warning('off'); % this works around a pymatbridge bug
addpath(genpath('~/cobratoolbox/'));
initCobraToolbox();

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.

How do I install cobrapy?

Please see the INSTALL.rst file.

How do I cite cobrapy?

Please cite the 2013 publication: 10.1186/1752-0509-7-74

How do I rename reactions or metabolites?

TL;DR Use Model.repair afterwards

When renaming metabolites or reactions, there are issues because cobra indexes based off of ID’s, which can cause errors. For example:

In [1]:
from __future__ import print_function
import cobra.test
model = cobra.test.create_test_model()

for metabolite in model.metabolites:
    metabolite.id = "test_" + metabolite.id

try:
    model.metabolites.get_by_id(model.metabolites[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 identifiertest_dcaACP_c
NameDecanoyl-ACP-n-C100ACP
Memory address 0x0110f09630
FormulaC21H39N2O8PRS

How do I delete a gene?

That depends on what precisely you mean by delete a gene.

If you want to simulate the model with a gene knockout, use the cobra.manipulation.delete_model_genes function. The effects of this function are reversed by cobra.manipulation.undelete_model_genes.

In [3]:
model = cobra.test.create_test_model()
PGI = model.reactions.get_by_id("PGI")
print("bounds before knockout:", (PGI.lower_bound, PGI.upper_bound))
cobra.manipulation.delete_model_genes(model, ["STM4221"])
print("bounds after knockouts", (PGI.lower_bound, PGI.upper_bound))
bounds before knockout: (-1000.0, 1000.0)
bounds after knockouts (0.0, 0.0)

If you want to actually remove all traces of a gene from a model, this is more difficult because this will require changing all the gene_reaction_rule strings for reactions involving the gene.

How do I change the reversibility of a Reaction?

Reaction.reversibility is a property in cobra which is computed when it is requested from the lower and upper bounds.

In [4]:
model = cobra.test.create_test_model()
model.reactions.get_by_id("PGI").reversibility
Out[4]:
True

Trying to set it directly will result in an error or warning:

In [5]:
try:
    model.reactions.get_by_id("PGI").reversibility = False
except Exception as e:
    print(repr(e))
cobra/core/reaction.py:501 UserWarning: Setting reaction reversibility is ignored

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

How do I generate an LP file from a COBRA model?

For optlang based solvers

With optlang solvers, the LP formulation of a model is obtained by it’s string representation. All solvers behave the same way.

In [7]:
with open('test.lp', 'w') as out:
    out.write(str(model.solver))

For cobrapy’s internal solvers

With the internal solvers, we first create the problem and use functions bundled with the solver.

Please note that unlike the LP file format, the MPS file format does not specify objective direction and is always a minimization. Some (but not all) solvers will rewrite the maximization as a minimization.

In [8]:
model = cobra.test.create_test_model()
# glpk through cglpk
glpk = cobra.solvers.cglpk.create_problem(model)
glpk.write("test.lp")
glpk.write("test.mps")  # will not rewrite objective
# cplex
cplex = cobra.solvers.cplex_solver.create_problem(model)
cplex.write("test.lp")
cplex.write("test.mps")  # rewrites objective

How do I visualize my flux solutions?

cobrapy works well with the escher package, which is well suited to this purpose. Consult the escher documentation for examples.

Sphinx AutoAPI Index

This page is the top-level of your generated API documentation. Below is a list of all items that are documented here.

cobra

Subpackages

cobra.core
Submodules
cobra.core.dictlist
Module Contents
class cobra.core.dictlist.DictList(*args)[source]

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)[source]

Instantiate a combined dict and list.

Parameters:args (iterable) – iterable as single argument to create new DictList from
has_id(id)[source]
_check(id)[source]

make sure duplicate id’s are not added. This function is called before adding in elements.

_generate_index()[source]

rebuild the _dict index

get_by_id(id)[source]

return the element with a matching id

list_attr(attribute)[source]

return a list of the given attribute for every object

get_by_any(iterable)[source]

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)[source]

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

>>> import cobra.test
>>> model = cobra.test.create_test_model('textbook')
>>> model.reactions.query(lambda x: x.boundary)
>>> import re
>>> regex = re.compile('^g', flags=re.IGNORECASE)
>>> model.metabolites.query(regex, attribute='name')
_replace_on_id(new_object)[source]

Replace an object by another with the same id.

append(object)[source]

append object to end

union(iterable)[source]

adds elements with id’s not already in the model

extend(iterable)[source]

extend list by appending elements from the iterable

_extend_nocheck(iterable)[source]

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)[source]

x.__sub__(y) <==> x - y

Parameters:other (iterable) – other must contain only unique id’s present in the list
__isub__(other)[source]

x.__sub__(y) <==> x -= y

Parameters:other (iterable) – other must contain only unique id’s present in the list
__add__(other)[source]

x.__add__(y) <==> x + y

Parameters:other (iterable) – other must contain only unique id’s which do not intersect with self
__iadd__(other)[source]

x.__iadd__(y) <==> x += y

Parameters:other (iterable) – other must contain only unique id’s whcih do not intersect with self
__reduce__()[source]
__getstate__()[source]

gets internal state

This is only provided for backwards compatibility so older versions of cobrapy can load pickles generated with cobrapy. In reality, the “_dict” state is ignored when loading a pickle

__setstate__(state)[source]

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)[source]

Determine the position in the list

id: A string or a Object

__contains__(object)[source]

DictList.__contains__(object) <==> object in DictList

object: str or Object

__copy__()[source]
insert(index, object)[source]

insert object before index

pop(*args)[source]

remove and return item at index (default last).

add(x)[source]

Opposite of remove. Mirrors set.add

remove(x)[source]

Warning

Internal use only

reverse()[source]

reverse IN PLACE

sort(cmp=None, key=None, reverse=False)[source]

stable sort IN PLACE

cmp(x, y) -> -1, 0, 1

__getitem__(i)[source]
__setitem__(i, y)[source]
__delitem__(index)[source]
__getslice__(i, j)[source]
__setslice__(i, j, y)[source]
__delslice__(i, j)[source]
__getattr__(attr)[source]
__dir__()[source]
cobra.core.formula
Module Contents
class cobra.core.formula.Formula(formula=None)[source]

Describes a Chemical Formula

Parameters:formula (string) – A legal formula string contains only letters and numbers.
__init__(formula=None)[source]
__add__(other_formula)[source]

Combine two molecular formulas.

Parameters:other_formula (Formula, str) – string for a chemical formula
Returns:The combined formula
Return type:Formula
parse_composition()[source]

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
cobra.core.gene.ast2str(expr, level=0, names=None)[source]

convert compiled ast to gene_reaction_rule str

Parameters:
  • expr (str) – string for a gene reaction rule, e.g “a and b”
  • level (int) – internal use only
  • names (dict) – Dict where each element id a gene identifier and the value is the gene name. Use this to get a rule str which uses names instead. This should be done for display purposes only. All gene_reaction_rule strings which are computed with should use the id.
Returns:

The gene reaction rule

Return type:

string

cobra.core.gene.eval_gpr(expr, knockouts)[source]

evaluate compiled ast of gene_reaction_rule with knockouts

Parameters:
  • expr (Expression) – The ast of the gene reaction rule
  • knockouts (DictList, set) – Set of genes that are knocked out
Returns:

True if the gene reaction rule is true with the given knockouts otherwise false

Return type:

bool

class cobra.core.gene.GPRCleaner[source]

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__()[source]
visit_Name(node)[source]
visit_BinOp(node)[source]
cobra.core.gene.parse_gpr(str_expr)[source]

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)[source]

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)[source]
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()[source]

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)[source]

Removes the association

Parameters:
  • model (cobra model) – The model to remove the gene from
  • make_dependent_reactions_nonfunctional (bool) – If True then replace 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_()[source]
cobra.core.metabolite
Module Contents
class cobra.core.metabolite.Metabolite(id=None, formula=None, name="", charge=None, compartment=None)[source]

Metabolite is a class for holding information regarding a metabolite in a cobra.Reaction object.

Parameters:
  • id (str) – the identifier to associate with the metabolite
  • formula (str) – Chemical formula (e.g. H2O)
  • name (str) – A human readable name.
  • charge (float) – The charge number of the metabolite
  • compartment (str or None) – Compartment of the metabolite.
__init__(id=None, formula=None, name="", charge=None, compartment=None)[source]
_set_id_with_model(value)[source]
constraint()

Get the constraints associated with this metabolite from the solve

Returns:the optlang constraint for this metabolite
Return type:optlang.<interface>.Constraint
elements()

Dictionary of elements as keys and their count in the metabolite as integer. When set, the formula property is update accordingly

elements(elements_dict)
formula_weight()

Calculate the formula weight

y()

The shadow price for the metabolite in the most recent solution

Shadow prices are computed from the dual values of the bounds in the solution.

shadow_price()

The shadow price in the most recent solution.

Shadow price is the dual value of the corresponding constraint in the model.

Warning

  • Accessing shadow prices through a Solution object is the safer, preferred, and only guaranteed 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 optimization 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

>>> import cobra
>>> import cobra.test
>>> model = cobra.test.create_test_model("textbook")
>>> solution = model.optimize()
>>> model.metabolites.glc__D_e.shadow_price
-0.09166474637510488
>>> solution.shadow_prices.glc__D_e
-0.091664746375104883
remove_from_model(destructive=False)[source]

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")[source]

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 solution 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 reported.
  • 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 previous 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 identifiers (default False).
  • floatfmt (string, optional) – Format string for floats (default ‘.3g’).
_repr_html_()[source]
cobra.core.model
Module Contents
class cobra.core.model.Model(id_or_model=None, name=None)[source]

Class representation for a cobra model

Parameters:
  • id_or_model (Model, string) – Either an existing Model object in which case a new model object is instantiated with the same properties as the original model, or a the identifier to associate with the model as a string.
  • name (string) – Human readable name for the model
reactions

DictList – A DictList where the key is the reaction identifier and the value a Reaction

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)[source]

Make sure all cobra.Objects in the model point to the model.

__getstate__()[source]

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)[source]
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

>>> import cobra.test
>>> model = cobra.test.create_test_model("textbook")
>>> new = model.problem.Constraint(model.objective.expression,
>>> lb=0.99)
>>> model.solver.add(new)
solver(value)
description()
description(value)
get_metabolite_compartments()[source]

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

>>> import cobra.test
>>> model = cobra.test.create_test_model("textbook")
>>> model.compartments = {'c': 'the cytosol'}
{'c': 'the cytosol', 'e': 'extracellular'}
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)[source]

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)[source]

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()[source]

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)[source]

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)[source]

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)[source]

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)[source]

Add a boundary reaction for a given metabolite.

There are three different types of pre-defined boundary reactions: exchange, demand, and sink reactions. An exchange reaction is a reversible, imbalanced reaction that adds to or removes an extracellular 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:
  • metabolite (cobra.Metabolite) – Any given metabolite. The compartment is not checked but you are encouraged to stick to the definition of exchanges and sinks.
  • type (str, {"exchange", "demand", "sink"}) – Using one of the pre-defined reaction types is easiest. If you want to create your own kind of boundary reaction choose any other string, e.g., ‘my-boundary’.
  • reaction_id (str, optional) – The ID of the resulting reaction. Only used for custom reactions.
  • lb (float, optional) – The lower bound of the resulting reaction. Only used for custom reactions.
  • ub (float, optional) – The upper bound of the resulting reaction. For the pre-defined reactions this default value determines all bounds.
Returns:

The created boundary reaction.

Return type:

cobra.Reaction

Examples

>>> import cobra.test
>>> model = cobra.test.create_test_model("textbook")
>>> demand = model.add_boundary(model.metabolites.atp_c, type="demand")
>>> demand.id
'DM_atp_c'
>>> demand.name
'ATP demand'
>>> demand.bounds
(0, 1000.0)
>>> demand.build_reaction_string()
'atp_c --> '
add_reactions(reaction_list)[source]

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)[source]

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)[source]

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:
  • what (list or tuple of optlang variables or constraints.) – The variables or constraints to add to the model. Must be of class optlang.interface.Variable or optlang.interface.Constraint.
  • **kwargs (keyword arguments) – Passed to solver.add()
remove_cons_vars(what)[source]

Remove variables and constraints from the model’s mathematical problem.

Remove variables and constraints that were added directly to the model’s underlying mathematical problem. Removals are reversed upon exit if the model itself is used as context.

Parameters:what (list or tuple of optlang variables or constraints.) – The variables or constraints to add to the model. Must be of class optlang.interface.Variable or optlang.interface.Constraint.
problem()

The interface to the model’s underlying mathematical problem.

Solutions to cobra models are obtained by formulating a mathematical problem and solving it. Cobrapy uses the optlang package to accomplish that and with this property you can get access to the problem interface directly.

Returns:The problem interface that defines methods for interacting with the problem and associated solver directly.
Return type:optlang.interface
variables()

The mathematical variables in the cobra model.

In a cobra model, most variables are reactions. However, for specific use cases, it may also be useful to have other types of variables. This property defines all variables currently associated with the model’s problem.

Returns:A container with all associated variables.
Return type:optlang.container.Container
constraints()

The constraints in the cobra model.

In a cobra model, most constraints are metabolites and their stoichiometries. However, for specific use cases, it may also be useful to have other types of constraints. This property defines all constraints currently associated with the model’s problem.

Returns:A container with all associated constraints.
Return type:optlang.container.Container
boundary()

Boundary reactions in the model. Reactions that either have no substrate or product.

exchanges()

Exchange reactions in model. Reactions that exchange mass with the exterior. Uses annotations and heuristics to exclude non-exchanges such as sink reactions.

demands()

Demand reactions in model. Irreversible reactions that accumulate or consume a metabolite in the inside of the model.

sinks()

Sink reactions in model. Reversible reactions that accumulate or consume a metabolite in the inside of the model.

_populate_solver(reaction_list, metabolite_list=None)[source]

Populate attached solver with constraints and variables that model the provided reactions.

slim_optimize(error_value=float, message=None)[source]

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 reactions/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 succeed.
Returns:

The objective value.

Return type:

float

optimize(objective_sense=None, raise_error=False)[source]

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)[source]

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 identifier), 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")[source]

Print a summary of the input and output fluxes of the model.

Parameters:
  • solution (cobra.Solution, optional) – A previously solved model solution 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 reported.
  • 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 previous 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 identifiers (default False).
  • floatfmt (string, optional) – Format string for floats (default ‘.3g’).
__enter__()[source]

Record all future changes to the model, undoing them when a call to __exit__ is received

__exit__(type, value, traceback)[source]

Pop the top context manager and trigger the undo functions

merge(right, prefix_existing=None, inplace=True, objective="left")[source]

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_()[source]
cobra.core.object
Module Contents
class cobra.core.object.Object(id=None, name="")[source]

Defines common behavior of object in cobra.core

__init__(id=None, name="")[source]

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)[source]
__getstate__()[source]

To prevent excessive replication during deepcopy.

__repr__()[source]
__str__()[source]
cobra.core.reaction
Module Contents
class cobra.core.reaction.Reaction(id=None, name="", subsystem="", lower_bound=0.0, upper_bound=1000.0, objective_coefficient=0.0)[source]

Reaction is a class for holding information regarding a biochemical reaction in a cobra.Model object.

Parameters:
  • id (string) – The identifier to associate with this reaction
  • name (string) – A human readable name for the reaction
  • subsystem (string) – Subsystem where the reaction is meant to occur
  • lower_bound (float) – The lower flux bound
  • upper_bound (float) – The upper flux bound
__init__(id=None, name="", subsystem="", lower_bound=0.0, upper_bound=1000.0, objective_coefficient=0.0)[source]
_set_id_with_model(value)[source]
reverse_id()

Generate the id of reverse_variable from the reaction’s id.

flux_expression()

Forward flux expression

Returns:The expression representing the the forward flux (if associated with model), otherwise None. Representing the net flux if model.reversible_encoding == ‘unsplit’ or None if reaction is not associated with a model
Return type:sympy expression
forward_variable()

An optlang variable representing the forward flux

Returns:An optlang variable for the forward flux or None if reaction is not associated with a model.
Return type:optlang.interface.Variable
reverse_variable()

An optlang variable representing the reverse flux

Returns:An optlang variable for the reverse flux or None if reaction is not associated with a model.
Return type:optlang.interface.Variable
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__()[source]
__deepcopy__(memo)[source]
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 guaranteed 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 optimization 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’.
  • AssertionError – If the flux value is not within the bounds.

Examples

>>> import cobra.test
>>> model = cobra.test.create_test_model("textbook")
>>> solution = model.optimize()
>>> model.reactions.PFK.flux
7.477381962160283
>>> solution.fluxes.PFK
7.4773819621602833
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 guaranteed 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 optimization 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

>>> import cobra.test
>>> model = cobra.test.create_test_model("textbook")
>>> solution = model.optimize()
>>> model.reactions.PFK.reduced_cost
-8.673617379884035e-18
>>> solution.reduced_costs.PFK
-8.6736173798840355e-18
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 reaction 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()[source]

Make sure all metabolites and genes that are associated with this reaction are aware of it.

remove_from_model(remove_orphans=False)[source]

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)[source]

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)[source]

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()[source]

Copy a reaction

The referenced metabolites and genes are also copied.

__add__(other)[source]

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)[source]
__sub__(other)[source]
__isub__(other)[source]
__imul__(coefficient)[source]

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)[source]
reactants()

Return a list of reactants for the reaction.

products()

Return a list of products for the reaction

get_coefficient(metabolite_id)[source]

Return the stoichiometric coefficient of a metabolite.

Parameters:metabolite_id (str or cobra.Metabolite) –
get_coefficients(metabolite_ids)[source]

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)[source]

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)[source]

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

  • A final coefficient < 0 implies a reactant.
  • The change is reverted upon exit when using the model as a context.
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)[source]

Generate a human readable reaction string

check_mass_balance()[source]

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()[source]

lists compartments the metabolites are in

_associate_gene(cobra_gene)[source]

Associates a cobra.Gene object with a cobra.Reaction.

Parameters:cobra_gene (cobra.core.Gene.Gene) –
_dissociate_gene(cobra_gene)[source]

Dissociates a cobra.Gene object with a cobra.Reaction.

Parameters:cobra_gene (cobra.core.Gene.Gene) –
knock_out()[source]

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="+")[source]

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 metabolites, 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__()[source]
_repr_html_()[source]
cobra.core.reaction.separate_forward_and_reverse_bounds(lower_bound, upper_bound)[source]

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, direction="both")[source]

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 corresponding flux bounds.
cobra.core.solution

Provide unified interfaces to optimization solutions.

Module Contents
class cobra.core.solution.Solution(objective_value, status, fluxes, reduced_costs=None, shadow_prices=None, **kwargs)[source]

A unified interface to a cobra.Model optimization solution.

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)[source]

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 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).
__repr__()[source]

String representation of the solution instance.

_repr_html_()[source]
__dir__()[source]

Hide deprecated attributes and methods from the public interface.

__getitem__(reaction_id)[source]

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()[source]

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)[source]

Legacy support for an interface to a cobra.Model optimization solution.

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

The LegacySolution class and its interface is deprecated.

__init__(f, x=None, x_dict=None, y=None, y_dict=None, solver=None, the_time=0, status="NA", **kwargs)[source]

Initialize a LegacySolution from an objective value.

Parameters:
  • f (float) – Objective value.
  • solver (str, optional) – A string indicating which solver package was used.
  • x (iterable, optional) – List or Array of the fluxes (primal values).
  • x_dict (dict, optional) – A dictionary of reaction IDs that maps to the respective primal values.
  • y (iterable, optional) – List or Array of the dual values.
  • y_dict (dict, optional) – A dictionary of reaction IDs that maps to the respective dual values.
  • the_time (int, optional) –
  • status (str, optional) –

:param .. warning :: deprecated:

__repr__()[source]

String representation of the solution instance.

__getitem__(reaction_id)[source]

Return the flux of a reaction.

Parameters:reaction_id (str) – A reaction ID.
dress_results(model)[source]

Method could be intended as a decorator.

Warning

deprecated

cobra.core.solution.get_solution(model, reactions=None, metabolites=None, raise_error=False)[source]

Generate a solution representation of the current solver state.

Parameters:
  • model (cobra.Model) – The model whose reactions to retrieve values for.
  • reactions (list, optional) – An iterable of cobra.Reaction objects. Uses model.reactions by default.
  • metabolites (list, optional) – An iterable of cobra.Metabolite objects. Uses model.metabolites by default.
  • raise_error (bool) – If true, raise an OptimizationError if solver status is not optimal.
Returns:

Return type:

cobra.Solution

Note

This is only intended for the optlang solver interfaces and not the legacy solvers.

cobra.core.species
Module Contents
class cobra.core.species.Species(id=None, name=None)[source]

Species is a class for holding information regarding a chemical Species

Parameters:
  • id (string) – An identifier for the chemical species
  • name (string) – A human readable name.
__init__(id=None, name=None)[source]
reactions()
__getstate__()[source]

Remove the references to container reactions when serializing to avoid problems associated with recursion.

copy()[source]

When copying a reaction, it is necessary to deepcopy the components so the list references aren’t carried over.

Additionally, a copy of a reaction is no longer in a cobra.Model.

This should be fixed with self.__deepcopy__ if possible

model()
cobra.flux_analysis
Submodules
cobra.flux_analysis.deletion
Module Contents
cobra.flux_analysis.deletion._reactions_knockouts_with_restore(model, reactions)[source]
cobra.flux_analysis.deletion._get_growth(model)[source]
cobra.flux_analysis.deletion._reaction_deletion(model, ids)[source]
cobra.flux_analysis.deletion._gene_deletion(model, ids)[source]
cobra.flux_analysis.deletion._reaction_deletion_worker(ids)[source]
cobra.flux_analysis.deletion._gene_deletion_worker(ids)[source]
cobra.flux_analysis.deletion._init_worker(model)[source]
cobra.flux_analysis.deletion._multi_deletion(model, entity, element_lists, method="fba", solution=None, processes=None, **kwargs)[source]

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 reference 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)[source]
cobra.flux_analysis.deletion._element_lists(entities, *ids)[source]
cobra.flux_analysis.deletion.single_reaction_deletion(model, reaction_list=None, method="fba", solution=None, processes=None, **kwargs)[source]

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 reference 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 – Keyword arguments are passed on to underlying simulation functions such as add_room.
Returns:

A representation of all single reaction deletions. The columns are ‘growth’ and ‘status’, where

index : frozenset([str])

The reaction identifier that was 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.single_gene_deletion(model, gene_list=None, method="fba", solution=None, processes=None, **kwargs)[source]

Knock out each gene from a given list.

Parameters:
  • model (cobra.Model) – The metabolic model to perform deletions in.
  • gene_list (iterable) – ``cobra.Gene``s to be deleted. If not passed, all the genes 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 reference 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 – Keyword arguments are passed on to underlying simulation functions such as add_room.
Returns:

A representation of all single gene deletions. The columns are ‘growth’ and ‘status’, where

index : frozenset([str])

The gene identifier that was 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.double_reaction_deletion(model, reaction_list1=None, reaction_list2=None, method="fba", solution=None, processes=None, **kwargs)[source]

Knock out each reaction pair from the combinations of two given lists.

We say ‘pair’ here but the order order does not matter.

Parameters:
  • model (cobra.Model) – The metabolic model to perform deletions in.
  • reaction_list1 (iterable, optional) – First iterable of ``cobra.Reaction``s to be deleted. If not passed, all the reactions from the model are used.
  • reaction_list2 (iterable, optional) – Second iterable of ``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 reference 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 – Keyword arguments are passed on to underlying simulation functions such as add_room.
Returns:

A representation of all combinations of reaction deletions. The columns are ‘growth’ and ‘status’, where

index : frozenset([str])

The 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.double_gene_deletion(model, gene_list1=None, gene_list2=None, method="fba", solution=None, processes=None, **kwargs)[source]

Knock out each gene pair from the combination of two given lists.

We say ‘pair’ here but the order order does not matter.

Parameters:
  • model (cobra.Model) – The metabolic model to perform deletions in.
  • gene_list1 (iterable, optional) – First iterable of ``cobra.Gene``s to be deleted. If not passed, all the genes from the model are used.
  • gene_list2 (iterable, optional) – Second iterable of ``cobra.Gene``s to be deleted. If not passed, all the genes 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 reference 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 – Keyword arguments are passed on to underlying simulation functions such as add_room.
Returns:

A representation of all combinations of gene deletions. The columns are ‘growth’ and ‘status’, where

index : frozenset([str])

The gene 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.gapfilling
Module Contents
class cobra.flux_analysis.gapfilling.GapFiller(model, universal=None, lower_bound=0.05, penalties=None, exchange_reactions=False, demand_reactions=True, integer_threshold=1e-06)[source]

Class for performing gap filling.

This class implements gap filling based on a mixed-integer approach, very similar to that described in [1] and the ‘no-growth but growth’ part of [2]_ but with minor adjustments. In short, we add indicator variables for using the reactions in the universal model, z_i and then solve problem

minimize sum_i c_i * z_i s.t. Sv = 0

v_o >= t lb_i <= v_i <= ub_i v_i = 0 if z_i = 0

where lb, ub are the upper, lower flux bounds for reaction i, c_i is a cost parameter and the objective v_o is greater than the lower bound t. The default costs are 1 for reactions from the universal model, 100 for exchange (uptake) reactions added and 1 for added demand reactions.

Note that this is a mixed-integer linear program and as such will expensive to solve for large models. Consider using alternatives [3]_ such as CORDA instead [4,5]_.

Parameters:
  • model (cobra.Model) – The model to perform gap filling on.
  • universal (cobra.Model) – A universal model with reactions that can be used to complete the model.
  • 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 exchange and demand reactions) for the three reaction types. Can also have reaction identifiers for reaction specific costs. Defaults are 1, 100 and 1 respectively.
  • integer_threshold (float) – The threshold at which a value is considered non-zero (aka integrality threshold). If gapfilled models fail to validate, you may want to lower this value.
  • exchange_reactions (bool) – Consider adding exchange (uptake) reactions for all metabolites in the model.
  • demand_reactions (bool) – Consider adding demand reactions for all metabolites.

References

[1]

Reed, Jennifer L., Trina R. Patel, Keri H. Chen, Andrew R. Joyce, Margaret K. Applebee, Christopher D. Herring, Olivia T. Bui, Eric 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

__init__(model, universal=None, lower_bound=0.05, penalties=None, exchange_reactions=False, demand_reactions=True, integer_threshold=1e-06)[source]
extend_model(exchange_reactions=False, demand_reactions=True)[source]

Extend gapfilling model.

Add reactions from universal model and optionally exchange and demand reactions for all metabolites in the model to perform gapfilling on.

Parameters:
  • exchange_reactions (bool) – Consider adding exchange (uptake) reactions for all metabolites in the model.
  • demand_reactions (bool) – Consider adding demand reactions for all metabolites.
update_costs()[source]

Update the coefficients for the indicator variables in the objective.

Done incrementally so that second time the function is called, active indicators in the current solutions gets higher cost than the unused indicators.

add_switches_and_objective()[source]

Update gapfilling model with switches and the indicator objective.

fill(iterations=1)[source]

Perform the gapfilling by iteratively solving the model, updating the costs and recording the used reactions.

Parameters:iterations (int) – The number of rounds of gapfilling to perform. For every iteration, 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.
Returns:A list of lists where each element is a list reactions that were used to gapfill the model.
Return type:iterable
Raises:RuntimeError – If the model fails to be validated (i.e. the original model with the proposed reactions added, still cannot get the required flux through the objective).
validate(reactions)[source]
cobra.flux_analysis.gapfilling.gapfill(model, universal=None, lower_bound=0.05, penalties=None, demand_reactions=True, exchange_reactions=False, iterations=1)[source]

Perform gapfilling on a model.

See documentation for the class GapFiller.

Parameters:
  • model (cobra.Model) – The model to perform gap filling on.
  • universal (cobra.Model, None) – A universal model with reactions that can be used to complete the model. Only gapfill considering demand and exchange reactions if left missing.
  • 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 exchange and demand reactions) for the three reaction types. Can also have reaction identifiers for reaction specific costs. Defaults are 1, 100 and 1 respectively.
  • iterations (int) – The number of rounds of gapfilling to perform. For every iteration, 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 metabolites.
Returns:

list of lists with on set of reactions that completes the model per requested iteration.

Return type:

iterable

Examples

>>> import cobra.test as ct
>>> from cobra import Model
>>> from cobra.flux_analysis import gapfill
>>> model = ct.create_test_model("salmonella")
>>> universal = Model('universal')
>>> universal.add_reactions(model.reactions.GF6PTA.copy())
>>> model.remove_reactions([model.reactions.GF6PTA])
>>> gapfill(model, universal)
cobra.flux_analysis.geometric

Provide an implementation of geometric FBA.

Module Contents
cobra.flux_analysis.geometric.geometric_fba(model, epsilon=1e-06, max_tries=200)[source]

Perform geometric FBA to obtain a unique, centered flux distribution.

Geometric FBA [1] formulates the problem as a polyhedron and then solves it by bounding the convex hull of the polyhedron. The bounding forms a box around the convex hull which reduces with every iteration and extracts a unique solution in this way.

Parameters:
  • model (cobra.Model) – The model to perform geometric FBA on.
  • epsilon (float, optional) – The convergence tolerance of the model (default 1E-06).
  • max_tries (int, optional) – Maximum number of iterations (default 200).
Returns:

The solution object containing all the constraints required for geometric FBA.

Return type:

cobra.Solution

References

[1]Smallbone, Kieran & Simeonidis, Vangelis. (2009). Flux balance analysis: A geometric perspective. Journal of theoretical biology.258. 311-5. 10.1016/j.jtbi.2009.01.027.
cobra.flux_analysis.loopless

Provides functions to remove thermodynamically infeasible loops.

Module Contents
cobra.flux_analysis.loopless.add_loopless(model, zero_cutoff=1e-12)[source]

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

[1]Elimination of thermodynamically infeasible loops in steady-state metabolic models. Schellenberger J, Lewis NE, Palsson BO. Biophys J. 2011 Feb 2;100(3):544-53. doi: 10.1016/j.bpj.2010.12.3707. Erratum in: Biophys J. 2011 Mar 2;100(5):1381.
cobra.flux_analysis.loopless._add_cycle_free(model, fluxes)[source]

Add constraints for CycleFreeFlux.

cobra.flux_analysis.loopless.loopless_solution(model, fluxes=None)[source]

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

The returned flux solution has the following properties:

  • it contains the minimal number of loops possible and no loops at all if all flux bounds include zero
  • it has an objective value close to the original one and the same objective value id the objective expression can not form a cycle (which is usually true since it consumes metabolites)
  • it has the same exact exchange fluxes as the previous solution
  • all fluxes have the same sign (flow in the same direction) as the previous solution

References

[1]CycleFreeFlux: efficient removal of thermodynamically infeasible loops from flux distributions. Desouki AA, Jarre F, Gelius-Dietrich G, Lercher MJ. Bioinformatics. 2015 Jul 1;31(13):2159-65. doi: 10.1093/bioinformatics/btv096.
cobra.flux_analysis.loopless.loopless_fva_iter(model, reaction, solution=False, zero_cutoff=1e-06)[source]

Plugin to get a loopless FVA solution from single FVA iteration.

Assumes the following about model and reaction: 1. the model objective is set to be reaction 2. the model has been optimized and contains the minimum/maximum flux for

reaction
  1. 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 (default). 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)[source]

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

Provide minimization of metabolic adjustment (MOMA).

Module Contents
cobra.flux_analysis.moma.moma(model, solution=None, linear=True)[source]

Compute a single solution based on (linear) MOMA.

Compute a new flux distribution that is at a minimal distance to a previous reference solution. Minimization of metabolic adjustment (MOMA) 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 MOMA-based solution for.
  • solution (cobra.Solution, optional) – A (wildtype) reference solution.
  • 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

add_moma()
add MOMA constraints and objective
cobra.flux_analysis.moma.add_moma(model, solution=None, linear=True)[source]

rAdd constraints and objective representing for MOMA.

This adds variables and constraints for the minimization of metabolic adjustment (MOMA) to the model.

Parameters:
  • model (cobra.Model) – The model to add MOMA constraints and objective to.
  • solution (cobra.Solution, optional) – A previous solution to use as a reference. If no solution is given, one will be computed using pFBA.
  • linear (bool, optional) – Whether to use the linear MOMA formulation or not (default True).

Notes

In the original MOMA [1] 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 MOMA [2], 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

pfba()
parsimonious FBA

References

[1]Segrè, Daniel, Dennis Vitkup, and George M. Church. “Analysis of Optimality in Natural and Perturbed Metabolic Networks.” Proceedings of the National Academy of Sciences 99, no. 23 (November 12, 2002): 15112. https://doi.org/10.1073/pnas.232349399.
[2]Becker, Scott A, Adam M Feist, Monica L Mo, Gregory Hannum, Bernhard Ø Palsson, and Markus J Herrgard. “Quantitative Prediction of Cellular Metabolism with Constraint-Based Models: The COBRA Toolbox.” Nature Protocols 2 (March 29, 2007): 727.
cobra.flux_analysis.parsimonious
Module Contents
cobra.flux_analysis.parsimonious.optimize_minimal_flux(*args, **kwargs)[source]
cobra.flux_analysis.parsimonious.pfba(model, fraction_of_optimum=1.0, objective=None, reactions=None)[source]

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 return_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

[1]Lewis, N. E., Hixson, K. K., Conrad, T. M., Lerman, J. A., Charusanti, P., Polpitiya, A. D., Palsson, B. O. (2010). Omic data from evolved E. coli are consistent with computed optimal growth from genome-scale models. Molecular Systems Biology, 6, 390. doi:10.1038/msb.2010.47
cobra.flux_analysis.parsimonious.add_pfba(model, objective=None, fraction_of_optimum=1.0)[source]

Add pFBA objective

Add objective to minimize the summed flux of all reactions to the current objective.

See also

pfba()

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 maintained. The original objective reaction is constrained to be greater than maximal_value * fraction_of_optimum.
cobra.flux_analysis.phenotype_phase_plane
Module Contents
cobra.flux_analysis.phenotype_phase_plane.production_envelope(model, reactions, objective=None, carbon_sources=None, points=20, threshold=1e-07)[source]

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 considered 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

>>> import cobra.test
>>> from cobra.flux_analysis import production_envelope
>>> model = cobra.test.create_test_model("textbook")
>>> production_envelope(model, ["EX_glc__D_e", "EX_o2_e"])
cobra.flux_analysis.phenotype_phase_plane.add_envelope(model, reactions, grid, c_input, c_output, threshold)[source]
cobra.flux_analysis.phenotype_phase_plane.total_yield(input_fluxes, input_elements, output_flux, output_elements)[source]

Compute total output per input unit.

Units are typically mol carbon atoms or gram of source and product.

Parameters:
  • input_fluxes (list) – A list of input reaction fluxes in the same order as the input_components.
  • input_elements (list) – A list of reaction components which are in turn list of numbers.
  • output_flux (float) – The output flux value.
  • output_elements (list) – A list of stoichiometrically weighted output reaction components.
Returns:

The ratio between output (mol carbon atoms or grams of product) and input (mol carbon atoms or grams of source compounds).

Return type:

float

cobra.flux_analysis.phenotype_phase_plane.reaction_elements(reaction)[source]

Split metabolites into the atoms times their stoichiometric coefficients.

Parameters:reaction (Reaction) – The metabolic reaction whose components are desired.
Returns:Each of the reaction’s metabolites’ desired carbon elements (if any) times that metabolite’s stoichiometric coefficient.
Return type:list
cobra.flux_analysis.phenotype_phase_plane.reaction_weight(reaction)[source]

Return the metabolite weight times its stoichiometric coefficient.

cobra.flux_analysis.phenotype_phase_plane.total_components_flux(flux, components, consumption=True)[source]

Compute the total components consumption or production flux.

Parameters:
  • flux (float) – The reaction flux for the components.
  • components (list) – List of stoichiometrically weighted components.
  • consumption (bool, optional) – Whether to sum up consumption or production fluxes.
cobra.flux_analysis.phenotype_phase_plane.find_carbon_sources(model)[source]

Find all active carbon source reactions.

Parameters:model (Model) – A genome-scale metabolic model.
Returns:The medium reactions with carbon input flux.
Return type:list
cobra.flux_analysis.reaction

functions for analyzing / creating objective functions

Module Contents
cobra.flux_analysis.reaction.assess(model, reaction, flux_coefficient_cutoff=0.001, solver=None)[source]

Assesses production capacity.

Assesses the capacity of the model to produce the precursors for the reaction and absorb the production of the reaction while the reaction is operating at, or above, the specified cutoff.

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 can produce the precursors and absorb the products for the reaction operating at, or above, flux_coefficient_cutoff. Otherwise, a dictionary of {‘precursor’: Status, ‘product’: Status}. Where Status is the results from assess_precursors and assess_products, respectively.

Return type:

bool or dict

cobra.flux_analysis.reaction.assess_component(model, reaction, side, flux_coefficient_cutoff=0.001, solver=None)[source]

Assesses the ability of the model to provide sufficient precursors, or absorb products, for a reaction operating at, or beyond, the specified cutoff.

Parameters:
  • model (cobra.Model) – The cobra model to assess production capacity for
  • reaction (reaction identifier or cobra.Reaction) – The reaction to assess
  • side (basestring) – Side of the reaction, ‘products’ or ‘reactants’
  • 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._optimize_or_value(model, value=0.0, solver=None)[source]
cobra.flux_analysis.reaction.assess_precursors(model, reaction, flux_coefficient_cutoff=0.001, solver=None)[source]

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)[source]

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 simultaneously 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

Provide regulatory on/off minimization (ROOM).

Module Contents
cobra.flux_analysis.room.room(model, solution=None, linear=False, delta=0.03, epsilon=0.001)[source]

Compute a single solution based on regulatory on/off minimization (ROOM).

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) (default 0.001).
Returns:

A flux distribution with minimal active reaction changes compared to the reference.

Return type:

cobra.Solution

See also

add_room()
add ROOM constraints and objective
cobra.flux_analysis.room.add_room(model, solution=None, linear=False, delta=0.03, epsilon=0.001)[source]

r Add constraints and objective for ROOM.

This function adds variables and constraints for applying regulatory on/off minimization (ROOM) to the model.

Parameters:
  • model (cobra.Model) – The model to add ROOM constraints and objective to.
  • solution (cobra.Solution, optional) – A previous solution to use as a reference. If no solution is given, one will be computed using pFBA.
  • linear (bool, optional) – Whether to use the linear ROOM formulation or not (default False).
  • delta (float, optional) – The relative tolerance range which is additive in nature (default 0.03).
  • epsilon (float, optional) – The absolute range of tolerance which is multiplicative (default 0.001).

Notes

The formulation used here is the same as stated in the original paper [1]. 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

pfba()
parsimonious FBA

References

[1]Tomer Shlomi, Omer Berkman and Eytan Ruppin, “Regulatory on/off minimization of metabolic flux changes after genetic perturbations”, PNAS 2005 102 (21) 7695-7700; doi:10.1073/pnas.0406346102
cobra.flux_analysis.sampling

Module implementing flux sampling for cobra models.

New samplers should derive from the abstract HRSampler class where possible to provide a uniform interface.

Module Contents
cobra.flux_analysis.sampling.mp_init(obj)[source]

Initialize the multiprocessing pool.

cobra.flux_analysis.sampling.shared_np_array(shape, data=None, integer=False)[source]

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)[source]

Sample a new feasible point from the point x in direction delta.

class cobra.flux_analysis.sampling.HRSampler(model, thinning, nproj=None, seed=None)[source]

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 matrix 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.

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)[source]

Initialize a new sampler object.

__build_problem()

Build the matrix representation of the sampling problem.

generate_fva_warmup()[source]

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)[source]

Reproject a point into the feasibility region.

This function is guaranteed to return a new feasible point. However, no guarantees in terms of proximity 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()[source]

Find an approximately random point in the flux cone.

_is_redundant(matrix, cutoff=None)[source]

Identify rdeundant rows in a matrix that can be removed.

_bounds_dist(p)[source]

Get the lower and upper bound distances. Negative is bad.

sample(n, fluxes=True)[source]

Abstract sampling function.

Should be overwritten by child classes.

batch(batch_size, batch_num, fluxes=True)[source]

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).
  • fluxes (boolean) – Whether to return fluxes or the internal solver variables. If set to False will return a variable for each forward and backward flux as well as all additional variables you might have defined in the model.
Yields:

pandas.DataFrame – A DataFrame with dimensions (batch_size x n_r) containing a valid flux sample for a total of n_r reactions (or variables if fluxes=False) in each row.

validate(samples)[source]

Validate a set of samples for equality and inequality feasibility.

Can be used to check whether the generated samples and warmup points are feasible.

Parameters:samples (numpy.matrix) – Must be of dimension (n_samples x n_reactions). Contains the samples to be validated. Samples must be from fluxes.
Returns:A one-dimensional numpy array of length containing a code of 1 to 3 letters denoting the validation result:
  • ’v’ means feasible in bounds and equality constraints
  • ’l’ means a lower bound violation
  • ’u’ means a lower bound validation
  • ’e’ means and equality constraint violation
Return type:numpy.array
class cobra.flux_analysis.sampling.ACHRSampler(model, thinning=100, nproj=None, seed=None)[source]

Artificial Centering Hit-and-Run sampler.

A sampler with low memory footprint and good convergence.

Parameters:
  • model (a cobra model) – The cobra model from which to generate samples.
  • 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 matrix 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.

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

[1]Direction Choice for Accelerated Convergence in Hit-and-Run Sampling David E. Kaufman Robert L. Smith Operations Research 199846:1 , 84-95 https://doi.org/10.1287/opre.46.1.84
[2]https://github.com/opencobra/cobratoolbox
__init__(model, thinning=100, nproj=None, seed=None)[source]

Initialize a new ACHRSampler.

__single_iteration()
sample(n, fluxes=True)[source]

Generate a set of samples.

This is the basic sampling function for all hit-and-run samplers.

Parameters:
  • n (int) – The number of samples that are generated at once.
  • fluxes (boolean) – Whether to return fluxes or the internal solver variables. If set to False will return a variable for each forward and backward flux as well as all additional variables you might have defined in the model.
Returns:

Returns a matrix with n rows, each containing a flux sample.

Return type:

numpy.matrix

Notes

Performance of this function linearly depends on the number of reactions in your model and the thinning factor.

cobra.flux_analysis.sampling._sample_chain(args)[source]

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)[source]

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 matrix 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.

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

[1]Megchelenbrink W, Huynen M, Marchiori E (2014) optGpSampler: An Improved Tool for Uniformly Sampling the Solution-Space of Genome-Scale Metabolic Networks. PLoS ONE 9(2): e86587. https://doi.org/10.1371/journal.pone.0086587
__init__(model, processes, thinning=100, nproj=None, seed=None)[source]

Initialize a new OptGPSampler.

sample(n, fluxes=True)[source]

Generate a set of samples.

This is the basic sampling function for all hit-and-run samplers.

n : int
The minimum number of samples that are generated at once (see Notes).
fluxes : boolean
Whether to return fluxes or the internal solver variables. If set to False will return a variable for each forward and backward flux as well as all additional variables you might have defined in the model.
Returns:Returns a matrix with n rows, each containing a flux sample.
Return type:numpy.matrix

Notes

Performance of this function linearly depends on the number of reactions in your model and the thinning 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__()[source]

Return the object for serialization.

cobra.flux_analysis.sampling.sample(model, n, method="optgp", thinning=100, processes=1, seed=None)[source]

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

  1. ‘achr’ which uses artificial centering hit-and-run. This is a single process method with good convergence [2]_.
Parameters:
  • model (cobra.Model) – The model from which to sample flux distributions.
  • n (int) – The number of samples to obtain. When using ‘optgp’ this must be a multiple of processes, otherwise a larger number of samples will be returned.
  • method (str, optional) – The sampling algorithm to use.
  • thinning (int, optional) – The thinning factor of the generated sampling chain. A thinning of 10 means samples are returned every 10 steps. Defaults to 100 which in benchmarks gives approximately uncorrelated samples. If set to one will return all iterates.
  • processes (int, optional) – Only used for ‘optgp’. The number of processes used to generate samples.
  • seed (positive integer, optional) – The random number seed to be used. Initialized to current time stamp if None.
Returns:

The generated flux samples. Each row corresponds to a sample of the fluxes and the columns are the reactions.

Return type:

pandas.DataFrame

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

[1]Megchelenbrink W, Huynen M, Marchiori E (2014) optGpSampler: An Improved Tool for Uniformly Sampling the Solution-Space of Genome-Scale Metabolic Networks. PLoS ONE 9(2): e86587.
[2]Direction Choice for Accelerated Convergence in Hit-and-Run Sampling David E. Kaufman Robert L. Smith Operations Research 199846:1 , 84-95
cobra.flux_analysis.summary
Module Contents
cobra.flux_analysis.summary.metabolite_summary(met, solution=None, threshold=0.01, fva=False, names=False, floatfmt=".3g")[source]

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 solution 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 reported.
  • 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 previous 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 identifiers (default False).
  • floatfmt (string, optional) – Format string for floats (default ‘.3g’).
cobra.flux_analysis.summary.model_summary(model, solution=None, threshold=0.01, fva=None, names=False, floatfmt=".3g")[source]

Print a summary of the input and output fluxes of the model.

Parameters:
  • solution (cobra.Solution, optional) – A previously solved model solution 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 reported.
  • 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 previous 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 identifiers (default False).
  • floatfmt (string, optional) – Format string for floats (default ‘.3g’).
cobra.flux_analysis.summary._process_flux_dataframe(flux_dataframe, fva, threshold, floatfmt)[source]

Some common methods for processing a database of flux information into print-ready formats. Used in both model_summary and metabolite_summary.

cobra.flux_analysis.variability
Module Contents
cobra.flux_analysis.variability.flux_variability_analysis(model, reaction_list=None, loopless=False, fraction_of_optimum=1.0, pfba_factor=None)[source]

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 total 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 in [1]. Please note that the flux distribution containing all minimal/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 (see [2]) 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

[1]Computationally efficient flux variability analysis. Gudmundsson S, Thiele I. BMC Bioinformatics. 2010 Sep 29;11:489. doi: 10.1186/1471-2105-11-489, PMID: 20920235
[2]CycleFreeFlux: efficient removal of thermodynamically infeasible loops from flux distributions. Desouki AA, Jarre F, Gelius-Dietrich G, Lercher MJ. Bioinformatics. 2015 Jul 1;31(13):2159-65. doi: 10.1093/bioinformatics/btv096.
cobra.flux_analysis.variability.find_blocked_reactions(model, reaction_list=None, zero_cutoff=1e-09, open_exchanges=False)[source]

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)[source]

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.
Returns:

Set of essential genes

Return type:

set

cobra.flux_analysis.variability.find_essential_reactions(model, threshold=None, processes=None)[source]

Return a set of essential reactions.

A reaction is considered essential if restricting its flux 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 reactions 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.
Returns:

Set of essential reactions

Return type:

set

cobra.io
Submodules
cobra.io.dict
Module Contents
cobra.io.dict._fix_type(value)[source]

convert possible types to str, float, and bool

cobra.io.dict._update_optional(cobra_object, new_dict, optional_attribute_dict, ordered_keys)[source]

update new_dict with optional attributes from cobra_object

cobra.io.dict.metabolite_to_dict(metabolite)[source]
cobra.io.dict.metabolite_from_dict(metabolite)[source]
cobra.io.dict.gene_to_dict(gene)[source]
cobra.io.dict.gene_from_dict(gene)[source]
cobra.io.dict.reaction_to_dict(reaction)[source]
cobra.io.dict.reaction_from_dict(reaction, model)[source]
cobra.io.dict.model_to_dict(model, sort=False)[source]

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.

Return type:

OrderedDict

See also

cobra.io.model_from_dict()

cobra.io.dict.model_from_dict(obj)[source]

Build a model from a dict.

Models stored in json are first formulated as a dict that can be read to cobra model using this function.

Parameters:obj (dict) – 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.
Returns:The generated model.
Return type:cora.core.Model

See also

cobra.io.model_to_dict()

cobra.io.json
Module Contents
cobra.io.json.to_json(model, sort=False, **kwargs)[source]

Return the model as a JSON document.

kwargs are passed on to json.dumps.

Parameters:
  • model (cobra.Model) – The cobra model to represent.
  • sort (bool, optional) – Whether to sort the metabolites, reactions, and genes or maintain the order defined in the model.
Returns:

String representation of the cobra model as a JSON document.

Return type:

str

See also

save_json_model()
Write directly to a file.
json.dumps()
Base function.
cobra.io.json.from_json(document)[source]

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

load_json_model()
Load directly from a file.
cobra.io.json.save_json_model(model, filename, sort=False, pretty=False, **kwargs)[source]

Write the cobra model to a file in JSON format.

kwargs are passed on to json.dump.

Parameters:
  • model (cobra.Model) – The cobra model to represent.
  • filename (str or file-like) – File path or descriptor that the JSON representation should be written to.
  • sort (bool, optional) – Whether to sort the metabolites, reactions, and genes or maintain the order defined in the model.
  • pretty (bool, optional) – Whether to format the JSON more compactly (default) or in a more verbose but easier to read fashion. Can be partially overwritten by the kwargs.

See also

to_json()
Return a string representation.
json.dump()
Base function.
cobra.io.json.load_json_model(filename)[source]

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

from_json()
Load from a string.
cobra.io.mat
Module Contents
cobra.io.mat._get_id_compartment(id)[source]

extract the compartment from the id string

cobra.io.mat._cell(x)[source]

translate an array x into a MATLAB cell array

cobra.io.mat.load_matlab_model(infile_path, variable_name=None, inf=inf)[source]

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)[source]

Save the cobra model as a .mat file.

This .mat file can be used directly in the MATLAB version of COBRA.

Parameters:
  • model (cobra.core.Model.Model object) – The model to save
  • file_name (str or file-like object) – The file to save to
  • varname (string) – The name of the variable within the workspace
cobra.io.mat.create_mat_metabolite_id(model)[source]
cobra.io.mat.create_mat_dict(model)[source]

create a dict mapping model attributes to arrays

cobra.io.mat.from_mat_struct(mat_struct, model_id=None, inf=inf)[source]

create a model from the COBRA toolbox struct

The struct will be a dict read in by scipy.io.loadmat

cobra.io.mat._check(result)[source]

ensure success of a pymatbridge operation

cobra.io.mat.model_to_pymatbridge(model, variable_name="model", matlab=None)[source]

send the model to a MATLAB workspace through pymatbridge

This model can then be manipulated through the COBRA toolbox

Parameters:
  • variable_name (str) – The variable name to which the model will be assigned in the MATLAB workspace
  • matlab (None or pymatbridge.Matlab instance) – The MATLAB workspace to which the variable will be sent. If this is None, then this will be sent to the same environment used in IPython magics.
cobra.io.sbml
Module Contents
cobra.io.sbml.parse_legacy_id(the_id, the_compartment=None, the_type="metabolite", use_hyphens=False)[source]

Deals with a bunch of problems due to bigg.ucsd.edu not following SBML standards

Parameters:
  • the_id (String.) –
  • the_compartment (String) –
  • the_type (String) – Currently only ‘metabolite’ is supported
  • use_hyphens (Boolean) – If True, double underscores (__) in an SBML ID will be converted to hyphens
Returns:

string

Return type:

the identifier

cobra.io.sbml.create_cobra_model_from_sbml_file(sbml_filename, old_sbml=False, legacy_metabolite=False, print_time=False, use_hyphens=False)[source]

convert an SBML XML file into a cobra.Model object.

Supports SBML Level 2 Versions 1 and 4. The function will detect if the SBML fbc package is used in the file and run the converter if the fbc package is used.

Parameters:
  • sbml_filename (string) –
  • 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=":")[source]

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)[source]

Write a cobra.Model object to an SBML XML file.

Parameters:

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)[source]

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)[source]

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 eventually 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)[source]
cobra.io.sbml.read_legacy_sbml(filename, use_hyphens=False)[source]

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)[source]

replace prefixes with namespace

cobra.io.sbml3.extract_rdf_annotation(sbml_element, metaid)
class cobra.io.sbml3.CobraSBMLError[source]
cobra.io.sbml3.get_attrib(tag, attribute, type=None, require=False)[source]
cobra.io.sbml3.set_attrib(xml, attribute_name, value)[source]
cobra.io.sbml3.parse_stream(filename)[source]

parses filename or compressed stream to xml

cobra.io.sbml3.clip(string, prefix)[source]

clips a prefix from the beginning of a string if it exists

>>> clip("R_pgi", "R_")
"pgi"
cobra.io.sbml3.strnum(number)[source]

Utility function to convert a number to a string

cobra.io.sbml3.construct_gpr_xml(parent, expression)[source]

create gpr xml under parent node

cobra.io.sbml3.annotate_cobra_from_sbml(cobra_element, sbml_element)[source]
cobra.io.sbml3.annotate_sbml_from_cobra(sbml_element, cobra_element)[source]
cobra.io.sbml3.parse_xml_into_model(xml, number=float)[source]
cobra.io.sbml3.model_to_xml(cobra_model, units=True)[source]
cobra.io.sbml3.read_sbml_model(filename, number=float, **kwargs)[source]
cobra.io.sbml3.validate_sbml_model(filename, check_model=True)[source]

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.sbml3.write_sbml_model(cobra_model, filename, use_fbc_package=True, **kwargs)[source]
cobra.io.sbml3.indent_xml(elem, level=0)[source]

indent xml for pretty printing

cobra.io.yaml
Module Contents
cobra.io.yaml.to_yaml(model, sort=False, **kwargs)[source]

Return the model as a YAML document.

kwargs are passed on to yaml.dump.

Parameters:
  • model (cobra.Model) – The cobra model to represent.
  • sort (bool, optional) – Whether to sort the metabolites, reactions, and genes or maintain the order defined in the model.
Returns:

String representation of the cobra model as a YAML document.

Return type:

str

See also

save_yaml_model()
Write directly to a file.
ruamel.yaml.dump()
Base function.
cobra.io.yaml.from_yaml(document)[source]

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

load_yaml_model()
Load directly from a file.
cobra.io.yaml.save_yaml_model(model, filename, sort=False, **kwargs)[source]

Write the cobra model to a file in YAML format.

kwargs are passed on to yaml.dump.

Parameters:
  • model (cobra.Model) – The cobra model to represent.
  • filename (str or file-like) – File path or descriptor that the YAML representation should be written to.
  • sort (bool, optional) – Whether to sort the metabolites, reactions, and genes or maintain the order defined in the model.

See also

to_yaml()
Return a string representation.
ruamel.yaml.dump()
Base function.
cobra.io.yaml.load_yaml_model(filename)[source]

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

from_yaml()
Load from a string.
cobra.manipulation
Submodules
cobra.manipulation.annotate
Module Contents
cobra.manipulation.annotate.add_SBO(model)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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, compiled_gene_reaction_rules=None)[source]

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, cumulative_deletions=True, disable_orphans=False)[source]

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 cobra_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)[source]
__init__(target_genes)[source]
visit_Name(node)[source]
visit_BoolOp(node)[source]
cobra.manipulation.delete.remove_genes(cobra_model, gene_list, remove_reactions=True)[source]

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)[source]

make a single string id SBML compliant

class cobra.manipulation.modify._GeneEscaper[source]
visit_Name(node)[source]
cobra.manipulation.modify.escape_ID(cobra_model)[source]

makes all ids SBML compliant

cobra.manipulation.modify.rename_genes(cobra_model, rename_dict)[source]

renames genes in a model from the rename_dict

cobra.manipulation.modify.convert_to_irreversible(cobra_model)[source]

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, update_solution=True)[source]

This function will convert an irreversible model made by convert_to_irreversible into a reversible model.

cobra_model : cobra.Model
A model which will be modified in place.
update_solution: bool
This option is ignored since model.solution was removed.
cobra.manipulation.validate
Module Contents
cobra.manipulation.validate.check_mass_balance(model)[source]
cobra.manipulation.validate.check_reaction_bounds(model)[source]
cobra.manipulation.validate.check_metabolite_compartment_formula(model)[source]
cobra.medium

Imports for the media module.

Submodules
cobra.medium.boundary_types

Contains function to identify the type of boundary reactions.

This module uses various heuristics to decide whether a boundary reaction is an exchange, demand or sink reaction. It mostly orientates on the following paper:

Thiele, I., & Palsson, B. Ø. (2010, January). A protocol for generating a high-quality genome-scale metabolic reconstruction. Nature protocols. Nature Publishing Group. http://doi.org/10.1038/nprot.2009.203

Module Contents
cobra.medium.boundary_types.find_external_compartment(model)[source]

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, external_compartment)[source]

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 “exchange”, “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, external_compartment=None)[source]

Find specific boundary reactions.

Parameters:
  • model (cobra.Model) – A cobra model.
  • boundary_type (str) – What boundary type to check for. Must be one of “exchange”, “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

Contains functions and helpers to obtain minimal growth media.

Module Contents
cobra.medium.minimal_medium.add_linear_obj(model)[source]

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
Parameters:model (cobra.Model) – The model to modify.
cobra.medium.minimal_medium.add_mip_obj(model)[source]

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.
cobra.medium.minimal_medium._as_medium(exchanges, tolerance=1e-06, exports=False)[source]

Convert a solution to medium.

Parameters:
  • exchanges (list of cobra.reaction) – The exchange reactions to consider.
  • tolerance (positive double) – The absolute tolerance for fluxes. Fluxes with an absolute value smaller than this number will be ignored.
  • exports (bool) – Whether to return export fluxes as well.
Returns:

The “medium”, meaning all active import fluxes in the solution.

Return type:

pandas.Series

cobra.medium.minimal_medium.minimal_medium(model, min_objective_value=0.1, exports=False, minimize_components=False, open_exchanges=False)[source]

Find the minimal growth medium for the model.

Finds the minimal growth medium for the model which allows for model as well as individual growth. Here, a minimal medium can either be the medium requiring the smallest total import flux or the medium requiring the least components (ergo ingredients), which will be much slower due to being a mixed integer problem (MIP).

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 minimize 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. positive 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 interested 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 homogeneous 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
compare_models(name, model1, model2)
extra_comparisons(name, model1, model2)
test_read_1(io_trial)
test_read_2(io_trial)
test_write_1(io_trial)
test_write_2(io_trial)
cobra.test.test_io.test_benchmark_read(data_directory, benchmark)
cobra.test.test_io.test_benchmark_write(model, benchmark)
cobra.test.test_io.test_validate(trial, data_directory)
cobra.test.test_io.test_read_nonexistent(trial)
cobra.test.test_io.test_sbml_error(data_directory)
cobra.test.test_io.test_bad_validation(data_directory)
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, minimized_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)
test_copy_benchmark_large_model(large_model, solver, benchmark)
test_copy(model)

modifying copy should not modify the original

test_deepcopy_benchmark(model, benchmark)
test_deepcopy(model)

Reference structures are maintained when deepcopying

test_add_reaction_orphans(model)

test reaction addition

Need to verify that no orphan genes or metabolites are contained in reactions after adding them to the model.

test_merge_models(model, tiny_toy_model)
test_change_objective_benchmark(model, benchmark, solver)
test_get_objective_direction(model)
test_set_objective_direction(model)
test_slim_optimize(model)
test_optimize(model, solver)
test_change_objective(model)
test_problem_properties(model)
test_solution_data_frame(model)
test_context_manager(model)
test_repr_html_(model)
class cobra.test.test_model.TestStoichiometricMatrix

Test the simple replacement for ArrayBasedModel

test_dense_matrix(model)
test_sparse_matrix(model)
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")[source]

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)[source]

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)[source]

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.
  • 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 corresponding type and ‘DataFrame’ will give a pandas DataFrame with metabolite indices and reaction columns
  • dtype (data-type) – The desired data-type for the array. If not given, defaults to float.
Returns:

The stoichiometric matrix for the given model.

Return type:

matrix of class dtype

cobra.util.array.nullspace(A, atol=1e-13, rtol=0)[source]

Compute an approximate basis for the nullspace of A. The algorithm used by this function is based on the singular value decomposition of A.

Parameters:
  • A (numpy.ndarray) – A should be at most 2-D. A 1-D array with length k will be treated as a 2-D with shape (1, k)
  • atol (float) – The absolute tolerance for a zero singular value. Singular values smaller than atol are considered to be zero.
  • rtol (float) – The relative tolerance. Singular values less than rtol*smax are considered to be zero, where smax is the largest singular value.
  • both atol and rtol are positive, the combined tolerance is the (If) –

:param maximum of the two; that is::: :param tol = max(atol, rtol * smax): :param Singular values smaller than tol are considered to be zero.:

Returns:If A is an array with shape (m, k), then ns will be an array with shape (k, n), where n is the estimated dimension of the nullspace of A. The columns of ns are a basis for the nullspace; each element in numpy.dot(A, ns) will be approximately zero.
Return type:numpy.ndarray

Notes

Taken from the numpy cookbook.

cobra.util.array.constraint_matrices(model, array_type="dense", include_vars=False, zero_tol=1e-06)[source]

Create a matrix representation of the problem.

This is used for alternative solution approaches that do not use optlang. The function will construct the equality matrix, inequality matrix and bounds for the complete problem.

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 corresponding 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.

Return type:

collections.namedtuple

cobra.util.context
Module Contents
class cobra.util.context.HistoryManager[source]

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__()[source]
__call__(operation)[source]

Add the corresponding method to the history stack.

Parameters:operation (function) – A function to be called at a later time
reset()[source]

Trigger executions for all items in the stack in reverse order

cobra.util.context.get_context(obj)[source]

Search for a context manager

cobra.util.context.resettable(f)[source]

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

Additional helper functions for the optlang solvers.

All functions integrate well with the context manager, meaning that all operations defined here are automatically reverted when used in a with model: block.

The functions defined here together with the existing model functions should allow you to implement custom flux analysis methods with ease.

Module Contents
cobra.util.solver.linear_reaction_coefficients(model, reactions=None)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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

Raises:

SolverNotFound – If no suitable solver could be found.

cobra.util.solver.choose_solver(model, solver=None, qp=False)[source]

Choose a solver given a solver name and model.

This will choose a solver compatible with the model and required capabilities. Also respects model.solver where it can.

Parameters:
  • model (a cobra model) – The model for which to choose the solver.
  • solver (str, optional) – The name of the solver to be used.
  • qp (boolean, optional) – Whether the solver needs Quadratic Programming capabilities.
Returns:

solver – Returns a valid solver for the problem.

Return type:

an optlang solver interface

Raises:

SolverNotFound – If no suitable solver could be found.

cobra.util.solver.add_cons_vars_to_problem(model, what, **kwargs)[source]

Add variables and constraints to a Model’s solver object.

Useful for variables and constraints that can not be expressed with reactions and lower/upper bounds. Will integrate with the Model’s context manager in order to revert changes upon leaving the context.

Parameters:
  • model (a cobra model) – The model to which to add the variables and constraints.
  • what (list or tuple of optlang variables or constraints.) – The variables or constraints to add to the model. Must be of class model.problem.Variable or model.problem.Constraint.
  • **kwargs (keyword arguments) – passed to solver.add()
cobra.util.solver.remove_cons_vars_from_problem(model, what)[source]

Remove variables and constraints from a Model’s solver object.

Useful to temporarily remove variables and constraints from a Models’s solver object.

Parameters:
  • model (a cobra model) – The model from which to remove the variables and constraints.
  • what (list or tuple of optlang variables or constraints.) – The variables or constraints to remove from the model. Must be of class model.problem.Variable or model.problem.Constraint.
cobra.util.solver.add_absolute_expression(model, expression, name="abs_var", ub=None, difference=0, add=True)[source]

Add the absolute value of an expression to the model.

Also defines a variable for the absolute value that can be used in other objectives or constraints.

Parameters:
  • model (a cobra model) – The model to which to add the absolute expression.
  • expression (A sympy expression) – Must be a valid expression within the Model’s solver object. The absolute value is applied automatically on the expression.
  • name (string) – The name of the newly created variable.
  • ub (positive float) – The upper bound for the variable.
  • difference (positive float) – The difference between the expression and the variable.
  • add (bool) – Whether to add the variable to the model at once.
Returns:

A named tuple with variable and two constraints (upper_constraint, lower_constraint) describing the new variable and the constraints that assign the absolute value of the expression to it.

Return type:

namedtuple

cobra.util.solver.fix_objective_as_constraint(model, fraction=1, bound=None, name="fixed_objective_{}")[source]

Fix current objective as an additional constraint.

When adding constraints to a model, such as done in pFBA which minimizes total flux, these constraints can become too powerful, resulting in solutions that satisfy optimality but sacrifices too much for the original objective function. To avoid that, we can fix the current objective value as a constraint to ignore solutions that give a lower (or higher depending on the optimization direction) objective value than the original model.

When done with the model as a context, the modification to the objective will be reverted when exiting that context.

Parameters:
  • model (cobra.Model) – The model to operate on
  • fraction (float) – The fraction of the optimum the objective is allowed to reach.
  • bound (float, None) – The bound to use instead of fraction of maximum optimal value. If not None, fraction is ignored.
  • name (str) – Name of the objective. May contain one {} placeholder which is filled with the name of the old objective.
cobra.util.solver.check_solver_status(status, raise_error=False)[source]

Perform standard checks on a solver’s status.

cobra.util.solver.assert_optimal(model, message="optimization failed")[source]

Assert model solver status is optimal.

Do nothing if model solver status is optimal, otherwise throw appropriate exception depending on the status.

Parameters:
  • model (cobra.Model) – The model to check the solver status for.
  • message (str (optional)) – Message to for the exception if solver status was not optimal.
cobra.util.util
Module Contents
cobra.util.util.format_long_string(string, max_length=50)[source]
class cobra.util.util.AutoVivification[source]

Implementation of perl’s autovivification feature. Checkout http://stackoverflow.com/a/652284/280182

__getitem__(item)[source]
cobra.util.util.show_versions()[source]

Print dependency information.

Submodules

cobra.exceptions
Module Contents
class cobra.exceptions.OptimizationError(message)[source]
__init__(message)[source]
class cobra.exceptions.Infeasible[source]
class cobra.exceptions.Unbounded[source]
class cobra.exceptions.FeasibleButNotOptimal[source]
class cobra.exceptions.UndefinedSolution[source]
class cobra.exceptions.SolverNotFound[source]

A simple Exception when a solver can not be found.

Package Contents

cobra._warn_format(message, category, filename, lineno, file=None, line=None)[source]

Indices and tables