annotate PsiCLASS-1.0.2/samtools-0.1.19/sam_view.c @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 #include <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #include <stdio.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include <unistd.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #include <math.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 #include <inttypes.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 #include "sam_header.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 #include "sam.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 #include "faidx.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 #include "kstring.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 #include "khash.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 KHASH_SET_INIT_STR(rg)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 // When counting records instead of printing them,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 // data passed to the bam_fetch callback is encapsulated in this struct.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 bam_header_t *header;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 int64_t *count; // int does overflow for very big BAMs
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 } count_func_data_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 typedef khash_t(rg) *rghash_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 // FIXME: we'd better use no global variables...
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 static rghash_t g_rghash = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 static int g_min_mapQ = 0, g_flag_on = 0, g_flag_off = 0, g_qual_scale = 0, g_min_qlen = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 static uint32_t g_subsam_seed = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 static double g_subsam_frac = -1.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 static char *g_library, *g_rg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 static void *g_bed;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 void *bed_read(const char *fn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 void bed_destroy(void *_h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 int bed_overlap(const void *_h, const char *chr, int beg, int end);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 static int process_aln(const bam_header_t *h, bam1_t *b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 if (g_qual_scale > 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 uint8_t *qual = bam1_qual(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 for (i = 0; i < b->core.l_qseq; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 int c = qual[i] * g_qual_scale;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 qual[i] = c < 93? c : 93;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 if (g_min_qlen > 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 int k, qlen = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 uint32_t *cigar = bam1_cigar(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 for (k = 0; k < b->core.n_cigar; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 if ((bam_cigar_type(bam_cigar_op(cigar[k]))&1) || bam_cigar_op(cigar[k]) == BAM_CHARD_CLIP)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 qlen += bam_cigar_oplen(cigar[k]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 if (qlen < g_min_qlen) return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 if (b->core.qual < g_min_mapQ || ((b->core.flag & g_flag_on) != g_flag_on) || (b->core.flag & g_flag_off))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 if (g_bed && b->core.tid >= 0 && !bed_overlap(g_bed, h->target_name[b->core.tid], b->core.pos, bam_calend(&b->core, bam1_cigar(b))))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 if (g_subsam_frac > 0.) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 uint32_t k = __ac_X31_hash_string(bam1_qname(b)) + g_subsam_seed;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 if ((double)(k&0xffffff) / 0x1000000 >= g_subsam_frac) return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 if (g_rg || g_rghash) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 uint8_t *s = bam_aux_get(b, "RG");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 if (s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 if (g_rg) return (strcmp(g_rg, (char*)(s + 1)) == 0)? 0 : 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 if (g_rghash) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 khint_t k = kh_get(rg, g_rghash, (char*)(s + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 return (k != kh_end(g_rghash))? 0 : 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 if (g_library) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 const char *p = bam_get_library((bam_header_t*)h, b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 return (p && strcmp(p, g_library) == 0)? 0 : 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 static char *drop_rg(char *hdtxt, rghash_t h, int *len)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 char *p = hdtxt, *q, *r, *s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 kstring_t str;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 memset(&str, 0, sizeof(kstring_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 while (1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 int toprint = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 q = strchr(p, '\n');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 if (q == 0) q = p + strlen(p);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 if (q - p < 3) break; // the line is too short; then stop
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 if (strncmp(p, "@RG\t", 4) == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 int c;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 khint_t k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 if ((r = strstr(p, "\tID:")) != 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 r += 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 for (s = r; *s != '\0' && *s != '\n' && *s != '\t'; ++s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 c = *s; *s = '\0';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 k = kh_get(rg, h, r);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 *s = c;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 if (k != kh_end(h)) toprint = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 } else toprint = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 if (toprint) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 kputsn(p, q - p, &str); kputc('\n', &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 p = q + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 *len = str.l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 return str.s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 // callback function for bam_fetch() that prints nonskipped records
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 static int view_func(const bam1_t *b, void *data)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 if (!process_aln(((samfile_t*)data)->header, (bam1_t*)b))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 samwrite((samfile_t*)data, b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 // callback function for bam_fetch() that counts nonskipped records
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 static int count_func(const bam1_t *b, void *data)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 if (!process_aln(((count_func_data_t*)data)->header, (bam1_t*)b)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 (*((count_func_data_t*)data)->count)++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 static int usage(int is_long_help);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 int main_samview(int argc, char *argv[])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 int c, is_header = 0, is_header_only = 0, is_bamin = 1, ret = 0, compress_level = -1, is_bamout = 0, is_count = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 int of_type = BAM_OFDEC, is_long_help = 0, n_threads = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 int64_t count = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 samfile_t *in = 0, *out = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 char in_mode[5], out_mode[5], *fn_out = 0, *fn_list = 0, *fn_ref = 0, *fn_rg = 0, *q;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 /* parse command-line options */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 strcpy(in_mode, "r"); strcpy(out_mode, "w");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 while ((c = getopt(argc, argv, "SbBct:h1Ho:q:f:F:ul:r:xX?T:R:L:s:Q:@:m:")) >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 switch (c) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 case 's':
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 if ((g_subsam_seed = strtol(optarg, &q, 10)) != 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 srand(g_subsam_seed);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 g_subsam_seed = rand();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 g_subsam_frac = strtod(q, &q);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 case 'm': g_min_qlen = atoi(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 case 'c': is_count = 1; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 case 'S': is_bamin = 0; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 case 'b': is_bamout = 1; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 case 't': fn_list = strdup(optarg); is_bamin = 0; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 case 'h': is_header = 1; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 case 'H': is_header_only = 1; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 case 'o': fn_out = strdup(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 case 'f': g_flag_on = strtol(optarg, 0, 0); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 case 'F': g_flag_off = strtol(optarg, 0, 0); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 case 'q': g_min_mapQ = atoi(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 case 'u': compress_level = 0; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 case '1': compress_level = 1; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 case 'l': g_library = strdup(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 case 'L': g_bed = bed_read(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 case 'r': g_rg = strdup(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 case 'R': fn_rg = strdup(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 case 'x': of_type = BAM_OFHEX; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 case 'X': of_type = BAM_OFSTR; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 case '?': is_long_help = 1; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 case 'T': fn_ref = strdup(optarg); is_bamin = 0; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 case 'B': bam_no_B = 1; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 case 'Q': g_qual_scale = atoi(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 case '@': n_threads = strtol(optarg, 0, 0); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 default: return usage(is_long_help);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 if (compress_level >= 0) is_bamout = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 if (is_header_only) is_header = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 if (is_bamout) strcat(out_mode, "b");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 if (of_type == BAM_OFHEX) strcat(out_mode, "x");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 else if (of_type == BAM_OFSTR) strcat(out_mode, "X");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 if (is_bamin) strcat(in_mode, "b");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 if (is_header) strcat(out_mode, "h");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 if (compress_level >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 char tmp[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 tmp[0] = compress_level + '0'; tmp[1] = '\0';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 strcat(out_mode, tmp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 if (argc == optind) return usage(is_long_help); // potential memory leak...
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 // read the list of read groups
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 if (fn_rg) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 FILE *fp_rg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 char buf[1024];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 int ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 g_rghash = kh_init(rg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 fp_rg = fopen(fn_rg, "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 while (!feof(fp_rg) && fscanf(fp_rg, "%s", buf) > 0) // this is not a good style, but bear me...
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 kh_put(rg, g_rghash, strdup(buf), &ret); // we'd better check duplicates...
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 fclose(fp_rg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 // generate the fn_list if necessary
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 if (fn_list == 0 && fn_ref) fn_list = samfaipath(fn_ref);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 // open file handlers
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 if ((in = samopen(argv[optind], in_mode, fn_list)) == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 fprintf(stderr, "[main_samview] fail to open \"%s\" for reading.\n", argv[optind]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 ret = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 goto view_end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 if (in->header == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 fprintf(stderr, "[main_samview] fail to read the header from \"%s\".\n", argv[optind]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 ret = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 goto view_end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 if (g_rghash) { // FIXME: I do not know what "bam_header_t::n_text" is for...
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 char *tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 int l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 tmp = drop_rg(in->header->text, g_rghash, &l);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 free(in->header->text);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 in->header->text = tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 in->header->l_text = l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 if (!is_count && (out = samopen(fn_out? fn_out : "-", out_mode, in->header)) == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 fprintf(stderr, "[main_samview] fail to open \"%s\" for writing.\n", fn_out? fn_out : "standard output");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 ret = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 goto view_end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 if (n_threads > 1) samthreads(out, n_threads, 256);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 if (is_header_only) goto view_end; // no need to print alignments
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 if (argc == optind + 1) { // convert/print the entire file
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 bam1_t *b = bam_init1();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 int r;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 while ((r = samread(in, b)) >= 0) { // read one alignment from `in'
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 if (!process_aln(in->header, b)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 if (!is_count) samwrite(out, b); // write the alignment to `out'
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 count++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 if (r < -1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 fprintf(stderr, "[main_samview] truncated file.\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 ret = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 bam_destroy1(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 } else { // retrieve alignments in specified regions
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 bam_index_t *idx = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 if (is_bamin) idx = bam_index_load(argv[optind]); // load BAM index
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 if (idx == 0) { // index is unavailable
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 fprintf(stderr, "[main_samview] random alignment retrieval only works for indexed BAM files.\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 ret = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 goto view_end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 for (i = optind + 1; i < argc; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 int tid, beg, end, result;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 bam_parse_region(in->header, argv[i], &tid, &beg, &end); // parse a region in the format like `chr2:100-200'
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 if (tid < 0) { // reference name is not found
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 fprintf(stderr, "[main_samview] region \"%s\" specifies an unknown reference name. Continue anyway.\n", argv[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 // fetch alignments
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 if (is_count) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 count_func_data_t count_data = { in->header, &count };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 result = bam_fetch(in->x.bam, idx, tid, beg, end, &count_data, count_func);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 } else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 result = bam_fetch(in->x.bam, idx, tid, beg, end, out, view_func);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 if (result < 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 fprintf(stderr, "[main_samview] retrieval of region \"%s\" failed due to truncated file or corrupt BAM index file\n", argv[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 ret = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 bam_index_destroy(idx); // destroy the BAM index
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 view_end:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 if (is_count && ret == 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 printf("%" PRId64 "\n", count);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 // close files, free and return
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 free(fn_list); free(fn_ref); free(fn_out); free(g_library); free(g_rg); free(fn_rg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 if (g_bed) bed_destroy(g_bed);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 if (g_rghash) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 khint_t k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 for (k = 0; k < kh_end(g_rghash); ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 if (kh_exist(g_rghash, k)) free((char*)kh_key(g_rghash, k));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 kh_destroy(rg, g_rghash);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 samclose(in);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 if (!is_count)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 samclose(out);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 return ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 static int usage(int is_long_help)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 fprintf(stderr, "\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 fprintf(stderr, "Usage: samtools view [options] <in.bam>|<in.sam> [region1 [...]]\n\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 fprintf(stderr, "Options: -b output BAM\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 fprintf(stderr, " -h print header for the SAM output\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 fprintf(stderr, " -H print header only (no alignments)\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 fprintf(stderr, " -S input is SAM\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 fprintf(stderr, " -u uncompressed BAM output (force -b)\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 fprintf(stderr, " -1 fast compression (force -b)\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 fprintf(stderr, " -x output FLAG in HEX (samtools-C specific)\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 fprintf(stderr, " -X output FLAG in string (samtools-C specific)\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 fprintf(stderr, " -c print only the count of matching records\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 fprintf(stderr, " -B collapse the backward CIGAR operation\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 fprintf(stderr, " -@ INT number of BAM compression threads [0]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 fprintf(stderr, " -L FILE output alignments overlapping the input BED FILE [null]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 fprintf(stderr, " -t FILE list of reference names and lengths (force -S) [null]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 fprintf(stderr, " -T FILE reference sequence file (force -S) [null]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 fprintf(stderr, " -o FILE output file name [stdout]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 fprintf(stderr, " -R FILE list of read groups to be outputted [null]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 fprintf(stderr, " -f INT required flag, 0 for unset [0]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 fprintf(stderr, " -F INT filtering flag, 0 for unset [0]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 fprintf(stderr, " -q INT minimum mapping quality [0]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 fprintf(stderr, " -l STR only output reads in library STR [null]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 fprintf(stderr, " -r STR only output reads in read group STR [null]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 fprintf(stderr, " -s FLOAT fraction of templates to subsample; integer part as seed [-1]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 fprintf(stderr, " -? longer help\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 fprintf(stderr, "\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 if (is_long_help)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 fprintf(stderr, "Notes:\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 \n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 1. By default, this command assumes the file on the command line is in\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 the BAM format and it prints the alignments in SAM. If `-t' is\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 applied, the input file is assumed to be in the SAM format. The\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 file supplied with `-t' is SPACE/TAB delimited with the first two\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 fields of each line consisting of the reference name and the\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 corresponding sequence length. The `.fai' file generated by `faidx'\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 can be used here. This file may be empty if reads are unaligned.\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 \n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 2. SAM->BAM conversion: `samtools view -bT ref.fa in.sam.gz'.\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 \n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 3. BAM->SAM conversion: `samtools view in.bam'.\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 \n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 4. A region should be presented in one of the following formats:\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339 `chr1', `chr2:1,000' and `chr3:1000-2,000'. When a region is\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 specified, the input alignment file must be an indexed BAM file.\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 \n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 5. Option `-u' is preferred over `-b' when the output is piped to\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 another samtools command.\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 \n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345 6. In a string FLAG, each character represents one bit with\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 p=0x1 (paired), P=0x2 (properly paired), u=0x4 (unmapped),\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347 U=0x8 (mate unmapped), r=0x10 (reverse), R=0x20 (mate reverse)\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 1=0x40 (first), 2=0x80 (second), s=0x100 (not primary), \n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 f=0x200 (failure) and d=0x400 (duplicate). Note that `-x' and\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 `-X' are samtools-C specific. Picard and older samtools do not\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351 support HEX or string flags.\n\
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 \n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356 int main_import(int argc, char *argv[])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 int argc2, ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 char **argv2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 if (argc != 4) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361 fprintf(stderr, "Usage: bamtk import <in.ref_list> <in.sam> <out.bam>\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364 argc2 = 6;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365 argv2 = calloc(6, sizeof(char*));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366 argv2[0] = "import", argv2[1] = "-o", argv2[2] = argv[3], argv2[3] = "-bt", argv2[4] = argv[1], argv2[5] = argv[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367 ret = main_samview(argc2, argv2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 free(argv2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369 return ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372 int8_t seq_comp_table[16] = { 0, 8, 4, 12, 2, 10, 9, 14, 1, 6, 5, 13, 3, 11, 7, 15 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 int main_bam2fq(int argc, char *argv[])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376 bamFile fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377 bam_header_t *h;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 bam1_t *b;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 int8_t *buf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380 int max_buf, c, no12 = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381 while ((c = getopt(argc, argv, "n")) > 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382 if (c == 'n') no12 = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383 if (argc == 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 fprintf(stderr, "Usage: samtools bam2fq <in.bam>\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387 fp = strcmp(argv[optind], "-")? bam_open(argv[optind], "r") : bam_dopen(fileno(stdin), "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 if (fp == 0) return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389 h = bam_header_read(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 b = bam_init1();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391 buf = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392 max_buf = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393 while (bam_read1(fp, b) >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394 int i, qlen = b->core.l_qseq;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395 uint8_t *seq;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396 putchar('@'); fputs(bam1_qname(b), stdout);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 if (no12) putchar('\n');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398 else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399 if ((b->core.flag & 0x40) && !(b->core.flag & 0x80)) puts("/1");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 else if ((b->core.flag & 0x80) && !(b->core.flag & 0x40)) puts("/2");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401 else putchar('\n');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
402 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403 if (max_buf < qlen + 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404 max_buf = qlen + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 kroundup32(max_buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406 buf = realloc(buf, max_buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408 buf[qlen] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 seq = bam1_seq(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 for (i = 0; i < qlen; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 buf[i] = bam1_seqi(seq, i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412 if (b->core.flag & 16) { // reverse complement
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 for (i = 0; i < qlen>>1; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414 int8_t t = seq_comp_table[buf[qlen - 1 - i]];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 buf[qlen - 1 - i] = seq_comp_table[buf[i]];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416 buf[i] = t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 if (qlen&1) buf[i] = seq_comp_table[buf[i]];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420 for (i = 0; i < qlen; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421 buf[i] = bam_nt16_rev_table[buf[i]];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 puts((char*)buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423 puts("+");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 seq = bam1_qual(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 for (i = 0; i < qlen; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426 buf[i] = 33 + seq[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427 if (b->core.flag & 16) { // reverse
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428 for (i = 0; i < qlen>>1; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429 int8_t t = buf[qlen - 1 - i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430 buf[qlen - 1 - i] = buf[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431 buf[i] = t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 puts((char*)buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 free(buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437 bam_destroy1(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 bam_header_destroy(h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 bam_close(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 }