]> git.donarmstrong.com Git - samtools.git/blobdiff - bam_pileup.c
* samtools-0.1.5-5 (r395)
[samtools.git] / bam_pileup.c
index 6a578319c24b739ea7bcfbaae7305b0f75607f52..d2d2aa6a8f1890b22344d330d0ca7e612c93b244 100644 (file)
@@ -1,7 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>
-#include "bam.h"
+#include <assert.h>
+#include "sam.h"
 
 typedef struct __linkbuf_t {
        bam1_t b;
@@ -61,7 +62,7 @@ static inline int resolve_cigar(bam_pileup1_t *p, uint32_t pos)
        int ret = 1, is_restart = 1;
 
        if (c->flag&BAM_FUNMAP) return 0; // unmapped read
-       assert(x <= pos);
+       assert(x <= pos); // otherwise a bug
        p->qpos = -1; p->indel = 0; p->is_del = p->is_head = p->is_tail = 0;
        for (k = 0; k < c->n_cigar; ++k) {
                int op = bam1_cigar(b)[k] & BAM_CIGAR_MASK; // operation
@@ -97,7 +98,7 @@ static inline int resolve_cigar(bam_pileup1_t *p, uint32_t pos)
                        break;
                }
        }
-       assert(x > pos);
+       assert(x > pos); // otherwise a bug
        return ret;
 }
 
@@ -161,6 +162,7 @@ void bam_plbuf_destroy(bam_plbuf_t *buf)
 int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf)
 {
        if (b) { // fill buffer
+               if (b->core.tid < 0) return 0;
                if (b->core.flag & buf->flag_mask) return 0;
                bam_copy1(&buf->tail->b, b);
                buf->tail->beg = b->core.pos; buf->tail->end = bam_calend(&b->core, bam1_cigar(b));
@@ -169,7 +171,7 @@ int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf)
                        abort();
                }
                buf->max_tid = b->core.tid; buf->max_pos = buf->tail->beg;
-               if (buf->tail->end > buf->pos) {
+               if (buf->tail->end > buf->pos || buf->tail->b.core.tid > buf->tid) {
                        buf->tail->next = mp_alloc(buf->mp);
                        buf->tail = buf->tail->next;
                }
@@ -195,7 +197,12 @@ int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf)
                        buf->func(buf->tid, buf->pos, n_pu, buf->pu, buf->func_data);
                }
                // update tid and pos
-               if (buf->head->next) assert(buf->tid <= buf->head->b.core.tid); // otherwise, not sorted
+               if (buf->head->next) {
+                       if (buf->tid > buf->head->b.core.tid) {
+                               fprintf(stderr, "[bam_plbuf_push] unsorted input. Pileup aborts.\n");
+                               return 1;
+                       }
+               }
                if (buf->tid < buf->head->b.core.tid) { // come to a new reference sequence
                        buf->tid = buf->head->b.core.tid; buf->pos = buf->head->beg; // jump to the next reference
                } else if (buf->pos < buf->head->beg) { // here: tid == head->b.core.tid
@@ -211,13 +218,13 @@ int bam_pileup_file(bamFile fp, int mask, bam_pileup_f func, void *func_data)
        bam_plbuf_t *buf;
        int ret;
        bam1_t *b;
-       b = (bam1_t*)calloc(1, sizeof(bam1_t));
+       b = bam_init1();
        buf = bam_plbuf_init(func, func_data);
        bam_plbuf_set_mask(buf, mask);
        while ((ret = bam_read1(fp, b)) >= 0)
                bam_plbuf_push(b, buf);
        bam_plbuf_push(0, buf);
        bam_plbuf_destroy(buf);
-       free(b->data); free(b);
+       bam_destroy1(b);
        return 0;
 }