{ "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": {}, "outputs": [], "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": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Metabolite identifiertest_dcaACP_c
NameDecanoyl-ACP-n-C100ACP
Memory address0x0110f09630
FormulaC21H39N2O8PRS
" ], "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.manipulation.delete_model_genes` function. The effects of this function are reversed by `cobra.manipulation.undelete_model_genes`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "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": {}, "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": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "cobra/core/reaction.py:501 \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": {}, "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": [ "### For optlang based solvers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With optlang solvers, the LP formulation of a model is obtained by it's string representation. All solvers behave the same way." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "with open('test.lp', 'w') as out:\n", " out.write(str(model.solver))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### For cobrapy's internal solvers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With the internal solvers, we first create the problem and use functions bundled with the solver. \n", "\n", "Please note that unlike the LP file format, the MPS file format does not specify objective direction and is always a minimization. Some (but not all) solvers will rewrite the maximization as a minimization." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "model = cobra.test.create_test_model()\n", "# glpk through cglpk\n", "glpk = cobra.solvers.cglpk.create_problem(model)\n", "glpk.write(\"test.lp\")\n", "glpk.write(\"test.mps\") # will not rewrite objective\n", "# cplex\n", "cplex = cobra.solvers.cplex_solver.create_problem(model)\n", "cplex.write(\"test.lp\")\n", "cplex.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.6.0" } }, "nbformat": 4, "nbformat_minor": 1 }