]> git.donarmstrong.com Git - bamtools.git/blob - bamtools_utilities.h
Implemented Mosaik-style command line parser, instead of former GetOpt parser. Setup...
[bamtools.git] / bamtools_utilities.h
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 // ***************************************************************************
10
11 #ifndef BAMTOOLS_UTILITIES_H
12 #define BAMTOOLS_UTILITIES_H
13
14 #include <cstdlib>
15 #include <iostream>
16 #include <string>
17
18 namespace BamTools {
19
20 // Parses a REGION string, stores in (startChrom, startPos, stopChrom, stopPos) variables
21 // Returns successful parse (true/false)
22 static inline
23 bool ParseRegionString(const std::string& regionString, std::string& startChrom, int& startPos, std::string& stopChrom, int& stopPos) {
24     
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;
29         return false; 
30     }
31   
32     // non-empty string, look for a colom
33     size_t foundFirstColon = regionString.find(':');
34     
35     // no colon found
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?)
42         stopChrom  = "";
43         stopPos    = -1;
44         return true;
45     }
46     
47     // colon found, so we at least have some sort of startPos requested
48     else {
49       
50         // store start chrom from beginning to first colon
51         startChrom = regionString.substr(0,foundFirstColon);
52         
53         // look for ".." after the colon
54         size_t foundRangeDots = regionString.find("..", foundFirstColon+1);
55         
56         // no dots found
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() ); 
61             stopChrom  = "";
62             stopPos    = -1;
63             return true;
64         } 
65         
66         // ".." found, so we have some sort of range selected
67         else {
68           
69             // store startPos between first colon and range dots ".."
70             startPos = atoi( regionString.substr(foundFirstColon+1, foundRangeDots-foundFirstColon-1).c_str() );
71           
72             // look for second colon
73             size_t foundSecondColon = regionString.find(':', foundRangeDots+1);
74             
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 ) {
78                 stopChrom  = "";
79                 stopPos    = atoi( regionString.substr(foundRangeDots+2).c_str() );
80                 return true;
81             }
82             
83             // second colon found
84             // so we have a range requested across 2 chrom's
85             else {
86                 stopChrom  = regionString.substr(foundRangeDots+2, regionString.length()-foundSecondColon-1);
87                 stopPos    = atoi( regionString.substr(foundSecondColon+1).c_str() );
88                 return true;
89             }
90         }
91     }
92   
93     // shouldn't get here - any code path that does?
94     // if not, what does true/false really signify?
95     return false;
96 }
97
98 } // namespace BamTools
99   
100 #endif // BAMTOOLS_UTILITIES_H