]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
sffinfo bug with flow grams right index when clipQualRight=0
[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 inline bool compareSpearman(spearmanRank left, spearmanRank right){
174         return (left.score < right.score);      
175
176 //********************************************************************************************************************
177 //sorts highest to lowest
178 inline bool compareSeqPriorityNodes(seqPriorityNode left, seqPriorityNode right){
179         if (left.numIdentical > right.numIdentical) {
180         return true;
181     }else if (left.numIdentical == right.numIdentical) {
182         if (left.seq > right.seq) { return true; }
183         else { return false; }
184     }
185     return false;       
186
187  
188 /************************************************************/
189 //sorts lowest to highest
190 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
191         return (left.end < right.end);  
192
193 //********************************************************************************************************************
194 //sorts lowest to highest
195 inline bool compareSequenceDistance(seqDist left, seqDist right){
196         return (left.dist < right.dist);        
197
198 /***********************************************************************/
199
200 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
201 // works for now, but there should be a way to do it without killing the whole program
202
203 class BadConversion : public runtime_error {
204 public:
205         BadConversion(const string& s) : runtime_error(s){ }
206 };
207
208 //**********************************************************************************************************************
209 template<typename T>
210 void convert(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                         throw BadConversion(s);
216         
217 }
218 //**********************************************************************************************************************
219 template <typename T> int sgn(T val){ return (val > T(0)) - (val < T(0)); }
220 //**********************************************************************************************************************
221
222 template<typename T>
223 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
224         
225                 istringstream i(s);
226                 char c;
227                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
228                 {
229                         return false;
230                 } 
231                 return true;
232         
233 }
234
235 //**********************************************************************************************************************
236
237 template<typename T>
238 bool convertTest(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 template<typename T>
251 string toString(const T&x){
252         
253                 stringstream output;
254                 output << x;
255                 return output.str();
256         
257 }
258
259 //**********************************************************************************************************************
260
261 template<typename T>
262 string toHex(const T&x){
263         
264                 stringstream output;
265                 
266                 output << hex << x;
267
268                 return output.str();
269         
270 }
271 //**********************************************************************************************************************
272
273 template<typename T>
274 string toString(const T&x, int i){
275         
276                 stringstream output;
277                 
278                 output.precision(i);
279                 output << fixed << x;
280                 
281                 return output.str();
282         
283 }
284 //**********************************************************************************************************************
285
286 template<class T>
287 T fromString(const string& s){
288         istringstream stream (s);
289         T t;
290         stream >> t;
291         return t;
292 }
293
294 //**********************************************************************************************************************
295
296 #endif
297