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 == 'Z' || type == 'H') { while (*(s)) ++(s); ++(s); } \
32 else if (type == 'B') (s) += 5 + bam_aux_type2size(*(s)) * (*(int32_t*)((s)+1)); \
33 else (s) += bam_aux_type2size(type); \
36 uint8_t *bam_aux_get(const bam1_t *b, const char tag[2])
39 int y = tag[0]<<8 | tag[1];
41 while (s < b->data + b->data_len) {
42 int x = (int)s[0]<<8 | s[1];
49 // s MUST BE returned by bam_aux_get()
50 int bam_aux_del(bam1_t *b, uint8_t *s)
56 memmove(p, s, b->l_aux - (s - aux));
62 void bam_init_header_hash(bam_header_t *header)
64 if (header->hash == 0) {
68 header->hash = h = kh_init(s);
69 for (i = 0; i < header->n_targets; ++i) {
70 iter = kh_put(s, h, header->target_name[i], &ret);
71 kh_value(h, iter) = i;
76 void bam_destroy_header_hash(bam_header_t *header)
79 kh_destroy(s, (khash_t(s)*)header->hash);
82 int32_t bam_get_tid(const bam_header_t *header, const char *seq_name)
85 khash_t(s) *h = (khash_t(s)*)header->hash;
86 k = kh_get(s, h, seq_name);
87 return k == kh_end(h)? -1 : kh_value(h, k);
90 int bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *beg, int *end)
93 int i, l, k, name_end;
97 bam_init_header_hash(header);
98 h = (khash_t(s)*)header->hash;
100 *ref_id = *beg = *end = -1;
101 name_end = l = strlen(str);
102 s = (char*)malloc(l+1);
104 for (i = k = 0; i < l; ++i)
105 if (!isspace(str[i])) s[k++] = str[i];
107 // determine the sequence name
108 for (i = l - 1; i >= 0; --i) if (s[i] == ':') break; // look for colon from the end
109 if (i >= 0) name_end = i;
110 if (name_end < l) { // check if this is really the end
112 for (i = name_end + 1; i < l; ++i) {
113 if (s[i] == '-') ++n_hyphen;
114 else if (!isdigit(s[i]) && s[i] != ',') break;
116 if (i < l || n_hyphen > 1) name_end = l; // malformated region string; then take str as the name
118 iter = kh_get(s, h, s);
119 if (iter == kh_end(h)) { // cannot find the sequence name
120 iter = kh_get(s, h, str); // try str as the name
121 if (iter == kh_end(h)) {
122 if (bam_verbose >= 2) fprintf(stderr, "[%s] fail to determine the sequence name.\n", __func__);
124 } else s[name_end] = ':', name_end = l;
126 } else iter = kh_get(s, h, str);
127 *ref_id = kh_val(h, iter);
128 // parse the interval
130 for (i = k = name_end + 1; i < l; ++i)
131 if (s[i] != ',') s[k++] = s[i];
133 *beg = atoi(s + name_end + 1);
134 for (i = name_end + 1; i != k; ++i) if (s[i] == '-') break;
135 *end = i < k? atoi(s + i + 1) : 1<<29;
136 if (*beg > 0) --*beg;
137 } else *beg = 0, *end = 1<<29;
139 return *beg <= *end? 0 : -1;
142 int32_t bam_aux2i(const uint8_t *s)
145 if (s == 0) return 0;
147 if (type == 'c') return (int32_t)*(int8_t*)s;
148 else if (type == 'C') return (int32_t)*(uint8_t*)s;
149 else if (type == 's') return (int32_t)*(int16_t*)s;
150 else if (type == 'S') return (int32_t)*(uint16_t*)s;
151 else if (type == 'i' || type == 'I') return *(int32_t*)s;
155 float bam_aux2f(const uint8_t *s)
159 if (s == 0) return 0.0;
160 if (type == 'f') return *(float*)s;
164 double bam_aux2d(const uint8_t *s)
168 if (s == 0) return 0.0;
169 if (type == 'd') return *(double*)s;
173 char bam_aux2A(const uint8_t *s)
177 if (s == 0) return 0;
178 if (type == 'A') return *(char*)s;
182 char *bam_aux2Z(const uint8_t *s)
186 if (s == 0) return 0;
187 if (type == 'Z' || type == 'H') return (char*)s;
194 return (double)rand() / RAND_MAX;