{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SPARCED Model Simulation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is how to simulate an existing SPARCED model, consisting of an SBML file and scripts for random sampling of mRNA counts for stochastic single-cell simulations. Below is an illustration of how a single-cell is simulated, with required inputs and arguments." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## First, import the required packages" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import libsbml\n", "import importlib\n", "import amici\n", "import amici.plotting\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "from datetime import datetime\n", "import scipy.stats\n", "import argparse\n", "import sys\n", "import os\n", "sys.path.append(os.getcwd()[0:os.getcwd().rfind('/')]+'/bin')\n", "from modules.RunSPARCED import RunSPARCED" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define the model SBML file name" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# SBML model we want to import\n", "sbml_file = 'SPARCED.xml'\n", "model_name = sbml_file[0:-4]\n", "# Directory to which the generated model code was written by AMICI during model creation/compilation\n", "model_output_dir = model_name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The required user input\n", "\n", "Users should define if the simulation should be \"deterministic for an average-cell\" or \"stochastic single-cell\". Please refer to our manuscript for details and explanitions. In short, a \"deterministic\" run assumes an average cell response and does not randomly sample gene activation/inactivation and mRNA birth/death events. All simulations are deterministic. For a \"stochastic\" run, these events are assumed as Poission processes and each simulation represents a single-cell response. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The flag for simulation type: deterministic=1, stochastic(hybrid)=0\n", "flagD = 1\n", "# The simulation time in hours\n", "th = 1\n", "# Input ligand concentrations (in order): EGF, Her, HGF, PDGF, FGF, IGF, INS\n", "STIMligs = [100.0,100.0,100.0,100.0,100.0,100.0,1721.0] # in nM, in extracellular volume\n", "# File name prior for saving the simulation trajectory and gene states\n", "nmoutfile = 'GrowthStim_'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Other required inputs\n", "\n", "Below parameters are not usually changed. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ts = 30 # The time frame at which stochastic gene module and deterministic SBML module exhange/update information\n", "cellNumber = 0 # used for filename to save" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Species initial conditions \n", "\n", "We read-in the \"Species\" input file values here to set the initial conditions for simulations. The ligand concentrations are updated based on the user input above." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sys.path.insert(0, os.path.abspath(model_output_dir))\n", "species_sheet = np.array([np.array(line.strip().split(\"\\t\")) for line in open('Species.txt', encoding='latin-1')])\n", "\n", "species_initializations = []\n", "for row in species_sheet[1:]:\n", " species_initializations.append(float(row[2]))\n", "species_initializations = np.array(species_initializations)\n", "species_initializations[np.argwhere(species_initializations <= 1e-6)] = 0.0\n", "species_initializations[155:162] = STIMligs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Model import\n", "\n", "The existing model SBML is imported by AMICI and \"solver\"arguments are set." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model_module = importlib.import_module(model_name)\n", "model = model_module.getModel()\n", "solver = model.getSolver() # Create solver instance\n", "solver.setMaxSteps = 1e10 \n", "model.setTimepoints(np.linspace(0,ts,2)) # set timepoints" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Model simulation\n", "\n", "The simulation step. The `RunSPARCED.py` function is called to simulate the model with the given user input. In short, the `RunSPARCED` function simulates the SBML module deterministically for 30 seconds, updates the transcriptional regulators for mRNA regulation, the stochastic sampling of gene activation/inactivations and mRNA birth/death events occur, and the new mRNA levels are updated for the next 30sec deterministic simulation. For more details, please refer to our manuscript." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xoutS_all, xoutG_all, tout_all = RunSPARCED(flagD,th,species_initializations,[],sbml_file,model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Save the output\n", "\n", "The simulation trajectory and active/inactive genes at every 30sec are written respectively in \"nmoutfile_S\" and \"nmoutfile_G\" files." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# saves output\n", "columnsS = [ele for ele in model.getStateIds()]\n", "columnsG = [x for n, x in enumerate(columnsS) if 'm_' in x]\n", "columnsG = columnsG[1:]\n", "resa = [sub.replace('m_', 'ag_') for sub in columnsG]\n", "resi = [sub.replace('m_', 'ig_') for sub in columnsG]\n", "columnsG2 = np.concatenate((resa, resi), axis=None)\n", "condsSDF = pd.DataFrame(data=xoutS_all,columns=columnsS)\n", "condsSDF.to_csv(nmoutfile+'S_'+str(cellNumber)+'.txt',sep=\"\\t\") \n", "condsSDF = None\n", "condsGDF = pd.DataFrame(data=xoutG_all,columns=columnsG2)\n", "condsGDF.to_csv(nmoutfile+'G_'+str(cellNumber)+'.txt',sep=\"\\t\") \n", "condsGDF = None\n", "cellNumber+=1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plot examples" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure(figsize=(8, 2), dpi=300, facecolor='w', edgecolor='k')\n", "tt = tout_all/3600.0\n", "\n", "ax1 = fig.add_subplot(121)\n", "plt.plot(tt, xoutS_all[:,717], 'r', linewidth=3, markersize=12, label='free ppERK')\n", "plt.plot(tt, xoutS_all[:,696], 'b', linewidth=3, markersize=12, label='free ppAKT')\n", "plt.xlim([-1,th+1])\n", "plt.legend()\n", "ax1.set_ylabel('Concentration (nM)')\n", "ax1.set_xlabel('Time (hr)')\n", "plt.xticks(np.arange(0, th+1, step=4)) # Set label locations.\n", "\n", "ax1 = fig.add_subplot(122)\n", "plt.plot(tt, xoutS_all[:,715], 'r--', linewidth=3, markersize=12, label='free total ERK')\n", "plt.plot(tt, xoutS_all[:,694], 'b--', linewidth=3, markersize=12, label='free total AKT')\n", "plt.legend()\n", "ax1.set_xlabel('Time (hr)')\n", "plt.xlim([-1,th+1])\n", "plt.xticks(np.arange(0, th+1, step=4)) # Set label locations.\n" ] } ], "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.9" } }, "nbformat": 4, "nbformat_minor": 2 }