]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
fixed bug with shhh.flow from file path name in write functions, added "smart" featur...
[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
46 //misc
47 #include <cerrno>
48 #include <ctime>
49 #include <limits>
50
51 #ifdef USE_MPI
52         #include "mpi.h"
53 #endif
54 /***********************************************************************/
55
56 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
57         #include <sys/wait.h>
58         #include <sys/time.h>
59         #include <sys/resource.h>
60         #include <sys/types.h>
61         #include <sys/stat.h>
62         #include <unistd.h>
63         
64         #ifdef USE_READLINE
65                 #include <readline/readline.h>
66                 #include <readline/history.h>
67         #endif
68
69 #else
70         #include <conio.h> //allows unbuffered screen capture from stdin
71         #include <direct.h> //get cwd
72         #include <windows.h>
73         #include <psapi.h>
74         #include <direct.h>
75         #include <tchar.h>
76
77 #endif
78
79 using namespace std;
80
81 #define exp(x) (exp((double) x))
82 #define sqrt(x) (sqrt((double) x))
83 #define log10(x) (log10((double) x))
84 #define log2(x) (log10(x)/log10(2))
85 #define isnan(x) ((x) != (x))
86 #define isinf(x) (fabs(x) == std::numeric_limits<double>::infinity())
87
88
89 typedef unsigned long ull;
90
91 struct IntNode {
92         int lvalue;
93         int rvalue;
94         int lcoef;
95         int rcoef;
96         IntNode* left;
97         IntNode* right;
98         
99         IntNode(int lv, int rv, IntNode* l, IntNode* r) : lvalue(lv), rvalue(rv), left(l), right(r) {};
100         IntNode() {};
101 };
102
103 struct ThreadNode {
104         int* pid;
105         IntNode* left;
106         IntNode* right;
107 };
108
109 struct diffPair {
110         float   prob;
111         float   reverseProb;
112         
113         diffPair() {
114                 prob = 0; reverseProb = 0;
115         }
116         diffPair(float p, float rp) {
117                 prob = p;
118                 reverseProb = rp;
119         }
120 };
121
122 /************************************************************/
123 struct clusterNode {
124         int numSeq;
125         int parent;
126         int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
127         clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
128 };
129 /************************************************************/
130 struct seqDist {
131         int seq1;
132         int seq2;
133         float dist;
134         seqDist() {}
135         seqDist(int s1, int s2, float d) : seq1(s1), seq2(s2), dist(d) {}
136         ~seqDist() {}
137 };
138 /************************************************************/
139 struct distlinePair {
140         int start;
141         int end;
142         
143 };
144 /************************************************************/
145 struct seqPriorityNode {
146         int numIdentical;
147         string seq;
148         string name;
149         seqPriorityNode() {}
150         seqPriorityNode(int n, string s, string nm) : numIdentical(n), seq(s), name(nm) {}
151         ~seqPriorityNode() {}
152 };
153 /***************************************************************/
154 struct spearmanRank {
155         string name;
156         float score;
157         
158         spearmanRank(string n, float s) : name(n), score(s) {}
159 };
160 //********************************************************************************************************************
161 //sorts highest to lowest
162 inline bool compareSpearman(spearmanRank left, spearmanRank right){
163         return (left.score > right.score);      
164
165 //********************************************************************************************************************
166 //sorts highest to lowest
167 inline bool compareSeqPriorityNodes(seqPriorityNode left, seqPriorityNode right){
168         return (left.numIdentical > right.numIdentical);        
169
170 //********************************************************************************************************************
171 //sorts lowest to highest
172 inline bool compareSpearmanReverse(spearmanRank left, spearmanRank right){
173         return (left.score < right.score);      
174
175 /************************************************************/
176 //sorts lowest to highest
177 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
178         return (left.end < right.end);  
179
180 //********************************************************************************************************************
181 //sorts lowest to highest
182 inline bool compareSequenceDistance(seqDist left, seqDist right){
183         return (left.dist < right.dist);        
184
185 /***********************************************************************/
186
187 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
188 // works for now, but there should be a way to do it without killing the whole program
189
190 class BadConversion : public runtime_error {
191 public:
192         BadConversion(const string& s) : runtime_error(s){ }
193 };
194
195 //**********************************************************************************************************************
196 template<typename T>
197 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
198         
199                 istringstream i(s);
200                 char c;
201                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
202                         throw BadConversion(s);
203         
204 }
205 //**********************************************************************************************************************
206 template <typename T> int sgn(T val){ return (val > T(0)) - (val < T(0)); }
207 //**********************************************************************************************************************
208
209 template<typename T>
210 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
211         
212                 istringstream i(s);
213                 char c;
214                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
215                 {
216                         return false;
217                 } 
218                 return true;
219         
220 }
221
222 //**********************************************************************************************************************
223
224 template<typename T>
225 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
226         
227                 istringstream i(s);
228                 char c;
229                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
230                 {
231                         return false;
232                 } 
233                 return true;
234         
235 }
236 //**********************************************************************************************************************
237 template<typename T>
238 string toString(const T&x){
239         
240                 stringstream output;
241                 output << x;
242                 return output.str();
243         
244 }
245
246 //**********************************************************************************************************************
247
248 template<typename T>
249 string toHex(const T&x){
250         
251                 stringstream output;
252                 
253                 output << hex << x;
254
255                 return output.str();
256         
257 }
258 //**********************************************************************************************************************
259
260 template<typename T>
261 string toString(const T&x, int i){
262         
263                 stringstream output;
264                 
265                 output.precision(i);
266                 output << fixed << x;
267                 
268                 return output.str();
269         
270 }
271 //**********************************************************************************************************************
272
273 template<class T>
274 T fromString(const string& s){
275         istringstream stream (s);
276         T t;
277         stream >> t;
278         return t;
279 }
280
281 //**********************************************************************************************************************
282
283 #endif
284