4 KHASH_MAP_INIT_INT(aux, uint8_t*)
5 KHASH_MAP_INIT_STR(s, int)
7 void bam_init_header_hash(bam_header_t *header)
9 if (header->hash == 0) {
13 header->hash = h = kh_init(s);
14 for (i = 0; i < header->n_targets; ++i) {
15 iter = kh_put(s, h, header->target_name[i], &ret);
16 kh_value(h, iter) = i;
21 void bam_destroy_header_hash(bam_header_t *header)
24 kh_destroy(s, (khash_t(s)*)header->hash);
27 int32_t bam_get_tid(const bam_header_t *header, const char *seq_name)
30 khash_t(s) *h = (khash_t(s)*)header->hash;
31 k = kh_get(s, h, seq_name);
32 return k == kh_end(h)? -1 : kh_value(h, k);
35 void bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end)
42 bam_init_header_hash(header);
43 h = (khash_t(s)*)header->hash;
46 p = s = (char*)malloc(l+1);
48 for (i = k = 0; i != l; ++i)
49 if (str[i] != ',' && !isspace(str[i])) s[k++] = str[i];
51 for (i = 0; i != k; ++i) if (s[i] == ':') break;
53 iter = kh_get(s, h, s); /* get the ref_id */
54 if (iter == kh_end(h)) { // name not found
55 *ref_id = -1; free(s);
58 *ref_id = kh_value(h, iter);
59 if (i == k) { /* dump the whole sequence */
60 *begin = 0; *end = 1<<29; free(s);
63 for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break;
69 if (*begin > 0) --*begin;
70 assert(*begin <= *end);
74 void bam_aux_init(bam1_t *b)
82 h = (khash_t(aux)*)b->hash;
86 while (s < b->data + b->data_len) {
87 uint32_t x = (uint32_t)s[0]<<8 | s[1];
90 s += 2; type = toupper(*s); ++s;
91 k = kh_put(aux, h, x, &ret);
94 else if (type == 'S') s += 2;
95 else if (type == 'I') s += 4;
96 else if (type == 'F') s += 4;
97 else if (type == 'Z') { while (*s) putchar(*s++); ++s; }
100 void bam_aux_destroy(bam1_t *b)
102 khash_t(aux) *h = (khash_t(aux)*)b->hash;
106 static uint8_t *bam_aux_get_core(bam1_t *b, const char tag[2])
108 uint32_t x = (uint32_t)tag[0]<<8 | tag[1];
111 if (b->hash == 0) bam_aux_init(b);
112 h = (khash_t(aux)*)b->hash;
113 k = kh_get(aux, h, x);
114 if (k == kh_end(h)) return 0;
115 return kh_value(h, k);
117 int32_t bam_aux_geti(bam1_t *b, const char tag[2], int *err)
120 uint8_t *s = bam_aux_get_core(b, tag);
122 if (s == 0) { *err = -1; return 0; }
124 if (type == 'c') return (int32_t)*(int8_t*)s;
125 else if (type == 'C') return (int32_t)*(uint8_t*)s;
126 else if (type == 's') return (int32_t)*(int16_t*)s;
127 else if (type == 'S') return (int32_t)*(uint16_t*)s;
128 else if (type == 'i' || type == 'I') return *(int32_t*)s;
129 else { *err = -2; return 0; }
131 float bam_aux_getf(bam1_t *b, const char tag[2], int *err)
134 uint8_t *s = bam_aux_get_core(b, tag);
137 if (s == 0) { *err = -1; return 0; }
138 if (type == 'f') return *(float*)s;
139 else { *err = -2; return 0; }
141 char bam_aux_getc(bam1_t *b, const char tag[2], int *err)
144 uint8_t *s = bam_aux_get_core(b, tag);
147 if (s == 0) { *err = -1; return 0; }
148 if (type == 'c') return *(char*)s;
149 else { *err = -2; return 0; }
151 char *bam_aux_getZH(bam1_t *b, const char tag[2], int *err)
154 uint8_t *s = bam_aux_get_core(b, tag);
157 if (s == 0) { *err = -1; return 0; }
158 if (type == 'Z' || type == 'H') return (char*)s;
159 else { *err = -2; return 0; }