Debian Codegraph @

bcftools/prob1.c

raw symbols SCIP
1 /* prob1.c -- mathematical utility functions.
2
3 Copyright (C) 2010, 2011 Broad Institute.
4 Copyright (C) 2012, 2013-2014, 2017 Genome Research Ltd.
5
6 Author: Heng Li <[email protected]>
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE. */
25
26 #include <math.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <assert.h>
32 #include <limits.h>
33 #include "prob1.h"
34
35 #define MC_MAX_EM_ITER 16
36 #define MC_EM_EPS 1e-5
37 #define MC_DEF_INDEL 0.15
38
39 void bcf_p1_indel_prior(bcf_p1aux_t *ma, double x)
40 {
41 int i;
42 for (i = 0; i < ma->M; ++i)
43 ma->phi_indel[i] = ma->phi[i] * x;
44 ma->phi_indel[ma->M] = 1. - ma->phi[ma->M] * x;
45 }
46
47 static void init_prior(int type, double theta, int M, double *phi)
48 {
49 int i;
50 if (type == MC_PTYPE_COND2) {
51 for (i = 0; i <= M; ++i)
52 phi[i] = 2. * (i + 1) / (M + 1) / (M + 2);
53 } else if (type == MC_PTYPE_FLAT) {
54 for (i = 0; i <= M; ++i)
55 phi[i] = 1. / (M + 1);
56 } else {
57 double sum;
58 for (i = 0, sum = 0.; i < M; ++i)
59 sum += (phi[i] = theta / (M - i));
60 phi[M] = 1. - sum;
61 }
62 }
63
64 void bcf_p1_init_prior(bcf_p1aux_t *ma, int type, double theta)
65 {
66 init_prior(type, theta, ma->M, ma->phi);
67 bcf_p1_indel_prior(ma, MC_DEF_INDEL);
68 }
69
70 void bcf_p1_init_subprior(bcf_p1aux_t *ma, int type, double theta)
71 {
72 if (ma->n1 <= 0 || ma->n1 >= ma->M) return;
73 init_prior(type, theta, 2*ma->n1, ma->phi1);
74 init_prior(type, theta, 2*(ma->n - ma->n1), ma->phi2);
75 }
76
77
78 /* Initialise a bcf_p1aux_t */
79 bcf_p1aux_t *bcf_p1_init(int n_smpl, uint8_t *ploidy)
80 {
81 bcf_p1aux_t *ma;
82 int i;
83 ma = (bcf_p1aux_t*) calloc(1, sizeof(bcf_p1aux_t));
84 ma->n1 = -1;
85 ma->n = n_smpl;
86 ma->M = 2 * n_smpl;
87 if (ploidy) {
88 ma->ploidy = (uint8_t*) malloc(n_smpl);
89 memcpy(ma->ploidy, ploidy, n_smpl);
90 for (i = 0, ma->M = 0; i < n_smpl; ++i) ma->M += ploidy[i];
91 if (ma->M == 2 * n_smpl) {
92 free(ma->ploidy);
93 ma->ploidy = 0;
94 }
95 }
96 ma->q2p = (double*) calloc(256, sizeof(double));
97 ma->pdg = (double*) calloc(3 * ma->n, sizeof(double));
98 ma->phi = (double*) calloc(ma->M + 1, sizeof(double));
99 ma->phi_indel = (double*) calloc(ma->M + 1, sizeof(double));
100 ma->phi1 = (double*) calloc(ma->M + 1, sizeof(double));
101 ma->phi2 = (double*) calloc(ma->M + 1, sizeof(double));
102 ma->z = (double*) calloc(ma->M + 1, sizeof(double));
103 ma->zswap = (double*) calloc(ma->M + 1, sizeof(double));
104 ma->z1 = (double*) calloc(ma->M + 1, sizeof(double)); // actually we do not need this large
105 ma->z2 = (double*) calloc(ma->M + 1, sizeof(double));
106 ma->afs = (double*) calloc(ma->M + 1, sizeof(double));
107 ma->afs1 = (double*) calloc(ma->M + 1, sizeof(double));
108 ma->lf = (double*) calloc(ma->M + 1, sizeof(double));
109 for (i = 0; i < 256; ++i)
110 ma->q2p[i] = pow(10., -i / 10.);
111 for (i = 0; i <= ma->M; ++i) ma->lf[i] = lgamma(i + 1);
112 bcf_p1_init_prior(ma, MC_PTYPE_FULL, 1e-3); // the simplest prior
113 return ma;
114 }
115
116 int bcf_p1_get_M(bcf_p1aux_t *b) { return b->M; }
117
118 int bcf_p1_set_n1(bcf_p1aux_t *b, int n1)
119 {
120 if (n1 == 0 || n1 >= b->n) return -1;
121 if (b->M != b->n * 2) {
122 fprintf(stderr, "[%s] unable to set `n1' when there are haploid samples.\n", __func__);
123 return -1;
124 }
125 b->n1 = n1;
126 return 0;
127 }
128
129 void bcf_p1_destroy(bcf_p1aux_t *ma)
130 {
131 if (ma) {
132 int k;
133 free(ma->lf);
134 if (ma->hg && ma->n1 > 0) {
135 for (k = 0; k <= 2*ma->n1; ++k) free(ma->hg[k]);
136 free(ma->hg);
137 }
138 free(ma->ploidy); free(ma->q2p); free(ma->pdg);
139 free(ma->phi); free(ma->phi_indel); free(ma->phi1); free(ma->phi2);
140 free(ma->z); free(ma->zswap); free(ma->z1); free(ma->z2);
141 free(ma->afs); free(ma->afs1);
142 free(ma);
143 }
144 }
145
146 extern double kf_gammap(double s, double z);
147 int test16(bcf1_t *b, anno16_t *a);
148
149 /* Calculate P(D|g) */
150 static int cal_pdg(const bcf1_t *b, bcf_p1aux_t *ma)
151 {
152 int i, j;
153 long p_a[16], *p=p_a, tmp;
154 if (b->n_allele > 16)
155 p = (long*) malloc(b->n_allele * sizeof(long));
156 memset(p, 0, sizeof(long) * b->n_allele);
157
158 // Set P(D|g) for each sample and sum phread likelihoods across all samples to create lk
159 for (j = 0; j < ma->n; ++j) {
160 // Fetch the PL array for the sample
161 const int *pi = ma->PL + j * ma->PL_len;
162 // Fetch the P(D|g) array for the sample
163 double *pdg = ma->pdg + j * 3;
164 pdg[0] = ma->q2p[pi[2]]; pdg[1] = ma->q2p[pi[1]]; pdg[2] = ma->q2p[pi[0]];
165 for (i = 0; i < b->n_allele; ++i)
166 p[i] += (int)pi[(i+1)*(i+2)/2-1];
167 }
168 for (i = 0; i < b->n_allele; ++i) p[i] = p[i]<<4 | i;
169 for (i = 1; i < b->n_allele; ++i) // insertion sort
170 for (j = i; j > 0 && p[j] < p[j-1]; --j)
171 tmp = p[j], p[j] = p[j-1], p[j-1] = tmp;
172 for (i = b->n_allele - 1; i >= 0; --i)
173 if ((p[i]&0xf) == 0) break;
174 if (p != p_a)
175 free(p);
176 return i;
177 }
178
179
180 /* f0 is freq of the ref allele */
181 int bcf_p1_call_gt(const bcf_p1aux_t *ma, double f0, int k, int is_var)
182 {
183 double sum, g[3];
184 double max, f3[3], *pdg = ma->pdg + k * 3;
185 int q, i, max_i, ploidy;
186 /* determine ploidy */
187 ploidy = ma->ploidy? ma->ploidy[k] : 2;
188 if (ploidy == 2) {
189 /* given allele frequency we can determine how many of each
190 * genotype we have by HWE p=1-q PP=p^2 PQ&QP=2*p*q QQ=q^2 */
191 f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*f0;
192 } else {
193 f3[0] = 1. - f0; f3[1] = 0; f3[2] = f0;
194 }
195 for (i = 0, sum = 0.; i < 3; ++i)
196 sum += (g[i] = pdg[i] * f3[i]);
197 /* normalise g and then determine max */
198 for (i = 0, max = -1., max_i = 0; i < 3; ++i) {
199 g[i] /= sum;
200 if (g[i] > max) max = g[i], max_i = i;
201 }
202 if ( !is_var ) { max_i = 2; max = g[2]; } // force 0/0 genotype if the site is non-variant
203 max = 1. - max;
204 if (max < 1e-308) max = 1e-308;
205 q = (int)(-4.343 * log(max) + .499);
206 if (q > 99) q = 99;
207 return q<<2|max_i;
208 }
209
210 // If likelihoods fall below this they get squashed to 0
211 #define TINY 1e-20
212 static void mc_cal_y_core(bcf_p1aux_t *ma, int beg)
213 {
214 double *z[2], *tmp, *pdg;
215 int _j, last_min, last_max;
216 assert(beg == 0 || ma->M == ma->n*2);
217 z[0] = ma->z;
218 z[1] = ma->zswap;
219 pdg = ma->pdg;
220 memset(z[0], 0, sizeof(double) * (ma->M + 1));
221 memset(z[1], 0, sizeof(double) * (ma->M + 1));
222 z[0][0] = 1.;
223 last_min = last_max = 0;
224 ma->t = 0.;
225 if (ma->M == ma->n * 2) {
226 int M = 0;
227 for (_j = beg; _j < ma->n; ++_j) {
228 int k, j = _j - beg, _min = last_min, _max = last_max, M0;
229 double p[3], sum;
230 M0 = M; M += 2;
231 // Fetch P(D|g) for this sample
232 pdg = ma->pdg + _j * 3;
233 p[0] = pdg[0]; p[1] = 2. * pdg[1]; p[2] = pdg[2];
234 for (; _min < _max && z[0][_min] < TINY; ++_min) z[0][_min] = z[1][_min] = 0.;
235 for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.;
236 _max += 2;
237 if (_min == 0) k = 0, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k];
238 if (_min <= 1) k = 1, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k] + k*(M0-k+2) * p[1] * z[0][k-1];
239 for (k = _min < 2? 2 : _min; k <= _max; ++k)
240 z[1][k] = (M0-k+1)*(M0-k+2) * p[0] * z[0][k] + k*(M0-k+2) * p[1] * z[0][k-1] + k*(k-1)* p[2] * z[0][k-2];
241 for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
242 ma->t += log(sum / (M * (M - 1.)));
243 for (k = _min; k <= _max; ++k) z[1][k] /= sum;
244 if (_min >= 1) z[1][_min-1] = 0.;
245 if (_min >= 2) z[1][_min-2] = 0.;
246 // If we are not on the last sample
247 if (j < ma->n - 1) z[1][_max+1] = z[1][_max+2] = 0.;
248 if (_j == ma->n1 - 1) { // set pop1; ma->n1==-1 when unset
249 ma->t1 = ma->t;
250 memcpy(ma->z1, z[1], sizeof(double) * (ma->n1 * 2 + 1));
251 }
252 tmp = z[0]; z[0] = z[1]; z[1] = tmp;
253 last_min = _min; last_max = _max;
254 }
255 //for (_j = 0; _j < last_min; ++_j) z[0][_j] = 0.; // TODO: are these necessary?
256 //for (_j = last_max + 1; _j < ma->M; ++_j) z[0][_j] = 0.;
257 } else { // this block is very similar to the block above; these two might be merged in future
258 int j, M = 0;
259 for (j = 0; j < ma->n; ++j) {
260 int k, M0, _min = last_min, _max = last_max;
261 double p[3], sum;
262 // Fetch P(D|g) for this sample
263 pdg = ma->pdg + j * 3;
264 for (; _min < _max && z[0][_min] < TINY; ++_min) z[0][_min] = z[1][_min] = 0.;
265 for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.;
266 M0 = M;
267 M += ma->ploidy[j];
268 if (ma->ploidy[j] == 1) {
269 p[0] = pdg[0]; p[1] = pdg[2];
270 _max++;
271 if (_min == 0) k = 0, z[1][k] = (M0+1-k) * p[0] * z[0][k];
272 for (k = _min < 1? 1 : _min; k <= _max; ++k)
273 z[1][k] = (M0+1-k) * p[0] * z[0][k] + k * p[1] * z[0][k-1];
274 for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
275 ma->t += log(sum / M);
276 for (k = _min; k <= _max; ++k) z[1][k] /= sum;
277 if (_min >= 1) z[1][_min-1] = 0.;
278 // If we are not on the last sample
279 if (j < ma->n - 1) z[1][_max+1] = 0.;
280 } else if (ma->ploidy[j] == 2) {
281 p[0] = pdg[0]; p[1] = 2 * pdg[1]; p[2] = pdg[2];
282 _max += 2;
283 if (_min == 0) k = 0, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k];
284 if (_min <= 1) k = 1, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k] + k*(M0-k+2) * p[1] * z[0][k-1];
285 for (k = _min < 2? 2 : _min; k <= _max; ++k)
286 z[1][k] = (M0-k+1)*(M0-k+2) * p[0] * z[0][k] + k*(M0-k+2) * p[1] * z[0][k-1] + k*(k-1)* p[2] * z[0][k-2];
287 for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
288 ma->t += log(sum / (M * (M - 1.)));
289 for (k = _min; k <= _max; ++k) z[1][k] /= sum;
290 if (_min >= 1) z[1][_min-1] = 0.;
291 if (_min >= 2) z[1][_min-2] = 0.;
292 // If we are not on the last sample
293 if (j < ma->n - 1) z[1][_max+1] = z[1][_max+2] = 0.;
294 }
295 tmp = z[0]; z[0] = z[1]; z[1] = tmp;
296 last_min = _min; last_max = _max;
297 }
298 }
299 if (z[0] != ma->z) memcpy(ma->z, z[0], sizeof(double) * (ma->M + 1));
300 }
301
302 static void mc_cal_y(bcf_p1aux_t *ma)
303 {
304 if (ma->n1 > 0 && ma->n1 < ma->n && ma->M == ma->n * 2) { // NB: ma->n1 is ineffective when there are haploid samples
305 int k;
306 long double x;
307 memset(ma->z1, 0, sizeof(double) * (2 * ma->n1 + 1));
308 memset(ma->z2, 0, sizeof(double) * (2 * (ma->n - ma->n1) + 1));
309 ma->t1 = ma->t2 = 0.;
310 mc_cal_y_core(ma, ma->n1);
311 ma->t2 = ma->t;
312 memcpy(ma->z2, ma->z, sizeof(double) * (2 * (ma->n - ma->n1) + 1));
313 mc_cal_y_core(ma, 0);
314 // rescale z
315 x = expl(ma->t - (ma->t1 + ma->t2));
316 for (k = 0; k <= ma->M; ++k) ma->z[k] *= x;
317 } else mc_cal_y_core(ma, 0);
318 }
319
320 #define CONTRAST_TINY 1e-30
321
322 extern double kf_gammaq(double s, double z); // incomplete gamma function for chi^2 test
323
324 static inline double chi2_test(int a, int b, int c, int d)
325 {
326 double x, z;
327 x = (double)(a+b) * (c+d) * (b+d) * (a+c);
328 if (x == 0.) return 1;
329 z = a * d - b * c;
330 return kf_gammaq(.5, .5 * z * z * (a+b+c+d) / x);
331 }
332
333 // chi2=(a+b+c+d)(ad-bc)^2/[(a+b)(c+d)(a+c)(b+d)]
334 static inline double contrast2_aux(const bcf_p1aux_t *p1, double sum, int k1, int k2, double x[3])
335 {
336 double p = p1->phi[k1+k2] * p1->z1[k1] * p1->z2[k2] / sum * p1->hg[k1][k2];
337 int n1 = p1->n1, n2 = p1->n - p1->n1;
338 if (p < CONTRAST_TINY) return -1;
339 if (.5*k1/n1 < .5*k2/n2) x[1] += p;
340 else if (.5*k1/n1 > .5*k2/n2) x[2] += p;
341 else x[0] += p;
342 return p * chi2_test(k1, k2, (n1<<1) - k1, (n2<<1) - k2);
343 }
344
345 static double contrast2(bcf_p1aux_t *p1, double ret[3])
346 {
347 int k, k1, k2, k10, k20, n1, n2;
348 double sum;
349 // get n1 and n2
350 n1 = p1->n1; n2 = p1->n - p1->n1;
351 if (n1 <= 0 || n2 <= 0) return 0.;
352 if (p1->hg == 0) { // initialize the hypergeometric distribution
353 /* NB: the hg matrix may take a lot of memory when there are many samples. There is a way
354 to avoid precomputing this matrix, but it is slower and quite intricate. The following
355 computation in this block can be accelerated with a similar strategy, but perhaps this
356 is not a serious concern for now. */
357 double tmp = lgamma(2*(n1+n2)+1) - (lgamma(2*n1+1) + lgamma(2*n2+1));
358 p1->hg = (double**) calloc(2*n1+1, sizeof(double*));
359 for (k1 = 0; k1 <= 2*n1; ++k1) {
360 p1->hg[k1] = (double*)calloc(2*n2+1, sizeof(double));
361 for (k2 = 0; k2 <= 2*n2; ++k2)
362 p1->hg[k1][k2] = exp(lgamma(k1+k2+1) + lgamma(p1->M-k1-k2+1) - (lgamma(k1+1) + lgamma(k2+1) + lgamma(2*n1-k1+1) + lgamma(2*n2-k2+1) + tmp));
363 }
364 }
365 { // compute
366 long double suml = 0;
367 for (k = 0; k <= p1->M; ++k) suml += p1->phi[k] * p1->z[k];
368 sum = suml;
369 }
370 { // get the max k1 and k2
371 double max;
372 int max_k;
373 for (k = 0, max = 0, max_k = -1; k <= 2*n1; ++k) {
374 double x = p1->phi1[k] * p1->z1[k];
375 if (x > max) max = x, max_k = k;
376 }
377 k10 = max_k;
378 for (k = 0, max = 0, max_k = -1; k <= 2*n2; ++k) {
379 double x = p1->phi2[k] * p1->z2[k];
380 if (x > max) max = x, max_k = k;
381 }
382 k20 = max_k;
383 }
384 { // We can do the following with one nested loop, but that is an O(N^2) thing. The following code block is much faster for large N.
385 double x[3], y;
386 long double z = 0., L[2];
387 x[0] = x[1] = x[2] = 0; L[0] = L[1] = 0;
388 for (k1 = k10; k1 >= 0; --k1) {
389 for (k2 = k20; k2 >= 0; --k2) {
390 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
391 else z += y;
392 }
393 for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
394 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
395 else z += y;
396 }
397 }
398 ret[0] = x[0]; ret[1] = x[1]; ret[2] = x[2];
399 x[0] = x[1] = x[2] = 0;
400 for (k1 = k10 + 1; k1 <= 2*n1; ++k1) {
401 for (k2 = k20; k2 >= 0; --k2) {
402 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
403 else z += y;
404 }
405 for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
406 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
407 else z += y;
408 }
409 }
410 ret[0] += x[0]; ret[1] += x[1]; ret[2] += x[2];
411 if (ret[0] + ret[1] + ret[2] < 0.95) { // in case of bad things happened
412 ret[0] = ret[1] = ret[2] = 0; L[0] = L[1] = 0;
413 for (k1 = 0, z = 0.; k1 <= 2*n1; ++k1)
414 for (k2 = 0; k2 <= 2*n2; ++k2)
415 if ((y = contrast2_aux(p1, sum, k1, k2, ret)) >= 0) z += y;
416 if (ret[0] + ret[1] + ret[2] < 0.95) // It seems that this may be caused by floating point errors. I do not really understand why...
417 z = 1.0, ret[0] = ret[1] = ret[2] = 1./3;
418 }
419 return (double)z;
420 }
421 }
422
423 static double mc_cal_afs(bcf_p1aux_t *ma, double *p_ref_folded, double *p_var_folded)
424 {
425 int k;
426 long double sum = 0., sum2;
427 double *phi = ma->is_indel? ma->phi_indel : ma->phi;
428 memset(ma->afs1, 0, sizeof(double) * (ma->M + 1));
429 mc_cal_y(ma);
430 // compute AFS
431 // MP15: is this using equation 20 from doi:10.1093/bioinformatics/btr509?
432 for (k = 0, sum = 0.; k <= ma->M; ++k)
433 sum += (long double)phi[k] * ma->z[k];
434 for (k = 0; k <= ma->M; ++k) {
435 ma->afs1[k] = phi[k] * ma->z[k] / sum;
436 if (isnan(ma->afs1[k]) || isinf(ma->afs1[k])) return -1.;
437 }
438 // compute folded variant probability
439 for (k = 0, sum = 0.; k <= ma->M; ++k)
440 sum += (long double)(phi[k] + phi[ma->M - k]) / 2. * ma->z[k];
441 for (k = 1, sum2 = 0.; k < ma->M; ++k)
442 sum2 += (long double)(phi[k] + phi[ma->M - k]) / 2. * ma->z[k];
443 *p_var_folded = sum2 / sum;
444 *p_ref_folded = (phi[k] + phi[ma->M - k]) / 2. * (ma->z[ma->M] + ma->z[0]) / sum;
445 // the expected frequency
446 for (k = 0, sum = 0.; k <= ma->M; ++k) {
447 ma->afs[k] += ma->afs1[k];
448 sum += k * ma->afs1[k];
449 }
450 return sum / ma->M;
451 }
452
453 int bcf_p1_cal(call_t *call, bcf1_t *b, int do_contrast, bcf_p1aux_t *ma, bcf_p1rst_t *rst)
454 {
455 int i, k;
456 long double sum = 0.;
457 ma->is_indel = bcf_is_snp(b) ? 0 : 1;
458 rst->perm_rank = -1;
459
460 ma->PL = call->PLs;
461 ma->PL_len = call->nPLs / b->n_sample;
462 if (b->n_allele < 2) return -1; // FIXME: find a better solution
463
464 rst->rank0 = cal_pdg(b, ma);
465 rst->f_exp = mc_cal_afs(ma, &rst->p_ref_folded, &rst->p_var_folded);
466 rst->p_ref = ma->afs1[ma->M];
467 for (k = 0, sum = 0.; k < ma->M; ++k)
468 sum += ma->afs1[k];
469 rst->p_var = (double)sum;
470 { // compute the allele count
471 double max = -1;
472 rst->ac = -1;
473 for (k = 0; k <= ma->M; ++k)
474 if (max < ma->z[k]) max = ma->z[k], rst->ac = k;
475 rst->ac = ma->M - rst->ac;
476 }
477 // calculate f_flat and f_em
478 for (k = 0, sum = 0.; k <= ma->M; ++k)
479 sum += (long double)ma->z[k];
480 rst->f_flat = 0.;
481 for (k = 0; k <= ma->M; ++k) {
482 double p = ma->z[k] / sum;
483 rst->f_flat += k * p;
484 }
485 rst->f_flat /= ma->M;
486 { // estimate equal-tail credible interval (95% level)
487 int l, h;
488 double p;
489 for (i = 0, p = 0.; i <= ma->M; ++i)
490 if (p + ma->afs1[i] > 0.025) break;
491 else p += ma->afs1[i];
492 l = i;
493 for (i = ma->M, p = 0.; i >= 0; --i)
494 if (p + ma->afs1[i] > 0.025) break;
495 else p += ma->afs1[i];
496 h = i;
497 rst->cil = (double)(ma->M - h) / ma->M; rst->cih = (double)(ma->M - l) / ma->M;
498 }
499 if (ma->n1 > 0) { // compute LRT
500 double max0, max1, max2;
501 for (k = 0, max0 = -1; k <= ma->M; ++k)
502 if (max0 < ma->z[k]) max0 = ma->z[k];
503 for (k = 0, max1 = -1; k <= ma->n1 * 2; ++k)
504 if (max1 < ma->z1[k]) max1 = ma->z1[k];
505 for (k = 0, max2 = -1; k <= ma->M - ma->n1 * 2; ++k)
506 if (max2 < ma->z2[k]) max2 = ma->z2[k];
507 rst->lrt = log(max1 * max2 / max0);
508 rst->lrt = rst->lrt < 0? 1 : kf_gammaq(.5, rst->lrt);
509 } else rst->lrt = -1.0;
510 rst->cmp[0] = rst->cmp[1] = rst->cmp[2] = rst->p_chi2 = -1.0;
511 if (do_contrast && rst->p_var > 0.5) // skip contrast2() if the locus is a strong non-variant
512 rst->p_chi2 = contrast2(ma, rst->cmp);
513 return 0;
514 }
515
516 void bcf_p1_dump_afs(bcf_p1aux_t *ma)
517 {
518 int k;
519 fprintf(stderr, "[afs]");
520 for (k = 0; k <= ma->M; ++k)
521 fprintf(stderr, " %d:%.3lf", k, ma->afs[ma->M - k]);
522 fprintf(stderr, "\n");
523 memset(ma->afs, 0, sizeof(double) * (ma->M + 1));
524 }