]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
added make.lefse command. fixed bug in make.contigs with trimming reverse barcodes...
[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 spearmanRank {
178         string name;
179         float score;
180         
181         spearmanRank(string n, float s) : name(n), score(s) {}
182 };
183 //***********************************************************************
184 inline bool compareIndexes(PDistCell left, PDistCell right){
185         return (left.index > right.index);      
186 }
187 //********************************************************************************************************************
188 inline bool compareSpearman(spearmanRank left, spearmanRank right){
189         return (left.score < right.score);      
190
191 //********************************************************************************************************************
192 //sorts highest to lowest
193 inline bool compareSeqPriorityNodes(seqPriorityNode left, seqPriorityNode right){
194         if (left.numIdentical > right.numIdentical) {
195         return true;
196     }else if (left.numIdentical == right.numIdentical) {
197         if (left.seq > right.seq) { return true; }
198         else { return false; }
199     }
200     return false;       
201
202  
203 /************************************************************/
204 //sorts lowest to highest
205 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
206         return (left.end < right.end);  
207
208 //********************************************************************************************************************
209 //sorts lowest to highest
210 inline bool compareSequenceDistance(seqDist left, seqDist right){
211         return (left.dist < right.dist);        
212
213 /***********************************************************************/
214
215 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
216 // works for now, but there should be a way to do it without killing the whole program
217
218 class BadConversion : public runtime_error {
219 public:
220         BadConversion(const string& s) : runtime_error(s){ }
221 };
222
223 //**********************************************************************************************************************
224 template<typename T>
225 void convert(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                         throw BadConversion(s);
231         
232 }
233 //**********************************************************************************************************************
234 template <typename T> int sgn(T val){ return (val > T(0)) - (val < T(0)); }
235 //**********************************************************************************************************************
236
237 template<typename T>
238 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
239         
240                 istringstream i(s);
241                 char c;
242                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
243                 {
244                         return false;
245                 } 
246                 return true;
247         
248 }
249
250 //**********************************************************************************************************************
251
252 template<typename T>
253 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
254         
255                 istringstream i(s);
256                 char c;
257                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
258                 {
259                         return false;
260                 } 
261                 return true;
262         
263 }
264 //**********************************************************************************************************************
265 template<typename T>
266 string toString(const T&x){
267         
268                 stringstream output;
269                 output << x;
270                 return output.str();
271         
272 }
273
274 //**********************************************************************************************************************
275
276 template<typename T>
277 string toHex(const T&x){
278         
279                 stringstream output;
280                 
281                 output << hex << x;
282
283                 return output.str();
284         
285 }
286 //**********************************************************************************************************************
287
288 template<typename T>
289 string toString(const T&x, int i){
290         
291                 stringstream output;
292                 
293                 output.precision(i);
294                 output << fixed << x;
295                 
296                 return output.str();
297         
298 }
299 //**********************************************************************************************************************
300
301 template<class T>
302 T fromString(const string& s){
303         istringstream stream (s);
304         T t;
305         stream >> t;
306         return t;
307 }
308
309 //**********************************************************************************************************************
310
311 #endif
312