]> git.donarmstrong.com Git - bamtools.git/commitdiff
Put utilities into own class with static methods
authorDerek <derekwbarnett@gmail.com>
Wed, 2 Jun 2010 16:06:43 +0000 (12:06 -0400)
committerDerek <derekwbarnett@gmail.com>
Wed, 2 Jun 2010 16:06:43 +0000 (12:06 -0400)
Makefile
bamtools_count.cpp
bamtools_utilities.cpp [new file with mode: 0644]

index 9a72fcc78a74f1b00051aea52d53f2bd4a8bdae9..280ab48895870ce8be5c399142673ceb4177413a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ CXX=            g++
 CXXFLAGS=      -Wall -O3\r
 PROG=          bamtools\r
 API=           BGZF.o BamReader.o BamWriter.o BamMultiReader.o
-UTILS=         bamtools_options.o
+UTILS=         bamtools_options.o bamtools_utilities.o
 TOOLKIT=       bamtools_count.o bamtools_coverage.o bamtools_header.o bamtools_index.o bamtools_merge.o bamtools_sam.o bamtools_sort.o bamtools_stats.o
 MAIN=          bamtools.o
 LIBS=          -lz
index bfdfd28a548d9389dd1df84a0fd2c2eda6493902..1560e1c5d4bdc5c79b973b9f5fef6c72e145cfab 100644 (file)
@@ -3,7 +3,7 @@
 // Marth Lab, Department of Biology, Boston College
 // All rights reserved.
 // ---------------------------------------------------------------------------
-// Last modified: 1 June 2010
+// Last modified: 2 June 2010
 // ---------------------------------------------------------------------------
 // Prints alignment count for BAM file
 //
@@ -90,7 +90,7 @@ int CountTool::Run(int argc, char* argv[]) {
     if ( !m_settings->HasRegion ) {
         cerr << "Counting all alignments " << endl;
     } else {
-        if ( ParseRegionString(m_settings->Region, startChrom, startPos, stopChrom, stopPos) ) {
+        if ( Utilities::ParseRegionString(m_settings->Region, startChrom, startPos, stopChrom, stopPos) ) {
             cerr << "Counting only alignments in region " << m_settings->Region << endl;
             cerr << "StartChrom: " << startChrom << endl;
             cerr << "StartPos:   " << startPos << endl;
diff --git a/bamtools_utilities.cpp b/bamtools_utilities.cpp
new file mode 100644 (file)
index 0000000..8eea9a6
--- /dev/null
@@ -0,0 +1,93 @@
+// ***************************************************************************
+// bamtools_utilities.cpp (c) 2010 Derek Barnett, Erik Garrison
+// Marth Lab, Department of Biology, Boston College
+// All rights reserved.
+// ---------------------------------------------------------------------------
+// Last modified: 2 June 2010
+// ---------------------------------------------------------------------------
+// Provides general utilities used by BamTools sub-tools.
+// ***************************************************************************
+
+#include <cstdlib>
+#include <iostream>
+#include "bamtools_utilities.h"
+
+using namespace std;
+using namespace BamTools;
+
+// Parses a REGION string, stores in (startChrom, startPos, stopChrom, stopPos) variables
+// Returns successful parse (true/false)
+bool Utilities::ParseRegionString(const string& regionString, string& startChrom, int& startPos, string& stopChrom, int& stopPos) {
+    
+    // shouldn't call this function with empty string but worth checking 
+    // checked first for clarity purposes later on, since we can assume at least some content in the string
+    if ( regionString.empty() ) { 
+        cerr << "Empty REGION. Usual format (e.g. chr2:1000..2000). See README for more detailed uses." << endl;
+        return false; 
+    }
+  
+    // non-empty string, look for a colom
+    size_t foundFirstColon = regionString.find(':');
+    
+    // no colon found
+    // going to use entire contents of requested chromosome 
+    // just store entire region string as startChrom name
+    // use BamReader methods to check if its valid for current BAM file
+    if ( foundFirstColon == string::npos ) {
+        startChrom = regionString;
+        startPos   = -1;                 // ** not sure about these defaults (should stopChrom == startChrom if same?)
+        stopChrom  = "";
+        stopPos    = -1;
+        return true;
+    }
+    
+    // colon found, so we at least have some sort of startPos requested
+    else {
+      
+        // store start chrom from beginning to first colon
+        startChrom = regionString.substr(0,foundFirstColon);
+        
+        // look for ".." after the colon
+        size_t foundRangeDots = regionString.find("..", foundFirstColon+1);
+        
+        // no dots found
+        // so we have a startPos but no range
+        // store contents before colon as startChrom, after as startPos
+        if ( foundRangeDots == string::npos ) {
+            startPos   = atoi( regionString.substr(foundFirstColon+1).c_str() ); 
+            stopChrom  = "";
+            stopPos    = -1;
+            return true;
+        } 
+        
+        // ".." found, so we have some sort of range selected
+        else {
+          
+            // store startPos between first colon and range dots ".."
+            startPos = atoi( regionString.substr(foundFirstColon+1, foundRangeDots-foundFirstColon-1).c_str() );
+          
+            // look for second colon
+            size_t foundSecondColon = regionString.find(':', foundRangeDots+1);
+            
+            // no second colon found
+            // so we have a "standard" chrom:start..stop input format (on single chrom)
+            if ( foundSecondColon == string::npos ) {
+                stopChrom  = "";
+                stopPos    = atoi( regionString.substr(foundRangeDots+2).c_str() );
+                return true;
+            }
+            
+            // second colon found
+            // so we have a range requested across 2 chrom's
+            else {
+                stopChrom  = regionString.substr(foundRangeDots+2, regionString.length()-foundSecondColon-1);
+                stopPos    = atoi( regionString.substr(foundSecondColon+1).c_str() );
+                return true;
+            }
+        }
+    }
+  
+    // shouldn't get here - any code path that does?
+    // if not, what does true/false really signify?
+    return false;
+}
\ No newline at end of file