From: Heng Li Date: Tue, 31 Aug 2010 16:28:39 +0000 (+0000) Subject: z' and z'' can be calculated X-Git-Url: https://git.donarmstrong.com/?p=samtools.git;a=commitdiff_plain;h=3f13738c659d0b49fe7ac99145cdc9fa2f507af4 z' and z'' can be calculated --- diff --git a/bcftools/call1.c b/bcftools/call1.c index 75483e9..c722b35 100644 --- a/bcftools/call1.c +++ b/bcftools/call1.c @@ -20,9 +20,10 @@ KSTREAM_INIT(gzFile, gzread, 16384) #define VC_VARONLY 16 #define VC_VCFIN 32 #define VC_UNCOMP 64 +#define VC_HWE 128 typedef struct { - int flag, prior_type; + int flag, prior_type, n1; char *fn_list, *prior_file; double theta, pref; } viewconf_t; @@ -140,14 +141,14 @@ static void rm_info(int n_smpl, bcf1_t *b, const char *key) bcf_sync(n_smpl, b); } -static int update_bcf1(int n_smpl, bcf1_t *b, const bcf_p1aux_t *pa, const bcf_p1rst_t *pr, double pref, int flag) +static int update_bcf1(int n_smpl, bcf1_t *b, const bcf_p1aux_t *pa, const bcf_p1rst_t *pr, double pref) { kstring_t s; int is_var = (pr->p_ref < pref); double p_hwe, r = is_var? pr->p_ref : 1. - pr->p_ref; anno16_t a; - p_hwe = test_hwe(pr->g); + p_hwe = pr->g[0] >= 0.? test_hwe(pr->g) : 1.0; // only do HWE g[] is calculated test16(b, &a); rm_info(n_smpl, b, "I16="); @@ -162,7 +163,8 @@ static int update_bcf1(int n_smpl, bcf1_t *b, const bcf_p1aux_t *pa, const bcf_p ksprintf(&s, "AF1=%.3lf;AFE=%.3lf", 1.-pr->f_em, 1.-pr->f_exp); ksprintf(&s, ";DP4=%d,%d,%d,%d;MQ=%d", a.d[0], a.d[1], a.d[2], a.d[3], a.mq); if (a.is_tested) ksprintf(&s, ";PV4=%.2lg,%.2lg,%.2lg,%.2lg", a.p[0], a.p[1], a.p[2], a.p[3]); - if (p_hwe <= .2) ksprintf(&s, ";GC=%.2lf,%.2lf,%.2lf;HWE=%.3lf", pr->g[2], pr->g[1], pr->g[0], p_hwe); + if (pr->g[0] >= 0. && p_hwe <= .2) + ksprintf(&s, ";GC=%.2lf,%.2lf,%.2lf;HWE=%.3lf", pr->g[2], pr->g[1], pr->g[0], p_hwe); kputc('\0', &s); kputs(b->fmt, &s); kputc('\0', &s); free(b->str); @@ -188,9 +190,10 @@ int bcfview(int argc, char *argv[]) tid = begin = end = -1; memset(&vc, 0, sizeof(viewconf_t)); - vc.prior_type = -1; vc.theta = 1e-3; vc.pref = 0.9; - while ((c = getopt(argc, argv, "l:cGvLbSuP:t:p:")) >= 0) { + vc.prior_type = vc.n1 = -1; vc.theta = 1e-3; vc.pref = 0.9; + while ((c = getopt(argc, argv, "1:l:cHGvLbSuP:t:p:")) >= 0) { switch (c) { + case '1': vc.n1 = atoi(optarg); break; case 'l': vc.fn_list = strdup(optarg); break; case 'G': vc.flag |= VC_NO_GENO; break; case 'L': vc.flag |= VC_NO_PL; break; @@ -199,6 +202,7 @@ int bcfview(int argc, char *argv[]) case 'c': vc.flag |= VC_CALL; break; case 'v': vc.flag |= VC_VARONLY; break; case 'u': vc.flag |= VC_UNCOMP | VC_BCFOUT; break; + case 'H': vc.flag |= VC_HWE; break; case 't': vc.theta = atof(optarg); break; case 'p': vc.pref = atof(optarg); break; case 'P': @@ -218,6 +222,7 @@ int bcfview(int argc, char *argv[]) fprintf(stderr, " -S input is VCF\n"); fprintf(stderr, " -G suppress all individual genotype information\n"); fprintf(stderr, " -L discard the PL genotype field\n"); + fprintf(stderr, " -H perform Hardy-Weinberg test (slower)\n"); fprintf(stderr, " -v output potential variant sites only\n"); fprintf(stderr, " -l FILE list of sites to output [all sites]\n"); fprintf(stderr, " -t FLOAT scaled mutation rate [%.4lg]\n", vc.theta); @@ -245,6 +250,7 @@ int bcfview(int argc, char *argv[]) return 1; } } else bcf_p1_init_prior(p1, vc.prior_type, vc.theta); + if (vc.n1 > 0) bcf_p1_set_n1(p1, vc.n1); } if (vc.fn_list) hash = bcf_load_pos(vc.fn_list, h); if (optind + 1 < argc && !(vc.flag&VC_VCFIN)) { @@ -274,10 +280,11 @@ int bcfview(int argc, char *argv[]) } if (vc.flag & VC_CALL) { bcf_p1rst_t pr; - bcf_p1_cal(b, p1, &pr); + bcf_p1_cal(b, p1, &pr); // pr.g[3] is not calculated here + if (vc.flag&VC_HWE) bcf_p1_cal_g3(p1, pr.g); if ((n_processed + 1) % 50000 == 0) bcf_p1_dump_afs(p1); if (pr.p_ref >= vc.pref && (vc.flag & VC_VARONLY)) continue; - update_bcf1(h->n_smpl, b, p1, &pr, vc.pref, vc.flag); + update_bcf1(h->n_smpl, b, p1, &pr, vc.pref); } if (vc.flag & VC_NO_GENO) { b->n_gi = 0; diff --git a/bcftools/prob1.c b/bcftools/prob1.c index e124b0f..afbbd41 100644 --- a/bcftools/prob1.c +++ b/bcftools/prob1.c @@ -36,6 +36,8 @@ 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 t, t1, t2; double *afs, *afs1; // afs: accumulative AFS; afs1: site posterior distribution const uint8_t *PL; // point to PL int PL_len; @@ -107,6 +109,8 @@ bcf_p1aux_t *bcf_p1_init(int n) // FIXME: assuming diploid 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) @@ -127,7 +131,7 @@ 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); } @@ -195,10 +199,10 @@ int bcf_p1_call_gt(const bcf_p1aux_t *ma, double f0, int k) #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; - int k, j, last_min, last_max; + int _j, last_min, last_max; z[0] = ma->z; z[1] = ma->zswap; pdg = ma->pdg; @@ -206,10 +210,11 @@ static void mc_cal_y(bcf_p1aux_t *ma) 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[0][_min] = z[1][_min] = 0.; for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.; @@ -223,16 +228,56 @@ 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; + 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 = exp(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 double mc_cal_afs(bcf_p1aux_t *ma) { int k; @@ -252,7 +297,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; @@ -312,7 +357,8 @@ 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.; +// bcf_p1_cal_g3(ma, rst->g); return 0; } diff --git a/bcftools/prob1.h b/bcftools/prob1.h index 358f551..6bb7440 100644 --- a/bcftools/prob1.h +++ b/bcftools/prob1.h @@ -26,6 +26,8 @@ extern "C" { int bcf_p1_call_gt(const bcf_p1aux_t *ma, double f0, int k); void bcf_p1_dump_afs(bcf_p1aux_t *ma); int bcf_p1_read_prior(bcf_p1aux_t *ma, const char *fn); + long double bcf_p1_cal_g3(bcf_p1aux_t *p1a, double g[3]); + int bcf_p1_set_n1(bcf_p1aux_t *b, int n1); #ifdef __cplusplus }