6 #include "bam_endian.h"
10 char *bam_flag2char_table = "pPuUrR12sfd\0\0\0\0\0";
12 /**************************
13 * CIGAR related routines *
14 **************************/
16 int bam_tpos2qpos(const bam1_core_t *c, const uint32_t *cigar, int32_t tpos, int32_t *_tpos)
18 int k, x = c->pos, y = 0, last_y = 0;
20 for (k = 0; k < c->n_cigar; ++k) {
21 int op = cigar[k] & BAM_CIGAR_MASK;
22 int l = cigar[k] >> BAM_CIGAR_SHIFT;
23 if (op == BAM_CMATCH) {
24 if (c->pos > tpos) return y;
27 return y + (tpos - x);
31 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
32 else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
44 uint32_t bam_calend(const bam1_core_t *c, const uint32_t *cigar)
48 for (k = 0; k < c->n_cigar; ++k) {
49 int op = cigar[k] & BAM_CIGAR_MASK;
50 if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP)
51 end += cigar[k] >> BAM_CIGAR_SHIFT;
56 int32_t bam_cigar2qlen(const bam1_core_t *c, const uint32_t *cigar)
60 for (k = 0; k < c->n_cigar; ++k) {
61 int op = cigar[k] & BAM_CIGAR_MASK;
62 if (op == BAM_CMATCH || op == BAM_CINS || op == BAM_CSOFT_CLIP)
63 l += cigar[k] >> BAM_CIGAR_SHIFT;
72 bam_header_t *bam_header_init()
74 bam_is_be = bam_is_big_endian();
75 return (bam_header_t*)calloc(1, sizeof(bam_header_t));
78 void bam_header_destroy(bam_header_t *header)
81 extern void bam_destroy_header_hash(bam_header_t *header);
82 if (header == 0) return;
83 if (header->target_name) {
84 for (i = 0; i < header->n_targets; ++i)
85 free(header->target_name[i]);
86 free(header->target_name);
87 free(header->target_len);
91 if (header->rg2lib) bam_strmap_destroy(header->rg2lib);
92 bam_destroy_header_hash(header);
97 bam_header_t *bam_header_read(bamFile fp)
101 int32_t i = 1, name_len;
103 i = bgzf_check_EOF(fp);
105 // If the file is a pipe, checking the EOF marker will *always* fail
106 // with ESPIPE. Suppress the error message in this case.
107 if (errno != ESPIPE) perror("[bam_header_read] bgzf_check_EOF");
109 else if (i == 0) fprintf(stderr, "[bam_header_read] EOF marker is absent.\n");
111 if (bam_read(fp, buf, 4) != 4) return 0;
112 if (strncmp(buf, "BAM\001", 4)) {
113 fprintf(stderr, "[bam_header_read] wrong header\n");
116 header = bam_header_init();
117 // read plain text and the number of reference sequences
118 bam_read(fp, &header->l_text, 4);
119 if (bam_is_be) bam_swap_endian_4p(&header->l_text);
120 header->text = (char*)calloc(header->l_text + 1, 1);
121 bam_read(fp, header->text, header->l_text);
122 bam_read(fp, &header->n_targets, 4);
123 if (bam_is_be) bam_swap_endian_4p(&header->n_targets);
124 // read reference sequence names and lengths
125 header->target_name = (char**)calloc(header->n_targets, sizeof(char*));
126 header->target_len = (uint32_t*)calloc(header->n_targets, 4);
127 for (i = 0; i != header->n_targets; ++i) {
128 bam_read(fp, &name_len, 4);
129 if (bam_is_be) bam_swap_endian_4p(&name_len);
130 header->target_name[i] = (char*)calloc(name_len, 1);
131 bam_read(fp, header->target_name[i], name_len);
132 bam_read(fp, &header->target_len[i], 4);
133 if (bam_is_be) bam_swap_endian_4p(&header->target_len[i]);
138 int bam_header_write(bamFile fp, const bam_header_t *header)
141 int32_t i, name_len, x;
143 strncpy(buf, "BAM\001", 4);
144 bam_write(fp, buf, 4);
145 // write plain text and the number of reference sequences
147 x = bam_swap_endian_4(header->l_text);
148 bam_write(fp, &x, 4);
149 if (header->l_text) bam_write(fp, header->text, header->l_text);
150 x = bam_swap_endian_4(header->n_targets);
151 bam_write(fp, &x, 4);
153 bam_write(fp, &header->l_text, 4);
154 if (header->l_text) bam_write(fp, header->text, header->l_text);
155 bam_write(fp, &header->n_targets, 4);
157 // write sequence names and lengths
158 for (i = 0; i != header->n_targets; ++i) {
159 char *p = header->target_name[i];
160 name_len = strlen(p) + 1;
162 x = bam_swap_endian_4(name_len);
163 bam_write(fp, &x, 4);
164 } else bam_write(fp, &name_len, 4);
165 bam_write(fp, p, name_len);
167 x = bam_swap_endian_4(header->target_len[i]);
168 bam_write(fp, &x, 4);
169 } else bam_write(fp, &header->target_len[i], 4);
174 static void swap_endian_data(const bam1_core_t *c, int data_len, uint8_t *data)
177 uint32_t i, *cigar = (uint32_t*)(data + c->l_qname);
178 s = data + c->n_cigar*4 + c->l_qname + c->l_qseq + (c->l_qseq + 1)/2;
179 for (i = 0; i < c->n_cigar; ++i) bam_swap_endian_4p(&cigar[i]);
180 while (s < data + data_len) {
183 type = toupper(*s); ++s; // skip type
184 if (type == 'C' || type == 'A') ++s;
185 else if (type == 'S') { bam_swap_endian_2p(s); s += 2; }
186 else if (type == 'I' || type == 'F') { bam_swap_endian_4p(s); s += 4; }
187 else if (type == 'D') { bam_swap_endian_8p(s); s += 8; }
188 else if (type == 'Z' || type == 'H') { while (*s) ++s; ++s; }
192 int bam_read1(bamFile fp, bam1_t *b)
194 bam1_core_t *c = &b->core;
195 int32_t block_len, ret, i;
198 assert(BAM_CORE_SIZE == 32);
199 if ((ret = bam_read(fp, &block_len, 4)) != 4) {
200 if (ret == 0) return -1; // normal end-of-file
201 else return -2; // truncated
203 if (bam_read(fp, x, BAM_CORE_SIZE) != BAM_CORE_SIZE) return -3;
205 bam_swap_endian_4p(&block_len);
206 for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i);
208 c->tid = x[0]; c->pos = x[1];
209 c->bin = x[2]>>16; c->qual = x[2]>>8&0xff; c->l_qname = x[2]&0xff;
210 c->flag = x[3]>>16; c->n_cigar = x[3]&0xffff;
212 c->mtid = x[5]; c->mpos = x[6]; c->isize = x[7];
213 b->data_len = block_len - BAM_CORE_SIZE;
214 if (b->m_data < b->data_len) {
215 b->m_data = b->data_len;
216 kroundup32(b->m_data);
217 b->data = (uint8_t*)realloc(b->data, b->m_data);
219 if (bam_read(fp, b->data, b->data_len) != b->data_len) return -4;
220 b->l_aux = b->data_len - c->n_cigar * 4 - c->l_qname - c->l_qseq - (c->l_qseq+1)/2;
221 if (bam_is_be) swap_endian_data(c, b->data_len, b->data);
222 return 4 + block_len;
225 inline int bam_write1_core(bamFile fp, const bam1_core_t *c, int data_len, uint8_t *data)
227 uint32_t x[8], block_len = data_len + BAM_CORE_SIZE, y;
229 assert(BAM_CORE_SIZE == 32);
232 x[2] = (uint32_t)c->bin<<16 | c->qual<<8 | c->l_qname;
233 x[3] = (uint32_t)c->flag<<16 | c->n_cigar;
239 for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i);
241 bam_write(fp, bam_swap_endian_4p(&y), 4);
242 swap_endian_data(c, data_len, data);
243 } else bam_write(fp, &block_len, 4);
244 bam_write(fp, x, BAM_CORE_SIZE);
245 bam_write(fp, data, data_len);
246 if (bam_is_be) swap_endian_data(c, data_len, data);
247 return 4 + block_len;
250 int bam_write1(bamFile fp, const bam1_t *b)
252 return bam_write1_core(fp, &b->core, b->data_len, b->data);
255 char *bam_format1_core(const bam_header_t *header, const bam1_t *b, int of)
257 uint8_t *s = bam1_seq(b), *t = bam1_qual(b);
259 const bam1_core_t *c = &b->core;
261 str.l = str.m = 0; str.s = 0;
263 ksprintf(&str, "%s\t", bam1_qname(b));
264 if (of == BAM_OFDEC) ksprintf(&str, "%d\t", c->flag);
265 else if (of == BAM_OFHEX) ksprintf(&str, "0x%x\t", c->flag);
267 for (i = 0; i < 16; ++i)
268 if ((c->flag & 1<<i) && bam_flag2char_table[i])
269 kputc(bam_flag2char_table[i], &str);
272 if (c->tid < 0) kputs("*\t", &str);
273 else ksprintf(&str, "%s\t", header->target_name[c->tid]);
274 ksprintf(&str, "%d\t%d\t", c->pos + 1, c->qual);
275 if (c->n_cigar == 0) kputc('*', &str);
277 for (i = 0; i < c->n_cigar; ++i)
278 ksprintf(&str, "%d%c", bam1_cigar(b)[i]>>BAM_CIGAR_SHIFT, "MIDNSHP"[bam1_cigar(b)[i]&BAM_CIGAR_MASK]);
281 if (c->mtid < 0) kputs("*\t", &str);
282 else if (c->mtid == c->tid) kputs("=\t", &str);
283 else ksprintf(&str, "%s\t", header->target_name[c->mtid]);
284 ksprintf(&str, "%d\t%d\t", c->mpos + 1, c->isize);
286 for (i = 0; i < c->l_qseq; ++i) kputc(bam_nt16_rev_table[bam1_seqi(s, i)], &str);
288 if (t[0] == 0xff) kputc('*', &str);
289 else for (i = 0; i < c->l_qseq; ++i) kputc(t[i] + 33, &str);
290 } else ksprintf(&str, "*\t*");
292 while (s < b->data + b->data_len) {
293 uint8_t type, key[2];
294 key[0] = s[0]; key[1] = s[1];
295 s += 2; type = *s; ++s;
296 ksprintf(&str, "\t%c%c:", key[0], key[1]);
297 if (type == 'A') { ksprintf(&str, "A:%c", *s); ++s; }
298 else if (type == 'C') { ksprintf(&str, "i:%u", *s); ++s; }
299 else if (type == 'c') { ksprintf(&str, "i:%d", *s); ++s; }
300 else if (type == 'S') { ksprintf(&str, "i:%u", *(uint16_t*)s); s += 2; }
301 else if (type == 's') { ksprintf(&str, "i:%d", *(int16_t*)s); s += 2; }
302 else if (type == 'I') { ksprintf(&str, "i:%u", *(uint32_t*)s); s += 4; }
303 else if (type == 'i') { ksprintf(&str, "i:%d", *(int32_t*)s); s += 4; }
304 else if (type == 'f') { ksprintf(&str, "f:%g", *(float*)s); s += 4; }
305 else if (type == 'd') { ksprintf(&str, "d:%lg", *(double*)s); s += 8; }
306 else if (type == 'Z' || type == 'H') { ksprintf(&str, "%c:", type); while (*s) kputc(*s++, &str); ++s; }
311 char *bam_format1(const bam_header_t *header, const bam1_t *b)
313 return bam_format1_core(header, b, BAM_OFDEC);
316 void bam_view1(const bam_header_t *header, const bam1_t *b)
318 char *s = bam_format1(header, b);