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