]> git.donarmstrong.com Git - samtools.git/blob - bam.c
Suppress bgzf_check_EOF() messages when reading from a pipe, as there is
[samtools.git] / bam.c
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <errno.h>
4 #include <assert.h>
5 #include "bam.h"
6 #include "bam_endian.h"
7 #include "kstring.h"
8
9 int bam_is_be = 0;
10 char *bam_flag2char_table = "pPuUrR12sfd\0\0\0\0\0";
11
12 /**************************
13  * CIGAR related routines *
14  **************************/
15
16 int bam_segreg(int32_t pos, const bam1_core_t *c, const uint32_t *cigar, bam_segreg_t *reg)
17 {
18         unsigned k;
19         int32_t x = c->pos, y = 0;
20         int state = 0;
21         for (k = 0; k < c->n_cigar; ++k) {
22                 int op = cigar[k] & BAM_CIGAR_MASK; // operation
23                 int l = cigar[k] >> BAM_CIGAR_SHIFT; // length
24                 if (state == 0 && (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CINS) && x + l > pos) {
25                         reg->tbeg = x; reg->qbeg = y; reg->cbeg = k;
26                         state = 1;
27                 }
28                 if (op == BAM_CMATCH) { x += l; y += l; }
29                 else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
30                 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
31                 if (state == 1 && (op == BAM_CSOFT_CLIP || op == BAM_CHARD_CLIP || op == BAM_CREF_SKIP || k == c->n_cigar - 1)) {
32                         reg->tend = x; reg->qend = y; reg->cend = k;
33                 }
34         }
35         return state? 0 : -1;
36 }
37
38 uint32_t bam_calend(const bam1_core_t *c, const uint32_t *cigar)
39 {
40         uint32_t k, end;
41         end = c->pos;
42         for (k = 0; k < c->n_cigar; ++k) {
43                 int op = cigar[k] & BAM_CIGAR_MASK;
44                 if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP)
45                         end += cigar[k] >> BAM_CIGAR_SHIFT;
46         }
47         return end;
48 }
49
50 int32_t bam_cigar2qlen(const bam1_core_t *c, const uint32_t *cigar)
51 {
52         uint32_t k;
53         int32_t l = 0;
54         for (k = 0; k < c->n_cigar; ++k) {
55                 int op = cigar[k] & BAM_CIGAR_MASK;
56                 if (op == BAM_CMATCH || op == BAM_CINS || op == BAM_CSOFT_CLIP)
57                         l += cigar[k] >> BAM_CIGAR_SHIFT;
58         }
59         return l;
60 }
61
62 /********************
63  * BAM I/O routines *
64  ********************/
65
66 bam_header_t *bam_header_init()
67 {
68         bam_is_be = bam_is_big_endian();
69         return (bam_header_t*)calloc(1, sizeof(bam_header_t));
70 }
71
72 void bam_header_destroy(bam_header_t *header)
73 {
74         int32_t i;
75         extern void bam_destroy_header_hash(bam_header_t *header);
76         if (header == 0) return;
77         if (header->target_name) {
78                 for (i = 0; i < header->n_targets; ++i)
79                         free(header->target_name[i]);
80                 free(header->target_name);
81                 free(header->target_len);
82         }
83         free(header->text);
84 #ifndef BAM_NO_HASH
85         if (header->rg2lib) bam_strmap_destroy(header->rg2lib);
86         bam_destroy_header_hash(header);
87 #endif
88         free(header);
89 }
90
91 bam_header_t *bam_header_read(bamFile fp)
92 {
93         bam_header_t *header;
94         char buf[4];
95         int32_t i = 1, name_len;
96         // check EOF
97         i = bgzf_check_EOF(fp);
98         if (i < 0) {
99                 // If the file is a pipe, checking the EOF marker will *always* fail
100                 // with ESPIPE.  Suppress the error message in this case.
101                 if (errno != ESPIPE) perror("[bam_header_read] bgzf_check_EOF");
102         }
103         else if (i == 0) fprintf(stderr, "[bam_header_read] EOF marker is absent.\n");
104         // read "BAM1"
105         if (bam_read(fp, buf, 4) != 4) return 0;
106         if (strncmp(buf, "BAM\001", 4)) {
107                 fprintf(stderr, "[bam_header_read] wrong header\n");
108                 return 0;
109         }
110         header = bam_header_init();
111         // read plain text and the number of reference sequences
112         bam_read(fp, &header->l_text, 4);
113         if (bam_is_be) bam_swap_endian_4p(&header->l_text);
114         header->text = (char*)calloc(header->l_text + 1, 1);
115         bam_read(fp, header->text, header->l_text);
116         bam_read(fp, &header->n_targets, 4);
117         if (bam_is_be) bam_swap_endian_4p(&header->n_targets);
118         // read reference sequence names and lengths
119         header->target_name = (char**)calloc(header->n_targets, sizeof(char*));
120         header->target_len = (uint32_t*)calloc(header->n_targets, 4);
121         for (i = 0; i != header->n_targets; ++i) {
122                 bam_read(fp, &name_len, 4);
123                 if (bam_is_be) bam_swap_endian_4p(&name_len);
124                 header->target_name[i] = (char*)calloc(name_len, 1);
125                 bam_read(fp, header->target_name[i], name_len);
126                 bam_read(fp, &header->target_len[i], 4);
127                 if (bam_is_be) bam_swap_endian_4p(&header->target_len[i]);
128         }
129         return header;
130 }
131
132 int bam_header_write(bamFile fp, const bam_header_t *header)
133 {
134         char buf[4];
135         int32_t i, name_len, x;
136         // write "BAM1"
137         strncpy(buf, "BAM\001", 4);
138         bam_write(fp, buf, 4);
139         // write plain text and the number of reference sequences
140         if (bam_is_be) {
141                 x = bam_swap_endian_4(header->l_text);
142                 bam_write(fp, &x, 4);
143                 if (header->l_text) bam_write(fp, header->text, header->l_text);
144                 x = bam_swap_endian_4(header->n_targets);
145                 bam_write(fp, &x, 4);
146         } else {
147                 bam_write(fp, &header->l_text, 4);
148                 if (header->l_text) bam_write(fp, header->text, header->l_text);
149                 bam_write(fp, &header->n_targets, 4);
150         }
151         // write sequence names and lengths
152         for (i = 0; i != header->n_targets; ++i) {
153                 char *p = header->target_name[i];
154                 name_len = strlen(p) + 1;
155                 if (bam_is_be) {
156                         x = bam_swap_endian_4(name_len);
157                         bam_write(fp, &x, 4);
158                 } else bam_write(fp, &name_len, 4);
159                 bam_write(fp, p, name_len);
160                 if (bam_is_be) {
161                         x = bam_swap_endian_4(header->target_len[i]);
162                         bam_write(fp, &x, 4);
163                 } else bam_write(fp, &header->target_len[i], 4);
164         }
165         return 0;
166 }
167
168 static void swap_endian_data(const bam1_core_t *c, int data_len, uint8_t *data)
169 {
170         uint8_t *s;
171         uint32_t i, *cigar = (uint32_t*)(data + c->l_qname);
172         s = data + c->n_cigar*4 + c->l_qname + c->l_qseq + (c->l_qseq + 1)/2;
173         for (i = 0; i < c->n_cigar; ++i) bam_swap_endian_4p(&cigar[i]);
174         while (s < data + data_len) {
175                 uint8_t type;
176                 s += 2; // skip key
177                 type = toupper(*s); ++s; // skip type
178                 if (type == 'C' || type == 'A') ++s;
179                 else if (type == 'S') { bam_swap_endian_2p(s); s += 2; }
180                 else if (type == 'I' || type == 'F') { bam_swap_endian_4p(s); s += 4; }
181                 else if (type == 'D') { bam_swap_endian_8p(s); s += 8; }
182                 else if (type == 'Z' || type == 'H') { while (*s) ++s; ++s; }
183         }
184 }
185
186 int bam_read1(bamFile fp, bam1_t *b)
187 {
188         bam1_core_t *c = &b->core;
189         int32_t block_len, ret, i;
190         uint32_t x[8];
191
192         assert(BAM_CORE_SIZE == 32);
193         if ((ret = bam_read(fp, &block_len, 4)) != 4) {
194                 if (ret == 0) return -1; // normal end-of-file
195                 else return -2; // truncated
196         }
197         if (bam_read(fp, x, BAM_CORE_SIZE) != BAM_CORE_SIZE) return -3;
198         if (bam_is_be) {
199                 bam_swap_endian_4p(&block_len);
200                 for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i);
201         }
202         c->tid = x[0]; c->pos = x[1];
203         c->bin = x[2]>>16; c->qual = x[2]>>8&0xff; c->l_qname = x[2]&0xff;
204         c->flag = x[3]>>16; c->n_cigar = x[3]&0xffff;
205         c->l_qseq = x[4];
206         c->mtid = x[5]; c->mpos = x[6]; c->isize = x[7];
207         b->data_len = block_len - BAM_CORE_SIZE;
208         if (b->m_data < b->data_len) {
209                 b->m_data = b->data_len;
210                 kroundup32(b->m_data);
211                 b->data = (uint8_t*)realloc(b->data, b->m_data);
212         }
213         if (bam_read(fp, b->data, b->data_len) != b->data_len) return -4;
214         b->l_aux = b->data_len - c->n_cigar * 4 - c->l_qname - c->l_qseq - (c->l_qseq+1)/2;
215         if (bam_is_be) swap_endian_data(c, b->data_len, b->data);
216         return 4 + block_len;
217 }
218
219 inline int bam_write1_core(bamFile fp, const bam1_core_t *c, int data_len, uint8_t *data)
220 {
221         uint32_t x[8], block_len = data_len + BAM_CORE_SIZE, y;
222         int i;
223         assert(BAM_CORE_SIZE == 32);
224         x[0] = c->tid;
225         x[1] = c->pos;
226         x[2] = (uint32_t)c->bin<<16 | c->qual<<8 | c->l_qname;
227         x[3] = (uint32_t)c->flag<<16 | c->n_cigar;
228         x[4] = c->l_qseq;
229         x[5] = c->mtid;
230         x[6] = c->mpos;
231         x[7] = c->isize;
232         if (bam_is_be) {
233                 for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i);
234                 y = block_len;
235                 bam_write(fp, bam_swap_endian_4p(&y), 4);
236                 swap_endian_data(c, data_len, data);
237         } else bam_write(fp, &block_len, 4);
238         bam_write(fp, x, BAM_CORE_SIZE);
239         bam_write(fp, data, data_len);
240         if (bam_is_be) swap_endian_data(c, data_len, data);
241         return 4 + block_len;
242 }
243
244 int bam_write1(bamFile fp, const bam1_t *b)
245 {
246         return bam_write1_core(fp, &b->core, b->data_len, b->data);
247 }
248
249 char *bam_format1_core(const bam_header_t *header, const bam1_t *b, int of)
250 {
251         uint8_t *s = bam1_seq(b), *t = bam1_qual(b);
252         int i;
253         const bam1_core_t *c = &b->core;
254         kstring_t str;
255         str.l = str.m = 0; str.s = 0;
256
257         ksprintf(&str, "%s\t", bam1_qname(b));
258         if (of == BAM_OFDEC) ksprintf(&str, "%d\t", c->flag);
259         else if (of == BAM_OFHEX) ksprintf(&str, "0x%x\t", c->flag);
260         else { // BAM_OFSTR
261                 for (i = 0; i < 16; ++i)
262                         if ((c->flag & 1<<i) && bam_flag2char_table[i])
263                                 kputc(bam_flag2char_table[i], &str);
264                 kputc('\t', &str);
265         }
266         if (c->tid < 0) kputs("*\t", &str);
267         else ksprintf(&str, "%s\t", header->target_name[c->tid]);
268         ksprintf(&str, "%d\t%d\t", c->pos + 1, c->qual);
269         if (c->n_cigar == 0) kputc('*', &str);
270         else {
271                 for (i = 0; i < c->n_cigar; ++i)
272                         ksprintf(&str, "%d%c", bam1_cigar(b)[i]>>BAM_CIGAR_SHIFT, "MIDNSHP"[bam1_cigar(b)[i]&BAM_CIGAR_MASK]);
273         }
274         kputc('\t', &str);
275         if (c->mtid < 0) kputs("*\t", &str);
276         else if (c->mtid == c->tid) kputs("=\t", &str);
277         else ksprintf(&str, "%s\t", header->target_name[c->mtid]);
278         ksprintf(&str, "%d\t%d\t", c->mpos + 1, c->isize);
279         if (c->l_qseq) {
280                 for (i = 0; i < c->l_qseq; ++i) kputc(bam_nt16_rev_table[bam1_seqi(s, i)], &str);
281                 kputc('\t', &str);
282                 if (t[0] == 0xff) kputc('*', &str);
283                 else for (i = 0; i < c->l_qseq; ++i) kputc(t[i] + 33, &str);
284         } else ksprintf(&str, "*\t*");
285         s = bam1_aux(b);
286         while (s < b->data + b->data_len) {
287                 uint8_t type, key[2];
288                 key[0] = s[0]; key[1] = s[1];
289                 s += 2; type = *s; ++s;
290                 ksprintf(&str, "\t%c%c:", key[0], key[1]);
291                 if (type == 'A') { ksprintf(&str, "A:%c", *s); ++s; }
292                 else if (type == 'C') { ksprintf(&str, "i:%u", *s); ++s; }
293                 else if (type == 'c') { ksprintf(&str, "i:%d", *s); ++s; }
294                 else if (type == 'S') { ksprintf(&str, "i:%u", *(uint16_t*)s); s += 2; }
295                 else if (type == 's') { ksprintf(&str, "i:%d", *(int16_t*)s); s += 2; }
296                 else if (type == 'I') { ksprintf(&str, "i:%u", *(uint32_t*)s); s += 4; }
297                 else if (type == 'i') { ksprintf(&str, "i:%d", *(int32_t*)s); s += 4; }
298                 else if (type == 'f') { ksprintf(&str, "f:%g", *(float*)s); s += 4; }
299                 else if (type == 'd') { ksprintf(&str, "d:%lg", *(double*)s); s += 8; }
300                 else if (type == 'Z' || type == 'H') { ksprintf(&str, "%c:", type); while (*s) kputc(*s++, &str); ++s; }
301         }
302         return str.s;
303 }
304
305 char *bam_format1(const bam_header_t *header, const bam1_t *b)
306 {
307         return bam_format1_core(header, b, BAM_OFDEC);
308 }
309
310 void bam_view1(const bam_header_t *header, const bam1_t *b)
311 {
312         char *s = bam_format1(header, b);
313         printf("%s\n", s);
314         free(s);
315 }