X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2Fapi%2Finternal%2FBamReader_p.cpp;h=384e2fe59d50b5912b6bb2091a413813dd6339ae;hb=c1fc1c5423ca73a1b5bcbe790650821d73e5959c;hp=65b19f330a60c7fe9ac00cc1b992a98ce7b186d6;hpb=72f5b82ad353b4cc9d6f8153f1ad19cc387b9597;p=bamtools.git diff --git a/src/api/internal/BamReader_p.cpp b/src/api/internal/BamReader_p.cpp index 65b19f3..384e2fe 100644 --- a/src/api/internal/BamReader_p.cpp +++ b/src/api/internal/BamReader_p.cpp @@ -9,6 +9,8 @@ #include #include +#include +#include #include #include #include @@ -96,6 +98,9 @@ bool BamReaderPrivate::GetNextAlignment(BamAlignment& alignment) { // useful for operations requiring ONLY positional or other alignment-related information bool BamReaderPrivate::GetNextAlignmentCore(BamAlignment& alignment) { + if ( !m_stream.IsOpen() ) + return false; + // skip if region is set but has no alignments if ( m_randomAccessController.HasRegion() && !m_randomAccessController.RegionHasAlignments() ) @@ -164,7 +169,7 @@ bool BamReaderPrivate::HasIndex(void) const { } bool BamReaderPrivate::IsOpen(void) const { - return m_stream.IsOpen; + return m_stream.IsOpen(); } // load BAM header data @@ -189,7 +194,7 @@ bool BamReaderPrivate::LoadNextAlignment(BamAlignment& alignment) { // swap core endian-ness if necessary if ( m_isBigEndian ) { - for ( int i = 0; i < Constants::BAM_CORE_SIZE; i+=sizeof(uint32_t) ) + for ( unsigned int i = 0; i < Constants::BAM_CORE_SIZE; i+=sizeof(uint32_t) ) BamTools::SwapEndian_32p(&x[i]); } @@ -219,7 +224,7 @@ bool BamReaderPrivate::LoadNextAlignment(BamAlignment& alignment) { const unsigned int dataLength = alignment.SupportData.BlockLength - Constants::BAM_CORE_SIZE; char* allCharData = (char*)calloc(sizeof(char), dataLength); - if ( m_stream.Read(allCharData, dataLength) == (signed int)dataLength ) { + if ( m_stream.Read(allCharData, dataLength) == dataLength ) { // store 'allCharData' in supportData structure alignment.SupportData.AllCharData.assign((const char*)allCharData, dataLength); @@ -300,12 +305,11 @@ bool BamReaderPrivate::LocateIndex(const BamIndex::IndexType& preferredType) { // opens BAM file (and index) bool BamReaderPrivate::Open(const string& filename) { - // close current BAM file if open - if ( m_stream.IsOpen ) - Close(); + // make sure we're starting with a fresh slate + Close(); // attempt to open BgzfStream for reading - if ( !m_stream.Open(filename, "rb") ) { + if ( !m_stream.Open(filename, IBamIODevice::ReadOnly) ) { cerr << "BamReader ERROR: Could not open BGZF stream for " << filename << endl; return false; } @@ -339,14 +343,23 @@ bool BamReaderPrivate::OpenIndex(const std::string& indexFilename) { // returns BAM file pointer to beginning of alignment data bool BamReaderPrivate::Rewind(void) { + if ( !m_stream.IsOpen() ) { + cerr << "BRP::Rewind() - stream not open" << endl; + return false; + } + // attempt rewind to first alignment - if ( !m_stream.Seek(m_alignmentsBeginOffset) ) + if ( !m_stream.Seek(m_alignmentsBeginOffset) ) { + cerr << "BRP::Rewind() - could not seek to ABO (1st time)" << endl; return false; + } // verify that we can read first alignment BamAlignment al; - if ( !LoadNextAlignment(al) ) + if ( !LoadNextAlignment(al) ) { + cerr << "BRP::Rewind() - could not read first alignment" << endl; return false; + } // reset region m_randomAccessController.ClearRegion();