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