forked from mgschiavon/CoRa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoRa_Main.jl
242 lines (239 loc) · 9.63 KB
/
CoRa_Main.jl
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
## CoRa ANALYSIS
# Mariana Gómez-Schiavon
# July, 2019
# Julia v.1.1.1
# Required libraries:
# DifferentialEquations
# ParameterizedFunctions
# Statistics
# Distributions
# DelimitedFiles
## INPUTS:
# iARG = (mm : Label for motif file, ex : Label for parameters file, pp : Label for perturbation type, an : Chose analysis type);
include(string("InputFiles\\ARGS_",iARG.mm,"_Pert_",iARG.ex,".jl")) # Perturbation details
include(string("InputFiles\\ARGS_",iARG.mm,"_Par_",iARG.ex,".jl")) # Core parameters
# Load functions & parameters:
using DelimitedFiles
using Distributions
mm = include(string("Library\\Md_",iARG.mm,".jl"));
fn = include(string("Library\\FN_DYs.jl"));
pO = copy(p);
## Run analysis
# Calculate CoRa curve for a range of parameters:
if(iARG.an=="ExSSs")
p = copy(pO);
open(string("OUT_ExSSs_",iARG.mm,"_",iARG.ex,"_",iARG.pp,"_",iARG.ax,".txt"), "w") do io
writedlm(io, [vcat(iARG.ax,[string("FbR_",i) for i in mm.odeFB.syms],[string("FbD_",i) for i in mm.odeFB.syms],[string("NfR_",i) for i in mm.odeNF.syms],[string("NfD_",i) for i in mm.odeNF.syms],string("CoRa(",iARG.pp,")"))],'\t');
r = 10 .^ collect(pert.r[1]:pert.s:pert.r[2]);
for i in 1:length(r)
rtol = 1e-12;
uns = 0;
p[pert.c] *= r[i];
flg1 = 1;
ssR = ones(length(mm.odeFB.syms));
soR = ones(length(mm.odeNF.syms));
while(rtol >= 1e-24)
# Reference steady state:
ssR = fn.SS(mm.odeFB, p, ssR, rtol, uns);
# Locally analogous system reference steady state:
mm.localNF(p,ssR);
soR = fn.SS(mm.odeNF, p, soR, rtol, uns);
if(abs(mm.outFB(ssR) - mm.outNF(soR)) > 1e-4)
rtol *= 1e-3;
if(rtol < 1e-24)
println("ERROR: Check NF system (reltol=",rtol*1e3,").")
println(vcat(pert.p,i,[p[i] for i in mm.odeFB.params],mm.outFB(ssR),mm.outNF(soR)))
#throw(DomainError("x-("))
if(abs(mm.outFB(ssR) - mm.outNF(soR))/mm.outFB(ssR) > 0.01)
flg1 = 0;
println("SS results excluded!")
end
end
else
break
end
end
# Perturbation:
p[pert.p] *= pert.d;
ssD = fn.SS(mm.odeFB, p, ssR, rtol, uns);
soD = fn.SS(mm.odeNF, p, soR, rtol, uns);
DYs = fn.DY(mm.outFB(ssR), mm.outFB(ssD), mm.outNF(soR), mm.outNF(soD));
p[pert.p] /= pert.d;
writedlm(io, [vcat(p[pert.c],ssR,ssD,soR,soD,DYs)],'\t');
p[pert.c] /= r[i];
end
end
# Calculate dynamic response after a perturbation:
elseif(iARG.an=="ExDyn")
p = copy(pO);
open(string("OUT_ExDyn_",iARG.mm,"_",iARG.ex,"_",iARG.pp,"_",iARG.ax,".txt"), "w") do io
writedlm(io, [vcat("FB","rho","time",[string(i) for i in mm.odeNF.syms])],'\t');
rtol = 1e-12;
uns = 0;
ssR = ones(length(mm.odeFB.syms));
soR = ones(length(mm.odeNF.syms));
while(rtol >= 1e-24)
# Reference steady state:
ssR = fn.SS(mm.odeFB, p, ssR, rtol, uns);
# Locally analogous system reference steady state:
mm.localNF(p,ssR);
soR = fn.SS(mm.odeNF, p, soR, rtol, uns);
if(abs(mm.outFB(ssR) - mm.outNF(soR)) > 1e-4)
rtol *= 1e-3;
if(rtol < 1e-24)
println("ERROR: Check NF system (reltol=",rtol*1e3,").")
println(vcat(pert.p,i,[p[i] for i in mm.odeFB.params],mm.outFB(ssR),mm.outNF(soR)))
#throw(DomainError("x-("))
if(abs(mm.outFB(ssR) - mm.outNF(soR))/mm.outFB(ssR) > 0.01)
flg1 = 0;
println("SS results excluded!")
end
end
else
break
end
end
# Feedback system:
x = fn.Dyn(mm.odeFB, p, ssR, 500.0);
for i in 1:length(x.t)
writedlm(io, [vcat(1,p[iARG.pp],x.t[i],x.u[i],"NaN")],'\t');
end
p[pert.p] *= pert.d;
x = fn.Dyn(mm.odeFB, p, last(x.u), 9500.0);
for i in 1:length(x.t)
writedlm(io, [vcat(1,p[iARG.pp],x.t[i]+500.0,x.u[i],"NaN")],'\t');
end
ssD = fn.SS(mm.odeFB, p, ssR, rtol, uns);
writedlm(io, [vcat(1,p[iARG.pp],"Inf",ssD,"NaN")],'\t');
p[pert.p] /= pert.d;
# No-Feedback system:
x = fn.Dyn(mm.odeNF, p, soR, 500.0);
for i in 1:length(x.t)
writedlm(io, [vcat(0,p[iARG.pp],x.t[i],x.u[i])],'\t');
end
p[pert.p] *= pert.d;
x = fn.Dyn(mm.odeNF, p, last(x.u), 9500.0);
for i in 1:length(x.t)
writedlm(io, [vcat(0,p[iARG.pp],x.t[i]+500.0,x.u[i])],'\t');
end
soD = fn.SS(mm.odeNF, p, soR, rtol, uns);
writedlm(io, [vcat(0,p[iARG.pp],"Inf",soD)],'\t');
p[pert.p] /= pert.d;
end
# Calculate CoRa curve for a range of parameters as another parameter varies:
elseif(iARG.an=="DYms")
include(string("InputFiles\\ARGS_",iARG.mm,"_DYms_",iARG.ex,".jl")) # Parameters to vary
open(string("OUT_DYms_",iARG.mm,"_",iARG.ex,"_",iARG.pp,"_",iARG.ax,".txt"), "w") do io
writedlm(io, [vcat([string(i) for i in keys(pN)],10 .^ collect(pert.r[1]:pert.s:pert.r[2]))],'\t')
for pI = pN
for i = pI[2]
p = copy(pO);
uns = 0;
p[pI[1]] *= (10. ^i);
writedlm(io, [vcat([p[i[1]] for i in pN],fn.DYc(p,pert,mm,uns))],'\t')
p[pI[1]] /= (10. ^i);
end
end
end
# Optimize CoRa curve for a range of parameters:
elseif(iARG.an=="OptDY")
include(string("InputFiles\\ARGS_",iARG.mm,"_OptDY_",iARG.ex,".jl"))
open(string("OUT_OptDY_",iARG.mm,"_",iARG.ex,"_",iARG.pp,"_",iARG.ax,".txt"), "w") do io
if(mrw.prtD==1)
writedlm(io, [vcat("Run","Iteration",[string(param) for param in mrw.pOp],string("|DY<=",pert.eps,"|"),"min(DY)",10 .^ collect(pert.r[1]:pert.s:pert.r[2]))], '\t')
else
writedlm(io, [vcat("Run","Iteration",[string(param) for param in mrw.pOp],string("|DY<=",pert.eps,"|"),"min(DY)")], '\t')
end
for ruN in 1:mrw.runs
println("RUN #",ruN)
uns = 0;
p = copy(pO);
####### Uncomment the next three lines for random initial conditions: #######
#for i in 1:length(mrw.pOp)
# p[mrw.pOp[i]] = 10 .^ (rand(Uniform(mrw.pMin[i], mrw.pMax[i])));
#end
#############################################################################
## Temperature function for simulated annealing:
if(mrw.temp==1)
mrwT = collect(mrw.iter:-1:1) ./ mrw.iter;
else
mrwT = ones(mrw.iter); # NOTE: For MRW, make T=1.
end
## Initialize system
DY0 = fn.DYc(p,pert,mm,uns); # Calculate DY curve
DYm = fn.DYm(DY0, pert); # Calculate metrics of DY curve
op0 = log10(DYm[3]/DYm[2]); # Property to optimize (e.g. DY<=eps range length)
mi0 = DYm[4]; # Secondary property to optimize (e.g. min(DY) value)
r0 = zeros(length(mrw.pOp));
if(mrw.prtD==1)
writedlm(io, [vcat(ruN,0,[p[i] for i in mrw.pOp],op0,mi0,DY0)],'\t')
else
writedlm(io, [vcat(ruN,0,[p[i] for i in mrw.pOp],op0,mi0)],'\t')
end
# Optimization iterations
println("I: minDY = ",mi0,"\t |DY| = ",op0)
for i in 1:mrw.iter
rI = rand(MvNormal(zeros(length(mrw.pOp)), zeros(length(mrw.pOp)) .+ mrw.cov)); # Random values to update parameters
for pI in 1:length(mrw.pOp) # Update parameter values
r0[pI] = p[mrw.pOp[pI]]; # Save previous value
p[mrw.pOp[pI]] *= (mrw.M .^ rI[pI]); # Update value
# Exclude values outside regime of exploration:
if p[mrw.pOp[pI]] < (10.0 ^ mrw.pMin[pI])
p[mrw.pOp[pI]] = (10.0 ^ mrw.pMin[pI])
elseif p[mrw.pOp[pI]] > (10.0 ^ mrw.pMax[pI])
p[mrw.pOp[pI]] = (10.0 ^ mrw.pMax[pI])
end
end
DYs = fn.DYc(p,pert,mm,uns); # Calculate new DY curve
DYm = fn.DYm(DYs, pert); # Calculate new metrics of DY curve
op1 = log10(DYm[3]/DYm[2]); # New value of property to optimize (e.g. DY<=eps range length)
mi1 = DYm[4]; # New value of secondary property to optimize (e.g. min(DY) value)
# Evaluate if accept new parameter values or not:
## Only accept in the regime of interest, i.e. DY>=0:
c1 = (mi1>=0);
## If DY>eps for all rho, evaluate the min(DY) for both sets:
### NOTE: As mi0,mi1=[0,1], correct exponential with the expected variance of ~U(0,1)
xiC = (mi0 ^ 2) / (2 * 0.083);
xiP = (mi1 ^ 2) / (2 * 0.083);
c2 = isnan(op0+op1) && (rand() < exp((xiC - xiP) / mrwT[i]));
## If DY>=eps for some rho, evaluate the |DY<=eps| for both sets:
### NOTE: As op0,op1=[0,rrO], but still correct exponential with the expected variance of ~U(0,1)
### !! ~U(0,1)*(rrO^2) variance resulted in very noisy runs...
rrO = pert.r[2] - pert.r[1];
xiC = (rrO - op0) / (2 * 0.083);
xiP = (rrO - op1) / (2 * 0.083);
c3 = rand() < exp((xiC - xiP) / mrwT[i]);
if(c1 && (c2 || c3))
# If yes, update "reference" system
op0 = op1;
mi0 = mi1;
DY0 = DYs;
else
# If not, revert to previous parameter values
for pI in 1:length(mrw.pOp)
p[mrw.pOp[pI]] = r0[pI];
end
end
if(mrw.prtW==1 || i==mrw.iter)
if(mrw.prtD==1)
writedlm(io, [vcat(ruN,i,[p[i] for i in mrw.pOp],op0,mi0,DY0)],'\t')
else
writedlm(io, [vcat(ruN,i,[p[i] for i in mrw.pOp],op0,mi0)],'\t')
end
end
if(op1==rrO)
println("Optimal value (|DY<=eps|=",op1,") reached at iteration ",i)
if(mrw.prtD==1)
writedlm(io, [vcat(ruN,i,[p[i] for i in mrw.pOp],op0,mi0,DY0)],'\t')
else
writedlm(io, [vcat(ruN,i,[p[i] for i in mrw.pOp],op0,mi0)],'\t')
end
break;
end
end
println("F: minDY = ",mi0,"\t |DY| = ",op0,"\n")
end
end
else
println("ERROR: Undetermined analysis. Options: ExSSs, ExDyn, DYms, OptDY")
end