{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Simulate models\n", "\n", "cameo uses and extends the model data structures defined by [cobrapy](https://opencobra.github.io/cobrapy/), our favorite COnstraints-Based Reconstruction and Analysis tool for Python. cameo is thus 100% compatible with cobrapy. For efficiency reasons, however, cameo implements its own simulation methods that take advantage of a more advanced solver interface." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Primer: Constraint-Based Modeling\n", "\n", "Constraint-based modeling is a powerful modeling framework for analyzing metabolism on the genome scale ([McCloskey et al., 2013](http://www.ncbi.nlm.nih.gov/pubmed/23632383)). For a model that encompasses $n$ reactions that involve $m$ metabolites, $\\mathbf{S}$ is a matrix of dimension $m \\times n$ that encodes the stoichiometry of the metabolic reaction system; it is usually referred to as stoichiometric matrix. Assuming that the system is in a steady state—the concentration of metabolites are constant—the system of flux-balances can be formulated as\n", "\n", "$$\n", "\\begin{align}\n", "\\mathbf{S} \\mathbf{v} = 0\\,,\n", "\\end{align}\n", "$$\n", "\n", "where $\\mathbf{v}$ is the vector of flux rates. With the addition of a biologically meaningful objective, flux capacity constraints, information about the reversibility of reactions under physiological conditions, an optimization problem can be formulated that can easily be solved using [linear programming](https://en.wikipedia.org/wiki/Linear_programming).\n", "\n", "\n", ", e.g., maximimization of biomass production,Given the maximization of growth rate as one potential biological objective $v_{biomass}$, i.e., the flux of an artificial reaction that consumes biomass components in empirically determined proportions, and assuming that the cell is evolutionary optimized to achieve that objective, and incorporating knowledge about reaction reversibility, uptake and secretion rates, and maximum flux capacities in the form of lower and uppers bounds ($\\mathbf{v}_{lb}$ and $\\mathbf{v}_{ub}$) on the flux variables $\\mathbf{v}$, one can formulate and solve an optimization problem to identify an optimal set of flux rates using flux balance analysis (FBA):\n", "\n", "$$\n", "\\begin{align}\n", " Max ~ & ~ Z_{obj} = \\mathbf{c}^{T} \\mathbf{v}\\\\\n", " \\text{s.t.}~ & ~ \\mathbf{S} \\mathbf{v} = 0 \\\\\n", " ~ & ~ \\mathbf{v}_{lb} \\leq \\mathbf{v} \\leq \\mathbf{v}_{ub} \\,.\n", "\\end{align}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Flux Balance Analysis\n", "\n", "Load a model." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "(function(global) {\n", " function now() {\n", " return new Date();\n", " }\n", "\n", " if (typeof (window._bokeh_onload_callbacks) === \"undefined\") {\n", " window._bokeh_onload_callbacks = [];\n", " }\n", "\n", " function run_callbacks() {\n", " window._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n", " delete window._bokeh_onload_callbacks\n", " console.info(\"Bokeh: all callbacks have finished\");\n", " }\n", "\n", " function load_libs(js_urls, callback) {\n", " window._bokeh_onload_callbacks.push(callback);\n", " if (window._bokeh_is_loading > 0) {\n", " console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n", " return null;\n", " }\n", " if (js_urls == null || js_urls.length === 0) {\n", " run_callbacks();\n", " return null;\n", " }\n", " console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", " window._bokeh_is_loading = js_urls.length;\n", " for (var i = 0; i < js_urls.length; i++) {\n", " var url = js_urls[i];\n", " var s = document.createElement('script');\n", " s.src = url;\n", " s.async = false;\n", " s.onreadystatechange = s.onload = function() {\n", " window._bokeh_is_loading--;\n", " if (window._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: all BokehJS libraries loaded\");\n", " run_callbacks()\n", " }\n", " };\n", " s.onerror = function() {\n", " console.warn(\"failed to load library \" + url);\n", " };\n", " console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", " document.getElementsByTagName(\"head\")[0].appendChild(s);\n", " }\n", " };\n", "\n", " var js_urls = ['https://cdn.pydata.org/bokeh/release/bokeh-0.11.1.min.js', 'https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.11.1.min.js', 'https://cdn.pydata.org/bokeh/release/bokeh-compiler-0.11.1.min.js'];\n", "\n", " var inline_js = [\n", " function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", " \n", " function(Bokeh) {\n", " Bokeh.$(\"#39b7e70b-33aa-410f-b9ac-a962119388ed\").text(\"BokehJS successfully loaded\");\n", " },\n", " function(Bokeh) {\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.11.1.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.11.1.min.css\");\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.11.1.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.11.1.min.css\");\n", " }\n", " ];\n", "\n", " function run_inline_js() {\n", " for (var i = 0; i < inline_js.length; i++) {\n", " inline_js[i](window.Bokeh);\n", " }\n", " }\n", "\n", " if (window._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n", " run_inline_js();\n", " } else {\n", " load_libs(js_urls, function() {\n", " console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n", " run_inline_js();\n", " });\n", " }\n", "}(this));" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from cameo import load_model\n", "model = load_model('iJO1366')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In cameo, flux balance analysis can be performed with the function `fba`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 348 ms, sys: 5.36 ms, total: 353 ms\n", "Wall time: 378 ms\n" ] } ], "source": [ "from cameo import fba\n", "%time fba_result = fba(model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Basically, `fba` calls `model.solve()` and wraps the optimization solution in a `FluxDistributionResult` object. The maximum objective values (corresponding to a maximum growth rate) can obtained throug `result.objective_value`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "scrolled": false }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
flux
DM_4crsol_c0.000219
DM_5drib_c0.000221
DM_aacald_c0.000000
DM_amob_c0.000002
DM_mththf_c0.000440
DM_oxam_c0.000000
BIOMASS_Ec_iJO1366_WT_53p95M0.000000
......
XYLt2pp0.000000
XYLtex0.000000
ZN2abcpp0.000000
ZN2t3pp0.000000
ZN2tpp0.000335
ZNabcpp0.000000
Zn2tex0.000335
\n", "

2583 rows × 1 columns

\n", "
" ], "text/plain": [ " flux\n", "DM_4crsol_c 0.000219\n", "DM_5drib_c 0.000221\n", "DM_aacald_c 0.000000\n", "DM_amob_c 0.000002\n", "DM_mththf_c 0.000440\n", "DM_oxam_c 0.000000\n", "BIOMASS_Ec_iJO1366_WT_53p95M 0.000000\n", "... ...\n", "XYLt2pp 0.000000\n", "XYLtex 0.000000\n", "ZN2abcpp 0.000000\n", "ZN2t3pp 0.000000\n", "ZN2tpp 0.000335\n", "ZNabcpp 0.000000\n", "Zn2tex 0.000335\n", "\n", "[2583 rows x 1 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fba_result.data_frame" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Flux distributions can be visualized using [*escher*](https://escher.github.io) :" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", "\n", "\n", "
\n", "\n", " \n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "fba_result.display_on_map(\"iJO1366.Central metabolism\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Parsimonious Flux Balance Analysis\n", "\n", "Parsimonious flux balance analysis ([Lewis et al., 2010](http://www.ncbi.nlm.nih.gov/pubmed/20664636)), a variant of FBA, performs FBA in in a first step to determine the maximum objective value $Z_{obj}$, fixes it in form of an additional model constraint ($\\mathbf{c}^{T} \\mathbf{v} \\ge Z_{obj}$), and then minimizes in a second optimization the $L_1$ norm of $\\mathbf{v}$. The assumption behind pFBA is that cells try to minimize flux magnitude as well in order to keep protein costs low.\n", "\n", "$$\n", "\\begin{align}\n", " Max ~ & ~ \\lvert \\mathbf{v} \\rvert\\\\\n", " \\text{s.t.}~ & ~ \\mathbf{S} \\mathbf{v} = 0 \\\\\n", " & ~ \\mathbf{c}^{T} \\mathbf{v} \\ge Z_{obj} \\\\\n", " ~ & ~ \\mathbf{v}_{lb} \\leq \\mathbf{v} \\leq \\mathbf{v}_{ub} \\,.\n", "\\end{align}\n", "$$\n", "\n", "In cameo, pFBA can be performed with the function `pfba`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 382 ms, sys: 15.3 ms, total: 398 ms\n", "Wall time: 494 ms\n" ] } ], "source": [ "from cameo import pfba\n", "%time pfba_result = pfba(model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `objective_function` value is $\\lvert \\mathbf{v} \\rvert$ ..." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "699.0222751839442" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pfba_result.objective_value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... which is smaller than flux vector of the original FBA solution." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "701.81777502444379" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(fba_result.data_frame.flux).sum()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Setp 2: Simulate knockouts phenotypes\n", "-----------------------------------\n", "\n", "Although PFBA and FBA can be used to simulate the effect of knockouts, other methods have been proven more valuable for that task: MOMA and ROOM. In *cameo* we implement a linear version of MOMA.\n", "\n", "*******************************************\n", "Simulating knockouts:\n", "\n", "* Manipulate the bounds of the reaction (or use the shorthand method knock_out)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
IdPGI
NameGlucose-6-phosphate isomerase
Stoichiometryg6p_c <=> f6p_c
Lower bound-1000.000000
Upper bound1000.000000
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.reactions.PGI" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
IdPGI
NameGlucose-6-phosphate isomerase
Stoichiometryg6p_c --> f6p_c
Lower bound0.000000
Upper bound0.000000
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.reactions.PGI.knock_out()\n", "model.reactions.PGI" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Simulate using different methods:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 257 ms, sys: 6.5 ms, total: 264 ms\n", "Wall time: 359 ms\n" ] }, { "data": { "text/plain": [ "0.9761293262973523" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time fba_knockout_result = fba(model)\n", "fba_knockout_result[model.reactions.BIOMASS_Ec_iJO1366_core_53p95M]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 664 ms, sys: 21.4 ms, total: 685 ms\n", "Wall time: 1.05 s\n" ] }, { "data": { "text/plain": [ "0.9761293262947272" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time pfba_knockout_result = pfba(model)\n", "pfba_knockout_result[model.reactions.BIOMASS_Ec_iJO1366_core_53p95M]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "MOMA and ROOM relly on a reference (wild-type) flux distribution and we can use the one previously computed.\n", "\n", "**Parsimonious FBA references seem to produce better results using this methods**" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from cameo.flux_analysis.simulation import room, lmoma" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 8.07 s, sys: 120 ms, total: 8.19 s\n", "Wall time: 8.93 s\n" ] }, { "data": { "text/plain": [ "0.8724092397035627" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time lmoma_result = lmoma(model, reference=pfba_result.fluxes)\n", "lmoma_result[model.reactions.BIOMASS_Ec_iJO1366_core_53p95M]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ROOM is a dificult computational problem. If the bounds of the system are not large enought, it can take many hours to simulate. To improve the speed of the simulation and the chances of finding a solution, we increase the bounds." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for reaction in model.reactions:\n", " if reaction.upper_bound == 1000:\n", " reaction.upper_bound = 99999999\n", " if reaction.lower_bound == -1000:\n", " reaction.lower_bound = -99999999" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 11.5 s, sys: 83 ms, total: 11.6 s\n", "Wall time: 11.8 s\n" ] }, { "data": { "text/plain": [ "0.8724092397035627" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time room_result = room(model, reference=pfba_result.fluxes)\n", "room_result[model.reactions.BIOMASS_Ec_iJO1366_core_53p95M]" ] } ], "metadata": { "kernelspec": { "display_name": "IPython (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.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }