From 18ffb7f0815a9311739d620b5f9227c2460ab0be Mon Sep 17 00:00:00 2001 From: barnett Date: Mon, 11 Jan 2010 15:11:15 +0000 Subject: [PATCH] Fixed fread() related compiler warnings. Fixed std types [u]intX_t errors (used, but not defined in BamAux.h). Added Aaron's stdin/stdout read/write feature. git-svn-id: svn+ssh://gene.bc.edu/home/subversion/Derek/BamTools/trunk@38 9efb377e-2e27-44b9-b91a-ec4abb80ed8b --- BGZF.cpp | 37 ++++++++++++++++++++++++++----------- BGZF.h | 32 +++++++++++++++++--------------- BamAux.h | 19 ++++++++++++++++++- BamReader.cpp | 22 ++++++++++++---------- 4 files changed, 73 insertions(+), 37 deletions(-) diff --git a/BGZF.cpp b/BGZF.cpp index ea2da8d..225ddf0 100644 --- a/BGZF.cpp +++ b/BGZF.cpp @@ -3,7 +3,7 @@ // Marth Lab, Department of Biology, Boston College // All rights reserved. // --------------------------------------------------------------------------- -// Last modified: 8 December 2009 (DB) +// Last modified: 11 January 2010 (DB) // --------------------------------------------------------------------------- // BGZF routines were adapted from the bgzf.c code developed at the Broad // Institute. @@ -40,8 +40,8 @@ BgzfData::BgzfData(void) // destructor BgzfData::~BgzfData(void) { - if(CompressedBlock) delete [] CompressedBlock; - if(UncompressedBlock) delete [] UncompressedBlock; + if(CompressedBlock) { delete[] CompressedBlock; } + if(UncompressedBlock) { delete[] UncompressedBlock; } } // closes BGZF file @@ -52,7 +52,7 @@ void BgzfData::Close(void) { IsOpen = false; // flush the current BGZF block - if ( IsWriteOnly ) { FlushBlock(); } + if (IsWriteOnly) { FlushBlock(); } // write an empty block (as EOF marker) int blockLength = DeflateBlock(); @@ -68,8 +68,6 @@ int BgzfData::DeflateBlock(void) { // initialize the gzip header char* buffer = CompressedBlock; - unsigned int bufferSize = CompressedBlockSize; - memset(buffer, 0, 18); buffer[0] = GZIP_ID1; buffer[1] = (char)GZIP_ID2; @@ -84,9 +82,11 @@ int BgzfData::DeflateBlock(void) { // loop to retry for blocks that do not compress enough int inputLength = BlockOffset; int compressedLength = 0; + unsigned int bufferSize = CompressedBlockSize; while(true) { - + + // initialize zstream values z_stream zs; zs.zalloc = NULL; zs.zfree = NULL; @@ -94,7 +94,7 @@ int BgzfData::DeflateBlock(void) { zs.avail_in = inputLength; zs.next_out = (Bytef*)&buffer[BLOCK_HEADER_LENGTH]; zs.avail_out = bufferSize - BLOCK_HEADER_LENGTH - BLOCK_FOOTER_LENGTH; - + // initialize the zlib compression algorithm if(deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, GZIP_WINDOW_BITS, Z_DEFAULT_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) { printf("ERROR: zlib deflate initialization failed.\n"); @@ -176,8 +176,8 @@ void BgzfData::FlushBlock(void) { if(numBytesWritten != blockLength) { printf("ERROR: Expected to write %u bytes during flushing, but wrote %u bytes.\n", blockLength, numBytesWritten); exit(1); - } - + } + BlockAddress += blockLength; } } @@ -218,6 +218,7 @@ int BgzfData::InflateBlock(const int& blockLength) { void BgzfData::Open(const string& filename, const char* mode) { + // determine open mode if ( strcmp(mode, "rb") == 0 ) { IsWriteOnly = false; } else if ( strcmp(mode, "wb") == 0) { @@ -227,7 +228,21 @@ void BgzfData::Open(const string& filename, const char* mode) { exit(1); } - Stream = fopen(filename.c_str(), mode); + // open Stream to read to/write from file, stdin, or stdout + // stdin/stdout option contributed by Aaron Quinlan (2010-Jan-03) + if ( (filename != "stdin") && (filename != "stdout") ) { + // read/wrtie BGZF data to/from a file + Stream = fopen(filename.c_str(), mode); + } + else if ( (filename == "stdin") && (strcmp(mode, "rb") == 0 ) ) { + // read BGZF data from stdin + Stream = freopen(NULL, mode, stdin); + } + else if ( (filename == "stdout") && (strcmp(mode, "wb") == 0) ) { + // write BGZF data to stdout + Stream = freopen(NULL, mode, stdout); + } + if(!Stream) { printf("ERROR: Unable to open the BAM file %s\n", filename.c_str() ); exit(1); diff --git a/BGZF.h b/BGZF.h index 2896542..344b070 100644 --- a/BGZF.h +++ b/BGZF.h @@ -3,7 +3,7 @@ // Marth Lab, Department of Biology, Boston College // All rights reserved. // --------------------------------------------------------------------------- -// Last modified: 8 December 2009 (DB) +// Last modified: 11 January 2010 (DB) // --------------------------------------------------------------------------- // BGZF routines were adapted from the bgzf.c code developed at the Broad // Institute. @@ -26,18 +26,21 @@ #include "zlib.h" // Platform-specific type definitions -#ifdef _MSC_VER - typedef char int8_t; - typedef unsigned char uint8_t; - typedef short int16_t; - typedef unsigned short uint16_t; - typedef int int32_t; - typedef unsigned int uint32_t; - typedef long long int64_t; - typedef unsigned long long uint64_t; -#else - #include -#endif +#ifndef BAMTOOLS_TYPES +#define BAMTOOLS_TYPES + #ifdef _MSC_VER + typedef char int8_t; + typedef unsigned char uint8_t; + typedef short int16_t; + typedef unsigned short uint16_t; + typedef int int32_t; + typedef unsigned int uint32_t; + typedef long long int64_t; + typedef unsigned long long uint64_t; + #else + #include + #endif +#endif // BAMTOOLS_TYPES namespace BamTools { @@ -117,7 +120,6 @@ struct BgzfData { inline bool BgzfData::CheckBlockHeader(char* header) { - return (header[0] == GZIP_ID1 && header[1] == (char)GZIP_ID2 && header[2] == Z_DEFLATED && @@ -171,7 +173,7 @@ unsigned int BgzfData::UnpackUnsignedInt(char* buffer) { // unpacks a buffer into an unsigned short inline unsigned short BgzfData::UnpackUnsignedShort(char* buffer) { - union { unsigned short value; unsigned char valueBuffer[sizeof(unsigned short)];} un; + union { unsigned short value; unsigned char valueBuffer[sizeof(unsigned short)]; } un; un.value = 0; un.valueBuffer[0] = buffer[0]; un.valueBuffer[1] = buffer[1]; diff --git a/BamAux.h b/BamAux.h index eec5556..d20454c 100644 --- a/BamAux.h +++ b/BamAux.h @@ -3,7 +3,7 @@ // Marth Lab, Department of Biology, Boston College // All rights reserved. // --------------------------------------------------------------------------- -// Last modified: 8 December 2009 (DB) +// Last modified: 11 December 2009 (DB) // --------------------------------------------------------------------------- // Provides the basic constants, data structures, etc. for using BAM files // *************************************************************************** @@ -22,6 +22,23 @@ #include #include +// Platform-specific type definitions +#ifndef BAMTOOLS_TYPES +#define BAMTOOLS_TYPES + #ifdef _MSC_VER + typedef char int8_t; + typedef unsigned char uint8_t; + typedef short int16_t; + typedef unsigned short uint16_t; + typedef int int32_t; + typedef unsigned int uint32_t; + typedef long long int64_t; + typedef unsigned long long uint64_t; + #else + #include + #endif +#endif // BAMTOOLS_TYPES + namespace BamTools { // BAM constants diff --git a/BamReader.cpp b/BamReader.cpp index 560b567..5dd65f2 100644 --- a/BamReader.cpp +++ b/BamReader.cpp @@ -3,7 +3,7 @@ // Marth Lab, Department of Biology, Boston College // All rights reserved. // --------------------------------------------------------------------------- -// Last modified: 8 December 2009 (DB) +// Last modified: 11 January 2010(DB) // --------------------------------------------------------------------------- // Uses BGZF routines were adapted from the bgzf.c code developed at the Broad // Institute. @@ -586,9 +586,11 @@ bool BamReader::BamReaderPrivate::LoadIndex(void) { return false; } + size_t elementsRead = 0; + // see if index is valid BAM index char magic[4]; - fread(magic, 1, 4, indexStream); + elementsRead = fread(magic, 1, 4, indexStream); if (strncmp(magic, "BAI\1", 4)) { printf("Problem with index file - invalid format.\n"); fclose(indexStream); @@ -597,7 +599,7 @@ bool BamReader::BamReaderPrivate::LoadIndex(void) { // get number of reference sequences uint32_t numRefSeqs; - fread(&numRefSeqs, 4, 1, indexStream); + elementsRead = fread(&numRefSeqs, 4, 1, indexStream); // intialize space for BamIndex data structure Index.reserve(numRefSeqs); @@ -607,7 +609,7 @@ bool BamReader::BamReaderPrivate::LoadIndex(void) { // get number of bins for this reference sequence int32_t numBins; - fread(&numBins, 4, 1, indexStream); + elementsRead = fread(&numBins, 4, 1, indexStream); if (numBins > 0) { RefData& refEntry = References[i]; @@ -622,11 +624,11 @@ bool BamReader::BamReaderPrivate::LoadIndex(void) { // get binID uint32_t binID; - fread(&binID, 4, 1, indexStream); + elementsRead = fread(&binID, 4, 1, indexStream); // get number of regionChunks in this bin uint32_t numChunks; - fread(&numChunks, 4, 1, indexStream); + elementsRead = fread(&numChunks, 4, 1, indexStream); // intialize ChunkVector ChunkVector regionChunks; @@ -638,8 +640,8 @@ bool BamReader::BamReaderPrivate::LoadIndex(void) { // get chunk boundaries (left, right) uint64_t left; uint64_t right; - fread(&left, 8, 1, indexStream); - fread(&right, 8, 1, indexStream); + elementsRead = fread(&left, 8, 1, indexStream); + elementsRead = fread(&right, 8, 1, indexStream); // save ChunkPair regionChunks.push_back( Chunk(left, right) ); @@ -656,7 +658,7 @@ bool BamReader::BamReaderPrivate::LoadIndex(void) { // get number of linear offsets int32_t numLinearOffsets; - fread(&numLinearOffsets, 4, 1, indexStream); + elementsRead = fread(&numLinearOffsets, 4, 1, indexStream); // intialize LinearOffsetVector LinearOffsetVector offsets; @@ -666,7 +668,7 @@ bool BamReader::BamReaderPrivate::LoadIndex(void) { uint64_t linearOffset; for (int j = 0; j < numLinearOffsets; ++j) { // read a linear offset & store - fread(&linearOffset, 8, 1, indexStream); + elementsRead = fread(&linearOffset, 8, 1, indexStream); offsets.push_back(linearOffset); } -- 2.39.5