{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise 3: Simulation of gene expression and regulation (DL 20 November)\n",
"\n",
"Lecturer: Antti Honkela\n",
"\n",
"Your name:\n",
"\n",
"Name of your pair:\n",
"\n",
"In this exercise you will implement simulators to simulate biochemical reaction systems similar to ones that could be used to simulate gene expression. Each task is worth 2 points."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Task 1: Discrete event simulation I: Simple systems"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this task your aim is to perform stochastic simulation of simple discrete reaction systems with a single species X in different scenarios. Each scenario comes with a number of questions. Please answer these by having your code print out the answers.\n",
"\n",
"### a) Simulate the decay of a population of 100 individuals with decay rate 1.\n",
"\n",
"The corresponding reaction system is:\n",
"$$ X \\rightarrow \\emptyset $$\n",
"\n",
"* Implement a function that runs the simulation.\n",
"* Repeat the simulation 100 times. What is the average time until extinction? What are the minimum and maximum times?\n",
"* What is the mean and standard deviation of the number of individuals left alive after 1 unit of time?\n",
"* Plot the simulated numbers of individuals as function of time together in a single plot."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%matplotlib inline\n",
"# Import some useful modules\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import numpy.random as npr\n",
"\n",
"# Suggested structure: simulate the decay of a population of N0 individuals at rate 'rate' for 'numsteps' steps\n",
"def simulate_decay(N0, rate, numsteps):\n",
" # Current population size\n",
" N = N0\n",
" # Current time\n",
" t_cur = 0\n",
" # History of population size across the simulation\n",
" Nhist = np.zeros(numsteps)\n",
" Nhist[0] = N\n",
" # Times of the corresponding population sizes\n",
" Thist = np.zeros(numsteps)\n",
" Thist[0] = t_cur\n",
" # ...\n",
" # ADD CODE FOR PERFORMING THE SIMULATION HERE\n",
" # ...\n",
" return (Thist, Nhist)\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### b) Simulate a birth-death process of a population of 100 individuals with decay rate 1 and birth rates [0.8, 1, 1.2] for 10000 steps.\n",
"\n",
"The corresponding reaction system is:\n",
"\\begin{align}\n",
"X &\\rightarrow 2X \\\\\n",
"X &\\rightarrow \\emptyset \\\\\n",
"\\end{align}\n",
"\n",
"* Implement a function that runs the simulation.\n",
"* Repeat each simulation 100 times. How many simulations lead to extinction in each scenario? What are the statistics of the final populations in each scenario (mean, min, max)?\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def simulate_birthdeath(N0, birthrate, deathrate, numsteps):\n",
" # Current population size\n",
" N = N0\n",
" # Current time\n",
" t_cur = 0\n",
" # History of population size across the simulation\n",
" Nhist = np.zeros(numsteps)\n",
" Nhist[0] = N\n",
" # Times of the corresponding population sizes\n",
" Thist = np.zeros(numsteps)\n",
" Thist[0] = t_cur\n",
" # ...\n",
" # ADD CODE FOR PERFORMING THE SIMULATION HERE\n",
" # ...\n",
" return (Thist, Nhist)\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### c) Simulate a migration-death process of a population of 100 individuals with decay rate 1 and migration rates [1.0, 10.0, 100.0] for 10000 steps.\n",
"\n",
"The corresponding reaction system is:\n",
"\\begin{align}\n",
"\\emptyset &\\rightarrow X \\\\\n",
"X &\\rightarrow \\emptyset \\\\\n",
"\\end{align}\n",
"\n",
"* Implement a function that runs the simulation.\n",
"* Repeat each simulation 100 times. How many simulations lead to extinction in each scenario? What are the statistics of the final populations in each scenario (mean, min, max)?\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def simulate_migrationdeath(N0, migrationrate, deathrate, numsteps):\n",
" # Current population size\n",
" N = N0\n",
" # Current time\n",
" t_cur = 0\n",
" # History of population size across the simulation\n",
" Nhist = np.zeros(numsteps)\n",
" Nhist[0] = N\n",
" # Times of the corresponding population sizes\n",
" Thist = np.zeros(numsteps)\n",
" Thist[0] = t_cur\n",
" # ...\n",
" # ADD CODE FOR PERFORMING THE SIMULATION HERE\n",
" # ...\n",
" return (Thist, Nhist)\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Briefly summarise how each model behaves qualitatively in the text area below:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Task 2: Discrete event simulation II: The general case"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### a) Implement the general Stochastic Simulation Algorithm of Gillespie (1977).\n",
"\n",
"* Implement a function that runs the simulator.\n",
"* Test your simulator by re-implementing exercises 1a and 1b to make sure your results are similar."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Suggested skeleton for the simulation algorithm\n",
"# Suggested arguments are NumPy arrays with sizes as follows:\n",
"# inputs: [reactions x state]\n",
"# outputs: [reactions x state]\n",
"# rates: [reactions]\n",
"def stochastic_simulation_algorithm(initstate, inputs, outputs, rates, numsteps):\n",
" # Current time and state\n",
" t_cur = 0\n",
" state = 1.0 * initstate\n",
" # State history\n",
" Nhist = np.zeros((numsteps, len(state)))\n",
" Nhist[0,:] = state\n",
" # Time history\n",
" Thist = np.zeros(numsteps)\n",
" Thist[0] = t_cur\n",
" # ...\n",
" # ADD CODE FOR PERFORMING THE SIMULATION HERE\n",
" # ...\n",
" return (Thist, Nhist)\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### b) Use your simulator to simulate the Lotka-Volterra predator-prey system:\n",
"\\begin{align}\n",
"X_1 + X_2 &\\rightarrow 2 X_1 \\quad &\\text{# Predator-prey interaction} \\\\\n",
"X_2 &\\rightarrow 2 X_2 &\\text{# Prey reproduction} \\\\\n",
"X_1 &\\rightarrow \\emptyset &\\text{# Predator death} \\\\\n",
"\\end{align}\n",
"\n",
"* Try to find rates for the different equations that produce interesting behaviour.\n",
"* Plot examples of the behaviour under different rates."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Task 3: Continuous-state simulation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### a) Implement a deterministic ordinary differential equation simulator using the same input as the stochastic simulator above.\n",
"\n",
"Your simulator should follow Eq. (11) in Gillespie (2000) paper. In order to discretise the time, please select a finite time step \\tau and apply the Euler discretisation of approximating\n",
"$$ \\frac{dX(t)}{dt} = f(X(t)) $$\n",
"by\n",
"$$ X(t + \\tau) \\approx X(t) + \\tau \\cdot f(X(t)). $$\n",
"\n",
"* Repeat exercises 1a, 1b and 2b using your simulator. How do the results differ from the original?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Suggested skeleton with same arguments as above, except tau is the time step\n",
"def simulate_reaction_ode(initstate, inputs, outputs, rates, numsteps, tau):\n",
" pass\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### b) Implement a stochastic continuous-state simulator using the same input as above.\n",
"\n",
"Your simulator should follow Eq. (21) of the Gillespie (2000) paper.\n",
"\n",
"* Repeat exercises 1a, 1b and 2b using your simulator. How do the results differ from the original and from 3a?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Suggested skeleton with same arguments as above, except tau is the time step\n",
"def simulate_chemical_langevin_eq(initstate, inputs, outputs, rates, numsteps, tau):\n",
" pass\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Task 4: Simulating repression"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this task you will implement a simplified model of repressive regulation, a simplified \"repressilator\". This is more complicated than the predator-prey system in 2b because preventing preduction is not the same as enhancing degradation. (In the predator analogue, predators would only target the young and therefore decrease the birth rate of prey but leave the existing (adult) population intact.\n",
"\n",
"The repressilator model has 3 genes, each of which represses the next in a cycle A -> B -> C -> A. A realistic model would include 6 species to model both mRNAs and protein levels of each gene. In this task we simplify things a bit by combining the mRNA and protein for each gene.\n",
"\n",
"In order to simulate repression, we augment the model by introducing additional states $aX$ and $rX$ that represent \"active\" and \"repressed\" promoter states of gene $X \\in \\{A, B, C\\}$. Production of gene $X$ only happens when the promoter is in the active state and the repressor moves the promoter into repressed state. The system can be expressed through reactions:\n",
"\\begin{align}\n",
"aX &\\rightarrow aX + 10 X \\quad &\\text{# transcription} \\\\\n",
"Y + aX &\\rightarrow rX &\\text{# repression, } Y \\text{ is the repressor}\\\\\n",
"rX &\\rightarrow aX &\\text{# recovery from repression} \\\\\n",
"X &\\rightarrow \\emptyset &\\text{# decay}\n",
"\\end{align}\n",
"These reactions are repeated for all genes, resulting in total 12 reactions for the 9 species in the system.\n",
"\n",
"* Implement the reactions and simulate them using the simulators you implemented in Tasks 2 and/or 3. Use rate 0.1 for the decay equation and rate 1.0 for the others.\n",
"* Repeat the stochastic simulations 100 times and compute the average. Visualise a number of the individual simulations."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Bonus task: implement a full repressilator model including both mRNAs and proteins. Try to assign realistic rates for all the reactions. Simulate your model using the simulators you implemented in Tasks 2 and/or 3 and visualise the results."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Task 5: Simulating network motifs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### a) Use the simulators to simulate from the SIM (single input motif) network motif.\n",
"\n",
"Using a model with regulator $TF$ and targets $X_i$ the system can be expressed through reactions:\n",
"\\begin{align}\n",
"TF &\\rightarrow TF + 10X_i \\quad &\\text{# transcription}\\\\\n",
"X_i &\\rightarrow \\emptyset &\\text{# target decay}\\\\\n",
"TF &\\rightarrow \\emptyset &\\text{# TF decay}\n",
"\\end{align}\n",
"Starting with relatively high TF abundance and fairly low decay will give interesting profiles.\n",
"\n",
"You can experiment with rate constants to obtain interesting results. One set of rate constants that yield reasonable behaviour is:\n",
"\n",
"`\n",
"target_transcription_rates = np.logspace(-1, 1, N)\n",
"target_decay_rates = np.logspace(-1, 2, N)\n",
"TF_decay_rate = 10\n",
"TF_initial_abundance = 100\n",
"`\n",
"\n",
"* Simulate the system and visualise the results."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Briefly describe what kind of behaviours you observe and how the rates affect the outcome."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### b) Use the simulators to simulate from the feed-forward loop network motif.\n",
"\n",
"* Similar as in a) above, construct a set of target genes following the feed-forward loop motif, simulate them and visualise the results."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* How are the results different from the SIM case?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## General hints and warnings\n",
"\n",
"* Remember that Python passes function arguments by reference. This means that if you change the argument in a function, it will also change in the caller. If you have a simulator that changes its initial state, you may inadvertedly change its value in the caller too, which may affect future calls if you are running the simulator in a loop. Directly assigning the value to a new variable is not enough because assignments are also by reference.\n",
"* The continuous-state simulators can be sensitive to the chosen $\\tau$ value. If the simulation is unstable, try decreasing $\\tau$."
]
}
],
"metadata": {}
}
]
}