]> git.donarmstrong.com Git - bamtools.git/blobdiff - BGZF.cpp
added warning for duplicate @RG tag in header
[bamtools.git] / BGZF.cpp
index 651fe81f4a18d4e3691336dc428bf33e046e1636..225ddf08cb367b546e12a171cd7025f42b37bac8 100644 (file)
--- a/BGZF.cpp
+++ b/BGZF.cpp
@@ -3,7 +3,7 @@
 // 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
@@ -40,19 +40,24 @@ BgzfData::BgzfData(void)
 \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
 void BgzfData::Close(void) {\r
 \r
+       // skip if file not open, otherwise set flag\r
     if (!IsOpen) { return; }\r
     IsOpen = false;\r
 \r
-    // flush the BGZF block\r
-    if ( IsWriteOnly ) { FlushBlock(); }\r
+    // flush the current BGZF block\r
+    if (IsWriteOnly) { FlushBlock(); }\r
 \r
+       // write an empty block (as EOF marker)\r
+       int blockLength = DeflateBlock();\r
+       fwrite(CompressedBlock, 1, blockLength, Stream);\r
+       \r
     // flush and close\r
     fflush(Stream);\r
     fclose(Stream);\r
@@ -63,8 +68,6 @@ int BgzfData::DeflateBlock(void) {
 \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
@@ -79,9 +82,11 @@ int BgzfData::DeflateBlock(void) {
     // 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
@@ -89,7 +94,7 @@ int BgzfData::DeflateBlock(void) {
         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
@@ -171,8 +176,8 @@ void BgzfData::FlushBlock(void) {
         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
@@ -213,6 +218,7 @@ int BgzfData::InflateBlock(const int& blockLength) {
 \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
@@ -222,7 +228,21 @@ void BgzfData::Open(const string& filename, const char* mode) {
         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