X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bam_mcns.c;h=dee2016507100888faf053163ec47a8b8b474828;hb=d763698cc12d7daa7361d610db3c9c9e83e7655b;hp=cd64b0ac139814706632a05d2e7b75d619a36b8a;hpb=d73a1b3b94255044787c0e4bfeba37df66e99770;p=samtools.git diff --git a/bam_mcns.c b/bam_mcns.c index cd64b0a..dee2016 100644 --- a/bam_mcns.c +++ b/bam_mcns.c @@ -12,7 +12,7 @@ struct __mc_aux_t { double theta; double *q2p, *pdg; // pdg -> P(D|g) double *alpha, *beta; - int *qsum, *bcnt, rcnt[4]; + int *qsum, *bcnt; }; void mc_init_prior(mc_aux_t *ma, double theta) @@ -65,11 +65,9 @@ static void sum_err(int *n, const bam_pileup1_t **plp, mc_aux_t *ma) int i, j; memset(ma->qsum, 0, sizeof(int) * 4 * ma->n); memset(ma->bcnt, 0, sizeof(int) * 4 * ma->n); - memset(ma->rcnt, 0, sizeof(int) * 4); for (j = 0; j < ma->n; ++j) { int tmp, *qsum = ma->qsum + j * 4; int *bcnt = ma->bcnt + j * 4; - double r = drand48(), rr; for (i = tmp = 0; i < n[j]; ++i) { const bam_pileup1_t *p = plp[j] + i; int q, b; @@ -83,13 +81,6 @@ static void sum_err(int *n, const bam_pileup1_t **plp, mc_aux_t *ma) ++bcnt[b]; ++tmp; } - if (tmp) { - for (i = 0, rr = 0.; i < 4; ++i) { - rr += (double)bcnt[i] / tmp; - if (rr >= r) break; - } - if (i < 4) ++ma->rcnt[i]; - } } } @@ -127,21 +118,21 @@ static void cal_pdg(mc_aux_t *ma) // return the reference allele frequency double mc_freq0(int ref, int *n, const bam_pileup1_t **plp, mc_aux_t *ma, int *_ref, int *alt) { - int i, acnt[4], j; - double fr = -1., f0 = -1.; + int i, cnt; + double f; sum_err(n, plp, ma); set_allele(ref, ma); cal_pdg(ma); - acnt[0] = acnt[1] = acnt[2] = acnt[3] = 0; - for (i = 0; i < ma->n; ++i) - for (j = 0; j < 4; ++j) - acnt[j] += ma->bcnt[i * 4 + j]; - if (ma->rcnt[ma->ref] + ma->rcnt[ma->alt]) // never used... - fr = (double)ma->rcnt[ref] / (ma->rcnt[ma->ref] + ma->rcnt[ma->alt]); - if (acnt[ma->ref] + acnt[ma->alt]) - f0 = (double)acnt[ref] / (acnt[ma->ref] + acnt[ma->alt]); *_ref = ma->ref; *alt = ma->alt; - return ma->n == 1 || fr < 0.? f0 : fr; + for (i = cnt = 0, f = 0.; i < ma->n; ++i) { + int *bcnt = ma->bcnt + i * 4; + int x = bcnt[ma->ref] + bcnt[ma->alt]; + if (x) { + ++cnt; + f += (double)bcnt[ma->ref] / x; + } + } + return f / cnt; } // f0 is the reference allele frequency double mc_freq_iter(double f0, mc_aux_t *ma)