12 // callback for bam_fetch()
13 static int fetch_func(const bam1_t *b, void *data)
15 bam_plbuf_t *buf = (bam_plbuf_t*)data;
16 bam_plbuf_push(b, buf);
19 // callback for bam_plbuf_init()
20 static int pileup_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
22 tmpstruct_t *tmp = (tmpstruct_t*)data;
23 if ((int)pos >= tmp->beg && (int)pos < tmp->end)
24 printf("%s\t%d\t%d\n", tmp->in->header->target_name[tid], pos + 1, n);
28 int main(int argc, char *argv[])
32 fprintf(stderr, "Usage: calDepth <in.bam> [region]\n");
35 tmp.beg = 0; tmp.end = 0x7fffffff;
36 tmp.in = samopen(argv[1], "rb", 0);
38 fprintf(stderr, "Fail to open BAM file %s\n", argv[1]);
42 sampileup(tmp.in, -1, pileup_func, &tmp);
47 idx = bam_index_load(argv[1]);
49 fprintf(stderr, "BAM indexing file is not available.\n");
52 bam_parse_region(tmp.in->header, argv[2], &ref, &tmp.beg, &tmp.end);
54 fprintf(stderr, "Invalid region %s\n", argv[2]);
57 buf = bam_plbuf_init(pileup_func, &tmp);
58 bam_fetch(tmp.in->x.bam, idx, ref, tmp.beg, tmp.end, buf, fetch_func);
59 bam_plbuf_push(0, buf);
60 bam_index_destroy(idx);
61 bam_plbuf_destroy(buf);