1 // ***************************************************************************
2 // bamtools_utilities.cpp (c) 2010 Derek Barnett, Erik Garrison
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 2 June 2010
7 // ---------------------------------------------------------------------------
8 // Provides general utilities used by BamTools sub-tools.
9 // ***************************************************************************
13 #include "bamtools_utilities.h"
16 using namespace BamTools;
18 // Parses a REGION string, stores in (startChrom, startPos, stopChrom, stopPos) variables
19 // Returns successful parse (true/false)
20 bool Utilities::ParseRegionString(const string& regionString, string& startChrom, int& startPos, string& stopChrom, int& stopPos) {
22 // shouldn't call this function with empty string but worth checking
23 // checked first for clarity purposes later on, since we can assume at least some content in the string
24 if ( regionString.empty() ) {
25 cerr << "Empty REGION. Usual format (e.g. chr2:1000..2000). See README for more detailed uses." << endl;
29 // non-empty string, look for a colom
30 size_t foundFirstColon = regionString.find(':');
33 // going to use entire contents of requested chromosome
34 // just store entire region string as startChrom name
35 // use BamReader methods to check if its valid for current BAM file
36 if ( foundFirstColon == string::npos ) {
37 startChrom = regionString;
39 stopChrom = regionString;
44 // colon found, so we at least have some sort of startPos requested
47 // store start chrom from beginning to first colon
48 startChrom = regionString.substr(0,foundFirstColon);
50 // look for ".." after the colon
51 size_t foundRangeDots = regionString.find("..", foundFirstColon+1);
54 // so we have a startPos but no range
55 // store contents before colon as startChrom, after as startPos
56 if ( foundRangeDots == string::npos ) {
57 startPos = atoi( regionString.substr(foundFirstColon+1).c_str() );
58 stopChrom = startChrom;
63 // ".." found, so we have some sort of range selected
66 // store startPos between first colon and range dots ".."
67 startPos = atoi( regionString.substr(foundFirstColon+1, foundRangeDots-foundFirstColon-1).c_str() );
69 // look for second colon
70 size_t foundSecondColon = regionString.find(':', foundRangeDots+1);
72 // no second colon found
73 // so we have a "standard" chrom:start..stop input format (on single chrom)
74 if ( foundSecondColon == string::npos ) {
75 stopChrom = startChrom;
76 stopPos = atoi( regionString.substr(foundRangeDots+2).c_str() );
81 // so we have a range requested across 2 chrom's
83 stopChrom = regionString.substr(foundRangeDots+2, foundSecondColon-(foundRangeDots+2));
84 stopPos = atoi( regionString.substr(foundSecondColon+1).c_str() );
90 // shouldn't get here - any code path that does?
91 // if not, what does true/false really signify?