{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "# Simulating Deletions" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "import pandas\n", "from time import time\n", "\n", "import cobra.test\n", "from cobra.flux_analysis import (\n", " single_gene_deletion, single_reaction_deletion, double_gene_deletion,\n", " double_reaction_deletion)\n", "\n", "cobra_model = cobra.test.create_test_model(\"textbook\")\n", "ecoli_model = cobra.test.create_test_model(\"ecoli\")" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "## Knocking out single genes and reactions" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "A commonly asked question when analyzing metabolic models is what will happen if a certain reaction was not allowed to have any flux at all. This can tested using cobrapy by" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "complete model: \n", "pfk knocked out: \n" ] } ], "source": [ "print('complete model: ', cobra_model.optimize())\n", "with cobra_model:\n", " cobra_model.reactions.PFK.knock_out()\n", " print('pfk knocked out: ', cobra_model.optimize())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For evaluating genetic manipulation strategies, it is more interesting to examine what happens if given genes are knocked out as doing so can affect no reactions in case of redundancy, or more reactions if gene when is participating in more than one reaction." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "complete model: \n", "pfkA knocked out: \n", "pfkB knocked out: \n" ] } ], "source": [ "print('complete model: ', cobra_model.optimize())\n", "with cobra_model:\n", " cobra_model.genes.b1723.knock_out()\n", " print('pfkA knocked out: ', cobra_model.optimize())\n", " cobra_model.genes.b3916.knock_out()\n", " print('pfkB knocked out: ', cobra_model.optimize())" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "## Single Deletions" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Perform all single gene deletions on a model" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "deletion_results = single_gene_deletion(cobra_model)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "These can also be done for only a subset of genes" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "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", " \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", "
fluxstatus
b01160.782351optimal
b01180.873922optimal
b03510.873922optimal
b03560.873922optimal
b04740.873922optimal
b07260.858307optimal
b07270.858307optimal
b12410.873922optimal
b12760.873922optimal
b14780.873922optimal
b18490.873922optimal
b22960.873922optimal
b25870.873922optimal
b31150.873922optimal
b37320.374230optimal
b37330.374230optimal
b37340.374230optimal
b37350.374230optimal
b37360.374230optimal
s00010.211141optimal
\n", "
" ], "text/plain": [ " flux status\n", "b0116 0.782351 optimal\n", "b0118 0.873922 optimal\n", "b0351 0.873922 optimal\n", "b0356 0.873922 optimal\n", "b0474 0.873922 optimal\n", "b0726 0.858307 optimal\n", "b0727 0.858307 optimal\n", "b1241 0.873922 optimal\n", "b1276 0.873922 optimal\n", "b1478 0.873922 optimal\n", "b1849 0.873922 optimal\n", "b2296 0.873922 optimal\n", "b2587 0.873922 optimal\n", "b3115 0.873922 optimal\n", "b3732 0.374230 optimal\n", "b3733 0.374230 optimal\n", "b3734 0.374230 optimal\n", "b3735 0.374230 optimal\n", "b3736 0.374230 optimal\n", "s0001 0.211141 optimal" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "single_gene_deletion(cobra_model, cobra_model.genes[:20])" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "This can also be done for reactions" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "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", " \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", "
fluxstatus
ACALD8.739215e-01optimal
ACALDt8.739215e-01optimal
ACKr8.739215e-01optimal
ACONTa-5.039994e-13optimal
ACONTb-1.477823e-12optimal
ACt2r8.739215e-01optimal
ADK18.739215e-01optimal
AKGDH8.583074e-01optimal
AKGt2r8.739215e-01optimal
ALCD2x8.739215e-01optimal
ATPM9.166475e-01optimal
ATPS4r3.742299e-01optimal
Biomass_Ecoli_core0.000000e+00optimal
CO2t4.616696e-01optimal
CS1.129472e-12optimal
CYTBD2.116629e-01optimal
D_LACt28.739215e-01optimal
ENO1.161773e-14optimal
ETOHt2r8.739215e-01optimal
EX_ac_e8.739215e-01optimal
\n", "
" ], "text/plain": [ " flux status\n", "ACALD 8.739215e-01 optimal\n", "ACALDt 8.739215e-01 optimal\n", "ACKr 8.739215e-01 optimal\n", "ACONTa -5.039994e-13 optimal\n", "ACONTb -1.477823e-12 optimal\n", "ACt2r 8.739215e-01 optimal\n", "ADK1 8.739215e-01 optimal\n", "AKGDH 8.583074e-01 optimal\n", "AKGt2r 8.739215e-01 optimal\n", "ALCD2x 8.739215e-01 optimal\n", "ATPM 9.166475e-01 optimal\n", "ATPS4r 3.742299e-01 optimal\n", "Biomass_Ecoli_core 0.000000e+00 optimal\n", "CO2t 4.616696e-01 optimal\n", "CS 1.129472e-12 optimal\n", "CYTBD 2.116629e-01 optimal\n", "D_LACt2 8.739215e-01 optimal\n", "ENO 1.161773e-14 optimal\n", "ETOHt2r 8.739215e-01 optimal\n", "EX_ac_e 8.739215e-01 optimal" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "single_reaction_deletion(cobra_model, cobra_model.reactions[:20])" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "## Double Deletions" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Double deletions run in a similar way. Passing in `return_frame=True` will cause them to format the results as a `pandas.DataFrame`." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "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", "
b2464b0008b2935b2465b3919
b24640.87390.86480.87390.87390.704
b00080.86480.87390.87390.87390.704
b29350.87390.87390.87390.00000.704
b24650.87390.87390.00000.87390.704
b39190.70400.70400.70400.70400.704
\n", "
" ], "text/plain": [ " b2464 b0008 b2935 b2465 b3919\n", "b2464 0.8739 0.8648 0.8739 0.8739 0.704\n", "b0008 0.8648 0.8739 0.8739 0.8739 0.704\n", "b2935 0.8739 0.8739 0.8739 0.0000 0.704\n", "b2465 0.8739 0.8739 0.0000 0.8739 0.704\n", "b3919 0.7040 0.7040 0.7040 0.7040 0.704" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "double_gene_deletion(\n", " cobra_model, cobra_model.genes[-5:], return_frame=True).round(4)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "By default, the double deletion function will automatically use multiprocessing, splitting the task over up to 4 cores if they are available. The number of cores can be manually specified as well. Setting use of a single core will disable use of the multiprocessing library, which often aids debugging." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Double gene deletions for 200 genes completed in 33.26 sec with 2 cores\n", "Double gene deletions for 200 genes completed in 45.38 sec with 1 core\n", "Speedup of 1.36x\n" ] } ], "source": [ "start = time() # start timer()\n", "double_gene_deletion(\n", " ecoli_model, ecoli_model.genes[:300], number_of_processes=2)\n", "t1 = time() - start\n", "print(\"Double gene deletions for 200 genes completed in \"\n", " \"%.2f sec with 2 cores\" % t1)\n", "\n", "start = time() # start timer()\n", "double_gene_deletion(\n", " ecoli_model, ecoli_model.genes[:300], number_of_processes=1)\n", "t2 = time() - start\n", "print(\"Double gene deletions for 200 genes completed in \"\n", " \"%.2f sec with 1 core\" % t2)\n", "\n", "print(\"Speedup of %.2fx\" % (t2 / t1))" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Double deletions can also be run for reactions." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "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", "
ACKrACONTaACONTbACt2rADK1
ACKr0.87390.00.00.87390.8739
ACONTa0.00000.00.00.00000.0000
ACONTb0.00000.00.00.0000-0.0000
ACt2r0.87390.00.00.87390.8739
ADK10.87390.0-0.00.87390.8739
\n", "
" ], "text/plain": [ " ACKr ACONTa ACONTb ACt2r ADK1\n", "ACKr 0.8739 0.0 0.0 0.8739 0.8739\n", "ACONTa 0.0000 0.0 0.0 0.0000 0.0000\n", "ACONTb 0.0000 0.0 0.0 0.0000 -0.0000\n", "ACt2r 0.8739 0.0 0.0 0.8739 0.8739\n", "ADK1 0.8739 0.0 -0.0 0.8739 0.8739" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "double_reaction_deletion(\n", " cobra_model, cobra_model.reactions[2:7], return_frame=True).round(4)" ] } ], "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": 0 }