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