4 KHASH_MAP_INIT_STR(s, int)
6 void bam_aux_append(bam1_t *b, const char tag[2], char type, int len, uint8_t *data)
8 int ori_len = b->data_len;
9 b->data_len += 3 + len;
11 if (b->m_data < b->data_len) {
12 b->m_data = b->data_len;
13 kroundup32(b->m_data);
14 b->data = (uint8_t*)realloc(b->data, b->m_data);
16 b->data[ori_len] = tag[0]; b->data[ori_len + 1] = tag[1];
17 b->data[ori_len + 2] = type;
18 memcpy(b->data + ori_len + 3, data, len);
21 uint8_t *bam_aux_get_core(bam1_t *b, const char tag[2])
23 return bam_aux_get(b, tag);
26 uint8_t *bam_aux_get(bam1_t *b, const char tag[2])
29 int y = tag[0]<<8 | tag[1];
31 while (s < b->data + b->data_len) {
32 int type, x = (int)s[0]<<8 | s[1];
35 type = toupper(*s); ++s;
37 else if (type == 'S') s += 2;
38 else if (type == 'I' || type == 'F') s += 4;
39 else if (type == 'D') s += 8;
40 else if (type == 'Z' || type == 'H') { while (*s) putchar(*s++); ++s; }
45 void bam_init_header_hash(bam_header_t *header)
47 if (header->hash == 0) {
51 header->hash = h = kh_init(s);
52 for (i = 0; i < header->n_targets; ++i) {
53 iter = kh_put(s, h, header->target_name[i], &ret);
54 kh_value(h, iter) = i;
59 void bam_destroy_header_hash(bam_header_t *header)
62 kh_destroy(s, (khash_t(s)*)header->hash);
65 int32_t bam_get_tid(const bam_header_t *header, const char *seq_name)
68 khash_t(s) *h = (khash_t(s)*)header->hash;
69 k = kh_get(s, h, seq_name);
70 return k == kh_end(h)? -1 : kh_value(h, k);
73 void bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end)
80 bam_init_header_hash(header);
81 h = (khash_t(s)*)header->hash;
84 p = s = (char*)malloc(l+1);
86 for (i = k = 0; i != l; ++i)
87 if (str[i] != ',' && !isspace(str[i])) s[k++] = str[i];
89 for (i = 0; i != k; ++i) if (s[i] == ':') break;
91 iter = kh_get(s, h, s); /* get the ref_id */
92 if (iter == kh_end(h)) { // name not found
93 *ref_id = -1; free(s);
96 *ref_id = kh_value(h, iter);
97 if (i == k) { /* dump the whole sequence */
98 *begin = 0; *end = 1<<29; free(s);
101 for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break;
107 if (*begin > 0) --*begin;
108 assert(*begin <= *end);
112 int32_t bam_aux2i(const uint8_t *s)
115 if (s == 0) return 0;
117 if (type == 'c') return (int32_t)*(int8_t*)s;
118 else if (type == 'C') return (int32_t)*(uint8_t*)s;
119 else if (type == 's') return (int32_t)*(int16_t*)s;
120 else if (type == 'S') return (int32_t)*(uint16_t*)s;
121 else if (type == 'i' || type == 'I') return *(int32_t*)s;
125 float bam_aux2f(const uint8_t *s)
129 if (s == 0) return 0.0;
130 if (type == 'f') return *(float*)s;
134 double bam_aux2d(const uint8_t *s)
138 if (s == 0) return 0.0;
139 if (type == 'd') return *(double*)s;
143 char bam_aux2A(const uint8_t *s)
147 if (s == 0) return 0;
148 if (type == 'A') return *(char*)s;
152 char *bam_aux2Z(const uint8_t *s)
156 if (s == 0) return 0;
157 if (type == 'Z' || type == 'H') return (char*)s;
161 char bam_aux_getCSi(bam1_t *b, int i)
163 uint8_t *c = bam_aux_get(b, "CS");
166 // return the base if the tag was not found
170 // adjust for strandedness and leading adaptor
171 if(bam1_strand(b)) i = strlen(cs) - 1 - i;
176 char bam_aux_getCQi(bam1_t *b, int i)
178 uint8_t *c = bam_aux_get(b, "CQ");
181 // return the base if the tag was not found
185 // adjust for strandedness
186 if(bam1_strand(b)) i = strlen(cq) - 1 - i;
190 char bam_aux_nt2int(char a)
211 char bam_aux_ntnt2cs(char a, char b)
213 a = bam_aux_nt2int(a);
214 b = bam_aux_nt2int(b);
215 if(4 == a || 4 == b) return '4';
216 return "0123"[(int)(a ^ b)];
219 char bam_aux_getCEi(bam1_t *b, int i)
222 uint8_t *c = bam_aux_get(b, "CS");
225 char cur_color, cor_color;
227 // return the base if the tag was not found
232 // adjust for strandedness and leading adaptor
233 if(bam1_strand(b)) { //reverse strand
234 cs_i = strlen(cs) - 1 - i;
236 cur_color = cs[cs_i];
238 prev_b = (0 == cs_i) ? cs[0] : bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i+1)];
240 cur_b = bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i)];
245 cur_color = cs[cs_i];
247 prev_b = (0 == i) ? cs[0] : bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i-1)];
249 cur_b = bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i)];
253 cor_color = bam_aux_ntnt2cs(prev_b, cur_b);
255 if(cur_color == cor_color) {