From 3b6edd753c52123d553151400443035f915637b9 Mon Sep 17 00:00:00 2001 From: Derek Date: Fri, 3 Sep 2010 00:35:05 -0400 Subject: [PATCH] Added IsIndexLoaded() public method to BamReader. Allows client to make sure index data is available before attempting any random access ops --- src/api/BamReader.cpp | 15 +++++++++++---- src/api/BamReader.h | 4 +++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/api/BamReader.cpp b/src/api/BamReader.cpp index 10aaa5e..f32b4fb 100644 --- a/src/api/BamReader.cpp +++ b/src/api/BamReader.cpp @@ -137,6 +137,7 @@ BamReader::~BamReader(void) { // file operations void BamReader::Close(void) { d->Close(); } +bool BamReader::IsIndexLoaded(void) const { return d->IsIndexLoaded; } bool BamReader::IsOpen(void) const { return d->mBGZF.IsOpen; } bool BamReader::Jump(int refID, int position) { d->Region.LeftRefID = refID; @@ -360,6 +361,7 @@ bool BamReader::BamReaderPrivate::BuildCharData(BamAlignment& bAlignment) { void BamReader::BamReaderPrivate::ClearIndex(void) { delete NewIndex; NewIndex = 0; + IsIndexLoaded = false; } // closes the BAM file @@ -393,11 +395,15 @@ bool BamReader::BamReaderPrivate::CreateIndex(bool useDefaultIndex) { else NewIndex = new BamToolsIndex(&mBGZF, Parent, IsBigEndian); + // build new index bool ok = true; ok &= NewIndex->Build(); + IsIndexLoaded = ok; + + // attempt to save index data to file ok &= NewIndex->Write(Filename); - // return success/fail + // return success/fail of both building & writing index return ok; } @@ -515,7 +521,7 @@ bool BamReader::BamReaderPrivate::Jump(int refID, int position) { // ----------------------------------------------------------------------- // check for existing index - if ( NewIndex == 0 ) return false; + if ( !IsIndexLoaded || NewIndex == 0 ) return false; // see if reference has alignments if ( !NewIndex->HasAlignments(refID) ) return false; // make sure position is valid @@ -605,8 +611,9 @@ bool BamReader::BamReaderPrivate::LoadIndex(void) { return false; } - // return success of loading index data - return NewIndex->Load(IndexFilename); + // return success of loading index data from file + IsIndexLoaded = NewIndex->Load(IndexFilename); + return IsIndexLoaded; } // populates BamAlignment with alignment data under file pointer, returns success/fail diff --git a/src/api/BamReader.h b/src/api/BamReader.h index c93987b..5d4c83a 100644 --- a/src/api/BamReader.h +++ b/src/api/BamReader.h @@ -3,7 +3,7 @@ // Marth Lab, Department of Biology, Boston College // All rights reserved. // --------------------------------------------------------------------------- -// Last modified: 9 July 2010 (DB) +// Last modified: 2 September 2010 (DB) // --------------------------------------------------------------------------- // Uses BGZF routines were adapted from the bgzf.c code developed at the Broad // Institute. @@ -38,6 +38,8 @@ class BamReader { // close BAM file void Close(void); + // returns whether index data is loaded (i.e. reader is able to Jump() or not) + bool IsIndexLoaded(void) const; // returns whether reader is open for reading or not bool IsOpen(void) const; // performs random-access jump to reference, position -- 2.39.2