]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
fixed some bugs
[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 #include <ctime>
44
45 #ifdef _WIN32
46         #define exp(x) (exp((double) x))
47         #define sqrt(x) (sqrt((double) x))
48         #define log10(x) (log10((double) x))
49         #define log2(x) (log10(x)/log10(2))
50 #endif
51
52
53 typedef unsigned long long ull;
54
55 struct IntNode {
56         int lvalue;
57         int rvalue;
58         int lcoef;
59         int rcoef;
60         IntNode* left;
61         IntNode* right;
62 };
63         
64 /***********************************************************************/
65
66 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
67 // works for now, but there should be a way to do it without killing the whole program
68
69 class BadConversion : public runtime_error {
70 public:
71         BadConversion(const string& s) : runtime_error(s){ }
72 };
73
74 //**********************************************************************************************************************
75
76 template<typename T>
77 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
78         istringstream i(s);
79         char c;
80         if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
81                 throw BadConversion(s);
82 }
83 //**********************************************************************************************************************
84
85 template<typename T>
86 inline bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
87         istringstream i(s);
88         char c;
89         if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
90         {
91                 return false;
92         } 
93         return true;
94 }
95
96 //**********************************************************************************************************************
97
98 template<typename T>
99 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
100         istringstream i(s);
101         char c;
102         if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
103         {
104                 cout << "'" << s << "' is unable to be converted into an integer.\n";
105                 return false;
106         } 
107         return true;
108 }
109
110 //**********************************************************************************************************************
111
112 template<typename T>
113 string toString(const T&x){
114     stringstream output;
115     output << x;
116     return output.str();
117 }
118
119 //**********************************************************************************************************************
120
121 template<typename T>
122 string toHex(const T&x){
123         stringstream output;
124         
125         output << hex << x;
126
127     return output.str();
128 }
129 //**********************************************************************************************************************
130
131 template<typename T>
132 string toString(const T&x, int i){
133         stringstream output;
134         
135         output.precision(i);
136     output << fixed << x;
137         
138     return output.str();
139 }
140
141
142 /***********************************************************************/
143
144 inline void gobble(istream& f){
145         
146         char d;
147     while(isspace(d=f.get()))           {;}
148         f.putback(d);
149         
150 }
151 /***********************************************************************/
152
153 inline bool isTrue(string f){
154         
155         if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) {       return true;    }
156         else {  return false;  }
157 }
158
159 /***********************************************************************/
160
161 inline float roundDist(float dist, int precision){
162         
163         return int(dist * precision + 0.5)/float(precision);
164         
165 }
166
167 /***********************************************************************/
168
169 inline int getNumNames(string names){
170         
171         int count = 0;
172         
173         if(names != ""){
174                 count = 1;
175                 for(int i=0;i<names.size();i++){
176                         if(names[i] == ','){
177                                 count++;
178                         }
179                 }
180         }
181         
182         return count;
183         
184 }
185
186 /**************************************************************************************************/
187
188 inline vector<vector<double> > binomial(int maxOrder){
189         
190         vector<vector<double> > binomial(maxOrder+1);
191         
192     for(int i=0;i<=maxOrder;i++){
193                 binomial[i].resize(maxOrder+1);
194                 binomial[i][0]=1;
195                 binomial[0][i]=0;
196     }
197     binomial[0][0]=1;
198         
199     binomial[1][0]=1;
200     binomial[1][1]=1;
201         
202     for(int i=2;i<=maxOrder;i++){
203                 binomial[1][i]=0;
204     }
205         
206     for(int i=2;i<=maxOrder;i++){
207                 for(int j=1;j<=maxOrder;j++){
208                         if(i==j){       binomial[i][j]=1;                                                                       }
209                         if(j>i) {       binomial[i][j]=0;                                                                       }
210                         else    {       binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j];     }
211                 }
212     }
213         
214         return binomial;
215 }
216
217 /***********************************************************************/
218
219 inline string getRootName(string longName){
220  
221         string rootName = longName;
222         
223         if(longName.find_last_of(".") != longName.npos){
224                 int pos = longName.find_last_of('.')+1;
225                 rootName = longName.substr(0, pos);
226         }
227
228         return rootName;
229 }
230 /***********************************************************************/
231
232 inline string getSimpleName(string longName){
233  
234         string simpleName = longName;
235         
236         if(longName.find_last_of("/") != longName.npos){
237                 int pos = longName.find_last_of('/')+1;
238                 simpleName = longName.substr(pos, longName.length());
239         }
240
241         return simpleName;
242 }
243 /***********************************************************************/
244
245 inline int factorial(int num){
246         int total = 1;
247         
248         for (int i = 1; i <= num; i++) {
249                 total *= i;
250         }
251         
252         return total;
253 }
254 /**************************************************************************************************
255
256 double min(double x, double y)
257 {
258     if(x<y){    return x;    }
259     else   {    return y;    }
260 }
261
262 /***********************************************************************/
263
264 inline string getPathName(string longName){
265  
266         string rootPathName = longName;
267         
268         if(longName.find_last_of('/') != longName.npos){
269                 int pos = longName.find_last_of('/')+1;
270                 rootPathName = longName.substr(0, pos);
271         }
272
273         return rootPathName;
274 }
275
276 /***********************************************************************/
277
278 inline string getExtension(string longName){
279         
280         string extension = longName;
281         
282         if(longName.find_last_of('.') != longName.npos){
283                 int pos = longName.find_last_of('.');
284                 extension = longName.substr(pos, longName.length());
285         }
286         
287         return extension;
288 }
289
290 /***********************************************************************/
291
292 inline int openInputFile(string fileName, ifstream& fileHandle){
293
294         fileHandle.open(fileName.c_str());
295         if(!fileHandle) {
296                 cerr << "Error: Could not open " << fileName << endl;
297                 return 1;
298         }
299         else {
300                 return 0;
301         }
302         
303 }
304
305 /***********************************************************************/
306
307 inline int openOutputFile(string fileName, ofstream& fileHandle){
308         
309         fileHandle.open(fileName.c_str(), ios::trunc);
310         if(!fileHandle) {
311                 cerr << "Error: Could not open " << fileName << endl;
312                 return 1;
313         }
314         else {
315                 return 0;
316         }
317
318 }
319 /***********************************************************************/
320
321 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
322         
323         fileHandle.open(fileName.c_str(), ios::app);
324         if(!fileHandle) {
325                 cerr << "Error: Could not open " << fileName << endl;
326                 return 1;
327         }
328         else {
329                 return 0;
330         }
331
332 }
333
334
335 /***********************************************************************/
336
337 inline int getNumSeqs(ifstream& file){
338         
339         int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
340         file.seekg(0);
341         return numSeqs;
342
343 }
344
345 /***********************************************************************/
346
347 //This function parses the estimator options and puts them in a vector
348 inline void splitAtDash(string& estim, vector<string>& container) {
349         try {
350                 string individual;
351                 
352                 while (estim.find_first_of('-') != -1) {
353                         individual = estim.substr(0,estim.find_first_of('-'));
354                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
355                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
356                                 container.push_back(individual);
357                         }
358                 }
359                 //get last one
360                 container.push_back(estim);
361         }
362         catch(exception& e) {
363                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
364                 exit(1);
365         }
366         catch(...) {
367                 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
368                 exit(1);
369         }
370
371 }
372
373 /***********************************************************************/
374 //This function parses the label options and puts them in a set
375 inline void splitAtDash(string& estim, set<string>& container) {
376         try {
377                 string individual;
378                 
379                 while (estim.find_first_of('-') != -1) {
380                         individual = estim.substr(0,estim.find_first_of('-'));
381                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
382                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
383                                 container.insert(individual);
384                         }
385                 }
386                 //get last one
387                 container.insert(estim);
388         }
389         catch(exception& e) {
390                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
391                 exit(1);
392         }
393         catch(...) {
394                 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
395                 exit(1);
396         }
397
398 }
399 /***********************************************************************/
400 //This function parses the line options and puts them in a set
401 inline void splitAtDash(string& estim, set<int>& container) {
402         try {
403                 string individual;
404                 int lineNum;
405                 
406                 while (estim.find_first_of('-') != -1) {
407                         individual = estim.substr(0,estim.find_first_of('-'));
408                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
409                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
410                                 convert(individual, lineNum); //convert the string to int
411                                 container.insert(lineNum);
412                         }
413                 }
414                 //get last one
415                 convert(estim, lineNum); //convert the string to int
416                 container.insert(lineNum);
417         }
418         catch(exception& e) {
419                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
420                 exit(1);
421         }
422         catch(...) {
423                 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
424                 exit(1);
425         }
426
427 }
428 /***********************************************************************/
429 //This function parses the a string and puts peices in a vector
430 inline void splitAtComma(string& estim, vector<string>& container) {
431         try {
432                 string individual;
433                 
434                 while (estim.find_first_of(',') != -1) {
435                         individual = estim.substr(0,estim.find_first_of(','));
436                         if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
437                                 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
438                                 container.push_back(individual);
439                         }
440                 }
441                 //get last one
442                 container.push_back(estim);
443         }
444         catch(exception& e) {
445                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
446                 exit(1);
447         }
448         catch(...) {
449                 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
450                 exit(1);
451         }
452 }
453 /***********************************************************************/
454
455 //This function splits up the various option parameters
456 inline void splitAtComma(string& prefix, string& suffix){
457         try {
458                 prefix = suffix.substr(0,suffix.find_first_of(','));
459                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
460                         suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
461                         string space = " ";
462                         while(suffix.at(0) == ' ')
463                                 suffix = suffix.substr(1, suffix.length());
464                 }
465
466         }
467         catch(exception& e) {
468                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
469                 exit(1);
470         }
471         catch(...) {
472                 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
473                 exit(1);
474         }
475
476 }
477 /***********************************************************************/
478
479 //This function separates the key value from the option value i.e. dist=96_...
480 inline void splitAtEquals(string& key, string& value){          
481         try {
482                 if(value.find_first_of('=') != -1){
483                         key = value.substr(0,value.find_first_of('='));
484                         if ((value.find_first_of('=')+1) <= value.length()) {
485                                 value = value.substr(value.find_first_of('=')+1, value.length());
486                         }
487                 }else{
488                         key = value;
489                         value = 1;
490                 }
491         }
492         catch(exception& e) {
493                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
494                 exit(1);
495         }
496         catch(...) {
497                 cout << "An unknown error has occurred in the mothur class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
498                 exit(1);
499         }
500
501 }
502 /**************************************************************************************************/
503
504 inline bool inUsersGroups(string groupname, vector<string> Groups) {
505         try {
506                 for (int i = 0; i < Groups.size(); i++) {
507                         if (groupname == Groups[i]) { return true; }
508                 }
509                 return false;
510         }
511         catch(exception& e) {
512                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
513                 exit(1);
514         }
515         catch(...) {
516                 cout << "An unknown error has occurred in the mothur class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
517                 exit(1);
518         }
519 }
520
521 /***********************************************************************/
522 //this function determines if the user has given us labels that are smaller than the given label.
523 //if so then it returns true so that the calling function can run the previous valid distance.
524 //it's a "smart" distance function.  It also checks for invalid labels.
525 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
526         try {
527                 set<string>::iterator it;
528                 vector<float> orderFloat;
529                 map<string, float> userMap;  //the conversion process removes trailing 0's which we need to put back
530                 map<string, float>::iterator it2;
531                 float labelFloat;
532                 bool smaller = false;
533                 
534                 //unique is the smallest line
535                 if (label == "unique") {  return false;  }
536                 else { convert(label, labelFloat); }
537                 
538                 //go through users set and make them floats
539                 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
540                         
541                         float temp;
542                         if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
543                                 convert(*it, temp);
544                                 orderFloat.push_back(temp);
545                                 userMap[*it] = temp;
546                         }else if (*it == "unique") { 
547                                 orderFloat.push_back(-1.0);
548                                 userMap["unique"] = -1.0;
549                         }else {
550                                 if (errorOff == "") {  cout << *it << " is not a valid label." << endl;  }
551                                 userLabels.erase(*it); 
552                                 it--;
553                         }
554                 }
555                 
556                 //sort order
557                 sort(orderFloat.begin(), orderFloat.end());
558                 
559                 /*************************************************/
560                 //is this label bigger than any of the users labels
561                 /*************************************************/
562                                 
563                 //loop through order until you find a label greater than label
564                 for (int i = 0; i < orderFloat.size(); i++) {
565                         if (orderFloat[i] < labelFloat) {
566                                 smaller = true;
567                                 if (orderFloat[i] == -1) { 
568                                         if (errorOff == "") { cout << "Your file does not include the label unique." <<  endl; }
569                                         userLabels.erase("unique");
570                                 }
571                                 else {  
572                                         if (errorOff == "") { cout << "Your file does not include the label "; }
573                                         string s = "";
574                                         for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {  
575                                                 if (it2->second == orderFloat[i]) {  
576                                                         s = it2->first;  
577                                                         //remove small labels
578                                                         userLabels.erase(s);
579                                                         break;
580                                                 }
581                                         }
582                                         if (errorOff == "") { cout << s << ". I will use the next smallest distance. "  <<  endl; }
583                                 }
584                         //since they are sorted once you find a bigger one stop looking
585                         }else { break; }
586                 }
587                 
588                 return smaller;
589                                                 
590         }
591         catch(exception& e) {
592                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function anyLabelsToProcess. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
593                 exit(1);
594         }
595         catch(...) {
596                 cout << "An unknown error has occurred in the mothur class function anyLabelsToProcess. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
597                 exit(1);
598         }
599
600 }
601
602 /**************************************************************************************************/
603 #endif
604