]> git.donarmstrong.com Git - samtools.git/blob - bam_lite.c
* samtools-0.1.5-5 (r395)
[samtools.git] / bam_lite.c
1 #include <ctype.h>
2 #include <assert.h>
3 #include <stdarg.h>
4
5 #define BAM_LITE
6 #include "bam.h"
7
8 #undef bam_open
9 #undef bam_dopen
10 #undef bam_close
11 #undef bam_read
12 #undef bam_write
13 #undef bam_tell
14 #undef bam_seek
15
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)
20
21 #define BAM_NO_HASH
22
23 char *bam_nt16_rev_table = "=ACMGRSVTWYHKDBN";
24
25 /*************************
26  *** from bam_endian.h ***
27  *************************/
28
29 static inline int bam_is_big_endian()
30 {
31         long one= 1;
32         return !(*((char *)(&one)));
33 }
34 static inline uint16_t bam_swap_endian_2(uint16_t v)
35 {
36         return (uint16_t)(((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8));
37 }
38 static inline void *bam_swap_endian_2p(void *x)
39 {
40         *(uint16_t*)x = bam_swap_endian_2(*(uint16_t*)x);
41         return x;
42 }
43 static inline uint32_t bam_swap_endian_4(uint32_t v)
44 {
45         v = ((v & 0x0000FFFFU) << 16) | (v >> 16);
46         return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8);
47 }
48 static inline void *bam_swap_endian_4p(void *x)
49 {
50         *(uint32_t*)x = bam_swap_endian_4(*(uint32_t*)x);
51         return x;
52 }
53 static inline uint64_t bam_swap_endian_8(uint64_t v)
54 {
55         v = ((v & 0x00000000FFFFFFFFLLU) << 32) | (v >> 32);
56         v = ((v & 0x0000FFFF0000FFFFLLU) << 16) | ((v & 0xFFFF0000FFFF0000LLU) >> 16);
57         return ((v & 0x00FF00FF00FF00FFLLU) << 8) | ((v & 0xFF00FF00FF00FF00LLU) >> 8);
58 }
59 static inline void *bam_swap_endian_8p(void *x)
60 {
61         *(uint64_t*)x = bam_swap_endian_8(*(uint64_t*)x);
62         return x;
63 }
64
65 /**********************
66  *** from kstring.* ***
67  **********************/
68
69 typedef struct __kstring_t {
70         size_t l, m;
71         char *s;
72 } kstring_t;
73
74 #ifndef kroundup32
75 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
76 #endif
77
78 static inline int kputsn(const char *p, int l, kstring_t *s)
79 {
80         if (s->l + l + 1 >= s->m) {
81                 s->m = s->l + l + 2;
82                 kroundup32(s->m);
83                 s->s = (char*)realloc(s->s, s->m);
84         }
85         strncpy(s->s + s->l, p, l);
86         s->l += l;
87         s->s[s->l] = 0;
88         return l;
89 }
90
91 static inline int kputs(const char *p, kstring_t *s)
92 {
93         return kputsn(p, strlen(p), s);
94 }
95
96 static inline int kputc(int c, kstring_t *s)
97 {
98         if (s->l + 1 >= s->m) {
99                 s->m = s->l + 2;
100                 kroundup32(s->m);
101                 s->s = (char*)realloc(s->s, s->m);
102         }
103         s->s[s->l++] = c;
104         s->s[s->l] = 0;
105         return c;
106 }
107
108 int ksprintf(kstring_t *s, const char *fmt, ...)
109 {
110         va_list ap;
111         int l;
112         va_start(ap, 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'.
114         va_end(ap);
115         if (l + 1 > s->m - s->l) {
116                 s->m = s->l + l + 2;
117                 kroundup32(s->m);
118                 s->s = (char*)realloc(s->s, s->m);
119                 va_start(ap, fmt);
120                 l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap);
121         }
122         va_end(ap);
123         s->l += l;
124         return l;
125 }
126
127 /******************
128  *** from bam.c ***
129  ******************/
130
131 int bam_is_be = 0;
132
133 /**************************
134  * CIGAR related routines *
135  **************************/
136
137 int bam_segreg(int32_t pos, const bam1_core_t *c, const uint32_t *cigar, bam_segreg_t *reg)
138 {
139         unsigned k;
140         int32_t x = c->pos, y = 0;
141         int state = 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;
147                         state = 1;
148                 }
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;
154                 }
155         }
156         return state? 0 : -1;
157 }
158
159 uint32_t bam_calend(const bam1_core_t *c, const uint32_t *cigar)
160 {
161         uint32_t k, end;
162         end = c->pos;
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;
167         }
168         return end;
169 }
170
171 int32_t bam_cigar2qlen(const bam1_core_t *c, const uint32_t *cigar)
172 {
173         uint32_t k;
174         int32_t l = 0;
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;
179         }
180         return l;
181 }
182
183 /********************
184  * BAM I/O routines *
185  ********************/
186
187 bam_header_t *bam_header_init()
188 {
189         bam_is_be = bam_is_big_endian();
190         return (bam_header_t*)calloc(1, sizeof(bam_header_t));
191 }
192
193 void bam_header_destroy(bam_header_t *header)
194 {
195         int32_t i;
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);
203         }
204         free(header->text);
205 #ifndef BAM_NO_HASH
206         if (header->rg2lib) bam_strmap_destroy(header->rg2lib);
207         bam_destroy_header_hash(header);
208 #endif
209         free(header);
210 }
211
212 bam_header_t *bam_header_read(bamFile fp)
213 {
214         bam_header_t *header;
215         char buf[4];
216         int32_t i, name_len;
217         // read "BAM1"
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");
221                 return 0;
222         }
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]);
241         }
242         return header;
243 }
244
245 static void swap_endian_data(const bam1_core_t *c, int data_len, uint8_t *data)
246 {
247         uint8_t *s;
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) {
252                 uint8_t type;
253                 s += 2; // skip key
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; }
260         }
261 }
262
263 int bam_read1(bamFile fp, bam1_t *b)
264 {
265         bam1_core_t *c = &b->core;
266         int32_t block_len, ret, i;
267         uint32_t x[8];
268
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
273         }
274         if (bam_read(fp, x, BAM_CORE_SIZE) != BAM_CORE_SIZE) return -3;
275         if (bam_is_be) {
276                 bam_swap_endian_4p(&block_len);
277                 for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i);
278         }
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;
282         c->l_qseq = x[4];
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);
289         }
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;
294 }
295
296 char *bam_format1(const bam_header_t *header, const bam1_t *b)
297 {
298         uint8_t *s = bam1_seq(b), *t = bam1_qual(b);
299         int i;
300         const bam1_core_t *c = &b->core;
301         kstring_t str;
302         str.l = str.m = 0; str.s = 0;
303
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);
309         else {
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]);
312         }
313         kputc('\t', &str);
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);
319         kputc('\t', &str);
320         if (t[0] == 0xff) kputc('*', &str);
321         else for (i = 0; i < c->l_qseq; ++i) kputc(t[i] + 33, &str);
322         s = bam1_aux(b);
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; }
338         }
339         return str.s;
340 }
341
342 /*************************
343  *** from bam_pileup.c ***
344  *************************/
345
346 typedef struct __linkbuf_t {
347         bam1_t b;
348         uint32_t beg, end;
349         struct __linkbuf_t *next;
350 } lbnode_t;
351
352 /* --- BEGIN: Memory pool */
353
354 typedef struct {
355         int cnt, n, max;
356         lbnode_t **buf;
357 } mempool_t;
358
359 static mempool_t *mp_init()
360 {
361         mempool_t *mp;
362         mp = (mempool_t*)calloc(1, sizeof(mempool_t));
363         return mp;
364 }
365 static void mp_destroy(mempool_t *mp)
366 {
367         int k;
368         for (k = 0; k < mp->n; ++k) {
369                 free(mp->buf[k]->b.data);
370                 free(mp->buf[k]);
371         }
372         free(mp->buf);
373         free(mp);
374 }
375 static inline lbnode_t *mp_alloc(mempool_t *mp)
376 {
377         ++mp->cnt;
378         if (mp->n == 0) return (lbnode_t*)calloc(1, sizeof(lbnode_t));
379         else return mp->buf[--mp->n];
380 }
381 static inline void mp_free(mempool_t *mp, lbnode_t *p)
382 {
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);
387         }
388         mp->buf[mp->n++] = p;
389 }
390
391 /* --- END: Memory pool */
392
393 /* --- BEGIN: Auxiliary functions */
394
395 static inline int resolve_cigar(bam_pileup1_t *p, uint32_t pos)
396 {
397         unsigned k;
398         bam1_t *b = p->b;
399         bam1_core_t *c = &b->core;
400         uint32_t x = c->pos, y = 0;
401         int ret = 1, is_restart = 1;
402
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
423                                 }
424                         }
425                         x += l; y += l;
426                 } else if (op == BAM_CDEL) { // then set ->is_del
427                         if (x + l > pos) {
428                                 p->indel = 0; p->is_del = 1;
429                                 p->qpos = y + (pos - x);
430                         }
431                         x += l;
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);
435                 if (x > pos) {
436                         if (op == BAM_CREF_SKIP) ret = 0; // then do not put it into pileup at all
437                         break;
438                 }
439         }
440         assert(x > pos); // otherwise a bug
441         return ret;
442 }
443
444 /* --- END: Auxiliary functions */
445
446 struct __bam_plbuf_t {
447         mempool_t *mp;
448         lbnode_t *head, *tail, *dummy;
449         bam_pileup_f func;
450         void *func_data;
451         int32_t tid, pos, max_tid, max_pos;
452         int max_pu, is_eof;
453         bam_pileup1_t *pu;
454         int flag_mask;
455 };
456
457 void bam_plbuf_reset(bam_plbuf_t *buf)
458 {
459         lbnode_t *p, *q;
460         buf->max_tid = buf->max_pos = -1;
461         buf->tid = buf->pos = 0;
462         buf->is_eof = 0;
463         for (p = buf->head; p->next;) {
464                 q = p->next;
465                 mp_free(buf->mp, p);
466                 p = q;
467         }
468         buf->head = buf->tail;
469 }
470
471 void bam_plbuf_set_mask(bam_plbuf_t *buf, int mask)
472 {
473         if (mask < 0) buf->flag_mask = BAM_DEF_MASK;
474         else buf->flag_mask = BAM_FUNMAP | mask;
475 }
476
477 bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data)
478 {
479         bam_plbuf_t *buf;
480         buf = (bam_plbuf_t*)calloc(1, sizeof(bam_plbuf_t));
481         buf->func = func; buf->func_data = data;
482         buf->mp = mp_init();
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;
487         return buf;
488 }
489
490 void bam_plbuf_destroy(bam_plbuf_t *buf)
491 {
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);
496         mp_destroy(buf->mp);
497         free(buf->pu);
498         free(buf);
499 }
500
501 int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf)
502 {
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");
510                         abort();
511                 }
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;
516                 }
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)) {
519                 int n_pu = 0;
520                 lbnode_t *p, *q;
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);
529                                 }
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
532                         }
533                 }
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);
537                 }
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");
542                                 return 1;
543                         }
544                 }
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;
551         }
552         return 0;
553 }
554
555 int bam_pileup_file(bamFile fp, int mask, bam_pileup_f func, void *func_data)
556 {
557         bam_plbuf_t *buf;
558         int ret;
559         bam1_t *b;
560         b = bam_init1();
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);
567         bam_destroy1(b);
568         return 0;
569 }
570
571 /**********************
572  *** from bam_aux.c ***
573  **********************/
574
575 uint8_t *bam_aux_get(const bam1_t *b, const char tag[2])
576 {
577         uint8_t *s;
578         int y = tag[0]<<8 | tag[1];
579         s = bam1_aux(b);
580         while (s < b->data + b->data_len) {
581                 int type, x = (int)s[0]<<8 | s[1];
582                 s += 2;
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; }
590         }
591         return 0;
592 }