]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/utils/bamtools_utilities.cpp
Converted intervals from 0-based, CLOSED to 0-based, HALF-OPEN
[bamtools.git] / src / utils / bamtools_utilities.cpp
index f79faacfaec80917b6e6cc726794563d7ea76c7a..c7c45ec35cb5c9f0ca2b031b97e3ae138e063f17 100644 (file)
@@ -2,7 +2,7 @@
 // bamtools_utilities.cpp (c) 2010 Derek Barnett, Erik Garrison
 // Marth Lab, Department of Biology, Boston College
 // ---------------------------------------------------------------------------
-// Last modified: 9 June 2011
+// Last modified: 8 October 2011
 // ---------------------------------------------------------------------------
 // Provides general utilities used by BamTools sub-tools.
 // ***************************************************************************
@@ -88,7 +88,7 @@ bool Utilities::ParseRegionString(const string& regionString,
         startChrom = regionString;
         startPos   = 0;
         stopChrom  = regionString;
-        stopPos    = -1;
+        stopPos    = 0;
     }
     
     // colon found, so we at least have some sort of startPos requested
@@ -141,17 +141,17 @@ bool Utilities::ParseRegionString(const string& regionString,
     
     // if startRefID not found, return false
     int startRefID = reader.GetReferenceID(startChrom);
-    if ( startRefID == (int)references.size() ) return false;  
+    if ( startRefID == -1 ) return false;
     
-    // if startPos is larger than reference, return false
+    // startPos cannot be greater than or equal to reference length
     const RefData& startReference = references.at(startRefID);
-    if ( startPos > startReference.RefLength ) return false;
+    if ( startPos >= startReference.RefLength ) return false;
     
     // if stopRefID not found, return false
     int stopRefID = reader.GetReferenceID(stopChrom);
-    if ( stopRefID == (int)references.size() ) return false;
+    if ( stopRefID == -1 ) return false;
     
-    // if stopPosition larger than reference, return false
+    // stopPosition cannot be larger than reference length
     const RefData& stopReference = references.at(stopRefID);
     if ( stopPos > stopReference.RefLength ) return false;
     
@@ -161,9 +161,9 @@ bool Utilities::ParseRegionString(const string& regionString,
     // -------------------------------
     // set up Region struct & return
     
-    region.LeftRefID = startRefID;
-    region.LeftPosition = startPos;
-    region.RightRefID = stopRefID;;
+    region.LeftRefID     = startRefID;
+    region.LeftPosition  = startPos;
+    region.RightRefID    = stopRefID;;
     region.RightPosition = stopPos;
     return true;
 }
@@ -245,34 +245,34 @@ bool Utilities::ParseRegionString(const string& regionString,
 
     // -------------------------------
     // validate reference IDs & genomic positions
-    
+
     const RefVector references = reader.GetReferenceData();
-    
+
     // if startRefID not found, return false
     int startRefID = reader.GetReferenceID(startChrom);
-    if ( startRefID == (int)references.size() ) return false;  
-    
-    // if startPos is larger than reference, return false
+    if ( startRefID == -1 ) return false;
+
+    // startPos cannot be greater than or equal to reference length
     const RefData& startReference = references.at(startRefID);
-    if ( startPos > startReference.RefLength ) return false;
-    
+    if ( startPos >= startReference.RefLength ) return false;
+
     // if stopRefID not found, return false
     int stopRefID = reader.GetReferenceID(stopChrom);
-    if ( stopRefID == (int)references.size() ) return false;
-    
-    // if stopPosition larger than reference, return false
+    if ( stopRefID == -1 ) return false;
+
+    // stopPosition cannot be larger than reference length
     const RefData& stopReference = references.at(stopRefID);
     if ( stopPos > stopReference.RefLength ) return false;
-    
+
     // if no stopPosition specified, set to reference end
-    if ( stopPos == -1 ) stopPos = stopReference.RefLength;  
-    
+    if ( stopPos == -1 ) stopPos = stopReference.RefLength;
+
     // -------------------------------
     // set up Region struct & return
-    
-    region.LeftRefID = startRefID;
-    region.LeftPosition = startPos;
-    region.RightRefID = stopRefID;;
+
+    region.LeftRefID     = startRefID;
+    region.LeftPosition  = startPos;
+    region.RightRefID    = stopRefID;;
     region.RightPosition = stopPos;
     return true;
 }