annotate pyPRADA_1.2/tools/samtools-0.1.16/examples/calDepth.c @ 3:f17965495ec9 draft default tip

Uploaded
author siyuan
date Tue, 11 Mar 2014 12:14:01 -0400
parents acc2ca1a3ba4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
1 #include <stdio.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2 #include "sam.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 typedef struct {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 int beg, end;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 samfile_t *in;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 } tmpstruct_t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 // callback for bam_fetch()
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 static int fetch_func(const bam1_t *b, void *data)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12 bam_plbuf_t *buf = (bam_plbuf_t*)data;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 bam_plbuf_push(b, buf);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 // callback for bam_plbuf_init()
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 static int pileup_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 tmpstruct_t *tmp = (tmpstruct_t*)data;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 if ((int)pos >= tmp->beg && (int)pos < tmp->end)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 printf("%s\t%d\t%d\n", tmp->in->header->target_name[tid], pos + 1, n);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25 int main(int argc, char *argv[])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 tmpstruct_t tmp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 if (argc == 1) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 fprintf(stderr, "Usage: calDepth <in.bam> [region]\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 tmp.beg = 0; tmp.end = 0x7fffffff;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 tmp.in = samopen(argv[1], "rb", 0);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 if (tmp.in == 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 fprintf(stderr, "Fail to open BAM file %s\n", argv[1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 if (argc == 2) { // if a region is not specified
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 sampileup(tmp.in, -1, pileup_func, &tmp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 int ref;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 bam_index_t *idx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 bam_plbuf_t *buf;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 idx = bam_index_load(argv[1]); // load BAM index
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 if (idx == 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 fprintf(stderr, "BAM indexing file is not available.\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 bam_parse_region(tmp.in->header, argv[2], &ref, &tmp.beg, &tmp.end); // parse the region
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 if (ref < 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 fprintf(stderr, "Invalid region %s\n", argv[2]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 buf = bam_plbuf_init(pileup_func, &tmp); // initialize pileup
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 bam_fetch(tmp.in->x.bam, idx, ref, tmp.beg, tmp.end, buf, fetch_func);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 bam_plbuf_push(0, buf); // finalize pileup
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 bam_index_destroy(idx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58 bam_plbuf_destroy(buf);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 samclose(tmp.in);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 }