{ "cells": [ { "cell_type": "markdown", "id": "e91e65cc", "metadata": { "id": "e91e65cc" }, "source": [ "# Removing elements using COBRApy" ] }, { "cell_type": "markdown", "id": "9f3c76d0", "metadata": { "id": "9f3c76d0" }, "source": [ "## Removal of reactions/metabolites\n", "\n", "In the following example we show how individual reactions or metabolites can be removed from the model based on their ID. More information about the function `remove_reactions` can be found [here](https://cobrapy.readthedocs.io/en/latest/autoapi/cobra/core/index.html?highlight=remove%20pathway#cobra.core.Model.remove_reactions) and for function `remove_metabolites` [here](https://cobrapy.readthedocs.io/en/latest/autoapi/cobra/core/index.html?highlight=remove%20pathway#cobra.core.Model.remove_metabolites).\n", "\n", "For demonstration purposes we add a reaction. The metabolites in this reactions\n", "are also added to the model." ] }, { "cell_type": "code", "execution_count": 1, "id": "a67246c1", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 265 }, "id": "a67246c1", "outputId": "5b0afb2e-e9fe-4acf-b13d-1c101ab18d8a" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[33mNothing was specified in argument \"genome\". Reaction \"R04382\" will not include genes. Please modify if necessary.\u001b[0m\n", "\u001b[33mReaction \"R04382_c\" is already present in the model. Skipping additions.\u001b[0m\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Reaction identifierR04382_c
Name4-(4-deoxy-alpha-D-galact-4-enuronosyl)-D-galacturonate lyase
Memory address0x750b10288470
Stoichiometry\n", "

C06118_c <=> 2.0 C04053_c

\n", "

Unsaturated digalacturonate <=> 2.0 (4S,5R)-4,5-Dihydroxy-2,6-dioxohexanoate

\n", "
GPR
Lower bound-1000
Upper bound1000
\n", " " ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from cobramod.test import textbook_kegg\n", "from cobramod import add_reactions\n", "from pathlib import Path\n", "\n", "\n", "test_model = textbook_kegg.copy()\n", "\n", "# Defining directory\n", "dir_data = Path.cwd().resolve().joinpath(\"memote-model-repository/data\")\n", " \n", "add_reactions(\n", " model=test_model,\n", " obj=[\"R04382, c\"],\n", " database=\"KEGG\",\n", " directory=dir_data,\n", ")\n", "test_model.reactions.get_by_id(\"R04382_c\")" ] }, { "cell_type": "markdown", "id": "1bf508c2", "metadata": { "id": "1bf508c2" }, "source": [ "Here, the metabolite with ID \"C04053_c\" will be removed." ] }, { "cell_type": "code", "execution_count": 2, "id": "864196fb", "metadata": { "id": "864196fb", "outputId": "96cc8e26-475d-4285-c340-75a499139926" }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Metabolite identifierC04053_c
Name(4S,5R)-4,5-Dihydroxy-2,6-dioxohexanoate
Memory address0x750b0bf5f8f0
FormulaC6H8O6
Compartmentc
In 1 reaction(s)\n", " R04382_c\n", "
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "metabolite_to_be_removed = test_model.metabolites.get_by_id(\"C04053_c\")\n", "metabolite_to_be_removed" ] }, { "cell_type": "code", "execution_count": 3, "id": "2e78c013", "metadata": { "id": "2e78c013", "outputId": "88aad778-28df-49f0-ed11-4c9f836d0cd6" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exception reporting mode: Minimal\n" ] }, { "ename": "KeyError", "evalue": "'C04053_c'", "output_type": "error", "traceback": [ "\u001b[0;31mKeyError\u001b[0m\u001b[0;31m:\u001b[0m 'C04053_c'\n" ] } ], "source": [ "%xmode Minimal\n", "test_model.remove_metabolites([metabolite_to_be_removed])\n", "test_model.metabolites.get_by_id(\"C04053_c\")\n", "%xmode Context" ] }, { "cell_type": "markdown", "id": "6c3b48af", "metadata": { "id": "6c3b48af" }, "source": [ "Now we remove a reaction. Please note that this reaction contained the previously removed metabolite, which is therefore now absent." ] }, { "cell_type": "code", "execution_count": 4, "id": "43dc2d30", "metadata": { "id": "43dc2d30", "outputId": "3e887c5f-7beb-41b4-d16a-9b1dd6ad0a8c", "scrolled": true }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Reaction identifierR04382_c
Name4-(4-deoxy-alpha-D-galact-4-enuronosyl)-D-galacturonate lyase
Memory address0x750b10288470
Stoichiometry\n", "

C06118_c <=>

\n", "

Unsaturated digalacturonate <=>

\n", "
GPR
Lower bound-1000
Upper bound1000
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reactions_to_be_removed = test_model.reactions.get_by_id(\"R04382_c\")\n", "reactions_to_be_removed" ] }, { "cell_type": "code", "execution_count": 5, "id": "c1a2b6f8", "metadata": { "id": "c1a2b6f8", "outputId": "143a6bf1-cbe6-426f-82f5-93704ccc222a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exception reporting mode: Minimal\n" ] }, { "ename": "KeyError", "evalue": "'R04382_c'", "output_type": "error", "traceback": [ "\u001b[0;31mKeyError\u001b[0m\u001b[0;31m:\u001b[0m 'R04382_c'\n" ] } ], "source": [ "%xmode Minimal\n", "test_model.remove_reactions([reactions_to_be_removed], remove_orphans=True)\n", "test_model.reactions.get_by_id(\"R04382_c\")\n", "%xmode Context" ] }, { "cell_type": "markdown", "id": "2d0a2a91", "metadata": { "id": "2d0a2a91" }, "source": [ "## Removal of pathways/groups\n", "\n", "In the following example, we show how individual reactions can be removed from the model using their ID. More information about the function `remove_reactions` can be found [here](https://cobrapy.readthedocs.io/en/latest/autoapi/cobra/core/index.html?highlight=remove%20pathway#cobra.core.Model.remove_reactions).\n", "\n", "> Please note that in Cobrapy reactions are defined as groups. Thus some of the functions in the following use the term 'group' instead of 'pathway'.\n", "\n", "For demonstration purposes, we add a pathway using CobraMod." ] }, { "cell_type": "code", "execution_count": 6, "id": "2d106fee", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 651 }, "id": "2d106fee", "outputId": "7d42e371-ed9e-4617-8ab4-bf38d08daad0", "scrolled": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[33mGene-reaction rule for reaction \"PEPDEPHOS-RXN\" set to \"OR\". Please modify it if necessary.\u001b[0m\n", "\u001b[33mGene-reaction rule for reaction \"PYRUVFORMLY-RXN\" set to \"OR\". Please modify it if necessary.\u001b[0m\n", "\u001b[33mGene-reaction rule for reaction \"FHLMULTI-RXN\" set to \"OR\". Please modify it if necessary.\u001b[0m\n", "\u001b[36mNon-zero flux test for reaction 'PEPDEPHOS_RXN_c' passed.\u001b[0m\n", "\u001b[36mReaction \"PEPDEPHOS_RXN_c\" added to group \"curated_pathway\".\u001b[0m\n", "\u001b[33mThe model cannot turnover the following metabolites ['FORMATE_c']. To overcome this, sink reactions were created to simulate their synthesis.\u001b[0m\n", "\u001b[36mNon-zero flux test for reaction 'PYRUVFORMLY_RXN_c' passed.\u001b[0m\n", "\u001b[36mReaction \"PYRUVFORMLY_RXN_c\" added to group \"curated_pathway\".\u001b[0m\n", "\u001b[36mNon-zero flux test for reaction 'FHLMULTI_RXN_c' passed.\u001b[0m\n", "\u001b[36mReaction \"FHLMULTI_RXN_c\" added to group \"curated_pathway\".\u001b[0m\n", "\u001b[33mAuxiliary sink reaction for \"SK_HYDROGEN_MOLECULE_c\" created. Consider removing it and adding the synthesis reactions for the metabolite.\u001b[0m\n", "\u001b[33mAuxiliary sink reaction for \"SK_FORMATE_c\" created. Consider removing it and adding the synthesis reactions for the metabolite.\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Number of reaction prior addition: 95\n", "Number of new | removed entities in\n", "*=====================|===================*\n", "Reactions 3 | 0 \n", "Metabolites 2 | 0 \n", "Exchange 0 | 0 \n", "Demand 0 | 0 \n", "Sinks 2 | 0 \n", "Genes 12 | 0 \n", "Groups 1 | 0 \n", "\n", "Number of reactions after addition: 100\n" ] }, { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Pathway identifiercurated_pathway
Name
Memory address0x0128690305761248
Reactions involved

PEPDEPHOS_RXN_c, FHLMULTI_RXN_c, PYRUVFORMLY_RXN_c

Genes involved

EG10803, EG10804, EG10480, EG10478, EG10477, EG10476, EG10479, EG10475, EG10285, EG10701, G7627, EG11784

Visualization attributes
  • vertical =\n", "False
  • color_negative = None
  • \n", "
  • color_positive = None
  • color_quantile =\n", "False

 

" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pathlib import Path\n", "from cobramod import add_pathway\n", "from cobramod.test import textbook_biocyc\n", "# Defining directory\n", "dir_data = Path.cwd().resolve().joinpath(\"memote-model-repository/data\")\n", "\n", "test_model = textbook_biocyc.copy()\n", "# Defining database-specific identifiers\n", "sequence = [\"PEPDEPHOS-RXN\", \"PYRUVFORMLY-RXN\", \"FHLMULTI-RXN\"]\n", " \n", "print(f'Number of reaction prior addition: {len(test_model.reactions)}')\n", " \n", "add_pathway(\n", " model=test_model,\n", " pathway=sequence,\n", " directory=dir_data,\n", " database=\"ECOLI\",\n", " compartment=\"c\",\n", " group=\"curated_pathway\"\n", ")\n", "\n", "print(f'Number of reactions after addition: {len(test_model.reactions)}')\n", "# Display in jupyter\n", "test_model.groups.get_by_id(\"curated_pathway\")" ] }, { "cell_type": "markdown", "id": "ca8d9393", "metadata": { "id": "ca8d9393" }, "source": [ "To remove a pathway completely, all of its members need to be removed first. Otherwise, they would remain in the model but would no longer be associated with the groups." ] }, { "cell_type": "code", "execution_count": 7, "id": "c1fbaeb2", "metadata": { "id": "c1fbaeb2", "outputId": "1f601dce-59d5-49fa-8784-3cfab76f6ba8", "scrolled": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Pathway identifiercurated_pathway
Name
Memory address0x0128690305761248
Reactions involved

PEPDEPHOS_RXN_c, FHLMULTI_RXN_c, PYRUVFORMLY_RXN_c

Genes involved

EG10803, EG10804, EG10480, EG10478, EG10477, EG10476, EG10479, EG10475, EG10285, EG10701, G7627, EG11784

Visualization attributes
  • vertical =\n", "False
  • color_negative = None
  • \n", "
  • color_positive = None
  • color_quantile =\n", "False

 

" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_model.groups.get_by_id(\"curated_pathway\")" ] }, { "cell_type": "code", "execution_count": 8, "id": "40be427c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Pathway identifiercurated_pathway
Name
Memory address0x0128690305761248
Reactions involved

Genes involved

Visualization attributes
  • vertical =\n", "False
  • color_negative = None
  • \n", "
  • color_positive = None
  • color_quantile =\n", "False

 

" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "members = test_model.groups.get_by_id(\"curated_pathway\").members\n", "test_model.groups.get_by_id(\"curated_pathway\").remove_members(members)\n", "\n", "test_model.groups.get_by_id(\"curated_pathway\")" ] }, { "cell_type": "markdown", "id": "8c943e8d", "metadata": { "id": "8c943e8d" }, "source": [ "The now-empty group can be removed without further effort and without leaving any elements behind." ] }, { "cell_type": "code", "execution_count": 9, "id": "beb055c2", "metadata": { "id": "beb055c2", "outputId": "526bc6e9-5b8c-4569-b676-5c1ee782d3be", "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exception reporting mode: Minimal\n" ] }, { "ename": "KeyError", "evalue": "'curated_pathway'", "output_type": "error", "traceback": [ "\u001b[0;31mKeyError\u001b[0m\u001b[0;31m:\u001b[0m 'curated_pathway'\n" ] } ], "source": [ "%xmode Minimal\n", "group_to_be_removed = test_model.groups.get_by_id(\"curated_pathway\")\n", "test_model.remove_groups([group_to_be_removed])\n", "test_model.groups.get_by_id(\"curated_pathway\")\n", "%xmode Context" ] } ], "metadata": { "colab": { "name": "delete.ipynb", "provenance": [] }, "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.12.2" } }, "nbformat": 4, "nbformat_minor": 5 }