]> git.donarmstrong.com Git - samtools.git/blob - bam_aux.c
* samtools-0.1.3-7 (r246)
[samtools.git] / bam_aux.c
1 #include <ctype.h>
2 #include "bam.h"
3 #include "khash.h"
4 KHASH_MAP_INIT_STR(s, int)
5
6 void bam_aux_append(bam1_t *b, const char tag[2], char type, int len, uint8_t *data)
7 {
8         int ori_len = b->data_len;
9         b->data_len += 3 + len;
10         b->l_aux += 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);
15         }
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);
19 }
20
21 uint8_t *bam_aux_get(bam1_t *b, const char tag[2])
22 {
23         uint8_t *s;
24         int y = tag[0]<<8 | tag[1];
25         s = bam1_aux(b);
26         while (s < b->data + b->data_len) {
27                 int type, x = (int)s[0]<<8 | s[1];
28                 s += 2;
29                 if (x == y) return s;
30                 type = toupper(*s); ++s;
31                 if (type == 'C') ++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; }
36         }
37         return 0;
38 }
39
40 void bam_init_header_hash(bam_header_t *header)
41 {
42         if (header->hash == 0) {
43                 int ret, i;
44                 khiter_t iter;
45                 khash_t(s) *h;
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;
50                 }
51         }
52 }
53
54 void bam_destroy_header_hash(bam_header_t *header)
55 {
56         if (header->hash)
57                 kh_destroy(s, (khash_t(s)*)header->hash);
58 }
59
60 int32_t bam_get_tid(const bam_header_t *header, const char *seq_name)
61 {
62         khint_t k;
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);
66 }
67
68 void bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end)
69 {
70         char *s, *p;
71         int i, l, k;
72         khiter_t iter;
73         khash_t(s) *h;
74
75         bam_init_header_hash(header);
76         h = (khash_t(s)*)header->hash;
77
78         l = strlen(str);
79         p = s = (char*)malloc(l+1);
80         /* squeeze out "," */
81         for (i = k = 0; i != l; ++i)
82                 if (str[i] != ',' && !isspace(str[i])) s[k++] = str[i];
83         s[k] = 0;
84         for (i = 0; i != k; ++i) if (s[i] == ':') break;
85         s[i] = 0;
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);
89                 return;
90         }
91         *ref_id = kh_value(h, iter);
92         if (i == k) { /* dump the whole sequence */
93                 *begin = 0; *end = 1<<29; free(s);
94                 return;
95         }
96         for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break;
97         *begin = atoi(p);
98         if (i < k) {
99                 p = s + i + 1;
100                 *end = atoi(p);
101         } else *end = 1<<29;
102         if (*begin > 0) --*begin;
103         assert(*begin <= *end);
104         free(s);
105 }
106
107 int32_t bam_aux2i(const uint8_t *s)
108 {
109         int type;
110         if (s == 0) return 0;
111         type = *s++;
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;
117         else return 0;
118 }
119
120 float bam_aux2f(const uint8_t *s)
121 {
122         int type;
123         type = *s++;
124         if (s == 0) return 0.0;
125         if (type == 'f') return *(float*)s;
126         else return 0.0;
127 }
128
129 double bam_aux2d(const uint8_t *s)
130 {
131         int type;
132         type = *s++;
133         if (s == 0) return 0.0;
134         if (type == 'd') return *(double*)s;
135         else return 0.0;
136 }
137
138 char bam_aux2A(const uint8_t *s)
139 {
140         int type;
141         type = *s++;
142         if (s == 0) return 0;
143         if (type == 'A') return *(char*)s;
144         else return 0;
145 }
146
147 char *bam_aux2Z(const uint8_t *s)
148 {
149         int type;
150         type = *s++;
151         if (s == 0) return 0;
152         if (type == 'Z' || type == 'H') return (char*)s;
153         else return 0;
154 }
155
156 char bam_aux_getCSi(bam1_t *b, int i)
157 {
158         uint8_t *c = bam_aux_get(b, "CS");
159         char *cs = NULL;
160
161         // return the base if the tag was not found
162         if(0 == c) return 0;
163
164         cs = bam_aux2Z(c);
165         // adjust for strandedness and leading adaptor
166         if(bam1_strand(b)) i = strlen(cs) - 1 - i;
167         else i++;
168         return cs[i];
169 }
170
171 char bam_aux_getCQi(bam1_t *b, int i)
172 {
173         uint8_t *c = bam_aux_get(b, "CQ");
174         char *cq = NULL;
175         
176         // return the base if the tag was not found
177         if(0 == c) return 0;
178
179         cq = bam_aux2Z(c);
180         // adjust for strandedness
181         if(bam1_strand(b)) i = strlen(cq) - 1 - i;
182         return cq[i];
183 }
184
185 char bam_aux_nt2int(char a)
186 {
187         switch(toupper(a)) {
188                 case 'A':
189                         return 0;
190                         break;
191                 case 'C':
192                         return 1;
193                         break;
194                 case 'G':
195                         return 2;
196                         break;
197                 case 'T':
198                         return 3;
199                         break;
200                 default:
201                         return 4;
202                         break;
203         }
204 }
205
206 char bam_aux_ntnt2cs(char a, char b)
207 {
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)];
212 }
213
214 char bam_aux_getCEi(bam1_t *b, int i)
215 {
216         int cs_i;
217         uint8_t *c = bam_aux_get(b, "CS");
218         char *cs = NULL;
219         char prev_b, cur_b;
220         char cur_color, cor_color;
221
222         // return the base if the tag was not found
223         if(0 == c) return 0;
224         
225         cs = bam_aux2Z(c);
226
227         // adjust for strandedness and leading adaptor
228         if(bam1_strand(b)) { //reverse strand
229                 cs_i = strlen(cs) - 1 - i;
230                 // get current color
231                 cur_color = cs[cs_i];
232                 // get previous base
233                 prev_b = (0 == cs_i) ? cs[0] : bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i+1)];
234                 // get current base
235                 cur_b = bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i)]; 
236         }
237         else {
238                 cs_i=i+1;
239                 // get current color
240                 cur_color = cs[cs_i];
241                 // get previous base
242                 prev_b = (0 == i) ? cs[0] : bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i-1)];
243                 // get current base
244                 cur_b = bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i)];
245         }
246
247         // corrected color
248         cor_color = bam_aux_ntnt2cs(prev_b, cur_b);
249
250         if(cur_color == cor_color) { 
251                 return '-';
252         }
253         else {
254                 return cur_color;
255         }
256 }