comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:acc2ca1a3ba4
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 *begin, int *end)
91 {
92 char *s, *p;
93 int i, l, k;
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 l = strlen(str);
101 p = s = (char*)malloc(l+1);
102 /* squeeze out "," */
103 for (i = k = 0; i != l; ++i)
104 if (str[i] != ',' && !isspace(str[i])) s[k++] = str[i];
105 s[k] = 0;
106 for (i = 0; i != k; ++i) if (s[i] == ':') break;
107 s[i] = 0;
108 iter = kh_get(s, h, s); /* get the ref_id */
109 if (iter == kh_end(h)) { // name not found
110 *ref_id = -1; free(s);
111 return -1;
112 }
113 *ref_id = kh_value(h, iter);
114 if (i == k) { /* dump the whole sequence */
115 *begin = 0; *end = 1<<29; free(s);
116 return 0;
117 }
118 for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break;
119 *begin = atoi(p);
120 if (i < k) {
121 p = s + i + 1;
122 *end = atoi(p);
123 } else *end = 1<<29;
124 if (*begin > 0) --*begin;
125 free(s);
126 if (*begin > *end) {
127 fprintf(stderr, "[bam_parse_region] invalid region.\n");
128 return -1;
129 }
130 return 0;
131 }
132
133 int32_t bam_aux2i(const uint8_t *s)
134 {
135 int type;
136 if (s == 0) return 0;
137 type = *s++;
138 if (type == 'c') return (int32_t)*(int8_t*)s;
139 else if (type == 'C') return (int32_t)*(uint8_t*)s;
140 else if (type == 's') return (int32_t)*(int16_t*)s;
141 else if (type == 'S') return (int32_t)*(uint16_t*)s;
142 else if (type == 'i' || type == 'I') return *(int32_t*)s;
143 else return 0;
144 }
145
146 float bam_aux2f(const uint8_t *s)
147 {
148 int type;
149 type = *s++;
150 if (s == 0) return 0.0;
151 if (type == 'f') return *(float*)s;
152 else return 0.0;
153 }
154
155 double bam_aux2d(const uint8_t *s)
156 {
157 int type;
158 type = *s++;
159 if (s == 0) return 0.0;
160 if (type == 'd') return *(double*)s;
161 else return 0.0;
162 }
163
164 char bam_aux2A(const uint8_t *s)
165 {
166 int type;
167 type = *s++;
168 if (s == 0) return 0;
169 if (type == 'A') return *(char*)s;
170 else return 0;
171 }
172
173 char *bam_aux2Z(const uint8_t *s)
174 {
175 int type;
176 type = *s++;
177 if (s == 0) return 0;
178 if (type == 'Z' || type == 'H') return (char*)s;
179 else return 0;
180 }
181
182 #ifdef _WIN32
183 double drand48()
184 {
185 return (double)rand() / RAND_MAX;
186 }
187 #endif