]> git.donarmstrong.com Git - mothur.git/blob - utilities.hpp
d1b32490beca985771529fb0665842109272329b
[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 toHex(const T&x){
57         stringstream output;
58         
59         output << hex << x;
60
61     return output.str();
62 }
63
64 //**********************************************************************************************************************
65
66 template<typename T>
67 string toString(const T&x, int i){
68         stringstream output;
69         
70         output.precision(i);
71     output << fixed << x;
72         
73     return output.str();
74 }
75
76
77 /***********************************************************************/
78
79 inline void gobble(istream& f){
80         
81         char d;
82     while(isspace(d=f.get()))           {;}
83         f.putback(d);
84         
85 }
86
87 /***********************************************************************/
88
89 inline float roundDist(float dist, int precision){
90         
91         return int(dist * precision + 0.5)/float(precision);
92         
93 }
94
95 /***********************************************************************/
96
97 inline int getNumNames(string names){
98         
99         int count = 0;
100         
101         if(names != ""){
102                 count = 1;
103                 for(int i=0;i<names.size();i++){
104                         if(names[i] == ','){
105                                 count++;
106                         }
107                 }
108         }
109         
110         return count;
111         
112 }
113
114 /**************************************************************************************************/
115
116 inline vector<vector<double> > binomial(int maxOrder){
117         
118         vector<vector<double> > binomial(maxOrder+1);
119         
120     for(int i=0;i<=maxOrder;i++){
121                 binomial[i].resize(maxOrder+1);
122                 binomial[i][0]=1;
123                 binomial[0][i]=0;
124     }
125     binomial[0][0]=1;
126         
127     binomial[1][0]=1;
128     binomial[1][1]=1;
129         
130     for(int i=2;i<=maxOrder;i++){
131                 binomial[1][i]=0;
132     }
133         
134     for(int i=2;i<=maxOrder;i++){
135                 for(int j=1;j<=maxOrder;j++){
136                         if(i==j){       binomial[i][j]=1;                                                                       }
137                         if(j>i) {       binomial[i][j]=0;                                                                       }
138                         else    {       binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j];     }
139                 }
140     }
141         
142         return binomial;
143 }
144
145 /***********************************************************************/
146
147 inline string getRootName(string longName){
148  
149         string rootName = longName;
150         
151         if(longName.find_last_of(".") != longName.npos){
152                 int pos = longName.find_last_of('.')+1;
153                 rootName = longName.substr(0, pos);
154         }
155
156         return rootName;
157 }
158 /***********************************************************************/
159
160 inline string getSimpleName(string longName){
161  
162         string simpleName = longName;
163         
164         if(longName.find_last_of("/") != longName.npos){
165                 int pos = longName.find_last_of('/')+1;
166                 simpleName = longName.substr(pos, longName.length());
167         }
168
169         return simpleName;
170 }
171
172 /***********************************************************************/
173
174 inline string getPathName(string longName){
175  
176         string rootPathName = longName;
177         
178         if(longName.find_last_of("/") != longName.npos){
179                 int pos = longName.find_last_of('/')+1;
180                 rootPathName = longName.substr(0, pos);
181         }
182
183         return rootPathName;
184 }
185
186 /***********************************************************************/
187
188 inline int openInputFile(string fileName, ifstream& fileHandle){
189
190         fileHandle.open(fileName.c_str());
191         if(!fileHandle) {
192                 cerr << "Error: Could not open " << fileName << endl;
193                 return 1;
194         }
195         else {
196                 return 0;
197         }
198         
199 }
200
201 /***********************************************************************/
202
203 inline int openOutputFile(string fileName, ofstream& fileHandle){
204         
205         fileHandle.open(fileName.c_str(), ios::trunc);
206         if(!fileHandle) {
207                 cerr << "Error: Could not open " << fileName << endl;
208                 return 1;
209         }
210         else {
211                 return 0;
212         }
213
214 }
215
216 /***********************************************************************/
217
218 //This function parses the estimator options and puts them in a vector
219 inline void splitAtDash(string& estim, vector<string>& container) {
220         try {
221                 string individual;
222                 
223                 while (estim.find_first_of('-') != -1) {
224                         individual = estim.substr(0,estim.find_first_of('-'));
225                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
226                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
227                                 container.push_back(individual);
228                         }
229                 }
230                 //get last one
231                 container.push_back(estim);
232         }
233         catch(exception& e) {
234                 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
235                 exit(1);
236         }
237         catch(...) {
238                 cout << "An unknown error has occurred in the utilities class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
239                 exit(1);
240         }
241
242 }
243
244 /***********************************************************************/
245 //This function parses the label options and puts them in a set
246 inline void splitAtDash(string& estim, set<string>& container) {
247         try {
248                 string individual;
249                 
250                 while (estim.find_first_of('-') != -1) {
251                         individual = estim.substr(0,estim.find_first_of('-'));
252                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
253                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
254                                 container.insert(individual);
255                         }
256                 }
257                 //get last one
258                 container.insert(estim);
259         }
260         catch(exception& e) {
261                 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
262                 exit(1);
263         }
264         catch(...) {
265                 cout << "An unknown error has occurred in the utilities class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
266                 exit(1);
267         }
268
269 }
270 /***********************************************************************/
271 //This function parses the line options and puts them in a set
272 inline void splitAtDash(string& estim, set<int>& container) {
273         try {
274                 string individual;
275                 int lineNum;
276                 
277                 while (estim.find_first_of('-') != -1) {
278                         individual = estim.substr(0,estim.find_first_of('-'));
279                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
280                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
281                                 convert(individual, lineNum); //convert the string to int
282                                 container.insert(lineNum);
283                         }
284                 }
285                 //get last one
286                 convert(estim, lineNum); //convert the string to int
287                 container.insert(lineNum);
288         }
289         catch(exception& e) {
290                 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
291                 exit(1);
292         }
293         catch(...) {
294                 cout << "An unknown error has occurred in the utilities class function splitAtDash. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
295                 exit(1);
296         }
297
298 }
299 /***********************************************************************/
300
301 //This function splits up the various option parameters
302 inline void splitAtComma(string& prefix, string& suffix){
303         try {
304                 prefix = suffix.substr(0,suffix.find_first_of(','));
305                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
306                         suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
307                         string space = " ";
308                         while(suffix.at(0) == ' ')
309                                 suffix = suffix.substr(1, suffix.length());
310                 }
311
312         }
313         catch(exception& e) {
314                 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtComma. 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 utilities class function splitAtComma. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
319                 exit(1);
320         }
321
322 }
323 /***********************************************************************/
324
325 //This function separates the key value from the option value i.e. dist=96_...
326 inline void splitAtEquals(string& key, string& value){          
327         try {
328                 if(value.find_first_of('=') != -1){
329                         key = value.substr(0,value.find_first_of('='));
330                         if ((value.find_first_of('=')+1) <= value.length()) {
331                                 value = value.substr(value.find_first_of('=')+1, value.length());
332                         }
333                 }else{
334                         key = value;
335                         value = 1;
336                 }
337         }
338         catch(exception& e) {
339                 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
340                 exit(1);
341         }
342         catch(...) {
343                 cout << "An unknown error has occurred in the utilities class function splitAtEquals. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
344                 exit(1);
345         }
346
347 }
348 /**************************************************************************************************/
349
350 inline bool inUsersGroups(string groupname, vector<string> Groups) {
351         try {
352                 for (int i = 0; i < Groups.size(); i++) {
353                         if (groupname == Groups[i]) { return true; }
354                 }
355                 return false;
356         }
357         catch(exception& e) {
358                 cout << "Standard Error: " << e.what() << " has occurred in the utilities class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
359                 exit(1);
360         }
361         catch(...) {
362                 cout << "An unknown error has occurred in the utilities class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
363                 exit(1);
364         }
365 }
366
367 #endif