annotate pyPRADA_1.2/tools/samtools-0.1.16/examples/bam2bed.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 static int fetch_func(const bam1_t *b, void *data)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 samfile_t *fp = (samfile_t*)data;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 uint32_t *cigar = bam1_cigar(b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 const bam1_core_t *c = &b->core;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8 int i, l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 if (b->core.tid < 0) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 for (i = l = 0; i < c->n_cigar; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 int op = cigar[i]&0xf;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12 if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 l += cigar[i]>>4;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15 printf("%s\t%d\t%d\t%s\t%d\t%c\n", fp->header->target_name[c->tid],
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 c->pos, c->pos + l, bam1_qname(b), c->qual, (c->flag&BAM_FREVERSE)? '-' : '+');
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 int main(int argc, char *argv[])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 samfile_t *fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 if (argc == 1) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 fprintf(stderr, "Usage: bam2bed <in.bam> [region]\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 if ((fp = samopen(argv[1], "rb", 0)) == 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 fprintf(stderr, "bam2bed: Fail to open BAM file %s\n", argv[1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 if (argc == 2) { /* if a region is not specified */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 bam1_t *b = bam_init1();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 while (samread(fp, b) >= 0) fetch_func(b, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 bam_destroy1(b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 int ref, beg, end;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 bam_index_t *idx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 if ((idx = bam_index_load(argv[1])) == 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 fprintf(stderr, "bam2bed: BAM indexing file is not available.\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 bam_parse_region(fp->header, argv[2], &ref, &beg, &end);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 if (ref < 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 fprintf(stderr, "bam2bed: Invalid region %s\n", argv[2]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 bam_fetch(fp->x.bam, idx, ref, beg, end, fp, fetch_func);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 bam_index_destroy(idx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 samclose(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 }