{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Getting Started\n", "\n", "To begin with, cobrapy comes with bundled models for _Salmonella_ and _E. coli_, as well as a \"textbook\" model of _E. coli_ core metabolism. To load a test model, type" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from __future__ import print_function\n", "import cobra.test\n", "\n", "# \"ecoli\" and \"salmonella\" are also valid arguments\n", "model = cobra.test.create_test_model(\"textbook\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The reactions, metabolites, and genes attributes of the cobrapy model are a special type of list called a DictList, and each one is made up of Reaction, Metabolite and Gene objects respectively." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "95\n", "72\n", "137\n" ] } ], "source": [ "print(len(model.reactions))\n", "print(len(model.metabolites))\n", "print(len(model.genes))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Just like a regular list, objects in the DictList can be retrived by index. For example, to get the 30th reaction in the model (at index 29 because of [0-indexing](https://en.wikipedia.org/wiki/Zero-based_numbering)):" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.reactions[29]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Addictionally, items can be retrived by their id using the get_by_id() function. For example, to get the cytosolic atp metabolite object (the id is \"atp_c\"), we can do the following:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.metabolites.get_by_id(\"atp_c\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As an added bonus, users with an interactive shell such as IPython will be able to tab-complete to list elements inside a list. While this is not recommended behavior for most code because of the possibility for characters like \"-\" inside ids, this is very useful while in an interactive prompt:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-10.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.reactions.EX_glc__D_e.lower_bound" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reactions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will consider the reaction glucose 6-phosphate isomerase, which interconverts glucose 6-phosphate and fructose 6-phosphate. The reaction id for this reaction in our test model is PGI." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pgi = model.reactions.get_by_id(\"PGI\")\n", "pgi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can view the full name and reaction catalyzed as strings" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "glucose-6-phosphate isomerase\n", "g6p_c <=> f6p_c\n" ] } ], "source": [ "print(pgi.name)\n", "print(pgi.reaction)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also view reaction upper and lower bounds. Because the pgi.lower_bound < 0, and pgi.upper_bound > 0, pgi is reversible" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-1000.0 < pgi < 1000.0\n", "True\n" ] } ], "source": [ "print(pgi.lower_bound, \"< pgi <\", pgi.upper_bound)\n", "print(pgi.reversibility)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also ensure the reaction is mass balanced. This function will return elements which violate mass balance. If it comes back empty, then the reaction is mass balanced." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pgi.check_mass_balance()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to add a metabolite, we pass in a dict with the metabolite object and its coefficient" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'g6p_c + h_c <=> f6p_c'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pgi.add_metabolites({model.metabolites.get_by_id(\"h_c\"): -1})\n", "pgi.reaction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The reaction is no longer mass balanced" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'H': -1.0, 'charge': -1.0}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pgi.check_mass_balance()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can remove the metabolite, and the reaction will be balanced once again." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "g6p_c <=> f6p_c\n", "{}\n" ] } ], "source": [ "pgi.pop(model.metabolites.get_by_id(\"h_c\"))\n", "print(pgi.reaction)\n", "print(pgi.check_mass_balance())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is also possible to build the reaction from a string. However, care must be taken when doing this to ensure reaction id's match those in the model. The direction of the arrow is also used to update the upper and lower bounds." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "unknown metabolite 'green_eggs' created\n", "unknown metabolite 'ham' created\n" ] } ], "source": [ "pgi.reaction = \"g6p_c --> f6p_c + h_c + green_eggs + ham\"" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'g6p_c --> f6p_c + green_eggs + h_c + ham'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pgi.reaction" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'g6p_c <=> f6p_c'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pgi.reaction = \"g6p_c <=> f6p_c\"\n", "pgi.reaction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Metabolites" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will consider cytosolic atp as our metabolite, which has the id atp_c in our test model." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "atp = model.metabolites.get_by_id(\"atp_c\")\n", "atp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can print out the metabolite name and compartment (cytosol in this case)." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ATP\n", "c\n" ] } ], "source": [ "print(atp.name)\n", "print(atp.compartment)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see that ATP is a charged molecule in our model." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-4" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "atp.charge" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see the chemical formula for the metabolite as well." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C10H12N5O13P3\n" ] } ], "source": [ "print(atp.formula)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The reactions attribute gives a frozenset of all reactions using the given metabolite. We can use this to count the number of reactions which use atp." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(atp.reactions)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A metabolite like glucose 6-phosphate will participate in fewer reactions." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "frozenset({,\n", " ,\n", " ,\n", " })" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.metabolites.get_by_id(\"g6p_c\").reactions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Genes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The gene_reaction_rule is a boolean representation of the gene requirements for this reaction to be active as described in [Schellenberger et al 2011 Nature Protocols 6(9):1290-307](http://dx.doi.org/doi:10.1038/nprot.2011.308).\n", "\n", "The GPR is stored as the gene_reaction_rule for a Reaction object as a string." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'b4025'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gpr = pgi.gene_reaction_rule\n", "gpr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Corresponding gene objects also exist. These objects are tracked by the reactions itself, as well as by the model" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "frozenset({})" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pgi.genes" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pgi_gene = model.genes.get_by_id(\"b4025\")\n", "pgi_gene" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each gene keeps track of the reactions it catalyzes" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "frozenset({})" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pgi_gene.reactions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Altering the gene_reaction_rule will create new gene objects if necessary and update all relationships." ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "frozenset({, })" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pgi.gene_reaction_rule = \"(spam or eggs)\"\n", "pgi.genes" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "frozenset()" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pgi_gene.reactions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Newly created genes are also added to the model" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.genes.get_by_id(\"spam\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The delete_model_genes function will evaluate the gpr and set the upper and lower bounds to 0 if the reaction is knocked out. This function can preserve existing deletions or reset them using the cumulative_deletions flag." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "after 1 KO: -1000 < flux_PGI < 1000\n", "after 2 KO: 0 < flux_PGI < 0\n" ] } ], "source": [ "cobra.manipulation.delete_model_genes(model, [\"spam\"],\n", " cumulative_deletions=True)\n", "print(\"after 1 KO: %4d < flux_PGI < %4d\" %\n", " (pgi.lower_bound, pgi.upper_bound))\n", "\n", "cobra.manipulation.delete_model_genes(model, [\"eggs\"],\n", " cumulative_deletions=True)\n", "print(\"after 2 KO: %4d < flux_PGI < %4d\" %\n", " (pgi.lower_bound, pgi.upper_bound))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The undelete_model_genes can be used to reset a gene deletion" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-1000 < pgi < 1000\n" ] } ], "source": [ "cobra.manipulation.undelete_model_genes(model)\n", "print(pgi.lower_bound, \"< pgi <\", pgi.upper_bound)" ] } ], "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.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }