]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
altered venn command to make use of sharedchao for any number of groups, fixed window...
[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 double min(double x, double y)
228 {
229     if(x<y){    return x;    }
230     else   {    return y;    }
231 }
232
233 /***********************************************************************/
234
235 inline string getPathName(string longName){
236  
237         string rootPathName = longName;
238         
239         if(longName.find_last_of("/") != longName.npos){
240                 int pos = longName.find_last_of('/')+1;
241                 rootPathName = longName.substr(0, pos);
242         }
243
244         return rootPathName;
245 }
246
247 /***********************************************************************/
248
249 inline int openInputFile(string fileName, ifstream& fileHandle){
250
251         fileHandle.open(fileName.c_str());
252         if(!fileHandle) {
253                 cerr << "Error: Could not open " << fileName << endl;
254                 return 1;
255         }
256         else {
257                 return 0;
258         }
259         
260 }
261
262 /***********************************************************************/
263
264 inline int openOutputFile(string fileName, ofstream& fileHandle){
265         
266         fileHandle.open(fileName.c_str(), ios::trunc);
267         if(!fileHandle) {
268                 cerr << "Error: Could not open " << fileName << endl;
269                 return 1;
270         }
271         else {
272                 return 0;
273         }
274
275 }
276
277 /***********************************************************************/
278
279 //This function parses the estimator options and puts them in a vector
280 inline void splitAtDash(string& estim, vector<string>& container) {
281         try {
282                 string individual;
283                 
284                 while (estim.find_first_of('-') != -1) {
285                         individual = estim.substr(0,estim.find_first_of('-'));
286                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
287                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
288                                 container.push_back(individual);
289                         }
290                 }
291                 //get last one
292                 container.push_back(estim);
293         }
294         catch(exception& e) {
295                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
296                 exit(1);
297         }
298         catch(...) {
299                 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
300                 exit(1);
301         }
302
303 }
304
305 /***********************************************************************/
306 //This function parses the label options and puts them in a set
307 inline void splitAtDash(string& estim, set<string>& container) {
308         try {
309                 string individual;
310                 
311                 while (estim.find_first_of('-') != -1) {
312                         individual = estim.substr(0,estim.find_first_of('-'));
313                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
314                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
315                                 container.insert(individual);
316                         }
317                 }
318                 //get last one
319                 container.insert(estim);
320         }
321         catch(exception& e) {
322                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
323                 exit(1);
324         }
325         catch(...) {
326                 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
327                 exit(1);
328         }
329
330 }
331 /***********************************************************************/
332 //This function parses the line options and puts them in a set
333 inline void splitAtDash(string& estim, set<int>& container) {
334         try {
335                 string individual;
336                 int lineNum;
337                 
338                 while (estim.find_first_of('-') != -1) {
339                         individual = estim.substr(0,estim.find_first_of('-'));
340                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
341                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
342                                 convert(individual, lineNum); //convert the string to int
343                                 container.insert(lineNum);
344                         }
345                 }
346                 //get last one
347                 convert(estim, lineNum); //convert the string to int
348                 container.insert(lineNum);
349         }
350         catch(exception& e) {
351                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
352                 exit(1);
353         }
354         catch(...) {
355                 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
356                 exit(1);
357         }
358
359 }
360 /***********************************************************************/
361 //This function parses the a string and puts peices in a vector
362 inline void splitAtComma(string& estim, vector<string>& container) {
363         try {
364                 string individual;
365                 
366                 while (estim.find_first_of(',') != -1) {
367                         individual = estim.substr(0,estim.find_first_of(','));
368                         if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
369                                 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
370                                 container.push_back(individual);
371                         }
372                 }
373                 //get last one
374                 container.push_back(estim);
375         }
376         catch(exception& e) {
377                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
378                 exit(1);
379         }
380         catch(...) {
381                 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
382                 exit(1);
383         }
384 }
385 /***********************************************************************/
386
387 //This function splits up the various option parameters
388 inline void splitAtComma(string& prefix, string& suffix){
389         try {
390                 prefix = suffix.substr(0,suffix.find_first_of(','));
391                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
392                         suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
393                         string space = " ";
394                         while(suffix.at(0) == ' ')
395                                 suffix = suffix.substr(1, suffix.length());
396                 }
397
398         }
399         catch(exception& e) {
400                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
401                 exit(1);
402         }
403         catch(...) {
404                 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
405                 exit(1);
406         }
407
408 }
409 /***********************************************************************/
410
411 //This function separates the key value from the option value i.e. dist=96_...
412 inline void splitAtEquals(string& key, string& value){          
413         try {
414                 if(value.find_first_of('=') != -1){
415                         key = value.substr(0,value.find_first_of('='));
416                         if ((value.find_first_of('=')+1) <= value.length()) {
417                                 value = value.substr(value.find_first_of('=')+1, value.length());
418                         }
419                 }else{
420                         key = value;
421                         value = 1;
422                 }
423         }
424         catch(exception& e) {
425                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
426                 exit(1);
427         }
428         catch(...) {
429                 cout << "An unknown error has occurred in the mothur class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
430                 exit(1);
431         }
432
433 }
434 /**************************************************************************************************/
435
436 inline bool inUsersGroups(string groupname, vector<string> Groups) {
437         try {
438                 for (int i = 0; i < Groups.size(); i++) {
439                         if (groupname == Groups[i]) { return true; }
440                 }
441                 return false;
442         }
443         catch(exception& e) {
444                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
445                 exit(1);
446         }
447         catch(...) {
448                 cout << "An unknown error has occurred in the mothur class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
449                 exit(1);
450         }
451 }
452
453 /**************************************************************************************************/
454
455
456 #endif
457