Compress binary packages with xz.
[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;
197         long *p, tmp;
198         p = alloca(b->n_alleles * sizeof(long));
199         memset(p, 0, sizeof(long) * b->n_alleles);
200         for (j = 0; j < ma->n; ++j) {
201                 const uint8_t *pi = ma->PL + j * ma->PL_len;
202                 double *pdg = ma->pdg + j * 3;
203                 pdg[0] = ma->q2p[pi[2]]; pdg[1] = ma->q2p[pi[1]]; pdg[2] = ma->q2p[pi[0]];
204                 for (i = 0; i < b->n_alleles; ++i)
205                         p[i] += (int)pi[(i+1)*(i+2)/2-1];
206         }
207         for (i = 0; i < b->n_alleles; ++i) p[i] = p[i]<<4 | i;
208         for (i = 1; i < b->n_alleles; ++i) // insertion sort
209                 for (j = i; j > 0 && p[j] < p[j-1]; --j)
210                         tmp = p[j], p[j] = p[j-1], p[j-1] = tmp;
211         for (i = b->n_alleles - 1; i >= 0; --i)
212                 if ((p[i]&0xf) == 0) break;
213         return i;
214 }
215
216 int bcf_p1_call_gt(const bcf_p1aux_t *ma, double f0, int k)
217 {
218         double sum, g[3];
219         double max, f3[3], *pdg = ma->pdg + k * 3;
220         int q, i, max_i, ploidy;
221         ploidy = ma->ploidy? ma->ploidy[k] : 2;
222         if (ploidy == 2) {
223                 f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*f0;
224         } else {
225                 f3[0] = 1. - f0; f3[1] = 0; f3[2] = f0;
226         }
227         for (i = 0, sum = 0.; i < 3; ++i)
228                 sum += (g[i] = pdg[i] * f3[i]);
229         for (i = 0, max = -1., max_i = 0; i < 3; ++i) {
230                 g[i] /= sum;
231                 if (g[i] > max) max = g[i], max_i = i;
232         }
233         max = 1. - max;
234         if (max < 1e-308) max = 1e-308;
235         q = (int)(-4.343 * log(max) + .499);
236         if (q > 99) q = 99;
237         return q<<2|max_i;
238 }
239
240 #define TINY 1e-20
241
242 static void mc_cal_y_core(bcf_p1aux_t *ma, int beg)
243 {
244         double *z[2], *tmp, *pdg;
245         int _j, last_min, last_max;
246         assert(beg == 0 || ma->M == ma->n*2);
247         z[0] = ma->z;
248         z[1] = ma->zswap;
249         pdg = ma->pdg;
250         memset(z[0], 0, sizeof(double) * (ma->M + 1));
251         memset(z[1], 0, sizeof(double) * (ma->M + 1));
252         z[0][0] = 1.;
253         last_min = last_max = 0;
254         ma->t = 0.;
255         if (ma->M == ma->n * 2) {
256                 int M = 0;
257                 for (_j = beg; _j < ma->n; ++_j) {
258                         int k, j = _j - beg, _min = last_min, _max = last_max, M0;
259                         double p[3], sum;
260                         M0 = M; M += 2;
261                         pdg = ma->pdg + _j * 3;
262                         p[0] = pdg[0]; p[1] = 2. * pdg[1]; p[2] = pdg[2];
263                         for (; _min < _max && z[0][_min] < TINY; ++_min) z[0][_min] = z[1][_min] = 0.;
264                         for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.;
265                         _max += 2;
266                         if (_min == 0) k = 0, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k];
267                         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];
268                         for (k = _min < 2? 2 : _min; k <= _max; ++k)
269                                 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];
270                         for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
271                         ma->t += log(sum / (M * (M - 1.)));
272                         for (k = _min; k <= _max; ++k) z[1][k] /= sum;
273                         if (_min >= 1) z[1][_min-1] = 0.;
274                         if (_min >= 2) z[1][_min-2] = 0.;
275                         if (j < ma->n - 1) z[1][_max+1] = z[1][_max+2] = 0.;
276                         if (_j == ma->n1 - 1) { // set pop1; ma->n1==-1 when unset
277                                 ma->t1 = ma->t;
278                                 memcpy(ma->z1, z[1], sizeof(double) * (ma->n1 * 2 + 1));
279                         }
280                         tmp = z[0]; z[0] = z[1]; z[1] = tmp;
281                         last_min = _min; last_max = _max;
282                 }
283                 //for (_j = 0; _j < last_min; ++_j) z[0][_j] = 0.; // TODO: are these necessary?
284                 //for (_j = last_max + 1; _j < ma->M; ++_j) z[0][_j] = 0.;
285         } else { // this block is very similar to the block above; these two might be merged in future
286                 int j, M = 0;
287                 for (j = 0; j < ma->n; ++j) {
288                         int k, M0, _min = last_min, _max = last_max;
289                         double p[3], sum;
290                         pdg = ma->pdg + j * 3;
291                         for (; _min < _max && z[0][_min] < TINY; ++_min) z[0][_min] = z[1][_min] = 0.;
292                         for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.;
293                         M0 = M;
294                         M += ma->ploidy[j];
295                         if (ma->ploidy[j] == 1) {
296                                 p[0] = pdg[0]; p[1] = pdg[2];
297                                 _max++;
298                                 if (_min == 0) k = 0, z[1][k] = (M0+1-k) * p[0] * z[0][k];
299                                 for (k = _min < 1? 1 : _min; k <= _max; ++k)
300                                         z[1][k] = (M0+1-k) * p[0] * z[0][k] + k * p[1] * z[0][k-1];
301                                 for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
302                                 ma->t += log(sum / M);
303                                 for (k = _min; k <= _max; ++k) z[1][k] /= sum;
304                                 if (_min >= 1) z[1][_min-1] = 0.;
305                                 if (j < ma->n - 1) z[1][_max+1] = 0.;
306                         } else if (ma->ploidy[j] == 2) {
307                                 p[0] = pdg[0]; p[1] = 2 * pdg[1]; p[2] = pdg[2];
308                                 _max += 2;
309                                 if (_min == 0) k = 0, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k];
310                                 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];
311                                 for (k = _min < 2? 2 : _min; k <= _max; ++k)
312                                         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];
313                                 for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
314                                 ma->t += log(sum / (M * (M - 1.)));
315                                 for (k = _min; k <= _max; ++k) z[1][k] /= sum;
316                                 if (_min >= 1) z[1][_min-1] = 0.;
317                                 if (_min >= 2) z[1][_min-2] = 0.;
318                                 if (j < ma->n - 1) z[1][_max+1] = z[1][_max+2] = 0.;
319                         }
320                         tmp = z[0]; z[0] = z[1]; z[1] = tmp;
321                         last_min = _min; last_max = _max;
322                 }
323         }
324         if (z[0] != ma->z) memcpy(ma->z, z[0], sizeof(double) * (ma->M + 1));
325 }
326
327 static void mc_cal_y(bcf_p1aux_t *ma)
328 {
329         if (ma->n1 > 0 && ma->n1 < ma->n && ma->M == ma->n * 2) { // NB: ma->n1 is ineffective when there are haploid samples
330                 int k;
331                 long double x;
332                 memset(ma->z1, 0, sizeof(double) * (2 * ma->n1 + 1));
333                 memset(ma->z2, 0, sizeof(double) * (2 * (ma->n - ma->n1) + 1));
334                 ma->t1 = ma->t2 = 0.;
335                 mc_cal_y_core(ma, ma->n1);
336                 ma->t2 = ma->t;
337                 memcpy(ma->z2, ma->z, sizeof(double) * (2 * (ma->n - ma->n1) + 1));
338                 mc_cal_y_core(ma, 0);
339                 // rescale z
340                 x = expl(ma->t - (ma->t1 + ma->t2));
341                 for (k = 0; k <= ma->M; ++k) ma->z[k] *= x;
342         } else mc_cal_y_core(ma, 0);
343 }
344
345 #define CONTRAST_TINY 1e-30
346
347 extern double kf_gammaq(double s, double z); // incomplete gamma function for chi^2 test
348
349 static inline double chi2_test(int a, int b, int c, int d)
350 {
351         double x, z;
352         x = (double)(a+b) * (c+d) * (b+d) * (a+c);
353         if (x == 0.) return 1;
354         z = a * d - b * c;
355         return kf_gammaq(.5, .5 * z * z * (a+b+c+d) / x);
356 }
357
358 // chi2=(a+b+c+d)(ad-bc)^2/[(a+b)(c+d)(a+c)(b+d)]
359 static inline double contrast2_aux(const bcf_p1aux_t *p1, double sum, int k1, int k2, double x[3])
360 {
361         double p = p1->phi[k1+k2] * p1->z1[k1] * p1->z2[k2] / sum * p1->hg[k1][k2];
362         int n1 = p1->n1, n2 = p1->n - p1->n1;
363         if (p < CONTRAST_TINY) return -1;
364         if (.5*k1/n1 < .5*k2/n2) x[1] += p;
365         else if (.5*k1/n1 > .5*k2/n2) x[2] += p;
366         else x[0] += p;
367         return p * chi2_test(k1, k2, (n1<<1) - k1, (n2<<1) - k2);
368 }
369
370 static double contrast2(bcf_p1aux_t *p1, double ret[3])
371 {
372         int k, k1, k2, k10, k20, n1, n2;
373         double sum;
374         // get n1 and n2
375         n1 = p1->n1; n2 = p1->n - p1->n1;
376         if (n1 <= 0 || n2 <= 0) return 0.;
377         if (p1->hg == 0) { // initialize the hypergeometric distribution
378                 /* NB: the hg matrix may take a lot of memory when there are many samples. There is a way
379                    to avoid precomputing this matrix, but it is slower and quite intricate. The following
380                    computation in this block can be accelerated with a similar strategy, but perhaps this
381                    is not a serious concern for now. */
382                 double tmp = lgamma(2*(n1+n2)+1) - (lgamma(2*n1+1) + lgamma(2*n2+1));
383                 p1->hg = calloc(2*n1+1, sizeof(void*));
384                 for (k1 = 0; k1 <= 2*n1; ++k1) {
385                         p1->hg[k1] = calloc(2*n2+1, sizeof(double));
386                         for (k2 = 0; k2 <= 2*n2; ++k2)
387                                 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));
388                 }
389         }
390         { // compute
391                 long double suml = 0;
392                 for (k = 0; k <= p1->M; ++k) suml += p1->phi[k] * p1->z[k];
393                 sum = suml;
394         }
395         { // get the max k1 and k2
396                 double max;
397                 int max_k;
398                 for (k = 0, max = 0, max_k = -1; k <= 2*n1; ++k) {
399                         double x = p1->phi1[k] * p1->z1[k];
400                         if (x > max) max = x, max_k = k;
401                 }
402                 k10 = max_k;
403                 for (k = 0, max = 0, max_k = -1; k <= 2*n2; ++k) {
404                         double x = p1->phi2[k] * p1->z2[k];
405                         if (x > max) max = x, max_k = k;
406                 }
407                 k20 = max_k;
408         }
409         { // 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.
410                 double x[3], y;
411                 long double z = 0., L[2];
412                 x[0] = x[1] = x[2] = 0; L[0] = L[1] = 0;
413                 for (k1 = k10; k1 >= 0; --k1) {
414                         for (k2 = k20; k2 >= 0; --k2) {
415                                 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
416                                 else z += y;
417                         }
418                         for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
419                                 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
420                                 else z += y;
421                         }
422                 }
423                 ret[0] = x[0]; ret[1] = x[1]; ret[2] = x[2];
424                 x[0] = x[1] = x[2] = 0;
425                 for (k1 = k10 + 1; k1 <= 2*n1; ++k1) {
426                         for (k2 = k20; k2 >= 0; --k2) {
427                                 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
428                                 else z += y;
429                         }
430                         for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
431                                 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
432                                 else z += y;
433                         }
434                 }
435                 ret[0] += x[0]; ret[1] += x[1]; ret[2] += x[2];
436                 if (ret[0] + ret[1] + ret[2] < 0.95) { // in case of bad things happened
437                         ret[0] = ret[1] = ret[2] = 0; L[0] = L[1] = 0;
438                         for (k1 = 0, z = 0.; k1 <= 2*n1; ++k1)
439                                 for (k2 = 0; k2 <= 2*n2; ++k2)
440                                         if ((y = contrast2_aux(p1, sum, k1, k2, ret)) >= 0) z += y;
441                         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...
442                                 z = 1.0, ret[0] = ret[1] = ret[2] = 1./3;
443                 }
444                 return (double)z;
445         }
446 }
447
448 static double mc_cal_afs(bcf_p1aux_t *ma, double *p_ref_folded, double *p_var_folded)
449 {
450         int k;
451         long double sum = 0., sum2;
452         double *phi = ma->is_indel? ma->phi_indel : ma->phi;
453         memset(ma->afs1, 0, sizeof(double) * (ma->M + 1));
454         mc_cal_y(ma);
455         // compute AFS
456         for (k = 0, sum = 0.; k <= ma->M; ++k)
457                 sum += (long double)phi[k] * ma->z[k];
458         for (k = 0; k <= ma->M; ++k) {
459                 ma->afs1[k] = phi[k] * ma->z[k] / sum;
460                 if (isnan(ma->afs1[k]) || isinf(ma->afs1[k])) return -1.;
461         }
462         // compute folded variant probability
463         for (k = 0, sum = 0.; k <= ma->M; ++k)
464                 sum += (long double)(phi[k] + phi[ma->M - k]) / 2. * ma->z[k];
465         for (k = 1, sum2 = 0.; k < ma->M; ++k)
466                 sum2 += (long double)(phi[k] + phi[ma->M - k]) / 2. * ma->z[k];
467         *p_var_folded = sum2 / sum;
468         *p_ref_folded = (phi[k] + phi[ma->M - k]) / 2. * (ma->z[ma->M] + ma->z[0]) / sum;
469         // the expected frequency
470         for (k = 0, sum = 0.; k <= ma->M; ++k) {
471                 ma->afs[k] += ma->afs1[k];
472                 sum += k * ma->afs1[k];
473         }
474         return sum / ma->M;
475 }
476
477 int bcf_p1_cal(const bcf1_t *b, int do_contrast, bcf_p1aux_t *ma, bcf_p1rst_t *rst)
478 {
479         int i, k;
480         long double sum = 0.;
481         ma->is_indel = bcf_is_indel(b);
482         rst->perm_rank = -1;
483         // set PL and PL_len
484         for (i = 0; i < b->n_gi; ++i) {
485                 if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
486                         ma->PL = (uint8_t*)b->gi[i].data;
487                         ma->PL_len = b->gi[i].len;
488                         break;
489                 }
490         }
491         if (i == b->n_gi) return -1; // no PL
492         if (b->n_alleles < 2) return -1; // FIXME: find a better solution
493         // 
494         rst->rank0 = cal_pdg(b, ma);
495         rst->f_exp = mc_cal_afs(ma, &rst->p_ref_folded, &rst->p_var_folded);
496         rst->p_ref = ma->afs1[ma->M];
497         for (k = 0, sum = 0.; k < ma->M; ++k)
498                 sum += ma->afs1[k];
499         rst->p_var = (double)sum;
500         { // compute the allele count
501                 double max = -1;
502                 rst->ac = -1;
503                 for (k = 0; k <= ma->M; ++k)
504                         if (max < ma->z[k]) max = ma->z[k], rst->ac = k;
505                 rst->ac = ma->M - rst->ac;
506         }
507         // calculate f_flat and f_em
508         for (k = 0, sum = 0.; k <= ma->M; ++k)
509                 sum += (long double)ma->z[k];
510         rst->f_flat = 0.;
511         for (k = 0; k <= ma->M; ++k) {
512                 double p = ma->z[k] / sum;
513                 rst->f_flat += k * p;
514         }
515         rst->f_flat /= ma->M;
516         { // estimate equal-tail credible interval (95% level)
517                 int l, h;
518                 double p;
519                 for (i = 0, p = 0.; i <= ma->M; ++i)
520                         if (p + ma->afs1[i] > 0.025) break;
521                         else p += ma->afs1[i];
522                 l = i;
523                 for (i = ma->M, p = 0.; i >= 0; --i)
524                         if (p + ma->afs1[i] > 0.025) break;
525                         else p += ma->afs1[i];
526                 h = i;
527                 rst->cil = (double)(ma->M - h) / ma->M; rst->cih = (double)(ma->M - l) / ma->M;
528         }
529         if (ma->n1 > 0) { // compute LRT
530                 double max0, max1, max2;
531                 for (k = 0, max0 = -1; k <= ma->M; ++k)
532                         if (max0 < ma->z[k]) max0 = ma->z[k];
533                 for (k = 0, max1 = -1; k <= ma->n1 * 2; ++k)
534                         if (max1 < ma->z1[k]) max1 = ma->z1[k];
535                 for (k = 0, max2 = -1; k <= ma->M - ma->n1 * 2; ++k)
536                         if (max2 < ma->z2[k]) max2 = ma->z2[k];
537                 rst->lrt = log(max1 * max2 / max0);
538                 rst->lrt = rst->lrt < 0? 1 : kf_gammaq(.5, rst->lrt);
539         } else rst->lrt = -1.0;
540         rst->cmp[0] = rst->cmp[1] = rst->cmp[2] = rst->p_chi2 = -1.0;
541         if (do_contrast && rst->p_var > 0.5) // skip contrast2() if the locus is a strong non-variant
542                 rst->p_chi2 = contrast2(ma, rst->cmp);
543         return 0;
544 }
545
546 void bcf_p1_dump_afs(bcf_p1aux_t *ma)
547 {
548         int k;
549         fprintf(stderr, "[afs]");
550         for (k = 0; k <= ma->M; ++k)
551                 fprintf(stderr, " %d:%.3lf", k, ma->afs[ma->M - k]);
552         fprintf(stderr, "\n");
553         memset(ma->afs, 0, sizeof(double) * (ma->M + 1));
554 }