]> git.donarmstrong.com Git - bamtools.git/blob - bamtools_utilities.cpp
Implemented CountTool, cleaned up MergeTool.
[bamtools.git] / bamtools_utilities.cpp
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 // ***************************************************************************
10
11 #include <cstdlib>
12 #include <iostream>
13 #include "bamtools_utilities.h"
14
15 using namespace std;
16 using namespace BamTools;
17
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) {
21     
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;
26         return false; 
27     }
28   
29     // non-empty string, look for a colom
30     size_t foundFirstColon = regionString.find(':');
31     
32     // no colon found
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;
38         startPos   = 0;
39         stopChrom  = regionString;
40         stopPos    = -1;
41         return true;
42     }
43     
44     // colon found, so we at least have some sort of startPos requested
45     else {
46       
47         // store start chrom from beginning to first colon
48         startChrom = regionString.substr(0,foundFirstColon);
49         
50         // look for ".." after the colon
51         size_t foundRangeDots = regionString.find("..", foundFirstColon+1);
52         
53         // no dots found
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;
59             stopPos    = -1;
60             return true;
61         } 
62         
63         // ".." found, so we have some sort of range selected
64         else {
65           
66             // store startPos between first colon and range dots ".."
67             startPos = atoi( regionString.substr(foundFirstColon+1, foundRangeDots-foundFirstColon-1).c_str() );
68           
69             // look for second colon
70             size_t foundSecondColon = regionString.find(':', foundRangeDots+1);
71             
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() );
77                 return true;
78             }
79             
80             // second colon found
81             // so we have a range requested across 2 chrom's
82             else {
83                 stopChrom  = regionString.substr(foundRangeDots+2, foundSecondColon-(foundRangeDots+2));
84                 stopPos    = atoi( regionString.substr(foundSecondColon+1).c_str() );
85                 return true;
86             }
87         }
88     }
89   
90     // shouldn't get here - any code path that does?
91     // if not, what does true/false really signify?
92     return false;
93 }