-
Notifications
You must be signed in to change notification settings - Fork 1
/
Modules.py
471 lines (371 loc) · 15 KB
/
Modules.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import Cleft_compartment
import Spine_compartment
from utility import *
from matplotlib import pyplot as plt
from pysb.simulator import ScipyOdeSimulator
from scipy.integrate import odeint
import pylab as pl
import numpy as np
Vr = -65# mV
Ve = 0 # mV
Rs = 500*1E6 # Ohm, *1
g_AMPA = 15*1E-12 # S, *1
g_NMDA = 40*1E-12 # S, *1
g_NMDA_Ca = -1/500 # -0.002uM/ms*mV, *2
# Mg = 1 # Mg concentration M
Ca_spine_init = 0. # 0 uM
e_Ca = 130 # mV
R = 8.31434 # J mol−1 K−1
T = 299.5 # K
F = 96486.7104 # Faraday constant, C/mol
z = +2
A = 6.022E23
e = 1.6E-19
Q = 6.2415E18 # number of charges in 1 Coulomb
# time constants
tau_Ca = 20*1E-3 # s (20 ms)
I_f = 0.75 # *2
I_s = 0.25 # *2
tau_f_bpap = 3*1E-3 # s, *2
tau_s_bpap = 25*1E-3 # s, *2
tau_rise_bpap = 0.5*1E-3 # s, *2
V_max_bpap = 67 # mV, *2
k_M = 0.093 # 0.093 1/mV, *2
def Stimulation_Pattern_Design (PARAMETERS, plot=False):
'''
Given the simulation parameters dictionary,
plot the stimulation pattern that has been designed.
'''
n_step = int(PARAMETERS['T_total']/PARAMETERS['dt'])
TIME = pl.linspace(0, PARAMETERS['T_total'], num=n_step)
PRE_ = np.zeros(n_step)
p1_PRE = 1/PARAMETERS['f1_PRE']
p2_PRE = 1/PARAMETERS['f2_PRE']
n2_PRE = 1+int(PARAMETERS['T_total']/p2_PRE)
glu_t = PARAMETERS['Glu_time']
glu_n = int(glu_t/PARAMETERS['dt'])
PRE_t = []
T_pre = PARAMETERS['T_PRE']
for i in range(n2_PRE):
t = T_pre
for j in range(PARAMETERS['n1_PRE']):
if t > PARAMETERS['T_total'] : break
PRE_t.append(t)
t += p1_PRE
T_pre += p2_PRE
for t in PRE_t:
i = np.where(TIME>=t)
PRE_[i[0][0]:i[0][0]+glu_n] = 1
PRE_[-1] = 0
POST_ = np.zeros(n_step)
p1_POST = 1/PARAMETERS['f1_POST']
p2_POST = 1/PARAMETERS['f2_POST']
n2_POST = 1+int(PARAMETERS['T_total']/p2_POST)
POST_t = []
T_post = PARAMETERS['T_POST']
for i in range(n2_POST):
t = T_post
for j in range(PARAMETERS['n1_POST']):
if t > PARAMETERS['T_total'] : break
POST_t.append(t)
t += p1_POST
T_post += p2_POST
for t in POST_t:
i = np.where(TIME>=t)
POST_[i[0][0]]=1.3
if plot:
plt.figure('Stimulation Protocol',(9,5))
plt.plot(TIME,PRE_,ds='steps',label='Pre-', c='black')
plt.plot(TIME,POST_,ds='steps',label='Post-', c='red')
plt.xlabel("Time (s)")
ax = plt.gca()
ax.yaxis.set_visible(False)
ax.set_aspect('auto', anchor='SW')
ax.fill(TIME,PRE_,'k')
plt.legend(loc='upper right')
plt.show()
results = {'PRE':PRE_,'POST':POST_}
return results
def Receptors_Gating_Simulation (PARAMETERS, plot=False):
T_total = PARAMETERS['T_total']
T_PRE = PARAMETERS['T_PRE']
dt = PARAMETERS['dt']
n_step = int(T_total/dt)
TIME = pl.linspace(0, T_total, num=n_step)
# PRE-SYNAPTIC STIMULATION PARAMETERS
p1_PRE = 1/PARAMETERS['f1_PRE']
p2_PRE = 1/PARAMETERS['f2_PRE']
n1_PRE = PARAMETERS['n1_PRE']
n2_PRE = 1+int(T_total/p2_PRE)
Glu = PARAMETERS['Glu']
Glu_time = PARAMETERS['Glu_time']
AMPA_tot = 1
NMDA_tot = 1
print('\nSimulating Synaptic Cleft Compartment ...', end =" ")
time_pre = TIME[int(T_PRE/dt):]
mymodel = Cleft_compartment.network(Glu, Glu_time, n1_PRE, n2_PRE, p1_PRE, p2_PRE,PARAMETERS['pKd'])
#N.B set 'atol' and 'rtol' parameters >0 for a more generous handling of integation's error
atol = 1E-6
rtol = 1E-5
try:
sim = ScipyOdeSimulator(mymodel, integrator='vode', compiler='cython', integrator_options={'atol': atol, 'rtol': rtol}, verbose=False)
except:
print('\nWARNING: Generated code is too complex to be executed with Cython compiler. Python compiler will be used istead.')
sim = ScipyOdeSimulator(mymodel, integrator='vode', compiler='python', integrator_options={'atol': atol, 'rtol': rtol}, verbose=False)
simres = sim.run(tspan=time_pre)
print('Done!')
yout = simres.all
# add zero-values time points at the begin of the simulated trajectories in order to set their sizes equal to TIME array's one
zeros = np.zeros(len(TIME[:int(T_PRE/dt)]))
P_AMPA = np.concatenate((zeros,yout['obs_AMPA_OA2']))
O_AMPA = P_AMPA*PARAMETERS['AMPA_tot']
D1_AMPA = np.concatenate((zeros,yout['obs_AMPA_D1']))
D2_AMPA = np.concatenate((zeros,yout['obs_AMPA_D2']))
D3_AMPA = np.concatenate((zeros,yout['obs_AMPA_D3']))
D_AMPA_total = []
for d1,d2,d3 in zip(D1_AMPA,D2_AMPA,D3_AMPA):
D_AMPA_total.append(d1+d2+d3)
D_AMPA = [r/AMPA_tot for r in D_AMPA_total]
P_NMDA = np.concatenate((zeros,yout['obs_NMDA_O1']))
O_NMDA = P_NMDA*PARAMETERS['NMDA_tot']
D1_NMDA = np.concatenate((zeros,yout['obs_NMDA_D1']))
D2_NMDA = np.concatenate((zeros,yout['obs_NMDA_D2']))
D_NMDA_total = []
for d1,d2 in zip(D1_NMDA,D2_NMDA):
D_NMDA_total.append(d1+d2)
D_NMDA = [r/NMDA_tot for r in D_NMDA_total]
results = {
'O_AMPA':O_AMPA,
'O_NMDA':O_NMDA,
'P_AMPA': O_AMPA,
'P_NMDA': O_NMDA,
'D_AMPA': D_AMPA,
'D_NMDA': D_NMDA,
'D1_NMDA': D1_NMDA,
'D2_NMDA': D2_NMDA
}
if plot:
plt.figure('Receptors Gating',(9,5))
plt.plot(TIME,O_AMPA,c='black',label='AMPA')
plt.plot(TIME,O_NMDA,c='gray',label='NMDA')
plt.xlabel("Time (s)")
plt.ylabel("Number of Receptors")
plt.legend()
plt.show()
return results
def EPSCs_EPSPs_Calculation (PARAMETERS,O_AMPA,O_NMDA, plot=False):
T_total = PARAMETERS['T_total']
n_step = int(PARAMETERS['T_total']/PARAMETERS['dt'])
T_PRE = PARAMETERS['T_PRE']
T_POST = PARAMETERS['T_POST']
f1_POST = PARAMETERS['f1_POST']
p1_POST = 1/PARAMETERS['f1_POST']
n1_POST = PARAMETERS['n1_POST']
p2_POST = 1/PARAMETERS['f2_POST']
n2_POST = 1+int(PARAMETERS['T_total']/p2_POST)
Mg = PARAMETERS['Mg']
Vr = PARAMETERS['Vr']
TIME = pl.linspace(0, T_total, num=n_step)
EPSC_AMPA = []
EPSP_AMPA = []
EPSC_NMDA = []
EPSP_NMDA = []
Ca_spine = []
bAP = []
Vm = []
EPSC_tot=[]
EPSP_total=[]
Mg_block = []
def intergrate_Ca(Ca,t,I):
tau = tau_Ca
dCadt = I*1E3-Ca/tau_Ca
return dCadt
# Create the time vector for the bAP stimulation pattern
bAPs_timevector = []
if f1_POST > 0:
T = T_POST
for i in range(n2_POST):
t = T
for j in range(n1_POST):
if t > T_total : break
bAPs_timevector.append(t)
t += p1_POST
T += p2_POST
else :
bAPs_timevector.append(-1)
print('\nEPSCs/EPSPs Calculation is running ...')
i=0
j=0
sample_rate = 1 # set a step value to use for sample the trajectories of AMPA and NMDA coming from RGS module
printProgressBar(0, len(TIME[::int(sample_rate)]), prefix = 'Progress:', suffix = 'Complete', length = 50)
for i,t in enumerate(TIME[::int(sample_rate)]):
k = i*int(sample_rate)
# bAP_
if t < T_POST:
bAP_=0
bAP.append(Vr)
else:
t_post = bAPs_timevector[j]
if len(bAPs_timevector) > j+1:
if t >= bAPs_timevector[j+1]:
j+=1
bAP_ = V_max_bpap * \
( I_f*np.exp(-(t-t_post)/tau_f_bpap) + \
I_s*np.exp(-(t-t_post)/tau_s_bpap))
# * (1-np.exp(-(t-t_post)/tau_rise_bpap)) # rise phase
bAP.append(Vr+bAP_)
if t < T_PRE:
# We assume that at t=0 Vm is at resting potential and the net current flow is 0 since the glutamate has not still diffused
EPSC_AMPA.append(0)
EPSC_NMDA.append(0)
EPSC_tot.append(0)
Ca_spine.append(Ca_spine_init)
EPSP_AMPA.append(Vr)
EPSP_NMDA.append(Vr)
Vm.append(Vr)
else:
O_ampa = O_AMPA[k]
O_nmda = O_NMDA[k]
P_ampa = O_AMPA[k]/PARAMETERS['AMPA_tot']
P_nmda = O_NMDA[k]/PARAMETERS['NMDA_tot']
Is_ampa = O_ampa*g_AMPA*(Vm[i-1]-Ve) *1E-3
Is_nmda = O_nmda*g_NMDA*(Vm[i-1]-Ve)*B *1E-3
Is_nmda_Ca = O_nmda*g_NMDA_Ca*(Vm[i-1]-e_Ca)*B
Is_tot = Is_ampa+Is_nmda
epsp_ampa = Rs*(-Is_ampa) *1E3
epsp_nmda = Rs*(-Is_nmda) *1E3
Vm_tot = Vr+epsp_ampa+epsp_nmda+bAP_
# integrate the Ca2+ variation in the spine
ts = [TIME[k-1], t]
Ca_0 = Ca_spine[i-1]
Ca = odeint(intergrate_Ca,Ca_0,ts,args=(Is_nmda_Ca,))
EPSC_AMPA.append(Is_ampa)
EPSC_NMDA.append(Is_nmda)
EPSC_tot.append(Is_tot)
Ca_spine.append(Ca[1])
EPSP_AMPA.append(Vr+epsp_ampa)
EPSP_NMDA.append(Vr+epsp_nmda)
Vm.append(Vm_tot)
# Mg blocking function
B = 1/(1+np.exp(-k_M*Vm[i])*(Mg/3.57))
Mg_block.append(B)
i+=1
if i % 100 == 0:
# print progress bar
printProgressBar(i/100, len(TIME[::int(sample_rate)][::100]), prefix = 'Progress:', suffix = 'Complete', length = 50)
print('Done!')
EPSC_AMPA = [i*1E12 for i in EPSC_AMPA] # converts from A to pA
EPSC_NMDA = [i*1E12 for i in EPSC_NMDA] # converts from A to pA
EPSC_tot = [i*1E12 for i in EPSC_tot] # converts from A to pA
results = {
'EPSC_AMPA':EPSC_AMPA,
'EPSP_AMPA':EPSP_AMPA,
'EPSC_NMDA':EPSC_NMDA,
'EPSP_NMDA':EPSP_NMDA,
'Ca_spine': Ca_spine,
'bAP':bAP,
'Vm':Vm,
'Mg_block':Mg_block
}
if plot :
plt.figure('EPSPs-ESPCs',(9,13))
#
plt.subplot(4,1,2)
plt.plot(TIME, EPSP_AMPA, label="EPSP_AMPA", c='black')
plt.plot(TIME, EPSP_NMDA, label="EPSP_NMDA", c='gray')
plt.plot(TIME, bAP, label="bAP")
plt.legend()
plt.xlabel("Time (s)")
plt.ylabel("mV")
#
plt.subplot(4,1,3)
plt.plot(TIME, Vm, label="Vm", c='red')
plt.legend()
plt.xlabel("Time (s)")
plt.ylabel("mV")
#
plt.subplot(4,1,1)
plt.plot(TIME, EPSC_AMPA, label="EPSC_AMPA",c='black')
plt.plot(TIME, EPSC_NMDA, label="EPSC_NMDA",c='gray')
plt.legend()
plt.xlabel("Time (s)")
plt.ylabel("pA")
#
plt.subplot(4,1,4)
plt.plot(TIME, Ca_spine, label=" Spine Ca$^{2+}$", c='navy')
plt.legend()
plt.xlabel("Time (s)")
plt.ylabel("\u03BCM")
#
plt.show()
return results
def CaMKII_Activation_Simulation (PARAMETERS, Ca_spine, plot=False):
print('\nSimulating Spine Compartment ...')
n_step = int(PARAMETERS['T_total']/PARAMETERS['dt'])
TIME = pl.linspace(0, PARAMETERS['T_total'], num=n_step)
init_cond = {'CaM_init': PARAMETERS['CaM_init'] , 'CaMKII_init': PARAMETERS['CaMKII_init'], 'Ca_init': Ca_spine[0],}
mymodel = Spine_compartment.network(init_cond)
atol = 1E-6
rtol = 1E-5
sim = ScipyOdeSimulator(mymodel, integrator='vode', compiler='cython', integrator_options={'atol': atol, 'rtol': rtol}, verbose=False)
sample_rate = int(1E3) # set a step value to use for sample the trajectory of Ca_spine coming from CPC module
# Ca_spine = [i*1E3 for i in Ca_spine] #test
Ca_spine_ = Ca_spine[0::sample_rate]
TIME_ = TIME[0::sample_rate]
simres = sim.run(tspan=[TIME_[0],TIME_[1]])
full_trajectories = simres.species
printProgressBar(0, len(Ca_spine_[1:-1]), prefix = 'Progress:', suffix = 'Complete', length = 50)
for i, Ca in enumerate(Ca_spine_[1:-1]):
initials = simres.species[-1]
initials[2] = Ca
simres = sim.run(tspan=[TIME_[i],TIME_[i+1]], initials=initials, param_values={'Ca_init':Ca},num_processors=1)
full_trajectories = np.concatenate((full_trajectories,np.array([simres.species[-1]])),axis=0)
printProgressBar(i+1, len(Ca_spine_[1:-1]), prefix = 'Progress:', suffix = 'Complete', length = 50)
print('Done!')
### Plot Trajectories ###
# create a dict specie -> index
species_indices = {}
for i,specie in enumerate(mymodel.species):
species_indices.update({str(specie) : i})
# specify here the species you want to plot: specie : []
query_species = {
"CaMKII(CaMKII_b1=None, CaMKII_s='CaMKII_CaM1N', CaMKII_p='p1')":[],
"CaMKII(CaMKII_b1=None, CaMKII_s='CaMKII_CaM2N', CaMKII_p='p1')":[],
"CaMKII(CaMKII_b1=None, CaMKII_s='CaMKII_CaM1C1N', CaMKII_p='p1')":[],
"CaMKII(CaMKII_b1=None, CaMKII_s='CaMKII_CaM1C2N', CaMKII_p='p1')":[],
"CaMKII(CaMKII_b1=None, CaMKII_s='CaMKII_CaM2C', CaMKII_p='p1')":[],
"CaMKII(CaMKII_b1=None, CaMKII_s='CaMKII_CaM2C1N', CaMKII_p='p1')":[],
"CaMKII(CaMKII_b1=None, CaMKII_s='CaMKII_CaM4', CaMKII_p='p1')":[],
"Ca()":[]
}
for specie in query_species:
i = species_indices[specie]
values = []
for iteration in full_trajectories:
values.append(iteration[i])
query_species.update({specie:values})
# Sum up all the phosphorilated species of CAMKII
query_species.update({'pCamKII_total': np.sum([query_species["CaMKII(CaMKII_b1=None, CaMKII_s='CaMKII_CaM1N', CaMKII_p='p1')"],\
query_species["CaMKII(CaMKII_b1=None, CaMKII_s='CaMKII_CaM2N', CaMKII_p='p1')"],\
query_species["CaMKII(CaMKII_b1=None, CaMKII_s='CaMKII_CaM1C1N', CaMKII_p='p1')"],\
query_species["CaMKII(CaMKII_b1=None, CaMKII_s='CaMKII_CaM1C2N', CaMKII_p='p1')"],\
query_species["CaMKII(CaMKII_b1=None, CaMKII_s='CaMKII_CaM2C', CaMKII_p='p1')"],\
query_species["CaMKII(CaMKII_b1=None, CaMKII_s='CaMKII_CaM2C1N', CaMKII_p='p1')"],\
query_species["CaMKII(CaMKII_b1=None, CaMKII_s='CaMKII_CaM4', CaMKII_p='p1')"]],\
axis=0)})
if plot:
plt.figure('CaM/CaMKII Activation Siumlation', (9,10))
plt.subplot(2,1,1)
plt.plot(TIME_, query_species["Ca()"], label="Ca_in", c='black')
plt.legend()
plt.xlabel("Time (s)")
plt.ylabel("\u03BCM")
#
plt.subplot(2,1,2)
plt.plot(TIME_, query_species["pCamKII_total"], label="Active CaMKII", c='purple')
plt.legend()
plt.xlabel("Time (s)")
plt.ylabel("\u03BCM")
#
plt.show()
return query_species