// 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
#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
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
: 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
// 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
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
// 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