9 // callback for bam_fetch()
10 static int fetch_func(const bam1_t *b, void *data)
12 bam_plbuf_t *buf = (bam_plbuf_t*)data;
13 bam_plbuf_push(b, buf);
16 // callback for bam_plbuf_init()
17 static int pileup_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
19 tmpstruct_t *tmp = (tmpstruct_t*)data;
20 if ((int)pos >= tmp->beg && (int)pos < tmp->end)
21 printf("%s\t%d\t%d\n", tmp->in->header->target_name[tid], pos + 1, n);
25 int main(int argc, char *argv[])
29 fprintf(stderr, "Usage: calDepth <in.bam> [region]\n");
32 tmp.beg = 0; tmp.end = 0x7fffffff;
33 tmp.in = samopen(argv[1], "rb", 0);
35 fprintf(stderr, "Fail to open BAM file %s\n", argv[1]);
38 if (argc == 2) { // if a region is not specified
39 sampileup(tmp.in, -1, pileup_func, &tmp);
44 idx = bam_index_load(argv[1]); // load BAM index
46 fprintf(stderr, "BAM indexing file is not available.\n");
49 bam_parse_region(tmp.in->header, argv[2], &ref, &tmp.beg, &tmp.end); // parse the region
51 fprintf(stderr, "Invalid region %s\n", argv[2]);
54 buf = bam_plbuf_init(pileup_func, &tmp); // initialize pileup
55 bam_fetch(tmp.in->x.bam, idx, ref, tmp.beg, tmp.end, buf, fetch_func);
56 bam_plbuf_push(0, buf); // finalize pileup
57 bam_index_destroy(idx);
58 bam_plbuf_destroy(buf);