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