-
Notifications
You must be signed in to change notification settings - Fork 4
/
RunModel.py
277 lines (167 loc) · 6.51 KB
/
RunModel.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
# function [tout_all,xoutG_all,xoutS_all]=RunModel(flagD,th,STIM,xoutS,xoutG,dataS,dataG,kTCleak,kTCmaxs)
# import sys
import time
import numpy as np
import matplotlib.pyplot as plt
from RunPrep import *
from gm import *
from nc_createODEs import *
from Jeval774 import Jeval774
# from scipy.integrate import odeint
# from createODEs import *
from assimulo.solvers import CVode
from assimulo.problem import Explicit_Problem
np.set_printoptions(threshold=np.nan)
def RunModel(flagD,th,STIM,xoutS,xoutG,dataS,dataG,kTCleak,kTCmaxs, inds_to_watch = []):
# going to return [tout_all,xoutG_all,xoutS_all]
# This function runs the model and outputs timecourse simulation results.
# Required Inputs:
# flagD: 1 for deterministic simulations, 0 for stochastic simulations.
# th: simulation time (hours)
# STIM: stimulus vector
#
# Outputs:
# tout_all: n-by-1 vector of time values (seconds)
# xoutG_all: n-by-g matrix of species (g) through time (n) (g indices lines up to gm tab in Names.xls sheet)
# xoutS_all: n-by-p matrix of speices (p) through time (n) (p indices lines up to PARCDL tab in Names.xls sheet)
#
# %% RUN
ts=dataS.ts;
ts_up=ts;
N_STEPS=th*3600/ts;
N_STEPS = int(N_STEPS)
# % IMPORT INITIALIZED PARAMETERS
pathi='initialized/';
# % for PARCDL
kbR0 = float(open(pathi + "i_kbR0.txt").read())
kTL = []
with open(pathi + 'i_kTLF.txt') as f:
for line in f:
kTL.append(float(line))
kTL = np.array(kTL)
kC173 = float(open(pathi + "i_kC173.txt").read())
kC82 = float(open(pathi + "i_kC82.txt").read())
kA77 = float(open(pathi + "i_kA77.txt").read())*5
# ^ forgot to add the *5 to this line and spent sooooo long looking for this mistake lol
kA87 = float(open(pathi + "i_kA87.txt").read())
Rt = float(open(pathi + "i_Rt.txt").read())
EIF4Efree = float(open(pathi + "i_EIF4Efree.txt").read())
kDDbasal = float(open(pathi + "i_kDDbasal.txt").read())
Vc = dataS.kS[2]
# % for gm
if len(kTCleak)==0:
for line in open(pathi + "i_kTCleakF.txt").readlines():
kTCleak.append(float(line))
kTCleak = np.matrix.transpose(np.matrix(kTCleak))
if len(kTCmaxs)==0:
for line in open(pathi + "i_kTCmaxsF.txt").readlines():
kTCmaxs.append(float(line))
kTCmaxs = np.matrix.transpose(np.matrix(kTCmaxs))
kTCmaxs = np.array(kTCmaxs)
# % modifying data.S structure
dataS.kS[0]=Rt;
dataS.kS[1]=EIF4Efree;
dataS.kS[11]=kbR0;
dataS.kS[16:157]=kTL;
dataS.kS[631]=kC173;
dataS.kS[540]=kC82;
dataS.kS[708]=kA77;
dataS.kS[718]=kA87;
dataS.kS[449]=kDDbasal;
# % modifying data.G structure
dataG.kTCleak=kTCleak;
dataG.kTCmaxs=kTCmaxs;
# %species
if len(xoutS) == 0:
xoutS = []
with open(pathi + 'i_xoutF.csv', newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:
x = ', '.join(row)
x = x.split(',')
to_append = []
for item in x:
to_append.append(float(item))
xoutS.append(to_append)
xoutS = np.matrix(xoutS)
xoutS = xoutS[24,:]
if len(xoutG) == 0:
if flagD:
xoutG = dataG.x0gm_mpc_D
else:
xoutG = dataG.x0gm_mpc
indsD=dataG.indsD
xoutG[indsD] = dataG.x0gm_mpc_D[indsD]
xoutG[indsD+141] = dataG.x0gm_mpc_D[indsD+141]
xoutG[indsD+141*2] = dataG.x0gm_mpc_D[indsD+141*2]
# % Apply STIM
Etop = STIM[len(STIM)-1]
STIM = STIM[0:len(STIM)-1]
# code for logical
if np.any(STIM):
xoutS[0,STIM.astype(bool)] = STIM[STIM.astype(bool)]
dataS.kS[452] = Etop
# NOTE - matlab code
# % Instantiation
# t0 = 0;
# optionscvodes = CVodeSetOptions('UserData', dataS,...
# 'RelTol',1.e-3,...
# 'LinearSolver','Dense',...
# 'JacobianFn',@Jeval774);
# CVodeInit(@createODEs, 'BDF', 'Newton', t0, xoutS', optionscvodes);
#
# %ODE15s options
# %optionsode15s=odeset('RelTol',1e-3,'Jacobian',@Jeval774ode15s);
#
tout_all = np.zeros(shape=(N_STEPS+1))
xoutG_all = np.zeros(shape=(N_STEPS+1,len(xoutG)))
xoutS_all = np.zeros(shape=(N_STEPS+1,xoutS.shape[1]))
tout_all[0] = 0
xoutG_all[0,:] = np.matrix.transpose(xoutG)
xoutS_all[0,:] = xoutS
# % Starting simulations
print("... Starting Sims")
start_time = time.time()
for i in range(0,int(N_STEPS)+1):
# gm
[xginN,xgacN,AllGenesVecN,xmN,vTC] = gm(flagD,dataG,ts,xoutG,xoutS);
xoutG = np.append(np.append(np.squeeze(np.asarray(xgacN)),np.squeeze(np.asarray(xginN))),np.squeeze(np.asarray(xmN)))
# NOTE - matrix to array syntax
xoutG = np.matrix.transpose(np.matrix(xoutG))
dataS.mMod=xmN*(1E9/(Vc*6.023E+23)); #convert mRNAs from mpc to nM
dataG.AllGenesVec=AllGenesVecN;
xoutG_all[i,:] = np.matrix.transpose(xoutG)
try:
xoutS_all[i,:] = np.squeeze(np.asarray(xoutS))
except:
xoutS_all[i,:] = np.squeeze(np.asarray(xoutS[1]))
if xoutS[0,103]<xoutS[0,105]:
print("Apoptosis happened")
tout_all = tout_all[0:i+1]
xoutG_all = xoutG_all[0:i+1]
xoutS_all = xoutS_all[0:i+1]
return [tout_all, xoutG_all, xoutS_all]
# scipy.odeint -- takes forever
# xoutS = odeint(createODEs, xoutS_all[i,:],np.array([ts_up-ts, ts_up]), args=(dataS.kS,dataS.VvPARCDL,dataS.VxPARCDL,dataS.S_PARCDL,dataS.mExp_nM.as_matrix(),dataS.mMod,dataS.flagE))
# assimulo -- much faster
ode_start_time = time.time()
exp_mod = MyProblem(y0=xoutS_all[i,:],dataS=dataS, Jeval774 = Jeval774)
exp_sim = CVode(exp_mod)
exp_sim.verbosity=50
exp_sim.re_init(ts_up-ts,xoutS_all[i,:] )
t1, xoutS = exp_sim.simulate(ts_up, 1)
try:
print(xoutS[1,inds_to_watch])
except:
print(xoutS)
print("--- %s seconds ---" % (time.time() - ode_start_time))
print("Percent complete: " + str(i/N_STEPS))
# xoutG_all[i,:] = np.matrix.transpose(xoutG);
try:
tout_all[i+1] = ts_up
except:
pass
ts_up = ts_up + ts
print("ODEs done")
print("--- %s seconds ---" % (time.time() - start_time))
return [tout_all, xoutG_all, xoutS_all]