]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/api/BamReader.cpp
Fixed: bug related to accessing data (or regions with no alignments) near the ends...
[bamtools.git] / src / api / BamReader.cpp
index 06232afd13e56fbae2b98bb7c8cf0c5951601adf..f16d98dcfd5db9714c740a60b7b8d0e929773483 100644 (file)
@@ -3,7 +3,7 @@
 // Marth Lab, Department of Biology, Boston College\r
 // All rights reserved.\r
 // ---------------------------------------------------------------------------\r
-// Last modified: 10 September 2010 (DB)\r
+// Last modified: 17 September 2010 (DB)\r
 // ---------------------------------------------------------------------------\r
 // Uses BGZF routines were adapted from the bgzf.c code developed at the Broad\r
 // Institute.\r
@@ -17,8 +17,6 @@
 #include <string>\r
 #include <vector>\r
 #include <iostream>\r
-\r
-// BamTools includes\r
 #include "BGZF.h"\r
 #include "BamReader.h"\r
 #include "BamIndex.h"\r
@@ -53,9 +51,8 @@ struct BamReader::BamReaderPrivate {
     bool IsBigEndian;\r
 \r
     // user-specified region values\r
-    BamRegion Region;    \r
-    int  CurrentRefID;\r
-    int  CurrentLeft;\r
+    BamRegion Region;\r
+    bool HasAlignmentsInRegion;\r
 \r
     // parent BamReader\r
     BamReader* Parent;\r
@@ -173,8 +170,6 @@ BamReader::BamReaderPrivate::BamReaderPrivate(BamReader* parent)
     : Index(0)\r
     , IsIndexLoaded(false)\r
     , AlignmentsBeginOffset(0)\r
-    , CurrentRefID(0)\r
-    , CurrentLeft(0)\r
     , Parent(parent)\r
     , DNA_LOOKUP("=ACMGRSVTWYHKDBN")\r
     , CIGAR_LOOKUP("MIDNSHP")\r
@@ -400,6 +395,10 @@ bool BamReader::BamReaderPrivate::GetNextAlignment(BamAlignment& bAlignment) {
 // useful for operations requiring ONLY positional or other alignment-related information\r
 bool BamReader::BamReaderPrivate::GetNextAlignmentCore(BamAlignment& bAlignment) {\r
 \r
+    // if region is set but has no alignments\r
+    if ( !Region.isNull() && !HasAlignmentsInRegion ) \r
+        return false;\r
+  \r
     // if valid alignment available\r
     if ( LoadNextAlignment(bAlignment) ) {\r
 \r
@@ -714,6 +713,7 @@ bool BamReader::BamReaderPrivate::Rewind(void) {
     Region.clear();\r
     Region.LeftRefID    = al.RefID;\r
     Region.LeftPosition = al.Position;\r
+    HasAlignmentsInRegion = true;\r
 \r
     // rewind back to beginning of first alignment\r
     // return success/fail of seek\r
@@ -737,10 +737,17 @@ bool BamReader::BamReaderPrivate::SetRegion(const BamRegion& region) {
     // check for existing index \r
     if ( !IsIndexLoaded || Index == 0 ) return false; \r
     \r
-    // attempt jump to user-specified region, return false if failed\r
-    if ( !Index->Jump(region) ) return false;\r
+    // attempt jump to user-specified region return false if jump could not be performed at all\r
+    // (invalid index, unknown reference, etc)\r
+    //\r
+    // Index::Jump() is allowed to modify the HasAlignmentsInRegion flag\r
+    //  * This covers case where a region is requested that lies beyond the last alignment on a reference\r
+    //    If this occurs, any subsequent calls to GetNexAlignment[Core] simply return false\r
+    //    BamMultiReader is then able to successfully pull alignments from a region from multiple files\r
+    //    even if one or more have no data.\r
+    if ( !Index->Jump(region, &HasAlignmentsInRegion) ) return false;\r
       \r
-    // if successful, save region data locally, return success\r
+    // if jump successful, save region data & return success\r
     Region = region;\r
     return true;\r
 }\r