]> git.donarmstrong.com Git - samtools.git/blobdiff - bam_mcns.c
* samtools-0.1.8-3 (r627)
[samtools.git] / bam_mcns.c
index cd64b0ac139814706632a05d2e7b75d619a36b8a..dee2016507100888faf053163ec47a8b8b474828 100644 (file)
@@ -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)