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