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