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