5 KHASH_MAP_INIT_STR(s, int)
6 KHASH_MAP_INIT_STR(r2l, str_p)
8 void bam_aux_append(bam1_t *b, const char tag[2], char type, int len, uint8_t *data)
10 int ori_len = b->data_len;
11 b->data_len += 3 + len;
13 if (b->m_data < b->data_len) {
14 b->m_data = b->data_len;
15 kroundup32(b->m_data);
16 b->data = (uint8_t*)realloc(b->data, b->m_data);
18 b->data[ori_len] = tag[0]; b->data[ori_len + 1] = tag[1];
19 b->data[ori_len + 2] = type;
20 memcpy(b->data + ori_len + 3, data, len);
23 uint8_t *bam_aux_get_core(bam1_t *b, const char tag[2])
25 return bam_aux_get(b, tag);
28 #define __skip_tag(s) do { \
29 int type = toupper(*(s)); \
31 if (type == 'C' || type == 'A') ++(s); \
32 else if (type == 'S') (s) += 2; \
33 else if (type == 'I' || type == 'F') (s) += 4; \
34 else if (type == 'D') (s) += 8; \
35 else if (type == 'Z' || type == 'H') { while (*(s)) ++(s); ++(s); } \
38 uint8_t *bam_aux_get(const bam1_t *b, const char tag[2])
41 int y = tag[0]<<8 | tag[1];
43 while (s < b->data + b->data_len) {
44 int x = (int)s[0]<<8 | s[1];
51 // s MUST BE returned by bam_aux_get()
52 int bam_aux_del(bam1_t *b, uint8_t *s)
58 memmove(p, s, b->l_aux - (s - aux));
64 void bam_init_header_hash(bam_header_t *header)
66 if (header->hash == 0) {
70 header->hash = h = kh_init(s);
71 for (i = 0; i < header->n_targets; ++i) {
72 iter = kh_put(s, h, header->target_name[i], &ret);
73 kh_value(h, iter) = i;
78 void bam_destroy_header_hash(bam_header_t *header)
81 kh_destroy(s, (khash_t(s)*)header->hash);
84 int32_t bam_get_tid(const bam_header_t *header, const char *seq_name)
87 khash_t(s) *h = (khash_t(s)*)header->hash;
88 k = kh_get(s, h, seq_name);
89 return k == kh_end(h)? -1 : kh_value(h, k);
92 int bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end)
99 bam_init_header_hash(header);
100 h = (khash_t(s)*)header->hash;
103 p = s = (char*)malloc(l+1);
104 /* squeeze out "," */
105 for (i = k = 0; i != l; ++i)
106 if (str[i] != ',' && !isspace(str[i])) s[k++] = str[i];
108 for (i = 0; i != k; ++i) if (s[i] == ':') break;
110 iter = kh_get(s, h, s); /* get the ref_id */
111 if (iter == kh_end(h)) { // name not found
112 *ref_id = -1; free(s);
115 *ref_id = kh_value(h, iter);
116 if (i == k) { /* dump the whole sequence */
117 *begin = 0; *end = 1<<29; free(s);
120 for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break;
126 if (*begin > 0) --*begin;
129 fprintf(stderr, "[bam_parse_region] invalid region.\n");
135 int32_t bam_aux2i(const uint8_t *s)
138 if (s == 0) return 0;
140 if (type == 'c') return (int32_t)*(int8_t*)s;
141 else if (type == 'C') return (int32_t)*(uint8_t*)s;
142 else if (type == 's') return (int32_t)*(int16_t*)s;
143 else if (type == 'S') return (int32_t)*(uint16_t*)s;
144 else if (type == 'i' || type == 'I') return *(int32_t*)s;
148 float bam_aux2f(const uint8_t *s)
152 if (s == 0) return 0.0;
153 if (type == 'f') return *(float*)s;
157 double bam_aux2d(const uint8_t *s)
161 if (s == 0) return 0.0;
162 if (type == 'd') return *(double*)s;
166 char bam_aux2A(const uint8_t *s)
170 if (s == 0) return 0;
171 if (type == 'A') return *(char*)s;
175 char *bam_aux2Z(const uint8_t *s)
179 if (s == 0) return 0;
180 if (type == 'Z' || type == 'H') return (char*)s;