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