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