{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# FAQ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This document will address frequently asked questions not addressed in other pages of the documentation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### How do I install cobrapy?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please see the [INSTALL.rst](https://github.com/opencobra/cobrapy/blob/master/INSTALL.rst) file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### How do I cite cobrapy?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please cite the 2013 publication: [10.1186/1752-0509-7-74](http://dx.doi.org/doi:10.1186/1752-0509-7-74)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### How do I rename reactions or metabolites?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "TL;DR Use Model.repair afterwards\n", "\n", "When renaming metabolites or reactions, there are issues because cobra indexes based off of ID's, which can cause errors. For example:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "KeyError('test_dcaACP_c',)\n" ] } ], "source": [ "from __future__ import print_function\n", "import cobra.test\n", "model = cobra.test.create_test_model()\n", "\n", "for metabolite in model.metabolites:\n", " metabolite.id = \"test_\" + metabolite.id\n", "\n", "try:\n", " model.metabolites.get_by_id(model.metabolites[0].id)\n", "except KeyError as e:\n", " print(repr(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Model.repair function will rebuild the necessary indexes" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.repair()\n", "model.metabolites.get_by_id(model.metabolites[0].id)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### How do I delete a gene?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That depends on what precisely you mean by delete a gene.\n", "\n", "If you want to simulate the model with a gene knockout, use the cobra.maniupulation.delete_model_genes function. The effects of this function are reversed by cobra.manipulation.undelete_model_genes." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bounds before knockout: (-1000.0, 1000.0)\n", "bounds after knockouts (0.0, 0.0)\n" ] } ], "source": [ "model = cobra.test.create_test_model()\n", "PGI = model.reactions.get_by_id(\"PGI\")\n", "print(\"bounds before knockout:\", (PGI.lower_bound, PGI.upper_bound))\n", "cobra.manipulation.delete_model_genes(model, [\"STM4221\"])\n", "print(\"bounds after knockouts\", (PGI.lower_bound, PGI.upper_bound))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to actually remove all traces of a gene from a model, this is more difficult because this will require changing all the gene_reaction_rule strings for reactions involving the gene." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### How do I change the reversibility of a Reaction?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reaction.reversibility is a property in cobra which is computed when it is requested from the lower and upper bounds." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model = cobra.test.create_test_model()\n", "model.reactions.get_by_id(\"PGI\").reversibility" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Trying to set it directly will result in an error or warning: " ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "cobra/core/Reaction.py:192 \u001b[1;31mUserWarning\u001b[0m: Setting reaction reversibility is ignored\n" ] } ], "source": [ "try:\n", " model.reactions.get_by_id(\"PGI\").reversibility = False\n", "except Exception as e:\n", " print(repr(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The way to change the reversibility is to change the bounds to make the reaction irreversible." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.reactions.get_by_id(\"PGI\").lower_bound = 10\n", "model.reactions.get_by_id(\"PGI\").reversibility" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### How do I generate an LP file from a COBRA model?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While the cobrapy does not include python code to support this feature directly, many of the bundled solvers have this capability. Create the problem with one of these solvers, and use its appropriate function.\n", "\n", "Please note that unlike the LP file format, the MPS file format does not specify objective direction and is always a minimzation. Some (but not all) solvers will rewrite the maximization as a minimzation." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "ename": "AttributeError", "evalue": "'module' object has no attribute 'gurobi_solver'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mglp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"test.mps\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# will not rewrite objective\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;31m# gurobi\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mgurobi_problem\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcobra\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msolvers\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgurobi_solver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcreate_problem\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 8\u001b[0m \u001b[0mgurobi_problem\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"test.lp\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[0mgurobi_problem\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"test.mps\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# rewrites objective\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mAttributeError\u001b[0m: 'module' object has no attribute 'gurobi_solver'" ] } ], "source": [ "model = cobra.test.create_test_model()\n", "# glpk through cglpk\n", "glp = cobra.solvers.cglpk.create_problem(model)\n", "glp.write(\"test.lp\")\n", "glp.write(\"test.mps\") # will not rewrite objective\n", "# gurobi\n", "gurobi_problem = cobra.solvers.gurobi_solver.create_problem(model)\n", "gurobi_problem.write(\"test.lp\")\n", "gurobi_problem.write(\"test.mps\") # rewrites objective\n", "# cplex\n", "cplex_problem = cobra.solvers.cplex_solver.create_problem(model)\n", "cplex_problem.write(\"test.lp\")\n", "cplex_problem.write(\"test.mps\") # rewrites objective" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### How do I visualize my flux solutions?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "cobrapy works well with the [escher](https://escher.github.io/) package, which is well suited to this purpose. Consult the [escher documentation](https://escher.readthedocs.org/en/latest/) for examples." ] } ], "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 }