1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,491 @@ |
1 |
+#define _POSIX_C_SOURCE 200112L /* Rsamtools: c99 fileno */ |
|
2 |
+#define _SVID_SOURCE /* Rsamtools: c99 strdup */ |
|
3 |
+#include <zlib.h> |
|
4 |
+#include <stdio.h> |
|
5 |
+#include <ctype.h> |
|
6 |
+#include <string.h> |
|
7 |
+#include <stdlib.h> |
|
8 |
+#include <unistd.h> |
|
9 |
+#include <assert.h> |
|
10 |
+#ifdef _WIN32 |
|
11 |
+#include <fcntl.h> |
|
12 |
+#endif |
|
13 |
+#include "kstring.h" |
|
14 |
+#include "bam.h" |
|
15 |
+#include "sam_header.h" |
|
16 |
+#include "kseq.h" |
|
17 |
+#include "khash.h" |
|
18 |
+ |
|
19 |
+KSTREAM_INIT(gzFile, gzread, 16384) |
|
20 |
+KHASH_MAP_INIT_STR(ref, uint64_t) |
|
21 |
+ |
|
22 |
+void bam_init_header_hash(bam_header_t *header); |
|
23 |
+void bam_destroy_header_hash(bam_header_t *header); |
|
24 |
+int32_t bam_get_tid(const bam_header_t *header, const char *seq_name); |
|
25 |
+ |
|
26 |
+unsigned char bam_nt16_table[256] = { |
|
27 |
+ 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, |
|
28 |
+ 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, |
|
29 |
+ 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, |
|
30 |
+ 1, 2, 4, 8, 15,15,15,15, 15,15,15,15, 15, 0 /*=*/,15,15, |
|
31 |
+ 15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15, |
|
32 |
+ 15,15, 5, 6, 8,15, 7, 9, 15,10,15,15, 15,15,15,15, |
|
33 |
+ 15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15, |
|
34 |
+ 15,15, 5, 6, 8,15, 7, 9, 15,10,15,15, 15,15,15,15, |
|
35 |
+ 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, |
|
36 |
+ 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, |
|
37 |
+ 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, |
|
38 |
+ 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, |
|
39 |
+ 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, |
|
40 |
+ 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, |
|
41 |
+ 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, |
|
42 |
+ 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15 |
|
43 |
+}; |
|
44 |
+ |
|
45 |
+unsigned short bam_char2flag_table[256] = { |
|
46 |
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, |
|
47 |
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, |
|
48 |
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, |
|
49 |
+ 0,BAM_FREAD1,BAM_FREAD2,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, |
|
50 |
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, |
|
51 |
+ BAM_FPROPER_PAIR,0,BAM_FMREVERSE,0, 0,BAM_FMUNMAP,0,0, 0,0,0,0, 0,0,0,0, |
|
52 |
+ 0,0,0,0, BAM_FDUP,0,BAM_FQCFAIL,0, 0,0,0,0, 0,0,0,0, |
|
53 |
+ BAM_FPAIRED,0,BAM_FREVERSE,BAM_FSECONDARY, 0,BAM_FUNMAP,0,0, 0,0,0,0, 0,0,0,0, |
|
54 |
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, |
|
55 |
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, |
|
56 |
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, |
|
57 |
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, |
|
58 |
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, |
|
59 |
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, |
|
60 |
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, |
|
61 |
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 |
|
62 |
+}; |
|
63 |
+ |
|
64 |
+char *bam_nt16_rev_table = "=ACMGRSVTWYHKDBN"; |
|
65 |
+ |
|
66 |
+struct __tamFile_t { |
|
67 |
+ gzFile fp; |
|
68 |
+ kstream_t *ks; |
|
69 |
+ kstring_t *str; |
|
70 |
+ uint64_t n_lines; |
|
71 |
+ int is_first; |
|
72 |
+}; |
|
73 |
+ |
|
74 |
+char **__bam_get_lines(const char *fn, int *_n) // for bam_plcmd.c only |
|
75 |
+{ |
|
76 |
+ char **list = 0, *s; |
|
77 |
+ int n = 0, dret, m = 0; |
|
78 |
+ gzFile fp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(fn, "r"); |
|
79 |
+ kstream_t *ks; |
|
80 |
+ kstring_t *str; |
|
81 |
+ str = (kstring_t*)calloc(1, sizeof(kstring_t)); |
|
82 |
+ ks = ks_init(fp); |
|
83 |
+ while (ks_getuntil(ks, '\n', str, &dret) > 0) { |
|
84 |
+ if (n == m) { |
|
85 |
+ m = m? m << 1 : 16; |
|
86 |
+ list = (char**)realloc(list, m * sizeof(char*)); |
|
87 |
+ } |
|
88 |
+ if (str->s[str->l-1] == '\r') |
|
89 |
+ str->s[--str->l] = '\0'; |
|
90 |
+ s = list[n++] = (char*)calloc(str->l + 1, 1); |
|
91 |
+ strcpy(s, str->s); |
|
92 |
+ } |
|
93 |
+ ks_destroy(ks); |
|
94 |
+ gzclose(fp); |
|
95 |
+ free(str->s); free(str); |
|
96 |
+ *_n = n; |
|
97 |
+ return list; |
|
98 |
+} |
|
99 |
+ |
|
100 |
+static bam_header_t *hash2header(const kh_ref_t *hash) |
|
101 |
+{ |
|
102 |
+ bam_header_t *header; |
|
103 |
+ khiter_t k; |
|
104 |
+ header = bam_header_init(); |
|
105 |
+ header->n_targets = kh_size(hash); |
|
106 |
+ header->target_name = (char**)calloc(kh_size(hash), sizeof(char*)); |
|
107 |
+ header->target_len = (uint32_t*)calloc(kh_size(hash), 4); |
|
108 |
+ for (k = kh_begin(hash); k != kh_end(hash); ++k) { |
|
109 |
+ if (kh_exist(hash, k)) { |
|
110 |
+ int i = (int)kh_value(hash, k); |
|
111 |
+ header->target_name[i] = (char*)kh_key(hash, k); |
|
112 |
+ header->target_len[i] = kh_value(hash, k)>>32; |
|
113 |
+ } |
|
114 |
+ } |
|
115 |
+ bam_init_header_hash(header); |
|
116 |
+ return header; |
|
117 |
+} |
|
118 |
+bam_header_t *sam_header_read2(const char *fn) |
|
119 |
+{ |
|
120 |
+ bam_header_t *header; |
|
121 |
+ int c, dret, ret, error = 0; |
|
122 |
+ gzFile fp; |
|
123 |
+ kstream_t *ks; |
|
124 |
+ kstring_t *str; |
|
125 |
+ kh_ref_t *hash; |
|
126 |
+ khiter_t k; |
|
127 |
+ if (fn == 0) return 0; |
|
128 |
+ fp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(fn, "r"); |
|
129 |
+ if (fp == 0) return 0; |
|
130 |
+ hash = kh_init(ref); |
|
131 |
+ ks = ks_init(fp); |
|
132 |
+ str = (kstring_t*)calloc(1, sizeof(kstring_t)); |
|
133 |
+ while (ks_getuntil(ks, 0, str, &dret) > 0) { |
|
134 |
+ char *s = strdup(str->s); |
|
135 |
+ int len, i; |
|
136 |
+ i = kh_size(hash); |
|
137 |
+ ks_getuntil(ks, 0, str, &dret); |
|
138 |
+ len = atoi(str->s); |
|
139 |
+ k = kh_put(ref, hash, s, &ret); |
|
140 |
+ if (ret == 0) { |
|
141 |
+ fprintf(stderr, "[sam_header_read2] duplicated sequence name: %s\n", s); |
|
142 |
+ error = 1; |
|
143 |
+ } |
|
144 |
+ kh_value(hash, k) = (uint64_t)len<<32 | i; |
|
145 |
+ if (dret != '\n') |
|
146 |
+ while ((c = ks_getc(ks)) != '\n' && c != -1); |
|
147 |
+ } |
|
148 |
+ ks_destroy(ks); |
|
149 |
+ gzclose(fp); |
|
150 |
+ free(str->s); free(str); |
|
151 |
+ fprintf(stderr, "[sam_header_read2] %d sequences loaded.\n", kh_size(hash)); |
|
152 |
+ if (error) return 0; |
|
153 |
+ header = hash2header(hash); |
|
154 |
+ kh_destroy(ref, hash); |
|
155 |
+ return header; |
|
156 |
+} |
|
157 |
+static inline uint8_t *alloc_data(bam1_t *b, int size) |
|
158 |
+{ |
|
159 |
+ if (b->m_data < size) { |
|
160 |
+ b->m_data = size; |
|
161 |
+ kroundup32(b->m_data); |
|
162 |
+ b->data = (uint8_t*)realloc(b->data, b->m_data); |
|
163 |
+ } |
|
164 |
+ return b->data; |
|
165 |
+} |
|
166 |
+static inline void parse_error(int64_t n_lines, const char * __restrict msg) |
|
167 |
+{ |
|
168 |
+ fprintf(stderr, "Parse error at line %lld: %s\n", (long long)n_lines, msg); |
|
169 |
+ abort(); |
|
170 |
+} |
|
171 |
+static inline void append_text(bam_header_t *header, kstring_t *str) |
|
172 |
+{ |
|
173 |
+ size_t x = header->l_text, y = header->l_text + str->l + 2; // 2 = 1 byte dret + 1 byte null |
|
174 |
+ kroundup32(x); kroundup32(y); |
|
175 |
+ if (x < y) |
|
176 |
+ { |
|
177 |
+ header->n_text = y; |
|
178 |
+ header->text = (char*)realloc(header->text, y); |
|
179 |
+ if ( !header->text ) |
|
180 |
+ { |
|
181 |
+ fprintf(stderr,"realloc failed to alloc %ld bytes\n", y); |
|
182 |
+ abort(); |
|
183 |
+ } |
|
184 |
+ } |
|
185 |
+ // Sanity check |
|
186 |
+ if ( header->l_text+str->l+1 >= header->n_text ) |
|
187 |
+ { |
|
188 |
+ fprintf(stderr,"append_text FIXME: %ld>=%ld, x=%ld,y=%ld\n", header->l_text+str->l+1,(long)header->n_text,x,y); |
|
189 |
+ abort(); |
|
190 |
+ } |
|
191 |
+ strncpy(header->text + header->l_text, str->s, str->l+1); // we cannot use strcpy() here. |
|
192 |
+ header->l_text += str->l + 1; |
|
193 |
+ header->text[header->l_text] = 0; |
|
194 |
+} |
|
195 |
+ |
|
196 |
+int sam_header_parse(bam_header_t *h) |
|
197 |
+{ |
|
198 |
+ char **tmp; |
|
199 |
+ int i; |
|
200 |
+ free(h->target_len); free(h->target_name); |
|
201 |
+ h->n_targets = 0; h->target_len = 0; h->target_name = 0; |
|
202 |
+ if (h->l_text < 3) return 0; |
|
203 |
+ if (h->dict == 0) h->dict = sam_header_parse2(h->text); |
|
204 |
+ tmp = sam_header2list(h->dict, "SQ", "SN", &h->n_targets); |
|
205 |
+ if (h->n_targets == 0) return 0; |
|
206 |
+ h->target_name = calloc(h->n_targets, sizeof(void*)); |
|
207 |
+ for (i = 0; i < h->n_targets; ++i) |
|
208 |
+ h->target_name[i] = strdup(tmp[i]); |
|
209 |
+ free(tmp); |
|
210 |
+ tmp = sam_header2list(h->dict, "SQ", "LN", &h->n_targets); |
|
211 |
+ h->target_len = calloc(h->n_targets, 4); |
|
212 |
+ for (i = 0; i < h->n_targets; ++i) |
|
213 |
+ h->target_len[i] = atoi(tmp[i]); |
|
214 |
+ free(tmp); |
|
215 |
+ return h->n_targets; |
|
216 |
+} |
|
217 |
+ |
|
218 |
+bam_header_t *sam_header_read(tamFile fp) |
|
219 |
+{ |
|
220 |
+ int ret, dret; |
|
221 |
+ bam_header_t *header = bam_header_init(); |
|
222 |
+ kstring_t *str = fp->str; |
|
223 |
+ while ((ret = ks_getuntil(fp->ks, KS_SEP_TAB, str, &dret)) >= 0 && str->s[0] == '@') { // skip header |
|
224 |
+ str->s[str->l] = dret; // note that str->s is NOT null terminated!! |
|
225 |
+ append_text(header, str); |
|
226 |
+ if (dret != '\n') { |
|
227 |
+ ret = ks_getuntil(fp->ks, '\n', str, &dret); |
|
228 |
+ str->s[str->l] = '\n'; // NOT null terminated!! |
|
229 |
+ append_text(header, str); |
|
230 |
+ } |
|
231 |
+ ++fp->n_lines; |
|
232 |
+ } |
|
233 |
+ sam_header_parse(header); |
|
234 |
+ bam_init_header_hash(header); |
|
235 |
+ fp->is_first = ret >= 0; /* MTM: not header-only SAM */ |
|
236 |
+ return header; |
|
237 |
+} |
|
238 |
+ |
|
239 |
+int sam_read1(tamFile fp, bam_header_t *header, bam1_t *b) |
|
240 |
+{ |
|
241 |
+ int ret, doff, doff0, dret, z = 0; |
|
242 |
+ bam1_core_t *c = &b->core; |
|
243 |
+ kstring_t *str = fp->str; |
|
244 |
+ kstream_t *ks = fp->ks; |
|
245 |
+ |
|
246 |
+ if (fp->is_first) { |
|
247 |
+ fp->is_first = 0; |
|
248 |
+ ret = str->l; |
|
249 |
+ } else { |
|
250 |
+ do { // special consideration for empty lines |
|
251 |
+ ret = ks_getuntil(fp->ks, KS_SEP_TAB, str, &dret); |
|
252 |
+ if (ret >= 0) z += str->l + 1; |
|
253 |
+ } while (ret == 0); |
|
254 |
+ } |
|
255 |
+ if (ret < 0) return -1; |
|
256 |
+ ++fp->n_lines; |
|
257 |
+ doff = 0; |
|
258 |
+ |
|
259 |
+ { // name |
|
260 |
+ c->l_qname = strlen(str->s) + 1; |
|
261 |
+ memcpy(alloc_data(b, doff + c->l_qname) + doff, str->s, c->l_qname); |
|
262 |
+ doff += c->l_qname; |
|
263 |
+ } |
|
264 |
+ { // flag |
|
265 |
+ long flag; |
|
266 |
+ char *s; |
|
267 |
+ ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; |
|
268 |
+ flag = strtol((char*)str->s, &s, 0); |
|
269 |
+ if (*s) { // not the end of the string |
|
270 |
+ flag = 0; |
|
271 |
+ for (s = str->s; *s; ++s) |
|
272 |
+ flag |= bam_char2flag_table[(int)*s]; |
|
273 |
+ } |
|
274 |
+ c->flag = flag; |
|
275 |
+ } |
|
276 |
+ { // tid, pos, qual |
|
277 |
+ ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; c->tid = bam_get_tid(header, str->s); |
|
278 |
+ if (c->tid < 0 && strcmp(str->s, "*")) { |
|
279 |
+ if (header->n_targets == 0) { |
|
280 |
+ fprintf(stderr, "[sam_read1] missing header? Abort!\n"); |
|
281 |
+ exit(1); |
|
282 |
+ } else fprintf(stderr, "[sam_read1] reference '%s' is recognized as '*'.\n", str->s); |
|
283 |
+ } |
|
284 |
+ ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; c->pos = isdigit(str->s[0])? atoi(str->s) - 1 : -1; |
|
285 |
+ ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; c->qual = isdigit(str->s[0])? atoi(str->s) : 0; |
|
286 |
+ if (ret < 0) return -2; |
|
287 |
+ } |
|
288 |
+ { // cigar |
|
289 |
+ char *s, *t; |
|
290 |
+ int i, op; |
|
291 |
+ long x; |
|
292 |
+ c->n_cigar = 0; |
|
293 |
+ if (ks_getuntil(ks, KS_SEP_TAB, str, &dret) < 0) return -3; |
|
294 |
+ z += str->l + 1; |
|
295 |
+ if (str->s[0] != '*') { |
|
296 |
+ uint32_t *cigar; |
|
297 |
+ for (s = str->s; *s; ++s) { |
|
298 |
+ if ((isalpha(*s)) || (*s=='=')) ++c->n_cigar; |
|
299 |
+ else if (!isdigit(*s)) parse_error(fp->n_lines, "invalid CIGAR character"); |
|
300 |
+ } |
|
301 |
+ b->data = alloc_data(b, doff + c->n_cigar * 4); |
|
302 |
+ cigar = bam1_cigar(b); |
|
303 |
+ for (i = 0, s = str->s; i != c->n_cigar; ++i) { |
|
304 |
+ x = strtol(s, &t, 10); |
|
305 |
+ op = toupper(*t); |
|
306 |
+ if (op == 'M') op = BAM_CMATCH; |
|
307 |
+ else if (op == 'I') op = BAM_CINS; |
|
308 |
+ else if (op == 'D') op = BAM_CDEL; |
|
309 |
+ else if (op == 'N') op = BAM_CREF_SKIP; |
|
310 |
+ else if (op == 'S') op = BAM_CSOFT_CLIP; |
|
311 |
+ else if (op == 'H') op = BAM_CHARD_CLIP; |
|
312 |
+ else if (op == 'P') op = BAM_CPAD; |
|
313 |
+ else if (op == '=') op = BAM_CEQUAL; |
|
314 |
+ else if (op == 'X') op = BAM_CDIFF; |
|
315 |
+ else if (op == 'B') op = BAM_CBACK; |
|
316 |
+ else parse_error(fp->n_lines, "invalid CIGAR operation"); |
|
317 |
+ s = t + 1; |
|
318 |
+ cigar[i] = bam_cigar_gen(x, op); |
|
319 |
+ } |
|
320 |
+ if (*s) parse_error(fp->n_lines, "unmatched CIGAR operation"); |
|
321 |
+ c->bin = bam_reg2bin(c->pos, bam_calend(c, cigar)); |
|
322 |
+ doff += c->n_cigar * 4; |
|
323 |
+ } else { |
|
324 |
+ if (!(c->flag&BAM_FUNMAP)) { |
|
325 |
+ fprintf(stderr, "Parse warning at line %lld: mapped sequence without CIGAR\n", (long long)fp->n_lines); |
|
326 |
+ c->flag |= BAM_FUNMAP; |
|
327 |
+ } |
|
328 |
+ c->bin = bam_reg2bin(c->pos, c->pos + 1); |
|
329 |
+ } |
|
330 |
+ } |
|
331 |
+ { // mtid, mpos, isize |
|
332 |
+ ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; |
|
333 |
+ c->mtid = strcmp(str->s, "=")? bam_get_tid(header, str->s) : c->tid; |
|
334 |
+ ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; |
|
335 |
+ c->mpos = isdigit(str->s[0])? atoi(str->s) - 1 : -1; |
|
336 |
+ ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; |
|
337 |
+ c->isize = (str->s[0] == '-' || isdigit(str->s[0]))? atoi(str->s) : 0; |
|
338 |
+ if (ret < 0) return -4; |
|
339 |
+ } |
|
340 |
+ { // seq and qual |
|
341 |
+ int i; |
|
342 |
+ uint8_t *p = 0; |
|
343 |
+ if (ks_getuntil(ks, KS_SEP_TAB, str, &dret) < 0) return -5; // seq |
|
344 |
+ z += str->l + 1; |
|
345 |
+ if (strcmp(str->s, "*")) { |
|
346 |
+ c->l_qseq = strlen(str->s); |
|
347 |
+ if (c->n_cigar && c->l_qseq != (int32_t)bam_cigar2qlen(c, bam1_cigar(b))) { |
|
348 |
+ fprintf(stderr, "Line %ld, sequence length %i vs %i from CIGAR\n", |
|
349 |
+ (long)fp->n_lines, c->l_qseq, (int32_t)bam_cigar2qlen(c, bam1_cigar(b))); |
|
350 |
+ parse_error(fp->n_lines, "CIGAR and sequence length are inconsistent"); |
|
351 |
+ } |
|
352 |
+ p = (uint8_t*)alloc_data(b, doff + c->l_qseq + (c->l_qseq+1)/2) + doff; |
|
353 |
+ memset(p, 0, (c->l_qseq+1)/2); |
|
354 |
+ for (i = 0; i < c->l_qseq; ++i) |
|
355 |
+ p[i/2] |= bam_nt16_table[(int)str->s[i]] << 4*(1-i%2); |
|
356 |
+ } else c->l_qseq = 0; |
|
357 |
+ if (ks_getuntil(ks, KS_SEP_TAB, str, &dret) < 0) return -6; // qual |
|
358 |
+ z += str->l + 1; |
|
359 |
+ if (strcmp(str->s, "*") && c->l_qseq != strlen(str->s)) |
|
360 |
+ parse_error(fp->n_lines, "sequence and quality are inconsistent"); |
|
361 |
+ p += (c->l_qseq+1)/2; |
|
362 |
+ if (strcmp(str->s, "*") == 0) for (i = 0; i < c->l_qseq; ++i) p[i] = 0xff; |
|
363 |
+ else for (i = 0; i < c->l_qseq; ++i) p[i] = str->s[i] - 33; |
|
364 |
+ doff += c->l_qseq + (c->l_qseq+1)/2; |
|
365 |
+ } |
|
366 |
+ doff0 = doff; |
|
367 |
+ if (dret != '\n' && dret != '\r') { // aux |
|
368 |
+ while (ks_getuntil(ks, KS_SEP_TAB, str, &dret) >= 0) { |
|
369 |
+ uint8_t *s, type, key[2]; |
|
370 |
+ z += str->l + 1; |
|
371 |
+ if (str->l < 6 || str->s[2] != ':' || str->s[4] != ':') |
|
372 |
+ parse_error(fp->n_lines, "missing colon in auxiliary data"); |
|
373 |
+ key[0] = str->s[0]; key[1] = str->s[1]; |
|
374 |
+ type = str->s[3]; |
|
375 |
+ s = alloc_data(b, doff + 3) + doff; |
|
376 |
+ s[0] = key[0]; s[1] = key[1]; s += 2; doff += 2; |
|
377 |
+ if (type == 'A' || type == 'a' || type == 'c' || type == 'C') { // c and C for backward compatibility |
|
378 |
+ s = alloc_data(b, doff + 2) + doff; |
|
379 |
+ *s++ = 'A'; *s = str->s[5]; |
|
380 |
+ doff += 2; |
|
381 |
+ } else if (type == 'I' || type == 'i') { |
|
382 |
+ long long x; |
|
383 |
+ s = alloc_data(b, doff + 5) + doff; |
|
384 |
+ x = (long long)atoll(str->s + 5); |
|
385 |
+ if (x < 0) { |
|
386 |
+ if (x >= -127) { |
|
387 |
+ *s++ = 'c'; *(int8_t*)s = (int8_t)x; |
|
388 |
+ s += 1; doff += 2; |
|
389 |
+ } else if (x >= -32767) { |
|
390 |
+ *s++ = 's'; *(int16_t*)s = (int16_t)x; |
|
391 |
+ s += 2; doff += 3; |
|
392 |
+ } else { |
|
393 |
+ *s++ = 'i'; *(int32_t*)s = (int32_t)x; |
|
394 |
+ s += 4; doff += 5; |
|
395 |
+ if (x < -2147483648ll) |
|
396 |
+ fprintf(stderr, "Parse warning at line %lld: integer %lld is out of range.", |
|
397 |
+ (long long)fp->n_lines, x); |
|
398 |
+ } |
|
399 |
+ } else { |
|
400 |
+ if (x <= 255) { |
|
401 |
+ *s++ = 'C'; *s++ = (uint8_t)x; |
|
402 |
+ doff += 2; |
|
403 |
+ } else if (x <= 65535) { |
|
404 |
+ *s++ = 'S'; *(uint16_t*)s = (uint16_t)x; |
|
405 |
+ s += 2; doff += 3; |
|
406 |
+ } else { |
|
407 |
+ *s++ = 'I'; *(uint32_t*)s = (uint32_t)x; |
|
408 |
+ s += 4; doff += 5; |
|
409 |
+ if (x > 4294967295ll) |
|
410 |
+ fprintf(stderr, "Parse warning at line %lld: integer %lld is out of range.", |
|
411 |
+ (long long)fp->n_lines, x); |
|
412 |
+ } |
|
413 |
+ } |
|
414 |
+ } else if (type == 'f') { |
|
415 |
+ s = alloc_data(b, doff + 5) + doff; |
|
416 |
+ *s++ = 'f'; |
|
417 |
+ *(float*)s = (float)atof(str->s + 5); |
|
418 |
+ s += 4; doff += 5; |
|
419 |
+ } else if (type == 'd') { |
|
420 |
+ s = alloc_data(b, doff + 9) + doff; |
|
421 |
+ *s++ = 'd'; |
|
422 |
+ *(float*)s = (float)atof(str->s + 9); |
|
423 |
+ s += 8; doff += 9; |
|
424 |
+ } else if (type == 'Z' || type == 'H') { |
|
425 |
+ int size = 1 + (str->l - 5) + 1; |
|
426 |
+ if (type == 'H') { // check whether the hex string is valid |
|
427 |
+ int i; |
|
428 |
+ if ((str->l - 5) % 2 == 1) parse_error(fp->n_lines, "length of the hex string not even"); |
|
429 |
+ for (i = 0; i < str->l - 5; ++i) { |
|
430 |
+ int c = toupper(str->s[5 + i]); |
|
431 |
+ if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F'))) |
|
432 |
+ parse_error(fp->n_lines, "invalid hex character"); |
|
433 |
+ } |
|
434 |
+ } |
|
435 |
+ s = alloc_data(b, doff + size) + doff; |
|
436 |
+ *s++ = type; |
|
437 |
+ memcpy(s, str->s + 5, str->l - 5); |
|
438 |
+ s[str->l - 5] = 0; |
|
439 |
+ doff += size; |
|
440 |
+ } else if (type == 'B') { |
|
441 |
+ int32_t n = 0, Bsize, k = 0, size; |
|
442 |
+ char *p; |
|
443 |
+ if (str->l < 8) parse_error(fp->n_lines, "too few values in aux type B"); |
|
444 |
+ Bsize = bam_aux_type2size(str->s[5]); // the size of each element |
|
445 |
+ for (p = (char*)str->s + 6; *p; ++p) // count the number of elements in the array |
|
446 |
+ if (*p == ',') ++n; |
|
447 |
+ p = str->s + 7; // now p points to the first number in the array |
|
448 |
+ size = 6 + Bsize * n; // total number of bytes allocated to this tag |
|
449 |
+ s = alloc_data(b, doff + 6 * Bsize * n) + doff; // allocate memory |
|
450 |
+ *s++ = 'B'; *s++ = str->s[5]; |
|
451 |
+ memcpy(s, &n, 4); s += 4; // write the number of elements |
|
452 |
+ if (str->s[5] == 'c') while (p < str->s + str->l) ((int8_t*)s)[k++] = (int8_t)strtol(p, &p, 0), ++p; |
|
453 |
+ else if (str->s[5] == 'C') while (p < str->s + str->l) ((uint8_t*)s)[k++] = (uint8_t)strtol(p, &p, 0), ++p; |
|
454 |
+ else if (str->s[5] == 's') while (p < str->s + str->l) ((int16_t*)s)[k++] = (int16_t)strtol(p, &p, 0), ++p; // FIXME: avoid unaligned memory |
|
455 |
+ else if (str->s[5] == 'S') while (p < str->s + str->l) ((uint16_t*)s)[k++] = (uint16_t)strtol(p, &p, 0), ++p; |
|
456 |
+ else if (str->s[5] == 'i') while (p < str->s + str->l) ((int32_t*)s)[k++] = (int32_t)strtol(p, &p, 0), ++p; |
|
457 |
+ else if (str->s[5] == 'I') while (p < str->s + str->l) ((uint32_t*)s)[k++] = (uint32_t)strtol(p, &p, 0), ++p; |
|
458 |
+ else if (str->s[5] == 'f') while (p < str->s + str->l) ((float*)s)[k++] = (float)strtod(p, &p), ++p; |
|
459 |
+ else parse_error(fp->n_lines, "unrecognized array type"); |
|
460 |
+ s += Bsize * n; doff += size; |
|
461 |
+ } else parse_error(fp->n_lines, "unrecognized type"); |
|
462 |
+ if (dret == '\n' || dret == '\r') break; |
|
463 |
+ } |
|
464 |
+ } |
|
465 |
+ b->l_aux = doff - doff0; |
|
466 |
+ b->data_len = doff; |
|
467 |
+ if (bam_no_B) bam_remove_B(b); |
|
468 |
+ return z; |
|
469 |
+} |
|
470 |
+ |
|
471 |
+tamFile sam_open(const char *fn) |
|
472 |
+{ |
|
473 |
+ tamFile fp; |
|
474 |
+ gzFile gzfp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "rb") : gzopen(fn, "rb"); |
|
475 |
+ if (gzfp == 0) return 0; |
|
476 |
+ fp = (tamFile)calloc(1, sizeof(struct __tamFile_t)); |
|
477 |
+ fp->str = (kstring_t*)calloc(1, sizeof(kstring_t)); |
|
478 |
+ fp->fp = gzfp; |
|
479 |
+ fp->ks = ks_init(fp->fp); |
|
480 |
+ return fp; |
|
481 |
+} |
|
482 |
+ |
|
483 |
+void sam_close(tamFile fp) |
|
484 |
+{ |
|
485 |
+ if (fp) { |
|
486 |
+ ks_destroy(fp->ks); |
|
487 |
+ gzclose(fp->fp); |
|
488 |
+ free(fp->str->s); free(fp->str); |
|
489 |
+ free(fp); |
|
490 |
+ } |
|
491 |
+} |