-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompute_case_I.cpp
121 lines (104 loc) · 3.02 KB
/
Compute_case_I.cpp
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
#include "Combinatorics.h"
using namespace std;
class InputParser{
public:
InputParser (int &argc, char **argv){
for (int i=1; i < argc; ++i)
this->tokens.push_back(std::string(argv[i]));
}
std::string getCmdOption(const std::string &option) {
std::vector<std::string>::iterator itr;
itr = std::find(this->tokens.begin(), this->tokens.end(), option);
if (itr != this->tokens.end() && ++itr != this->tokens.end()){
return *itr;
}
static std::string empty_string("");
return empty_string;
}
bool cmdOptionExists(const std::string &option) const{
return std::find(this->tokens.begin(), this->tokens.end(), option)
!= this->tokens.end();
}
private:
std::vector <std::string> tokens;
};
int main(int argc, char **argv) {
int n = 1e3, maxac, k;
string ntmp, tfile1, tfile2, outfile, prefix;
InputParser input(argc, argv);
ntmp = input.getCmdOption("-n");
if (!ntmp.empty())
n = (int) stof(ntmp);
ntmp = input.getCmdOption("-maxac");
if (!ntmp.empty())
maxac = stoi(ntmp);
tfile1 = input.getCmdOption("-tf1");
tfile2 = input.getCmdOption("-tf2");
prefix = input.getCmdOption("-prefix");
if (prefix.empty())
prefix = "./test";
// Read coalescent time
vector<double> tk1(n, 0);
vector<double> tk2(n+1, 0);
ifstream rf;
rf.open(tfile1, ifstream::in);
double tk = 0.0;
int i = 0;
while (rf >> tk) {
tk1[i] = tk;
i++;
}
rf.close();
rf.open(tfile2, ifstream::in);
i = 0;
while (rf >> tk) {
tk2[i] = tk;
i++;
}
while (i < n+1) {
tk2[i] = tk;
i++;
}
rf.close();
cout << "Finish reading time\n";
double **fjkmat;
fjkmat = FJK(maxac, n);
cout << "Finishe computing f(j,k;n)\n";
vector<double> res(maxac+1,0.0);
vector<double> up(maxac+1,0.0);
vector<double> down(maxac+1,0.0);
for (k = 2; k <= n - maxac + 1; ++k) {
double up_in = 0.0, down_in = 0.0;
for (int d = 2; d < k; ++d) {
up_in += d * (d-1) * (tk2[d+1] - tk2[k+1]);
down_in += (d-1) * (tk1[d-1] - tk1[k]);
}
for (int j = 1; j <= maxac; ++j) {
up[j] += up_in * exp(fjkmat[j][k]);
down[j] += down_in * exp(fjkmat[j][k]);
}
}
res[maxac] = up[maxac] / down[maxac] / ((double) (n*(n+1)));
cout << maxac << '\t' << up[maxac] << '\t' << down[maxac] << '\t' << res[maxac] << '\n';
for (int ac = (maxac-1); ac > 0; --ac) {
k = n - ac + 1;
double up_in = 0.0, down_in = 0.0;
for (int d = 2; d < k; ++d) {
up_in += d * (d-1) * (tk2[d+1] - tk2[k+1]);
down_in += (d-1) * (tk1[d-1] - tk1[k]);
}
for (int j = 1; j <= ac; ++j) {
up[j] += up_in * exp(fjkmat[j][k]);
down[j] += down_in * exp(fjkmat[j][k]);
}
res[ac] = up[ac] / down[ac] / ((double) (n*(n+1)));
cout << ac << '\t' << up[ac] << '\t' << down[ac] << '\t' << res[ac] << '\n';
}
outfile = prefix + "_ProbWrong.txt";
ofstream sfile;
sfile.open(outfile);
for (int j = 0; j <= maxac; ++j) {
sfile << j << '\t' << res[j] << endl;
}
sfile.close();
}