]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
Merge remote-tracking branch 'mothur/master'
[mothur.git] / mothur.h
1 #ifndef MOTHUR_H
2 #define MOTHUR_H
3
4
5
6 /*
7  *  mothur.h
8  *  Mothur
9  *
10  *  Created by Sarah Westcott on 2/19/09.
11  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
12  *
13  */
14
15 /* This file contains all the standard incudes we use in the project as well as some common utilities. */
16
17 //#include <cstddef>
18
19 //io libraries
20 #include <iostream>
21 #include <iomanip>
22 #include <fstream>
23 #include <sstream>
24 #include <signal.h>
25
26
27 //exception
28 #include <stdexcept>
29 #include <exception>
30 #include <cstdlib> 
31
32
33 //containers
34 #include <vector>
35 #include <set>
36 #include <map>
37 #include <string>
38 #include <list>
39 #include <string.h>
40
41 //math
42 #include <cmath>
43 #include <math.h>
44 #include <algorithm>
45 #include <numeric>
46
47 //misc
48 #include <cerrno>
49 #include <ctime>
50 #include <limits>
51
52 #ifdef USE_MPI
53         #include "mpi.h"
54 #endif
55 /***********************************************************************/
56
57 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
58         #include <sys/wait.h>
59         #include <sys/time.h>
60         #include <sys/resource.h>
61         #include <sys/types.h>
62         #include <sys/stat.h>
63         #include <unistd.h>
64         
65         #ifdef USE_READLINE
66                 #include <readline/readline.h>
67                 #include <readline/history.h>
68         #endif
69
70 #else
71         #include <conio.h> //allows unbuffered screen capture from stdin
72         #include <direct.h> //get cwd
73         #include <windows.h>
74         #include <psapi.h>
75         #include <direct.h>
76         #include <tchar.h>
77
78 #endif
79
80 using namespace std;
81
82 #define exp(x) (exp((double) x))
83 #define sqrt(x) (sqrt((double) x))
84 #define log10(x) (log10((double) x))
85 #define log2(x) (log10(x)/log10(2))
86 #define isnan(x) ((x) != (x))
87 #define isinf(x) (fabs(x) == std::numeric_limits<double>::infinity())
88
89
90 typedef unsigned long ull;
91 typedef unsigned short intDist;
92
93 struct IntNode {
94         int lvalue;
95         int rvalue;
96         int lcoef;
97         int rcoef;
98         IntNode* left;
99         IntNode* right;
100         
101         IntNode(int lv, int rv, IntNode* l, IntNode* r) : lvalue(lv), rvalue(rv), left(l), right(r) {};
102         IntNode() {};
103 };
104
105 struct ThreadNode {
106         int* pid;
107         IntNode* left;
108         IntNode* right;
109 };
110
111 struct diffPair {
112         float   prob;
113         float   reverseProb;
114         
115         diffPair() {
116                 prob = 0; reverseProb = 0;
117         }
118         diffPair(float p, float rp) {
119                 prob = p;
120                 reverseProb = rp;
121         }
122 };
123 /***********************************************************************/
124 struct PDistCell{
125         ull index;
126         float dist;
127         PDistCell() :  index(0), dist(0) {};
128         PDistCell(ull c, float d) :  index(c), dist(d) {}
129 };
130 /************************************************************/
131 struct clusterNode {
132         int numSeq;
133         int parent;
134         int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
135         clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
136 };
137 /************************************************************/
138 struct seqDist {
139         int seq1;
140         int seq2;
141         double dist;
142         seqDist() {}
143         seqDist(int s1, int s2, double d) : seq1(s1), seq2(s2), dist(d) {}
144         ~seqDist() {}
145 };
146 /************************************************************/
147 struct distlinePair {
148         int start;
149         int end;
150         
151 };
152 /************************************************************/
153 struct seqPriorityNode {
154         int numIdentical;
155         string seq;
156         string name;
157         seqPriorityNode() {}
158         seqPriorityNode(int n, string s, string nm) : numIdentical(n), seq(s), name(nm) {}
159         ~seqPriorityNode() {}
160 };
161 /***************************************************************/
162 struct spearmanRank {
163         string name;
164         float score;
165         
166         spearmanRank(string n, float s) : name(n), score(s) {}
167 };
168 //***********************************************************************
169 inline bool compareIndexes(PDistCell left, PDistCell right){
170         return (left.index > right.index);      
171 }
172 //********************************************************************************************************************
173 //sorts highest to lowest
174 inline bool compareSpearman(spearmanRank left, spearmanRank right){
175         return (left.score < right.score);      
176
177 //********************************************************************************************************************
178 //sorts highest to lowest
179 inline bool compareSeqPriorityNodes(seqPriorityNode left, seqPriorityNode right){
180         return (left.numIdentical > right.numIdentical);        
181
182 //********************************************************************************************************************
183 //sorts lowest to highest
184 inline bool compareSpearmanReverse(spearmanRank left, spearmanRank right){
185         return (left.score < right.score);      
186
187 /************************************************************/
188 //sorts lowest to highest
189 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
190         return (left.end < right.end);  
191
192 //********************************************************************************************************************
193 //sorts lowest to highest
194 inline bool compareSequenceDistance(seqDist left, seqDist right){
195         return (left.dist < right.dist);        
196
197 /***********************************************************************/
198
199 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
200 // works for now, but there should be a way to do it without killing the whole program
201
202 class BadConversion : public runtime_error {
203 public:
204         BadConversion(const string& s) : runtime_error(s){ }
205 };
206
207 //**********************************************************************************************************************
208 template<typename T>
209 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
210         
211                 istringstream i(s);
212                 char c;
213                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
214                         throw BadConversion(s);
215         
216 }
217 //**********************************************************************************************************************
218 template <typename T> int sgn(T val){ return (val > T(0)) - (val < T(0)); }
219 //**********************************************************************************************************************
220
221 template<typename T>
222 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
223         
224                 istringstream i(s);
225                 char c;
226                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
227                 {
228                         return false;
229                 } 
230                 return true;
231         
232 }
233
234 //**********************************************************************************************************************
235
236 template<typename T>
237 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
238         
239                 istringstream i(s);
240                 char c;
241                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
242                 {
243                         return false;
244                 } 
245                 return true;
246         
247 }
248 //**********************************************************************************************************************
249 template<typename T>
250 string toString(const T&x){
251         
252                 stringstream output;
253                 output << x;
254                 return output.str();
255         
256 }
257
258 //**********************************************************************************************************************
259
260 template<typename T>
261 string toHex(const T&x){
262         
263                 stringstream output;
264                 
265                 output << hex << x;
266
267                 return output.str();
268         
269 }
270 //**********************************************************************************************************************
271
272 template<typename T>
273 string toString(const T&x, int i){
274         
275                 stringstream output;
276                 
277                 output.precision(i);
278                 output << fixed << x;
279                 
280                 return output.str();
281         
282 }
283 //**********************************************************************************************************************
284
285 template<class T>
286 T fromString(const string& s){
287         istringstream stream (s);
288         T t;
289         stream >> t;
290         return t;
291 }
292
293 //**********************************************************************************************************************
294
295 #endif
296