X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bcftools%2Fprob1.c;h=dc4c16ccc7c24edafd39ec0de193eb618435beb8;hb=cac5f70d5bc208e7699f5b8e72ebd120d41a28ea;hp=afbbd419cd0242aeefd1e31776e45fecdf36a981;hpb=3f13738c659d0b49fe7ac99145cdc9fa2f507af4;p=samtools.git diff --git a/bcftools/prob1.c b/bcftools/prob1.c index afbbd41..dc4c16c 100644 --- a/bcftools/prob1.c +++ b/bcftools/prob1.c @@ -36,30 +36,42 @@ struct __bcf_p1aux_t { double *q2p, *pdg; // pdg -> P(D|g) double *phi; double *z, *zswap; // aux for afs - double *z1, *z2; // only calculated when n1 is set + double *z1, *z2, *phi1, *phi2; // only calculated when n1 is set double t, t1, t2; double *afs, *afs1; // afs: accumulative AFS; afs1: site posterior distribution const uint8_t *PL; // point to PL int PL_len; }; -void bcf_p1_init_prior(bcf_p1aux_t *ma, int type, double theta) +static void init_prior(int type, double theta, int M, double *phi) { int i; if (type == MC_PTYPE_COND2) { - for (i = 0; i <= ma->M; ++i) - ma->phi[i] = 2. * (i + 1) / (ma->M + 1) / (ma->M + 2); + for (i = 0; i <= M; ++i) + phi[i] = 2. * (i + 1) / (M + 1) / (M + 2); } else if (type == MC_PTYPE_FLAT) { - for (i = 0; i <= ma->M; ++i) - ma->phi[i] = 1. / (ma->M + 1); + for (i = 0; i <= M; ++i) + phi[i] = 1. / (M + 1); } else { double sum; - for (i = 0, sum = 0.; i < ma->M; ++i) - sum += (ma->phi[i] = theta / (ma->M - i)); - ma->phi[ma->M] = 1. - sum; + for (i = 0, sum = 0.; i < M; ++i) + sum += (phi[i] = theta / (M - i)); + phi[M] = 1. - sum; } } +void bcf_p1_init_prior(bcf_p1aux_t *ma, int type, double theta) +{ + init_prior(type, theta, ma->M, ma->phi); +} + +void bcf_p1_init_subprior(bcf_p1aux_t *ma, int type, double theta) +{ + if (ma->n1 <= 0 || ma->n1 >= ma->M) return; + init_prior(type, theta, 2*ma->n1, ma->phi1); + init_prior(type, theta, 2*(ma->n - ma->n1), ma->phi2); +} + int bcf_p1_read_prior(bcf_p1aux_t *ma, const char *fn) { gzFile fp; @@ -94,10 +106,14 @@ int bcf_p1_read_prior(bcf_p1aux_t *ma, const char *fn) for (k = 0; k <= ma->M; ++k) ma->phi[k] /= sum; for (k = 0; k <= ma->M; ++k) fprintf(stderr, " %d:%.3lg", k, ma->phi[ma->M - k]); fputc('\n', stderr); + 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)); + fprintf(stderr, "[%s] heterozygosity=%lf, ", __func__, (double)sum); + for (sum = 0., k = 1; k <= ma->M; ++k) sum += k * ma->phi[ma->M - k] / ma->M; + fprintf(stderr, "theta=%lf\n", (double)sum); return 0; } -bcf_p1aux_t *bcf_p1_init(int n) // FIXME: assuming diploid +bcf_p1aux_t *bcf_p1_init(int n) { bcf_p1aux_t *ma; int i; @@ -107,6 +123,8 @@ bcf_p1aux_t *bcf_p1_init(int n) // FIXME: assuming diploid ma->q2p = calloc(256, sizeof(double)); ma->pdg = calloc(3 * ma->n, sizeof(double)); ma->phi = calloc(ma->M + 1, sizeof(double)); + ma->phi1 = calloc(ma->M + 1, sizeof(double)); + ma->phi2 = calloc(ma->M + 1, sizeof(double)); ma->z = calloc(2 * ma->n + 1, sizeof(double)); ma->zswap = calloc(2 * ma->n + 1, sizeof(double)); ma->z1 = calloc(ma->M + 1, sizeof(double)); // actually we do not need this large @@ -130,7 +148,7 @@ void bcf_p1_destroy(bcf_p1aux_t *ma) { if (ma) { free(ma->q2p); free(ma->pdg); - free(ma->phi); + free(ma->phi); free(ma->phi1); free(ma->phi2); free(ma->z); free(ma->zswap); free(ma->z1); free(ma->z2); free(ma->afs); free(ma->afs1); free(ma); @@ -192,7 +210,7 @@ int bcf_p1_call_gt(const bcf_p1aux_t *ma, double f0, int k) } max = 1. - max; if (max < 1e-308) max = 1e-308; - q = (int)(-3.434 * log(max) + .499); + q = (int)(-4.343 * log(max) + .499); if (q > 99) q = 99; return q<<2|max_i; } @@ -247,7 +265,7 @@ static void mc_cal_y(bcf_p1aux_t *ma) { if (ma->n1 > 0 && ma->n1 < ma->n) { int k; - double x; + long double x; memset(ma->z1, 0, sizeof(double) * (2 * ma->n1 + 1)); memset(ma->z2, 0, sizeof(double) * (2 * (ma->n - ma->n1) + 1)); ma->t1 = ma->t2 = 0.; @@ -256,26 +274,31 @@ static void mc_cal_y(bcf_p1aux_t *ma) memcpy(ma->z2, ma->z, sizeof(double) * (2 * (ma->n - ma->n1) + 1)); mc_cal_y_core(ma, 0); // rescale z - x = exp(ma->t - (ma->t1 + ma->t2)); + x = expl(ma->t - (ma->t1 + ma->t2)); for (k = 0; k <= ma->M; ++k) ma->z[k] *= x; } else mc_cal_y_core(ma, 0); -/* - if (ma->n1 > 0 && ma->n1 < ma->n) { - int i; - double y[5]; - printf("*****\n"); - for (i = 0; i <= 2; ++i) - printf("(%lf,%lf) ", ma->z1[i], ma->z2[i]); - printf("\n"); - y[0] = ma->z1[0] * ma->z2[0]; - y[1] = 1./2. * (ma->z1[0] * ma->z2[1] + ma->z1[1] * ma->z2[0]); - y[2] = 1./6. * (ma->z1[0] * ma->z2[2] + ma->z1[2] * ma->z2[0]) + 4./6. * ma->z1[1] * ma->z2[1]; - y[3] = 1./2. * (ma->z1[1] * ma->z2[2] + ma->z1[2] * ma->z2[1]); - y[4] = ma->z1[2] * ma->z2[2]; - for (i = 0; i <= 4; ++i) printf("(%lf,%lf) ", ma->z[i], y[i]); - printf("\n"); +} + +static void contrast(bcf_p1aux_t *ma, double pc[4]) // mc_cal_y() must be called before hand +{ + int k, n1 = ma->n1, n2 = ma->n - ma->n1; + long double sum1, sum2; + pc[0] = pc[1] = pc[2] = pc[3] = -1.; + if (n1 <= 0 || n2 <= 0) return; + for (k = 0, sum1 = 0.; k <= 2*n1; ++k) sum1 += ma->phi1[k] * ma->z1[k]; + for (k = 0, sum2 = 0.; k <= 2*n2; ++k) sum2 += ma->phi2[k] * ma->z2[k]; + pc[2] = ma->phi1[2*n1] * ma->z1[2*n1] / sum1; + pc[3] = ma->phi2[2*n2] * ma->z2[2*n2] / sum2; + for (k = 2; k < 4; ++k) { + pc[k] = pc[k] > .5? -(-4.343 * log(1. - pc[k] + TINY) + .499) : -4.343 * log(pc[k] + TINY) + .499; + pc[k] = (int)pc[k]; + if (pc[k] > 99) pc[k] = 99; + if (pc[k] < -99) pc[k] = -99; } -*/ + pc[0] = ma->phi2[2*n2] * ma->z2[2*n2] / sum2 * (1. - ma->phi1[2*n1] * ma->z1[2*n1] / sum1); + pc[1] = ma->phi1[2*n1] * ma->z1[2*n1] / sum1 * (1. - ma->phi2[2*n2] * ma->z2[2*n2] / sum2); + pc[0] = pc[0] == 1.? 99 : (int)(-4.343 * log(1. - pc[0]) + .499); + pc[1] = pc[1] == 1.? 99 : (int)(-4.343 * log(1. - pc[1]) + .499); } static double mc_cal_afs(bcf_p1aux_t *ma) @@ -358,7 +381,7 @@ int bcf_p1_cal(bcf1_t *b, bcf_p1aux_t *ma, bcf_p1rst_t *rst) } } rst->g[0] = rst->g[1] = rst->g[2] = -1.; -// bcf_p1_cal_g3(ma, rst->g); + contrast(ma, rst->pc); return 0; }