Uploading samtools_0.1.17-1_amd64.changes.
[samtools.git] / bam_aux.c
1 #include <ctype.h>
2 #include "bam.h"
3 #include "khash.h"
4 typedef char *str_p;
5 KHASH_MAP_INIT_STR(s, int)
6 KHASH_MAP_INIT_STR(r2l, str_p)
7
8 void bam_aux_append(bam1_t *b, const char tag[2], char type, int len, uint8_t *data)
9 {
10         int ori_len = b->data_len;
11         b->data_len += 3 + len;
12         b->l_aux += 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);
17         }
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);
21 }
22
23 uint8_t *bam_aux_get_core(bam1_t *b, const char tag[2])
24 {
25         return bam_aux_get(b, tag);
26 }
27
28 #define __skip_tag(s) do { \
29                 int type = toupper(*(s)); \
30                 ++(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); \
34         } while(0)
35
36 uint8_t *bam_aux_get(const bam1_t *b, const char tag[2])
37 {
38         uint8_t *s;
39         int y = tag[0]<<8 | tag[1];
40         s = bam1_aux(b);
41         while (s < b->data + b->data_len) {
42                 int x = (int)s[0]<<8 | s[1];
43                 s += 2;
44                 if (x == y) return s;
45                 __skip_tag(s);
46         }
47         return 0;
48 }
49 // s MUST BE returned by bam_aux_get()
50 int bam_aux_del(bam1_t *b, uint8_t *s)
51 {
52         uint8_t *p, *aux;
53         aux = bam1_aux(b);
54         p = s - 2;
55         __skip_tag(s);
56         memmove(p, s, b->l_aux - (s - aux));
57         b->data_len -= s - p;
58         b->l_aux -= s - p;
59         return 0;
60 }
61
62 void bam_init_header_hash(bam_header_t *header)
63 {
64         if (header->hash == 0) {
65                 int ret, i;
66                 khiter_t iter;
67                 khash_t(s) *h;
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;
72                 }
73         }
74 }
75
76 void bam_destroy_header_hash(bam_header_t *header)
77 {
78         if (header->hash)
79                 kh_destroy(s, (khash_t(s)*)header->hash);
80 }
81
82 int32_t bam_get_tid(const bam_header_t *header, const char *seq_name)
83 {
84         khint_t k;
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);
88 }
89
90 int bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *beg, int *end)
91 {
92         char *s;
93         int i, l, k, name_end;
94         khiter_t iter;
95         khash_t(s) *h;
96
97         bam_init_header_hash(header);
98         h = (khash_t(s)*)header->hash;
99
100         *ref_id = *beg = *end = -1;
101         name_end = l = strlen(str);
102         s = (char*)malloc(l+1);
103         // remove space
104         for (i = k = 0; i < l; ++i)
105                 if (!isspace(str[i])) s[k++] = str[i];
106         s[k] = 0; l = k;
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
111                 int n_hyphen = 0;
112                 for (i = name_end + 1; i < l; ++i) {
113                         if (s[i] == '-') ++n_hyphen;
114                         else if (!isdigit(s[i]) && s[i] != ',') break;
115                 }
116                 if (i < l || n_hyphen > 1) name_end = l; // malformated region string; then take str as the name
117                 s[name_end] = 0;
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__);
123                                 free(s); return -1;
124                         } else s[name_end] = ':', name_end = l;
125                 }
126         } else iter = kh_get(s, h, str);
127         *ref_id = kh_val(h, iter);
128         // parse the interval
129         if (name_end < l) {
130                 for (i = k = name_end + 1; i < l; ++i)
131                         if (s[i] != ',') s[k++] = s[i];
132                 s[k] = 0;
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;
138         free(s);
139         return *beg <= *end? 0 : -1;
140 }
141
142 int32_t bam_aux2i(const uint8_t *s)
143 {
144         int type;
145         if (s == 0) return 0;
146         type = *s++;
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;
152         else return 0;
153 }
154
155 float bam_aux2f(const uint8_t *s)
156 {
157         int type;
158         type = *s++;
159         if (s == 0) return 0.0;
160         if (type == 'f') return *(float*)s;
161         else return 0.0;
162 }
163
164 double bam_aux2d(const uint8_t *s)
165 {
166         int type;
167         type = *s++;
168         if (s == 0) return 0.0;
169         if (type == 'd') return *(double*)s;
170         else return 0.0;
171 }
172
173 char bam_aux2A(const uint8_t *s)
174 {
175         int type;
176         type = *s++;
177         if (s == 0) return 0;
178         if (type == 'A') return *(char*)s;
179         else return 0;
180 }
181
182 char *bam_aux2Z(const uint8_t *s)
183 {
184         int type;
185         type = *s++;
186         if (s == 0) return 0;
187         if (type == 'Z' || type == 'H') return (char*)s;
188         else return 0;
189 }
190
191 #ifdef _WIN32
192 double drand48()
193 {
194         return (double)rand() / RAND_MAX;
195 }
196 #endif