Skip to content

Instantly share code, notes, and snippets.

@BjornFJohansson
Created October 3, 2023 16:26
Show Gist options
  • Save BjornFJohansson/8a7946a99242f2c11c73cd7364d2ca48 to your computer and use it in GitHub Desktop.
Save BjornFJohansson/8a7946a99242f2c11c73cd7364d2ca48 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "3f9f1753-3b9e-4fc6-bf41-644afdcdafdc",
"metadata": {},
"source": [
"![](banana-oil-500x500.webp)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ac8dccf1",
"metadata": {
"lines_to_next_cell": 0
},
"outputs": [
{
"data": {
"text/plain": [
"'0.27.0'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import cobra\n",
"cobra.__version__ # '0.27.0'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9715eff6",
"metadata": {
"lines_to_next_cell": 0
},
"outputs": [],
"source": [
"# load_json_model, load_matlab_model, read_sbml_model\n",
"model = cobra.io.read_sbml_model(\"yeast-GEM-8.7.0/model/yeast-GEM.xml\")"
]
},
{
"cell_type": "markdown",
"id": "85fdff2a-8bf8-49b4-beb5-2b96b4c104b0",
"metadata": {},
"source": [
"![](yeastGEMgithub.png)\n",
"\n",
"[yeast-GEM](https://github.com/SysBioChalmers/yeast-GEM#yeast-gem-the-consensus-genome-scale-metabolic-model-of-saccharomyces-cerevisiae)\n",
"\n",
"Downloaded [zip file](https://github.com/SysBioChalmers/yeast-GEM/releases/tag/v8.7.0)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "17c61cb9-9274-4aab-b8e5-2323dc0dc455",
"metadata": {
"lines_to_next_cell": 0
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4131\n"
]
}
],
"source": [
"print(len(model.reactions))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "680830c2-bd60-499a-84b4-d5c7da0c133a",
"metadata": {
"lines_to_next_cell": 0
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2806\n"
]
}
],
"source": [
"print(len(model.metabolites))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "17449281-75d3-43a4-9f24-e967142ac4c6",
"metadata": {},
"outputs": [],
"source": [
"medium = model.medium"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "606e6f53-c314-4632-ac44-d0b04767bf03",
"metadata": {
"lines_to_next_cell": 0
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"r_1654 ammonium exchange = 1000.0\n",
"r_1714 D-glucose exchange = 1.0\n",
"r_1832 H+ exchange = 1000.0\n",
"r_1861 iron(2+) exchange = 1000.0\n",
"r_1992 oxygen exchange = 1000.0\n",
"r_2005 phosphate exchange = 1000.0\n",
"r_2020 potassium exchange = 1000.0\n",
"r_2049 sodium exchange = 1000.0\n",
"r_2060 sulphate exchange = 1000.0\n",
"r_2100 water exchange = 1000.0\n",
"r_4593 chloride exchange = 1000.0\n",
"r_4594 Cu2(+) exchange = 1000.0\n",
"r_4595 Mn(2+) exchange = 1000.0\n",
"r_4596 Zn(2+) exchange = 1000.0\n",
"r_4597 Mg(2+) exchange = 1000.0\n",
"r_4600 Ca(2+) exchange = 1000.0\n"
]
}
],
"source": [
"for key in medium:\n",
" print(key, model.reactions.get_by_id(key).name, \" = \" ,medium[key])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "59b2d80d-3592-4fbd-922b-6549bb222bf9",
"metadata": {},
"outputs": [],
"source": [
"del medium[\"r_1714\"]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "de4c7790-1c01-497e-9a41-82536762f81a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"r_1024 sucrose hydrolyzing enxyme frozenset({<Gene YIL162W at 0x7fe9cb3ab0d0>})\n",
"r_2058 sucrose exchange frozenset()\n",
"r_4317 sucrose glucohydrolase frozenset({<Gene YOL157C at 0x7fe9cb201690>})\n"
]
}
],
"source": [
"for i, r in enumerate(model.reactions):\n",
" if \"sucrose\" in r.name.lower():\n",
" print(r.id, r.name, r.genes)"
]
},
{
"cell_type": "markdown",
"id": "1b7f0844-0ecf-48de-98de-935d4edb3990",
"metadata": {},
"source": [
"- r_2058 sucrose exchange"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "771276b6-50f7-4f84-9b9c-deea4d3a3448",
"metadata": {},
"outputs": [],
"source": [
"medium[\"r_2058\"] = 1.0"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "76c0f3e0-28e9-4289-9990-2b1e0da36ad9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"r_1654 ammonium exchange = 1000.0\n",
"r_1832 H+ exchange = 1000.0\n",
"r_1861 iron(2+) exchange = 1000.0\n",
"r_1992 oxygen exchange = 1000.0\n",
"r_2005 phosphate exchange = 1000.0\n",
"r_2020 potassium exchange = 1000.0\n",
"r_2049 sodium exchange = 1000.0\n",
"r_2060 sulphate exchange = 1000.0\n",
"r_2100 water exchange = 1000.0\n",
"r_4593 chloride exchange = 1000.0\n",
"r_4594 Cu2(+) exchange = 1000.0\n",
"r_4595 Mn(2+) exchange = 1000.0\n",
"r_4596 Zn(2+) exchange = 1000.0\n",
"r_4597 Mg(2+) exchange = 1000.0\n",
"r_4600 Ca(2+) exchange = 1000.0\n",
"r_2058 sucrose exchange = 1.0\n"
]
}
],
"source": [
"for key in medium:\n",
" print(key, model.reactions.get_by_id(key).name, \" = \" , medium[key])"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "21959331-a0b4-446b-a531-464656d8f547",
"metadata": {},
"outputs": [],
"source": [
"model.medium = medium"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "1b1867b7",
"metadata": {
"lines_to_next_cell": 0
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"r_0160 alcohol acetyltransferase (isoamyl alcohol) frozenset({<Gene YOR377W at 0x7fe9cb20f010>, <Gene YGR177C at 0x7fe9cb3966d0>})\n",
"r_0179 aldehyde dehydrogenase (isoamyl alcohol, NAD) frozenset({<Gene YOL086C at 0x7fe9cb200510>, <Gene YDL168W at 0x7fe9cb35b010>, <Gene YBR145W at 0x7fe9cb34f290>})\n",
"r_0180 aldehyde dehydrogenase (isoamyl alcohol, NAD) frozenset({<Gene YGL256W at 0x7fe9cb38f710>, <Gene YMR083W at 0x7fe9cb3e1e10>})\n",
"r_0181 aldehyde dehydrogenase (isoamyl alcohol, NADP) frozenset({<Gene YCR105W at 0x7fe9cb358f90>, <Gene YMR318C at 0x7fe9cb3f17d0>})\n",
"r_0656 isoamyl acetate-hydrolyzing esterase frozenset({<Gene YOR126C at 0x7fe9cb202e50>})\n",
"r_1180 isoamyl acetate transport frozenset()\n",
"r_1862 isoamyl acetate exchange frozenset()\n",
"r_1863 isoamyl alcohol transport frozenset()\n",
"r_1864 isoamyl alcohol transport frozenset()\n",
"r_1865 isoamylol exchange frozenset()\n"
]
}
],
"source": [
"for i, r in enumerate(model.reactions):\n",
" if \"isoamyl\" in r.name.lower():\n",
" print(r.id, r.name, r.genes)"
]
},
{
"cell_type": "markdown",
"id": "da12f5c6-d116-4791-a03b-f135033d7aab",
"metadata": {
"lines_to_next_cell": 0
},
"source": [
"- r_1862 isoamyl acetate exchange"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "8a93de3a-1f01-42d4-9769-73669ee90c99",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <table>\n",
" <tr>\n",
" <td><strong>Reaction identifier</strong></td><td>r_1862</td>\n",
" </tr><tr>\n",
" <td><strong>Name</strong></td><td>isoamyl acetate exchange</td>\n",
" </tr><tr>\n",
" <td><strong>Memory address</strong></td>\n",
" <td>0x7fe9ca9642d0</td>\n",
" </tr><tr>\n",
" <td><strong>Stoichiometry</strong></td>\n",
" <td>\n",
" <p style='text-align:right'>s_0928 --></p>\n",
" <p style='text-align:right'>isoamyl acetate --></p>\n",
" </td>\n",
" </tr><tr>\n",
" <td><strong>GPR</strong></td><td></td>\n",
" </tr><tr>\n",
" <td><strong>Lower bound</strong></td><td>-0.0</td>\n",
" </tr><tr>\n",
" <td><strong>Upper bound</strong></td><td>1000.0</td>\n",
" </tr>\n",
" </table>\n",
" "
],
"text/plain": [
"<Reaction r_1862 at 0x7fe9ca9642d0>"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r = model.reactions.get_by_id(\"r_1862\")\n",
"r"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "823bc0a5-031e-4cd6-b41b-3b510ab3b52c",
"metadata": {
"lines_to_next_cell": 0
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Maximize\n",
"1.0*r_2111 - 1.0*r_2111_reverse_58b69\n"
]
}
],
"source": [
"# We can investigate the model objective\n",
"print(model.objective) # biomass?"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "a9ad09e9-addb-4574-aa7a-d95e7ed38806",
"metadata": {
"lines_to_next_cell": 0
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"growth\n"
]
}
],
"source": [
"objr = model.reactions.get_by_id(\"r_2111\")\n",
"print(objr.name)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "783e5234",
"metadata": {
"lines_to_next_cell": 0
},
"outputs": [],
"source": [
"model.objective = r"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "7cb3abfd-6695-462b-a9de-7657e2c215c2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Maximize\n",
"1.0*r_1862 - 1.0*r_1862_reverse_0b3f6\n"
]
}
],
"source": [
"print(model.objective)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "2bddd974-163f-48d2-a555-8197b708b167",
"metadata": {},
"outputs": [],
"source": [
"# Optimise the model using fba\n",
"fba_solution = model.optimize()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "81865c6c-10df-4b6e-af56-9e65084fd3c7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'optimal'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fba_solution.status"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "13e56e54-9f09-4dd6-901a-11bec62f64cd",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<strong><em>Optimal</em> solution with objective value 1.124</strong><br><div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>fluxes</th>\n",
" <th>reduced_costs</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>r_0001</th>\n",
" <td>0.0</td>\n",
" <td>0.000000e+00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>r_0002</th>\n",
" <td>0.0</td>\n",
" <td>5.724587e-17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>r_0003</th>\n",
" <td>0.0</td>\n",
" <td>0.000000e+00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>r_0004</th>\n",
" <td>0.0</td>\n",
" <td>1.734723e-18</td>\n",
" </tr>\n",
" <tr>\n",
" <th>r_0005</th>\n",
" <td>0.0</td>\n",
" <td>-1.110223e-16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>r_4774</th>\n",
" <td>0.0</td>\n",
" <td>1.110223e-16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>r_4775</th>\n",
" <td>0.0</td>\n",
" <td>-1.035400e-02</td>\n",
" </tr>\n",
" <tr>\n",
" <th>r_4776</th>\n",
" <td>0.0</td>\n",
" <td>1.110223e-16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>r_4777</th>\n",
" <td>0.0</td>\n",
" <td>-0.000000e+00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>r_4778</th>\n",
" <td>0.0</td>\n",
" <td>-0.000000e+00</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>4131 rows × 2 columns</p>\n",
"</div>"
],
"text/plain": [
"<Solution 1.124 at 0x7fe9ca84c390>"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fba_solution"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "5dbc5eb1-4655-4862-a789-6222d0d0fa48",
"metadata": {},
"outputs": [],
"source": [
"sm = model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "0ab7785d-3e96-4600-8e55-82c764a5f606",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<h3>Objective</h3><p>1.0 r_1862 = 1.1236785703192131</p><h4>Uptake</h4><table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>Metabolite</th>\n",
" <th>Reaction</th>\n",
" <th>Flux</th>\n",
" <th>C-Number</th>\n",
" <th>C-Flux</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>s_1277</td>\n",
" <td>r_1992</td>\n",
" <td>1.325</td>\n",
" <td>0</td>\n",
" <td>0.00%</td>\n",
" </tr>\n",
" <tr>\n",
" <td>s_1466</td>\n",
" <td>r_2058</td>\n",
" <td>1</td>\n",
" <td>12</td>\n",
" <td>100.00%</td>\n",
" </tr>\n",
" </tbody>\n",
"</table><h4>Secretion</h4><table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>Metabolite</th>\n",
" <th>Reaction</th>\n",
" <th>Flux</th>\n",
" <th>C-Number</th>\n",
" <th>C-Flux</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>s_0458</td>\n",
" <td>r_1672</td>\n",
" <td>-4.134</td>\n",
" <td>1</td>\n",
" <td>34.45%</td>\n",
" </tr>\n",
" <tr>\n",
" <td>s_0928</td>\n",
" <td>r_1862</td>\n",
" <td>-1.124</td>\n",
" <td>7</td>\n",
" <td>65.55%</td>\n",
" </tr>\n",
" <tr>\n",
" <td>s_0805</td>\n",
" <td>r_2100</td>\n",
" <td>-3.134</td>\n",
" <td>0</td>\n",
" <td>0.00%</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>"
],
"text/plain": [
"<cobra.summary.model_summary.ModelSummary at 0x7fe9cb044b90>"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sm"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "30797898-f419-48c8-ac38-89957f884203",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"s_1277 oxygen\n",
"s_1466 sucrose\n"
]
}
],
"source": [
"for id in sm.uptake_flux['metabolite'].values:\n",
" print(id, model.metabolites.get_by_id(id).name)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "e5c67398-afde-41cb-a561-ec6ad74162a9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"r_1672 carbon dioxide\n",
"r_1862 isoamyl acetate\n",
"r_2100 H2O\n"
]
}
],
"source": [
"for id, row in sm.secretion_flux.iterrows():\n",
" if row[\"flux\"] != 0.0:\n",
" print(id, model.metabolites.get_by_id(row[\"metabolite\"]).name)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "b26dd29b-09d8-44e2-a03f-e50413914554",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.0"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fba_solution.get_primal_by_id(\"r_2111\") # Growth!"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "2578ce39-b1d4-4d99-98b0-6828f65b4a97",
"metadata": {},
"outputs": [],
"source": [
"molar_yeld = (-1. * fba_solution.get_primal_by_id(\"r_1862\")/fba_solution.get_primal_by_id(\"r_2058\") )"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "eede0666-eee8-4152-a23e-5f70b7761336",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.1236785703192156\n"
]
}
],
"source": [
"print(molar_yeld)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "baaff831-1511-4ad5-9784-ab5a5199206c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"342.29648"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mw_suc = model.metabolites.get_by_id(\"s_1466\").formula_weight\n",
"mw_suc"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "5fb11df1-b381-4466-8d6c-bf52203c5863",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"130.18486"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mw_banana_oil = model.metabolites.get_by_id(\"s_0928\").formula_weight\n",
"mw_banana_oil"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "ec0cd64b-69d4-4a8e-a582-4aadf4252778",
"metadata": {},
"outputs": [],
"source": [
"mass_yield = molar_yeld * (mw_banana_oil/mw_suc)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "2e08bdb3-cc93-4959-9d80-173071cd93d1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.4273661749662375 g/g\n"
]
}
],
"source": [
"print( str(mass_yield) +\" g/g\")"
]
}
],
"metadata": {
"jupytext": {
"cell_metadata_filter": "-all",
"encoding": "# -*- coding: utf-8 -*-",
"executable": "/usr/bin/env python3",
"main_language": "python",
"notebook_metadata_filter": "-all"
},
"kernelspec": {
"display_name": "bjorn311",
"language": "python",
"name": "bjorn311"
},
"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.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment