]> git.donarmstrong.com Git - samtools.git/blob - bcftools/prob1.c
backup commit
[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 <limits.h>
8 #include "prob1.h"
9 #include "kstring.h"
10
11 #include "kseq.h"
12 KSTREAM_INIT(gzFile, gzread, 16384)
13
14 #define MC_MAX_EM_ITER 16
15 #define MC_EM_EPS 1e-5
16 #define MC_DEF_INDEL 0.15
17
18 unsigned char seq_nt4_table[256] = {
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, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4 /*'-'*/, 4, 4,
22         4, 4, 4, 4,  4, 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, 0, 4, 1,  4, 4, 4, 2,  4, 4, 4, 4,  4, 4, 4, 4, 
26         4, 4, 4, 4,  3, 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         4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
34         4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4
35 };
36
37 struct __bcf_p1aux_t {
38         int n, M, n1, is_indel;
39         uint8_t *ploidy; // haploid or diploid ONLY
40         double *q2p, *pdg; // pdg -> P(D|g)
41         double *phi, *phi_indel;
42         double *z, *zswap; // aux for afs
43         double *z1, *z2, *phi1, *phi2; // only calculated when n1 is set
44         double **hg; // hypergeometric distribution
45         double *lf; // log factorial
46         double t, t1, t2;
47         double *afs, *afs1; // afs: accumulative AFS; afs1: site posterior distribution
48         const uint8_t *PL; // point to PL
49         int PL_len;
50 };
51
52 void bcf_p1_indel_prior(bcf_p1aux_t *ma, double x)
53 {
54         int i;
55         for (i = 0; i < ma->M; ++i)
56                 ma->phi_indel[i] = ma->phi[i] * x;
57         ma->phi_indel[ma->M] = 1. - ma->phi[ma->M] * x;
58 }
59
60 static void init_prior(int type, double theta, int M, double *phi)
61 {
62         int i;
63         if (type == MC_PTYPE_COND2) {
64                 for (i = 0; i <= M; ++i)
65                         phi[i] = 2. * (i + 1) / (M + 1) / (M + 2);
66         } else if (type == MC_PTYPE_FLAT) {
67                 for (i = 0; i <= M; ++i)
68                         phi[i] = 1. / (M + 1);
69         } else {
70                 double sum;
71                 for (i = 0, sum = 0.; i < M; ++i)
72                         sum += (phi[i] = theta / (M - i));
73                 phi[M] = 1. - sum;
74         }
75 }
76
77 void bcf_p1_init_prior(bcf_p1aux_t *ma, int type, double theta)
78 {
79         init_prior(type, theta, ma->M, ma->phi);
80         bcf_p1_indel_prior(ma, MC_DEF_INDEL);
81 }
82
83 void bcf_p1_init_subprior(bcf_p1aux_t *ma, int type, double theta)
84 {
85         if (ma->n1 <= 0 || ma->n1 >= ma->M) return;
86         init_prior(type, theta, 2*ma->n1, ma->phi1);
87         init_prior(type, theta, 2*(ma->n - ma->n1), ma->phi2);
88 }
89
90 int bcf_p1_read_prior(bcf_p1aux_t *ma, const char *fn)
91 {
92         gzFile fp;
93         kstring_t s;
94         kstream_t *ks;
95         long double sum;
96         int dret, k;
97         memset(&s, 0, sizeof(kstring_t));
98         fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
99         ks = ks_init(fp);
100         memset(ma->phi, 0, sizeof(double) * (ma->M + 1));
101         while (ks_getuntil(ks, '\n', &s, &dret) >= 0) {
102                 if (strstr(s.s, "[afs] ") == s.s) {
103                         char *p = s.s + 6;
104                         for (k = 0; k <= ma->M; ++k) {
105                                 int x;
106                                 double y;
107                                 x = strtol(p, &p, 10);
108                                 if (x != k && (errno == EINVAL || errno == ERANGE)) return -1;
109                                 ++p;
110                                 y = strtod(p, &p);
111                                 if (y == 0. && (errno == EINVAL || errno == ERANGE)) return -1;
112                                 ma->phi[ma->M - k] += y;
113                         }
114                 }
115         }
116         ks_destroy(ks);
117         gzclose(fp);
118         free(s.s);
119         for (sum = 0., k = 0; k <= ma->M; ++k) sum += ma->phi[k];
120         fprintf(stderr, "[prior]");
121         for (k = 0; k <= ma->M; ++k) ma->phi[k] /= sum;
122         for (k = 0; k <= ma->M; ++k) fprintf(stderr, " %d:%.3lg", k, ma->phi[ma->M - k]);
123         fputc('\n', stderr);
124         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));
125         fprintf(stderr, "[%s] heterozygosity=%lf, ", __func__, (double)sum);
126         for (sum = 0., k = 1; k <= ma->M; ++k) sum += k * ma->phi[ma->M - k] / ma->M;
127         fprintf(stderr, "theta=%lf\n", (double)sum);
128         bcf_p1_indel_prior(ma, MC_DEF_INDEL);
129         return 0;
130 }
131
132 bcf_p1aux_t *bcf_p1_init(int n, uint8_t *ploidy)
133 {
134         bcf_p1aux_t *ma;
135         int i;
136         ma = calloc(1, sizeof(bcf_p1aux_t));
137         ma->n1 = -1;
138         ma->n = n; ma->M = 2 * n;
139         if (ploidy) {
140                 ma->ploidy = malloc(n);
141                 memcpy(ma->ploidy, ploidy, n);
142                 for (i = 0, ma->M = 0; i < n; ++i) ma->M += ploidy[i];
143                 if (ma->M == 2 * n) {
144                         free(ma->ploidy);
145                         ma->ploidy = 0;
146                 }
147         }
148         ma->q2p = calloc(256, sizeof(double));
149         ma->pdg = calloc(3 * ma->n, sizeof(double));
150         ma->phi = calloc(ma->M + 1, sizeof(double));
151         ma->phi_indel = calloc(ma->M + 1, sizeof(double));
152         ma->phi1 = calloc(ma->M + 1, sizeof(double));
153         ma->phi2 = calloc(ma->M + 1, sizeof(double));
154         ma->z = calloc(ma->M + 1, sizeof(double));
155         ma->zswap = calloc(ma->M + 1, sizeof(double));
156         ma->z1 = calloc(ma->M + 1, sizeof(double)); // actually we do not need this large
157         ma->z2 = calloc(ma->M + 1, sizeof(double));
158         ma->afs = calloc(ma->M + 1, sizeof(double));
159         ma->afs1 = calloc(ma->M + 1, sizeof(double));
160         ma->lf = calloc(ma->M + 1, sizeof(double));
161         for (i = 0; i < 256; ++i)
162                 ma->q2p[i] = pow(10., -i / 10.);
163         for (i = 0; i <= ma->M; ++i) ma->lf[i] = lgamma(i + 1);
164         bcf_p1_init_prior(ma, MC_PTYPE_FULL, 1e-3); // the simplest prior
165         return ma;
166 }
167
168 int bcf_p1_set_n1(bcf_p1aux_t *b, int n1)
169 {
170         if (n1 == 0 || n1 >= b->n) return -1;
171         if (b->M != b->n * 2) {
172                 fprintf(stderr, "[%s] unable to set `n1' when there are haploid samples.\n", __func__);
173                 return -1;
174         }
175         b->n1 = n1;
176         return 0;
177 }
178
179 void bcf_p1_set_ploidy(bcf1_t *b, bcf_p1aux_t *ma)
180 {
181     // bcf_p1aux_t fields are not visible outside of prob1.c, hence this wrapper.
182     // Ideally, this should set ploidy per site to allow pseudo-autosomal regions
183     b->ploidy = ma->ploidy;
184 }
185
186 void bcf_p1_destroy(bcf_p1aux_t *ma)
187 {
188         if (ma) {
189                 int k;
190                 free(ma->lf);
191                 if (ma->hg && ma->n1 > 0) {
192                         for (k = 0; k <= 2*ma->n1; ++k) free(ma->hg[k]);
193                         free(ma->hg);
194                 }
195                 free(ma->ploidy); free(ma->q2p); free(ma->pdg);
196                 free(ma->phi); free(ma->phi_indel); free(ma->phi1); free(ma->phi2);
197                 free(ma->z); free(ma->zswap); free(ma->z1); free(ma->z2);
198                 free(ma->afs); free(ma->afs1);
199                 free(ma);
200         }
201 }
202
203 extern double kf_gammap(double s, double z);
204 int test16(bcf1_t *b, anno16_t *a);
205
206 // Wigginton 2005, PMID: 15789306
207 // written by Jan Wigginton
208 double calc_hwe(int obs_hom1, int obs_hom2, int obs_hets)
209 {
210     if (obs_hom1 + obs_hom2 + obs_hets == 0 ) return 1;
211
212     assert(obs_hom1 >= 0 && obs_hom2 >= 0 && obs_hets >= 0);
213
214     int obs_homc = obs_hom1 < obs_hom2 ? obs_hom2 : obs_hom1;
215     int obs_homr = obs_hom1 < obs_hom2 ? obs_hom1 : obs_hom2;
216
217     int rare_copies = 2 * obs_homr + obs_hets;
218     int genotypes   = obs_hets + obs_homc + obs_homr;
219
220     double *het_probs = (double*) calloc(rare_copies+1, sizeof(double));
221
222     /* start at midpoint */
223     int mid = rare_copies * (2 * genotypes - rare_copies) / (2 * genotypes);
224
225     /* check to ensure that midpoint and rare alleles have same parity */
226     if ((rare_copies & 1) ^ (mid & 1)) mid++;
227
228     int curr_hets = mid;
229     int curr_homr = (rare_copies - mid) / 2;
230     int curr_homc = genotypes - curr_hets - curr_homr;
231
232     het_probs[mid] = 1.0;
233     double sum = het_probs[mid];
234     for (curr_hets = mid; curr_hets > 1; curr_hets -= 2)
235     {
236         het_probs[curr_hets - 2] = het_probs[curr_hets] * curr_hets * (curr_hets - 1.0) / (4.0 * (curr_homr + 1.0) * (curr_homc + 1.0));
237         sum += het_probs[curr_hets - 2];
238
239         /* 2 fewer heterozygotes for next iteration -> add one rare, one common homozygote */
240         curr_homr++;
241         curr_homc++;
242     }
243
244     curr_hets = mid;
245     curr_homr = (rare_copies - mid) / 2;
246     curr_homc = genotypes - curr_hets - curr_homr;
247     for (curr_hets = mid; curr_hets <= rare_copies - 2; curr_hets += 2)
248     {
249         het_probs[curr_hets + 2] = het_probs[curr_hets] * 4.0 * curr_homr * curr_homc /((curr_hets + 2.0) * (curr_hets + 1.0));
250         sum += het_probs[curr_hets + 2];
251
252         /* add 2 heterozygotes for next iteration -> subtract one rare, one common homozygote */
253         curr_homr--;
254         curr_homc--;
255     }
256     int i;
257     for (i = 0; i <= rare_copies; i++) het_probs[i] /= sum;
258
259     /*  p-value calculation for p_hwe  */
260     double p_hwe = 0.0;
261     for (i = 0; i <= rare_copies; i++)
262     {
263         if (het_probs[i] > het_probs[obs_hets])
264             continue;
265         p_hwe += het_probs[i];
266     }
267
268     p_hwe = p_hwe > 1.0 ? 1.0 : p_hwe;
269     free(het_probs);
270     return p_hwe;
271
272 }
273
274
275 static void _bcf1_set_ref(bcf1_t *b, int idp)
276 {
277     kstring_t s;
278     int old_n_gi = b->n_gi;
279     s.m = b->m_str; s.l = b->l_str - 1; s.s = b->str;
280     kputs(":GT", &s); kputc('\0', &s);
281     b->m_str = s.m; b->l_str = s.l; b->str = s.s;
282     bcf_sync(b);
283
284     // Call GTs
285     int isample, an = 0;
286     for (isample = 0; isample < b->n_smpl; isample++) 
287     {
288         if ( idp>=0 && ((uint16_t*)b->gi[idp].data)[isample]==0 )
289             ((uint8_t*)b->gi[old_n_gi].data)[isample] = 1<<7;
290         else
291         {
292             ((uint8_t*)b->gi[old_n_gi].data)[isample] = 0;
293             an += b->ploidy ? b->ploidy[isample] : 2;
294         }
295     }
296     bcf_fit_alt(b,1);
297     b->qual = 999;
298
299     // Prepare BCF for output: ref, alt, filter, info, format
300     memset(&s, 0, sizeof(kstring_t)); kputc('\0', &s); 
301     kputs(b->ref, &s); kputc('\0', &s);
302     kputs(b->alt, &s); kputc('\0', &s); kputc('\0', &s);
303     {
304         ksprintf(&s, "AN=%d;", an);
305         kputs(b->info, &s); 
306         anno16_t a;
307         int has_I16 = test16(b, &a) >= 0? 1 : 0;
308         if (has_I16 )
309         {
310             if ( a.is_tested) ksprintf(&s, ";PV4=%.2g,%.2g,%.2g,%.2g", a.p[0], a.p[1], a.p[2], a.p[3]);
311             ksprintf(&s, ";DP4=%d,%d,%d,%d;MQ=%d", a.d[0], a.d[1], a.d[2], a.d[3], a.mq);
312         }
313         kputc('\0', &s);
314         rm_info(&s, "I16=");
315         rm_info(&s, "QS=");
316     }
317     kputs(b->fmt, &s); kputc('\0', &s);
318     free(b->str);
319     b->m_str = s.m; b->l_str = s.l; b->str = s.s;
320     bcf_sync(b);
321 }
322
323 int call_multiallelic_gt(bcf1_t *b, bcf_p1aux_t *ma, double threshold, int var_only)
324 {
325     int nals = 1;
326     char *p;
327     for (p=b->alt; *p; p++)
328     {
329         if ( *p=='X' || p[0]=='.' ) break;
330         if ( p[0]==',' ) nals++;
331     }
332     if ( b->alt[0] && !*p ) nals++;
333
334     if ( nals>4 )
335     {
336         if ( *b->ref=='N' ) return 0;
337         fprintf(stderr,"Not ready for this, more than 4 alleles at %d: %s, %s\n", b->pos+1, b->ref,b->alt); 
338         exit(1);
339     }
340
341     // find PL, DV and DP FORMAT indexes
342     uint8_t *pl = NULL;
343     int i, npl = 0, idp = -1, idv = -1;
344     for (i = 0; i < b->n_gi; ++i) 
345     {
346         if (b->gi[i].fmt == bcf_str2int("PL", 2)) 
347         {
348             pl  = (uint8_t*)b->gi[i].data;
349             npl = b->gi[i].len;
350         }
351         else if (b->gi[i].fmt == bcf_str2int("DP", 2))  idp=i;
352         else if (b->gi[i].fmt == bcf_str2int("DV", 2))  idv=i;
353     }
354     if ( nals==1 ) 
355     {
356         if ( !var_only ) _bcf1_set_ref(b, idp);
357         return 1;
358     }
359     if ( !pl ) return -1;
360
361     assert(ma->q2p[0] == 1);
362
363     // Init P(D|G)
364     int npdg = nals*(nals+1)/2;
365     double *pdg,*_pdg;
366     _pdg = pdg = malloc(sizeof(double)*ma->n*npdg);
367     for (i=0; i<ma->n; i++)
368     {
369         int j; 
370         double sum = 0;
371         for (j=0; j<npdg; j++)
372         {
373             //_pdg[j] = pow(10,-0.1*pl[j]); 
374             _pdg[j] = ma->q2p[pl[j]];
375             sum += _pdg[j];
376         }
377         if ( sum )
378             for (j=0; j<npdg; j++) _pdg[j] /= sum;
379         _pdg += npdg;
380         pl += npl;
381     }
382
383     if ((p = strstr(b->info, "QS=")) == 0) { fprintf(stderr,"INFO/QS is required with -m, exiting\n"); exit(1); }
384     double qsum[4];
385     if ( sscanf(p+3,"%lf,%lf,%lf,%lf",&qsum[0],&qsum[1],&qsum[2],&qsum[3])!=4 ) { fprintf(stderr,"Could not parse %s\n",p); exit(1); }
386
387
388     // Calculate the most likely combination of alleles, remembering the most and second most likely set
389     int ia,ib,ic, max_als=0, max_als2=0;
390     double ref_lk = 0, max_lk = INT_MIN, max_lk2 = INT_MIN, lk_sum = INT_MIN, lk_sums[3];
391     for (ia=0; ia<nals; ia++)
392     {
393         double lk_tot = 0;
394         int iaa = (ia+1)*(ia+2)/2-1;
395         int isample;
396         for (isample=0; isample<ma->n; isample++)
397         {
398             double *p = pdg + isample*npdg;
399             // assert( log(p[iaa]) <= 0 );
400             lk_tot += log(p[iaa]);
401         }
402         if ( ia==0 ) ref_lk = lk_tot;
403         if ( max_lk<lk_tot ) { max_lk2 = max_lk; max_als2 = max_als; max_lk = lk_tot; max_als = 1<<ia; }
404         else if ( max_lk2<lk_tot ) { max_lk2 = lk_tot; max_als2 = 1<<ia; }
405         lk_sum = lk_tot>lk_sum ? lk_tot + log(1+exp(lk_sum-lk_tot)) : lk_sum + log(1+exp(lk_tot-lk_sum));
406     }
407     lk_sums[0] = lk_sum;
408     if ( nals>1 )
409     {
410         for (ia=0; ia<nals; ia++)
411         {
412             if ( qsum[ia]==0 ) continue;
413             int iaa = (ia+1)*(ia+2)/2-1;
414             for (ib=0; ib<ia; ib++)
415             {
416                 if ( qsum[ib]==0 ) continue;
417                 double lk_tot = 0;
418                 double fa  = qsum[ia]/(qsum[ia]+qsum[ib]);
419                 double fb  = qsum[ib]/(qsum[ia]+qsum[ib]);
420                 double fab = 2*fa*fb; fa *= fa; fb *= fb;
421                 int isample, ibb = (ib+1)*(ib+2)/2-1, iab = iaa - ia + ib;
422                 for (isample=0; isample<ma->n; isample++)
423                 {
424                     double *p = pdg + isample*npdg;
425                     //assert( log(fa*p[iaa] + fb*p[ibb] + fab*p[iab]) <= 0 );
426                     if ( b->ploidy && b->ploidy[isample]==1 )
427                         lk_tot +=  log(fa*p[iaa] + fb*p[ibb]);
428                     else 
429                         lk_tot +=  log(fa*p[iaa] + fb*p[ibb] + fab*p[iab]);
430                 }
431                 if ( max_lk<lk_tot ) { max_lk2 = max_lk; max_als2 = max_als; max_lk = lk_tot; max_als = 1<<ia|1<<ib; }
432                 else if ( max_lk2<lk_tot ) { max_lk2 = lk_tot; max_als2 = 1<<ia|1<<ib; }
433                 lk_sum = lk_tot>lk_sum ? lk_tot + log(1+exp(lk_sum-lk_tot)) : lk_sum + log(1+exp(lk_tot-lk_sum));
434             }
435         }
436         lk_sums[1] = lk_sum;
437     }
438     if ( nals>2 )
439     {
440         for (ia=0; ia<nals; ia++)
441         {
442             if ( qsum[ia]==0 ) continue;
443             int iaa = (ia+1)*(ia+2)/2-1;
444             for (ib=0; ib<ia; ib++)
445             {
446                 if ( qsum[ib]==0 ) continue;
447                 int ibb = (ib+1)*(ib+2)/2-1; 
448                 int iab = iaa - ia + ib;
449                 for (ic=0; ic<ib; ic++)
450                 {
451                     if ( qsum[ic]==0 ) continue;
452                     double lk_tot = 0;
453                     double fa  = qsum[ia]/(qsum[ia]+qsum[ib]+qsum[ic]);
454                     double fb  = qsum[ib]/(qsum[ia]+qsum[ib]+qsum[ic]);
455                     double fc  = qsum[ic]/(qsum[ia]+qsum[ib]+qsum[ic]);
456                     double fab = 2*fa*fb, fac = 2*fa*fc, fbc = 2*fb*fc; fa *= fa; fb *= fb; fc *= fc;
457                     int isample, icc = (ic+1)*(ic+2)/2-1;
458                     int iac = iaa - ia + ic, ibc = ibb - ib + ic;
459                     for (isample=0; isample<ma->n; isample++)
460                     {
461                         double *p = pdg + isample*npdg;
462                         //assert( log(fa*p[iaa] + fb*p[ibb] + fc*p[icc] + fab*p[iab] + fac*p[iac] + fbc*p[ibc]) <= 0 );
463                         if ( b->ploidy && b->ploidy[isample]==1 ) 
464                             lk_tot += log(fa*p[iaa] + fb*p[ibb] + fc*p[icc]);
465                         else
466                             lk_tot += log(fa*p[iaa] + fb*p[ibb] + fc*p[icc] + fab*p[iab] + fac*p[iac] + fbc*p[ibc]);
467                     }
468                     if ( max_lk<lk_tot ) { max_lk2 = max_lk; max_als2 = max_als; max_lk = lk_tot; max_als = 1<<ia|1<<ib|1<<ic; }
469                     else if ( max_lk2<lk_tot ) { max_lk2 = lk_tot; max_als2 = 1<<ia|1<<ib|1<<ic; }
470                     lk_sum = lk_tot>lk_sum ? lk_tot + log(1+exp(lk_sum-lk_tot)) : lk_sum + log(1+exp(lk_tot-lk_sum));
471                 }
472             }
473         }
474         lk_sums[2] = lk_sum;
475     }
476
477     // Should we add another allele, does it increase the likelihood significantly?
478     int n1=0, n2=0;
479     for (i=0; i<nals; i++) if ( max_als&1<<i) n1++;
480     for (i=0; i<nals; i++) if ( max_als2&1<<i) n2++;
481     if ( n2<n1 && kf_gammap(1,2.0*(max_lk-max_lk2))<threshold )
482     {
483         // the threshold not exceeded, use the second most likely set with fewer alleles
484         max_lk  = max_lk2;
485         max_als = max_als2;
486         n1 = n2;
487     }
488     lk_sum = lk_sums[n1-1];
489
490     // Get the BCF record ready for GT and GQ
491     kstring_t s;
492     int old_n_gi = b->n_gi;
493     s.m = b->m_str; s.l = b->l_str - 1; s.s = b->str;
494     kputs(":GT:GQ", &s); kputc('\0', &s);
495     b->m_str = s.m; b->l_str = s.l; b->str = s.s;
496     bcf_sync(b);
497
498     // Call GTs
499     int isample, gts=0, ac[4] = {0,0,0,0};
500     int nRR = 0, nAA = 0, nRA = 0, max_dv = 0, dp_nref = 0;
501     for (isample = 0; isample < b->n_smpl; isample++) 
502     {
503         int ploidy = b->ploidy ? b->ploidy[isample] : 2;
504         double *p = pdg + isample*npdg;
505         int ia, als = 0;
506         double lk = 0, lk_s = 0;
507         for (ia=0; ia<nals; ia++)
508         {
509             if ( !(max_als&1<<ia) ) continue;
510             int iaa = (ia+1)*(ia+2)/2-1;
511             double _lk = p[iaa]*qsum[ia]*qsum[ia];
512             if ( _lk > lk ) { lk = _lk; als = ia<<3 | ia; }
513             lk_s += _lk;
514         }
515         if ( ploidy==2 ) 
516         {
517             for (ia=0; ia<nals; ia++)
518             {
519                 if ( !(max_als&1<<ia) ) continue;
520                 int iaa = (ia+1)*(ia+2)/2-1;
521                 for (ib=0; ib<ia; ib++)
522                 {
523                     if ( !(max_als&1<<ib) ) continue;
524                     int iab = iaa - ia + ib;
525                     double _lk = 2*qsum[ia]*qsum[ib]*p[iab];
526                     if ( _lk > lk ) { lk = _lk; als = ib<<3 | ia; }
527                     lk_s += _lk;
528                 }
529             }
530         }
531         lk = -log(1-lk/lk_s)/0.2302585;
532         int dp = 0;
533         if ( idp>=0 && (dp=((uint16_t*)b->gi[idp].data)[isample])==0 )
534         {
535             // no coverage
536             ((uint8_t*)b->gi[old_n_gi].data)[isample]   = 1<<7;
537             ((uint8_t*)b->gi[old_n_gi+1].data)[isample] = 0;
538             continue;
539         }
540         if ( lk>99 ) lk = 99;
541         ((uint8_t*)b->gi[old_n_gi].data)[isample]   = als;
542         ((uint8_t*)b->gi[old_n_gi+1].data)[isample] = (int)lk;
543
544         // For MDV annotation
545         int dv;
546         if ( als && idv>=0 && (dv=((uint16_t*)b->gi[idv].data)[isample]) )
547         {
548             if ( max_dv < dv ) max_dv = dv;
549             dp_nref += dp;
550         }
551
552         // For HWE annotation; multiple ALT alleles treated as one
553         if ( !als ) nRR++;
554         else if ( !(als>>3&7) || !(als&7) ) nRA++;
555         else nAA++;
556
557         gts |= 1<<(als>>3&7) | 1<<(als&7);
558         ac[ als>>3&7 ]++;
559         ac[ als&7 ]++;
560     }
561     free(pdg);
562     bcf_fit_alt(b,max_als);
563
564     // The VCF spec is ambiguous about QUAL: is it the probability of anything else
565     //  (that is QUAL(non-ref) = P(ref)+P(any non-ref other than ALT)) or is it
566     //  QUAL(non-ref)=P(ref) and QUAL(ref)=1-P(ref)? Assuming the latter.
567     b->qual = gts>1 ? -4.343*(ref_lk - lk_sum) : -4.343*log(1-exp(ref_lk - lk_sum));
568     if ( b->qual>999 ) b->qual = 999;
569
570     // Prepare BCF for output: ref, alt, filter, info, format
571     memset(&s, 0, sizeof(kstring_t)); kputc('\0', &s); 
572     kputs(b->ref, &s); kputc('\0', &s);
573     kputs(b->alt, &s); kputc('\0', &s); kputc('\0', &s);
574     {
575         int an=0, nalts=0;
576         for (i=0; i<nals; i++)
577         {
578             an += ac[i];
579             if ( i>0 && ac[i] ) nalts++;
580         }
581         ksprintf(&s, "AN=%d;", an);
582         if ( nalts )
583         {
584             kputs("AC=", &s);
585             for (i=1; i<nals; i++)
586             {
587                 if ( !(gts&1<<i) ) continue;
588                 nalts--;
589                 ksprintf(&s,"%d", ac[i]);
590                 if ( nalts>0 ) kputc(',', &s);
591             }
592             kputc(';', &s);
593         }
594         kputs(b->info, &s); 
595         anno16_t a;
596         int has_I16 = test16(b, &a) >= 0? 1 : 0;
597         if (has_I16 )
598         {
599             if ( a.is_tested) ksprintf(&s, ";PV4=%.2g,%.2g,%.2g,%.2g", a.p[0], a.p[1], a.p[2], a.p[3]);
600             ksprintf(&s, ";DP4=%d,%d,%d,%d;MQ=%d", a.d[0], a.d[1], a.d[2], a.d[3], a.mq);
601             ksprintf(&s, ";QBD=%e", b->qual/(a.d[0] + a.d[1] + a.d[2] + a.d[3]));
602             if ( dp_nref ) ksprintf(&s, ";QBDNR=%e", b->qual/dp_nref);
603             if ( max_dv ) ksprintf(&s, ";MDV=%d", max_dv);
604         }
605         if ( nAA+nRA )
606         {
607             double hwe = calc_hwe(nAA, nRR, nRA);
608             ksprintf(&s, ";HWE=%e", hwe);
609         }
610         kputc('\0', &s);
611         rm_info(&s, "I16=");
612         rm_info(&s, "QS=");
613     }
614     kputs(b->fmt, &s); kputc('\0', &s);
615     free(b->str);
616     b->m_str = s.m; b->l_str = s.l; b->str = s.s;
617     bcf_sync(b);
618
619     return gts;
620 }
621
622 static int cal_pdg(const bcf1_t *b, bcf_p1aux_t *ma)
623 {
624     int i, j;
625     long *p, tmp;
626     p = alloca(b->n_alleles * sizeof(long));
627     memset(p, 0, sizeof(long) * b->n_alleles);
628     for (j = 0; j < ma->n; ++j) {
629         const uint8_t *pi = ma->PL + j * ma->PL_len;
630         double *pdg = ma->pdg + j * 3;
631         pdg[0] = ma->q2p[pi[2]]; pdg[1] = ma->q2p[pi[1]]; pdg[2] = ma->q2p[pi[0]];
632         for (i = 0; i < b->n_alleles; ++i)
633             p[i] += (int)pi[(i+1)*(i+2)/2-1];
634     }
635     for (i = 0; i < b->n_alleles; ++i) p[i] = p[i]<<4 | i;
636     for (i = 1; i < b->n_alleles; ++i) // insertion sort
637         for (j = i; j > 0 && p[j] < p[j-1]; --j)
638             tmp = p[j], p[j] = p[j-1], p[j-1] = tmp;
639     for (i = b->n_alleles - 1; i >= 0; --i)
640         if ((p[i]&0xf) == 0) break;
641     return i;
642 }
643
644
645 int bcf_p1_call_gt(const bcf_p1aux_t *ma, double f0, int k)
646 {
647         double sum, g[3];
648         double max, f3[3], *pdg = ma->pdg + k * 3;
649         int q, i, max_i, ploidy;
650         ploidy = ma->ploidy? ma->ploidy[k] : 2;
651         if (ploidy == 2) {
652                 f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*f0;
653         } else {
654                 f3[0] = 1. - f0; f3[1] = 0; f3[2] = f0;
655         }
656         for (i = 0, sum = 0.; i < 3; ++i)
657                 sum += (g[i] = pdg[i] * f3[i]);
658         for (i = 0, max = -1., max_i = 0; i < 3; ++i) {
659                 g[i] /= sum;
660                 if (g[i] > max) max = g[i], max_i = i;
661         }
662         max = 1. - max;
663         if (max < 1e-308) max = 1e-308;
664         q = (int)(-4.343 * log(max) + .499);
665         if (q > 99) q = 99;
666         return q<<2|max_i;
667 }
668
669 #define TINY 1e-20
670
671 static void mc_cal_y_core(bcf_p1aux_t *ma, int beg)
672 {
673         double *z[2], *tmp, *pdg;
674         int _j, last_min, last_max;
675         assert(beg == 0 || ma->M == ma->n*2);
676         z[0] = ma->z;
677         z[1] = ma->zswap;
678         pdg = ma->pdg;
679         memset(z[0], 0, sizeof(double) * (ma->M + 1));
680         memset(z[1], 0, sizeof(double) * (ma->M + 1));
681         z[0][0] = 1.;
682         last_min = last_max = 0;
683         ma->t = 0.;
684         if (ma->M == ma->n * 2) {
685                 int M = 0;
686                 for (_j = beg; _j < ma->n; ++_j) {
687                         int k, j = _j - beg, _min = last_min, _max = last_max, M0;
688                         double p[3], sum;
689                         M0 = M; M += 2;
690                         pdg = ma->pdg + _j * 3;
691                         p[0] = pdg[0]; p[1] = 2. * pdg[1]; p[2] = pdg[2];
692                         for (; _min < _max && z[0][_min] < TINY; ++_min) z[0][_min] = z[1][_min] = 0.;
693                         for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.;
694                         _max += 2;
695                         if (_min == 0) k = 0, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k];
696                         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];
697                         for (k = _min < 2? 2 : _min; k <= _max; ++k)
698                                 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];
699                         for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
700                         ma->t += log(sum / (M * (M - 1.)));
701                         for (k = _min; k <= _max; ++k) z[1][k] /= sum;
702                         if (_min >= 1) z[1][_min-1] = 0.;
703                         if (_min >= 2) z[1][_min-2] = 0.;
704                         if (j < ma->n - 1) z[1][_max+1] = z[1][_max+2] = 0.;
705                         if (_j == ma->n1 - 1) { // set pop1; ma->n1==-1 when unset
706                                 ma->t1 = ma->t;
707                                 memcpy(ma->z1, z[1], sizeof(double) * (ma->n1 * 2 + 1));
708                         }
709                         tmp = z[0]; z[0] = z[1]; z[1] = tmp;
710                         last_min = _min; last_max = _max;
711                 }
712                 //for (_j = 0; _j < last_min; ++_j) z[0][_j] = 0.; // TODO: are these necessary?
713                 //for (_j = last_max + 1; _j < ma->M; ++_j) z[0][_j] = 0.;
714         } else { // this block is very similar to the block above; these two might be merged in future
715                 int j, M = 0;
716                 for (j = 0; j < ma->n; ++j) {
717                         int k, M0, _min = last_min, _max = last_max;
718                         double p[3], sum;
719                         pdg = ma->pdg + j * 3;
720                         for (; _min < _max && z[0][_min] < TINY; ++_min) z[0][_min] = z[1][_min] = 0.;
721                         for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.;
722                         M0 = M;
723                         M += ma->ploidy[j];
724                         if (ma->ploidy[j] == 1) {
725                                 p[0] = pdg[0]; p[1] = pdg[2];
726                                 _max++;
727                                 if (_min == 0) k = 0, z[1][k] = (M0+1-k) * p[0] * z[0][k];
728                                 for (k = _min < 1? 1 : _min; k <= _max; ++k)
729                                         z[1][k] = (M0+1-k) * p[0] * z[0][k] + k * p[1] * z[0][k-1];
730                                 for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
731                                 ma->t += log(sum / M);
732                                 for (k = _min; k <= _max; ++k) z[1][k] /= sum;
733                                 if (_min >= 1) z[1][_min-1] = 0.;
734                                 if (j < ma->n - 1) z[1][_max+1] = 0.;
735                         } else if (ma->ploidy[j] == 2) {
736                                 p[0] = pdg[0]; p[1] = 2 * pdg[1]; p[2] = pdg[2];
737                                 _max += 2;
738                                 if (_min == 0) k = 0, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k];
739                                 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];
740                                 for (k = _min < 2? 2 : _min; k <= _max; ++k)
741                                         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];
742                                 for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
743                                 ma->t += log(sum / (M * (M - 1.)));
744                                 for (k = _min; k <= _max; ++k) z[1][k] /= sum;
745                                 if (_min >= 1) z[1][_min-1] = 0.;
746                                 if (_min >= 2) z[1][_min-2] = 0.;
747                                 if (j < ma->n - 1) z[1][_max+1] = z[1][_max+2] = 0.;
748                         }
749                         tmp = z[0]; z[0] = z[1]; z[1] = tmp;
750                         last_min = _min; last_max = _max;
751                 }
752         }
753         if (z[0] != ma->z) memcpy(ma->z, z[0], sizeof(double) * (ma->M + 1));
754 }
755
756 static void mc_cal_y(bcf_p1aux_t *ma)
757 {
758         if (ma->n1 > 0 && ma->n1 < ma->n && ma->M == ma->n * 2) { // NB: ma->n1 is ineffective when there are haploid samples
759                 int k;
760                 long double x;
761                 memset(ma->z1, 0, sizeof(double) * (2 * ma->n1 + 1));
762                 memset(ma->z2, 0, sizeof(double) * (2 * (ma->n - ma->n1) + 1));
763                 ma->t1 = ma->t2 = 0.;
764                 mc_cal_y_core(ma, ma->n1);
765                 ma->t2 = ma->t;
766                 memcpy(ma->z2, ma->z, sizeof(double) * (2 * (ma->n - ma->n1) + 1));
767                 mc_cal_y_core(ma, 0);
768                 // rescale z
769                 x = expl(ma->t - (ma->t1 + ma->t2));
770                 for (k = 0; k <= ma->M; ++k) ma->z[k] *= x;
771         } else mc_cal_y_core(ma, 0);
772 }
773
774 #define CONTRAST_TINY 1e-30
775
776 extern double kf_gammaq(double s, double z); // incomplete gamma function for chi^2 test
777
778 static inline double chi2_test(int a, int b, int c, int d)
779 {
780         double x, z;
781         x = (double)(a+b) * (c+d) * (b+d) * (a+c);
782         if (x == 0.) return 1;
783         z = a * d - b * c;
784         return kf_gammaq(.5, .5 * z * z * (a+b+c+d) / x);
785 }
786
787 // chi2=(a+b+c+d)(ad-bc)^2/[(a+b)(c+d)(a+c)(b+d)]
788 static inline double contrast2_aux(const bcf_p1aux_t *p1, double sum, int k1, int k2, double x[3])
789 {
790         double p = p1->phi[k1+k2] * p1->z1[k1] * p1->z2[k2] / sum * p1->hg[k1][k2];
791         int n1 = p1->n1, n2 = p1->n - p1->n1;
792         if (p < CONTRAST_TINY) return -1;
793         if (.5*k1/n1 < .5*k2/n2) x[1] += p;
794         else if (.5*k1/n1 > .5*k2/n2) x[2] += p;
795         else x[0] += p;
796         return p * chi2_test(k1, k2, (n1<<1) - k1, (n2<<1) - k2);
797 }
798
799 static double contrast2(bcf_p1aux_t *p1, double ret[3])
800 {
801         int k, k1, k2, k10, k20, n1, n2;
802         double sum;
803         // get n1 and n2
804         n1 = p1->n1; n2 = p1->n - p1->n1;
805         if (n1 <= 0 || n2 <= 0) return 0.;
806         if (p1->hg == 0) { // initialize the hypergeometric distribution
807                 /* NB: the hg matrix may take a lot of memory when there are many samples. There is a way
808                    to avoid precomputing this matrix, but it is slower and quite intricate. The following
809                    computation in this block can be accelerated with a similar strategy, but perhaps this
810                    is not a serious concern for now. */
811                 double tmp = lgamma(2*(n1+n2)+1) - (lgamma(2*n1+1) + lgamma(2*n2+1));
812                 p1->hg = calloc(2*n1+1, sizeof(void*));
813                 for (k1 = 0; k1 <= 2*n1; ++k1) {
814                         p1->hg[k1] = calloc(2*n2+1, sizeof(double));
815                         for (k2 = 0; k2 <= 2*n2; ++k2)
816                                 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));
817                 }
818         }
819         { // compute
820                 long double suml = 0;
821                 for (k = 0; k <= p1->M; ++k) suml += p1->phi[k] * p1->z[k];
822                 sum = suml;
823         }
824         { // get the max k1 and k2
825                 double max;
826                 int max_k;
827                 for (k = 0, max = 0, max_k = -1; k <= 2*n1; ++k) {
828                         double x = p1->phi1[k] * p1->z1[k];
829                         if (x > max) max = x, max_k = k;
830                 }
831                 k10 = max_k;
832                 for (k = 0, max = 0, max_k = -1; k <= 2*n2; ++k) {
833                         double x = p1->phi2[k] * p1->z2[k];
834                         if (x > max) max = x, max_k = k;
835                 }
836                 k20 = max_k;
837         }
838         { // 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.
839                 double x[3], y;
840                 long double z = 0., L[2];
841                 x[0] = x[1] = x[2] = 0; L[0] = L[1] = 0;
842                 for (k1 = k10; k1 >= 0; --k1) {
843                         for (k2 = k20; k2 >= 0; --k2) {
844                                 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
845                                 else z += y;
846                         }
847                         for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
848                                 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
849                                 else z += y;
850                         }
851                 }
852                 ret[0] = x[0]; ret[1] = x[1]; ret[2] = x[2];
853                 x[0] = x[1] = x[2] = 0;
854                 for (k1 = k10 + 1; k1 <= 2*n1; ++k1) {
855                         for (k2 = k20; k2 >= 0; --k2) {
856                                 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
857                                 else z += y;
858                         }
859                         for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
860                                 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
861                                 else z += y;
862                         }
863                 }
864                 ret[0] += x[0]; ret[1] += x[1]; ret[2] += x[2];
865                 if (ret[0] + ret[1] + ret[2] < 0.95) { // in case of bad things happened
866                         ret[0] = ret[1] = ret[2] = 0; L[0] = L[1] = 0;
867                         for (k1 = 0, z = 0.; k1 <= 2*n1; ++k1)
868                                 for (k2 = 0; k2 <= 2*n2; ++k2)
869                                         if ((y = contrast2_aux(p1, sum, k1, k2, ret)) >= 0) z += y;
870                         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...
871                                 z = 1.0, ret[0] = ret[1] = ret[2] = 1./3;
872                 }
873                 return (double)z;
874         }
875 }
876
877 static double mc_cal_afs(bcf_p1aux_t *ma, double *p_ref_folded, double *p_var_folded)
878 {
879         int k;
880         long double sum = 0., sum2;
881         double *phi = ma->is_indel? ma->phi_indel : ma->phi;
882         memset(ma->afs1, 0, sizeof(double) * (ma->M + 1));
883         mc_cal_y(ma);
884         // compute AFS
885         for (k = 0, sum = 0.; k <= ma->M; ++k)
886                 sum += (long double)phi[k] * ma->z[k];
887         for (k = 0; k <= ma->M; ++k) {
888                 ma->afs1[k] = phi[k] * ma->z[k] / sum;
889                 if (isnan(ma->afs1[k]) || isinf(ma->afs1[k])) return -1.;
890         }
891         // compute folded variant probability
892         for (k = 0, sum = 0.; k <= ma->M; ++k)
893                 sum += (long double)(phi[k] + phi[ma->M - k]) / 2. * ma->z[k];
894         for (k = 1, sum2 = 0.; k < ma->M; ++k)
895                 sum2 += (long double)(phi[k] + phi[ma->M - k]) / 2. * ma->z[k];
896         *p_var_folded = sum2 / sum;
897         *p_ref_folded = (phi[k] + phi[ma->M - k]) / 2. * (ma->z[ma->M] + ma->z[0]) / sum;
898         // the expected frequency
899         for (k = 0, sum = 0.; k <= ma->M; ++k) {
900                 ma->afs[k] += ma->afs1[k];
901                 sum += k * ma->afs1[k];
902         }
903         return sum / ma->M;
904 }
905
906 int bcf_p1_cal(const bcf1_t *b, int do_contrast, bcf_p1aux_t *ma, bcf_p1rst_t *rst)
907 {
908         int i, k;
909         long double sum = 0.;
910         ma->is_indel = bcf_is_indel(b);
911         rst->perm_rank = -1;
912         // set PL and PL_len
913         for (i = 0; i < b->n_gi; ++i) {
914                 if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
915                         ma->PL = (uint8_t*)b->gi[i].data;
916                         ma->PL_len = b->gi[i].len;
917                         break;
918                 }
919         }
920         if (i == b->n_gi) return -1; // no PL
921         if (b->n_alleles < 2) return -1; // FIXME: find a better solution
922         // 
923         rst->rank0 = cal_pdg(b, ma);
924         rst->f_exp = mc_cal_afs(ma, &rst->p_ref_folded, &rst->p_var_folded);
925         rst->p_ref = ma->afs1[ma->M];
926         for (k = 0, sum = 0.; k < ma->M; ++k)
927                 sum += ma->afs1[k];
928         rst->p_var = (double)sum;
929         { // compute the allele count
930                 double max = -1;
931                 rst->ac = -1;
932                 for (k = 0; k <= ma->M; ++k)
933                         if (max < ma->z[k]) max = ma->z[k], rst->ac = k;
934                 rst->ac = ma->M - rst->ac;
935         }
936         // calculate f_flat and f_em
937         for (k = 0, sum = 0.; k <= ma->M; ++k)
938                 sum += (long double)ma->z[k];
939         rst->f_flat = 0.;
940         for (k = 0; k <= ma->M; ++k) {
941                 double p = ma->z[k] / sum;
942                 rst->f_flat += k * p;
943         }
944         rst->f_flat /= ma->M;
945         { // estimate equal-tail credible interval (95% level)
946                 int l, h;
947                 double p;
948                 for (i = 0, p = 0.; i <= ma->M; ++i)
949                         if (p + ma->afs1[i] > 0.025) break;
950                         else p += ma->afs1[i];
951                 l = i;
952                 for (i = ma->M, p = 0.; i >= 0; --i)
953                         if (p + ma->afs1[i] > 0.025) break;
954                         else p += ma->afs1[i];
955                 h = i;
956                 rst->cil = (double)(ma->M - h) / ma->M; rst->cih = (double)(ma->M - l) / ma->M;
957         }
958         if (ma->n1 > 0) { // compute LRT
959                 double max0, max1, max2;
960                 for (k = 0, max0 = -1; k <= ma->M; ++k)
961                         if (max0 < ma->z[k]) max0 = ma->z[k];
962                 for (k = 0, max1 = -1; k <= ma->n1 * 2; ++k)
963                         if (max1 < ma->z1[k]) max1 = ma->z1[k];
964                 for (k = 0, max2 = -1; k <= ma->M - ma->n1 * 2; ++k)
965                         if (max2 < ma->z2[k]) max2 = ma->z2[k];
966                 rst->lrt = log(max1 * max2 / max0);
967                 rst->lrt = rst->lrt < 0? 1 : kf_gammaq(.5, rst->lrt);
968         } else rst->lrt = -1.0;
969         rst->cmp[0] = rst->cmp[1] = rst->cmp[2] = rst->p_chi2 = -1.0;
970         if (do_contrast && rst->p_var > 0.5) // skip contrast2() if the locus is a strong non-variant
971                 rst->p_chi2 = contrast2(ma, rst->cmp);
972         return 0;
973 }
974
975 void bcf_p1_dump_afs(bcf_p1aux_t *ma)
976 {
977         int k;
978         fprintf(stderr, "[afs]");
979         for (k = 0; k <= ma->M; ++k)
980                 fprintf(stderr, " %d:%.3lf", k, ma->afs[ma->M - k]);
981         fprintf(stderr, "\n");
982         memset(ma->afs, 0, sizeof(double) * (ma->M + 1));
983 }