]> git.donarmstrong.com Git - samtools.git/blobdiff - bam2bcf.c
faster for large sample size (in principle)
[samtools.git] / bam2bcf.c
index e64e3357c04d5cd61bc7ecdc3e6af6d0fd859182..1f7a0c042b2dad49abe5bb9d1668d90ddc96edf5 100644 (file)
--- a/bam2bcf.c
+++ b/bam2bcf.c
@@ -5,6 +5,8 @@
 #include "bam2bcf.h"
 #include "bcftools/bcf.h"
 
+#define END_DIST_THRES 11
+
 extern void ks_introsort_uint32_t(size_t n, uint32_t a[]);
 
 #define CALL_ETA 0.03f
@@ -71,11 +73,12 @@ typedef struct {
  */
 int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base /*4-bit*/, bcf_callaux_t *bca, bcf_callret1_t *r)
 {
-       int i, j, k, c, n;
+       int i, j, k, c, n, ref4;
        float *p = r->p;
        auxaux_t aux;
 
        memset(r, 0, sizeof(bcf_callret1_t));
+       ref4 = bam_nt16_nt4_table[ref_base];
        if (_n == 0) return -1;
 
        // enlarge the aux array if necessary
@@ -88,6 +91,7 @@ int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base /*4-bit*/, bcf
        for (i = n = 0; i < _n; ++i) {
                const bam_pileup1_t *p = pl + i;
                uint32_t q, x = 0, qq;
+               int min_dist;
                if (p->is_del || (p->b->core.flag&BAM_FUNMAP)) continue; // skip unmapped reads and deleted bases
                q = (uint32_t)bam1_qual(p->b)[p->qpos]; // base quality
                if (q < bca->min_baseQ) continue;
@@ -97,8 +101,15 @@ int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base /*4-bit*/, bcf
                qq = bam1_seqi(bam1_seq(p->b), p->qpos); // base
                q = bam_nt16_nt4_table[qq? qq : ref_base]; // q is the 2-bit base
                if (q < 4) x |= 1 << 21 | q << 16;
-               
+               k = (ref4 < 4 && q == ref4)? 0 : 1;
+               k = k<<1 | bam1_strand(p->b);
+               ++r->d[k];
                bca->info[n++] = x;
+               // calculate min_dist
+               min_dist = p->b->core.l_qseq - 1 - p->qpos;
+               if (min_dist > p->qpos) min_dist = p->qpos;
+               k = (k&2) | (min_dist <= END_DIST_THRES);
+               ++r->ed[k];
        }
        ks_introsort_uint32_t(n, bca->info);
        r->depth = n;
@@ -204,8 +215,11 @@ int bcf_call_combine(int n, const bcf_callret1_t *calls, int ref_base /*4-bit*/,
                }
                call->shift = (int)(sum_min + .499);
        }
+       memset(call->d, 0, 4 * sizeof(int));
+       memset(call->ed, 0, 4 * sizeof(int));
        for (i = call->depth = 0, tmp = 0; i < n; ++i) {
                call->depth += calls[i].depth;
+               for (j = 0; j < 4; ++j) call->d[j] += calls[i].d[j], call->ed[j] += calls[i].ed[j];
                tmp += calls[i].sum_Q2;
        }
        call->rmsQ = (int)(sqrt((double)tmp / call->depth) + .499);
@@ -228,7 +242,20 @@ int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b)
        }
        kputc('\0', &s);
        kputc('\0', &s);
-       kputs("MQ=", &s); kputw(bc->rmsQ, &s); kputs(";DP=", &s); kputw(bc->depth, &s); kputc('\0', &s);
+       // INFO
+       kputs("MQ=", &s); kputw(bc->rmsQ, &s); // kputs(";DP=", &s); kputw(bc->depth, &s);
+       kputs(";DP4=", &s);
+       for (i = 0; i < 4; ++i) {
+               if (i) kputc(',', &s);
+               kputw(bc->d[i], &s);
+       }
+       kputs(";ED4=", &s);
+       for (i = 0; i < 4; ++i) {
+               if (i) kputc(',', &s);
+               kputw(bc->ed[i], &s);
+       }
+       kputc('\0', &s);
+       // FMT
        kputs("PL", &s); kputc('\0', &s);
        b->m_str = s.m; b->str = s.s; b->l_str = s.l;
        bcf_sync(bc->n, b);