4 #include "bam_endian.h"
9 /**************************
10 * CIGAR related routines *
11 **************************/
13 int bam_segreg(int32_t pos, const bam1_core_t *c, const uint32_t *cigar, bam_segreg_t *reg)
16 int32_t x = c->pos, y = 0;
18 for (k = 0; k < c->n_cigar; ++k) {
19 int op = cigar[k] & BAM_CIGAR_MASK; // operation
20 int l = cigar[k] >> BAM_CIGAR_SHIFT; // length
21 if (state == 0 && (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CINS) && x + l > pos) {
22 reg->tbeg = x; reg->qbeg = y; reg->cbeg = k;
25 if (op == BAM_CMATCH) { x += l; y += l; }
26 else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
27 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
28 if (state == 1 && (op == BAM_CSOFT_CLIP || op == BAM_CHARD_CLIP || op == BAM_CREF_SKIP || k == c->n_cigar - 1)) {
29 reg->tend = x; reg->qend = y; reg->cend = k;
35 uint32_t bam_calend(const bam1_core_t *c, const uint32_t *cigar)
39 for (k = 0; k < c->n_cigar; ++k) {
40 int op = cigar[k] & BAM_CIGAR_MASK;
41 if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP)
42 end += cigar[k] >> BAM_CIGAR_SHIFT;
47 int32_t bam_cigar2qlen(const bam1_core_t *c, const uint32_t *cigar)
51 for (k = 0; k < c->n_cigar; ++k) {
52 int op = cigar[k] & BAM_CIGAR_MASK;
53 if (op == BAM_CMATCH || op == BAM_CINS || op == BAM_CSOFT_CLIP)
54 l += cigar[k] >> BAM_CIGAR_SHIFT;
63 bam_header_t *bam_header_init()
65 bam_is_be = bam_is_big_endian();
66 return (bam_header_t*)calloc(1, sizeof(bam_header_t));
69 void bam_header_destroy(bam_header_t *header)
72 extern void bam_destroy_header_hash(bam_header_t *header);
73 if (header == 0) return;
74 if (header->target_name) {
75 for (i = 0; i < header->n_targets; ++i)
76 free(header->target_name[i]);
77 free(header->target_name);
78 free(header->target_len);
82 if (header->rg2lib) bam_strmap_destroy(header->rg2lib);
83 bam_destroy_header_hash(header);
88 bam_header_t *bam_header_read(bamFile fp)
94 if (bam_read(fp, buf, 4) != 4) return 0;
95 if (strncmp(buf, "BAM\001", 4)) {
96 fprintf(stderr, "[bam_header_read] wrong header\n");
99 header = bam_header_init();
100 // read plain text and the number of reference sequences
101 bam_read(fp, &header->l_text, 4);
102 if (bam_is_be) bam_swap_endian_4p(&header->l_text);
103 header->text = (char*)calloc(header->l_text + 1, 1);
104 bam_read(fp, header->text, header->l_text);
105 bam_read(fp, &header->n_targets, 4);
106 if (bam_is_be) bam_swap_endian_4p(&header->n_targets);
107 // read reference sequence names and lengths
108 header->target_name = (char**)calloc(header->n_targets, sizeof(char*));
109 header->target_len = (uint32_t*)calloc(header->n_targets, 4);
110 for (i = 0; i != header->n_targets; ++i) {
111 bam_read(fp, &name_len, 4);
112 if (bam_is_be) bam_swap_endian_4p(&name_len);
113 header->target_name[i] = (char*)calloc(name_len, 1);
114 bam_read(fp, header->target_name[i], name_len);
115 bam_read(fp, &header->target_len[i], 4);
116 if (bam_is_be) bam_swap_endian_4p(&header->target_len[i]);
121 int bam_header_write(bamFile fp, const bam_header_t *header)
124 int32_t i, name_len, x;
126 strncpy(buf, "BAM\001", 4);
127 bam_write(fp, buf, 4);
128 // write plain text and the number of reference sequences
130 x = bam_swap_endian_4(header->l_text);
131 bam_write(fp, &x, 4);
132 if (header->l_text) bam_write(fp, header->text, header->l_text);
133 x = bam_swap_endian_4(header->n_targets);
134 bam_write(fp, &x, 4);
136 bam_write(fp, &header->l_text, 4);
137 if (header->l_text) bam_write(fp, header->text, header->l_text);
138 bam_write(fp, &header->n_targets, 4);
140 // write sequence names and lengths
141 for (i = 0; i != header->n_targets; ++i) {
142 char *p = header->target_name[i];
143 name_len = strlen(p) + 1;
145 x = bam_swap_endian_4(name_len);
146 bam_write(fp, &x, 4);
147 } else bam_write(fp, &name_len, 4);
148 bam_write(fp, p, name_len);
150 x = bam_swap_endian_4(header->target_len[i]);
151 bam_write(fp, &x, 4);
152 } else bam_write(fp, &header->target_len[i], 4);
157 static void swap_endian_data(const bam1_core_t *c, int data_len, uint8_t *data)
160 uint32_t i, *cigar = (uint32_t*)(data + c->l_qname);
161 s = data + c->n_cigar*4 + c->l_qname + c->l_qseq + (c->l_qseq + 1)/2;
162 for (i = 0; i < c->n_cigar; ++i) bam_swap_endian_4p(&cigar[i]);
163 while (s < data + data_len) {
166 type = toupper(*s); ++s; // skip type
167 if (type == 'C' || type == 'A') ++s;
168 else if (type == 'S') { bam_swap_endian_2p(s); s += 2; }
169 else if (type == 'I' || type == 'F') { bam_swap_endian_4p(s); s += 4; }
170 else if (type == 'D') { bam_swap_endian_8p(s); s += 8; }
171 else if (type == 'Z' || type == 'H') { while (*s) ++s; ++s; }
175 int bam_read1(bamFile fp, bam1_t *b)
177 bam1_core_t *c = &b->core;
178 int32_t block_len, ret, i;
181 assert(BAM_CORE_SIZE == 32);
182 if ((ret = bam_read(fp, &block_len, 4)) != 4) {
183 if (ret == 0) return -1; // normal end-of-file
184 else return -2; // truncated
186 if (bam_read(fp, x, BAM_CORE_SIZE) != BAM_CORE_SIZE) return -3;
188 bam_swap_endian_4p(&block_len);
189 for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i);
191 c->tid = x[0]; c->pos = x[1];
192 c->bin = x[2]>>16; c->qual = x[2]>>8&0xff; c->l_qname = x[2]&0xff;
193 c->flag = x[3]>>16; c->n_cigar = x[3]&0xffff;
195 c->mtid = x[5]; c->mpos = x[6]; c->isize = x[7];
196 b->data_len = block_len - BAM_CORE_SIZE;
197 if (b->m_data < b->data_len) {
198 b->m_data = b->data_len;
199 kroundup32(b->m_data);
200 b->data = (uint8_t*)realloc(b->data, b->m_data);
202 if (bam_read(fp, b->data, b->data_len) != b->data_len) return -4;
203 b->l_aux = b->data_len - c->n_cigar * 4 - c->l_qname - c->l_qseq - (c->l_qseq+1)/2;
204 if (bam_is_be) swap_endian_data(c, b->data_len, b->data);
205 return 4 + block_len;
208 inline int bam_write1_core(bamFile fp, const bam1_core_t *c, int data_len, uint8_t *data)
210 uint32_t x[8], block_len = data_len + BAM_CORE_SIZE, y;
212 assert(BAM_CORE_SIZE == 32);
215 x[2] = (uint32_t)c->bin<<16 | c->qual<<8 | c->l_qname;
216 x[3] = (uint32_t)c->flag<<16 | c->n_cigar;
222 for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i);
224 bam_write(fp, bam_swap_endian_4p(&y), 4);
225 swap_endian_data(c, data_len, data);
226 } else bam_write(fp, &block_len, 4);
227 bam_write(fp, x, BAM_CORE_SIZE);
228 bam_write(fp, data, data_len);
229 if (bam_is_be) swap_endian_data(c, data_len, data);
230 return 4 + block_len;
233 int bam_write1(bamFile fp, const bam1_t *b)
235 return bam_write1_core(fp, &b->core, b->data_len, b->data);
238 char *bam_format1(const bam_header_t *header, const bam1_t *b)
240 uint8_t *s = bam1_seq(b), *t = bam1_qual(b);
242 const bam1_core_t *c = &b->core;
244 str.l = str.m = 0; str.s = 0;
246 ksprintf(&str, "%s\t%d\t", bam1_qname(b), c->flag);
247 if (c->tid < 0) kputs("*\t", &str);
248 else ksprintf(&str, "%s\t", header->target_name[c->tid]);
249 ksprintf(&str, "%d\t%d\t", c->pos + 1, c->qual);
250 if (c->n_cigar == 0) kputc('*', &str);
252 for (i = 0; i < c->n_cigar; ++i)
253 ksprintf(&str, "%d%c", bam1_cigar(b)[i]>>BAM_CIGAR_SHIFT, "MIDNSHP"[bam1_cigar(b)[i]&BAM_CIGAR_MASK]);
256 if (c->mtid < 0) kputs("*\t", &str);
257 else if (c->mtid == c->tid) kputs("=\t", &str);
258 else ksprintf(&str, "%s\t", header->target_name[c->mtid]);
259 ksprintf(&str, "%d\t%d\t", c->mpos + 1, c->isize);
260 for (i = 0; i < c->l_qseq; ++i) kputc(bam_nt16_rev_table[bam1_seqi(s, i)], &str);
262 if (t[0] == 0xff) kputc('*', &str);
263 else for (i = 0; i < c->l_qseq; ++i) kputc(t[i] + 33, &str);
265 while (s < b->data + b->data_len) {
266 uint8_t type, key[2];
267 key[0] = s[0]; key[1] = s[1];
268 s += 2; type = *s; ++s;
269 ksprintf(&str, "\t%c%c:", key[0], key[1]);
270 if (type == 'A') { ksprintf(&str, "A:%c", *s); ++s; }
271 else if (type == 'C') { ksprintf(&str, "i:%u", *s); ++s; }
272 else if (type == 'c') { ksprintf(&str, "i:%d", *s); ++s; }
273 else if (type == 'S') { ksprintf(&str, "i:%u", *(uint16_t*)s); s += 2; }
274 else if (type == 's') { ksprintf(&str, "i:%d", *(int16_t*)s); s += 2; }
275 else if (type == 'I') { ksprintf(&str, "i:%u", *(uint32_t*)s); s += 4; }
276 else if (type == 'i') { ksprintf(&str, "i:%d", *(int32_t*)s); s += 4; }
277 else if (type == 'f') { ksprintf(&str, "f:%g", *(float*)s); s += 4; }
278 else if (type == 'd') { ksprintf(&str, "d:%lg", *(double*)s); s += 8; }
279 else if (type == 'Z' || type == 'H') { ksprintf(&str, "%c:", type); while (*s) kputc(*s++, &str); ++s; }
284 void bam_view1(const bam_header_t *header, const bam1_t *b)
286 char *s = bam_format1(header, b);