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