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")
In [2]:
solution = model.optimize()
print(solution)
The Model.optimize() function will return a Solution object. A solution object has several attributes:
objective_value
: the objective valuestatus
: the status from the linear programming solverfluxes
: 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]:
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
Out[4]:
In [5]:
%%time
model.slim_optimize()
Out[5]:
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 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()
Or to get a sense of the main energy production and consumption reactions
In [8]:
model.metabolites.atp_c.summary()
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]:
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]:
In [12]:
model.optimize().objective_value
Out[12]:
We can also have more complicated objectives including quadratic terms.
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]:
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]:
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]:
In [17]:
flux_variability_analysis(model, reaction_list=loop_reactions, loopless=True)
Out[17]:
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)
Similarly, variability in metabolite mass balances can also be checked with flux variability analysis.
In [19]:
model.metabolites.pyr_c.summary(fva=0.95)
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.
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]: