]> git.donarmstrong.com Git - mothur.git/blob - utilities.hpp
16e762c295708ddbc3c0cd4835cd4b60bc8e9162
[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
14 typedef unsigned long long ull;
15
16 /***********************************************************************/
17
18 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
19 // works for now, but there should be a way to do it without killing the whole program
20
21 class BadConversion : public runtime_error {
22 public:
23         BadConversion(const string& s) : runtime_error(s){ }
24 };
25
26 //**********************************************************************************************************************
27
28 template<typename T>
29 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
30         istringstream i(s);
31         char c;
32         if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
33                 throw BadConversion(s);
34 }
35
36 //**********************************************************************************************************************
37
38 template<typename T>
39 string toString(const T&x){
40     stringstream output;
41     output << x;
42     return output.str();
43 }
44
45 //**********************************************************************************************************************
46
47 template<typename T>
48 string toString(const T&x, int i){
49         stringstream output;
50         
51         output.precision(i);
52     output << fixed << x;
53         
54     return output.str();
55 }
56
57
58 /***********************************************************************/
59
60 inline void gobble(ifstream& f){
61         
62         char d;
63     while(isspace(d=f.get()))           {;}
64         f.putback(d);
65         
66 }
67
68 /***********************************************************************/
69
70 inline float roundDist(float dist, int precision){
71         
72         return int(dist * precision + 0.5)/float(precision);
73         
74 }
75
76 /***********************************************************************/
77
78 inline int getNumNames(string names){
79         
80         int count = 0;
81         
82         if(names != ""){
83                 count = 1;
84                 for(int i=0;i<names.size();i++){
85                         if(names[i] == ','){
86                                 count++;
87                         }
88                 }
89         }
90         
91         return count;
92         
93 }
94
95 /**************************************************************************************************/
96
97 inline vector<vector<double> > binomial(int maxOrder){
98         
99         vector<vector<double> > binomial(maxOrder+1);
100         
101     for(int i=0;i<=maxOrder;i++){
102                 binomial[i].resize(maxOrder+1);
103                 binomial[i][0]=1;
104                 binomial[0][i]=0;
105     }
106     binomial[0][0]=1;
107         
108     binomial[1][0]=1;
109     binomial[1][1]=1;
110         
111     for(int i=2;i<=maxOrder;i++){
112                 binomial[1][i]=0;
113     }
114         
115     for(int i=2;i<=maxOrder;i++){
116                 for(int j=1;j<=maxOrder;j++){
117                         if(i==j){       binomial[i][j]=1;                                                                       }
118                         if(j>i) {       binomial[i][j]=0;                                                                       }
119                         else    {       binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j];     }
120                 }
121     }
122         
123         return binomial;
124 }
125
126 /***********************************************************************/
127
128 inline string getRootName(string longName){
129  
130         string rootName = longName;
131         
132         if(longName.find_last_of(".") != longName.npos){
133                 int pos = longName.find_last_of('.')+1;
134                 rootName = longName.substr(0, pos);
135         }
136
137         return rootName;
138 }
139 /***********************************************************************/
140
141 inline string getSimpleName(string longName){
142  
143         string simpleName = longName;
144         
145         if(longName.find_last_of("/") != longName.npos){
146                 int pos = longName.find_last_of('/')+1;
147                 simpleName = longName.substr(pos, longName.length());
148         }
149
150         return simpleName;
151 }
152
153 /***********************************************************************/
154
155 inline string getPathName(string longName){
156  
157         string rootPathName = longName;
158         
159         if(longName.find_last_of("/") != longName.npos){
160                 int pos = longName.find_last_of('/')+1;
161                 rootPathName = longName.substr(0, pos);
162         }
163
164         return rootPathName;
165 }
166
167 /***********************************************************************/
168
169 inline int openInputFile(string fileName, ifstream& fileHandle){
170
171         fileHandle.open(fileName.c_str());
172         if(!fileHandle) {
173                 cerr << "Error: Could not open " << fileName << endl;
174                 return 1;
175         }
176         else {
177                 return 0;
178         }
179         
180 }
181
182 /***********************************************************************/
183
184 inline int openOutputFile(string fileName, ofstream& fileHandle){
185         
186         fileHandle.open(fileName.c_str(), ios::trunc);
187         if(!fileHandle) {
188                 cerr << "Error: Could not open " << fileName << endl;
189                 return 1;
190         }
191         else {
192                 return 0;
193         }
194
195 }
196
197 /***********************************************************************/
198
199 #endif