]> git.donarmstrong.com Git - mothur.git/blob - utilities.hpp
added read.shared, broke up globaldata a bit
[mothur.git] / utilities.hpp
1 #ifndef UTILITIES_H
2 #define UTILITIES_H
3
4 using namespace std;
5
6 #include <iostream>
7 #include <iomanip>
8 #include <fstream>
9 #include <sstream>
10 #include <cmath>
11 #include <vector>
12 #include <stdexcept>
13 #include <set>
14
15 typedef unsigned long long ull;
16
17 /***********************************************************************/
18
19 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
20 // works for now, but there should be a way to do it without killing the whole program
21
22 class BadConversion : public runtime_error {
23 public:
24         BadConversion(const string& s) : runtime_error(s){ }
25 };
26
27 //**********************************************************************************************************************
28
29 template<typename T>
30 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
31         istringstream i(s);
32         char c;
33         if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
34                 throw BadConversion(s);
35 }
36
37 //**********************************************************************************************************************
38
39 template<typename T>
40 string toString(const T&x){
41     stringstream output;
42     output << x;
43     return output.str();
44 }
45
46 //**********************************************************************************************************************
47
48 template<typename T>
49 string toString(const T&x, int i){
50         stringstream output;
51         
52         output.precision(i);
53     output << fixed << x;
54         
55     return output.str();
56 }
57
58
59 /***********************************************************************/
60
61 inline void gobble(ifstream& f){
62         
63         char d;
64     while(isspace(d=f.get()))           {;}
65         f.putback(d);
66         
67 }
68
69 /***********************************************************************/
70
71 inline float roundDist(float dist, int precision){
72         
73         return int(dist * precision + 0.5)/float(precision);
74         
75 }
76
77 /***********************************************************************/
78
79 inline int getNumNames(string names){
80         
81         int count = 0;
82         
83         if(names != ""){
84                 count = 1;
85                 for(int i=0;i<names.size();i++){
86                         if(names[i] == ','){
87                                 count++;
88                         }
89                 }
90         }
91         
92         return count;
93         
94 }
95
96 /**************************************************************************************************/
97
98 inline vector<vector<double> > binomial(int maxOrder){
99         
100         vector<vector<double> > binomial(maxOrder+1);
101         
102     for(int i=0;i<=maxOrder;i++){
103                 binomial[i].resize(maxOrder+1);
104                 binomial[i][0]=1;
105                 binomial[0][i]=0;
106     }
107     binomial[0][0]=1;
108         
109     binomial[1][0]=1;
110     binomial[1][1]=1;
111         
112     for(int i=2;i<=maxOrder;i++){
113                 binomial[1][i]=0;
114     }
115         
116     for(int i=2;i<=maxOrder;i++){
117                 for(int j=1;j<=maxOrder;j++){
118                         if(i==j){       binomial[i][j]=1;                                                                       }
119                         if(j>i) {       binomial[i][j]=0;                                                                       }
120                         else    {       binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j];     }
121                 }
122     }
123         
124         return binomial;
125 }
126
127 /***********************************************************************/
128
129 inline string getRootName(string longName){
130  
131         string rootName = longName;
132         
133         if(longName.find_last_of(".") != longName.npos){
134                 int pos = longName.find_last_of('.')+1;
135                 rootName = longName.substr(0, pos);
136         }
137
138         return rootName;
139 }
140 /***********************************************************************/
141
142 inline string getSimpleName(string longName){
143  
144         string simpleName = longName;
145         
146         if(longName.find_last_of("/") != longName.npos){
147                 int pos = longName.find_last_of('/')+1;
148                 simpleName = longName.substr(pos, longName.length());
149         }
150
151         return simpleName;
152 }
153
154 /***********************************************************************/
155
156 inline string getPathName(string longName){
157  
158         string rootPathName = longName;
159         
160         if(longName.find_last_of("/") != longName.npos){
161                 int pos = longName.find_last_of('/')+1;
162                 rootPathName = longName.substr(0, pos);
163         }
164
165         return rootPathName;
166 }
167
168 /***********************************************************************/
169
170 inline int openInputFile(string fileName, ifstream& fileHandle){
171
172         fileHandle.open(fileName.c_str());
173         if(!fileHandle) {
174                 cerr << "Error: Could not open " << fileName << endl;
175                 return 1;
176         }
177         else {
178                 return 0;
179         }
180         
181 }
182
183 /***********************************************************************/
184
185 inline int openOutputFile(string fileName, ofstream& fileHandle){
186         
187         fileHandle.open(fileName.c_str(), ios::trunc);
188         if(!fileHandle) {
189                 cerr << "Error: Could not open " << fileName << endl;
190                 return 1;
191         }
192         else {
193                 return 0;
194         }
195
196 }
197
198 /***********************************************************************/
199
200 //This function parses the estimator options and puts them in a vector
201 inline void splitAtDash(string& estim, vector<string>& container) {
202         try {
203                 string individual;
204                 
205                 while (estim.find_first_of('-') != -1) {
206                         individual = estim.substr(0,estim.find_first_of('-'));
207                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
208                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
209                                 container.push_back(individual);
210                         }
211                 }
212                 //get last one
213                 container.push_back(estim);
214         }
215         catch(exception& e) {
216                 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
217                 exit(1);
218         }
219         catch(...) {
220                 cout << "An unknown error has occurred in the utilities class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
221                 exit(1);
222         }
223
224 }
225
226 /***********************************************************************/
227 //This function parses the label options and puts them in a set
228 inline void splitAtDash(string& estim, set<string>& container) {
229         try {
230                 string individual;
231                 
232                 while (estim.find_first_of('-') != -1) {
233                         individual = estim.substr(0,estim.find_first_of('-'));
234                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
235                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
236                                 container.insert(individual);
237                         }
238                 }
239                 //get last one
240                 container.insert(estim);
241         }
242         catch(exception& e) {
243                 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
244                 exit(1);
245         }
246         catch(...) {
247                 cout << "An unknown error has occurred in the utilities class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
248                 exit(1);
249         }
250
251 }
252 /***********************************************************************/
253 //This function parses the line options and puts them in a set
254 inline void splitAtDash(string& estim, set<int>& container) {
255         try {
256                 string individual;
257                 int lineNum;
258                 
259                 while (estim.find_first_of('-') != -1) {
260                         individual = estim.substr(0,estim.find_first_of('-'));
261                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
262                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
263                                 convert(individual, lineNum); //convert the string to int
264                                 container.insert(lineNum);
265                         }
266                 }
267                 //get last one
268                 convert(estim, lineNum); //convert the string to int
269                 container.insert(lineNum);
270         }
271         catch(exception& e) {
272                 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
273                 exit(1);
274         }
275         catch(...) {
276                 cout << "An unknown error has occurred in the utilities class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
277                 exit(1);
278         }
279
280 }
281 /***********************************************************************/
282
283 //This function splits up the various option parameters
284 inline void splitAtComma(string& prefix, string& suffix){
285         try {
286                 prefix = suffix.substr(0,suffix.find_first_of(','));
287                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
288                         suffix = suffix.substr(suffix.find_first_of(',')+2, suffix.length());
289                 }
290         }
291         catch(exception& e) {
292                 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
293                 exit(1);
294         }
295         catch(...) {
296                 cout << "An unknown error has occurred in the utilities class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
297                 exit(1);
298         }
299
300 }
301 /***********************************************************************/
302
303 //This function separates the key value from the option value i.e. dist=96_...
304 inline void splitAtEquals(string& key, string& value){          
305         try {
306                 if(value.find_first_of('=') != -1){
307                         key = value.substr(0,value.find_first_of('='));
308                         if ((value.find_first_of('=')+1) <= value.length()) {
309                                 value = value.substr(value.find_first_of('=')+1, value.length());
310                         }
311                 }else{
312                         key = value;
313                         value = 1;
314                 }
315         }
316         catch(exception& e) {
317                 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
318                 exit(1);
319         }
320         catch(...) {
321                 cout << "An unknown error has occurred in the utilities class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
322                 exit(1);
323         }
324
325 }
326 /*******************************************************/
327
328
329
330 #endif