annotate PsiCLASS-1.0.2/samtools-0.1.19/examples/calDepth.c @ 0:903fc43d6227 draft default tip

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