]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
fixed xp user bug with rarefact commands and cluster error checking
[mothur.git] / mothur.h
1 #ifndef MOTHUR_H
2 #define MOTHUR_H
3
4 using namespace std;
5
6
7 /*
8  *  mothur.h
9  *  Mothur
10  *
11  *  Created by Sarah Westcott on 2/19/09.
12  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
13  *
14  */
15
16 /* This file contains all the standard incudes we use in the project as well as some common utilities. */
17
18 //#include <cstddef>
19
20 //io libraries
21 #include <iostream>
22 #include <iomanip>
23 #include <fstream>
24 #include <sstream>
25
26 //exception
27 #include <stdexcept>
28 #include <exception>
29 #include <cstdlib> 
30
31
32 //containers
33 #include <vector>
34 #include <set>
35 #include <map>
36 #include <string>
37 #include <list>
38
39 //math
40 #include <cmath>
41 #include <math.h>
42 #include <algorithm>
43
44 typedef unsigned long long ull;
45
46 struct IntNode {
47         int lvalue;
48         int rvalue;
49         int lcoef;
50         int rcoef;
51         IntNode* left;
52         IntNode* right;
53 };
54         
55 /***********************************************************************/
56
57 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
58 // works for now, but there should be a way to do it without killing the whole program
59
60 class BadConversion : public runtime_error {
61 public:
62         BadConversion(const string& s) : runtime_error(s){ }
63 };
64
65 //**********************************************************************************************************************
66
67 template<typename T>
68 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
69         istringstream i(s);
70         char c;
71         if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
72                 throw BadConversion(s);
73 }
74 //**********************************************************************************************************************
75
76 template<typename T>
77 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
78         istringstream i(s);
79         char c;
80         if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
81         {
82                 cout << "'" << s << "' is unable to be converted into an integer.\n";
83                 return false;
84         } 
85         return true;
86 }
87
88 //**********************************************************************************************************************
89
90 template<typename T>
91 string toString(const T&x){
92     stringstream output;
93     output << x;
94     return output.str();
95 }
96
97 //**********************************************************************************************************************
98
99 template<typename T>
100 string toHex(const T&x){
101         stringstream output;
102         
103         output << hex << x;
104
105     return output.str();
106 }
107 //**********************************************************************************************************************
108
109 template<typename T>
110 string toString(const T&x, int i){
111         stringstream output;
112         
113         output.precision(i);
114     output << fixed << x;
115         
116     return output.str();
117 }
118
119
120 /***********************************************************************/
121
122 inline void gobble(istream& f){
123         
124         char d;
125     while(isspace(d=f.get()))           {;}
126         f.putback(d);
127         
128 }
129
130 /***********************************************************************/
131
132 inline float roundDist(float dist, int precision){
133         
134         return int(dist * precision + 0.5)/float(precision);
135         
136 }
137
138 /***********************************************************************/
139
140 inline int getNumNames(string names){
141         
142         int count = 0;
143         
144         if(names != ""){
145                 count = 1;
146                 for(int i=0;i<names.size();i++){
147                         if(names[i] == ','){
148                                 count++;
149                         }
150                 }
151         }
152         
153         return count;
154         
155 }
156
157 /**************************************************************************************************/
158
159 inline vector<vector<double> > binomial(int maxOrder){
160         
161         vector<vector<double> > binomial(maxOrder+1);
162         
163     for(int i=0;i<=maxOrder;i++){
164                 binomial[i].resize(maxOrder+1);
165                 binomial[i][0]=1;
166                 binomial[0][i]=0;
167     }
168     binomial[0][0]=1;
169         
170     binomial[1][0]=1;
171     binomial[1][1]=1;
172         
173     for(int i=2;i<=maxOrder;i++){
174                 binomial[1][i]=0;
175     }
176         
177     for(int i=2;i<=maxOrder;i++){
178                 for(int j=1;j<=maxOrder;j++){
179                         if(i==j){       binomial[i][j]=1;                                                                       }
180                         if(j>i) {       binomial[i][j]=0;                                                                       }
181                         else    {       binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j];     }
182                 }
183     }
184         
185         return binomial;
186 }
187
188 /***********************************************************************/
189
190 inline string getRootName(string longName){
191  
192         string rootName = longName;
193         
194         if(longName.find_last_of(".") != longName.npos){
195                 int pos = longName.find_last_of('.')+1;
196                 rootName = longName.substr(0, pos);
197         }
198
199         return rootName;
200 }
201 /***********************************************************************/
202
203 inline string getSimpleName(string longName){
204  
205         string simpleName = longName;
206         
207         if(longName.find_last_of("/") != longName.npos){
208                 int pos = longName.find_last_of('/')+1;
209                 simpleName = longName.substr(pos, longName.length());
210         }
211
212         return simpleName;
213 }
214 /***********************************************************************/
215
216 inline int factorial(int num){
217         int total = 1;
218         
219         for (int i = 1; i <= num; i++) {
220                 total *= i;
221         }
222         
223         return total;
224 }
225
226 /***********************************************************************/
227
228 inline string getPathName(string longName){
229  
230         string rootPathName = longName;
231         
232         if(longName.find_last_of("/") != longName.npos){
233                 int pos = longName.find_last_of('/')+1;
234                 rootPathName = longName.substr(0, pos);
235         }
236
237         return rootPathName;
238 }
239
240 /***********************************************************************/
241
242 inline int openInputFile(string fileName, ifstream& fileHandle){
243
244         fileHandle.open(fileName.c_str());
245         if(!fileHandle) {
246                 cerr << "Error: Could not open " << fileName << endl;
247                 return 1;
248         }
249         else {
250                 return 0;
251         }
252         
253 }
254
255 /***********************************************************************/
256
257 inline int openOutputFile(string fileName, ofstream& fileHandle){
258         
259         fileHandle.open(fileName.c_str(), ios::trunc);
260         if(!fileHandle) {
261                 cerr << "Error: Could not open " << fileName << endl;
262                 return 1;
263         }
264         else {
265                 return 0;
266         }
267
268 }
269
270 /***********************************************************************/
271
272 //This function parses the estimator options and puts them in a vector
273 inline void splitAtDash(string& estim, vector<string>& container) {
274         try {
275                 string individual;
276                 
277                 while (estim.find_first_of('-') != -1) {
278                         individual = estim.substr(0,estim.find_first_of('-'));
279                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
280                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
281                                 container.push_back(individual);
282                         }
283                 }
284                 //get last one
285                 container.push_back(estim);
286         }
287         catch(exception& e) {
288                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
289                 exit(1);
290         }
291         catch(...) {
292                 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
293                 exit(1);
294         }
295
296 }
297
298 /***********************************************************************/
299 //This function parses the label options and puts them in a set
300 inline void splitAtDash(string& estim, set<string>& container) {
301         try {
302                 string individual;
303                 
304                 while (estim.find_first_of('-') != -1) {
305                         individual = estim.substr(0,estim.find_first_of('-'));
306                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
307                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
308                                 container.insert(individual);
309                         }
310                 }
311                 //get last one
312                 container.insert(estim);
313         }
314         catch(exception& e) {
315                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
316                 exit(1);
317         }
318         catch(...) {
319                 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
320                 exit(1);
321         }
322
323 }
324 /***********************************************************************/
325 //This function parses the line options and puts them in a set
326 inline void splitAtDash(string& estim, set<int>& container) {
327         try {
328                 string individual;
329                 int lineNum;
330                 
331                 while (estim.find_first_of('-') != -1) {
332                         individual = estim.substr(0,estim.find_first_of('-'));
333                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
334                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
335                                 convert(individual, lineNum); //convert the string to int
336                                 container.insert(lineNum);
337                         }
338                 }
339                 //get last one
340                 convert(estim, lineNum); //convert the string to int
341                 container.insert(lineNum);
342         }
343         catch(exception& e) {
344                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
345                 exit(1);
346         }
347         catch(...) {
348                 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
349                 exit(1);
350         }
351
352 }
353 /***********************************************************************/
354 //This function parses the a string and puts peices in a vector
355 inline void splitAtComma(string& estim, vector<string>& container) {
356         try {
357                 string individual;
358                 
359                 while (estim.find_first_of(',') != -1) {
360                         individual = estim.substr(0,estim.find_first_of(','));
361                         if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
362                                 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
363                                 container.push_back(individual);
364                         }
365                 }
366                 //get last one
367                 container.push_back(estim);
368         }
369         catch(exception& e) {
370                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
371                 exit(1);
372         }
373         catch(...) {
374                 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
375                 exit(1);
376         }
377 }
378 /***********************************************************************/
379
380 //This function splits up the various option parameters
381 inline void splitAtComma(string& prefix, string& suffix){
382         try {
383                 prefix = suffix.substr(0,suffix.find_first_of(','));
384                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
385                         suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
386                         string space = " ";
387                         while(suffix.at(0) == ' ')
388                                 suffix = suffix.substr(1, suffix.length());
389                 }
390
391         }
392         catch(exception& e) {
393                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
394                 exit(1);
395         }
396         catch(...) {
397                 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
398                 exit(1);
399         }
400
401 }
402 /***********************************************************************/
403
404 //This function separates the key value from the option value i.e. dist=96_...
405 inline void splitAtEquals(string& key, string& value){          
406         try {
407                 if(value.find_first_of('=') != -1){
408                         key = value.substr(0,value.find_first_of('='));
409                         if ((value.find_first_of('=')+1) <= value.length()) {
410                                 value = value.substr(value.find_first_of('=')+1, value.length());
411                         }
412                 }else{
413                         key = value;
414                         value = 1;
415                 }
416         }
417         catch(exception& e) {
418                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
419                 exit(1);
420         }
421         catch(...) {
422                 cout << "An unknown error has occurred in the mothur class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
423                 exit(1);
424         }
425
426 }
427 /**************************************************************************************************/
428
429 inline bool inUsersGroups(string groupname, vector<string> Groups) {
430         try {
431                 for (int i = 0; i < Groups.size(); i++) {
432                         if (groupname == Groups[i]) { return true; }
433                 }
434                 return false;
435         }
436         catch(exception& e) {
437                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
438                 exit(1);
439         }
440         catch(...) {
441                 cout << "An unknown error has occurred in the mothur class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
442                 exit(1);
443         }
444 }
445
446 /**************************************************************************************************/
447
448
449 #endif
450