- if (!cont)
- {
- 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));
- 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();
- }
- 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;
- }
- else
- // continue end of loop
- {
- // 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;
- }
- }
- 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) return 0;
- }
-
- // enter yield loop
- 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
- }
- }
- buf->head = buf->dummy->next; // dummy->next may be changed
-
- // exit if alignments need to be emitted
- if (n_pu) { return n_pu; }
-
- // 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 -2;
- }
- }
- 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;
- }
- return 0;