{
"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",
"
flux
\n",
"
\n",
" \n",
" \n",
"
\n",
"
DM_4crsol_c
\n",
"
0.000219
\n",
"
\n",
"
\n",
"
DM_5drib_c
\n",
"
0.000221
\n",
"
\n",
"
\n",
"
DM_aacald_c
\n",
"
0.000000
\n",
"
\n",
"
\n",
"
DM_amob_c
\n",
"
0.000002
\n",
"
\n",
"
\n",
"
DM_mththf_c
\n",
"
0.000440
\n",
"
\n",
"
\n",
"
DM_oxam_c
\n",
"
0.000000
\n",
"
\n",
"
\n",
"
BIOMASS_Ec_iJO1366_WT_53p95M
\n",
"
0.000000
\n",
"
\n",
"
\n",
"
...
\n",
"
...
\n",
"
\n",
"
\n",
"
XYLt2pp
\n",
"
0.000000
\n",
"
\n",
"
\n",
"
XYLtex
\n",
"
0.000000
\n",
"
\n",
"
\n",
"
ZN2abcpp
\n",
"
0.000000
\n",
"
\n",
"
\n",
"
ZN2t3pp
\n",
"
0.000000
\n",
"
\n",
"
\n",
"
ZN2tpp
\n",
"
0.000335
\n",
"
\n",
"
\n",
"
ZNabcpp
\n",
"
0.000000
\n",
"
\n",
"
\n",
"
Zn2tex
\n",
"
0.000335
\n",
"
\n",
" \n",
"
\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",
"