16 #define bam_open(fn, mode) gzopen(fn, mode)
17 #define bam_dopen(fd, mode) gzdopen(fd, mode)
18 #define bam_close(fp) gzclose(fp)
19 #define bam_read(fp, buf, size) gzread(fp, buf, size)
23 char *bam_nt16_rev_table = "=ACMGRSVTWYHKDBN";
25 /*************************
26 *** from bam_endian.h ***
27 *************************/
29 static inline int bam_is_big_endian()
32 return !(*((char *)(&one)));
34 static inline uint16_t bam_swap_endian_2(uint16_t v)
36 return (uint16_t)(((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8));
38 static inline void *bam_swap_endian_2p(void *x)
40 *(uint16_t*)x = bam_swap_endian_2(*(uint16_t*)x);
43 static inline uint32_t bam_swap_endian_4(uint32_t v)
45 v = ((v & 0x0000FFFFU) << 16) | (v >> 16);
46 return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8);
48 static inline void *bam_swap_endian_4p(void *x)
50 *(uint32_t*)x = bam_swap_endian_4(*(uint32_t*)x);
53 static inline uint64_t bam_swap_endian_8(uint64_t v)
55 v = ((v & 0x00000000FFFFFFFFLLU) << 32) | (v >> 32);
56 v = ((v & 0x0000FFFF0000FFFFLLU) << 16) | ((v & 0xFFFF0000FFFF0000LLU) >> 16);
57 return ((v & 0x00FF00FF00FF00FFLLU) << 8) | ((v & 0xFF00FF00FF00FF00LLU) >> 8);
59 static inline void *bam_swap_endian_8p(void *x)
61 *(uint64_t*)x = bam_swap_endian_8(*(uint64_t*)x);
65 /**********************
66 *** from kstring.* ***
67 **********************/
69 typedef struct __kstring_t {
75 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
78 static inline int kputsn(const char *p, int l, kstring_t *s)
80 if (s->l + l + 1 >= s->m) {
83 s->s = (char*)realloc(s->s, s->m);
85 strncpy(s->s + s->l, p, l);
91 static inline int kputs(const char *p, kstring_t *s)
93 return kputsn(p, strlen(p), s);
96 static inline int kputc(int c, kstring_t *s)
98 if (s->l + 1 >= s->m) {
101 s->s = (char*)realloc(s->s, s->m);
108 int ksprintf(kstring_t *s, const char *fmt, ...)
113 l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap); // This line does not work with glibc 2.0. See `man snprintf'.
115 if (l + 1 > s->m - s->l) {
118 s->s = (char*)realloc(s->s, s->m);
120 l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap);
133 /**************************
134 * CIGAR related routines *
135 **************************/
137 int bam_segreg(int32_t pos, const bam1_core_t *c, const uint32_t *cigar, bam_segreg_t *reg)
140 int32_t x = c->pos, y = 0;
142 for (k = 0; k < c->n_cigar; ++k) {
143 int op = cigar[k] & BAM_CIGAR_MASK; // operation
144 int l = cigar[k] >> BAM_CIGAR_SHIFT; // length
145 if (state == 0 && (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CINS) && x + l > pos) {
146 reg->tbeg = x; reg->qbeg = y; reg->cbeg = k;
149 if (op == BAM_CMATCH) { x += l; y += l; }
150 else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
151 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
152 if (state == 1 && (op == BAM_CSOFT_CLIP || op == BAM_CHARD_CLIP || op == BAM_CREF_SKIP || k == c->n_cigar - 1)) {
153 reg->tend = x; reg->qend = y; reg->cend = k;
156 return state? 0 : -1;
159 uint32_t bam_calend(const bam1_core_t *c, const uint32_t *cigar)
163 for (k = 0; k < c->n_cigar; ++k) {
164 int op = cigar[k] & BAM_CIGAR_MASK;
165 if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP)
166 end += cigar[k] >> BAM_CIGAR_SHIFT;
171 int32_t bam_cigar2qlen(const bam1_core_t *c, const uint32_t *cigar)
175 for (k = 0; k < c->n_cigar; ++k) {
176 int op = cigar[k] & BAM_CIGAR_MASK;
177 if (op == BAM_CMATCH || op == BAM_CINS || op == BAM_CSOFT_CLIP)
178 l += cigar[k] >> BAM_CIGAR_SHIFT;
183 /********************
185 ********************/
187 bam_header_t *bam_header_init()
189 bam_is_be = bam_is_big_endian();
190 return (bam_header_t*)calloc(1, sizeof(bam_header_t));
193 void bam_header_destroy(bam_header_t *header)
196 extern void bam_destroy_header_hash(bam_header_t *header);
197 if (header == 0) return;
198 if (header->target_name) {
199 for (i = 0; i < header->n_targets; ++i)
200 free(header->target_name[i]);
201 free(header->target_name);
202 free(header->target_len);
206 if (header->rg2lib) bam_strmap_destroy(header->rg2lib);
207 bam_destroy_header_hash(header);
212 bam_header_t *bam_header_read(bamFile fp)
214 bam_header_t *header;
218 if (bam_read(fp, buf, 4) != 4) return 0;
219 if (strncmp(buf, "BAM\001", 4)) {
220 fprintf(stderr, "[bam_header_read] wrong header\n");
223 header = bam_header_init();
224 // read plain text and the number of reference sequences
225 bam_read(fp, &header->l_text, 4);
226 if (bam_is_be) bam_swap_endian_4p(&header->l_text);
227 header->text = (char*)calloc(header->l_text + 1, 1);
228 bam_read(fp, header->text, header->l_text);
229 bam_read(fp, &header->n_targets, 4);
230 if (bam_is_be) bam_swap_endian_4p(&header->n_targets);
231 // read reference sequence names and lengths
232 header->target_name = (char**)calloc(header->n_targets, sizeof(char*));
233 header->target_len = (uint32_t*)calloc(header->n_targets, 4);
234 for (i = 0; i != header->n_targets; ++i) {
235 bam_read(fp, &name_len, 4);
236 if (bam_is_be) bam_swap_endian_4p(&name_len);
237 header->target_name[i] = (char*)calloc(name_len, 1);
238 bam_read(fp, header->target_name[i], name_len);
239 bam_read(fp, &header->target_len[i], 4);
240 if (bam_is_be) bam_swap_endian_4p(&header->target_len[i]);
245 static void swap_endian_data(const bam1_core_t *c, int data_len, uint8_t *data)
248 uint32_t i, *cigar = (uint32_t*)(data + c->l_qname);
249 s = data + c->n_cigar*4 + c->l_qname + c->l_qseq + (c->l_qseq + 1)/2;
250 for (i = 0; i < c->n_cigar; ++i) bam_swap_endian_4p(&cigar[i]);
251 while (s < data + data_len) {
254 type = toupper(*s); ++s; // skip type
255 if (type == 'C' || type == 'A') ++s;
256 else if (type == 'S') { bam_swap_endian_2p(s); s += 2; }
257 else if (type == 'I' || type == 'F') { bam_swap_endian_4p(s); s += 4; }
258 else if (type == 'D') { bam_swap_endian_8p(s); s += 8; }
259 else if (type == 'Z' || type == 'H') { while (*s) ++s; ++s; }
263 int bam_read1(bamFile fp, bam1_t *b)
265 bam1_core_t *c = &b->core;
266 int32_t block_len, ret, i;
269 assert(BAM_CORE_SIZE == 32);
270 if ((ret = bam_read(fp, &block_len, 4)) != 4) {
271 if (ret == 0) return -1; // normal end-of-file
272 else return -2; // truncated
274 if (bam_read(fp, x, BAM_CORE_SIZE) != BAM_CORE_SIZE) return -3;
276 bam_swap_endian_4p(&block_len);
277 for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i);
279 c->tid = x[0]; c->pos = x[1];
280 c->bin = x[2]>>16; c->qual = x[2]>>8&0xff; c->l_qname = x[2]&0xff;
281 c->flag = x[3]>>16; c->n_cigar = x[3]&0xffff;
283 c->mtid = x[5]; c->mpos = x[6]; c->isize = x[7];
284 b->data_len = block_len - BAM_CORE_SIZE;
285 if (b->m_data < b->data_len) {
286 b->m_data = b->data_len;
287 kroundup32(b->m_data);
288 b->data = (uint8_t*)realloc(b->data, b->m_data);
290 if (bam_read(fp, b->data, b->data_len) != b->data_len) return -4;
291 b->l_aux = b->data_len - c->n_cigar * 4 - c->l_qname - c->l_qseq - (c->l_qseq+1)/2;
292 if (bam_is_be) swap_endian_data(c, b->data_len, b->data);
293 return 4 + block_len;
296 char *bam_format1(const bam_header_t *header, const bam1_t *b)
298 uint8_t *s = bam1_seq(b), *t = bam1_qual(b);
300 const bam1_core_t *c = &b->core;
302 str.l = str.m = 0; str.s = 0;
304 ksprintf(&str, "%s\t%d\t", bam1_qname(b), c->flag);
305 if (c->tid < 0) kputs("*\t", &str);
306 else ksprintf(&str, "%s\t", header->target_name[c->tid]);
307 ksprintf(&str, "%d\t%d\t", c->pos + 1, c->qual);
308 if (c->n_cigar == 0) kputc('*', &str);
310 for (i = 0; i < c->n_cigar; ++i)
311 ksprintf(&str, "%d%c", bam1_cigar(b)[i]>>BAM_CIGAR_SHIFT, "MIDNSHP"[bam1_cigar(b)[i]&BAM_CIGAR_MASK]);
314 if (c->mtid < 0) kputs("*\t", &str);
315 else if (c->mtid == c->tid) kputs("=\t", &str);
316 else ksprintf(&str, "%s\t", header->target_name[c->mtid]);
317 ksprintf(&str, "%d\t%d\t", c->mpos + 1, c->isize);
318 for (i = 0; i < c->l_qseq; ++i) kputc(bam_nt16_rev_table[bam1_seqi(s, i)], &str);
320 if (t[0] == 0xff) kputc('*', &str);
321 else for (i = 0; i < c->l_qseq; ++i) kputc(t[i] + 33, &str);
323 while (s < b->data + b->data_len) {
324 uint8_t type, key[2];
325 key[0] = s[0]; key[1] = s[1];
326 s += 2; type = *s; ++s;
327 ksprintf(&str, "\t%c%c:", key[0], key[1]);
328 if (type == 'A') { ksprintf(&str, "A:%c", *s); ++s; }
329 else if (type == 'C') { ksprintf(&str, "i:%u", *s); ++s; }
330 else if (type == 'c') { ksprintf(&str, "i:%d", *s); ++s; }
331 else if (type == 'S') { ksprintf(&str, "i:%u", *(uint16_t*)s); s += 2; }
332 else if (type == 's') { ksprintf(&str, "i:%d", *(int16_t*)s); s += 2; }
333 else if (type == 'I') { ksprintf(&str, "i:%u", *(uint32_t*)s); s += 4; }
334 else if (type == 'i') { ksprintf(&str, "i:%d", *(int32_t*)s); s += 4; }
335 else if (type == 'f') { ksprintf(&str, "f:%g", *(float*)s); s += 4; }
336 else if (type == 'd') { ksprintf(&str, "d:%lg", *(double*)s); s += 8; }
337 else if (type == 'Z' || type == 'H') { ksprintf(&str, "%c:", type); while (*s) kputc(*s++, &str); ++s; }
342 /*************************
343 *** from bam_pileup.c ***
344 *************************/
346 typedef struct __linkbuf_t {
349 struct __linkbuf_t *next;
352 /* --- BEGIN: Memory pool */
359 static mempool_t *mp_init()
362 mp = (mempool_t*)calloc(1, sizeof(mempool_t));
365 static void mp_destroy(mempool_t *mp)
368 for (k = 0; k < mp->n; ++k) {
369 free(mp->buf[k]->b.data);
375 static inline lbnode_t *mp_alloc(mempool_t *mp)
378 if (mp->n == 0) return (lbnode_t*)calloc(1, sizeof(lbnode_t));
379 else return mp->buf[--mp->n];
381 static inline void mp_free(mempool_t *mp, lbnode_t *p)
383 --mp->cnt; p->next = 0; // clear lbnode_t::next here
384 if (mp->n == mp->max) {
385 mp->max = mp->max? mp->max<<1 : 256;
386 mp->buf = (lbnode_t**)realloc(mp->buf, sizeof(lbnode_t*) * mp->max);
388 mp->buf[mp->n++] = p;
391 /* --- END: Memory pool */
393 /* --- BEGIN: Auxiliary functions */
395 static inline int resolve_cigar(bam_pileup1_t *p, uint32_t pos)
399 bam1_core_t *c = &b->core;
400 uint32_t x = c->pos, y = 0;
401 int ret = 1, is_restart = 1;
403 if (c->flag&BAM_FUNMAP) return 0; // unmapped read
404 assert(x <= pos); // otherwise a bug
405 p->qpos = -1; p->indel = 0; p->is_del = p->is_head = p->is_tail = 0;
406 for (k = 0; k < c->n_cigar; ++k) {
407 int op = bam1_cigar(b)[k] & BAM_CIGAR_MASK; // operation
408 int l = bam1_cigar(b)[k] >> BAM_CIGAR_SHIFT; // length
409 if (op == BAM_CMATCH) { // NOTE: this assumes the first and the last operation MUST BE a match or a clip
410 if (x + l > pos) { // overlap with pos
411 p->indel = p->is_del = 0;
412 p->qpos = y + (pos - x);
413 if (x == pos && is_restart) p->is_head = 1;
414 if (x + l - 1 == pos) { // come to the end of a match
415 if (k < c->n_cigar - 1) { // there are additional operation(s)
416 uint32_t cigar = bam1_cigar(b)[k+1]; // next CIGAR
417 int op_next = cigar&BAM_CIGAR_MASK; // next CIGAR operation
418 if (op_next == BAM_CDEL) p->indel = -(int32_t)(cigar>>BAM_CIGAR_SHIFT); // del
419 else if (op_next == BAM_CINS) p->indel = cigar>>BAM_CIGAR_SHIFT; // ins
420 if (op_next == BAM_CSOFT_CLIP || op_next == BAM_CREF_SKIP || op_next == BAM_CHARD_CLIP)
421 p->is_tail = 1; // tail
422 } else p->is_tail = 1; // this is the last operation; set tail
426 } else if (op == BAM_CDEL) { // then set ->is_del
428 p->indel = 0; p->is_del = 1;
429 p->qpos = y + (pos - x);
432 } else if (op == BAM_CREF_SKIP) x += l;
433 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
434 is_restart = (op == BAM_CREF_SKIP || op == BAM_CSOFT_CLIP || op == BAM_CHARD_CLIP);
436 if (op == BAM_CREF_SKIP) ret = 0; // then do not put it into pileup at all
440 assert(x > pos); // otherwise a bug
444 /* --- END: Auxiliary functions */
446 struct __bam_plbuf_t {
448 lbnode_t *head, *tail, *dummy;
451 int32_t tid, pos, max_tid, max_pos;
457 void bam_plbuf_reset(bam_plbuf_t *buf)
460 buf->max_tid = buf->max_pos = -1;
461 buf->tid = buf->pos = 0;
463 for (p = buf->head; p->next;) {
468 buf->head = buf->tail;
471 void bam_plbuf_set_mask(bam_plbuf_t *buf, int mask)
473 if (mask < 0) buf->flag_mask = BAM_DEF_MASK;
474 else buf->flag_mask = BAM_FUNMAP | mask;
477 bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data)
480 buf = (bam_plbuf_t*)calloc(1, sizeof(bam_plbuf_t));
481 buf->func = func; buf->func_data = data;
483 buf->head = buf->tail = mp_alloc(buf->mp);
484 buf->dummy = mp_alloc(buf->mp);
485 buf->max_tid = buf->max_pos = -1;
486 buf->flag_mask = BAM_DEF_MASK;
490 void bam_plbuf_destroy(bam_plbuf_t *buf)
492 mp_free(buf->mp, buf->dummy);
493 mp_free(buf->mp, buf->head);
494 if (buf->mp->cnt != 0)
495 fprintf(stderr, "[bam_plbuf_destroy] memory leak: %d. Continue anyway.\n", buf->mp->cnt);
501 int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf)
503 if (b) { // fill buffer
504 if (b->core.tid < 0) return 0;
505 if (b->core.flag & buf->flag_mask) return 0;
506 bam_copy1(&buf->tail->b, b);
507 buf->tail->beg = b->core.pos; buf->tail->end = bam_calend(&b->core, bam1_cigar(b));
508 if (!(b->core.tid >= buf->max_tid || (b->core.tid == buf->max_tid && buf->tail->beg >= buf->max_pos))) {
509 fprintf(stderr, "[bam_pileup_core] the input is not sorted. Abort!\n");
512 buf->max_tid = b->core.tid; buf->max_pos = buf->tail->beg;
513 if (buf->tail->end > buf->pos || buf->tail->b.core.tid > buf->tid) {
514 buf->tail->next = mp_alloc(buf->mp);
515 buf->tail = buf->tail->next;
517 } else buf->is_eof = 1;
518 while (buf->is_eof || buf->max_tid > buf->tid || (buf->max_tid == buf->tid && buf->max_pos > buf->pos)) {
521 buf->dummy->next = buf->head;
522 for (p = buf->head, q = buf->dummy; p->next; q = p, p = p->next) {
523 if (p->b.core.tid < buf->tid || (p->b.core.tid == buf->tid && p->end <= buf->pos)) { // then remove from the list
524 q->next = p->next; mp_free(buf->mp, p); p = q;
525 } else if (p->b.core.tid == buf->tid && p->beg <= buf->pos) { // here: p->end > pos; then add to pileup
526 if (n_pu == buf->max_pu) { // then double the capacity
527 buf->max_pu = buf->max_pu? buf->max_pu<<1 : 256;
528 buf->pu = (bam_pileup1_t*)realloc(buf->pu, sizeof(bam_pileup1_t) * buf->max_pu);
530 buf->pu[n_pu].b = &p->b;
531 if (resolve_cigar(buf->pu + n_pu, buf->pos)) ++n_pu; // skip the read if we are looking at BAM_CREF_SKIP
534 buf->head = buf->dummy->next; // dummy->next may be changed
535 if (n_pu) { // then call user defined function
536 buf->func(buf->tid, buf->pos, n_pu, buf->pu, buf->func_data);
538 // update tid and pos
539 if (buf->head->next) {
540 if (buf->tid > buf->head->b.core.tid) {
541 fprintf(stderr, "[bam_plbuf_push] unsorted input. Pileup aborts.\n");
545 if (buf->tid < buf->head->b.core.tid) { // come to a new reference sequence
546 buf->tid = buf->head->b.core.tid; buf->pos = buf->head->beg; // jump to the next reference
547 } else if (buf->pos < buf->head->beg) { // here: tid == head->b.core.tid
548 buf->pos = buf->head->beg; // jump to the next position
549 } else ++buf->pos; // scan contiguously
550 if (buf->is_eof && buf->head->next == 0) break;
555 int bam_pileup_file(bamFile fp, int mask, bam_pileup_f func, void *func_data)
561 buf = bam_plbuf_init(func, func_data);
562 bam_plbuf_set_mask(buf, mask);
563 while ((ret = bam_read1(fp, b)) >= 0)
564 bam_plbuf_push(b, buf);
565 bam_plbuf_push(0, buf);
566 bam_plbuf_destroy(buf);
571 /**********************
572 *** from bam_aux.c ***
573 **********************/
575 uint8_t *bam_aux_get(const bam1_t *b, const char tag[2])
578 int y = tag[0]<<8 | tag[1];
580 while (s < b->data + b->data_len) {
581 int type, x = (int)s[0]<<8 | s[1];
583 if (x == y) return s;
584 type = toupper(*s); ++s;
585 if (type == 'C') ++s;
586 else if (type == 'S') s += 2;
587 else if (type == 'I' || type == 'F') s += 4;
588 else if (type == 'D') s += 8;
589 else if (type == 'Z' || type == 'H') { while (*s) ++s; ++s; }