]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
added kruskal.wallis command. added worked on make.lefse. working of lefse command...
[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 consTax{
132         string name;
133     string taxonomy;
134     int abundance;
135         consTax() :  name(""), taxonomy("unknown"), abundance(0) {};
136         consTax(string n, string t, int a) :  name(n), taxonomy(t), abundance(a) {}
137 };
138 /***********************************************************************/
139 struct consTax2{
140     string taxonomy;
141     int abundance;
142         consTax2() :  taxonomy("unknown"), abundance(0) {};
143         consTax2(string t, int a) :  taxonomy(t), abundance(a) {}
144 };
145 /************************************************************/
146 struct clusterNode {
147         int numSeq;
148         int parent;
149         int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
150         clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
151 };
152 /************************************************************/
153 struct seqDist {
154         int seq1;
155         int seq2;
156         double dist;
157         seqDist() {}
158         seqDist(int s1, int s2, double d) : seq1(s1), seq2(s2), dist(d) {}
159         ~seqDist() {}
160 };
161 /************************************************************/
162 struct distlinePair {
163         int start;
164         int end;
165         
166 };
167 /************************************************************/
168 struct seqPriorityNode {
169         int numIdentical;
170         string seq;
171         string name;
172         seqPriorityNode() {}
173         seqPriorityNode(int n, string s, string nm) : numIdentical(n), seq(s), name(nm) {}
174         ~seqPriorityNode() {}
175 };
176 /************************************************************/
177 struct compGroup {
178         string group1;
179         string group2;
180         compGroup() {}
181         compGroup(string s, string nm) : group1(s), group2(nm) {}
182     string getCombo() { return group1+"-"+group2; }
183         ~compGroup() {}
184 };
185 /***************************************************************/
186 struct spearmanRank {
187         string name;
188         float score;
189         
190         spearmanRank(string n, float s) : name(n), score(s) {}
191 };
192 //***********************************************************************
193 inline bool compareIndexes(PDistCell left, PDistCell right){
194         return (left.index > right.index);      
195 }
196 //********************************************************************************************************************
197 inline bool compareSpearman(spearmanRank left, spearmanRank right){
198         return (left.score < right.score);      
199
200 //********************************************************************************************************************
201 //sorts highest to lowest
202 inline bool compareSeqPriorityNodes(seqPriorityNode left, seqPriorityNode right){
203         if (left.numIdentical > right.numIdentical) {
204         return true;
205     }else if (left.numIdentical == right.numIdentical) {
206         if (left.seq > right.seq) { return true; }
207         else { return false; }
208     }
209     return false;       
210
211  
212 /************************************************************/
213 //sorts lowest to highest
214 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
215         return (left.end < right.end);  
216
217 //********************************************************************************************************************
218 //sorts lowest to highest
219 inline bool compareSequenceDistance(seqDist left, seqDist right){
220         return (left.dist < right.dist);        
221 }
222 //********************************************************************************************************************
223 //returns sign of double
224 inline double sign(double temp){
225         //find sign
226     if (temp > 0)       { return 1.0;   }
227     else if (temp < 0)  { return -1.0;  }
228     return 0;
229 }
230 /***********************************************************************/
231
232 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
233 // works for now, but there should be a way to do it without killing the whole program
234
235 class BadConversion : public runtime_error {
236 public:
237         BadConversion(const string& s) : runtime_error(s){ }
238 };
239
240 //**********************************************************************************************************************
241 template<typename T>
242 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
243         
244                 istringstream i(s);
245                 char c;
246                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
247                         throw BadConversion(s);
248         
249 }
250 //**********************************************************************************************************************
251 template <typename T> int sgn(T val){ return (val > T(0)) - (val < T(0)); }
252 //**********************************************************************************************************************
253
254 template<typename T>
255 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
256         
257                 istringstream i(s);
258                 char c;
259                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
260                 {
261                         return false;
262                 } 
263                 return true;
264         
265 }
266
267 //**********************************************************************************************************************
268
269 template<typename T>
270 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
271         
272                 istringstream i(s);
273                 char c;
274                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
275                 {
276                         return false;
277                 } 
278                 return true;
279         
280 }
281 //**********************************************************************************************************************
282 template<typename T>
283 string toString(const T&x){
284         
285                 stringstream output;
286                 output << x;
287                 return output.str();
288         
289 }
290
291 //**********************************************************************************************************************
292
293 template<typename T>
294 string toHex(const T&x){
295         
296                 stringstream output;
297                 
298                 output << hex << x;
299
300                 return output.str();
301         
302 }
303 //**********************************************************************************************************************
304
305 template<typename T>
306 string toString(const T&x, int i){
307         
308                 stringstream output;
309                 
310                 output.precision(i);
311                 output << fixed << x;
312                 
313                 return output.str();
314         
315 }
316 //**********************************************************************************************************************
317
318 template<class T>
319 T fromString(const string& s){
320         istringstream stream (s);
321         T t;
322         stream >> t;
323         return t;
324 }
325
326 //**********************************************************************************************************************
327
328 #endif
329