-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
418 lines (357 loc) · 9 KB
/
main.cc
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include "main.h"
using namespace std;
using namespace alglib;
void usage() {
cout << "Program: " << PROGRAM << " " << REMARKS << "\n"
"Version: " << VERSION << "\n"
"Authors: " << AUTHORS << "\n"
"Contact: " << CONTACT << "\n\n"
"Options: " << PROGRAM << "\n"
"\n"
"\n";
exit(1);
}
// pois + pois_dis 2var
// log_likelihood() of mu and phi given Nb, Z, and vector of y>0
//
double llh(
const double &N,
const double &Z,
const double &precision,
const vector<double> &y,
const double &m,
const double &p
)
{
double lambda = Z/m/N;
return llh_3var(N, precision, y, lambda, m, p);
}
// pois_dis + pois 2var
// log_likelihood() of mu and phi given Nb, Z, and vector of y>0 ; prob_y2
//
double llh2(
const double &N,
const double &Z,
const double &precision,
const vector<double> &y,
const double &m,
const double &p
)
{
double lambda = Z/m/N;
return llh2_3var(N, precision, y, lambda, m, p);
}
// pois + pois_dis 3var
// log_likelihood() of lambda mu and phi
//
double llh_3var(
const double &N,
const double &precision,
const vector<double> &y,
const double &lambda,
const double &m,
const double &p
)
{
double llh(0);
if (DEBUG)
cout << "~~" << m << ' ' << p << endl;
for ( size_t i(0); i != y.size(); i++ ) { // elements in y are > 0
llh += log( prob_y(lambda, m, p, y[i], precision) );
}
llh += ( N - y.size() ) * prob_y(lambda, m, p, 0.0, precision);
return llh;
}
// pois_dis + pois 3var
// log_likelihood() of lambda mu and phi
//
double llh2_3var(
const double &N,
const double &precision,
const vector<double> &y,
const double &lambda,
const double &m,
const double &p
)
{
double llh(0);
if (DEBUG)
cout << "~~" << m << ' ' << p << endl;
for ( size_t i(0); i != y.size(); i++ ) { // elements in y are > 0
llh += log( prob_y2(lambda, m, p, y[i], precision) );
}
llh += ( N - y.size() ) * prob_y2(lambda, m, p, 0.0, precision);
return llh;
}
// pois + pois
// log_likelihood() of lambda mu and phi
//
double llh_2lambda (
const double &N,
const double &precision,
const vector<double> &y,
const double &lambda,
const double &lam2
)
{
double llh(0);
for ( size_t i(0); i != y.size(); i++ ) { // elements in y are > 0
llh += log( prob_y_2lambda(lambda, lam2, y[i], precision) );
}
llh += ( N - y.size() ) * prob_y_2lambda(lambda, lam2, 0.0, precision);
return llh;
}
// pois + pois_dis
// prob of observe y reads in the family, given 3var: lambda + mu phi
//
double prob_y(
const double &lambda,
const double &m,
const double &p,
const double &y,
const double &precision
)
{
double pr(0.0);
double mp = m * p;
if ( y == 0.0 ) pr += exp(-lambda);
double target_k = int(y / m);
double start_k = ( target_k - 30 <= 1.0 ? 1.0 : target_k - 30 );
for ( double k(start_k); k < target_k + 30; k++ ) {
double tmp = -k*log(1+mp)/p + k*log(lambda) - lambda - lgamma(k+1);
if ( y != 0.0 )
tmp += lgamma(y+k/p) - lgamma(k/p) -lgamma(y+1) - y*log(1+1/mp);
pr += exp(tmp);
}
return pr;
}
// pois_dis + pois
// prob of observe y reads in the family, given 3var: mu phi + lambda
//
double prob_y2(
const double &lambda,
const double &m,
const double &p,
const double &y,
const double &precision
)
{
double pr(0.0);
double mp = m * p;
if ( y == 0.0 ) pr += pow( 1/(1+mp), 1/p );
double target_k = int(y / lambda);
double start_k = ( target_k - 30 <= 1.0 ? 1.0 : target_k - 30 );
for ( double k(start_k); k < target_k + 30; k++ ) {
double tmp = -k*lambda + y*log(k*lambda) - lgamma(y+1)
+ lgamma(k+1/p) - lgamma(1/p) - lgamma(k+1) - log(1+mp)/p - k*log(1+1/mp);
pr += exp(tmp);
}
return pr;
}
// pois + pois
// prob of observe y reads in the family, given 2var: lambda lam2
//
double prob_y_2lambda(
const double &lambda,
const double &lam2,
const double &y,
const double &precision
)
{
double pr(0.0);
if ( y == 0.0 ) pr += exp(-lambda);
double target_k = int(y / lam2);
double start_k = ( target_k - 30 <= 1.0 ? 1.0 : target_k - 30 );
for ( double k(start_k); k < target_k + 30; k++ ) {
double tmp = -k*lam2 + k*log(lambda) - lambda - lgamma(k+1);
if ( y != 0.0 )
tmp += y*log(k*lam2) -lgamma(y+1);
pr += exp(tmp);
}
return pr;
}
// pois + pois_dis
// given readN y in a bin, return prob that has k fragments
//
double prob_k_given_y (
const double &lambda,
const double &m,
const double &p,
const double &y,
const double &precision,
const double &k
)
{
double mp = m * p;
if ( k == 0 ) {
return ( y == 0 ? exp(-lambda)/prob_y(lambda, m, p, y, precision) : 0.0 );
}
else if ( k > 0 ) {
double tmp = -k*log(1+mp)/p + k*log(lambda) - lambda - lgamma(k+1);
if ( y != 0.0 )
tmp += lgamma(y+k/p) - lgamma(k/p) -lgamma(y+1) - y*log(1+1/mp);
return exp(tmp)/prob_y(lambda, m, p, y, precision);
}
else {
cerr << "k can not < 0" << endl;
exit(1);
}
}
// pois_dis + pois
// given readN y in a bin, return prob that has k fragments
//
double prob_k_given_y2 (
const double &lambda,
const double &m,
const double &p,
const double &y,
const double &precision,
const double &k
)
{
double mp = m * p;
if ( k == 0 ) {
return ( y == 0 ? pow(1/(1+mp), 1/p) /prob_y2(lambda, m, p, y, precision) : 0.0 );
}
else if ( k > 0 ) {
double tmp = -k*lambda + y*log(k*lambda) - lgamma(y+1)
+ lgamma(k+1/p) - lgamma(1/p) - lgamma(k+1) - log(1+mp)/p - k*log(1+1/mp);
return exp(tmp)/prob_y2(lambda, m, p, y, precision);
}
else {
cerr << "k can not < 0" << endl;
exit(1);
}
}
// pois + pois
// given readN y in a bin, return prob that has k fragments
//
double prob_k_given_y_2lambda (
const double &lambda,
const double &lam2,
const double &y,
const double &precision,
const double &k
)
{
if ( k == 0 ) {
return ( y == 0 ? exp(-lambda)/prob_y_2lambda(lambda, lam2, y, precision) : 0.0 );
}
else if ( k > 0 ) {
double tmp = -k*lam2 + k*log(lambda) - lambda - lgamma(k+1);
if ( y != 0.0 )
tmp += y*log(k*lam2) -lgamma(y+1);
return exp(tmp)/prob_y_2lambda(lambda, lam2, y, precision);
}
else {
cerr << "k can not < 0" << endl;
exit(1);
}
}
double p_k_given_y_2lambda (
const double &lambda,
const double &lam2,
const double &y,
const double &precision,
const double &k
)
{
if ( k == 0 ) {
return ( y == 0 ? exp(-lambda) : 0.0 );
}
else if ( k > 0 ) {
double tmp = -k*lam2 + k*log(lambda) - lambda - lgamma(k+1);
if ( y != 0.0 )
tmp += y*log(k*lam2) -lgamma(y+1);
return exp(tmp);
}
else {
cerr << "k can not < 0" << endl;
exit(1);
}
}
// pois + pois_dis 2var
void LogLikelihoodFunc (
const real_1d_array &x,
double &func,
void *opt_data
)
{
fn_data* obj = reinterpret_cast<fn_data*>(opt_data);
// minimize func == maximize llh
func = -llh(obj->N, obj->Z, obj->precision, obj->y, x[0], x[1]);
}
// pois_dis + pois 2var
void LogLikelihoodFunc2 (
const real_1d_array &x,
double &func,
void *opt_data
)
{
fn_data* obj = reinterpret_cast<fn_data*>(opt_data);
// minimize func == maximize llh
func = -llh2(obj->N, obj->Z, obj->precision, obj->y, x[0], x[1]);
}
// pois + pois_dis 3var
void LogLikelihoodFunc_3var (
const real_1d_array &x,
double &func,
void *opt_data
)
{
fn_data* obj = reinterpret_cast<fn_data*>(opt_data);
func = -llh_3var(obj->N, obj->precision, obj->y, x[0], x[1], x[2]);
}
// pois_dis + pois 3var
void LogLikelihoodFunc2_3var (
const real_1d_array &x,
double &func,
void *opt_data
)
{
fn_data* obj = reinterpret_cast<fn_data*>(opt_data);
func = -llh2_3var(obj->N, obj->precision, obj->y, x[0], x[1], x[2]);
}
// pois + pois 2var
void LogLikelihoodFunc_2lambda (
const real_1d_array &x,
double &func,
void *opt_data
)
{
fn_data* obj = reinterpret_cast<fn_data*>(opt_data);
// minimize func == maximize llh
func = -llh_2lambda(obj->N, obj->precision, obj->y, x[0], x[1]);
}
// pois + pois 1var
// diff = P(Y=0) = Nempty/Nb, given lambda2 = Z/Nb/lambda
// return diff^2, to find lambda to minimize func
//
void diff_prob_y0_2lambda (
const real_1d_array &x,
double &func,
void *opt_data
)
{
fn_data* obj = reinterpret_cast<fn_data*>(opt_data);
double diff = prob_y_2lambda(x[0], obj->Z/obj->N/x[0], 0.0, obj->precision)
-( obj->N - obj->y.size() )/obj->N;
func = pow(diff, 2);
}
// probability that a mutation with frequency 'f', can not be detected in a read family with size 's'
//
double notDetect(
const double &f,
const double &s
)
{
if ( f == 1.0 ) return 0.0;
double lnF = log(f);
double lnC = log(1-f);
return (
exp( s*lnC )
+ exp( log(s) + lnF + (s-1)*lnC )
+ exp( log(0.5) + log(s) + log(s-1) + 2*lnF + (s-2)*lnC )
+ exp( log(0.005) + log(s) + log(s-1) + log(s-2) + 3*lnF + (s-3)*lnC )
);
}