In [1]:
import pandas
pandas.options.display.max_rows = 100
import cobra.test
model = cobra.test.create_test_model("textbook")
In [2]:
model.optimize()
Out[2]:
The Model.optimize() function will return a Solution object, which will also be stored at model.solution. A solution object has several attributes:
For example, after the last call to model.optimize(), the status should be 'optimal' if the solver returned no errors, and f should be the objective value
In [3]:
model.solution.status
Out[3]:
In [4]:
model.solution.f
Out[4]:
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 [5]:
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 [6]:
model.metabolites.nadh_c.summary()
Or to get a sense of the main energy production and consumption reactions
In [7]:
model.metabolites.atp_c.summary()
In [8]:
biomass_rxn = model.reactions.get_by_id("Biomass_Ecoli_core")
Currently in the model, there is only one objective reaction (the biomass reaction), with an objective coefficient of 1.
In [9]:
model.objective
Out[9]:
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 [10]:
# 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.
model.objective
Out[10]:
In [11]:
model.optimize().f
Out[11]:
The objective function can also be changed by setting Reaction.objective_coefficient directly.
In [12]:
model.reactions.get_by_id("ATPM").objective_coefficient = 0.
biomass_rxn.objective_coefficient = 1.
model.objective
Out[12]:
In [13]:
fva_result = cobra.flux_analysis.flux_variability_analysis(
model, model.reactions[:20])
pandas.DataFrame.from_dict(fva_result).T
Out[13]:
Setting parameter fraction_of_optimium=0.90 would give the flux ranges for reactions at 90% optimality.
In [14]:
fva_result = cobra.flux_analysis.flux_variability_analysis(
model, model.reactions[:20], fraction_of_optimum=0.9)
pandas.DataFrame.from_dict(fva_result).T
Out[14]:
In [15]:
model.summary(fva=0.95)
Similarly, variability in metabolite mass balances can also be checked with flux variability analysis
In [16]:
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 [17]:
FBA_sol = model.optimize()
pFBA_sol = cobra.flux_analysis.optimize_minimal_flux(model)
These functions should give approximately the same objective value
In [18]:
abs(FBA_sol.f - pFBA_sol.f)
Out[18]: