annotate pyPRADA_1.2/tools/samtools-0.1.16/bam_aux.c @ 0:acc2ca1a3ba4

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