Skip to content

Commit

Permalink
Removed more dependencies on utils.h
Browse files Browse the repository at this point in the history
bamlite.c now includes some wrappers for gzopen/gzread/gzclose that print
messages when errors occur.  They do not attempt to quit the program but
pass on the return code.  bwaseqio.c now checks the return codes from
bam_open, bam_close and bam_read1.

Code in bwt_gen.c now checks for IO errors itself instead of using the
wrappers.  A benefit of this is it can now say which file had a problem.

Removed call to err_fatal_simple in is_bwt and unnecessary inclusion of
malloc_wrap.h in ksw.h.
  • Loading branch information
Rob Davies committed May 3, 2013
1 parent 96e445d commit 5c43a1f
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 36 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ depend:
# DO NOT DELETE THIS LINE -- make depend depends on it.

QSufSort.o: QSufSort.h
bamlite.o: bamlite.h utils.h malloc_wrap.h
bamlite.o: bamlite.h malloc_wrap.h
bntseq.o: bntseq.h utils.h kseq.h malloc_wrap.h
bwa.o: bntseq.h bwa.h bwt.h ksw.h malloc_wrap.h utils.h kseq.h
bwa.o: bntseq.h bwa.h bwt.h ksw.h utils.h malloc_wrap.h kseq.h
bwamem.o: kstring.h malloc_wrap.h bwamem.h bwt.h bntseq.h bwa.h ksw.h kvec.h
bwamem.o: ksort.h utils.h kbtree.h
bwamem_pair.o: kstring.h malloc_wrap.h bwamem.h bwt.h bntseq.h bwa.h kvec.h
Expand All @@ -51,7 +51,7 @@ bwase.o: bwase.h bntseq.h bwt.h bwtaln.h utils.h kstring.h malloc_wrap.h
bwase.o: bwa.h ksw.h
bwaseqio.o: bwtaln.h bwt.h utils.h bamlite.h malloc_wrap.h kseq.h
bwt.o: utils.h bwt.h kvec.h malloc_wrap.h
bwt_gen.o: QSufSort.h utils.h malloc_wrap.h
bwt_gen.o: QSufSort.h malloc_wrap.h
bwt_lite.o: bwt_lite.h malloc_wrap.h
bwtaln.o: bwtaln.h bwt.h bwtgap.h utils.h bwa.h bntseq.h malloc_wrap.h
bwtgap.o: bwtgap.h bwt.h bwtaln.h malloc_wrap.h
Expand All @@ -66,11 +66,11 @@ bwtsw2_pair.o: utils.h bwt.h bntseq.h bwtsw2.h bwt_lite.h kstring.h
bwtsw2_pair.o: malloc_wrap.h ksw.h
example.o: bwamem.h bwt.h bntseq.h bwa.h kseq.h malloc_wrap.h
fastmap.o: bwa.h bntseq.h bwt.h bwamem.h kvec.h malloc_wrap.h utils.h kseq.h
is.o: utils.h malloc_wrap.h
is.o: malloc_wrap.h
kopen.o: malloc_wrap.h
kstring.o: kstring.h malloc_wrap.h
ksw.o: ksw.h malloc_wrap.h
main.o: utils.h
malloc_wrap.o: malloc_wrap.h
pemerge.o: ksw.h malloc_wrap.h kseq.h kstring.h bwa.h bntseq.h bwt.h utils.h
pemerge.o: ksw.h kseq.h malloc_wrap.h kstring.h bwa.h bntseq.h bwt.h utils.h
utils.o: utils.h ksort.h malloc_wrap.h kseq.h
46 changes: 46 additions & 0 deletions bamlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "bamlite.h"

#ifdef USE_MALLOC_WRAPPERS
Expand Down Expand Up @@ -162,3 +163,48 @@ int bam_read1(bamFile fp, bam1_t *b)
if (bam_is_be) swap_endian_data(c, b->data_len, b->data);
return 4 + block_len;
}


#ifdef USE_VERBOSE_ZLIB_WRAPPERS
// Versions of gzopen, gzread and gzclose that print up error messages

gzFile bamlite_gzopen(const char *fn, const char *mode) {
gzFile fp;
if (strcmp(fn, "-") == 0) {
fp = gzdopen(fileno((strstr(mode, "r"))? stdin : stdout), mode);
if (!fp) {
fprintf(stderr, "Couldn't open %s : %s",
(strstr(mode, "r"))? "stdin" : "stdout",
strerror(errno));
}
return fp;
}
if ((fp = gzopen(fn, mode)) == 0) {
fprintf(stderr, "Couldn't open %s : %s\n", fn,
errno ? strerror(errno) : "Out of memory");
}
return fp;
}

int bamlite_gzread(gzFile file, void *ptr, unsigned int len) {
int ret = gzread(file, ptr, len);

if (ret < 0) {
int errnum = 0;
const char *msg = gzerror(file, &errnum);
fprintf(stderr, "gzread error: %s\n",
Z_ERRNO == errnum ? strerror(errno) : msg);
}
return ret;
}

int bamlite_gzclose(gzFile file) {
int ret = gzclose(file);
if (Z_OK != ret) {
fprintf(stderr, "gzclose error: %s\n",
Z_ERRNO == ret ? strerror(errno) : zError(ret));
}

return ret;
}
#endif /* USE_VERBOSE_ZLIB_WRAPPERS */
25 changes: 20 additions & 5 deletions bamlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@

#include <stdint.h>
#include <zlib.h>
#include "utils.h"

#ifdef USE_MALLOC_WRAPPERS
# include "malloc_wrap.h"
#endif

#define USE_VERBOSE_ZLIB_WRAPPERS

typedef gzFile bamFile;
#define bam_open(fn, mode) xzopen(fn, mode)
#define bam_dopen(fd, mode) gzdopen(fd, mode)
#define bam_close(fp) err_gzclose(fp)
#define bam_read(fp, buf, size) err_gzread(fp, buf, size)
#ifdef USE_VERBOSE_ZLIB_WRAPPERS
/* These print error messages on failure */
# define bam_open(fn, mode) bamlite_gzopen(fn, mode)
# define bam_dopen(fd, mode) gzdopen(fd, mode)
# define bam_close(fp) bamlite_gzclose(fp)
# define bam_read(fp, buf, size) bamlite_gzread(fp, buf, size)
#else
# define bam_open(fn, mode) gzopen(fn, mode)
# define bam_dopen(fd, mode) gzdopen(fd, mode)
# define bam_close(fp) gzclose(fp)
# define bam_read(fp, buf, size) gzread(fp, buf, size)
#endif /* USE_VERBOSE_ZLIB_WRAPPERS */

typedef struct {
int32_t n_targets;
Expand Down Expand Up @@ -92,6 +101,12 @@ extern "C" {
bam_header_t *bam_header_read(bamFile fp);
int bam_read1(bamFile fp, bam1_t *b);

#ifdef USE_VERBOSE_ZLIB_WRAPPERS
gzFile bamlite_gzopen(const char *fn, const char *mode);
int bamlite_gzread(gzFile file, void *ptr, unsigned int len);
int bamlite_gzclose(gzFile file);
#endif /* USE_VERBOSE_ZLIB_WRAPPERS */

#ifdef __cplusplus
}
#endif
Expand Down
10 changes: 7 additions & 3 deletions bwaseqio.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ bwa_seqio_t *bwa_bam_open(const char *fn, int which)
bs->is_bam = 1;
bs->which = which;
bs->fp = bam_open(fn, "r");
if (0 == bs->fp) err_fatal_simple("Couldn't open bam file");
h = bam_header_read(bs->fp);
bam_header_destroy(h);
return bs;
Expand All @@ -48,8 +49,9 @@ bwa_seqio_t *bwa_seq_open(const char *fn)
void bwa_seq_close(bwa_seqio_t *bs)
{
if (bs == 0) return;
if (bs->is_bam) bam_close(bs->fp);
else {
if (bs->is_bam) {
if (0 != bam_close(bs->fp)) err_fatal_simple("Error closing bam file");
} else {
err_gzclose(bs->ks->f->f);
kseq_destroy(bs->ks);
}
Expand Down Expand Up @@ -94,11 +96,12 @@ static bwa_seq_t *bwa_read_bam(bwa_seqio_t *bs, int n_needed, int *n, int is_com
int n_seqs, l, i;
long n_trimmed = 0, n_tot = 0;
bam1_t *b;
int res;

b = bam_init1();
n_seqs = 0;
seqs = (bwa_seq_t*)calloc(n_needed, sizeof(bwa_seq_t));
while (bam_read1(bs->fp, b) >= 0) {
while ((res = bam_read1(bs->fp, b)) >= 0) {
uint8_t *s, *q;
int go = 0;
if ((bs->which & 1) && (b->core.flag & BAM_FREAD1)) go = 1;
Expand Down Expand Up @@ -130,6 +133,7 @@ static bwa_seq_t *bwa_read_bam(bwa_seqio_t *bs, int n_needed, int *n, int is_com
p->name = strdup((const char*)bam1_qname(b));
if (n_seqs == n_needed) break;
}
if (res < 0 && res != -1) err_fatal_simple("Error reading bam file");
*n = n_seqs;
if (n_seqs && trim_qual >= 1)
fprintf(stderr, "[bwa_read_seq] %.1f%% bases are trimmed.\n", 100.0f * n_trimmed/n_tot);
Expand Down
90 changes: 73 additions & 17 deletions bwt_gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#include <string.h>
#include <assert.h>
#include <stdint.h>
#include <errno.h>
#include "QSufSort.h"
#include "utils.h"

#ifdef USE_MALLOC_WRAPPERS
# include "malloc_wrap.h"
Expand Down Expand Up @@ -1448,13 +1448,29 @@ BWTInc *BWTIncConstructFromPacked(const char *inputFileName, bgint_t initialMaxB
packedFile = (FILE*)fopen(inputFileName, "rb");

if (packedFile == NULL) {
fprintf(stderr, "BWTIncConstructFromPacked() : Cannot open inputFileName!\n");
fprintf(stderr, "BWTIncConstructFromPacked() : Cannot open %s : %s\n",
inputFileName, strerror(errno));
exit(1);
}

err_fseek(packedFile, -1, SEEK_END);
packedFileLen = err_ftell(packedFile);
err_fread_noeof(&lastByteLength, sizeof(unsigned char), 1, packedFile);
if (fseek(packedFile, -1, SEEK_END) != 0) {
fprintf(stderr, "BWTIncConstructFromPacked() : Can't seek on %s : %s\n",
inputFileName, strerror(errno));
exit(1);
}
packedFileLen = ftell(packedFile);
if (packedFileLen == -1) {
fprintf(stderr, "BWTIncConstructFromPacked() : Can't ftell on %s : %s\n",
inputFileName, strerror(errno));
exit(1);
}
if (fread(&lastByteLength, sizeof(unsigned char), 1, packedFile) != 1) {
fprintf(stderr,
"BWTIncConstructFromPacked() : Can't read from %s : %s\n",
inputFileName,
ferror(packedFile)? strerror(errno) : "Unexpected end of file");
exit(1);
}
totalTextLength = TextLengthFromBytePacked(packedFileLen, BIT_PER_CHAR, lastByteLength);

bwtInc = BWTIncCreate(totalTextLength, initialMaxBuildSize, incMaxBuildSize);
Expand All @@ -1468,10 +1484,23 @@ BWTInc *BWTIncConstructFromPacked(const char *inputFileName, bgint_t initialMaxB
}
textSizeInByte = textToLoad / CHAR_PER_BYTE; // excluded the odd byte

err_fseek(packedFile, -2, SEEK_CUR);
err_fseek(packedFile, -((long)textSizeInByte), SEEK_CUR);
err_fread_noeof(bwtInc->textBuffer, sizeof(unsigned char), textSizeInByte + 1, packedFile);
err_fseek(packedFile, -((long)textSizeInByte + 1), SEEK_CUR);
if (fseek(packedFile, -((long)textSizeInByte + 2), SEEK_CUR) != 0) {
fprintf(stderr, "BWTIncConstructFromPacked() : Can't seek on %s : %s\n",
inputFileName, strerror(errno));
exit(1);
}
if (fread(bwtInc->textBuffer, sizeof(unsigned char), textSizeInByte + 1, packedFile) != textSizeInByte + 1) {
fprintf(stderr,
"BWTIncConstructFromPacked() : Can't read from %s : %s\n",
inputFileName,
ferror(packedFile)? strerror(errno) : "Unexpected end of file");
exit(1);
}
if (fseek(packedFile, -((long)textSizeInByte + 1), SEEK_CUR) != 0) {
fprintf(stderr, "BWTIncConstructFromPacked() : Can't seek on %s : %s\n",
inputFileName, strerror(errno));
exit(1);
}

ConvertBytePackedToWordPacked(bwtInc->textBuffer, bwtInc->packedText, ALPHABET_SIZE, textToLoad);
BWTIncConstruct(bwtInc, textToLoad);
Expand All @@ -1484,9 +1513,23 @@ BWTInc *BWTIncConstructFromPacked(const char *inputFileName, bgint_t initialMaxB
textToLoad = totalTextLength - processedTextLength;
}
textSizeInByte = textToLoad / CHAR_PER_BYTE;
err_fseek(packedFile, -((long)textSizeInByte), SEEK_CUR);
err_fread_noeof(bwtInc->textBuffer, sizeof(unsigned char), textSizeInByte, packedFile);
err_fseek(packedFile, -((long)textSizeInByte), SEEK_CUR);
if (fseek(packedFile, -((long)textSizeInByte), SEEK_CUR) != 0) {
fprintf(stderr, "BWTIncConstructFromPacked() : Can't seek on %s : %s\n",
inputFileName, strerror(errno));
exit(1);
}
if (fread(bwtInc->textBuffer, sizeof(unsigned char), textSizeInByte, packedFile) != textSizeInByte) {
fprintf(stderr,
"BWTIncConstructFromPacked() : Can't read from %s : %s\n",
inputFileName,
ferror(packedFile)? strerror(errno) : "Unexpected end of file");
exit(1);
}
if (fseek(packedFile, -((long)textSizeInByte), SEEK_CUR) != 0) {
fprintf(stderr, "BWTIncConstructFromPacked() : Can't seek on %s : %s\n",
inputFileName, strerror(errno));
exit(1);
}
ConvertBytePackedToWordPacked(bwtInc->textBuffer, bwtInc->packedText, ALPHABET_SIZE, textToLoad);
BWTIncConstruct(bwtInc, textToLoad);
processedTextLength += textToLoad;
Expand Down Expand Up @@ -1531,15 +1574,28 @@ void BWTSaveBwtCodeAndOcc(const BWT *bwt, const char *bwtFileName, const char *o

bwtFile = (FILE*)fopen(bwtFileName, "wb");
if (bwtFile == NULL) {
fprintf(stderr, "BWTSaveBwtCodeAndOcc(): Cannot open BWT code file!\n");
fprintf(stderr,
"BWTSaveBwtCodeAndOcc(): Cannot open %s for writing: %s\n",
bwtFileName, strerror(errno));
exit(1);
}

err_fwrite(&bwt->inverseSa0, sizeof(bgint_t), 1, bwtFile);
err_fwrite(bwt->cumulativeFreq + 1, sizeof(bgint_t), ALPHABET_SIZE, bwtFile);
bwtLength = BWTFileSizeInWord(bwt->textLength);
err_fwrite(bwt->bwtCode, sizeof(unsigned int), bwtLength, bwtFile);
err_fclose(bwtFile);

if (fwrite(&bwt->inverseSa0, sizeof(bgint_t), 1, bwtFile) != 1
|| fwrite(bwt->cumulativeFreq + 1,
sizeof(bgint_t), ALPHABET_SIZE, bwtFile) != ALPHABET_SIZE
|| fwrite(bwt->bwtCode,
sizeof(unsigned int), bwtLength, bwtFile) != bwtLength) {
fprintf(stderr, "BWTSaveBwtCodeAndOcc(): Error writing to %s : %s\n",
bwtFileName, strerror(errno));
exit(1);
}
if (fclose(bwtFile) != 0) {
fprintf(stderr, "BWTSaveBwtCodeAndOcc(): Error on closing %s : %s\n",
bwtFileName, strerror(errno));
exit(1);
}
}

void bwt_bwtgen(const char *fn_pac, const char *fn_bwt)
Expand Down
3 changes: 1 addition & 2 deletions is.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
*/

#include <stdlib.h>
#include "utils.h"

#ifdef USE_MALLOC_WRAPPERS
# include "malloc_wrap.h"
Expand Down Expand Up @@ -211,7 +210,7 @@ int is_bwt(ubyte_t *T, int n)
int *SA, i, primary = 0;
SA = (int*)calloc(n+1, sizeof(int));

if (is_sa(T, SA, n)) err_fatal_simple("is_sa failed");
if (is_sa(T, SA, n)) return -1;

for (i = 0; i <= n; ++i) {
if (SA[i] == 0) primary = i;
Expand Down
4 changes: 0 additions & 4 deletions ksw.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

#include <stdint.h>

#ifdef USE_MALLOC_WRAPPERS
# include "malloc_wrap.h"
#endif

#define KSW_XBYTE 0x10000
#define KSW_XSTOP 0x20000
#define KSW_XSUBO 0x40000
Expand Down

0 comments on commit 5c43a1f

Please sign in to comment.