-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneurord_fit_Ca_RyR3CaM_steady_state.py
186 lines (158 loc) · 7.23 KB
/
neurord_fit_Ca_RyR3CaM_steady_state.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
#How to use :doc:`ajustador` to fit a NeuroRD model of CamKII activation
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import os
from lxml import etree
import ajustador as aju
import numpy as np
from ajustador import drawing,loadconc,nrd_fitness
from ajustador.helpers import converge,save_params
# constants:
kd = 1000 # Ca affinity for RyRCaM
dirname='fit_RyR3CaM_Ca/' #where data and model file are stored. Can be different than current directory. Multiple datafiles allowed
#Set of model files that have first part of file name in common. All included files must be in same directory.
model_set='model'
exp_set='combined' #set of data files corresponding to model files; files may contain several molecules
#which molecule(s) to match in optimization
fname_xml = os.path.join(dirname, "Rxn_RyR3CaM.xml")
tree = etree.parse(fname_xml)
root = tree.getroot()
mol_list = []
for son in root:
if son.tag == "Specie":
if "O" in son.attrib["id"]:
mol_list.append(son.attrib["id"])
mol={"RO": mol_list}
print(mol)
tmpdir='/tmp/RyR3CaM_Ca_ss'+dirname
os.chdir(dirname)
# Use loadconc.CSV_conc_set if data to match are csv format (typically from wet experiments)
exp = loadconc.CSV_conc_set(exp_set)
# number of iterations, use 1 for testing
# default popsize=8, use 3 for testing
iterations=100
popsize=8
test_size=25 #for convergence
P = aju.xml.XMLParam
#list of parameters to change/optimize
my_params = []
counters = {}
for son in root:
if son.tag == "Reaction":
species = []
for grandson in son:
try:
species.append(grandson.attrib["specieID"])
except KeyError:
continue
reac_id = son.attrib["id"]
forward_path = '//Reaction[@id="%s"]/forwardRate' % reac_id
reverse_path = '//Reaction[@id="%s"]/reverseRate' % reac_id
if species[-1].endswith("O1"):
continue
if species[-1].endswith("O2"):
continue
if species[-1].endswith("C1"):
continue
if species[-1].endswith("I"):
continue
if species[-1].startswith("Ca1") and species[0].startswith("RyR"):
print(species, reac_id)
if "Ca1" not in counters:
counters["Ca1"] = 0
my_params.append(P('Ca1RyR_fwd_rate', 0.003, min=1e-4,
max=1,
xpath=forward_path))
else:
my_params.append(P('Ca1RyR_fwd_rate%d'%counters["Ca1"], 0,
fixed='Ca1RyR_fwd_rate',
constant=1,
xpath=forward_path))
my_params.append(P('Ca1RyR_bkw_rate%d'%counters["Ca1"], 0,
fixed='Ca1RyR_fwd_rate',
constant=kd,
xpath=reverse_path))
counters["Ca1"] += 1
elif species[-1].startswith("Ca2") and species[0].startswith("Ca1"):
print(species, reac_id)
if "Ca2" not in counters:
counters["Ca2"] = 0
my_params.append(P('Ca2RyR_fwd_rate%d'%counters["Ca2"], 0,
fixed='Ca1RyR_fwd_rate',
constant=0.75,
xpath=forward_path))
my_params.append(P('Ca2RyR_bkw_rate%d'%counters["Ca2"], 0,
fixed='Ca1RyR_fwd_rate',
constant=kd*2,
xpath=reverse_path))
counters["Ca2"] += 1
elif species[-1].startswith("Ca3") and species[0].startswith("Ca2"):
print(species, reac_id)
if "Ca3" not in counters:
counters["Ca3"] = 0
my_params.append(P('Ca3RyR_fwd_rate%d'%counters["Ca3"], 0,
fixed='Ca1RyR_fwd_rate',
constant=0.5,
xpath=forward_path))
my_params.append(P('Ca3RyR_bkw_rate%d'%counters["Ca3"], 0,
fixed='Ca1RyR_fwd_rate',
constant=kd*3,
xpath=reverse_path))
counters["Ca3"] += 1
elif species[-1].startswith("Ca4") and species[0].startswith("Ca3"):
print(species, reac_id)
if "Ca4" not in counters:
counters["Ca4"] = 0
my_params.append(P('Ca4RyR_fwd_rate%d'%counters["Ca4"], 0,
fixed='Ca1RyR_fwd_rate',
constant=0.25,
xpath=forward_path))
my_params.append(P('Ca4RyR_bkw_rate%d'%counters["Ca4"], 0,
fixed='Ca1RyR_fwd_rate',
constant=kd*4,
xpath=reverse_path))
counters["Ca4"] += 1
elif species[-1].endswith("I2"):
print(species, reac_id)
if "I2" not in counters:
counters["I2"] = 0
my_params.append(P("I2_flicker_fwd_rate", 9,
min=1e-3, max=1e3,
xpath=forward_path))
my_params.append(P("I2_flicker_bkw_rate", 0.9,
min=1e-3, max=1e3,
xpath=reverse_path))
else:
my_params.append(P("I2_flicker_fwd_rate%d" % counters["I2"], 0,
fixed="I2_flicker_fwd_rate",
constant=1,
xpath=forward_path))
my_params.append(P("I2_flicker_bkw_rate%d" % counters["I2"], 0,
fixed="I2_flicker_bkw_rate", constant=1,
xpath=reverse_path))
counters["I2"] += 1
params = aju.optimize.ParamSet(*my_params)
# ###################### END CUSTOMIZATION #######################################
fitness = nrd_fitness.specie_concentration_fitness(species_list=mol)
############ Test fitness function
#model=dirname+'Model-CKnew-Cahz1.xml'
#sim = aju.xml.NeurordSimulation('/tmp', model=model, params=params)
#sim2=aju.xml.NeurordResult('Model_syngap_ras.h5')
#print(fitness(sim2, exp))
################
fit = aju.optimize.Fit(tmpdir, exp, model_set, None, fitness, params,
_make_simulation=aju.xml.NeurordSimulation.make,
_result_constructor=aju.xml.NeurordResult)
fit.load()
fit.do_fit(iterations, popsize=popsize,sigma=0.3)
mean_dict,std_dict,CV=converge.iterate_fit(fit,test_size,popsize)
########################################### Done with fitting
#to look at fit history
aju.drawing.plot_history(fit,fit.measurement)
if callable(fit.optimizer.result):
result = fit.optimizer.result()
else:
result = fit.optimizer.result
#print centroid [0] and stdev [6] of cloud of good results:
for i,p in enumerate(fit.params.unscale(result[0])):
print(fit.param_names()[i],'=',p, '+/-', fit.params.unscale(result[6])[i])
save_params.save_params(fit,0,1)