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