{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating and simulating a DFBA model\n", "This tutorial demonstrates how to build and simulate a DFBA model corresponding to the growth of a single strain of _Escherichia coli_ (based on the iJR904 genome-scale model) under anaerobic conditions with glucose and xylose as limiting carbon substrates. \n", "\n", "This example can be adapted to other DFBA models by modifying the choice of genome-scale model, relevant kinetic variables, and exchange fluxes. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from os.path import dirname, join, pardir\n", "\n", "from cobra.io import read_sbml_model\n", "\n", "from dfba import DfbaModel, ExchangeFlux, KineticVariable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Dfba model\n", "Specify path for loading file containing genome-scale metabolic model as [cobra.Model](https://cobrapy.readthedocs.io/en/latest/building_model.html) object and set GLPK as LP solver of choice. After that, instantiate object of class `DfbaModel` with cobrapy model." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path_to_model = join(pardir, \"sbml-models\", \"iJR904.xml.gz\")\n", "fba_model = read_sbml_model(path_to_model)\n", "fba_model.solver = \"glpk\"\n", "dfba_model = DfbaModel(fba_model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Kinetic Variables\n", "Instantiate kinetic variables to appear in model. Default initial conditions are $0.0$, but can be set here if wanted (e.g., Oxygen). The last command adds kinetic variables to the model." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "X = KineticVariable(\"Biomass\")\n", "Gluc = KineticVariable(\"Glucose\")\n", "Xyl = KineticVariable(\"Xylose\")\n", "Oxy = KineticVariable(\"Oxygen\", initial_condition=0.24)\n", "Eth = KineticVariable(\"Ethanol\")\n", "\n", "dfba_model.add_kinetic_variables([X, Gluc, Xyl, Oxy, Eth])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Exchange fluxes\n", "Instantiate exchange fluxes to appear in model, with ids corresponding to exchange reactions of the cobrapy FBA model. The last command adds exchange fluxes to the model." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "mu = ExchangeFlux(\"BiomassEcoli\")\n", "v_G = ExchangeFlux(\"EX_glc(e)\")\n", "v_Z = ExchangeFlux(\"EX_xyl_D(e)\")\n", "v_O = ExchangeFlux(\"EX_o2(e)\")\n", "v_E = ExchangeFlux(\"EX_etoh(e)\")\n", "\n", "dfba_model.add_exchange_fluxes([mu, v_G, v_Z, v_O, v_E])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Rhs expressions \n", "Provide symbolic expression for calculating the time derivative of each kinetic variable currently in the model." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "dfba_model.add_rhs_expression(\"Biomass\", mu * X)\n", "dfba_model.add_rhs_expression(\"Glucose\", v_G * 180.1559 * X / 1000.0)\n", "dfba_model.add_rhs_expression(\"Xylose\", v_Z * 150.13 * X / 1000.0)\n", "dfba_model.add_rhs_expression(\"Oxygen\", v_O * 16.0 * X / 1000.0)\n", "dfba_model.add_rhs_expression(\"Ethanol\", v_E * 46.06844 * X / 1000.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Lower/upper bound expressions \n", "Add symbolic expressions for calculating lower/upper bounds of selected exchange fluxes\n", "currently in the model. Here convention is that both lower and upper bound expressions have \n", "positive signs, whereas lower bounds values are typically negative in cobrapy. \n", "\n", "In many applications, vector components (e.g., concentrations)\n", "in the true solution are always positive or non-negative, though at times very\n", "small. In the numerical solution, however, small negative (hence unphysical)\n", "values can then occur. To prevent these from interfering with the simulation,\n", "the user can supply a symbolic expression that must be non-negative for\n", "correct evaluation of lower/upper bounds." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "dfba_model.add_exchange_flux_lb(\n", " \"EX_glc(e)\", 10.5 * (Gluc / (0.0027 + Gluc)) * (1 / (1 + Eth / 20.0)), Gluc\n", ")\n", "dfba_model.add_exchange_flux_lb(\"EX_o2(e)\", 15.0 * (Oxy / (0.024 + Oxy)), Oxy)\n", "dfba_model.add_exchange_flux_lb(\n", " \"EX_xyl_D(e)\",\n", " 6.0\n", " * (Xyl / (0.0165 + Xyl))\n", " * (1 / (1 + Eth / 20.0))\n", " * (1 / (1 + Gluc / 0.005)),\n", " Xyl,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6. Add initial conditions to the model and launch the simulation\n", "Initial values for each kinetic variable are provided in dictionary form. \n", "\n", "The model is simulated using the `simulate` method. This simulation covers the interval $[0.0, 25.0]$ hours, with results stored every $0.1$ hours. Results (trajectories of kinetic variables) will be returned as [pandas.DataFrame](https://pandas.pydata.org/). Optionally, the user can also provide a list of reaction ids whose flux trajectories will also be returned as a separate [pandas.DataFrame](https://pandas.pydata.org/), in this case three exchange fluxes in the model." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "dfba_model.add_initial_conditions(\n", " {\n", " \"Biomass\": 0.03,\n", " \"Glucose\": 15.5,\n", " \"Xylose\": 8.0,\n", " \"Oxygen\": 0.0,\n", " \"Ethanol\": 0.0,\n", " }\n", ")\n", "concentrations, trajectories = dfba_model.simulate(0.0, 25.0, 0.1, [\"EX_glc(e)\", \"EX_xyl_D(e)\", \"EX_etoh(e)\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7. Plotting the results\n", "