X-Git-Url: http://woldlab.caltech.edu/gitweb/?p=samtools.git;a=blobdiff_plain;f=bam_pileup.c;h=3c41a169127fcc5d3977df3e8febd944b7ced77e;hp=f68f400ab5cd086dc88a33e14899abe6cd4e53bb;hb=cb12a866906ec4ac644de0e658679261c82ab098;hpb=3a3b72190c04cb076438f5027e13fcf5ddee77be diff --git a/bam_pileup.c b/bam_pileup.c index f68f400..3c41a16 100644 --- a/bam_pileup.c +++ b/bam_pileup.c @@ -73,18 +73,28 @@ static inline int resolve_cigar(bam_pileup1_t *p, uint32_t pos) p->qpos = y + (pos - x); if (x == pos && is_restart) p->is_head = 1; if (x + l - 1 == pos) { // come to the end of a match - if (k < c->n_cigar - 1) { // there are additional operation(s) + int has_next_match = 0; + unsigned i; + for (i = k + 1; i < c->n_cigar; ++i) { + uint32_t cigar = bam1_cigar(b)[i]; + int opi = cigar&BAM_CIGAR_MASK; + if (opi == BAM_CMATCH) { + has_next_match = 1; + break; + } else if (opi == BAM_CSOFT_CLIP || opi == BAM_CREF_SKIP || opi == BAM_CHARD_CLIP) break; + } + if (!has_next_match) p->is_tail = 1; + if (k < c->n_cigar - 1 && has_next_match) { // there are additional operation(s) uint32_t cigar = bam1_cigar(b)[k+1]; // next CIGAR 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; + else if (op_next == BAM_CPAD && k + 2 < c->n_cigar) { // no working for adjacent padding + cigar = bam1_cigar(b)[k+2]; op_next = cigar&BAM_CIGAR_MASK; + 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_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 + } } } x += l; y += l; @@ -96,7 +106,8 @@ static inline int resolve_cigar(bam_pileup1_t *p, uint32_t pos) x += l; } else if (op == BAM_CREF_SKIP) x += l; else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l; - is_restart = (op == BAM_CREF_SKIP || op == BAM_CSOFT_CLIP || op == BAM_CHARD_CLIP); + if (is_restart) is_restart ^= (op == BAM_CMATCH); + else is_restart ^= (op == BAM_CREF_SKIP || op == BAM_CSOFT_CLIP || op == BAM_CHARD_CLIP); if (x > pos) { if (op == BAM_CREF_SKIP) ret = 0; // then do not put it into pileup at all break; @@ -108,119 +119,167 @@ static inline int resolve_cigar(bam_pileup1_t *p, uint32_t pos) /* --- END: Auxiliary functions */ -struct __bam_plbuf_t { +/******************* + * pileup iterator * + *******************/ + +struct __bam_plp_t { mempool_t *mp; lbnode_t *head, *tail, *dummy; - bam_pileup_f func; - void *func_data; int32_t tid, pos, max_tid, max_pos; - int max_pu, is_eof; - bam_pileup1_t *pu; - int flag_mask; + int is_eof, flag_mask, max_plp, error; + bam_pileup1_t *plp; + // for the "auto" interface only + bam1_t *b; + bam_plp_auto_f func; + void *data; }; -void bam_plbuf_reset(bam_plbuf_t *buf) +bam_plp_t bam_plp_init(bam_plp_auto_f func, void *data) { - lbnode_t *p, *q; - buf->max_tid = buf->max_pos = -1; - buf->tid = buf->pos = 0; - buf->is_eof = 0; - for (p = buf->head; p->next;) { - q = p->next; - mp_free(buf->mp, p); - p = q; + bam_plp_t iter; + iter = calloc(1, sizeof(struct __bam_plp_t)); + iter->mp = mp_init(); + iter->head = iter->tail = mp_alloc(iter->mp); + iter->dummy = mp_alloc(iter->mp); + iter->max_tid = iter->max_pos = -1; + iter->flag_mask = BAM_DEF_MASK; + if (func) { + iter->func = func; + iter->data = data; + iter->b = bam_init1(); } - buf->head = buf->tail; + return iter; } -void bam_plbuf_set_mask(bam_plbuf_t *buf, int mask) -{ - if (mask < 0) buf->flag_mask = BAM_DEF_MASK; - else buf->flag_mask = BAM_FUNMAP | mask; -} - -bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data) +void bam_plp_destroy(bam_plp_t iter) { - bam_plbuf_t *buf; - buf = (bam_plbuf_t*)calloc(1, sizeof(bam_plbuf_t)); - buf->func = func; buf->func_data = data; - buf->mp = mp_init(); - buf->head = buf->tail = mp_alloc(buf->mp); - buf->dummy = mp_alloc(buf->mp); - buf->max_tid = buf->max_pos = -1; - buf->flag_mask = BAM_DEF_MASK; - return buf; + mp_free(iter->mp, iter->dummy); + mp_free(iter->mp, iter->head); + if (iter->mp->cnt != 0) + fprintf(stderr, "[bam_plp_destroy] memory leak: %d. Continue anyway.\n", iter->mp->cnt); + mp_destroy(iter->mp); + if (iter->b) bam_destroy1(iter->b); + free(iter->plp); + free(iter); } -void bam_plbuf_destroy(bam_plbuf_t *buf) +const bam_pileup1_t *bam_plp_next(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp) { - mp_free(buf->mp, buf->dummy); - mp_free(buf->mp, buf->head); - if (buf->mp->cnt != 0) - fprintf(stderr, "[bam_plbuf_destroy] memory leak: %d. Continue anyway.\n", buf->mp->cnt); - mp_destroy(buf->mp); - free(buf->pu); - free(buf); + if (iter->error) { *_n_plp = -1; return 0; } + *_n_plp = 0; + if (iter->is_eof && iter->head->next == 0) return 0; + while (iter->is_eof || iter->max_tid > iter->tid || (iter->max_tid == iter->tid && iter->max_pos > iter->pos)) { + int n_plp = 0; + lbnode_t *p, *q; + // write iter->plp at iter->pos + iter->dummy->next = iter->head; + for (p = iter->head, q = iter->dummy; p->next; q = p, p = p->next) { + if (p->b.core.tid < iter->tid || (p->b.core.tid == iter->tid && p->end <= iter->pos)) { // then remove + q->next = p->next; mp_free(iter->mp, p); p = q; + } else if (p->b.core.tid == iter->tid && p->beg <= iter->pos) { // here: p->end > pos; then add to pileup + if (n_plp == iter->max_plp) { // then double the capacity + iter->max_plp = iter->max_plp? iter->max_plp<<1 : 256; + iter->plp = (bam_pileup1_t*)realloc(iter->plp, sizeof(bam_pileup1_t) * iter->max_plp); + } + iter->plp[n_plp].b = &p->b; + if (resolve_cigar(iter->plp + n_plp, iter->pos)) ++n_plp; // skip the read if we are looking at ref-skip + } + } + iter->head = iter->dummy->next; // dummy->next may be changed + *_n_plp = n_plp; *_tid = iter->tid; *_pos = iter->pos; + // update iter->tid and iter->pos + if (iter->head->next) { + if (iter->tid > iter->head->b.core.tid) { + fprintf(stderr, "[%s] unsorted input. Pileup aborts.\n", __func__); + iter->error = 1; + *_n_plp = -1; + return 0; + } + } + if (iter->tid < iter->head->b.core.tid) { // come to a new reference sequence + iter->tid = iter->head->b.core.tid; iter->pos = iter->head->beg; // jump to the next reference + } else if (iter->pos < iter->head->beg) { // here: tid == head->b.core.tid + iter->pos = iter->head->beg; // jump to the next position + } else ++iter->pos; // scan contiguously + // return + if (n_plp) return iter->plp; + if (iter->is_eof && iter->head->next == 0) break; + } + return 0; } -int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf) +int bam_plp_push(bam_plp_t iter, const bam1_t *b) { - if (b) { // fill buffer + if (iter->error) return -1; + if (b) { 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)); - if (b->core.tid < buf->max_tid) { + if (b->core.flag & iter->flag_mask) return 0; + bam_copy1(&iter->tail->b, b); + iter->tail->beg = b->core.pos; iter->tail->end = bam_calend(&b->core, bam1_cigar(b)); + if (b->core.tid < iter->max_tid) { fprintf(stderr, "[bam_pileup_core] the input is not sorted (chromosomes out of order)\n"); + iter->error = 1; return -1; } - if ((b->core.tid == buf->max_tid) && (buf->tail->beg < buf->max_pos)) { + if ((b->core.tid == iter->max_tid) && (iter->tail->beg < iter->max_pos)) { fprintf(stderr, "[bam_pileup_core] the input is not sorted (reads out of order)\n"); + iter->error = 1; 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) { - buf->tail->next = mp_alloc(buf->mp); - buf->tail = buf->tail->next; - } - } else buf->is_eof = 1; - while (buf->is_eof || buf->max_tid > buf->tid || (buf->max_tid == buf->tid && buf->max_pos > buf->pos)) { - int n_pu = 0; - lbnode_t *p, *q; - buf->dummy->next = buf->head; - for (p = buf->head, q = buf->dummy; p->next; q = p, p = p->next) { - if (p->b.core.tid < buf->tid || (p->b.core.tid == buf->tid && p->end <= buf->pos)) { // then remove from the list - q->next = p->next; mp_free(buf->mp, p); p = q; - } else if (p->b.core.tid == buf->tid && p->beg <= buf->pos) { // here: p->end > pos; then add to pileup - if (n_pu == buf->max_pu) { // then double the capacity - buf->max_pu = buf->max_pu? buf->max_pu<<1 : 256; - buf->pu = (bam_pileup1_t*)realloc(buf->pu, sizeof(bam_pileup1_t) * buf->max_pu); - } - buf->pu[n_pu].b = &p->b; - if (resolve_cigar(buf->pu + n_pu, buf->pos)) ++n_pu; // skip the read if we are looking at BAM_CREF_SKIP - } + iter->max_tid = b->core.tid; iter->max_pos = iter->tail->beg; + if (iter->tail->end > iter->pos || iter->tail->b.core.tid > iter->tid) { + iter->tail->next = mp_alloc(iter->mp); + iter->tail = iter->tail->next; } - buf->head = buf->dummy->next; // dummy->next may be changed - if (n_pu) { // then call user defined function - buf->func(buf->tid, buf->pos, n_pu, buf->pu, buf->func_data); - } - // update tid and pos - if (buf->head->next) { - if (buf->tid > buf->head->b.core.tid) { - fprintf(stderr, "[bam_plbuf_push] unsorted input. Pileup aborts.\n"); - return 1; + } else iter->is_eof = 1; + return 0; +} + +const bam_pileup1_t *bam_plp_auto(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp) +{ + const bam_pileup1_t *plp; + if (iter->func == 0 || iter->error) { *_n_plp = -1; return 0; } + if ((plp = bam_plp_next(iter, _tid, _pos, _n_plp)) != 0) return plp; + else { + *_n_plp = 0; + if (iter->is_eof) return 0; + while (iter->func(iter->data, iter->b) >= 0) { + if (bam_plp_push(iter, iter->b) < 0) { + *_n_plp = -1; + return 0; } + if ((plp = bam_plp_next(iter, _tid, _pos, _n_plp)) != 0) return plp; } - 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 - buf->pos = buf->head->beg; // jump to the next position - } else ++buf->pos; // scan contiguously - if (buf->is_eof && buf->head->next == 0) break; + bam_plp_push(iter, 0); + if ((plp = bam_plp_next(iter, _tid, _pos, _n_plp)) != 0) return plp; + return 0; } - return 0; } +void bam_plp_reset(bam_plp_t iter) +{ + lbnode_t *p, *q; + iter->max_tid = iter->max_pos = -1; + iter->tid = iter->pos = 0; + iter->is_eof = 0; + for (p = iter->head; p->next;) { + q = p->next; + mp_free(iter->mp, p); + p = q; + } + iter->head = iter->tail; +} + +void bam_plp_set_mask(bam_plp_t iter, int mask) +{ + iter->flag_mask = mask < 0? BAM_DEF_MASK : (BAM_FUNMAP | mask); +} + +/***************** + * callback APIs * + *****************/ + int bam_pileup_file(bamFile fp, int mask, bam_pileup_f func, void *func_data) { bam_plbuf_t *buf; @@ -236,3 +295,102 @@ int bam_pileup_file(bamFile fp, int mask, bam_pileup_f func, void *func_data) bam_destroy1(b); return 0; } + +void bam_plbuf_set_mask(bam_plbuf_t *buf, int mask) +{ + bam_plp_set_mask(buf->iter, mask); +} + +void bam_plbuf_reset(bam_plbuf_t *buf) +{ + bam_plp_reset(buf->iter); +} + +bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data) +{ + bam_plbuf_t *buf; + buf = calloc(1, sizeof(bam_plbuf_t)); + buf->iter = bam_plp_init(0, 0); + buf->func = func; + buf->data = data; + return buf; +} + +void bam_plbuf_destroy(bam_plbuf_t *buf) +{ + bam_plp_destroy(buf->iter); + free(buf); +} + +int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf) +{ + int ret, n_plp, tid, pos; + const bam_pileup1_t *plp; + ret = bam_plp_push(buf->iter, b); + if (ret < 0) return ret; + while ((plp = bam_plp_next(buf->iter, &tid, &pos, &n_plp)) != 0) + buf->func(tid, pos, n_plp, plp, buf->data); + return 0; +} + +/*********** + * mpileup * + ***********/ + +struct __bam_mplp_t { + int n; + uint64_t min, *pos; + bam_plp_t *iter; + int *n_plp; + const bam_pileup1_t **plp; +}; + +bam_mplp_t bam_mplp_init(int n, bam_plp_auto_f func, void **data) +{ + int i; + bam_mplp_t iter; + iter = calloc(1, sizeof(struct __bam_mplp_t)); + iter->pos = calloc(n, 8); + iter->n_plp = calloc(n, sizeof(int)); + iter->plp = calloc(n, sizeof(void*)); + iter->iter = calloc(n, sizeof(void*)); + iter->n = n; + iter->min = (uint64_t)-1; + for (i = 0; i < n; ++i) { + iter->iter[i] = bam_plp_init(func, data[i]); + iter->pos[i] = iter->min; + } + return iter; +} + +void bam_mplp_destroy(bam_mplp_t iter) +{ + int i; + for (i = 0; i < iter->n; ++i) bam_plp_destroy(iter->iter[i]); + free(iter->iter); free(iter->pos); free(iter->n_plp); free(iter->plp); + free(iter); +} + +int bam_mplp_auto(bam_mplp_t iter, int *_tid, int *_pos, int *n_plp, const bam_pileup1_t **plp) +{ + int i, ret = 0; + uint64_t new_min = (uint64_t)-1; + for (i = 0; i < iter->n; ++i) { + if (iter->pos[i] == iter->min) { + int tid, pos; + iter->plp[i] = bam_plp_auto(iter->iter[i], &tid, &pos, &iter->n_plp[i]); + iter->pos[i] = (uint64_t)tid<<32 | pos; + } + if (iter->plp[i] && iter->pos[i] < new_min) new_min = iter->pos[i]; + } + iter->min = new_min; + if (new_min == (uint64_t)-1) return 0; + *_tid = new_min>>32; *_pos = (uint32_t)new_min; + for (i = 0; i < iter->n; ++i) { + if (iter->pos[i] == iter->min) { + n_plp[i] = iter->n_plp[i], plp[i] = iter->plp[i]; + ++ret; + } else n_plp[i] = 0, plp[i] = 0; + } + return ret; +}