1 // ***************************************************************************
2 // bamtools_utilities.h (c) 2010 Derek Barnett, Erik Garrison
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 27 May 2010
7 // ---------------------------------------------------------------------------
8 // Provides general utilities used by BamTools sub-tools.
9 // ***************************************************************************
11 #ifndef BAMTOOLS_UTILITIES_H
12 #define BAMTOOLS_UTILITIES_H
20 // Parses a REGION string, stores in (startChrom, startPos, stopChrom, stopPos) variables
21 // Returns successful parse (true/false)
23 bool ParseRegionString(const std::string& regionString, std::string& startChrom, int& startPos, std::string& stopChrom, int& stopPos) {
25 // shouldn't call this function with empty string but worth checking
26 // checked first for clarity purposes later on, since we can assume at least some content in the string
27 if ( regionString.empty() ) {
28 std::cerr << "Empty REGION. Usual format (e.g. chr2:1000..2000). See README for more detailed uses." << std::endl;
32 // non-empty string, look for a colom
33 size_t foundFirstColon = regionString.find(':');
36 // going to use entire contents of requested chromosome
37 // just store entire region string as startChrom name
38 // use BamReader methods to check if its valid for current BAM file
39 if ( foundFirstColon == std::string::npos ) {
40 startChrom = regionString;
41 startPos = -1; // ** not sure about these defaults (should stopChrom == startChrom if same?)
47 // colon found, so we at least have some sort of startPos requested
50 // store start chrom from beginning to first colon
51 startChrom = regionString.substr(0,foundFirstColon);
53 // look for ".." after the colon
54 size_t foundRangeDots = regionString.find("..", foundFirstColon+1);
57 // so we have a startPos but no range
58 // store contents before colon as startChrom, after as startPos
59 if ( foundRangeDots == std::string::npos ) {
60 startPos = atoi( regionString.substr(foundFirstColon+1).c_str() );
66 // ".." found, so we have some sort of range selected
69 // store startPos between first colon and range dots ".."
70 startPos = atoi( regionString.substr(foundFirstColon+1, foundRangeDots-foundFirstColon-1).c_str() );
72 // look for second colon
73 size_t foundSecondColon = regionString.find(':', foundRangeDots+1);
75 // no second colon found
76 // so we have a "standard" chrom:start..stop input format (on single chrom)
77 if ( foundSecondColon == std::string::npos ) {
79 stopPos = atoi( regionString.substr(foundRangeDots+2).c_str() );
84 // so we have a range requested across 2 chrom's
86 stopChrom = regionString.substr(foundRangeDots+2, regionString.length()-foundSecondColon-1);
87 stopPos = atoi( regionString.substr(foundSecondColon+1).c_str() );
93 // shouldn't get here - any code path that does?
94 // if not, what does true/false really signify?
98 } // namespace BamTools
100 #endif // BAMTOOLS_UTILITIES_H