12 KSTREAM_INIT(gzFile, gzread, 8192)
13 KHASH_MAP_INIT_STR(ref, uint64_t)
15 void bam_init_header_hash(bam_header_t *header);
16 void bam_destroy_header_hash(bam_header_t *header);
17 int32_t bam_get_tid(const bam_header_t *header, const char *seq_name);
19 unsigned char bam_nt16_table[256] = {
20 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
21 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
22 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
23 1, 2, 4, 8, 15,15,15,15, 15,15,15,15, 15, 0 /*=*/,15,15,
24 15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15,
25 15,15, 5, 6, 8,15, 7, 9, 15,10,15,15, 15,15,15,15,
26 15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15,
27 15,15, 5, 6, 8,15, 7, 9, 15,10,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 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
31 15,15,15,15, 15,15,15,15, 15,15,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
38 char *bam_nt16_rev_table = "=ACMGRSVTWYHKDBN";
48 char **__bam_get_lines(const char *fn, int *_n) // for bam_plcmd.c only
51 int n = 0, dret, m = 0;
52 gzFile fp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(fn, "r");
55 str = (kstring_t*)calloc(1, sizeof(kstring_t));
57 while (ks_getuntil(ks, '\n', str, &dret) > 0) {
60 list = (char**)realloc(list, m * sizeof(char*));
62 if (str->s[str->l-1] == '\r')
63 str->s[--str->l] = '\0';
64 s = list[n++] = (char*)calloc(str->l + 1, 1);
69 free(str->s); free(str);
74 static bam_header_t *hash2header(const kh_ref_t *hash)
78 header = bam_header_init();
79 header->n_targets = kh_size(hash);
80 header->target_name = (char**)calloc(kh_size(hash), sizeof(char*));
81 header->target_len = (uint32_t*)calloc(kh_size(hash), 4);
82 for (k = kh_begin(hash); k != kh_end(hash); ++k) {
83 if (kh_exist(hash, k)) {
84 int i = (int)kh_value(hash, k);
85 header->target_name[i] = (char*)kh_key(hash, k);
86 header->target_len[i] = kh_value(hash, k)>>32;
89 bam_init_header_hash(header);
92 bam_header_t *sam_header_read2(const char *fn)
102 fp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(fn, "r");
105 str = (kstring_t*)calloc(1, sizeof(kstring_t));
106 while (ks_getuntil(ks, 0, str, &dret) > 0) {
107 char *s = strdup(str->s);
110 ks_getuntil(ks, 0, str, &dret);
112 k = kh_put(ref, hash, s, &ret);
113 kh_value(hash, k) = (uint64_t)len<<32 | i;
115 while ((c = ks_getc(ks)) != '\n' && c != -1);
119 free(str->s); free(str);
120 fprintf(stderr, "[sam_header_read2] %d sequences loaded.\n", kh_size(hash));
121 header = hash2header(hash);
122 kh_destroy(ref, hash);
125 static inline uint8_t *alloc_data(bam1_t *b, int size)
127 if (b->m_data < size) {
129 kroundup32(b->m_data);
130 b->data = (uint8_t*)realloc(b->data, b->m_data);
134 static inline void parse_error(int64_t n_lines, const char * __restrict msg)
136 fprintf(stderr, "Parse error at line %lld: %s\n", (long long)n_lines, msg);
139 static inline void append_text(bam_header_t *header, kstring_t *str)
141 int x = header->l_text, y = header->l_text + str->l + 2; // 2 = 1 byte dret + 1 byte null
142 kroundup32(x); kroundup32(y);
143 if (x < y) header->text = (char*)realloc(header->text, y);
144 strncpy(header->text + header->l_text, str->s, str->l+1); // we cannot use strcpy() here.
145 header->l_text += str->l + 1;
146 header->text[header->l_text] = 0;
149 int sam_header_parse(bam_header_t *h)
155 free(h->target_len); free(h->target_name);
156 h->n_targets = 0; h->target_len = 0; h->target_name = 0;
157 if (h->l_text < 3) return 0;
158 // count number of @SQ
160 while ((s = strstr(s, "@SQ")) != 0) {
164 if (h->n_targets == 0) return 0;
165 h->target_len = (uint32_t*)calloc(h->n_targets, 4);
166 h->target_name = (char**)calloc(h->n_targets, sizeof(void*));
170 while ((s = strstr(s, "@SQ")) != 0) {
173 if ((p = strstr(s, "SN:")) != 0) {
175 for (p = q; *p && *p != '\t' && *p != '\r' && *p != '\n'; ++p);
176 h->target_name[i] = (char*)calloc(p - q + 1, 1);
177 strncpy(h->target_name[i], q, p - q);
178 } else goto header_err_ret;
180 if ((p = strstr(s, "LN:")) != 0) h->target_len[i] = strtol(p + 3, 0, 10);
181 else goto header_err_ret;
189 fprintf(stderr, "[bam_header_parse] missing SN or LN tag in @SQ lines.\n");
190 free(h->target_len); free(h->target_name);
191 h->n_targets = 0; h->target_len = 0; h->target_name = 0;
195 bam_header_t *sam_header_read(tamFile fp)
198 bam_header_t *header = bam_header_init();
199 kstring_t *str = fp->str;
200 while ((ret = ks_getuntil(fp->ks, KS_SEP_TAB, str, &dret)) >= 0 && str->s[0] == '@') { // skip header
201 str->s[str->l] = dret; // note that str->s is NOT null terminated!!
202 append_text(header, str);
204 ret = ks_getuntil(fp->ks, '\n', str, &dret);
205 str->s[str->l] = '\n'; // NOT null terminated!!
206 append_text(header, str);
210 sam_header_parse(header);
211 bam_init_header_hash(header);
216 int sam_read1(tamFile fp, bam_header_t *header, bam1_t *b)
218 int ret, doff, doff0, dret, z = 0;
219 bam1_core_t *c = &b->core;
220 kstring_t *str = fp->str;
221 kstream_t *ks = fp->ks;
227 do { // special consideration for empty lines
228 ret = ks_getuntil(fp->ks, KS_SEP_TAB, str, &dret);
229 if (ret >= 0) z += str->l + 1;
232 if (ret < 0) return -1;
237 c->l_qname = strlen(str->s) + 1;
238 memcpy(alloc_data(b, doff + c->l_qname) + doff, str->s, c->l_qname);
241 { // flag, tid, pos, qual
242 ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; c->flag = atoi(str->s);
243 ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; c->tid = bam_get_tid(header, str->s);
244 if (c->tid < 0 && strcmp(str->s, "*")) {
245 if (header->n_targets == 0) {
246 fprintf(stderr, "[sam_read1] missing header? Abort!\n");
248 } else fprintf(stderr, "[sam_read1] reference '%s' is recognized as '*'.\n", str->s);
250 ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; c->pos = isdigit(str->s[0])? atoi(str->s) - 1 : -1;
251 ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; c->qual = isdigit(str->s[0])? atoi(str->s) : 0;
252 if (ret < 0) return -2;
259 if (ks_getuntil(ks, KS_SEP_TAB, str, &dret) < 0) return -3;
261 if (str->s[0] != '*') {
262 for (s = str->s; *s; ++s) {
263 if (isalpha(*s)) ++c->n_cigar;
264 else if (!isdigit(*s)) parse_error(fp->n_lines, "invalid CIGAR character");
266 b->data = alloc_data(b, doff + c->n_cigar * 4);
267 for (i = 0, s = str->s; i != c->n_cigar; ++i) {
268 x = strtol(s, &t, 10);
270 if (op == 'M') op = BAM_CMATCH;
271 else if (op == 'I') op = BAM_CINS;
272 else if (op == 'D') op = BAM_CDEL;
273 else if (op == 'N') op = BAM_CREF_SKIP;
274 else if (op == 'S') op = BAM_CSOFT_CLIP;
275 else if (op == 'H') op = BAM_CHARD_CLIP;
276 else if (op == 'P') op = BAM_CPAD;
277 else parse_error(fp->n_lines, "invalid CIGAR operation");
279 bam1_cigar(b)[i] = x << BAM_CIGAR_SHIFT | op;
281 if (*s) parse_error(fp->n_lines, "unmatched CIGAR operation");
282 c->bin = bam_reg2bin(c->pos, bam_calend(c, bam1_cigar(b)));
283 doff += c->n_cigar * 4;
284 } else c->bin = bam_reg2bin(c->pos, c->pos + 1);
286 { // mtid, mpos, isize
287 ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1;
288 c->mtid = strcmp(str->s, "=")? bam_get_tid(header, str->s) : c->tid;
289 ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1;
290 c->mpos = isdigit(str->s[0])? atoi(str->s) - 1 : -1;
291 ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1;
292 c->isize = (str->s[0] == '-' || isdigit(str->s[0]))? atoi(str->s) : 0;
293 if (ret < 0) return -4;
298 if (ks_getuntil(ks, KS_SEP_TAB, str, &dret) < 0) return -5; // seq
300 c->l_qseq = strlen(str->s);
301 if (c->n_cigar && c->l_qseq != (int32_t)bam_cigar2qlen(c, bam1_cigar(b)))
302 parse_error(fp->n_lines, "CIGAR and sequence length are inconsistent");
303 p = (uint8_t*)alloc_data(b, doff + c->l_qseq + (c->l_qseq+1)/2) + doff;
304 bzero(p, (c->l_qseq+1)/2);
305 for (i = 0; i < c->l_qseq; ++i)
306 p[i/2] |= bam_nt16_table[(int)str->s[i]] << 4*(1-i%2);
307 if (ks_getuntil(ks, KS_SEP_TAB, str, &dret) < 0) return -6; // qual
309 if (strcmp(str->s, "*") && c->l_qseq != strlen(str->s))
310 parse_error(fp->n_lines, "sequence and quality are inconsistent");
311 p += (c->l_qseq+1)/2;
312 if (strcmp(str->s, "*") == 0) for (i = 0; i < c->l_qseq; ++i) p[i] = 0xff;
313 else for (i = 0; i < c->l_qseq; ++i) p[i] = str->s[i] - 33;
314 doff += c->l_qseq + (c->l_qseq+1)/2;
317 if (dret != '\n' && dret != '\r') { // aux
318 while (ks_getuntil(ks, KS_SEP_TAB, str, &dret) >= 0) {
319 uint8_t *s, type, key[2];
321 if (str->l < 6 || str->s[2] != ':' || str->s[4] != ':')
322 parse_error(fp->n_lines, "missing colon in auxiliary data");
323 key[0] = str->s[0]; key[1] = str->s[1];
325 s = alloc_data(b, doff + 3) + doff;
326 s[0] = key[0]; s[1] = key[1]; s += 2; doff += 2;
327 if (type == 'A' || type == 'a' || type == 'c' || type == 'C') { // c and C for backward compatibility
328 s = alloc_data(b, doff + 2) + doff;
329 *s++ = 'A'; *s = str->s[5];
331 } else if (type == 'I' || type == 'i') {
333 s = alloc_data(b, doff + 5) + doff;
334 x = (long long)atoll(str->s + 5);
337 *s++ = 'c'; *(int8_t*)s = (int8_t)x;
339 } else if (x >= -32767) {
340 *s++ = 's'; *(int16_t*)s = (int16_t)x;
343 *s++ = 'i'; *(int32_t*)s = (int32_t)x;
345 if (x < -2147483648ll)
346 fprintf(stderr, "Parse warning at line %lld: integer %lld is out of range.",
347 (long long)fp->n_lines, x);
351 *s++ = 'C'; *s++ = (uint8_t)x;
353 } else if (x <= 65535) {
354 *s++ = 'S'; *(uint16_t*)s = (uint16_t)x;
357 *s++ = 'I'; *(uint32_t*)s = (uint32_t)x;
359 if (x > 4294967295ll)
360 fprintf(stderr, "Parse warning at line %lld: integer %lld is out of range.",
361 (long long)fp->n_lines, x);
364 } else if (type == 'f') {
365 s = alloc_data(b, doff + 5) + doff;
367 *(float*)s = (float)atof(str->s + 5);
369 } else if (type == 'd') {
370 s = alloc_data(b, doff + 9) + doff;
372 *(float*)s = (float)atof(str->s + 9);
374 } else if (type == 'Z' || type == 'H') {
375 int size = 1 + (str->l - 5) + 1;
376 if (type == 'H') { // check whether the hex string is valid
378 if ((str->l - 5) % 2 == 1) parse_error(fp->n_lines, "length of the hex string not even");
379 for (i = 0; i < str->l - 5; ++i) {
380 int c = toupper(str->s[5 + i]);
381 if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')))
382 parse_error(fp->n_lines, "invalid hex character");
385 s = alloc_data(b, doff + size) + doff;
387 memcpy(s, str->s + 5, str->l - 5);
390 } else parse_error(fp->n_lines, "unrecognized type");
391 if (dret == '\n' || dret == '\r') break;
394 b->l_aux = doff - doff0;
399 tamFile sam_open(const char *fn)
402 fp = (tamFile)calloc(1, sizeof(struct __tamFile_t));
403 fp->str = (kstring_t*)calloc(1, sizeof(kstring_t));
404 fp->fp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(fn, "r");
405 fp->ks = ks_init(fp->fp);
409 void sam_close(tamFile fp)
414 free(fp->str->s); free(fp->str);