// Marth Lab, Department of Biology, Boston College\r
// All rights reserved.\r
// ---------------------------------------------------------------------------\r
-// Last modified: 8 December 2009 (DB)\r
+// Last modified: 11 January 2010 (DB)\r
// ---------------------------------------------------------------------------\r
// BGZF routines were adapted from the bgzf.c code developed at the Broad\r
// Institute.\r
\r
// destructor\r
BgzfData::~BgzfData(void) {\r
- if(CompressedBlock) delete [] CompressedBlock;\r
- if(UncompressedBlock) delete [] UncompressedBlock;\r
+ if(CompressedBlock) { delete[] CompressedBlock; }\r
+ if(UncompressedBlock) { delete[] UncompressedBlock; }\r
}\r
\r
// closes BGZF file\r
IsOpen = false;\r
\r
// flush the current BGZF block\r
- if ( IsWriteOnly ) { FlushBlock(); }\r
+ if (IsWriteOnly) { FlushBlock(); }\r
\r
// write an empty block (as EOF marker)\r
int blockLength = DeflateBlock();\r
\r
// initialize the gzip header\r
char* buffer = CompressedBlock;\r
- unsigned int bufferSize = CompressedBlockSize;\r
-\r
memset(buffer, 0, 18);\r
buffer[0] = GZIP_ID1;\r
buffer[1] = (char)GZIP_ID2;\r
// loop to retry for blocks that do not compress enough\r
int inputLength = BlockOffset;\r
int compressedLength = 0;\r
+ unsigned int bufferSize = CompressedBlockSize;\r
\r
while(true) {\r
-\r
+ \r
+ // initialize zstream values\r
z_stream zs;\r
zs.zalloc = NULL;\r
zs.zfree = NULL;\r
zs.avail_in = inputLength;\r
zs.next_out = (Bytef*)&buffer[BLOCK_HEADER_LENGTH];\r
zs.avail_out = bufferSize - BLOCK_HEADER_LENGTH - BLOCK_FOOTER_LENGTH;\r
-\r
+ \r
// initialize the zlib compression algorithm\r
if(deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, GZIP_WINDOW_BITS, Z_DEFAULT_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) {\r
printf("ERROR: zlib deflate initialization failed.\n");\r
if(numBytesWritten != blockLength) {\r
printf("ERROR: Expected to write %u bytes during flushing, but wrote %u bytes.\n", blockLength, numBytesWritten);\r
exit(1);\r
- }\r
-\r
+ }\r
+ \r
BlockAddress += blockLength;\r
}\r
}\r
\r
void BgzfData::Open(const string& filename, const char* mode) {\r
\r
+ // determine open mode\r
if ( strcmp(mode, "rb") == 0 ) {\r
IsWriteOnly = false;\r
} else if ( strcmp(mode, "wb") == 0) {\r
exit(1);\r
}\r
\r
- Stream = fopen(filename.c_str(), mode);\r
+ // open Stream to read to/write from file, stdin, or stdout\r
+ // stdin/stdout option contributed by Aaron Quinlan (2010-Jan-03)\r
+ if ( (filename != "stdin") && (filename != "stdout") ) {\r
+ // read/wrtie BGZF data to/from a file\r
+ Stream = fopen(filename.c_str(), mode);\r
+ }\r
+ else if ( (filename == "stdin") && (strcmp(mode, "rb") == 0 ) ) { \r
+ // read BGZF data from stdin\r
+ Stream = freopen(NULL, mode, stdin);\r
+ }\r
+ else if ( (filename == "stdout") && (strcmp(mode, "wb") == 0) ) { \r
+ // write BGZF data to stdout\r
+ Stream = freopen(NULL, mode, stdout);\r
+ }\r
+ \r
if(!Stream) {\r
printf("ERROR: Unable to open the BAM file %s\n", filename.c_str() );\r
exit(1);\r