]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
added qvalues to metastats. fixed bug with chimera.uchime location. fixed windows...
[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 /************************************************************/
110 struct clusterNode {
111         int numSeq;
112         int parent;
113         int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
114         clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
115 };
116 /************************************************************/
117 struct seqDist {
118         int seq1;
119         int seq2;
120         float dist;
121         seqDist() {}
122         seqDist(int s1, int s2, float d) : seq1(s1), seq2(s2), dist(d) {}
123         ~seqDist() {}
124 };
125 /************************************************************/
126 struct distlinePair {
127         int start;
128         int end;
129         
130 };
131 /************************************************************/
132 struct seqPriorityNode {
133         int numIdentical;
134         string seq;
135         string name;
136         seqPriorityNode() {}
137         seqPriorityNode(int n, string s, string nm) : numIdentical(n), seq(s), name(nm) {}
138         ~seqPriorityNode() {}
139 };
140 /***************************************************************/
141 struct spearmanRank {
142         string name;
143         float score;
144         
145         spearmanRank(string n, float s) : name(n), score(s) {}
146 };
147 //********************************************************************************************************************
148 //sorts highest to lowest
149 inline bool compareSpearman(spearmanRank left, spearmanRank right){
150         return (left.score > right.score);      
151
152 //********************************************************************************************************************
153 //sorts highest to lowest
154 inline bool compareSeqPriorityNodes(seqPriorityNode left, seqPriorityNode right){
155         return (left.numIdentical > right.numIdentical);        
156
157 //********************************************************************************************************************
158 //sorts lowest to highest
159 inline bool compareSpearmanReverse(spearmanRank left, spearmanRank right){
160         return (left.score < right.score);      
161
162 /************************************************************/
163 //sorts lowest to highest
164 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
165         return (left.end < right.end);  
166
167 //********************************************************************************************************************
168 //sorts lowest to highest
169 inline bool compareSequenceDistance(seqDist left, seqDist right){
170         return (left.dist < right.dist);        
171
172 /***********************************************************************/
173
174 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
175 // works for now, but there should be a way to do it without killing the whole program
176
177 class BadConversion : public runtime_error {
178 public:
179         BadConversion(const string& s) : runtime_error(s){ }
180 };
181
182 //**********************************************************************************************************************
183 template<typename T>
184 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
185         
186                 istringstream i(s);
187                 char c;
188                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
189                         throw BadConversion(s);
190         
191 }
192 //**********************************************************************************************************************
193 template <typename T> int sgn(T val){ return (val > T(0)) - (val < T(0)); }
194 //**********************************************************************************************************************
195
196 template<typename T>
197 bool convertTestFloat(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                 {
203                         return false;
204                 } 
205                 return true;
206         
207 }
208
209 //**********************************************************************************************************************
210
211 template<typename T>
212 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
213         
214                 istringstream i(s);
215                 char c;
216                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
217                 {
218                         return false;
219                 } 
220                 return true;
221         
222 }
223 //**********************************************************************************************************************
224 template<typename T>
225 string toString(const T&x){
226         
227                 stringstream output;
228                 output << x;
229                 return output.str();
230         
231 }
232
233 //**********************************************************************************************************************
234
235 template<typename T>
236 string toHex(const T&x){
237         
238                 stringstream output;
239                 
240                 output << hex << x;
241
242                 return output.str();
243         
244 }
245 //**********************************************************************************************************************
246
247 template<typename T>
248 string toString(const T&x, int i){
249         
250                 stringstream output;
251                 
252                 output.precision(i);
253                 output << fixed << x;
254                 
255                 return output.str();
256         
257 }
258 //**********************************************************************************************************************
259
260 template<class T>
261 T fromString(const string& s){
262         istringstream stream (s);
263         T t;
264         stream >> t;
265         return t;
266 }
267
268 //**********************************************************************************************************************
269
270 #endif
271