{ "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/stable/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": [ { "name": "stdout", "output_type": "stream", "text": [ "Scaling...\n", " A: min|aij| = 1.000e+00 max|aij| = 1.000e+00 ratio = 1.000e+00\n", "Problem data seem to be well scaled\n" ] } ], "source": [ "from cobra.io import load_model\n", "model = load_model(\"iYS1720\")\n", "\n", "for metabolite in model.metabolites:\n", " metabolite.id = f\"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", " \n", "
Metabolite identifiertest_10fthf_c
Name10-Formyltetrahydrofolate
Memory address0x7f9c91ec0a90
FormulaC20H21N7O7
Compartmentc
In 9 reaction(s)\n", " FTHFD, ULA4NFT, GARFT, AICART, TDPFRMT, MTHFC, BIOMASS_Ec_iAF1260_core_59p81M, BIOMASS_invivo, FMETTRS\n", "
" ], "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.knock_out_model_genes` function within a context. The effects of this function are reversed when exiting a context." ] }, { "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 = load_model(\"iYS1720\")\n", "PGI = model.reactions.get_by_id(\"PGI\")\n", "print(\"bounds before knockout:\", (PGI.lower_bound, PGI.upper_bound))\n", "from cobra.manipulation import knock_out_model_genes\n", "knock_out_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 = load_model(\"iYS1720\")\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": [ "/Users/uridavidakavia/PycharmProjects/cobrapy/src/cobra/core/reaction.py:810: UserWarning: Setting reaction reversibility is ignored\n", " warn(\"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 its string representation. All solvers behave the same way." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "with open('test.lp', 'w') as out:\n", " out.write(str(model.solver))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### How do I visualize my flux solutions?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please browse the [visualization packages](https://opencobra.github.io/cobrapy/tags/visualization/) on our website for the most recent list of tools." ] } ], "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": 1 }