30 #include <zlib/zlib.h>
51 const uint32 truncate_read_len,
55 fp =
gzopen(read_file_name,
"rb");
59 log_error(stderr,
"unable to open BAM file %s\n", read_file_name);
66 bool ReadDataFile_BAM::readData(
void *output,
unsigned int len)
70 ret =
gzread(fp, output, len);
88 log_error(stderr,
"error processing BAM file: zlib error %d (%s)\n", err, msg);
98 #define GZREAD(field) \
99 if (readData(&(field), sizeof(field)) == false) { \
105 #define GZFWD(bytes) \
106 gzseek(fp, (bytes), SEEK_CUR)
109 #define GZSKIP(field) \
110 gzseek(fp, sizeof(field), SEEK_CUR)
115 static const bool error =
false;
129 if (header.
magic[0] !=
'B' ||
130 header.
magic[1] !=
'A' ||
131 header.
magic[2] !=
'M' ||
132 header.
magic[3] !=
'\1')
134 log_error(stderr,
"error parsing BAM file (invalid magic)\n");
145 for(c = 0; c < header.
n_ref; c++)
158 inline unsigned char decode_BAM_bp(
uint8 bp)
160 static const char table[] =
"=ACMGRSVTWYHKDBN";
175 static const int error = -1;
209 int encoded_read_len;
226 read_block_start =
gztell(fp);
236 read_flags = align.
flag_nc >> 16;
256 data.read_name = (
char *) malloc(read_name_len + 1);
257 data.read_name[read_name_len] = 0;
259 if (
gzread(fp, data.read_name, read_name_len) != read_name_len)
261 log_error(stderr,
"error processing BAM file (could not fetch read name)\n");
271 encoded_read_len = (align.
l_seq + 1) / 2;
272 data.encoded_read = (
uint8 *) malloc((align.
l_seq + 1) / 2);
274 if (
gzread(fp, data.encoded_read, encoded_read_len) != encoded_read_len)
276 log_error(stderr,
"error processing BAM file (could not fetch sequence data)\n");
285 log_error(stderr,
"error processing BAM file (could not fetch quality data)\n");
295 data.decoded_read = (
uint8 *) malloc(align.
l_seq + 1);
296 data.decoded_read[align.
l_seq] = 0;
299 for(c = 0; c < align.
l_seq; c++)
301 data.decoded_read[c] = decode_BAM_bp(stream[c]);