{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Simulating with FBA" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import cobra.test\n", "model = cobra.test.create_test_model(\"textbook\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running FBA" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "solution = model.optimize()\n", "print(solution)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Model.optimize() function will return a Solution object. A solution object has several attributes:\n", "\n", " - `objective_value`: the objective value\n", " - `status`: the status from the linear programming solver\n", " - `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.\n", " - `shadow_prices`: a pandas series with shadow price indexed by the metabolite identifier." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.8739215069684307" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "solution.objective_value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 3.84 ms, sys: 672 µs, total: 4.51 ms\n", "Wall time: 6.16 ms\n" ] }, { "data": { "text/plain": [ "0.8739215069684307" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "model.optimize().objective_value" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 229 µs, sys: 19 µs, total: 248 µs\n", "Wall time: 257 µs\n" ] }, { "data": { "text/plain": [ "0.8739215069684307" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "model.slim_optimize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Analyzing FBA solutions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "IN FLUXES OUT FLUXES OBJECTIVES\n", "--------------- ------------ ----------------------\n", "o2_e 21.8 h2o_e 29.2 Biomass_Ecol... 0.874\n", "glc__D_e 10 co2_e 22.8\n", "nh4_e 4.77 h_e 17.5\n", "pi_e 3.21\n" ] } ], "source": [ "model.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "PRODUCING REACTIONS -- Nicotinamide adenine dinucleotide - reduced (nadh_c)\n", "---------------------------------------------------------------------------\n", "% FLUX RXN ID REACTION\n", "---- ------ ---------- --------------------------------------------------\n", "42% 16 GAPD g3p_c + nad_c + pi_c <=> 13dpg_c + h_c + nadh_c\n", "24% 9.28 PDH coa_c + nad_c + pyr_c --> accoa_c + co2_c + nadh_c\n", "13% 5.06 AKGDH akg_c + coa_c + nad_c --> co2_c + nadh_c + succ...\n", "13% 5.06 MDH mal__L_c + nad_c <=> h_c + nadh_c + oaa_c\n", "8% 3.1 Biomass... 1.496 3pg_c + 3.7478 accoa_c + 59.81 atp_c + 0....\n", "\n", "CONSUMING REACTIONS -- Nicotinamide adenine dinucleotide - reduced (nadh_c)\n", "---------------------------------------------------------------------------\n", "% FLUX RXN ID REACTION\n", "---- ------ ---------- --------------------------------------------------\n", "100% 38.5 NADH16 4.0 h_c + nadh_c + q8_c --> 3.0 h_e + nad_c + q...\n" ] } ], "source": [ "model.metabolites.nadh_c.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or to get a sense of the main energy production and consumption reactions" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "PRODUCING REACTIONS -- ATP (atp_c)\n", "----------------------------------\n", "% FLUX RXN ID REACTION\n", "--- ------ ---------- --------------------------------------------------\n", "67% 45.5 ATPS4r adp_c + 4.0 h_e + pi_c <=> atp_c + h2o_c + 3.0 h_c\n", "23% 16 PGK 3pg_c + atp_c <=> 13dpg_c + adp_c\n", "7% 5.06 SUCOAS atp_c + coa_c + succ_c <=> adp_c + pi_c + succoa_c\n", "3% 1.76 PYK adp_c + h_c + pep_c --> atp_c + pyr_c\n", "\n", "CONSUMING REACTIONS -- ATP (atp_c)\n", "----------------------------------\n", "% FLUX RXN ID REACTION\n", "--- ------ ---------- --------------------------------------------------\n", "76% 52.3 Biomass... 1.496 3pg_c + 3.7478 accoa_c + 59.81 atp_c + 0....\n", "12% 8.39 ATPM atp_c + h2o_c --> adp_c + h_c + pi_c\n", "11% 7.48 PFK atp_c + f6p_c --> adp_c + fdp_c + h_c\n", "0% 0.223 GLNS atp_c + glu__L_c + nh4_c --> adp_c + gln__L_c +...\n" ] } ], "source": [ "model.metabolites.atp_c.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Changing the Objectives" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "biomass_rxn = model.reactions.get_by_id(\"Biomass_Ecoli_core\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Currently in the model, there is only one reaction in the objective (the biomass reaction), with an linear coefficient of 1." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{: 1.0}" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from cobra.util.solver import linear_reaction_coefficients\n", "linear_reaction_coefficients(model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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}`." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{: 1.0}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# change the objective to ATPM\n", "model.objective = \"ATPM\"\n", "\n", "# The upper bound should be 1000, so that we get\n", "# the actual optimal value\n", "model.reactions.get_by_id(\"ATPM\").upper_bound = 1000.\n", "linear_reaction_coefficients(model)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "174.99999999999966" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.optimize().objective_value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also have more complicated objectives including quadratic terms." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running FVA" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from cobra.flux_analysis import flux_variability_analysis" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
maximumminimum
ACALD-2.208811e-30-5.247085e-14
ACALDt0.000000e+00-5.247085e-14
ACKr0.000000e+00-8.024953e-14
ACONTa2.000000e+012.000000e+01
ACONTb2.000000e+012.000000e+01
ACt2r0.000000e+00-8.024953e-14
ADK13.410605e-130.000000e+00
AKGDH2.000000e+012.000000e+01
AKGt2r0.000000e+00-2.902643e-14
ALCD2x0.000000e+00-4.547474e-14
\n", "
" ], "text/plain": [ " maximum minimum\n", "ACALD -2.208811e-30 -5.247085e-14\n", "ACALDt 0.000000e+00 -5.247085e-14\n", "ACKr 0.000000e+00 -8.024953e-14\n", "ACONTa 2.000000e+01 2.000000e+01\n", "ACONTb 2.000000e+01 2.000000e+01\n", "ACt2r 0.000000e+00 -8.024953e-14\n", "ADK1 3.410605e-13 0.000000e+00\n", "AKGDH 2.000000e+01 2.000000e+01\n", "AKGt2r 0.000000e+00 -2.902643e-14\n", "ALCD2x 0.000000e+00 -4.547474e-14" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "flux_variability_analysis(model, model.reactions[:10])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Setting parameter `fraction_of_optimium=0.90` would give the flux ranges for reactions at 90% optimality." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
maximumminimum
ACALD0.000000e+00-2.692308
ACALDt0.000000e+00-2.692308
ACKr6.635712e-30-4.117647
ACONTa2.000000e+018.461538
ACONTb2.000000e+018.461538
ACt2r0.000000e+00-4.117647
ADK11.750000e+010.000000
AKGDH2.000000e+012.500000
AKGt2r2.651196e-16-1.489362
ALCD2x0.000000e+00-2.333333
\n", "
" ], "text/plain": [ " maximum minimum\n", "ACALD 0.000000e+00 -2.692308\n", "ACALDt 0.000000e+00 -2.692308\n", "ACKr 6.635712e-30 -4.117647\n", "ACONTa 2.000000e+01 8.461538\n", "ACONTb 2.000000e+01 8.461538\n", "ACt2r 0.000000e+00 -4.117647\n", "ADK1 1.750000e+01 0.000000\n", "AKGDH 2.000000e+01 2.500000\n", "AKGt2r 2.651196e-16 -1.489362\n", "ALCD2x 0.000000e+00 -2.333333" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cobra.flux_analysis.flux_variability_analysis(\n", " model, model.reactions[:10], fraction_of_optimum=0.9)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
maximumminimum
FRD7980.00.0
SUCDi1000.020.0
\n", "
" ], "text/plain": [ " maximum minimum\n", "FRD7 980.0 0.0\n", "SUCDi 1000.0 20.0" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "loop_reactions = [model.reactions.FRD7, model.reactions.SUCDi]\n", "flux_variability_analysis(model, reaction_list=loop_reactions, loopless=False)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
maximumminimum
FRD70.00.0
SUCDi20.020.0
\n", "
" ], "text/plain": [ " maximum minimum\n", "FRD7 0.0 0.0\n", "SUCDi 20.0 20.0" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "flux_variability_analysis(model, reaction_list=loop_reactions, loopless=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Running FVA in summary methods" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "IN FLUXES OUT FLUXES OBJECTIVES\n", "---------------------------- ---------------------------- ------------\n", "id Flux Range id Flux Range ATPM 175\n", "-------- ------ ---------- -------- ------ ----------\n", "o2_e 60 [55.9, 60] co2_e 60 [54.2, 60]\n", "glc__D_e 10 [9.5, 10] h2o_e 60 [54.2, 60]\n", "nh4_e 0 [0, 0.673] for_e 0 [0, 5.83]\n", "pi_e 0 [0, 0.171] h_e 0 [0, 5.83]\n", " ac_e 0 [0, 2.06]\n", " acald_e 0 [0, 1.35]\n", " pyr_e 0 [0, 1.35]\n", " etoh_e 0 [0, 1.17]\n", " lac__D_e 0 [0, 1.13]\n", " succ_e 0 [0, 0.875]\n", " akg_e 0 [0, 0.745]\n", " glu__L_e 0 [0, 0.673]\n" ] } ], "source": [ "model.optimize()\n", "model.summary(fva=0.95)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly, variability in metabolite mass balances can also be checked with flux variability analysis." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "PRODUCING REACTIONS -- Pyruvate (pyr_c)\n", "---------------------------------------\n", "% FLUX RANGE RXN ID REACTION\n", "---- ------ ------------ ---------- ----------------------------------------\n", "50% 10 [1.25, 18.8] PYK adp_c + h_c + pep_c --> atp_c + pyr_c\n", "50% 10 [9.5, 10] GLCpts glc__D_e + pep_c --> g6p_c + pyr_c\n", "0% 0 [0, 8.75] ME1 mal__L_c + nad_c --> co2_c + nadh_c +...\n", "0% 0 [0, 8.75] ME2 mal__L_c + nadp_c --> co2_c + nadph_c...\n", "\n", "CONSUMING REACTIONS -- Pyruvate (pyr_c)\n", "---------------------------------------\n", "% FLUX RANGE RXN ID REACTION\n", "---- ------ ------------ ---------- ----------------------------------------\n", "100% 20 [13, 28.8] PDH coa_c + nad_c + pyr_c --> accoa_c + c...\n", "0% 0 [0, 8.75] PPS atp_c + h2o_c + pyr_c --> amp_c + 2.0...\n", "0% 0 [0, 5.83] PFL coa_c + pyr_c --> accoa_c + for_c\n", "0% 0 [0, 1.35] PYRt2 h_e + pyr_e <=> h_c + pyr_c\n", "0% 0 [0, 1.13] LDH_D lac__D_c + nad_c <=> h_c + nadh_c + p...\n", "0% 0 [0, 0.132] Biomass... 1.496 3pg_c + 3.7478 accoa_c + 59.81 ...\n" ] } ], "source": [ "model.metabolites.pyr_c.summary(fva=0.95)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running pFBA" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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)." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "model.objective = 'Biomass_Ecoli_core'\n", "fba_solution = model.optimize()\n", "pfba_solution = cobra.flux_analysis.pfba(model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These functions should give approximately the same objective value." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7.7715611723760958e-16" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(fba_solution.fluxes[\"Biomass_Ecoli_core\"] - pfba_solution.fluxes[\n", " \"Biomass_Ecoli_core\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running geometric FBA" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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)." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Optimal solution with objective value 0.000
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
fluxesreduced_costs
ACALD1.785214e-140.0
ACALDt1.785214e-140.0
ACKr0.000000e+000.0
ACONTa6.007250e+000.0
ACONTb6.007250e+000.0
.........
TALA1.496984e+000.0
THD21.522652e-140.0
TKT11.496984e+000.0
TKT21.181498e+000.0
TPI7.477382e+000.0
\n", "

95 rows × 2 columns

\n", "
" ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "geometric_fba_sol = cobra.flux_analysis.geometric_fba(model)\n", "geometric_fba_sol" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.0" } }, "nbformat": 4, "nbformat_minor": 1 }