#!/usr/bin/env python # coding: utf-8 # # 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]: from cobra.io import load_model model = load_model("textbook") # ## Running FBA # 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 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 # 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]: get_ipython().run_cell_magic('time', '', 'model.optimize().objective_value\n') # In[5]: get_ipython().run_cell_magic('time', '', 'model.slim_optimize()\n') # ### 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 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() # ## 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) # 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) # In[12]: model.optimize().objective_value # 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]) # 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) # 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) # In[17]: flux_variability_analysis(model, reaction_list=loop_reactions, loopless=True) # ### 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[17]: model.optimize() model.summary(fva=0.95) # Similarly, variability in metabolite mass balances can also be checked with flux variability analysis. # In[18]: 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. # ## 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)](http://dx.doi.org/10.1038/msb.2010.47). # In[19]: model.objective = 'Biomass_Ecoli_core' fba_solution = model.optimize() pfba_solution = cobra.flux_analysis.pfba(model) # These functions will likely give completely different objective values, because the objective value shown from pFBA is defined as `sum(abs(pfba_solution.fluxes.values))`, while the objective value for standard FBA is defined as the weighted flux through the reactions being optimized for (e.g. `fba_solution.fluxes["Biomass_Ecoli_core"]`). # # Both pFBA and FBA should return identical results within solver tolerances for the objective being optimized. E.g. for a FBA problem which maximizes the reaction `Biomass_Ecoli_core`: # In[20]: abs(fba_solution.fluxes["Biomass_Ecoli_core"] - pfba_solution.fluxes[ "Biomass_Ecoli_core"]) # In[21]: import numpy as np np.isclose( fba_solution.fluxes["Biomass_Ecoli_core"], pfba_solution.fluxes["Biomass_Ecoli_core"] ) # ## 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)](http://dx.doi.org/10.1016/j.jtbi.2009.01.027). # In[22]: geometric_fba_sol = cobra.flux_analysis.geometric_fba(model) geometric_fba_sol # In[ ]: