]> git.donarmstrong.com Git - samtools.git/blob - bam_aux.c
* samtools-0.1.3-3 (r240)
[samtools.git] / bam_aux.c
1 #include <ctype.h>
2 #include "bam.h"
3 #include "khash.h"
4 KHASH_MAP_INIT_STR(s, int)
5
6 void bam_aux_append(bam1_t *b, const char tag[2], char type, int len, uint8_t *data)
7 {
8         int ori_len = b->data_len;
9         b->data_len += 3 + len;
10         b->l_aux += 3 + len;
11         if (b->m_data < b->data_len) {
12                 b->m_data = b->data_len;
13                 kroundup32(b->m_data);
14                 b->data = (uint8_t*)realloc(b->data, b->m_data);
15         }
16         b->data[ori_len] = tag[0]; b->data[ori_len + 1] = tag[1];
17         b->data[ori_len + 2] = type;
18         memcpy(b->data + ori_len + 3, data, len);
19 }
20
21 uint8_t *bam_aux_get(bam1_t *b, const char tag[2])
22 {
23         uint8_t *s;
24         int y = tag[0]<<8 | tag[1];
25         s = bam1_aux(b);
26         while (s < b->data + b->data_len) {
27                 int type, x = (int)s[0]<<8 | s[1];
28                 s += 2;
29                 if (x == y) return s;
30                 type = toupper(*s); ++s;
31                 if (type == 'C') ++s;
32                 else if (type == 'S') s += 2;
33                 else if (type == 'I' || type == 'F') s += 4;
34                 else if (type == 'D') s += 8;
35                 else if (type == 'Z' || type == 'H') { while (*s) putchar(*s++); ++s; }
36         }
37         return 0;
38 }
39
40 void bam_init_header_hash(bam_header_t *header)
41 {
42         if (header->hash == 0) {
43                 int ret, i;
44                 khiter_t iter;
45                 khash_t(s) *h;
46                 header->hash = h = kh_init(s);
47                 for (i = 0; i < header->n_targets; ++i) {
48                         iter = kh_put(s, h, header->target_name[i], &ret);
49                         kh_value(h, iter) = i;
50                 }
51         }
52 }
53
54 void bam_destroy_header_hash(bam_header_t *header)
55 {
56         if (header->hash)
57                 kh_destroy(s, (khash_t(s)*)header->hash);
58 }
59
60 int32_t bam_get_tid(const bam_header_t *header, const char *seq_name)
61 {
62         khint_t k;
63         khash_t(s) *h = (khash_t(s)*)header->hash;
64         k = kh_get(s, h, seq_name);
65         return k == kh_end(h)? -1 : kh_value(h, k);
66 }
67
68 void bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end)
69 {
70         char *s, *p;
71         int i, l, k;
72         khiter_t iter;
73         khash_t(s) *h;
74
75         bam_init_header_hash(header);
76         h = (khash_t(s)*)header->hash;
77         
78         l = strlen(str);
79         p = s = (char*)malloc(l+1);
80         /* squeeze out "," */
81         for (i = k = 0; i != l; ++i)
82                 if (str[i] != ',' && !isspace(str[i])) s[k++] = str[i];
83         s[k] = 0;
84         for (i = 0; i != k; ++i) if (s[i] == ':') break;
85         s[i] = 0;
86         iter = kh_get(s, h, s); /* get the ref_id */
87         if (iter == kh_end(h)) { // name not found
88                 *ref_id = -1; free(s);
89                 return;
90         }
91         *ref_id = kh_value(h, iter);
92         if (i == k) { /* dump the whole sequence */
93                 *begin = 0; *end = 1<<29; free(s);
94                 return;
95         }
96         for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break;
97         *begin = atoi(p);
98         if (i < k) {
99                 p = s + i + 1;
100                 *end = atoi(p);
101         } else *end = 1<<29;
102         if (*begin > 0) --*begin;
103         assert(*begin <= *end);
104         free(s);
105 }
106
107 int32_t bam_aux2i(const uint8_t *s)
108 {
109         int type;
110         if (s == 0) return 0;
111         type = *s++;
112         if (type == 'c') return (int32_t)*(int8_t*)s;
113         else if (type == 'C') return (int32_t)*(uint8_t*)s;
114         else if (type == 's') return (int32_t)*(int16_t*)s;
115         else if (type == 'S') return (int32_t)*(uint16_t*)s;
116         else if (type == 'i' || type == 'I') return *(int32_t*)s;
117         else return 0;
118 }
119
120 float bam_aux2f(const uint8_t *s)
121 {
122         int type;
123         type = *s++;
124         if (s == 0) return 0.0;
125         if (type == 'f') return *(float*)s;
126         else return 0.0;
127 }
128
129 double bam_aux2d(const uint8_t *s)
130 {
131         int type;
132         type = *s++;
133         if (s == 0) return 0.0;
134         if (type == 'd') return *(double*)s;
135         else return 0.0;
136 }
137
138 char bam_aux2A(const uint8_t *s)
139 {
140         int type;
141         type = *s++;
142         if (s == 0) return 0;
143         if (type == 'A') return *(char*)s;
144         else return 0;
145 }
146
147 char *bam_aux2Z(const uint8_t *s)
148 {
149         int type;
150         type = *s++;
151         if (s == 0) return 0;
152         if (type == 'Z' || type == 'H') return (char*)s;
153         else return 0;
154 }