]> git.donarmstrong.com Git - samtools.git/blob - sam_view.c
* samtools-0.1.6-19 (r494)
[samtools.git] / sam_view.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <math.h>
6 #include "sam_header.h"
7 #include "sam.h"
8 #include "faidx.h"
9
10 static int g_min_mapQ = 0, g_flag_on = 0, g_flag_off = 0;
11 static char *g_library, *g_rg;
12 static int g_sol2sanger_tbl[128];
13
14 static void sol2sanger(bam1_t *b)
15 {
16         int l;
17         uint8_t *qual = bam1_qual(b);
18         if (g_sol2sanger_tbl[30] == 0) {
19                 for (l = 0; l != 128; ++l) {
20                         g_sol2sanger_tbl[l] = (int)(10.0 * log(1.0 + pow(10.0, (l - 64 + 33) / 10.0)) / log(10.0) + .499);
21                         if (g_sol2sanger_tbl[l] >= 93) g_sol2sanger_tbl[l] = 93;
22                 }
23         }
24         for (l = 0; l < b->core.l_qseq; ++l)
25                 qual[l] = g_sol2sanger_tbl[qual[l]];
26 }
27
28 static inline int __g_skip_aln(const bam_header_t *h, const bam1_t *b)
29 {
30         if (b->core.qual < g_min_mapQ || ((b->core.flag & g_flag_on) != g_flag_on) || (b->core.flag & g_flag_off))
31                 return 1;
32         if (g_rg) {
33                 uint8_t *s = bam_aux_get(b, "RG");
34                 if (s && strcmp(g_rg, (char*)(s + 1)) == 0) return 0;
35         }
36         if (g_library) {
37                 const char *p = bam_get_library((bam_header_t*)h, b);
38                 return (p && strcmp(p, g_library) == 0)? 0 : 1;
39         }
40         return 0;
41 }
42
43 // callback function for bam_fetch()
44 static int view_func(const bam1_t *b, void *data)
45 {
46         if (!__g_skip_aln(((samfile_t*)data)->header, b))
47                 samwrite((samfile_t*)data, b);
48         return 0;
49 }
50
51 static int usage(int is_long_help);
52
53 int main_samview(int argc, char *argv[])
54 {
55         int c, is_header = 0, is_header_only = 0, is_bamin = 1, ret = 0, is_uncompressed = 0, is_bamout = 0, slx2sngr = 0;
56         int of_type = BAM_OFDEC, is_long_help = 0;
57         samfile_t *in = 0, *out = 0;
58         char in_mode[5], out_mode[5], *fn_out = 0, *fn_list = 0, *fn_ref = 0;
59
60         /* parse command-line options */
61         strcpy(in_mode, "r"); strcpy(out_mode, "w");
62         while ((c = getopt(argc, argv, "Sbt:hHo:q:f:F:ul:r:xX?T:C")) >= 0) {
63                 switch (c) {
64                 case 'C': slx2sngr = 1; break;
65                 case 'S': is_bamin = 0; break;
66                 case 'b': is_bamout = 1; break;
67                 case 't': fn_list = strdup(optarg); is_bamin = 0; break;
68                 case 'h': is_header = 1; break;
69                 case 'H': is_header_only = 1; break;
70                 case 'o': fn_out = strdup(optarg); break;
71                 case 'f': g_flag_on = strtol(optarg, 0, 0); break;
72                 case 'F': g_flag_off = strtol(optarg, 0, 0); break;
73                 case 'q': g_min_mapQ = atoi(optarg); break;
74                 case 'u': is_uncompressed = 1; break;
75                 case 'l': g_library = strdup(optarg); break;
76                 case 'r': g_rg = strdup(optarg); break;
77                 case 'x': of_type = BAM_OFHEX; break;
78                 case 'X': of_type = BAM_OFSTR; break;
79                 case '?': is_long_help = 1; break;
80                 case 'T': fn_ref = strdup(optarg); is_bamin = 0; break;
81                 default: return usage(is_long_help);
82                 }
83         }
84         if (is_uncompressed) is_bamout = 1;
85         if (is_header_only) is_header = 1;
86         if (is_bamout) strcat(out_mode, "b");
87         else {
88                 if (of_type == BAM_OFHEX) strcat(out_mode, "x");
89                 else if (of_type == BAM_OFSTR) strcat(out_mode, "X");
90         }
91         if (is_bamin) strcat(in_mode, "b");
92         if (is_header) strcat(out_mode, "h");
93         if (is_uncompressed) strcat(out_mode, "u");
94         if (argc == optind) return usage(is_long_help);
95
96         // generate the fn_list if necessary
97         if (fn_list == 0 && fn_ref) fn_list = samfaipath(fn_ref);
98         // open file handlers
99         if ((in = samopen(argv[optind], in_mode, fn_list)) == 0) {
100                 fprintf(stderr, "[main_samview] fail to open file for reading.\n");
101                 goto view_end;
102         }
103         if (in->header == 0) {
104                 fprintf(stderr, "[main_samview] fail to read the header.\n");
105                 goto view_end;
106         }
107         if ((out = samopen(fn_out? fn_out : "-", out_mode, in->header)) == 0) {
108                 fprintf(stderr, "[main_samview] fail to open file for writing.\n");
109                 goto view_end;
110         }
111         if (is_header_only) goto view_end; // no need to print alignments
112
113         if (argc == optind + 1) { // convert/print the entire file
114                 bam1_t *b = bam_init1();
115                 int r;
116                 while ((r = samread(in, b)) >= 0) { // read one alignment from `in'
117                         if (!__g_skip_aln(in->header, b)) {
118                                 if (slx2sngr) sol2sanger(b);
119                                 samwrite(out, b); // write the alignment to `out'
120                         }
121                 }
122                 if (r < -1) fprintf(stderr, "[main_samview] truncated file.\n");
123                 bam_destroy1(b);
124         } else { // retrieve alignments in specified regions
125                 int i;
126                 bam_index_t *idx = 0;
127                 if (is_bamin) idx = bam_index_load(argv[optind]); // load BAM index
128                 if (idx == 0) { // index is unavailable
129                         fprintf(stderr, "[main_samview] random alignment retrieval only works for indexed BAM files.\n");
130                         ret = 1;
131                         goto view_end;
132                 }
133                 for (i = optind + 1; i < argc; ++i) {
134                         int tid, beg, end;
135                         bam_parse_region(in->header, argv[i], &tid, &beg, &end); // parse a region in the format like `chr2:100-200'
136                         if (tid < 0) { // reference name is not found
137                                 fprintf(stderr, "[main_samview] fail to get the reference name. Continue anyway.\n");
138                                 continue;
139                         }
140                         bam_fetch(in->x.bam, idx, tid, beg, end, out, view_func); // fetch alignments
141                 }
142                 bam_index_destroy(idx); // destroy the BAM index
143         }
144
145 view_end:
146         // close files, free and return
147         free(fn_list); free(fn_ref); free(fn_out); free(g_library); free(g_rg);
148         samclose(in);
149         samclose(out);
150         return ret;
151 }
152
153 static int usage(int is_long_help)
154 {
155         fprintf(stderr, "\n");
156         fprintf(stderr, "Usage:   samtools view [options] <in.bam>|<in.sam> [region1 [...]]\n\n");
157         fprintf(stderr, "Options: -b       output BAM\n");
158         fprintf(stderr, "         -h       print header for the SAM output\n");
159         fprintf(stderr, "         -H       print header only (no alignments)\n");
160         fprintf(stderr, "         -S       input is SAM\n");
161         fprintf(stderr, "         -u       uncompressed BAM output (force -b)\n");
162         fprintf(stderr, "         -x       output FLAG in HEX (samtools-C specific)\n");
163         fprintf(stderr, "         -X       output FLAG in string (samtools-C specific)\n");
164         fprintf(stderr, "         -t FILE  list of reference names and lengths (force -S) [null]\n");
165         fprintf(stderr, "         -T FILE  reference sequence file (force -S) [null]\n");
166         fprintf(stderr, "         -o FILE  output file name [stdout]\n");
167         fprintf(stderr, "         -f INT   required flag, 0 for unset [0]\n");
168         fprintf(stderr, "         -F INT   filtering flag, 0 for unset [0]\n");
169         fprintf(stderr, "         -q INT   minimum mapping quality [0]\n");
170         fprintf(stderr, "         -l STR   only output reads in library STR [null]\n");
171         fprintf(stderr, "         -r STR   only output reads in read group STR [null]\n");
172         fprintf(stderr, "         -?       longer help\n");
173         fprintf(stderr, "\n");
174         if (is_long_help)
175                 fprintf(stderr, "Notes:\n\
176 \n\
177   1. By default, this command assumes the file on the command line is in\n\
178      the BAM format and it prints the alignments in SAM. If `-t' is\n\
179      applied, the input file is assumed to be in the SAM format. The\n\
180      file supplied with `-t' is SPACE/TAB delimited with the first two\n\
181      fields of each line consisting of the reference name and the\n\
182      corresponding sequence length. The `.fai' file generated by `faidx'\n\
183      can be used here. This file may be empty if reads are unaligned.\n\
184 \n\
185   2. SAM->BAM conversion: `samtools view -bT ref.fa in.sam.gz'.\n\
186 \n\
187   3. BAM->SAM conversion: `samtools view in.bam'.\n\
188 \n\
189   4. A region should be presented in one of the following formats:\n\
190      `chr1', `chr2:1,000' and `chr3:1000-2,000'. When a region is\n\
191      specified, the input alignment file must be an indexed BAM file.\n\
192 \n\
193   5. Option `-u' is preferred over `-b' when the output is piped to\n\
194      another samtools command.\n\
195 \n\
196   6. In a string FLAG, each character represents one bit with\n\
197      p=0x1 (paired), P=0x2 (properly paired), u=0x4 (unmapped),\n\
198      U=0x8 (mate unmapped), r=0x10 (reverse), R=0x20 (mate reverse)\n\
199      1=0x40 (first), 2=0x80 (second), s=0x100 (not primary), \n\
200      f=0x200 (failure) and d=0x400 (duplicate). Note that `-x' and\n\
201      `-X' are samtools-C specific. Picard and older samtools do not\n\
202      support HEX or string flags.\n\
203 \n");
204         return 1;
205 }
206
207 int main_import(int argc, char *argv[])
208 {
209         int argc2, ret;
210         char **argv2;
211         if (argc != 4) {
212                 fprintf(stderr, "Usage: bamtk import <in.ref_list> <in.sam> <out.bam>\n");
213                 return 1;
214         }
215         argc2 = 6;
216         argv2 = calloc(6, sizeof(char*));
217         argv2[0] = "import", argv2[1] = "-o", argv2[2] = argv[3], argv2[3] = "-bt", argv2[4] = argv[1], argv2[5] = argv[2];
218         ret = main_samview(argc2, argv2);
219         free(argv2);
220         return ret;
221 }