annotate PsiCLASS-1.0.2/samtools-0.1.19/bam_aux.c @ 0:903fc43d6227 draft default tip

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