{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Simulating Deletions" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas\n", "from time import time\n", "\n", "from cobra.io import load_model\n", "from cobra.flux_analysis import (\n", " single_gene_deletion, single_reaction_deletion, double_gene_deletion,\n", " double_reaction_deletion)\n", "\n", "cobra_model = load_model(\"textbook\")\n", "ecoli_model = load_model(\"iJO1366\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Knocking out single genes and reactions" ] }, { "cell_type": "markdown", "metadata": {}, "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": { "tags": [] }, "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": { "tags": [] }, "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": {}, "source": [ "## Single Deletions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Perform all single gene deletions on a model" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "deletion_results = single_gene_deletion(cobra_model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These can also be done for only a subset of genes" ] }, { "cell_type": "code", "execution_count": 5, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsgrowthstatus
0{b1849}0.873922optimal
1{b1478}0.873922optimal
2{b1276}0.873922optimal
3{b0351}0.873922optimal
4{b3733}0.374230optimal
5{b1241}0.873922optimal
6{b0116}0.782351optimal
7{b0727}0.858307optimal
8{b0356}0.873922optimal
9{b2587}0.873922optimal
10{b0118}0.873922optimal
11{b0726}0.858307optimal
12{b3736}0.374230optimal
13{b3735}0.374230optimal
14{b2296}0.873922optimal
15{b3115}0.873922optimal
16{b3732}0.374230optimal
17{b0474}0.873922optimal
18{s0001}0.211141optimal
19{b3734}0.374230optimal
\n", "
" ], "text/plain": [ " ids growth status\n", "0 {b1849} 0.873922 optimal\n", "1 {b1478} 0.873922 optimal\n", "2 {b1276} 0.873922 optimal\n", "3 {b0351} 0.873922 optimal\n", "4 {b3733} 0.374230 optimal\n", "5 {b1241} 0.873922 optimal\n", "6 {b0116} 0.782351 optimal\n", "7 {b0727} 0.858307 optimal\n", "8 {b0356} 0.873922 optimal\n", "9 {b2587} 0.873922 optimal\n", "10 {b0118} 0.873922 optimal\n", "11 {b0726} 0.858307 optimal\n", "12 {b3736} 0.374230 optimal\n", "13 {b3735} 0.374230 optimal\n", "14 {b2296} 0.873922 optimal\n", "15 {b3115} 0.873922 optimal\n", "16 {b3732} 0.374230 optimal\n", "17 {b0474} 0.873922 optimal\n", "18 {s0001} 0.211141 optimal\n", "19 {b3734} 0.374230 optimal" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "single_gene_deletion(cobra_model, cobra_model.genes[:20])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This can also be done for reactions" ] }, { "cell_type": "code", "execution_count": 6, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsgrowthstatus
0{AKGt2r}8.739215e-01optimal
1{D_LACt2}8.739215e-01optimal
2{Biomass_Ecoli_core}0.000000e+00optimal
3{ETOHt2r}8.739215e-01optimal
4{ADK1}8.739215e-01optimal
5{ACONTb}3.279963e-17optimal
6{ACt2r}8.739215e-01optimal
7{ACONTa}3.344590e-15optimal
8{ACKr}8.739215e-01optimal
9{ALCD2x}8.739215e-01optimal
10{ATPS4r}3.742299e-01optimal
11{ACALD}8.739215e-01optimal
12{ENO}1.357454e-16optimal
13{EX_ac_e}8.739215e-01optimal
14{ACALDt}8.739215e-01optimal
15{CS}4.757918e-15optimal
16{AKGDH}8.583074e-01optimal
17{CO2t}4.616696e-01optimal
18{CYTBD}2.116629e-01optimal
19{ATPM}9.166475e-01optimal
\n", "
" ], "text/plain": [ " ids growth status\n", "0 {AKGt2r} 8.739215e-01 optimal\n", "1 {D_LACt2} 8.739215e-01 optimal\n", "2 {Biomass_Ecoli_core} 0.000000e+00 optimal\n", "3 {ETOHt2r} 8.739215e-01 optimal\n", "4 {ADK1} 8.739215e-01 optimal\n", "5 {ACONTb} 3.279963e-17 optimal\n", "6 {ACt2r} 8.739215e-01 optimal\n", "7 {ACONTa} 3.344590e-15 optimal\n", "8 {ACKr} 8.739215e-01 optimal\n", "9 {ALCD2x} 8.739215e-01 optimal\n", "10 {ATPS4r} 3.742299e-01 optimal\n", "11 {ACALD} 8.739215e-01 optimal\n", "12 {ENO} 1.357454e-16 optimal\n", "13 {EX_ac_e} 8.739215e-01 optimal\n", "14 {ACALDt} 8.739215e-01 optimal\n", "15 {CS} 4.757918e-15 optimal\n", "16 {AKGDH} 8.583074e-01 optimal\n", "17 {CO2t} 4.616696e-01 optimal\n", "18 {CYTBD} 2.116629e-01 optimal\n", "19 {ATPM} 9.166475e-01 optimal" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "single_reaction_deletion(cobra_model, cobra_model.reactions[:20])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Double Deletions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double deletions run in a similar way." ] }, { "cell_type": "code", "execution_count": 7, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsgrowthstatus
0{b2465, b3919}0.7040optimal
1{b2935, b2464}0.8739optimal
2{b0008, b2465}0.8739optimal
3{b2465}0.8739optimal
4{b0008}0.8739optimal
5{b0008, b2464}0.8648optimal
6{b2465, b2935}-0.0000optimal
7{b0008, b3919}0.7040optimal
8{b2465, b2464}0.8739optimal
9{b0008, b2935}0.8739optimal
10{b3919}0.7040optimal
11{b2464}0.8739optimal
12{b2935}0.8739optimal
13{b2935, b3919}0.7040optimal
14{b3919, b2464}0.7040optimal
\n", "
" ], "text/plain": [ " ids growth status\n", "0 {b2465, b3919} 0.7040 optimal\n", "1 {b2935, b2464} 0.8739 optimal\n", "2 {b0008, b2465} 0.8739 optimal\n", "3 {b2465} 0.8739 optimal\n", "4 {b0008} 0.8739 optimal\n", "5 {b0008, b2464} 0.8648 optimal\n", "6 {b2465, b2935} -0.0000 optimal\n", "7 {b0008, b3919} 0.7040 optimal\n", "8 {b2465, b2464} 0.8739 optimal\n", "9 {b0008, b2935} 0.8739 optimal\n", "10 {b3919} 0.7040 optimal\n", "11 {b2464} 0.8739 optimal\n", "12 {b2935} 0.8739 optimal\n", "13 {b2935, b3919} 0.7040 optimal\n", "14 {b3919, b2464} 0.7040 optimal" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "double_gene_deletion(\n", " cobra_model, cobra_model.genes[-5:]).round(4)" ] }, { "cell_type": "markdown", "metadata": {}, "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": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Double gene deletions for 200 genes completed in 1.29 sec with 2 cores\n", "Double gene deletions for 200 genes completed in 1.74 sec with 1 core\n", "Speedup of 1.35x\n" ] } ], "source": [ "start = time() # start timer()\n", "double_gene_deletion(\n", " ecoli_model, ecoli_model.genes[:25], 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[:25], 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": {}, "source": [ "Double deletions can also be run for reactions." ] }, { "cell_type": "code", "execution_count": 9, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsgrowthstatus
0{ACONTa}0.0000optimal
1{ACt2r, ACONTa}0.0000optimal
2{ACKr}0.8739optimal
3{ACt2r, ACKr}0.8739optimal
4{ADK1, ACONTb}0.0000optimal
5{ACONTa, ACONTb}0.0000optimal
6{ACKr, ACONTa}0.0000optimal
7{ACKr, ACONTb}0.0000optimal
8{ACt2r, ADK1}0.8739optimal
9{ACt2r, ACONTb}0.0000optimal
10{ADK1, ACKr}0.8739optimal
11{ACONTb}0.0000optimal
12{ADK1, ACONTa}0.0000optimal
13{ACt2r}0.8739optimal
14{ADK1}0.8739optimal
\n", "
" ], "text/plain": [ " ids growth status\n", "0 {ACONTa} 0.0000 optimal\n", "1 {ACt2r, ACONTa} 0.0000 optimal\n", "2 {ACKr} 0.8739 optimal\n", "3 {ACt2r, ACKr} 0.8739 optimal\n", "4 {ADK1, ACONTb} 0.0000 optimal\n", "5 {ACONTa, ACONTb} 0.0000 optimal\n", "6 {ACKr, ACONTa} 0.0000 optimal\n", "7 {ACKr, ACONTb} 0.0000 optimal\n", "8 {ACt2r, ADK1} 0.8739 optimal\n", "9 {ACt2r, ACONTb} 0.0000 optimal\n", "10 {ADK1, ACKr} 0.8739 optimal\n", "11 {ACONTb} 0.0000 optimal\n", "12 {ADK1, ACONTa} 0.0000 optimal\n", "13 {ACt2r} 0.8739 optimal\n", "14 {ADK1} 0.8739 optimal" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "double_reaction_deletion(\n", " cobra_model, cobra_model.reactions[2:7]).round(4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing individual deletion results\n", "\n", "Note that the indices for deletions are python [set](https://docs.python.org/3.8/library/stdtypes.html#set-types-set-frozenset) objects. This is the appropriate type since the order of deletions does not matter. Deleting reaction 1 and reaction 2 will have the same effect as deleting reaction 2 and reaction 1. \n", "\n", "To make it easier to access results all DataFrames returned by COBRAPpy deletion functions have a `knockout` indexer that makes that a bit simpler. Each entry in the indexer is treated as a single deletion entry. So you need to pass sets for double deletions." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " ids growth status\n", "12 {ATPM} 0.916647 optimal\n", " ids growth status\n", "859 {ATPM, TKT1} 0.90584 optimal\n" ] } ], "source": [ "single = single_reaction_deletion(cobra_model)\n", "double = double_reaction_deletion(cobra_model)\n", "\n", "print(single.knockout[\"ATPM\"])\n", "print(double.knockout[{\"ATPM\", \"TKT1\"}])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This can be used to get several deletions at once and will also work for Reaction or Gene objects (depending on what you deleted) directly." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " ids growth status\n", "12 {ATPM} 0.916647 optimal\n", "44 {TKT1} 0.864759 optimal\n", "71 {PFK} 0.704037 optimal\n", " ids growth status\n", "425 {ATPM, PFK} 0.704037 optimal\n", "859 {ATPM, TKT1} 0.905840 optimal\n", "4125 {ATPM} 0.916647 optimal\n" ] } ], "source": [ "atpm = cobra_model.reactions.ATPM\n", "tkt1 = cobra_model.reactions.TKT1\n", "pfk = cobra_model.reactions.PFK\n", "\n", "print(single.knockout[atpm, tkt1, pfk])\n", "print(double.knockout[{atpm, tkt1}, {atpm, pfk}, {atpm}])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }