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(bam1_t *b, const char tag[2])
24 int y = tag[0]<<8 | tag[1];
26 while (s < b->data + b->data_len) {
27 int type, x = (int)s[0]<<8 | s[1];
30 type = toupper(*s); ++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) putchar(*s++); ++s; }
40 void bam_init_header_hash(bam_header_t *header)
42 if (header->hash == 0) {
46 header->hash = h = kh_init(s);
47 for (i = 0; i < header->n_targets; ++i) {
48 iter = kh_put(s, h, header->target_name[i], &ret);
49 kh_value(h, iter) = i;
54 void bam_destroy_header_hash(bam_header_t *header)
57 kh_destroy(s, (khash_t(s)*)header->hash);
60 int32_t bam_get_tid(const bam_header_t *header, const char *seq_name)
63 khash_t(s) *h = (khash_t(s)*)header->hash;
64 k = kh_get(s, h, seq_name);
65 return k == kh_end(h)? -1 : kh_value(h, k);
68 void bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end)
75 bam_init_header_hash(header);
76 h = (khash_t(s)*)header->hash;
79 p = s = (char*)malloc(l+1);
81 for (i = k = 0; i != l; ++i)
82 if (str[i] != ',' && !isspace(str[i])) s[k++] = str[i];
84 for (i = 0; i != k; ++i) if (s[i] == ':') break;
86 iter = kh_get(s, h, s); /* get the ref_id */
87 if (iter == kh_end(h)) { // name not found
88 *ref_id = -1; free(s);
91 *ref_id = kh_value(h, iter);
92 if (i == k) { /* dump the whole sequence */
93 *begin = 0; *end = 1<<29; free(s);
96 for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break;
102 if (*begin > 0) --*begin;
103 assert(*begin <= *end);
107 int32_t bam_aux2i(const uint8_t *s)
110 if (s == 0) return 0;
112 if (type == 'c') return (int32_t)*(int8_t*)s;
113 else if (type == 'C') return (int32_t)*(uint8_t*)s;
114 else if (type == 's') return (int32_t)*(int16_t*)s;
115 else if (type == 'S') return (int32_t)*(uint16_t*)s;
116 else if (type == 'i' || type == 'I') return *(int32_t*)s;
120 float bam_aux2f(const uint8_t *s)
124 if (s == 0) return 0.0;
125 if (type == 'f') return *(float*)s;
129 double bam_aux2d(const uint8_t *s)
133 if (s == 0) return 0.0;
134 if (type == 'd') return *(double*)s;
138 char bam_aux2A(const uint8_t *s)
142 if (s == 0) return 0;
143 if (type == 'A') return *(char*)s;
147 char *bam_aux2Z(const uint8_t *s)
151 if (s == 0) return 0;
152 if (type == 'Z' || type == 'H') return (char*)s;
156 char bam_aux_getCSi(bam1_t *b, int i)
158 uint8_t *c = bam_aux_get(b, "CS");
161 // return the base if the tag was not found
165 // adjust for strandedness and leading adaptor
166 if(bam1_strand(b)) i = strlen(cs) - 1 - i;
171 char bam_aux_getCQi(bam1_t *b, int i)
173 uint8_t *c = bam_aux_get(b, "CQ");
176 // return the base if the tag was not found
180 // adjust for strandedness
181 if(bam1_strand(b)) i = strlen(cq) - 1 - i;
185 char bam_aux_nt2int(char a)
206 char bam_aux_ntnt2cs(char a, char b)
208 a = bam_aux_nt2int(a);
209 b = bam_aux_nt2int(b);
210 if(4 == a || 4 == b) return '4';
211 return "0123"[(int)(a ^ b)];
214 char bam_aux_getCEi(bam1_t *b, int i)
217 uint8_t *c = bam_aux_get(b, "CS");
220 char cur_color, cor_color;
222 // return the base if the tag was not found
227 // adjust for strandedness and leading adaptor
228 if(bam1_strand(b)) { //reverse strand
229 cs_i = strlen(cs) - 1 - i;
231 cur_color = cs[cs_i];
233 prev_b = (0 == cs_i) ? cs[0] : bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i+1)];
235 cur_b = bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i)];
240 cur_color = cs[cs_i];
242 prev_b = (0 == i) ? cs[0] : bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i-1)];
244 cur_b = bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i)];
248 cor_color = bam_aux_ntnt2cs(prev_b, cur_b);
250 if(cur_color == cor_color) {