Debian Codegraph @

bwa-apache2/bamlite.c

raw
1 #include <stdlib.h>
2 #include <ctype.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include "bamlite.h"
7
8 #ifdef USE_MALLOC_WRAPPERS
9 # include "malloc_wrap.h"
10 #endif
11
12 /*********************
13 * from bam_endian.c *
14 *********************/
15
16 static inline int bam_is_big_endian()
17 {
18 long one= 1;
19 return !(*((char *)(&one)));
20 }
21 static inline uint16_t bam_swap_endian_2(uint16_t v)
22 {
23 return (uint16_t)(((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8));
24 }
25 static inline void *bam_swap_endian_2p(void *x)
26 {
27 *(uint16_t*)x = bam_swap_endian_2(*(uint16_t*)x);
28 return x;
29 }
30 static inline uint32_t bam_swap_endian_4(uint32_t v)
31 {
32 v = ((v & 0x0000FFFFU) << 16) | (v >> 16);
33 return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8);
34 }
35 static inline void *bam_swap_endian_4p(void *x)
36 {
37 *(uint32_t*)x = bam_swap_endian_4(*(uint32_t*)x);
38 return x;
39 }
40 static inline uint64_t bam_swap_endian_8(uint64_t v)
41 {
42 v = ((v & 0x00000000FFFFFFFFLLU) << 32) | (v >> 32);
43 v = ((v & 0x0000FFFF0000FFFFLLU) << 16) | ((v & 0xFFFF0000FFFF0000LLU) >> 16);
44 return ((v & 0x00FF00FF00FF00FFLLU) << 8) | ((v & 0xFF00FF00FF00FF00LLU) >> 8);
45 }
46 static inline void *bam_swap_endian_8p(void *x)
47 {
48 *(uint64_t*)x = bam_swap_endian_8(*(uint64_t*)x);
49 return x;
50 }
51
52 /**************
53 * from bam.c *
54 **************/
55
56 int bam_is_be;
57
58 bam_header_t *bam_header_init()
59 {
60 bam_is_be = bam_is_big_endian();
61 return (bam_header_t*)calloc(1, sizeof(bam_header_t));
62 }
63
64 void bam_header_destroy(bam_header_t *header)
65 {
66 int32_t i;
67 if (header == 0) return;
68 if (header->target_name) {
69 for (i = 0; i < header->n_targets; ++i)
70 if (header->target_name[i]) free(header->target_name[i]);
71 if (header->target_len) free(header->target_len);
72 free(header->target_name);
73 }
74 if (header->text) free(header->text);
75 free(header);
76 }
77
78 bam_header_t *bam_header_read(bamFile fp)
79 {
80 bam_header_t *header;
81 char buf[4];
82 int magic_len;
83 int32_t i = 1, name_len;
84 // read "BAM1"
85 magic_len = bam_read(fp, buf, 4);
86 if (magic_len != 4 || strncmp(buf, "BAM\001", 4) != 0) {
87 fprintf(stderr, "[bam_header_read] invalid BAM binary header (this is not a BAM file).\n");
88 return NULL;
89 }
90 header = bam_header_init();
91 // read plain text and the number of reference sequences
92 if (bam_read(fp, &header->l_text, 4) != 4) goto fail;
93 if (bam_is_be) bam_swap_endian_4p(&header->l_text);
94 header->text = (char*)calloc(header->l_text + 1, 1);
95 if (bam_read(fp, header->text, header->l_text) != header->l_text) goto fail;
96 if (bam_read(fp, &header->n_targets, 4) != 4) goto fail;
97 if (bam_is_be) bam_swap_endian_4p(&header->n_targets);
98 // read reference sequence names and lengths
99 header->target_name = (char**)calloc(header->n_targets, sizeof(char*));
100 header->target_len = (uint32_t*)calloc(header->n_targets, 4);
101 for (i = 0; i != header->n_targets; ++i) {
102 if (bam_read(fp, &name_len, 4) != 4) goto fail;
103 if (bam_is_be) bam_swap_endian_4p(&name_len);
104 header->target_name[i] = (char*)calloc(name_len, 1);
105 if (bam_read(fp, header->target_name[i], name_len) != name_len) {
106 goto fail;
107 }
108 if (bam_read(fp, &header->target_len[i], 4) != 4) goto fail;
109 if (bam_is_be) bam_swap_endian_4p(&header->target_len[i]);
110 }
111 return header;
112 fail:
113 bam_header_destroy(header);
114 return NULL;
115 }
116
117 static void swap_endian_data(const bam1_core_t *c, int data_len, uint8_t *data)
118 {
119 uint8_t *s;
120 uint32_t i, *cigar = (uint32_t*)(data + c->l_qname);
121 s = data + c->n_cigar*4 + c->l_qname + c->l_qseq + (c->l_qseq + 1)/2;
122 for (i = 0; i < c->n_cigar; ++i) bam_swap_endian_4p(&cigar[i]);
123 while (s < data + data_len) {
124 uint8_t type;
125 s += 2; // skip key
126 type = toupper(*s); ++s; // skip type
127 if (type == 'C' || type == 'A') ++s;
128 else if (type == 'S') { bam_swap_endian_2p(s); s += 2; }
129 else if (type == 'I' || type == 'F') { bam_swap_endian_4p(s); s += 4; }
130 else if (type == 'D') { bam_swap_endian_8p(s); s += 8; }
131 else if (type == 'Z' || type == 'H') { while (*s) ++s; ++s; }
132 }
133 }
134
135 int bam_read1(bamFile fp, bam1_t *b)
136 {
137 bam1_core_t *c = &b->core;
138 int32_t block_len, ret, i;
139 uint32_t x[8];
140
141 if ((ret = bam_read(fp, &block_len, 4)) != 4) {
142 if (ret == 0) return -1; // normal end-of-file
143 else return -2; // truncated
144 }
145 if (bam_read(fp, x, sizeof(bam1_core_t)) != sizeof(bam1_core_t)) return -3;
146 if (bam_is_be) {
147 bam_swap_endian_4p(&block_len);
148 for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i);
149 }
150 c->tid = x[0]; c->pos = x[1];
151 c->bin = x[2]>>16; c->qual = x[2]>>8&0xff; c->l_qname = x[2]&0xff;
152 c->flag = x[3]>>16; c->n_cigar = x[3]&0xffff;
153 c->l_qseq = x[4];
154 c->mtid = x[5]; c->mpos = x[6]; c->isize = x[7];
155 b->data_len = block_len - sizeof(bam1_core_t);
156 if (b->m_data < b->data_len) {
157 b->m_data = b->data_len;
158 kroundup32(b->m_data);
159 b->data = (uint8_t*)realloc(b->data, b->m_data);
160 }
161 if (bam_read(fp, b->data, b->data_len) != b->data_len) return -4;
162 b->l_aux = b->data_len - c->n_cigar * 4 - c->l_qname - c->l_qseq - (c->l_qseq+1)/2;
163 if (bam_is_be) swap_endian_data(c, b->data_len, b->data);
164 return 4 + block_len;
165 }
166
167
168 #ifdef USE_VERBOSE_ZLIB_WRAPPERS
169 // Versions of gzopen, gzread and gzclose that print up error messages
170
171 gzFile bamlite_gzopen(const char *fn, const char *mode) {
172 gzFile fp;
173 if (strcmp(fn, "-") == 0) {
174 fp = gzdopen(fileno((strstr(mode, "r"))? stdin : stdout), mode);
175 if (!fp) {
176 fprintf(stderr, "Couldn't open %s : %s",
177 (strstr(mode, "r"))? "stdin" : "stdout",
178 strerror(errno));
179 }
180 return fp;
181 }
182 if ((fp = gzopen(fn, mode)) == 0) {
183 fprintf(stderr, "Couldn't open %s : %s\n", fn,
184 errno ? strerror(errno) : "Out of memory");
185 }
186 return fp;
187 }
188
189 int bamlite_gzread(gzFile file, void *ptr, unsigned int len) {
190 int ret = gzread(file, ptr, len);
191
192 if (ret < 0) {
193 int errnum = 0;
194 const char *msg = gzerror(file, &errnum);
195 fprintf(stderr, "gzread error: %s\n",
196 Z_ERRNO == errnum ? strerror(errno) : msg);
197 }
198 return ret;
199 }
200
201 int bamlite_gzclose(gzFile file) {
202 int ret = gzclose(file);
203 if (Z_OK != ret) {
204 fprintf(stderr, "gzclose error: %s\n",
205 Z_ERRNO == ret ? strerror(errno) : zError(ret));
206 }
207
208 return ret;
209 }
210 #endif /* USE_VERBOSE_ZLIB_WRAPPERS */