]> git.donarmstrong.com Git - samtools.git/blobdiff - bcftools/prob1.c
* changed 3.434 to 4.343 (typo!)
[samtools.git] / bcftools / prob1.c
index 1acd49b48295d4d315f52c2f3969bd6c3b66e36f..37dc8ed32f5af45edc9af6fb8a2d2e306a275e97 100644 (file)
@@ -2,12 +2,18 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#include <errno.h>
 #include "prob1.h"
 
+#include "kseq.h"
+KSTREAM_INIT(gzFile, gzread, 16384)
+
 #define MC_AVG_ERR 0.007
 #define MC_MAX_EM_ITER 16
 #define MC_EM_EPS 1e-4
 
+//#define _BCF_QUAD
+
 unsigned char seq_nt4_table[256] = {
        4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
        4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
@@ -28,11 +34,14 @@ unsigned char seq_nt4_table[256] = {
 };
 
 struct __bcf_p1aux_t {
-       int n, M;
+       int n, M, n1;
        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 t, t1, t2;
        double *afs, *afs1; // afs: accumulative AFS; afs1: site posterior distribution
+       double *k1k2;
        const uint8_t *PL; // point to PL
        int PL_len;
 };
@@ -54,17 +63,59 @@ void bcf_p1_init_prior(bcf_p1aux_t *ma, int type, double theta)
        }
 }
 
-bcf_p1aux_t *bcf_p1_init(int n) // FIXME: assuming diploid
+int bcf_p1_read_prior(bcf_p1aux_t *ma, const char *fn)
+{
+       gzFile fp;
+       kstring_t s;
+       kstream_t *ks;
+       long double sum;
+       int dret, k;
+       memset(&s, 0, sizeof(kstring_t));
+       fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
+       ks = ks_init(fp);
+       memset(ma->phi, 0, sizeof(double) * (ma->M + 1));
+       while (ks_getuntil(ks, '\n', &s, &dret) >= 0) {
+               if (strstr(s.s, "[afs] ") == s.s) {
+                       char *p = s.s + 6;
+                       for (k = 0; k <= ma->M; ++k) {
+                               int x;
+                               double y;
+                               x = strtol(p, &p, 10);
+                               if (x != k && (errno == EINVAL || errno == ERANGE)) return -1;
+                               ++p;
+                               y = strtod(p, &p);
+                               if (y == 0. && (errno == EINVAL || errno == ERANGE)) return -1;
+                               ma->phi[ma->M - k] += y;
+                       }
+               }
+       }
+       ks_destroy(ks);
+       gzclose(fp);
+       free(s.s);
+       for (sum = 0., k = 0; k <= ma->M; ++k) sum += ma->phi[k];
+       fprintf(stderr, "[prior]");
+       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 += k * ma->phi[ma->M - k];
+       fprintf(stderr, "[heterozygosity] %lf\n", (double)sum / ma->M);
+       return 0;
+}
+
+bcf_p1aux_t *bcf_p1_init(int n)
 {
        bcf_p1aux_t *ma;
        int i;
        ma = calloc(1, sizeof(bcf_p1aux_t));
+       ma->n1 = -1;
        ma->n = n; ma->M = 2 * n;
        ma->q2p = calloc(256, sizeof(double));
        ma->pdg = calloc(3 * ma->n, sizeof(double));
        ma->phi = 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
+       ma->z2 = calloc(ma->M + 1, sizeof(double));
        ma->afs = calloc(2 * ma->n + 1, sizeof(double));
        ma->afs1 = calloc(2 * ma->n + 1, sizeof(double));
        for (i = 0; i < 256; ++i)
@@ -73,13 +124,37 @@ bcf_p1aux_t *bcf_p1_init(int n) // FIXME: assuming diploid
        return ma;
 }
 
+#ifdef _BCF_QUAD
+static double lbinom(int n, int k)
+{
+       return lgamma(n+1) - lgamma(k+1) - lgamma(n-k+1);
+}
+#endif
+
+int bcf_p1_set_n1(bcf_p1aux_t *b, int n1)
+{
+       if (n1 == 0 || n1 >= b->n) return -1;
+       b->n1 = n1;
+#ifdef _BCF_QUAD
+       {
+               int k1, k2, n2 = b->n - b->n1;
+               b->k1k2 = calloc((2*n1+1) * (2*n2+1), sizeof(double));
+               for (k1 = 0; k1 <= 2*n1; ++k1)
+                       for (k2 = 0; k2 <= 2*n2; ++k2)
+                               b->k1k2[k1*(2*n2+1)+k2] = exp(lbinom(2*n1,k1) + lbinom(2*n2,k2) - lbinom(b->M,k1+k2));
+       }
+#endif
+       return 0;
+}
+
 void bcf_p1_destroy(bcf_p1aux_t *ma)
 {
        if (ma) {
                free(ma->q2p); free(ma->pdg);
                free(ma->phi);
-               free(ma->z); free(ma->zswap);
+               free(ma->z); free(ma->zswap); free(ma->z1); free(ma->z2);
                free(ma->afs); free(ma->afs1);
+               free(ma->k1k2);
                free(ma);
        }
 }
@@ -139,29 +214,32 @@ 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;
 }
 
 #define TINY 1e-20
 
-static void mc_cal_y(bcf_p1aux_t *ma)
+static void mc_cal_y_core(bcf_p1aux_t *ma, int beg)
 {
-       double *z[2], *tmp, *pdg, last_min, last_max;
-       int k, j;
+       double *z[2], *tmp, *pdg;
+       int _j, last_min, last_max;
        z[0] = ma->z;
        z[1] = ma->zswap;
        pdg = ma->pdg;
-       z[0][0] = 1.; z[0][1] = z[0][2] = 0.;
+       memset(z[0], 0, sizeof(double) * (ma->M + 1));
+       memset(z[1], 0, sizeof(double) * (ma->M + 1));
+       z[0][0] = 1.;
        last_min = last_max = 0;
-       for (j = 0; j < ma->n; ++j) {
-               int _min = last_min, _max = last_max;
+       ma->t = 0.;
+       for (_j = beg; _j < ma->n; ++_j) {
+               int k, j = _j - beg, _min = last_min, _max = last_max;
                double p[3], sum;
-               pdg = ma->pdg + j * 3;
+               pdg = ma->pdg + _j * 3;
                p[0] = pdg[0]; p[1] = 2. * pdg[1]; p[2] = pdg[2];
-//             for (; _min < _max && z[0][_min] < TINY; ++_min) z[1][_min] = 0.;
-//             for (; _max > _min && z[0][_max] < TINY; --_max) z[1][_max] = 0.;
+               for (; _min < _max && z[0][_min] < TINY; ++_min) z[0][_min] = z[1][_min] = 0.;
+               for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.;
                _max += 2;
                if (_min == 0) 
                        k = 0, z[1][k] = (2*j+2-k)*(2*j-k+1) * p[0] * z[0][k];
@@ -172,14 +250,110 @@ static void mc_cal_y(bcf_p1aux_t *ma)
                                + k*(2*j+2-k) * p[1] * z[0][k-1]
                                + k*(k-1)* p[2] * z[0][k-2];
                for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
+               ma->t += log(sum / ((2. * j + 2) * (2. * j + 1)));
                for (k = _min; k <= _max; ++k) z[1][k] /= sum;
+               if (_min >= 1) z[1][_min-1] = 0.;
+               if (_min >= 2) z[1][_min-2] = 0.;
                if (j < ma->n - 1) z[1][_max+1] = z[1][_max+2] = 0.;
+               if (_j == ma->n1 - 1) { // set pop1
+                       ma->t1 = ma->t;
+                       memcpy(ma->z1, z[1], sizeof(double) * (ma->n1 * 2 + 1));
+               }
                tmp = z[0]; z[0] = z[1]; z[1] = tmp;
                last_min = _min; last_max = _max;
        }
        if (z[0] != ma->z) memcpy(ma->z, z[0], sizeof(double) * (ma->M + 1));
 }
 
+static void mc_cal_y(bcf_p1aux_t *ma)
+{
+       if (ma->n1 > 0 && ma->n1 < ma->n) {
+               int k;
+               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.;
+               mc_cal_y_core(ma, ma->n1);
+               ma->t2 = ma->t;
+               memcpy(ma->z2, ma->z, sizeof(double) * (2 * (ma->n - ma->n1) + 1));
+               mc_cal_y_core(ma, 0);
+               // rescale z
+               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);
+#ifdef _BCF_QUAD
+/*
+       if (ma->n1 > 0 && ma->n1 < ma->n) { // DEBUG: consistency check; z[i] should equal y[i]
+               int i, k1, k2, n1 = ma->n1, n2 = ma->n - n1;
+               double *y;
+               printf("*** ");
+               y = calloc(ma->M + 1, sizeof(double));
+               for (k1 = 0; k1 <= 2*n1; ++k1)
+                       for (k2 = 0; k2 <= 2*n2; ++k2)
+                               y[k1+k2] += ma->k1k2[k1*(2*n2+1)+k2] * ma->z1[k1] * ma->z2[k2];
+               for (i = 0; i <= ma->M; ++i) printf("(%lf,%lf) ", ma->z[i], y[i]);
+               printf("\n");
+               free(y);
+       }
+*/
+#endif
+}
+
+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 sum = -1., x, sum_alt;
+       double y;
+       pc[0] = pc[1] = pc[2] = pc[3] = -1.;
+       if (n1 <= 0 || n2 <= 0) return;
+#ifdef _BCF_QUAD
+       { // FIXME: can be improved by skipping zero cells
+               int k1, k2;
+               long double z[3];
+               z[0] = z[1] = z[2] = 0.;
+               for (k1 = 0; k1 <= 2*n1; ++k1)
+                       for (k2 = 0; k2 <= 2*n2; ++k2) {
+                               double zz = ma->phi[k1+k2] * ma->z1[k1] * ma->z2[k2] * ma->k1k2[k1*(2*n2+1)+k2];
+                               if ((double)k1/n1 < (double)k2/n2) z[0] += zz;
+                               else if ((double)k1/n1 > (double)k2/n2) z[1] += zz;
+                               else z[2] += zz;
+                       }
+               sum = z[0] + z[1] + z[2];
+               pc[2] = z[0] / sum; pc[3] = z[1] / sum;
+       }
+#else
+       pc[2] = pc[3] = 0.;
+#endif
+       for (k = 0, sum_alt = 0.; k <= ma->M; ++k)
+               sum_alt += (long double)ma->phi[k] * ma->z[k];
+//     printf("* %lg, %lg *\n", (double)sum, (double)sum_alt); // DEBUG: sum should equal sum_alt
+       sum = sum_alt;
+       // the variant is specific to group2
+//     printf("%lg %lg %lg %lg\n", ma->z[2*(n1+n2)]/exp(ma->t - (ma->t1 + ma->t2)), ma->z1[2*n1], ma->z2[2*n2], (double)sum);
+       y = lgamma(2*n2 + 1) - lgamma(ma->M + 1);
+       for (k = 0, x = 0.; k < 2 * n2; ++k)
+               x += ma->phi[2*n1+k] * ma->z2[k] * expl(lgamma(2*n1 + k + 1) - lgamma(k + 1) + y);
+       pc[1] = ma->z1[2*n1] * x / sum;
+       for (k = 1, x = 0.; k <= 2 * n2; ++k)
+               x += ma->phi[k] * ma->z2[k] * expl(lgamma(ma->M - k + 1) - lgamma(2*n2 - k + 1) + y);
+       pc[1] += ma->z1[0] * x / sum;
+       // the variant is specific to group1
+       y = lgamma(2*n1 + 1) - lgamma(ma->M + 1);
+       for (k = 0, x = 0.; k < 2 * n1; ++k)
+               x += ma->phi[2*n2+k] * ma->z1[k] * expl(lgamma(2*n2 + k + 1) - lgamma(k + 1) + y);
+       pc[0] = ma->z2[2*n2] * x / sum;
+       for (k = 1, x = 0.; k <= 2 * n1; ++k)
+               x += ma->phi[k] * ma->z1[k] * expl(lgamma(ma->M - k + 1) - lgamma(2*n1 - k + 1) + y);
+       pc[0] += ma->z2[0] * x / sum;
+       // rescale
+       for (k = 2; k < 4; ++k) {
+               y = 1. - pc[k];
+               if (y <= 0.) y = 1e-100;
+               pc[k] = (int)(-4.343 * log(y) + .499);
+               if (pc[k] > 99.) pc[k] = 99.;
+       }
+}
+
 static double mc_cal_afs(bcf_p1aux_t *ma)
 {
        int k;
@@ -199,7 +373,7 @@ static double mc_cal_afs(bcf_p1aux_t *ma)
        return sum / ma->M;
 }
 
-static long double p1_cal_g3(bcf_p1aux_t *p1a, double g[3])
+long double bcf_p1_cal_g3(bcf_p1aux_t *p1a, double g[3])
 {
        long double pd = 0., g2[3];
        int i, k;
@@ -259,7 +433,9 @@ int bcf_p1_cal(bcf1_t *b, bcf_p1aux_t *ma, bcf_p1rst_t *rst)
                        flast = rst->f_em;
                }
        }
-       p1_cal_g3(ma, rst->g);
+       rst->g[0] = rst->g[1] = rst->g[2] = -1.;
+       contrast(ma, rst->pc);
+//     bcf_p1_cal_g3(ma, rst->g);
        return 0;
 }