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