X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bam_pileup.c;h=f68f400ab5cd086dc88a33e14899abe6cd4e53bb;hb=a7dfcd368cda94d5847d3f2b42d4288399d77835;hp=46906dba429032c49920f61a4149c29ec43eae11;hpb=7a36e408af60d3a48eb8e2fda313f4ee748ad0fc;p=samtools.git diff --git a/bam_pileup.c b/bam_pileup.c index 46906db..f68f400 100644 --- a/bam_pileup.c +++ b/bam_pileup.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "sam.h" typedef struct __linkbuf_t { @@ -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 @@ -77,6 +78,10 @@ static inline int resolve_cigar(bam_pileup1_t *p, uint32_t pos) int op_next = cigar&BAM_CIGAR_MASK; // next CIGAR operation if (op_next == BAM_CDEL) p->indel = -(int32_t)(cigar>>BAM_CIGAR_SHIFT); // del else if (op_next == BAM_CINS) p->indel = cigar>>BAM_CIGAR_SHIFT; // ins + if (op_next == BAM_CDEL || op_next == BAM_CINS) { + if (k + 2 < c->n_cigar) op_next = bam1_cigar(b)[k+2]&BAM_CIGAR_MASK; + else p->is_tail = 1; + } if (op_next == BAM_CSOFT_CLIP || op_next == BAM_CREF_SKIP || op_next == BAM_CHARD_CLIP) p->is_tail = 1; // tail } else p->is_tail = 1; // this is the last operation; set tail @@ -97,7 +102,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; } @@ -165,9 +170,13 @@ int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf) 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)); - if (!(b->core.tid >= buf->max_tid || (b->core.tid == buf->max_tid && buf->tail->beg >= buf->max_pos))) { - fprintf(stderr, "[bam_pileup_core] the input is not sorted. Abort!\n"); - abort(); + if (b->core.tid < buf->max_tid) { + fprintf(stderr, "[bam_pileup_core] the input is not sorted (chromosomes out of order)\n"); + return -1; + } + if ((b->core.tid == buf->max_tid) && (buf->tail->beg < buf->max_pos)) { + fprintf(stderr, "[bam_pileup_core] the input is not sorted (reads out of order)\n"); + return -1; } buf->max_tid = b->core.tid; buf->max_pos = buf->tail->beg; if (buf->tail->end > buf->pos || buf->tail->b.core.tid > buf->tid) { @@ -196,7 +205,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 @@ -206,3 +220,19 @@ int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf) } return 0; } + +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 = 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); + bam_destroy1(b); + return 0; +}