Debian Codegraph @

bcftools/prob1.c.pysam.c

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