Imported Upstream version 0.6
[pysam.git] / samtools / bam_aux.c.pysam.c
1 #include "pysam.h"
2
3 #include <ctype.h>
4 #include "bam.h"
5 #include "khash.h"
6 typedef char *str_p;
7 KHASH_MAP_INIT_STR(s, int)
8 KHASH_MAP_INIT_STR(r2l, str_p)
9
10 void bam_aux_append(bam1_t *b, const char tag[2], char type, int len, uint8_t *data)
11 {
12         int ori_len = b->data_len;
13         b->data_len += 3 + len;
14         b->l_aux += 3 + len;
15         if (b->m_data < b->data_len) {
16                 b->m_data = b->data_len;
17                 kroundup32(b->m_data);
18                 b->data = (uint8_t*)realloc(b->data, b->m_data);
19         }
20         b->data[ori_len] = tag[0]; b->data[ori_len + 1] = tag[1];
21         b->data[ori_len + 2] = type;
22         memcpy(b->data + ori_len + 3, data, len);
23 }
24
25 uint8_t *bam_aux_get_core(bam1_t *b, const char tag[2])
26 {
27         return bam_aux_get(b, tag);
28 }
29
30 #define __skip_tag(s) do { \
31                 int type = toupper(*(s)); \
32                 ++(s); \
33                 if (type == 'Z' || type == 'H') { while (*(s)) ++(s); ++(s); } \
34                 else if (type == 'B') (s) += 5 + bam_aux_type2size(*(s)) * (*(int32_t*)((s)+1)); \
35                 else (s) += bam_aux_type2size(type); \
36         } while(0)
37
38 uint8_t *bam_aux_get(const bam1_t *b, const char tag[2])
39 {
40         uint8_t *s;
41         int y = tag[0]<<8 | tag[1];
42         s = bam1_aux(b);
43         while (s < b->data + b->data_len) {
44                 int x = (int)s[0]<<8 | s[1];
45                 s += 2;
46                 if (x == y) return s;
47                 __skip_tag(s);
48         }
49         return 0;
50 }
51 // s MUST BE returned by bam_aux_get()
52 int bam_aux_del(bam1_t *b, uint8_t *s)
53 {
54         uint8_t *p, *aux;
55         aux = bam1_aux(b);
56         p = s - 2;
57         __skip_tag(s);
58         memmove(p, s, b->l_aux - (s - aux));
59         b->data_len -= s - p;
60         b->l_aux -= s - p;
61         return 0;
62 }
63
64 int bam_aux_drop_other(bam1_t *b, uint8_t *s)
65 {
66         if (s) {
67                 uint8_t *p, *aux;
68                 aux = bam1_aux(b);
69                 p = s - 2;
70                 __skip_tag(s);
71                 memmove(aux, p, s - p);
72                 b->data_len -= b->l_aux - (s - p);
73                 b->l_aux = s - p;
74         } else {
75                 b->data_len -= b->l_aux;
76                 b->l_aux = 0;
77         }
78         return 0;
79 }
80
81 void bam_init_header_hash(bam_header_t *header)
82 {
83         if (header->hash == 0) {
84                 int ret, i;
85                 khiter_t iter;
86                 khash_t(s) *h;
87                 header->hash = h = kh_init(s);
88                 for (i = 0; i < header->n_targets; ++i) {
89                         iter = kh_put(s, h, header->target_name[i], &ret);
90                         kh_value(h, iter) = i;
91                 }
92         }
93 }
94
95 void bam_destroy_header_hash(bam_header_t *header)
96 {
97         if (header->hash)
98                 kh_destroy(s, (khash_t(s)*)header->hash);
99 }
100
101 int32_t bam_get_tid(const bam_header_t *header, const char *seq_name)
102 {
103         khint_t k;
104         khash_t(s) *h = (khash_t(s)*)header->hash;
105         k = kh_get(s, h, seq_name);
106         return k == kh_end(h)? -1 : kh_value(h, k);
107 }
108
109 int bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *beg, int *end)
110 {
111         char *s;
112         int i, l, k, name_end;
113         khiter_t iter;
114         khash_t(s) *h;
115
116         bam_init_header_hash(header);
117         h = (khash_t(s)*)header->hash;
118
119         *ref_id = *beg = *end = -1;
120         name_end = l = strlen(str);
121         s = (char*)malloc(l+1);
122         // remove space
123         for (i = k = 0; i < l; ++i)
124                 if (!isspace(str[i])) s[k++] = str[i];
125         s[k] = 0; l = k;
126         // determine the sequence name
127         for (i = l - 1; i >= 0; --i) if (s[i] == ':') break; // look for colon from the end
128         if (i >= 0) name_end = i;
129         if (name_end < l) { // check if this is really the end
130                 int n_hyphen = 0;
131                 for (i = name_end + 1; i < l; ++i) {
132                         if (s[i] == '-') ++n_hyphen;
133                         else if (!isdigit(s[i]) && s[i] != ',') break;
134                 }
135                 if (i < l || n_hyphen > 1) name_end = l; // malformated region string; then take str as the name
136                 s[name_end] = 0;
137                 iter = kh_get(s, h, s);
138                 if (iter == kh_end(h)) { // cannot find the sequence name
139                         iter = kh_get(s, h, str); // try str as the name
140                         if (iter == kh_end(h)) {
141                                 if (bam_verbose >= 2) fprintf(pysamerr, "[%s] fail to determine the sequence name.\n", __func__);
142                                 free(s); return -1;
143                         } else s[name_end] = ':', name_end = l;
144                 }
145         } else iter = kh_get(s, h, str);
146         *ref_id = kh_val(h, iter);
147         // parse the interval
148         if (name_end < l) {
149                 for (i = k = name_end + 1; i < l; ++i)
150                         if (s[i] != ',') s[k++] = s[i];
151                 s[k] = 0;
152                 *beg = atoi(s + name_end + 1);
153                 for (i = name_end + 1; i != k; ++i) if (s[i] == '-') break;
154                 *end = i < k? atoi(s + i + 1) : 1<<29;
155                 if (*beg > 0) --*beg;
156         } else *beg = 0, *end = 1<<29;
157         free(s);
158         return *beg <= *end? 0 : -1;
159 }
160
161 int32_t bam_aux2i(const uint8_t *s)
162 {
163         int type;
164         if (s == 0) return 0;
165         type = *s++;
166         if (type == 'c') return (int32_t)*(int8_t*)s;
167         else if (type == 'C') return (int32_t)*(uint8_t*)s;
168         else if (type == 's') return (int32_t)*(int16_t*)s;
169         else if (type == 'S') return (int32_t)*(uint16_t*)s;
170         else if (type == 'i' || type == 'I') return *(int32_t*)s;
171         else return 0;
172 }
173
174 float bam_aux2f(const uint8_t *s)
175 {
176         int type;
177         type = *s++;
178         if (s == 0) return 0.0;
179         if (type == 'f') return *(float*)s;
180         else return 0.0;
181 }
182
183 double bam_aux2d(const uint8_t *s)
184 {
185         int type;
186         type = *s++;
187         if (s == 0) return 0.0;
188         if (type == 'd') return *(double*)s;
189         else return 0.0;
190 }
191
192 char bam_aux2A(const uint8_t *s)
193 {
194         int type;
195         type = *s++;
196         if (s == 0) return 0;
197         if (type == 'A') return *(char*)s;
198         else return 0;
199 }
200
201 char *bam_aux2Z(const uint8_t *s)
202 {
203         int type;
204         type = *s++;
205         if (s == 0) return 0;
206         if (type == 'Z' || type == 'H') return (char*)s;
207         else return 0;
208 }
209
210 #ifdef _WIN32
211 double drand48()
212 {
213         return (double)rand() / RAND_MAX;
214 }
215 #endif