0
|
1 #include <stdio.h>
|
|
2 #include <unistd.h>
|
|
3 #include <assert.h>
|
|
4 #include <fcntl.h>
|
|
5 #include "bam.h"
|
|
6
|
|
7 #ifdef _USE_KNETFILE
|
|
8 #include "knetfile.h"
|
|
9 #endif
|
|
10
|
|
11 #ifndef PACKAGE_VERSION
|
|
12 #define PACKAGE_VERSION "0.1.6 (r453)"
|
|
13 #endif
|
|
14
|
|
15 int bam_taf2baf(int argc, char *argv[]);
|
|
16 int bam_pileup(int argc, char *argv[]);
|
|
17 int bam_merge(int argc, char *argv[]);
|
|
18 int bam_index(int argc, char *argv[]);
|
|
19 int bam_sort(int argc, char *argv[]);
|
|
20 int bam_tview_main(int argc, char *argv[]);
|
|
21 int bam_mating(int argc, char *argv[]);
|
|
22 int bam_rmdup(int argc, char *argv[]);
|
|
23 int bam_rmdupse(int argc, char *argv[]);
|
|
24 int bam_flagstat(int argc, char *argv[]);
|
|
25 int bam_fillmd(int argc, char *argv[]);
|
|
26
|
|
27 int main_samview(int argc, char *argv[]);
|
|
28 int main_import(int argc, char *argv[]);
|
|
29
|
|
30 int faidx_main(int argc, char *argv[]);
|
|
31 int glf3_view_main(int argc, char *argv[]);
|
|
32
|
|
33 int bam_tagview(int argc, char *argv[])
|
|
34 {
|
|
35 bamFile fp;
|
|
36 bam_header_t *header;
|
|
37 bam1_t *b;
|
|
38 char tag[2];
|
|
39 int ret;
|
|
40 if (argc < 3) {
|
|
41 fprintf(stderr, "Usage: samtools tagview <in.bam> <tag>\n");
|
|
42 return 1;
|
|
43 }
|
|
44 fp = strcmp(argv[1], "-")? bam_open(argv[1], "r") : bam_dopen(fileno(stdin), "r");
|
|
45 assert(fp);
|
|
46 header = bam_header_read(fp);
|
|
47 if (header == 0) {
|
|
48 fprintf(stderr, "[bam_view] fail to read the BAM header. Abort!\n");
|
|
49 return 1;
|
|
50 }
|
|
51 tag[0] = argv[2][0]; tag[1] = argv[2][1];
|
|
52 b = (bam1_t*)calloc(1, sizeof(bam1_t));
|
|
53 while ((ret = bam_read1(fp, b)) >= 0) {
|
|
54 uint8_t *d = bam_aux_get(b, tag);
|
|
55 if (d) {
|
|
56 printf("%s\t%d\t", bam1_qname(b), b->core.flag);
|
|
57 if (d[0] == 'Z' || d[0] == 'H') printf("%s\n", bam_aux2Z(d));
|
|
58 else if (d[0] == 'f') printf("%f\n", bam_aux2f(d));
|
|
59 else if (d[0] == 'd') printf("%lf\n", bam_aux2d(d));
|
|
60 else if (d[0] == 'A') printf("%c\n", bam_aux2A(d));
|
|
61 else if (d[0] == 'c' || d[0] == 's' || d[0] == 'i') printf("%d\n", bam_aux2i(d));
|
|
62 else if (d[0] == 'C' || d[0] == 'S' || d[0] == 'I') printf("%u\n", bam_aux2i(d));
|
|
63 else printf("\n");
|
|
64 }
|
|
65 }
|
|
66 if (ret < -1) fprintf(stderr, "[bam_view] truncated file? Continue anyway. (%d)\n", ret);
|
|
67 free(b->data); free(b);
|
|
68 bam_header_destroy(header);
|
|
69 bam_close(fp);
|
|
70 return 0;
|
|
71 }
|
|
72
|
|
73 static int usage()
|
|
74 {
|
|
75 fprintf(stderr, "\n");
|
|
76 fprintf(stderr, "Program: samtools (Tools for alignments in the SAM format)\n");
|
|
77 fprintf(stderr, "Version: %s\n\n", PACKAGE_VERSION);
|
|
78 fprintf(stderr, "Usage: samtools <command> [options]\n\n");
|
|
79 fprintf(stderr, "Command: view SAM<->BAM conversion\n");
|
|
80 fprintf(stderr, " sort sort alignment file\n");
|
|
81 fprintf(stderr, " pileup generate pileup output\n");
|
|
82 fprintf(stderr, " faidx index/extract FASTA\n");
|
|
83 #if _CURSES_LIB != 0
|
|
84 fprintf(stderr, " tview text alignment viewer\n");
|
|
85 #endif
|
|
86 fprintf(stderr, " index index alignment\n");
|
|
87 fprintf(stderr, " fixmate fix mate information\n");
|
|
88 fprintf(stderr, " glfview print GLFv3 file\n");
|
|
89 fprintf(stderr, " flagstat simple stats\n");
|
|
90 fprintf(stderr, " calmd recalculate MD/NM tags and '=' bases\n");
|
|
91 fprintf(stderr, " merge merge sorted alignments (Picard recommended)\n");
|
|
92 fprintf(stderr, " rmdup remove PCR duplicates (Picard recommended)\n");
|
|
93 fprintf(stderr, "\n");
|
|
94 return 1;
|
|
95 }
|
|
96
|
|
97 int main(int argc, char *argv[])
|
|
98 {
|
|
99 #ifdef _WIN32
|
|
100 setmode(fileno(stdout), O_BINARY);
|
|
101 setmode(fileno(stdin), O_BINARY);
|
|
102 #ifdef _USE_KNETFILE
|
|
103 knet_win32_init();
|
|
104 #endif
|
|
105 #endif
|
|
106 if (argc < 2) return usage();
|
|
107 if (strcmp(argv[1], "view") == 0) return main_samview(argc-1, argv+1);
|
|
108 else if (strcmp(argv[1], "import") == 0) return main_import(argc-1, argv+1);
|
|
109 else if (strcmp(argv[1], "pileup") == 0) return bam_pileup(argc-1, argv+1);
|
|
110 else if (strcmp(argv[1], "merge") == 0) return bam_merge(argc-1, argv+1);
|
|
111 else if (strcmp(argv[1], "sort") == 0) return bam_sort(argc-1, argv+1);
|
|
112 else if (strcmp(argv[1], "index") == 0) return bam_index(argc-1, argv+1);
|
|
113 else if (strcmp(argv[1], "faidx") == 0) return faidx_main(argc-1, argv+1);
|
|
114 else if (strcmp(argv[1], "fixmate") == 0) return bam_mating(argc-1, argv+1);
|
|
115 else if (strcmp(argv[1], "rmdup") == 0) return bam_rmdup(argc-1, argv+1);
|
|
116 else if (strcmp(argv[1], "rmdupse") == 0) return bam_rmdupse(argc-1, argv+1);
|
|
117 else if (strcmp(argv[1], "glfview") == 0) return glf3_view_main(argc-1, argv+1);
|
|
118 else if (strcmp(argv[1], "flagstat") == 0) return bam_flagstat(argc-1, argv+1);
|
|
119 else if (strcmp(argv[1], "tagview") == 0) return bam_tagview(argc-1, argv+1);
|
|
120 else if (strcmp(argv[1], "calmd") == 0) return bam_fillmd(argc-1, argv+1);
|
|
121 else if (strcmp(argv[1], "fillmd") == 0) return bam_fillmd(argc-1, argv+1);
|
|
122 #if _CURSES_LIB != 0
|
|
123 else if (strcmp(argv[1], "tview") == 0) return bam_tview_main(argc-1, argv+1);
|
|
124 #endif
|
|
125 else {
|
|
126 fprintf(stderr, "[main] unrecognized command '%s'\n", argv[1]);
|
|
127 return 1;
|
|
128 }
|
|
129 return 0;
|
|
130 }
|