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