-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsettings.cpp
More file actions
282 lines (259 loc) · 7.2 KB
/
settings.cpp
File metadata and controls
282 lines (259 loc) · 7.2 KB
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
/*
* settings.cpp
* Selection_Recombination
*
* Created by Joshua Schraiber on 4/30/13.
* Copyright 2013 UC Berkeley. All rights reserved.
*
*/
#include "math.h"
#include "settings.h"
#include "param.h"
#include "MbRandom.h"
#include "popsize.h"
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <algorithm>
settings::settings(int argc, char* const argv[]) {
//define defaults
max_dt = 0.001;
min_grid = 10;
bridge = 0;
mcmc = 0;
linked_sites = 0;
num_gen = 100;
p = 0;
num_test = 1000;
rescale = -INFINITY;
printFreq = 1000;
sampleFreq = 1000;
fracOfPath = 20;
min_update = 10;
baseName = "out";
fOrigin = 0.0;
infer_age = 0;
popFile = "";
inputFile = "";
mySeed = time(0);
output_tsv = 0;
a1prop = 2.0;
a2prop = 2.0;
ageprop = 20.0;
endprop = 5.0;
timeprop = 0.1;
pathprop = 10.0;
a1start = 25.0;
a2start = 50.0;
set_gen = 0;
set_N0 = 0;
gen_time = 1;
N0 = 0.5;
h = 0.5;
fix_h = false;
min_freq = 0;
ascertain = false;
//read the parameters
int ac = 1;
while (ac < argc) {
switch(argv[ac][1]) {
case 'd':
max_dt = atof(argv[ac+1]);
ac += 2;
break;
case 'g':
min_grid = atoi(argv[ac+1]);
ac += 2;
break;
case 'n':
num_gen = atoi(argv[ac+1]);
ac += 2;
break;
case 'b':
bridge = 1;
bridge_pars = argv[ac+1];
ac += 2;
break;
case 'R':
output_tsv = 1;
ac += 1;
break;
case 'p':
p = 1;
ac += 1;
break;
case 'T':
num_test = atoi(argv[ac+1]);
ac += 2;
break;
case 'r':
rescale = atof(argv[ac+1]);
ac += 2;
break;
case 'f':
printFreq = atoi(argv[ac+1]);
ac += 2;
break;
case 'o':
baseName = std::string(argv[ac+1]);
ac += 2;
break;
case 's':
sampleFreq = atoi(argv[ac+1]);
ac += 2;
break;
case 'F':
fracOfPath = atoi(argv[ac+1]);
ac += 2;
break;
case 'M':
min_update = atoi(argv[ac+1]);
ac += 2;
break;
case 'O':
fOrigin = atof(argv[ac+1]);
ac += 2;
break;
case 'e':
mySeed = atoi(argv[ac+1]);
ac += 2;
break;
case 'a':
infer_age = 1;
ac += 1;
break;
case 'P':
popFile = argv[ac+1];
ac += 2;
break;
case 'G':
set_gen = 1;
gen_time = atoi(argv[ac+1]);
ac += 2;
break;
case 'N':
set_N0 = 1;
N0 = atoi(argv[ac+1]);
ac += 2;
break;
case '0':
a1prop = 0.0;
a2prop = 0.0;
a1start = 0.0;
a2start = 0.0;
ac += 1;
break;
case 'h':
h = atof(argv[ac+1]);
a1prop = 0.0;
a1start = a2start*h;
fix_h = true;
ac += 2;
break;
case 'D':
mcmc = 1;
inputFile = argv[ac+1];
ac += 2;
break;
case 'A':
ascertain = true;
min_freq = atof(argv[ac+1]);
ac += 2;
break;
}
}
}
std::vector<double> settings::parse_bridge_pars() {
std::vector<double> pars(0);
std::string string_pars(bridge_pars);
std::istringstream stringstream_pars(string_pars);
std::string cur_par;
while (std::getline(stringstream_pars,cur_par,',')) {
pars.push_back(atof(cur_par.c_str()));
}
if (pars.size() < 4) {
std::cout << "ERROR: Not enough bridge parameters" << std::endl;
std::cout << "Only " << pars.size() << " specified; 4 are required" << std::endl;
exit(1);
}
return pars;
}
void settings::print() {
std::cout << "max_dt\t" << max_dt << std::endl;
std::cout << "min_grid\t" << min_grid << std::endl;
if (bridge) {
std::cout << "num_test\t" << num_test << std::endl;
std::cout << "rescale\t" << rescale << std::endl;
std::vector<double> pars = parse_bridge_pars();
std::cout << "x0\t" << pars[0] << std::endl;
std::cout << "xt\t" << pars[1] << std::endl;
std::cout << "gamma\t" << pars[2] << std::endl;
std::cout << "t\t" << pars[3] << std::endl;
} else if (mcmc) {
std::cout << "num_gen\t" << num_gen << std::endl;
if (linked_sites) {
//file destinations
} else {
//parameters of the path
}
}
}
popsize* settings::parse_popsize_file() {
if (popFile == "") {
std::cout << "ERROR: No population size history specified! Use -P option" << std::endl;
exit(1);
}
if (set_gen && !set_N0) {
std::cout << "Specified a generation time but not a base population size. Unless your times are measured in units of 2N0 years, this is likely an error" << std::endl;
} else if (!set_gen && set_N0) {
std::cout << "Specified base population size but not a generation time. Assuming times are measured in generations, converting all units to 2N0 generations" << std::endl;
} else if (set_gen && set_N0) {
std::cout << "Specified both generation time and base population size. Assuming times are measured in years, converting all units to 2N0 generations" << std::endl;
} else {
std::cout << "Did not specify either generation time or base population size. Assuming times are in units of 2N0 generations" << std::endl;
}
//make pop sizes
return new popsize(*this);
}
bool comparePtrToSampleTime(sample_time* a, sample_time* b) { return (*a < *b); }
std::vector<sample_time*> settings::parse_input_file(MbRandom* r) {
std::cout << "Parsing input" << std::endl;
std::ifstream inFile(inputFile.c_str());
std::string curLineString;
int curCount;
int curSS;
double curLowTime;
double curHighTime;
double curTime;
std::vector<sample_time*> sample_time_vec;
while (getline(inFile, curLineString)) {
std::istringstream curLine(curLineString);
curLine >> curCount >> curSS >> curLowTime >> curHighTime;
if (curCount < 0 || curCount > curSS) {
std::cout << "Allele count is not between 0 and sample size: X = " << curCount << ", SS = " << curSS << std::endl;
exit(1);
}
if (curLowTime > curHighTime) {
std::cout << "Low end of time range higher than high end: t_low = " << curLowTime << ", t_high = " << curHighTime << std::endl;
exit(1);
}
//Convert time units
curLowTime /= (gen_time*2*N0);
curHighTime /= (gen_time*2*N0);
//Set the sample time randomly to initialize
curTime = r->uniformRv(curLowTime, curHighTime);
sample_time* cur_sample_time = new sample_time(curTime, curLowTime, curHighTime, curSS, curCount, r);
sample_time_vec.push_back(cur_sample_time);
}
//Sort so in order by current value
std::sort(sample_time_vec.begin(), sample_time_vec.end(),comparePtrToSampleTime);
//check that most recent time point is fixed
int num_sam = sample_time_vec.size();
if (sample_time_vec[num_sam-1]->get_oldest()<sample_time_vec[num_sam-1]->get_youngest()) {
std::cout << "ERROR: most recent time point must have no uncertainty" << std::endl;
exit(1);
}
return sample_time_vec;
}