7 typedef struct __linkbuf_t {
10 struct __linkbuf_t *next;
13 /* --- BEGIN: Memory pool */
20 static mempool_t *mp_init()
23 mp = (mempool_t*)calloc(1, sizeof(mempool_t));
26 static void mp_destroy(mempool_t *mp)
29 for (k = 0; k < mp->n; ++k) {
30 free(mp->buf[k]->b.data);
36 static inline lbnode_t *mp_alloc(mempool_t *mp)
39 if (mp->n == 0) return (lbnode_t*)calloc(1, sizeof(lbnode_t));
40 else return mp->buf[--mp->n];
42 static inline void mp_free(mempool_t *mp, lbnode_t *p)
44 --mp->cnt; p->next = 0; // clear lbnode_t::next here
45 if (mp->n == mp->max) {
46 mp->max = mp->max? mp->max<<1 : 256;
47 mp->buf = (lbnode_t**)realloc(mp->buf, sizeof(lbnode_t*) * mp->max);
52 /* --- END: Memory pool */
54 /* --- BEGIN: Auxiliary functions */
56 static inline int resolve_cigar(bam_pileup1_t *p, uint32_t pos)
60 bam1_core_t *c = &b->core;
61 uint32_t x = c->pos, y = 0;
62 int ret = 1, is_restart = 1;
64 if (c->flag&BAM_FUNMAP) return 0; // unmapped read
65 assert(x <= pos); // otherwise a bug
66 p->qpos = -1; p->indel = 0; p->is_del = p->is_head = p->is_tail = 0;
67 for (k = 0; k < c->n_cigar; ++k) {
68 int op = bam1_cigar(b)[k] & BAM_CIGAR_MASK; // operation
69 int l = bam1_cigar(b)[k] >> BAM_CIGAR_SHIFT; // length
70 if (op == BAM_CMATCH) { // NOTE: this assumes the first and the last operation MUST BE a match or a clip
71 if (x + l > pos) { // overlap with pos
72 p->indel = p->is_del = 0;
73 p->qpos = y + (pos - x);
74 if (x == pos && is_restart) p->is_head = 1;
75 if (x + l - 1 == pos) { // come to the end of a match
76 if (k < c->n_cigar - 1) { // there are additional operation(s)
77 uint32_t cigar = bam1_cigar(b)[k+1]; // next CIGAR
78 int op_next = cigar&BAM_CIGAR_MASK; // next CIGAR operation
79 if (op_next == BAM_CDEL) p->indel = -(int32_t)(cigar>>BAM_CIGAR_SHIFT); // del
80 else if (op_next == BAM_CINS) p->indel = cigar>>BAM_CIGAR_SHIFT; // ins
81 if (op_next == BAM_CDEL || op_next == BAM_CINS) {
82 if (k + 2 < c->n_cigar) op_next = bam1_cigar(b)[k+2]&BAM_CIGAR_MASK;
85 if (op_next == BAM_CSOFT_CLIP || op_next == BAM_CREF_SKIP || op_next == BAM_CHARD_CLIP)
86 p->is_tail = 1; // tail
87 } else p->is_tail = 1; // this is the last operation; set tail
91 } else if (op == BAM_CDEL) { // then set ->is_del
93 p->indel = 0; p->is_del = 1;
94 p->qpos = y + (pos - x);
97 } else if (op == BAM_CREF_SKIP) x += l;
98 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
99 is_restart = (op == BAM_CREF_SKIP || op == BAM_CSOFT_CLIP || op == BAM_CHARD_CLIP);
101 if (op == BAM_CREF_SKIP) ret = 0; // then do not put it into pileup at all
105 assert(x > pos); // otherwise a bug
109 /* --- END: Auxiliary functions */
117 lbnode_t *head, *tail, *dummy;
118 int32_t tid, pos, max_tid, max_pos;
119 int is_eof, flag_mask, max_plp, error;
121 // for the "auto" interface only
127 bam_plp_t bam_plp_init(bam_plp_auto_f func, void *data)
130 iter = calloc(1, sizeof(struct __bam_plp_t));
131 iter->mp = mp_init();
132 iter->head = iter->tail = mp_alloc(iter->mp);
133 iter->dummy = mp_alloc(iter->mp);
134 iter->max_tid = iter->max_pos = -1;
135 iter->flag_mask = BAM_DEF_MASK;
139 iter->b = bam_init1();
144 void bam_plp_destroy(bam_plp_t iter)
146 mp_free(iter->mp, iter->dummy);
147 mp_free(iter->mp, iter->head);
148 if (iter->mp->cnt != 0)
149 fprintf(stderr, "[bam_plp_destroy] memory leak: %d. Continue anyway.\n", iter->mp->cnt);
150 mp_destroy(iter->mp);
151 if (iter->b) bam_destroy1(iter->b);
156 const bam_pileup1_t *bam_plp_next(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp)
158 if (iter->error) { *_n_plp = -1; return 0; }
160 if (iter->is_eof && iter->head->next == 0) return 0;
161 while (iter->is_eof || iter->max_tid > iter->tid || (iter->max_tid == iter->tid && iter->max_pos > iter->pos)) {
164 // write iter->plp at iter->pos
165 iter->dummy->next = iter->head;
166 for (p = iter->head, q = iter->dummy; p->next; q = p, p = p->next) {
167 if (p->b.core.tid < iter->tid || (p->b.core.tid == iter->tid && p->end <= iter->pos)) { // then remove
168 q->next = p->next; mp_free(iter->mp, p); p = q;
169 } else if (p->b.core.tid == iter->tid && p->beg <= iter->pos) { // here: p->end > pos; then add to pileup
170 if (n_plp == iter->max_plp) { // then double the capacity
171 iter->max_plp = iter->max_plp? iter->max_plp<<1 : 256;
172 iter->plp = (bam_pileup1_t*)realloc(iter->plp, sizeof(bam_pileup1_t) * iter->max_plp);
174 iter->plp[n_plp].b = &p->b;
175 if (resolve_cigar(iter->plp + n_plp, iter->pos)) ++n_plp; // skip the read if we are looking at ref-skip
178 iter->head = iter->dummy->next; // dummy->next may be changed
179 *_n_plp = n_plp; *_tid = iter->tid; *_pos = iter->pos;
180 // update iter->tid and iter->pos
181 if (iter->head->next) {
182 if (iter->tid > iter->head->b.core.tid) {
183 fprintf(stderr, "[%s] unsorted input. Pileup aborts.\n", __func__);
189 if (iter->tid < iter->head->b.core.tid) { // come to a new reference sequence
190 iter->tid = iter->head->b.core.tid; iter->pos = iter->head->beg; // jump to the next reference
191 } else if (iter->pos < iter->head->beg) { // here: tid == head->b.core.tid
192 iter->pos = iter->head->beg; // jump to the next position
193 } else ++iter->pos; // scan contiguously
195 if (n_plp) return iter->plp;
196 if (iter->is_eof && iter->head->next == 0) break;
201 int bam_plp_push(bam_plp_t iter, const bam1_t *b)
203 if (iter->error) return -1;
205 if (b->core.tid < 0) return 0;
206 if (b->core.flag & iter->flag_mask) return 0;
207 bam_copy1(&iter->tail->b, b);
208 iter->tail->beg = b->core.pos; iter->tail->end = bam_calend(&b->core, bam1_cigar(b));
209 if (b->core.tid < iter->max_tid) {
210 fprintf(stderr, "[bam_pileup_core] the input is not sorted (chromosomes out of order)\n");
214 if ((b->core.tid == iter->max_tid) && (iter->tail->beg < iter->max_pos)) {
215 fprintf(stderr, "[bam_pileup_core] the input is not sorted (reads out of order)\n");
219 iter->max_tid = b->core.tid; iter->max_pos = iter->tail->beg;
220 if (iter->tail->end > iter->pos || iter->tail->b.core.tid > iter->tid) {
221 iter->tail->next = mp_alloc(iter->mp);
222 iter->tail = iter->tail->next;
224 } else iter->is_eof = 1;
228 const bam_pileup1_t *bam_plp_auto(bam_plp_t iter, int *_tid, int *_pos, int *_n_plp)
230 const bam_pileup1_t *plp;
231 if (iter->func == 0 || iter->error) { *_n_plp = -1; return 0; }
232 if ((plp = bam_plp_next(iter, _n_plp, _tid, _pos)) != 0) return plp;
235 while (iter->func(iter->b, iter->data) >= 0) {
236 if (bam_plp_push(iter, iter->b) < 0) {
240 if ((plp = bam_plp_next(iter, _n_plp, _tid, _pos)) != 0) return plp;
246 void bam_plp_reset(bam_plp_t iter)
249 iter->max_tid = iter->max_pos = -1;
250 iter->tid = iter->pos = 0;
252 for (p = iter->head; p->next;) {
254 mp_free(iter->mp, p);
257 iter->head = iter->tail;
260 void bam_plp_set_mask(bam_plp_t iter, int mask)
262 iter->flag_mask = mask < 0? BAM_DEF_MASK : (BAM_FUNMAP | mask);
269 int bam_pileup_file(bamFile fp, int mask, bam_pileup_f func, void *func_data)
275 buf = bam_plbuf_init(func, func_data);
276 bam_plbuf_set_mask(buf, mask);
277 while ((ret = bam_read1(fp, b)) >= 0)
278 bam_plbuf_push(b, buf);
279 bam_plbuf_push(0, buf);
280 bam_plbuf_destroy(buf);
285 void bam_plbuf_set_mask(bam_plbuf_t *buf, int mask)
287 bam_plp_set_mask(buf->iter, mask);
290 void bam_plbuf_reset(bam_plbuf_t *buf)
292 bam_plp_reset(buf->iter);
295 bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data)
298 buf = calloc(1, sizeof(bam_plbuf_t));
299 buf->iter = bam_plp_init(0, 0);
305 void bam_plbuf_destroy(bam_plbuf_t *buf)
307 bam_plp_destroy(buf->iter);
311 int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf)
313 int ret, n_plp, tid, pos;
314 const bam_pileup1_t *plp;
315 ret = bam_plp_push(buf->iter, b);
316 if (ret < 0) return ret;
317 while ((plp = bam_plp_next(buf->iter, &tid, &pos, &n_plp)) != 0)
318 buf->func(tid, pos, n_plp, plp, buf->data);
326 struct __bam_mplp_t {
331 const bam_pileup1_t **plp;
334 bam_mplp_t bam_mplp_init(int n, bam_plp_auto_f func, void **data)
338 iter = calloc(1, sizeof(struct __bam_mplp_t));
339 iter->pos = calloc(n, 8);
340 iter->n_plp = calloc(n, sizeof(int));
341 iter->plp = calloc(n, sizeof(void*));
343 iter->min = (uint64_t)-1;
344 for (i = 0; i < n; ++i)
345 iter->iter[i] = bam_plp_init(func, data[i]);
349 void bam_mplp_destroy(bam_mplp_t iter)
352 for (i = 0; i < iter->n; ++i) bam_plp_destroy(iter->iter[i]);
353 free(iter->pos); free(iter->n_plp); free(iter->plp);
357 int bam_mplp_auto(bam_mplp_t iter, int *_tid, int *_pos, int *n_plp, const bam_pileup1_t **plp)
360 uint64_t new_min = (uint64_t)-1;
361 for (i = 0; i < iter->n; ++i) {
362 if (iter->pos[i] == iter->min) {
364 iter->plp[i] = bam_plp_auto(iter->iter[i], &tid, &pos, &iter->n_plp[i]);
365 iter->pos[i] = (uint64_t)tid<<32 | pos;
367 if (iter->pos[i] < new_min) new_min = iter->pos[i];
370 if (new_min == (uint64_t)-1) return 0;
371 *_tid = new_min>>32; *_pos = (uint32_t)new_min;
372 for (i = 0; i < iter->n; ++i) {
373 if (iter->pos[i] == iter->min) {
374 n_plp[i] = iter->n_plp[i], plp[i] = iter->plp[i];
376 } else n_plp[i] = 0, plp[i] = 0;