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);
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, int beg, int end)
-{
- double f, f3[3];
- int i;
- f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*f0;
- for (i = beg, f = 0.; i < end; ++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 /= (end - beg) * 2.;
- return f;
-}
-
-static double mc_gtfreq_iter(double g[3], const bcf_p1aux_t *ma, int beg, int end)
-{
- double err, gg[3];
- int i;
- gg[0] = gg[1] = gg[2] = 0.;
- for (i = beg; i < end; ++i) {
- double *pdg, sum, tmp[3];
- pdg = ma->pdg + i * 3;
- tmp[0] = pdg[0] * g[0]; tmp[1] = pdg[1] * g[1]; tmp[2] = pdg[2] * g[2];
- sum = (tmp[0] + tmp[1] + tmp[2]) * (end - beg);
- gg[0] += tmp[0] / sum; gg[1] += tmp[1] / sum; gg[2] += tmp[2] / sum;
- }
- err = fabs(gg[0] - g[0]) > fabs(gg[1] - g[1])? fabs(gg[0] - g[0]) : fabs(gg[1] - g[1]);
- err = err > fabs(gg[2] - g[2])? err : fabs(gg[2] - g[2]);
- g[0] = gg[0]; g[1] = gg[1]; g[2] = gg[2];
- return err;
-}
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 (ret[0] + ret[1] + ret[2] < 0.99) // It seems that this may be caused by floating point errors. I do not really understand why...
+ 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, 0, ma->n);
- if (fabs(rst->f_em - flast) < MC_EM_EPS) break;
- flast = rst->f_em;
- }
- if (ma->n1 > 0 && ma->n1 < ma->n) {
- for (k = 0; k < 2; ++k) {
- flast = rst->f_em;
- for (i = 0; i < MC_MAX_EM_ITER; ++i) {
- rst->f_em2[k] = k? mc_freq_iter(flast, ma, ma->n1, ma->n) : mc_freq_iter(flast, ma, 0, ma->n1);
- if (fabs(rst->f_em2[k] - flast) < MC_EM_EPS) break;
- flast = rst->f_em2[k];
- }
- }
- }
- }
- { // compute g[3]
- rst->g[0] = (1. - rst->f_em) * (1. - rst->f_em);
- rst->g[1] = 2. * rst->f_em * (1. - rst->f_em);
- rst->g[2] = rst->f_em * rst->f_em;
- for (i = 0; i < MC_MAX_EM_ITER; ++i)
- if (mc_gtfreq_iter(rst->g, ma, 0, ma->n) < MC_EM_EPS) break;
- }
{ // 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;
}
+ 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;
}