]> git.donarmstrong.com Git - samtools.git/blob - bcftools/prob1.c
Fix to prevent long stretches of Ns to be mistaken for indels; Always output the...
[samtools.git] / bcftools / prob1.c
1 #include <math.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <assert.h>
7 #include "prob1.h"
8
9 #include "kseq.h"
10 KSTREAM_INIT(gzFile, gzread, 16384)
11
12 #define MC_MAX_EM_ITER 16
13 #define MC_EM_EPS 1e-5
14 #define MC_DEF_INDEL 0.15
15
16 unsigned char seq_nt4_table[256] = {
17         4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
18         4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
19         4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4 /*'-'*/, 4, 4,
20         4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
21         4, 0, 4, 1,  4, 4, 4, 2,  4, 4, 4, 4,  4, 4, 4, 4, 
22         4, 4, 4, 4,  3, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
23         4, 0, 4, 1,  4, 4, 4, 2,  4, 4, 4, 4,  4, 4, 4, 4, 
24         4, 4, 4, 4,  3, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
25         4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
26         4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
27         4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
28         4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
29         4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
30         4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
31         4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
32         4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4
33 };
34
35 struct __bcf_p1aux_t {
36         int n, M, n1, is_indel;
37         uint8_t *ploidy; // haploid or diploid ONLY
38         double *q2p, *pdg; // pdg -> P(D|g)
39         double *phi, *phi_indel;
40         double *z, *zswap; // aux for afs
41         double *z1, *z2, *phi1, *phi2; // only calculated when n1 is set
42         double **hg; // hypergeometric distribution
43         double *lf; // log factorial
44         double t, t1, t2;
45         double *afs, *afs1; // afs: accumulative AFS; afs1: site posterior distribution
46         const uint8_t *PL; // point to PL
47         int PL_len;
48 };
49
50 void bcf_p1_indel_prior(bcf_p1aux_t *ma, double x)
51 {
52         int i;
53         for (i = 0; i < ma->M; ++i)
54                 ma->phi_indel[i] = ma->phi[i] * x;
55         ma->phi_indel[ma->M] = 1. - ma->phi[ma->M] * x;
56 }
57
58 static void init_prior(int type, double theta, int M, double *phi)
59 {
60         int i;
61         if (type == MC_PTYPE_COND2) {
62                 for (i = 0; i <= M; ++i)
63                         phi[i] = 2. * (i + 1) / (M + 1) / (M + 2);
64         } else if (type == MC_PTYPE_FLAT) {
65                 for (i = 0; i <= M; ++i)
66                         phi[i] = 1. / (M + 1);
67         } else {
68                 double sum;
69                 for (i = 0, sum = 0.; i < M; ++i)
70                         sum += (phi[i] = theta / (M - i));
71                 phi[M] = 1. - sum;
72         }
73 }
74
75 void bcf_p1_init_prior(bcf_p1aux_t *ma, int type, double theta)
76 {
77         init_prior(type, theta, ma->M, ma->phi);
78         bcf_p1_indel_prior(ma, MC_DEF_INDEL);
79 }
80
81 void bcf_p1_init_subprior(bcf_p1aux_t *ma, int type, double theta)
82 {
83         if (ma->n1 <= 0 || ma->n1 >= ma->M) return;
84         init_prior(type, theta, 2*ma->n1, ma->phi1);
85         init_prior(type, theta, 2*(ma->n - ma->n1), ma->phi2);
86 }
87
88 int bcf_p1_read_prior(bcf_p1aux_t *ma, const char *fn)
89 {
90         gzFile fp;
91         kstring_t s;
92         kstream_t *ks;
93         long double sum;
94         int dret, k;
95         memset(&s, 0, sizeof(kstring_t));
96         fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
97         ks = ks_init(fp);
98         memset(ma->phi, 0, sizeof(double) * (ma->M + 1));
99         while (ks_getuntil(ks, '\n', &s, &dret) >= 0) {
100                 if (strstr(s.s, "[afs] ") == s.s) {
101                         char *p = s.s + 6;
102                         for (k = 0; k <= ma->M; ++k) {
103                                 int x;
104                                 double y;
105                                 x = strtol(p, &p, 10);
106                                 if (x != k && (errno == EINVAL || errno == ERANGE)) return -1;
107                                 ++p;
108                                 y = strtod(p, &p);
109                                 if (y == 0. && (errno == EINVAL || errno == ERANGE)) return -1;
110                                 ma->phi[ma->M - k] += y;
111                         }
112                 }
113         }
114         ks_destroy(ks);
115         gzclose(fp);
116         free(s.s);
117         for (sum = 0., k = 0; k <= ma->M; ++k) sum += ma->phi[k];
118         fprintf(stderr, "[prior]");
119         for (k = 0; k <= ma->M; ++k) ma->phi[k] /= sum;
120         for (k = 0; k <= ma->M; ++k) fprintf(stderr, " %d:%.3lg", k, ma->phi[ma->M - k]);
121         fputc('\n', stderr);
122         for (sum = 0., k = 1; k < ma->M; ++k) sum += ma->phi[ma->M - k] * (2.* k * (ma->M - k) / ma->M / (ma->M - 1));
123         fprintf(stderr, "[%s] heterozygosity=%lf, ", __func__, (double)sum);
124         for (sum = 0., k = 1; k <= ma->M; ++k) sum += k * ma->phi[ma->M - k] / ma->M;
125         fprintf(stderr, "theta=%lf\n", (double)sum);
126         bcf_p1_indel_prior(ma, MC_DEF_INDEL);
127         return 0;
128 }
129
130 bcf_p1aux_t *bcf_p1_init(int n, uint8_t *ploidy)
131 {
132         bcf_p1aux_t *ma;
133         int i;
134         ma = calloc(1, sizeof(bcf_p1aux_t));
135         ma->n1 = -1;
136         ma->n = n; ma->M = 2 * n;
137         if (ploidy) {
138                 ma->ploidy = malloc(n);
139                 memcpy(ma->ploidy, ploidy, n);
140                 for (i = 0, ma->M = 0; i < n; ++i) ma->M += ploidy[i];
141                 if (ma->M == 2 * n) {
142                         free(ma->ploidy);
143                         ma->ploidy = 0;
144                 }
145         }
146         ma->q2p = calloc(256, sizeof(double));
147         ma->pdg = calloc(3 * ma->n, sizeof(double));
148         ma->phi = calloc(ma->M + 1, sizeof(double));
149         ma->phi_indel = calloc(ma->M + 1, sizeof(double));
150         ma->phi1 = calloc(ma->M + 1, sizeof(double));
151         ma->phi2 = calloc(ma->M + 1, sizeof(double));
152         ma->z = calloc(ma->M + 1, sizeof(double));
153         ma->zswap = calloc(ma->M + 1, sizeof(double));
154         ma->z1 = calloc(ma->M + 1, sizeof(double)); // actually we do not need this large
155         ma->z2 = calloc(ma->M + 1, sizeof(double));
156         ma->afs = calloc(ma->M + 1, sizeof(double));
157         ma->afs1 = calloc(ma->M + 1, sizeof(double));
158         ma->lf = calloc(ma->M + 1, sizeof(double));
159         for (i = 0; i < 256; ++i)
160                 ma->q2p[i] = pow(10., -i / 10.);
161         for (i = 0; i <= ma->M; ++i) ma->lf[i] = lgamma(i + 1);
162         bcf_p1_init_prior(ma, MC_PTYPE_FULL, 1e-3); // the simplest prior
163         return ma;
164 }
165
166 int bcf_p1_set_n1(bcf_p1aux_t *b, int n1)
167 {
168         if (n1 == 0 || n1 >= b->n) return -1;
169         if (b->M != b->n * 2) {
170                 fprintf(stderr, "[%s] unable to set `n1' when there are haploid samples.\n", __func__);
171                 return -1;
172         }
173         b->n1 = n1;
174         return 0;
175 }
176
177 void bcf_p1_destroy(bcf_p1aux_t *ma)
178 {
179         if (ma) {
180                 int k;
181                 free(ma->lf);
182                 if (ma->hg && ma->n1 > 0) {
183                         for (k = 0; k <= 2*ma->n1; ++k) free(ma->hg[k]);
184                         free(ma->hg);
185                 }
186                 free(ma->ploidy); free(ma->q2p); free(ma->pdg);
187                 free(ma->phi); free(ma->phi_indel); free(ma->phi1); free(ma->phi2);
188                 free(ma->z); free(ma->zswap); free(ma->z1); free(ma->z2);
189                 free(ma->afs); free(ma->afs1);
190                 free(ma);
191         }
192 }
193
194 static int cal_pdg(const bcf1_t *b, bcf_p1aux_t *ma)
195 {
196         int i, j, imax=0;
197         for (j = 0; j < ma->n; ++j) {
198                 const uint8_t *pi = ma->PL + j * ma->PL_len;
199                 double *pdg = ma->pdg + j * 3;
200                 pdg[0] = ma->q2p[pi[2]]; pdg[1] = ma->q2p[pi[1]]; pdg[2] = ma->q2p[pi[0]];
201         int ib,ia=0,n=(b->n_alleles+1)*b->n_alleles/2;
202         for (i=0; i<n;) {
203             for (ib=0; ib<=ia; ib++) {
204                 if ( pi[i]==0 && ia>imax ) imax=ia;
205                 i++;
206             }
207             ia++;
208         }
209         }
210         return imax;
211 }
212
213 int bcf_p1_call_gt(const bcf_p1aux_t *ma, double f0, int k)
214 {
215         double sum, g[3];
216         double max, f3[3], *pdg = ma->pdg + k * 3;
217         int q, i, max_i, ploidy;
218         ploidy = ma->ploidy? ma->ploidy[k] : 2;
219         if (ploidy == 2) {
220                 f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*f0;
221         } else {
222                 f3[0] = 1. - f0; f3[1] = 0; f3[2] = f0;
223         }
224         for (i = 0, sum = 0.; i < 3; ++i)
225                 sum += (g[i] = pdg[i] * f3[i]);
226         for (i = 0, max = -1., max_i = 0; i < 3; ++i) {
227                 g[i] /= sum;
228                 if (g[i] > max) max = g[i], max_i = i;
229         }
230         max = 1. - max;
231         if (max < 1e-308) max = 1e-308;
232         q = (int)(-4.343 * log(max) + .499);
233         if (q > 99) q = 99;
234         return q<<2|max_i;
235 }
236
237 #define TINY 1e-20
238
239 static void mc_cal_y_core(bcf_p1aux_t *ma, int beg)
240 {
241         double *z[2], *tmp, *pdg;
242         int _j, last_min, last_max;
243         assert(beg == 0 || ma->M == ma->n*2);
244         z[0] = ma->z;
245         z[1] = ma->zswap;
246         pdg = ma->pdg;
247         memset(z[0], 0, sizeof(double) * (ma->M + 1));
248         memset(z[1], 0, sizeof(double) * (ma->M + 1));
249         z[0][0] = 1.;
250         last_min = last_max = 0;
251         ma->t = 0.;
252         if (ma->M == ma->n * 2) {
253                 int M = 0;
254                 for (_j = beg; _j < ma->n; ++_j) {
255                         int k, j = _j - beg, _min = last_min, _max = last_max, M0;
256                         double p[3], sum;
257                         M0 = M; M += 2;
258                         pdg = ma->pdg + _j * 3;
259                         p[0] = pdg[0]; p[1] = 2. * pdg[1]; p[2] = pdg[2];
260                         for (; _min < _max && z[0][_min] < TINY; ++_min) z[0][_min] = z[1][_min] = 0.;
261                         for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.;
262                         _max += 2;
263                         if (_min == 0) k = 0, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k];
264                         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];
265                         for (k = _min < 2? 2 : _min; k <= _max; ++k)
266                                 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];
267                         for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
268                         ma->t += log(sum / (M * (M - 1.)));
269                         for (k = _min; k <= _max; ++k) z[1][k] /= sum;
270                         if (_min >= 1) z[1][_min-1] = 0.;
271                         if (_min >= 2) z[1][_min-2] = 0.;
272                         if (j < ma->n - 1) z[1][_max+1] = z[1][_max+2] = 0.;
273                         if (_j == ma->n1 - 1) { // set pop1; ma->n1==-1 when unset
274                                 ma->t1 = ma->t;
275                                 memcpy(ma->z1, z[1], sizeof(double) * (ma->n1 * 2 + 1));
276                         }
277                         tmp = z[0]; z[0] = z[1]; z[1] = tmp;
278                         last_min = _min; last_max = _max;
279                 }
280                 //for (_j = 0; _j < last_min; ++_j) z[0][_j] = 0.; // TODO: are these necessary?
281                 //for (_j = last_max + 1; _j < ma->M; ++_j) z[0][_j] = 0.;
282         } else { // this block is very similar to the block above; these two might be merged in future
283                 int j, M = 0;
284                 for (j = 0; j < ma->n; ++j) {
285                         int k, M0, _min = last_min, _max = last_max;
286                         double p[3], sum;
287                         pdg = ma->pdg + j * 3;
288                         for (; _min < _max && z[0][_min] < TINY; ++_min) z[0][_min] = z[1][_min] = 0.;
289                         for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.;
290                         M0 = M;
291                         M += ma->ploidy[j];
292                         if (ma->ploidy[j] == 1) {
293                                 p[0] = pdg[0]; p[1] = pdg[2];
294                                 _max++;
295                                 if (_min == 0) k = 0, z[1][k] = (M0+1-k) * p[0] * z[0][k];
296                                 for (k = _min < 1? 1 : _min; k <= _max; ++k)
297                                         z[1][k] = (M0+1-k) * p[0] * z[0][k] + k * p[1] * z[0][k-1];
298                                 for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
299                                 ma->t += log(sum / M);
300                                 for (k = _min; k <= _max; ++k) z[1][k] /= sum;
301                                 if (_min >= 1) z[1][_min-1] = 0.;
302                                 if (j < ma->n - 1) z[1][_max+1] = 0.;
303                         } else if (ma->ploidy[j] == 2) {
304                                 p[0] = pdg[0]; p[1] = 2 * pdg[1]; p[2] = pdg[2];
305                                 _max += 2;
306                                 if (_min == 0) k = 0, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k];
307                                 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];
308                                 for (k = _min < 2? 2 : _min; k <= _max; ++k)
309                                         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];
310                                 for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
311                                 ma->t += log(sum / (M * (M - 1.)));
312                                 for (k = _min; k <= _max; ++k) z[1][k] /= sum;
313                                 if (_min >= 1) z[1][_min-1] = 0.;
314                                 if (_min >= 2) z[1][_min-2] = 0.;
315                                 if (j < ma->n - 1) z[1][_max+1] = z[1][_max+2] = 0.;
316                         }
317                         tmp = z[0]; z[0] = z[1]; z[1] = tmp;
318                         last_min = _min; last_max = _max;
319                 }
320         }
321         if (z[0] != ma->z) memcpy(ma->z, z[0], sizeof(double) * (ma->M + 1));
322 }
323
324 static void mc_cal_y(bcf_p1aux_t *ma)
325 {
326         if (ma->n1 > 0 && ma->n1 < ma->n && ma->M == ma->n * 2) { // NB: ma->n1 is ineffective when there are haploid samples
327                 int k;
328                 long double x;
329                 memset(ma->z1, 0, sizeof(double) * (2 * ma->n1 + 1));
330                 memset(ma->z2, 0, sizeof(double) * (2 * (ma->n - ma->n1) + 1));
331                 ma->t1 = ma->t2 = 0.;
332                 mc_cal_y_core(ma, ma->n1);
333                 ma->t2 = ma->t;
334                 memcpy(ma->z2, ma->z, sizeof(double) * (2 * (ma->n - ma->n1) + 1));
335                 mc_cal_y_core(ma, 0);
336                 // rescale z
337                 x = expl(ma->t - (ma->t1 + ma->t2));
338                 for (k = 0; k <= ma->M; ++k) ma->z[k] *= x;
339         } else mc_cal_y_core(ma, 0);
340 }
341
342 #define CONTRAST_TINY 1e-30
343
344 extern double kf_gammaq(double s, double z); // incomplete gamma function for chi^2 test
345
346 static inline double chi2_test(int a, int b, int c, int d)
347 {
348         double x, z;
349         x = (double)(a+b) * (c+d) * (b+d) * (a+c);
350         if (x == 0.) return 1;
351         z = a * d - b * c;
352         return kf_gammaq(.5, .5 * z * z * (a+b+c+d) / x);
353 }
354
355 // chi2=(a+b+c+d)(ad-bc)^2/[(a+b)(c+d)(a+c)(b+d)]
356 static inline double contrast2_aux(const bcf_p1aux_t *p1, double sum, int k1, int k2, double x[3])
357 {
358         double p = p1->phi[k1+k2] * p1->z1[k1] * p1->z2[k2] / sum * p1->hg[k1][k2];
359         int n1 = p1->n1, n2 = p1->n - p1->n1;
360         if (p < CONTRAST_TINY) return -1;
361         if (.5*k1/n1 < .5*k2/n2) x[1] += p;
362         else if (.5*k1/n1 > .5*k2/n2) x[2] += p;
363         else x[0] += p;
364         return p * chi2_test(k1, k2, (n1<<1) - k1, (n2<<1) - k2);
365 }
366
367 static double contrast2(bcf_p1aux_t *p1, double ret[3])
368 {
369         int k, k1, k2, k10, k20, n1, n2;
370         double sum;
371         // get n1 and n2
372         n1 = p1->n1; n2 = p1->n - p1->n1;
373         if (n1 <= 0 || n2 <= 0) return 0.;
374         if (p1->hg == 0) { // initialize the hypergeometric distribution
375                 /* NB: the hg matrix may take a lot of memory when there are many samples. There is a way
376                    to avoid precomputing this matrix, but it is slower and quite intricate. The following
377                    computation in this block can be accelerated with a similar strategy, but perhaps this
378                    is not a serious concern for now. */
379                 double tmp = lgamma(2*(n1+n2)+1) - (lgamma(2*n1+1) + lgamma(2*n2+1));
380                 p1->hg = calloc(2*n1+1, sizeof(void*));
381                 for (k1 = 0; k1 <= 2*n1; ++k1) {
382                         p1->hg[k1] = calloc(2*n2+1, sizeof(double));
383                         for (k2 = 0; k2 <= 2*n2; ++k2)
384                                 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));
385                 }
386         }
387         { // compute
388                 long double suml = 0;
389                 for (k = 0; k <= p1->M; ++k) suml += p1->phi[k] * p1->z[k];
390                 sum = suml;
391         }
392         { // get the max k1 and k2
393                 double max;
394                 int max_k;
395                 for (k = 0, max = 0, max_k = -1; k <= 2*n1; ++k) {
396                         double x = p1->phi1[k] * p1->z1[k];
397                         if (x > max) max = x, max_k = k;
398                 }
399                 k10 = max_k;
400                 for (k = 0, max = 0, max_k = -1; k <= 2*n2; ++k) {
401                         double x = p1->phi2[k] * p1->z2[k];
402                         if (x > max) max = x, max_k = k;
403                 }
404                 k20 = max_k;
405         }
406         { // 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.
407                 double x[3], y;
408                 long double z = 0., L[2];
409                 x[0] = x[1] = x[2] = 0; L[0] = L[1] = 0;
410                 for (k1 = k10; k1 >= 0; --k1) {
411                         for (k2 = k20; k2 >= 0; --k2) {
412                                 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
413                                 else z += y;
414                         }
415                         for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
416                                 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
417                                 else z += y;
418                         }
419                 }
420                 ret[0] = x[0]; ret[1] = x[1]; ret[2] = x[2];
421                 x[0] = x[1] = x[2] = 0;
422                 for (k1 = k10 + 1; k1 <= 2*n1; ++k1) {
423                         for (k2 = k20; k2 >= 0; --k2) {
424                                 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
425                                 else z += y;
426                         }
427                         for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
428                                 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
429                                 else z += y;
430                         }
431                 }
432                 ret[0] += x[0]; ret[1] += x[1]; ret[2] += x[2];
433                 if (ret[0] + ret[1] + ret[2] < 0.95) { // in case of bad things happened
434                         ret[0] = ret[1] = ret[2] = 0; L[0] = L[1] = 0;
435                         for (k1 = 0, z = 0.; k1 <= 2*n1; ++k1)
436                                 for (k2 = 0; k2 <= 2*n2; ++k2)
437                                         if ((y = contrast2_aux(p1, sum, k1, k2, ret)) >= 0) z += y;
438                         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...
439                                 z = 1.0, ret[0] = ret[1] = ret[2] = 1./3;
440                 }
441                 return (double)z;
442         }
443 }
444
445 static double mc_cal_afs(bcf_p1aux_t *ma, double *p_ref_folded, double *p_var_folded)
446 {
447         int k;
448         long double sum = 0., sum2;
449         double *phi = ma->is_indel? ma->phi_indel : ma->phi;
450         memset(ma->afs1, 0, sizeof(double) * (ma->M + 1));
451         mc_cal_y(ma);
452         // compute AFS
453         for (k = 0, sum = 0.; k <= ma->M; ++k)
454                 sum += (long double)phi[k] * ma->z[k];
455         for (k = 0; k <= ma->M; ++k) {
456                 ma->afs1[k] = phi[k] * ma->z[k] / sum;
457                 if (isnan(ma->afs1[k]) || isinf(ma->afs1[k])) return -1.;
458         }
459         // compute folded variant probability
460         for (k = 0, sum = 0.; k <= ma->M; ++k)
461                 sum += (long double)(phi[k] + phi[ma->M - k]) / 2. * ma->z[k];
462         for (k = 1, sum2 = 0.; k < ma->M; ++k)
463                 sum2 += (long double)(phi[k] + phi[ma->M - k]) / 2. * ma->z[k];
464         *p_var_folded = sum2 / sum;
465         *p_ref_folded = (phi[k] + phi[ma->M - k]) / 2. * (ma->z[ma->M] + ma->z[0]) / sum;
466         // the expected frequency
467         for (k = 0, sum = 0.; k <= ma->M; ++k) {
468                 ma->afs[k] += ma->afs1[k];
469                 sum += k * ma->afs1[k];
470         }
471         return sum / ma->M;
472 }
473
474 int bcf_p1_cal(const bcf1_t *b, int do_contrast, bcf_p1aux_t *ma, bcf_p1rst_t *rst)
475 {
476         int i, k;
477         long double sum = 0.;
478         ma->is_indel = bcf_is_indel(b);
479         rst->perm_rank = -1;
480         // set PL and PL_len
481         for (i = 0; i < b->n_gi; ++i) {
482                 if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
483                         ma->PL = (uint8_t*)b->gi[i].data;
484                         ma->PL_len = b->gi[i].len;
485                         break;
486                 }
487         }
488         if (i == b->n_gi) return -1; // no PL
489         if (b->n_alleles < 2) return -1; // FIXME: find a better solution
490         // 
491         rst->rank0 = cal_pdg(b, ma);
492         rst->f_exp = mc_cal_afs(ma, &rst->p_ref_folded, &rst->p_var_folded);
493         rst->p_ref = ma->afs1[ma->M];
494         for (k = 0, sum = 0.; k < ma->M; ++k)
495                 sum += ma->afs1[k];
496         rst->p_var = (double)sum;
497         { // compute the allele count
498                 double max = -1;
499                 rst->ac = -1;
500                 for (k = 0; k <= ma->M; ++k)
501                         if (max < ma->z[k]) max = ma->z[k], rst->ac = k;
502                 rst->ac = ma->M - rst->ac;
503         }
504         // calculate f_flat and f_em
505         for (k = 0, sum = 0.; k <= ma->M; ++k)
506                 sum += (long double)ma->z[k];
507         rst->f_flat = 0.;
508         for (k = 0; k <= ma->M; ++k) {
509                 double p = ma->z[k] / sum;
510                 rst->f_flat += k * p;
511         }
512         rst->f_flat /= ma->M;
513         { // estimate equal-tail credible interval (95% level)
514                 int l, h;
515                 double p;
516                 for (i = 0, p = 0.; i <= ma->M; ++i)
517                         if (p + ma->afs1[i] > 0.025) break;
518                         else p += ma->afs1[i];
519                 l = i;
520                 for (i = ma->M, p = 0.; i >= 0; --i)
521                         if (p + ma->afs1[i] > 0.025) break;
522                         else p += ma->afs1[i];
523                 h = i;
524                 rst->cil = (double)(ma->M - h) / ma->M; rst->cih = (double)(ma->M - l) / ma->M;
525         }
526         if (ma->n1 > 0) { // compute LRT
527                 double max0, max1, max2;
528                 for (k = 0, max0 = -1; k <= ma->M; ++k)
529                         if (max0 < ma->z[k]) max0 = ma->z[k];
530                 for (k = 0, max1 = -1; k <= ma->n1 * 2; ++k)
531                         if (max1 < ma->z1[k]) max1 = ma->z1[k];
532                 for (k = 0, max2 = -1; k <= ma->M - ma->n1 * 2; ++k)
533                         if (max2 < ma->z2[k]) max2 = ma->z2[k];
534                 rst->lrt = log(max1 * max2 / max0);
535                 rst->lrt = rst->lrt < 0? 1 : kf_gammaq(.5, rst->lrt);
536         } else rst->lrt = -1.0;
537         rst->cmp[0] = rst->cmp[1] = rst->cmp[2] = rst->p_chi2 = -1.0;
538         if (do_contrast && rst->p_var > 0.5) // skip contrast2() if the locus is a strong non-variant
539                 rst->p_chi2 = contrast2(ma, rst->cmp);
540         return 0;
541 }
542
543 void bcf_p1_dump_afs(bcf_p1aux_t *ma)
544 {
545         int k;
546         fprintf(stderr, "[afs]");
547         for (k = 0; k <= ma->M; ++k)
548                 fprintf(stderr, " %d:%.3lf", k, ma->afs[ma->M - k]);
549         fprintf(stderr, "\n");
550         memset(ma->afs, 0, sizeof(double) * (ma->M + 1));
551 }