]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
added distance command and filterseqs
[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 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
279         
280         fileHandle.open(fileName.c_str(), ios::app);
281         if(!fileHandle) {
282                 cerr << "Error: Could not open " << fileName << endl;
283                 return 1;
284         }
285         else {
286                 return 0;
287         }
288
289 }
290
291
292 /***********************************************************************/
293
294 //This function parses the estimator options and puts them in a vector
295 inline void splitAtDash(string& estim, vector<string>& container) {
296         try {
297                 string individual;
298                 
299                 while (estim.find_first_of('-') != -1) {
300                         individual = estim.substr(0,estim.find_first_of('-'));
301                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
302                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
303                                 container.push_back(individual);
304                         }
305                 }
306                 //get last one
307                 container.push_back(estim);
308         }
309         catch(exception& e) {
310                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
311                 exit(1);
312         }
313         catch(...) {
314                 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
315                 exit(1);
316         }
317
318 }
319
320 /***********************************************************************/
321 //This function parses the label options and puts them in a set
322 inline void splitAtDash(string& estim, set<string>& container) {
323         try {
324                 string individual;
325                 
326                 while (estim.find_first_of('-') != -1) {
327                         individual = estim.substr(0,estim.find_first_of('-'));
328                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
329                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
330                                 container.insert(individual);
331                         }
332                 }
333                 //get last one
334                 container.insert(estim);
335         }
336         catch(exception& e) {
337                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
338                 exit(1);
339         }
340         catch(...) {
341                 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
342                 exit(1);
343         }
344
345 }
346 /***********************************************************************/
347 //This function parses the line options and puts them in a set
348 inline void splitAtDash(string& estim, set<int>& container) {
349         try {
350                 string individual;
351                 int lineNum;
352                 
353                 while (estim.find_first_of('-') != -1) {
354                         individual = estim.substr(0,estim.find_first_of('-'));
355                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
356                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
357                                 convert(individual, lineNum); //convert the string to int
358                                 container.insert(lineNum);
359                         }
360                 }
361                 //get last one
362                 convert(estim, lineNum); //convert the string to int
363                 container.insert(lineNum);
364         }
365         catch(exception& e) {
366                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
367                 exit(1);
368         }
369         catch(...) {
370                 cout << "An unknown error has occurred in the mothur class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
371                 exit(1);
372         }
373
374 }
375 /***********************************************************************/
376 //This function parses the a string and puts peices in a vector
377 inline void splitAtComma(string& estim, vector<string>& container) {
378         try {
379                 string individual;
380                 
381                 while (estim.find_first_of(',') != -1) {
382                         individual = estim.substr(0,estim.find_first_of(','));
383                         if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
384                                 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
385                                 container.push_back(individual);
386                         }
387                 }
388                 //get last one
389                 container.push_back(estim);
390         }
391         catch(exception& e) {
392                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
393                 exit(1);
394         }
395         catch(...) {
396                 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
397                 exit(1);
398         }
399 }
400 /***********************************************************************/
401
402 //This function splits up the various option parameters
403 inline void splitAtComma(string& prefix, string& suffix){
404         try {
405                 prefix = suffix.substr(0,suffix.find_first_of(','));
406                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
407                         suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
408                         string space = " ";
409                         while(suffix.at(0) == ' ')
410                                 suffix = suffix.substr(1, suffix.length());
411                 }
412
413         }
414         catch(exception& e) {
415                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
416                 exit(1);
417         }
418         catch(...) {
419                 cout << "An unknown error has occurred in the mothur class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
420                 exit(1);
421         }
422
423 }
424 /***********************************************************************/
425
426 //This function separates the key value from the option value i.e. dist=96_...
427 inline void splitAtEquals(string& key, string& value){          
428         try {
429                 if(value.find_first_of('=') != -1){
430                         key = value.substr(0,value.find_first_of('='));
431                         if ((value.find_first_of('=')+1) <= value.length()) {
432                                 value = value.substr(value.find_first_of('=')+1, value.length());
433                         }
434                 }else{
435                         key = value;
436                         value = 1;
437                 }
438         }
439         catch(exception& e) {
440                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
441                 exit(1);
442         }
443         catch(...) {
444                 cout << "An unknown error has occurred in the mothur class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
445                 exit(1);
446         }
447
448 }
449 /**************************************************************************************************/
450
451 inline bool inUsersGroups(string groupname, vector<string> Groups) {
452         try {
453                 for (int i = 0; i < Groups.size(); i++) {
454                         if (groupname == Groups[i]) { return true; }
455                 }
456                 return false;
457         }
458         catch(exception& e) {
459                 cout << "Standard Error: " << e.what() << " has occurred in the mothur class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
460                 exit(1);
461         }
462         catch(...) {
463                 cout << "An unknown error has occurred in the mothur class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
464                 exit(1);
465         }
466 }
467
468 /**************************************************************************************************/
469
470
471 #endif
472