KSTREAM_INIT(gzFile, gzread, 16384)
#define MC_MAX_EM_ITER 16
-#define MC_EM_EPS 1e-4
+#define MC_EM_EPS 1e-5
#define MC_DEF_INDEL 0.15
unsigned char seq_nt4_table[256] = {
double *z, *zswap; // aux for afs
double *z1, *z2, *phi1, *phi2; // only calculated when n1 is set
double **hg; // hypergeometric distribution
+ double *lf; // log factorial
double t, t1, t2;
double *afs, *afs1; // afs: accumulative AFS; afs1: site posterior distribution
const uint8_t *PL; // point to PL
ma->z2 = calloc(ma->M + 1, sizeof(double));
ma->afs = calloc(ma->M + 1, sizeof(double));
ma->afs1 = calloc(ma->M + 1, sizeof(double));
+ ma->lf = calloc(ma->M + 1, sizeof(double));
for (i = 0; i < 256; ++i)
ma->q2p[i] = pow(10., -i / 10.);
+ for (i = 0; i <= ma->M; ++i) ma->lf[i] = lgamma(i + 1);
bcf_p1_init_prior(ma, MC_PTYPE_FULL, 1e-3); // the simplest prior
return ma;
}
{
if (ma) {
int k;
+ free(ma->lf);
if (ma->hg && ma->n1 > 0) {
for (k = 0; k <= 2*ma->n1; ++k) free(ma->hg[k]);
free(ma->hg);
static int cal_pdg(const bcf1_t *b, bcf_p1aux_t *ma)
{
int i, j;
- long *p, tmp;
- p = alloca(b->n_alleles * sizeof(long));
- memset(p, 0, sizeof(long) * b->n_alleles);
+ int n = (b->n_alleles+1)*b->n_alleles/2;
+ double *lk = alloca(n * sizeof(long));
+ memset(lk, 0, sizeof(double) * n);
for (j = 0; j < ma->n; ++j) {
const uint8_t *pi = ma->PL + j * ma->PL_len;
double *pdg = ma->pdg + j * 3;
pdg[0] = ma->q2p[pi[2]]; pdg[1] = ma->q2p[pi[1]]; pdg[2] = ma->q2p[pi[0]];
- for (i = 0; i < b->n_alleles; ++i)
- p[i] += (int)pi[(i+1)*(i+2)/2-1];
- }
- for (i = 0; i < b->n_alleles; ++i) p[i] = p[i]<<4 | i;
- for (i = 1; i < b->n_alleles; ++i) // insertion sort
- for (j = i; j > 0 && p[j] < p[j-1]; --j)
- tmp = p[j], p[j] = p[j-1], p[j-1] = tmp;
- for (i = b->n_alleles - 1; i >= 0; --i)
- if ((p[i]&0xf) == 0) break;
- return i;
-}
-// f0 is the reference allele frequency
-static double mc_freq_iter(double f0, const bcf_p1aux_t *ma)
-{
- double f, f3[3];
- int i;
- f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*f0;
- for (i = 0, f = 0.; i < ma->n; ++i) {
- double *pdg;
- pdg = ma->pdg + i * 3;
- f += (pdg[1] * f3[1] + 2. * pdg[2] * f3[2])
- / (pdg[0] * f3[0] + pdg[1] * f3[1] + pdg[2] * f3[2]);
- }
- f /= ma->n * 2.;
- return f;
+ for (i=0; i<n; i++) lk[i] += pi[i];
+ }
+
+ double norm=lk[0];
+ for (i=1; i<n; i++) if (lk[i]<norm) norm=lk[i];
+ #if DBG
+ for (i=0,j=0; i<b->n_alleles; i++)
+ {
+ int k; for (k=0; k<=i; k++) printf("%.0f\t", lk[j++]);
+ printf("\n");
+ }
+ #endif
+ for (i=0; i<n; i++) lk[i] = pow(10,-0.1*(lk[i]-norm));
+
+ // Find out the most likely alleles. In contrast to the original version,
+ // ALT alleles may not be printed when they are more likely than REF but
+ // significantly less likely than the most likely ALT. The only criterion
+ // is the LK ratio now. To obtain behaviour similar to the original one,
+ // use the pref variant below.
+ double pmax=0; //,pref=0;
+ n = ma->is_indel ? b->n_alleles : b->n_alleles-1;
+ for (i=0; i<n; i++)
+ {
+ double pr=0;
+ int k=i*(i+1)/2;
+ for (j=0; j<=i; j++) { pr+=lk[k]; k++; }
+ for (j=i+1; j<b->n_alleles; j++) { k=j*(j+1)/2+i; pr+=lk[k]; }
+ #if DBG
+ printf("%d\t%e\n", i,pr);
+ #endif
+ if (pmax<pr) pmax=pr;
+ // if (i==0) pref=pr;
+ // if (pr<pref && pr/pmax < 1e-4) break;
+ if (pr/pmax < 1e-4) break; // Assuming the alleles are sorted by the lk
+ }
+ return i-1;
}
int bcf_p1_call_gt(const bcf_p1aux_t *ma, double f0, int k)
}
// chi2=(a+b+c+d)(ad-bc)^2/[(a+b)(c+d)(a+c)(b+d)]
-static inline double contrast2_aux(const bcf_p1aux_t *p1, double sum, int n1, int n2, int k1, int k2, double x[3])
+static inline double contrast2_aux(const bcf_p1aux_t *p1, double sum, int k1, int k2, double x[3])
{
double p = p1->phi[k1+k2] * p1->z1[k1] * p1->z2[k2] / sum * p1->hg[k1][k2];
+ int n1 = p1->n1, n2 = p1->n - p1->n1;
if (p < CONTRAST_TINY) return -1;
if (.5*k1/n1 < .5*k2/n2) x[1] += p;
else if (.5*k1/n1 > .5*k2/n2) x[2] += p;
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));
}
}
- { // compute sum1 and sum2
+ { // compute
long double suml = 0;
for (k = 0; k <= p1->M; ++k) suml += p1->phi[k] * p1->z[k];
sum = suml;
}
- { // get the mean k1 and k2
+ { // get the max k1 and k2
double max;
int max_k;
for (k = 0, max = 0, max_k = -1; k <= 2*n1; ++k) {
}
{ // 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.
double x[3], y;
- long double z = 0.;
- x[0] = x[1] = x[2] = 0;
+ long double z = 0., L[2];
+ x[0] = x[1] = x[2] = 0; L[0] = L[1] = 0;
for (k1 = k10; k1 >= 0; --k1) {
for (k2 = k20; k2 >= 0; --k2) {
- if ((y = contrast2_aux(p1, sum, n1, n2, k1, k2, x)) < 0) break;
+ if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
else z += y;
}
for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
- if ((y = contrast2_aux(p1, sum, n1, n2, k1, k2, x)) < 0) break;
+ if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
else z += y;
}
}
x[0] = x[1] = x[2] = 0;
for (k1 = k10 + 1; k1 <= 2*n1; ++k1) {
for (k2 = k20; k2 >= 0; --k2) {
- if ((y = contrast2_aux(p1, sum, n1, n2, k1, k2, x)) < 0) break;
+ if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
else z += y;
}
for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
- if ((y = contrast2_aux(p1, sum, n1, n2, k1, k2, x)) < 0) break;
+ if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
else z += y;
}
}
ret[0] += x[0]; ret[1] += x[1]; ret[2] += x[2];
- if (ret[0] + ret[1] + ret[2] < 0.99) { // in case of bad things happened
- ret[0] = ret[1] = ret[2] = 0;
+ if (ret[0] + ret[1] + ret[2] < 0.95) { // in case of bad things happened
+ ret[0] = ret[1] = ret[2] = 0; L[0] = L[1] = 0;
for (k1 = 0, z = 0.; k1 <= 2*n1; ++k1)
for (k2 = 0; k2 <= 2*n2; ++k2)
- if ((y = contrast2_aux(p1, sum, n1, n2, k1, k2, ret)) >= 0) z += y;
+ if ((y = contrast2_aux(p1, sum, k1, k2, ret)) >= 0) z += y;
+ 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...
+ z = 1.0, ret[0] = ret[1] = ret[2] = 1./3;
}
return (double)z;
}
return sum / ma->M;
}
-int bcf_p1_cal(const bcf1_t *b, bcf_p1aux_t *ma, bcf_p1rst_t *rst)
+int bcf_p1_cal(const bcf1_t *b, int do_contrast, bcf_p1aux_t *ma, bcf_p1rst_t *rst)
{
int i, k;
long double sum = 0.;
break;
}
}
+ if (i == b->n_gi) return -1; // no PL
if (b->n_alleles < 2) return -1; // FIXME: find a better solution
//
rst->rank0 = cal_pdg(b, ma);
for (k = 0, sum = 0.; k < ma->M; ++k)
sum += ma->afs1[k];
rst->p_var = (double)sum;
+ { // compute the allele count
+ double max = -1;
+ rst->ac = -1;
+ for (k = 0; k <= ma->M; ++k)
+ if (max < ma->z[k]) max = ma->z[k], rst->ac = k;
+ rst->ac = ma->M - rst->ac;
+ }
// calculate f_flat and f_em
for (k = 0, sum = 0.; k <= ma->M; ++k)
sum += (long double)ma->z[k];
rst->f_flat += k * p;
}
rst->f_flat /= ma->M;
- { // calculate f_em
- double flast = rst->f_flat;
- for (i = 0; i < MC_MAX_EM_ITER; ++i) {
- rst->f_em = mc_freq_iter(flast, ma);
- if (fabs(rst->f_em - flast) < MC_EM_EPS) break;
- flast = rst->f_em;
- }
- }
{ // estimate equal-tail credible interval (95% level)
int l, h;
double p;
- for (i = 0, p = 0.; i < ma->M; ++i)
+ for (i = 0, p = 0.; i <= ma->M; ++i)
if (p + ma->afs1[i] > 0.025) break;
else p += ma->afs1[i];
l = i;
- for (i = ma->M-1, p = 0.; i >= 0; --i)
+ for (i = ma->M, p = 0.; i >= 0; --i)
if (p + ma->afs1[i] > 0.025) break;
else p += ma->afs1[i];
h = i;
rst->cil = (double)(ma->M - h) / ma->M; rst->cih = (double)(ma->M - l) / ma->M;
}
- rst->g[0] = rst->g[1] = rst->g[2] = -1.;
+ if (ma->n1 > 0) { // compute LRT
+ double max0, max1, max2;
+ for (k = 0, max0 = -1; k <= ma->M; ++k)
+ if (max0 < ma->z[k]) max0 = ma->z[k];
+ for (k = 0, max1 = -1; k <= ma->n1 * 2; ++k)
+ if (max1 < ma->z1[k]) max1 = ma->z1[k];
+ for (k = 0, max2 = -1; k <= ma->M - ma->n1 * 2; ++k)
+ if (max2 < ma->z2[k]) max2 = ma->z2[k];
+ rst->lrt = log(max1 * max2 / max0);
+ rst->lrt = rst->lrt < 0? 1 : kf_gammaq(.5, rst->lrt);
+ } else rst->lrt = -1.0;
rst->cmp[0] = rst->cmp[1] = rst->cmp[2] = rst->p_chi2 = -1.0;
- if (rst->p_var > 0.1) // skip contrast2() if the locus is a strong non-variant
+ if (do_contrast && rst->p_var > 0.5) // skip contrast2() if the locus is a strong non-variant
rst->p_chi2 = contrast2(ma, rst->cmp);
return 0;
}