-
Notifications
You must be signed in to change notification settings - Fork 0
/
Repressilator_Stochastic_simulation.py
303 lines (223 loc) · 7.12 KB
/
Repressilator_Stochastic_simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 7 00:22:25 2021
@author: savannah
"""
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
import random
###################
# Helper functions (Do not change!)
def find_index_from_time(t_obs,time,start_index=0):
# loop through t_obs array from i=0
# stopping when t_obs[i+1] is greater than time
# so that t_obs[i] < time < t_obs[i+1]
# return i
i=start_index
while i+1<len(t_obs):
if t_obs[i+1]>time:
break
i=i+1
# i now stores index corresponding to system at time requested
return i
def resample_observations(t_obs_in, s_obs_in, t_obs_out):
s_obs_out=[]
pos=0
for time in t_obs_out:
i=find_index_from_time(t_obs_in,time, start_index=pos)
si = s_obs_in[i]
s_obs_out.append(si)
pos = i
return s_obs_out
def gen_next_event_time(rate):
t=random.expovariate(rate)
return t
def random_choice_from_pdf(pdf):
cdf=[]
cumulative_p=0
for p in pdf:
cumulative_p+=p
cdf.append(cumulative_p)
rand=random.random()
for i in range(len(cdf)):
if rand<cdf[i]:
return i
# last cdf should be 1.0 so the following should never happen!
print("Error generating choice, check PDF")
return None
# In[ ]:
def gillespie_repressilator(s0,t_obs_out,params):
#--0--# Unpack parameters and species variables
km, km0, kdm, kp, kdp, K, n = params
m_tetR, m_lacI, m_cI, p_tetR, p_lacI, p_cI = s0
#--0--#
# create arrays for output
s_obs=[]
t_obs=[]
# read in start time and end time
t_init=t_obs_out[0]
t_final=t_obs_out[-1]
t=t_init
t_obs.append(t)
s_obs.append(s0)
while t < t_final:
#--1--# Write labels for each event type here.
types=["m_tetR_prod",
"m_lacI_prod",
"m_cI_prod",
"m_tetR_loss",
"m_lacI_loss",
"m_cI_loss",
"p_tetR_prod",
"p_lacI_prod",
"p_cI_prod",
"p_tetR_loss",
"p_lacI_loss",
"p_cI_loss",
]
#--1--#
#--2--# Write rate expressions for each of the events
# COPY YOUR RATE EQUATIONS FROM
# THE ODE REPRESSILATOR MODEL HERE
rate_m_tetR_prod = (km*((K**n)/((K**n)+((p_lacI)**n))))+km0
rate_m_lacI_prod = (km*((K**n)/((K**n)+(p_cI**n))))+km0
rate_m_cI_prod = (km*((K**n)/((K**n)+(p_tetR**n))))+km0
rate_p_tetR_prod = kp*m_tetR
rate_p_lacI_prod = kp*m_lacI
rate_p_cI_prod = kp*m_cI
rate_m_tetR_loss = kdm*m_tetR
rate_m_lacI_loss = kdm*m_lacI
rate_m_cI_loss = kdm*m_cI
rate_p_tetR_loss = kdp*p_tetR
rate_p_lacI_loss = kdp*p_lacI
rate_p_cI_loss = kdp*p_cI
#--2--#
#--3--# Store the rates into a list preserving the order of step 1.
rates=[ rate_m_tetR_prod,
rate_m_lacI_prod,
rate_m_cI_prod,
rate_m_tetR_loss,
rate_m_lacI_loss,
rate_m_cI_loss,
rate_p_tetR_prod,
rate_p_lacI_prod,
rate_p_cI_prod,
rate_p_tetR_loss,
rate_p_lacI_loss,
rate_p_cI_loss ]
#--3--#
#-- Do not edit below --#
## CARRY OUT GILLESPIE ALGORITHM TO STEP FORWARD TO NEXT EVENT
## AND UPDATE SYSTEM STATE ACCORDING TO EVENT TYPE
# calc total reaction rate
rate_all_events=sum(rates)
# if rate of events is zero break from loop
# e.g. when all reactants used up
if rate_all_events==0:
break
# generate the time until the next event
# in accordance with rate_all_events
next_event=gen_next_event_time(rate_all_events)
# calc PDF for event type
# in accordance with relative rates
pdf=[]
for event_rate in rates:
p_event = event_rate/sum(rates)
pdf.append(p_event)
rand_i = random_choice_from_pdf(pdf)
event_type=types[rand_i]
# increment time and number of molecules
# according to event type
t=t+next_event
#-----------------------------------#
## ALGORITHM HAS INCREMENTED TIME AND SELECTED NEXT EVENT
## WE NOW NEED TO UPDATE OUR SYSTEM ACCORDING TO THE EVENT
## TYPE STORED IN VARIABLE event_type
if event_type=="m_tetR_prod":
m_tetR = m_tetR + 1
elif event_type=="m_lacI_prod":
m_lacI = m_lacI + 1
elif event_type=="m_cI_prod":
m_cI = m_cI + 1
elif event_type=="m_tetR_loss":
m_tetR = m_tetR - 1
elif event_type=="m_lacI_loss":
m_lacI = m_lacI - 1
elif event_type=="m_cI_loss":
m_cI = m_cI - 1
elif event_type=="p_tetR_prod":
p_tetR = p_tetR + 1
elif event_type=="p_lacI_prod":
p_lacI = p_lacI + 1
elif event_type=="p_cI_prod":
p_cI = p_cI + 1
elif event_type=="p_tetR_loss":
p_tetR = p_tetR - 1
elif event_type=="p_lacI_loss":
p_lacI = p_lacI - 1
elif event_type=="p_cI_loss":
p_cI = p_cI - 1
else:
print("error unknown event type!!")
#--4--#
# store observation
s=[m_tetR, m_lacI, m_cI, p_tetR, p_lacI, p_cI]
t_obs.append(t)
s_obs.append(s)
# loops until time t exceeds t_final
# loop has ended
# before we return the results we must
# resample the output to provide observations in accordance
# with the t_obs passed to the function
s_obs_out=resample_observations(t_obs,s_obs,t_obs_out)
return np.array(s_obs_out)
# In[ ]:
# DEFINE INITIAL CONDITIONS AND PARAMETERS
# set random seed so that notebook results are reproducible
random.seed(1000)
# default parameter values
# to match Repressilator model
km = 30
km0 = 0.03
kdm = 0.3466
kp = 6.931
kdp = 0.06931
K = 40
n = 2
km = 0.5*60
km0 = km*1e-4
params = [ km, km0, kdm, kp, kdp, K, n ]
#intitial condtions
m_tetR0 = 5
m_lacI0 = 0
m_cI0 = 0
p_tetR0 = 0
p_lacI0 = 0
p_cI0 = 0
s0 = [m_tetR0, m_lacI0, m_cI0, p_tetR0, p_lacI0, p_cI0]
# set time observations
t_max=1000
t_obs=np.linspace(0,t_max,t_max*5+1) # 5 observations a minute
# In[ ]:
# run simulation
s_obs=gillespie_repressilator(s0,t_obs,params)
m_tetR_obs = s_obs[:,0]
m_lacI_obs = s_obs[:,1]
m_cI_obs = s_obs[:,2]
p_tetR_obs = s_obs[:,3]
p_lacI_obs = s_obs[:,4]
p_cI_obs = s_obs[:,5]
# In[ ]:
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_xlabel('Time (min)')
ax.set_ylabel('Proteins per cell')
ax.set_ylim(0,8000)
## CREATE TIMESERIES PLOT FULL BEHAVIOUR
ax.set_title('Repressilator stochastic simulation')
ax.plot(t_obs, p_tetR_obs, '-',label='tetR',color='r')
ax.plot(t_obs, p_lacI_obs, '-',label='lacI', color='b')
ax.plot(t_obs, p_cI_obs, '-',label='cI', color='y')
ax.legend()