CompressedBlock = new char[CompressedBlockSize];\r
UncompressedBlock = new char[UncompressedBlockSize];\r
} catch( std::bad_alloc& ba ) {\r
- printf("BGZF ERROR: unable to allocate memory for our BGZF object.\n");\r
+ fprintf(stderr, "BGZF ERROR: unable to allocate memory for our BGZF object.\n");\r
exit(1);\r
}\r
}\r
\r
// initialize the zlib compression algorithm\r
if ( deflateInit2(&zs, compressionLevel, Z_DEFLATED, GZIP_WINDOW_BITS, Z_DEFAULT_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK ) {\r
- printf("BGZF ERROR: zlib deflate initialization failed.\n");\r
+ fprintf(stderr, "BGZF ERROR: zlib deflate initialization failed.\n");\r
exit(1);\r
}\r
\r
if ( status == Z_OK ) {\r
inputLength -= 1024;\r
if( inputLength < 0 ) {\r
- printf("BGZF ERROR: input reduction failed.\n");\r
+ fprintf(stderr, "BGZF ERROR: input reduction failed.\n");\r
exit(1);\r
}\r
continue;\r
}\r
\r
- printf("BGZF ERROR: zlib::deflateEnd() failed.\n");\r
+ fprintf(stderr, "BGZF ERROR: zlib::deflateEnd() failed.\n");\r
exit(1);\r
}\r
\r
// finalize the compression routine\r
if ( deflateEnd(&zs) != Z_OK ) {\r
- printf("BGZF ERROR: zlib::deflateEnd() failed.\n");\r
+ fprintf(stderr, "BGZF ERROR: zlib::deflateEnd() failed.\n");\r
exit(1);\r
}\r
\r
compressedLength = zs.total_out;\r
compressedLength += BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH;\r
if ( compressedLength > MAX_BLOCK_SIZE ) {\r
- printf("BGZF ERROR: deflate overflow.\n");\r
+ fprintf(stderr, "BGZF ERROR: deflate overflow.\n");\r
exit(1);\r
}\r
\r
int remaining = BlockOffset - inputLength;\r
if ( remaining > 0 ) {\r
if ( remaining > inputLength ) {\r
- printf("BGZF ERROR: after deflate, remainder too large.\n");\r
+ fprintf(stderr, "BGZF ERROR: after deflate, remainder too large.\n");\r
exit(1);\r
}\r
memcpy(UncompressedBlock, UncompressedBlock + inputLength, remaining);\r
int numBytesWritten = fwrite(CompressedBlock, 1, blockLength, Stream);\r
\r
if ( numBytesWritten != blockLength ) {\r
- printf("BGZF ERROR: expected to write %u bytes during flushing, but wrote %u bytes.\n", blockLength, numBytesWritten);\r
+ fprintf(stderr, "BGZF ERROR: expected to write %u bytes during flushing, but wrote %u bytes.\n", blockLength, numBytesWritten);\r
exit(1);\r
}\r
\r
\r
int status = inflateInit2(&zs, GZIP_WINDOW_BITS);\r
if ( status != Z_OK ) {\r
- printf("BGZF ERROR: could not decompress block - zlib::inflateInit() failed\n");\r
+ fprintf(stderr, "BGZF ERROR: could not decompress block - zlib::inflateInit() failed\n");\r
return -1;\r
}\r
\r
status = inflate(&zs, Z_FINISH);\r
if ( status != Z_STREAM_END ) {\r
inflateEnd(&zs);\r
- printf("BGZF ERROR: could not decompress block - zlib::inflate() failed\n");\r
+ fprintf(stderr, "BGZF ERROR: could not decompress block - zlib::inflate() failed\n");\r
return -1;\r
}\r
\r
status = inflateEnd(&zs);\r
if ( status != Z_OK ) {\r
- printf("BGZF ERROR: could not decompress block - zlib::inflateEnd() failed\n");\r
+ fprintf(stderr, "BGZF ERROR: could not decompress block - zlib::inflateEnd() failed\n");\r
return -1;\r
}\r
\r
else if ( strcmp(mode, "wb") == 0) \r
IsWriteOnly = true;\r
else {\r
- printf("BGZF ERROR: unknown file mode: %s\n", mode);\r
+ fprintf(stderr, "BGZF ERROR: unknown file mode: %s\n", mode);\r
return false; \r
}\r
\r
Stream = freopen(NULL, mode, stdout);\r
\r
if ( !Stream ) {\r
- printf("BGZF ERROR: unable to open file %s\n", filename.c_str() );\r
+ fprintf(stderr, "BGZF ERROR: unable to open file %s\n", filename.c_str() );\r
return false;\r
}\r
\r
}\r
\r
if ( count != sizeof(header) ) {\r
- printf("BGZF ERROR: read block failed - could not read block header\n");\r
+ fprintf(stderr, "BGZF ERROR: read block failed - could not read block header\n");\r
return false;\r
}\r
\r
if ( !BgzfData::CheckBlockHeader(header) ) {\r
- printf("BGZF ERROR: read block failed - invalid block header\n");\r
+ fprintf(stderr, "BGZF ERROR: read block failed - invalid block header\n");\r
return false;\r
}\r
\r
\r
count = fread(&compressedBlock[BLOCK_HEADER_LENGTH], 1, remaining, Stream);\r
if ( count != remaining ) {\r
- printf("BGZF ERROR: read block failed - could not read data from block\n");\r
+ fprintf(stderr, "BGZF ERROR: read block failed - could not read data from block\n");\r
return false;\r
}\r
\r
count = InflateBlock(blockLength);\r
if ( count < 0 ) { \r
- printf("BGZF ERROR: read block failed - could not decompress block data\n");\r
+ fprintf(stderr, "BGZF ERROR: read block failed - could not decompress block data\n");\r
return false;\r
}\r
\r
int64_t blockAddress = (position >> 16) & 0xFFFFFFFFFFFFLL;\r
\r
if ( fseek64(Stream, blockAddress, SEEK_SET) != 0 ) {\r
- printf("BGZF ERROR: unable to seek in file\n");\r
+ fprintf(stderr, "BGZF ERROR: unable to seek in file\n");\r
return false;\r
}\r
\r
case 'f':\r
case 'Z':\r
case 'H':\r
- printf("ERROR: Cannot store tag of type %c in integer destination\n", type);\r
+ fprintf(stderr, "ERROR: Cannot store tag of type %c in integer destination\n", type);\r
return false;\r
\r
// unknown tag type\r
default:\r
- printf("ERROR: Unknown tag storage class encountered: [%c]\n", type);\r
+ fprintf(stderr, "ERROR: Unknown tag storage class encountered: [%c]\n", type);\r
return false;\r
}\r
\r
// unsupported type (var-length strings)\r
case 'Z':\r
case 'H':\r
- printf("ERROR: Cannot store tag of type %c in integer destination\n", type);\r
+ fprintf(stderr, "ERROR: Cannot store tag of type %c in integer destination\n", type);\r
return false;\r
\r
// unknown tag type\r
default:\r
- printf("ERROR: Unknown tag storage class encountered: [%c]\n", type);\r
+ fprintf(stderr, "ERROR: Unknown tag storage class encountered: [%c]\n", type);\r
return false;\r
}\r
\r
\r
default: \r
// error case\r
- printf("ERROR: Unknown tag storage class encountered: [%c]\n", storageType);\r
+ fprintf(stderr, "ERROR: Unknown tag storage class encountered: [%c]\n", storageType);\r
return false;\r
}\r
\r
// if lastCoordinate greater than BAM position - file not sorted properly
else if ( lastCoordinate > bAlignment.Position ) {
- printf("BAM file not properly sorted:\n");
- printf("Alignment %s : %d > %d on reference (id = %d)", bAlignment.Name.c_str(), lastCoordinate, bAlignment.Position, bAlignment.RefID);
+ fprintf(stderr, "BAM file not properly sorted:\n");
+ fprintf(stderr, "Alignment %s : %d > %d on reference (id = %d)", bAlignment.Name.c_str(), lastCoordinate, bAlignment.Position, bAlignment.RefID);
exit(1);
}
// make sure that current file pointer is beyond lastOffset
if ( m_BGZF->Tell() <= (int64_t)lastOffset ) {
- printf("Error in BGZF offsets.\n");
+ fprintf(stderr, "Error in BGZF offsets.\n");
exit(1);
}
// open index file, abort on error
FILE* indexStream = fopen(filename.c_str(), "rb");
if( !indexStream ) {
- printf("ERROR: Unable to open the BAM index file %s for reading.\n", filename.c_str());
+ fprintf(stderr, "ERROR: Unable to open the BAM index file %s for reading.\n", filename.c_str());
return false;
}
char magic[4];
elementsRead = fread(magic, 1, 4, indexStream);
if ( strncmp(magic, "BAI\1", 4) ) {
- printf("Problem with index file - invalid format.\n");
+ fprintf(stderr, "Problem with index file - invalid format.\n");
fclose(indexStream);
return false;
}
string indexFilename = bamFilename + ".bai";
FILE* indexStream = fopen(indexFilename.c_str(), "wb");
if ( indexStream == 0 ) {
- printf("ERROR: Could not open file to save index.\n");
+ fprintf(stderr, "ERROR: Could not open file to save index.\n");
return false;
}
// open index file, abort on error
FILE* indexStream = fopen(filename.c_str(), "rb");
if( !indexStream ) {
- printf("ERROR: Unable to open the BAM index file %s for reading.\n", filename.c_str());
+ fprintf(stderr, "ERROR: Unable to open the BAM index file %s for reading.\n", filename.c_str());
return false;
}
char magic[4];
elementsRead = fread(magic, 1, 4, indexStream);
if ( strncmp(magic, "BTI\1", 4) ) {
- printf("Problem with index file - invalid format.\n");
+ fprintf(stderr, "Problem with index file - invalid format.\n");
fclose(indexStream);
return false;
}
string indexFilename = bamFilename + ".bti";
FILE* indexStream = fopen(indexFilename.c_str(), "wb");
if ( indexStream == 0 ) {
- printf("ERROR: Could not open file to save index.\n");
+ fprintf(stderr, "ERROR: Could not open file to save index.\n");
return false;
}
break; // for 'H' - hard clip, do nothing to AlignedBases, move to next op\r
\r
default:\r
- printf("ERROR: Invalid Cigar op type\n"); // shouldn't get here\r
+ fprintf(stderr, "ERROR: Invalid Cigar op type\n"); // shouldn't get here\r
exit(1);\r
}\r
}\r
break;\r
\r
default : \r
- printf("ERROR: Invalid tag value type\n"); // shouldn't get here\r
+ fprintf(stderr, "ERROR: Invalid tag value type\n"); // shouldn't get here\r
exit(1);\r
}\r
}\r
// determine possible offsets\r
vector<int64_t> offsets;\r
if ( !NewIndex->GetOffsets(Region, IsRightBoundSpecified, offsets) ) {\r
- printf("ERROR: Could not jump: unable to calculate offset for specified region.\n");\r
+ fprintf(stderr, "ERROR: Could not jump: unable to calculate offset for specified region.\n");\r
return false;\r
}\r
\r
// check to see if proper BAM header\r
char buffer[4];\r
if (mBGZF.Read(buffer, 4) != 4) {\r
- printf("Could not read header type\n");\r
+ fprintf(stderr, "Could not read header type\n");\r
exit(1);\r
}\r
\r
if (strncmp(buffer, "BAM\001", 4)) {\r
- printf("wrong header type!\n");\r
+ fprintf(stderr, "wrong header type!\n");\r
exit(1);\r
}\r
\r
\r
// else unknown\r
else {\r
- printf("ERROR: Unknown index file extension.\n");\r
+ fprintf(stderr, "ERROR: Unknown index file extension.\n");\r
return false;\r
}\r
\r
cigarOp = BAM_CPAD;\r
break;\r
default:\r
- printf("ERROR: Unknown cigar operation found: %c\n", coIter->Type);\r
+ fprintf(stderr, "ERROR: Unknown cigar operation found: %c\n", coIter->Type);\r
exit(1);\r
}\r
\r
break;\r
\r
default:\r
- printf("ERROR: Only the following bases are supported in the BAM format: {=, A, C, G, T, N}. Found [%c]\n", *pQuery);\r
+ fprintf(stderr, "ERROR: Only the following bases are supported in the BAM format: {=, A, C, G, T, N}. Found [%c]\n", *pQuery);\r
exit(1);\r
}\r
\r
break;\r
\r
default : \r
- printf("ERROR: Invalid tag value type\n"); // shouldn't get here\r
+ fprintf(stderr, "ERROR: Invalid tag value type\n"); // shouldn't get here\r
free(tagData);\r
exit(1); \r
}\r