-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextras.h
executable file
·323 lines (287 loc) · 11 KB
/
extras.h
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
//--------------------------------------------------------------------------
inline double GRand(double Sigma) {
return Sigma * sqrt(-2*log(RGauss())) * cos(2*M_PI*RGauss());
}
//--------------------------------------------------------------------------
double Gaussian(double Sigma){
float x1, x2, w, y1, y2;
if(Sigma < 10e-10) return 0.;
do {
x1 = (2.0 * RNDF()) - 1.0;
x2 = (2.0 * RNDF()) - 1.0;
w = (x1 * x1) + (x2 * x2);
} while ( w >= 1.0 );
w = sqrt((-2.0 * log(w))/w);
y1 = x1 * w;
y2 = x2 * w;
return Sigma * y1;
}
//--------------------------------------------------------------------------
double ExtNoiseGaussian(double sigma){
double gr = Gaussian(sigma);
const double CUTOFF = 0.4;
while(abs(gr) > CUTOFF ) {
gr = Gaussian(sigma); //Assert %noise < %CUTOFF
}
return 1. + gr; // %extrinsic noise level
}
//--------------------------------------------------------------------------
double ExtNoise(double sigma){
double gr = (RNDF(2*sigma) - sigma);
return 1. + gr; // %extrinsic noise level
}
//--------------------------------------------------------------------------
class Parameters {
public:
psimag::KISS::seed_type SEED;
unsigned GENERATIONS;
unsigned POPSIZE;
unsigned FITNESSAVGOVER;
double MuReacRate, MuNewTFDNA, MuNewDimer;
double runtime;
double FitnessParam; // I implemented this for timing. No specific purpose.
int FitnessParam2; // Another parameter to specify the fitness function.
double SharpnessWeight, TimingWeight;
// Refer to the definition in scoring.h for usage.
double MaxRate, MinRate, MaxRndRate, MinRndRate;
double stepsize;
double lognoisemax; // lognoise is produced as 10^r. r is a rnd number on [-max, max]
double sigma; // Stdev of the random Gaussian distr for extrinsic noise.
// sigma>0 implies cell to cell variability
bool stochastic; // ODE or Gillespie? (Intrinsic noise)
bool extrinsicnoise; // Vary rates of transcription? (works with both ODE and Gillespie)
bool detailedlogs; // Write out mutatables after every time step?
bool usesic1forfitness; // Use Sic1 sharpest degr. for timing and sharpness. False uses Clb.
bool startiszero; // Take t=0 as START.
int randomize; // randomize: 0=none, 1=only mutatable reaction rates, 2=all rates
unsigned smoothingrange;// #of elements to use in moving average.
bool noscoring;
string FileNameTemplate;
vector <int> ReactantsToRecord, CSPcols, SIC1cols, CLNcol, fCLBcol;
vector <int> RatesToMutate;
vector <string> ReactantsToRecordNames;
string FitnessFunc, parameterstrlong, parameterstrshort;
map< string, int > ReactantTypes;
double MaxSynthRate, MaxDNATFAssocDissocRate;
char reactionfile[300], reactantfile[300];
char exec[200];
Parameters(){
SEED=12345678;
GENERATIONS = 1;
POPSIZE = 2;
FITNESSAVGOVER = 1;
FitnessParam = 35; // Desired timing in Clb activation
FitnessParam2= 0; // Use both timing and sharpness
MuReacRate = 0.3; // Mutation rate of reaction rates
MuNewTFDNA = 0.3; MuNewDimer = 0.3;
MaxSynthRate = 0.1;
runtime = 50; //Integration duration of ODEs
stepsize = .25; //Report concentrations in large timesteps
MaxDNATFAssocDissocRate = 1;
strcpy(reactantfile, "reactants.dat");
strcpy(reactionfile, "reactions.dat");
FileNameTemplate = "output";
usesic1forfitness = false;
stochastic = false;
extrinsicnoise = false;
detailedlogs = false;
randomize = 0;
sigma = 0.0;
lognoisemax = 1.0;
TimingWeight=1.;
SharpnessWeight=1.;
startiszero=false;
MaxRate=MaxRndRate=MAXRATE;
MinRate=MinRndRate=MINRATE;
smoothingrange=10;
noscoring=false;
ReactantTypes["GENE"] = GENE;
ReactantTypes["PROTEIN"] = PROTEIN;
ReactantTypes["PCOMPLEX"] = PCOMPLEX;
ReactantTypes["PGCOMPLEX"] = PGCOMPLEX;
ReactantTypes["NOISYPROTEIN"] = NOISYPROTEIN;
}
double RandomSynthRate() {
return MaxSynthRate*RNDF();
}
double RandomDNATFAssocDissocRate() {
return MaxDNATFAssocDissocRate*RNDF();
}
double RandomReactionRate() {
double range, exp, result;
//log sampling
range = log10(MaxRndRate) - log10(MinRndRate);
exp = RNDF()*range; //Sample between 10^-6 and 1
result = MaxRndRate*pow(10, -exp);
//cout << "range: " << range << ", exp:" << exp << ", result" << result << endl;
if(result>MaxRate) result=MaxRate;
if(result<MinRate) result=MinRate;
return result;
}
void Assign(string variable, string value) {
boost::to_lower(variable);
if(variable == "generations")
GENERATIONS = boost::lexical_cast<unsigned>(value);
else if(variable == "fitnessavgover")
FITNESSAVGOVER = boost::lexical_cast<unsigned>(value);
else if(variable == "popsize")
POPSIZE = boost::lexical_cast<unsigned>(value);
else if(variable == "seed")
SEED = (psimag::KISS::seed_type) boost::lexical_cast<int>(value);
else if(variable == "runtime")
runtime = boost::lexical_cast<double>(value);
else if(variable == "stochastic")
stochastic = boost::lexical_cast<bool>(value);
else if(variable == "extrinsicnoise")
extrinsicnoise = boost::lexical_cast<bool>(value);
else if(variable == "detailedlogs")
detailedlogs = boost::lexical_cast<bool>(value);
else if(variable == "sigma")
sigma = boost::lexical_cast<double>(value);
else if(variable == "randomize")
randomize = boost::lexical_cast<unsigned>(value);
else if(variable == "fitnessparam")
FitnessParam = boost::lexical_cast<double>(value);
else if(variable == "fitnessparam2")
FitnessParam2 = boost::lexical_cast<int>(value);
else if(variable == "mureacrate"){
MuReacRate = boost::lexical_cast<double>(value);
assert(MuReacRate >= 0 && MuReacRate <= 1);
}
else if(variable == "lognoisemax"){
lognoisemax = boost::lexical_cast<double>(value);
assert(lognoisemax >= 0);
}
else if(variable == "sharpnessweight"){
SharpnessWeight = boost::lexical_cast<double>(value);
assert(SharpnessWeight >= 0.);
}
else if(variable == "timingweight"){
TimingWeight = boost::lexical_cast<double>(value);
assert(TimingWeight >= 0.);
}
else if(variable == "usesic1forfitness"){
usesic1forfitness = boost::lexical_cast<bool>(value);
}
else if(variable == "startiszero"){
startiszero = boost::lexical_cast<bool>(value);
}
else if(variable == "maxrate")
MaxRate = boost::lexical_cast<double>(value);
else if(variable == "minrate")
MinRate = boost::lexical_cast<double>(value);
else if(variable == "maxrndrate")
MaxRndRate = boost::lexical_cast<double>(value);
else if(variable == "minrndrate")
MinRndRate = boost::lexical_cast<double>(value);
else if(variable == "noscoring")
noscoring = boost::lexical_cast<bool>(value);
else if(variable == "smoothingrange")
smoothingrange = boost::lexical_cast<unsigned>(value);
else MyErr("Argument not recognized");
}
};
Parameters p;
//********************************************************************************************
void WriteParameters() {
char filename[150];
sprintf(filename, "%s-parameters.prm", p.FileNameTemplate.c_str());
ofstream file (filename);
ostringstream oss;
time_t rawtime;
time ( &rawtime );
oss << "# Date & Time = " << ctime (&rawtime);
oss << "# Executable = " << p.exec << endl;
oss << "# Reactant File = " << p.reactantfile << endl;
oss << "# Reaction File = " << p.reactionfile << endl;
oss << "# SEED = " << p.SEED << endl;
oss << "# Runtime = " << p.runtime << endl;
oss << "# GENERATIONS = " << p.GENERATIONS << endl;
oss << "# POPSIZE = " << p.POPSIZE << endl;
oss << "# stepsize = " << p.stepsize << endl;
oss << "# stochastic? " << p.stochastic << endl;
oss << "# randomize = " << p.randomize << endl;
oss << "# extrinsic noise? " << p.extrinsicnoise << endl;
oss << "# Sigma (for extr. noise) = " << p.sigma << endl;
oss << "# lognoisemax (for reaction class 2) = " << p.lognoisemax << endl;
oss << "# \n";
oss << "# Fitness Func. = " << p.FitnessFunc << endl;
oss << "# Fitness Param = " << p.FitnessParam << endl;
oss << "# Fitness Param2 = " << p.FitnessParam2 << endl;
oss << "# Use Sic1 Sharpest Degr. for Fitness = " << p.usesic1forfitness << endl;
oss << "# SharpnessWeight = " << p.SharpnessWeight << endl;
oss << "# TimingWeight = " << p.TimingWeight << endl;
oss << "# MaxRate = " << p.MaxRate << endl;
oss << "# MinRate = " << p.MinRate << endl;
oss << "# smoothingrange = " << p.smoothingrange << endl;
oss << "# MuReacRate = " << p.MuReacRate <<endl;
oss << "# MuNewTFDNA = " << p.MuNewTFDNA << endl;
oss << "# MuNewDimer = " << p.MuNewDimer << endl;
oss << "# MaxSynthRate = " << p.MaxSynthRate << endl;
oss << "# MaxDNATFAssocDissocRate = " << p.MaxDNATFAssocDissocRate << endl;
p.parameterstrlong = oss.str();
//cout << oss.str();
file << oss.str();
file.close();
}
//********************************************************************************************
string ShortParameterString() {
ostringstream oss;
//Construct the short version here:
oss << "Fitness Func. = " << p.FitnessFunc << endl;
oss << "GENERATIONS = " << p.GENERATIONS << endl;
oss << "POPSIZE = " << p.POPSIZE << endl;
oss << "MuReacRate = " << p.MuReacRate <<endl;
oss << "MuNewTFDNA = " << p.MuNewTFDNA << endl;
oss << "MuNewDimer = " << p.MuNewDimer << endl;
oss << "Sigma = " << p.sigma << endl;
oss << "Extrinsic noise? " << p.extrinsicnoise << endl;
p.parameterstrshort = oss.str();
return p.parameterstrshort;
}
//**************************************************************************************************
void CmdLineParser(int argc, char *argv[]) {
char msg[]=
"Need at least 2 arguments: reactantfile reactionfile VARIABLE=VALUE\n \
Allowed variables are GENERATIONS, POPSIZE, SEED, sigma, extrinsicnoise, mureacrate, lognoisemax, randomize";
boost::regex pat("([\\w]+)[=:]([\\w\\d\\.]+)");
if(argc < 3) MyErr(msg);
strcpy(p.reactantfile, argv[1]);
strcpy(p.reactionfile, argv[2]);
boost::regex pat2("(.+?)(\\.[^.]*$|$)"); //match filename and extension
boost::smatch matches;
string f(p.reactionfile);
if (boost::regex_match(f, matches, pat2)) { //remove the extension in the filename
p.FileNameTemplate = matches[1];
}
strcpy(p.exec, argv[0]);
for(int i = 3; i < argc; i++){
boost::smatch matches;
string argument = argv[i];
if(boost::regex_match(argument, matches, pat)) {
p.Assign(matches[1], matches[2]); //assign variable the value
}
}
WriteParameters();
}
//**************************************************************************************************
void GetReactantNamesForSummation() {
int count=1; //Start from 1 because the column 0 is time.
boost::smatch matches;
boost::regex patCSP("CSP.+");
boost::regex patSIC(".+SIC1.+");
boost::regex patCLN("CLN");
boost::regex patfCLB("fCLB");
p.CSPcols.clear();
foreach(string &n, p.ReactantsToRecordNames) {
if(boost::regex_match(n, matches, patCSP))
p.CSPcols.push_back(count);
if(boost::regex_match(n, matches, patSIC))
p.SIC1cols.push_back(count);
if(boost::regex_match(n, matches, patCLN))
p.CLNcol.push_back(count);
if(boost::regex_match(n, matches, patfCLB))
p.fCLBcol.push_back(count);
count++;
}
}