]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
fixes while testing 1.33.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 /**********************************************************/
125 struct CommonHeader {
126         unsigned int magicNumber;
127         string version;
128         unsigned long long indexOffset;
129         unsigned int indexLength;
130         unsigned int numReads;
131         unsigned short headerLength;
132         unsigned short keyLength;
133         unsigned short numFlowsPerRead;
134         int flogramFormatCode;
135         string flowChars; //length depends on number flow reads
136         string keySequence; //length depends on key length
137         
138         CommonHeader(){ magicNumber=0; indexOffset=0; indexLength=0; numReads=0; headerLength=0; keyLength=0; numFlowsPerRead=0; flogramFormatCode='s'; }
139         ~CommonHeader() { }
140 };
141 /**********************************************************/
142 struct Header {
143         unsigned short headerLength;
144         unsigned short nameLength;
145         unsigned int numBases;
146         unsigned short clipQualLeft;
147         unsigned short clipQualRight;
148         unsigned short clipAdapterLeft;
149         unsigned short clipAdapterRight;
150         string name; //length depends on nameLength
151         string timestamp;
152         string region;
153         string xy;
154         
155         Header() { headerLength=0; nameLength=0; numBases=0; clipQualLeft=0; clipQualRight=0; clipAdapterLeft=0; clipAdapterRight=0; }
156         ~Header() { }
157 };
158 /**********************************************************/
159 struct seqRead {
160         vector<unsigned short> flowgram;
161         vector<unsigned int> flowIndex;
162         string bases;
163         vector<unsigned int> qualScores;
164         
165         seqRead() { }
166         ~seqRead() { }
167 };
168
169 /***********************************************************************/
170 struct PDistCell{
171         ull index;
172         float dist;
173         PDistCell() :  index(0), dist(0) {};
174         PDistCell(ull c, float d) :  index(c), dist(d) {}
175 };
176 /***********************************************************************/
177 struct consTax{
178         string name;
179     string taxonomy;
180     int abundance;
181         consTax() :  name(""), taxonomy("unknown"), abundance(0) {};
182         consTax(string n, string t, int a) :  name(n), taxonomy(t), abundance(a) {}
183 };
184 /***********************************************************************/
185 struct consTax2{
186     string taxonomy;
187     int abundance;
188         consTax2() :  taxonomy("unknown"), abundance(0) {};
189         consTax2(string t, int a) :  taxonomy(t), abundance(a) {}
190 };
191 /************************************************************/
192 struct clusterNode {
193         int numSeq;
194         int parent;
195         int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
196         clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
197 };
198 /************************************************************/
199 struct seqDist {
200         int seq1;
201         int seq2;
202         double dist;
203         seqDist() {}
204         seqDist(int s1, int s2, double d) : seq1(s1), seq2(s2), dist(d) {}
205         ~seqDist() {}
206 };
207 /************************************************************/
208 struct distlinePair {
209         int start;
210         int end;
211         
212 };
213 /************************************************************/
214 struct seqPriorityNode {
215         int numIdentical;
216         string seq;
217         string name;
218         seqPriorityNode() {}
219         seqPriorityNode(int n, string s, string nm) : numIdentical(n), seq(s), name(nm) {}
220         ~seqPriorityNode() {}
221 };
222 /************************************************************/
223 struct compGroup {
224         string group1;
225         string group2;
226         compGroup() {}
227         compGroup(string s, string nm) : group1(s), group2(nm) {}
228     string getCombo() { return group1+"-"+group2; }
229         ~compGroup() {}
230 };
231 /***************************************************************/
232 struct spearmanRank {
233         string name;
234         float score;
235         
236         spearmanRank(string n, float s) : name(n), score(s) {}
237 };
238 //***********************************************************************
239 inline bool compareIndexes(PDistCell left, PDistCell right){
240         return (left.index > right.index);      
241 }
242 //********************************************************************************************************************
243 inline bool compareSpearman(spearmanRank left, spearmanRank right){
244         return (left.score < right.score);      
245
246 //********************************************************************************************************************
247 //sorts highest to lowest
248 inline bool compareSeqPriorityNodes(seqPriorityNode left, seqPriorityNode right){
249         if (left.numIdentical > right.numIdentical) {
250         return true;
251     }else if (left.numIdentical == right.numIdentical) {
252         if (left.seq > right.seq) { return true; }
253         else { return false; }
254     }
255     return false;       
256
257  
258 /************************************************************/
259 //sorts lowest to highest
260 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
261         return (left.end < right.end);  
262
263 //********************************************************************************************************************
264 //sorts lowest to highest
265 inline bool compareSequenceDistance(seqDist left, seqDist right){
266         return (left.dist < right.dist);        
267 }
268 //********************************************************************************************************************
269 //returns sign of double
270 inline double sign(double temp){
271         //find sign
272     if (temp > 0)       { return 1.0;   }
273     else if (temp < 0)  { return -1.0;  }
274     return 0;
275 }
276 /***********************************************************************/
277
278 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
279 // works for now, but there should be a way to do it without killing the whole program
280
281 class BadConversion : public runtime_error {
282 public:
283         BadConversion(const string& s) : runtime_error(s){ }
284 };
285
286 //**********************************************************************************************************************
287 template<typename T>
288 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
289         
290                 istringstream i(s);
291                 char c;
292                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
293                         throw BadConversion(s);
294         
295 }
296 //**********************************************************************************************************************
297 template <typename T> int sgn(T val){ return (val > T(0)) - (val < T(0)); }
298 //**********************************************************************************************************************
299
300 template<typename T>
301 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
302         
303                 istringstream i(s);
304                 char c;
305                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
306                 {
307                         return false;
308                 } 
309                 return true;
310         
311 }
312
313 //**********************************************************************************************************************
314
315 template<typename T>
316 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
317         
318                 istringstream i(s);
319                 char c;
320                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
321                 {
322                         return false;
323                 } 
324                 return true;
325         
326 }
327 //**********************************************************************************************************************
328 template<typename T>
329 string toString(const T&x){
330         
331                 stringstream output;
332                 output << x;
333                 return output.str();
334         
335 }
336
337 //**********************************************************************************************************************
338
339 template<typename T>
340 string toHex(const T&x){
341         
342                 stringstream output;
343                 
344                 output << hex << x;
345
346                 return output.str();
347         
348 }
349 //**********************************************************************************************************************
350
351 template<typename T>
352 string toString(const T&x, int i){
353         
354                 stringstream output;
355                 
356                 output.precision(i);
357                 output << fixed << x;
358                 
359                 return output.str();
360         
361 }
362 //**********************************************************************************************************************
363
364 template<class T>
365 T fromString(const string& s){
366         istringstream stream (s);
367         T t;
368         stream >> t;
369         return t;
370 }
371
372 //**********************************************************************************************************************
373
374 #endif
375