]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
update .gitignore
[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 oligosPair {
215         string forward;
216         string reverse;
217         
218         oligosPair() { forward = ""; reverse = "";  }
219         oligosPair(string f, string r) : forward(f), reverse(r) {}
220         ~oligosPair() {}
221 };
222
223 /************************************************************/
224 struct seqPriorityNode {
225         int numIdentical;
226         string seq;
227         string name;
228         seqPriorityNode() {}
229         seqPriorityNode(int n, string s, string nm) : numIdentical(n), seq(s), name(nm) {}
230         ~seqPriorityNode() {}
231 };
232 /************************************************************/
233 struct compGroup {
234         string group1;
235         string group2;
236         compGroup() {}
237         compGroup(string s, string nm) : group1(s), group2(nm) {}
238     string getCombo() { return group1+"-"+group2; }
239         ~compGroup() {}
240 };
241 /***************************************************************/
242 struct spearmanRank {
243         string name;
244         float score;
245         
246         spearmanRank(string n, float s) : name(n), score(s) {}
247 };
248 //***********************************************************************
249 inline bool compareIndexes(PDistCell left, PDistCell right){
250         return (left.index > right.index);      
251 }
252 //********************************************************************************************************************
253 inline bool compareSpearman(spearmanRank left, spearmanRank right){
254         return (left.score < right.score);      
255 }
256 //********************************************************************************************************************
257 inline double max(double left, double right){
258     if (left > right) { return left; }
259     else { return right; }
260 }
261 //********************************************************************************************************************
262 inline double max(int left, double right){
263     double value = left;
264     if (left > right) { return value; }
265     else { return right; }
266 }
267 //********************************************************************************************************************
268 inline double max(double left, int right){
269     double value = right;
270     if (left > value) { return left; }
271     else { return value; }
272 }
273 //********************************************************************************************************************
274 //sorts highest to lowest
275 inline bool compareSeqPriorityNodes(seqPriorityNode left, seqPriorityNode right){
276         if (left.numIdentical > right.numIdentical) {
277         return true;
278     }else if (left.numIdentical == right.numIdentical) {
279         if (left.seq > right.seq) { return true; }
280         else { return false; }
281     }
282     return false;       
283
284  
285 /************************************************************/
286 //sorts lowest to highest
287 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
288         return (left.end < right.end);  
289
290 //********************************************************************************************************************
291 //sorts lowest to highest
292 inline bool compareSequenceDistance(seqDist left, seqDist right){
293         return (left.dist < right.dist);        
294 }
295 //********************************************************************************************************************
296 //returns sign of double
297 inline double sign(double temp){
298         //find sign
299     if (temp > 0)       { return 1.0;   }
300     else if (temp < 0)  { return -1.0;  }
301     return 0;
302 }
303 /***********************************************************************/
304
305 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
306 // works for now, but there should be a way to do it without killing the whole program
307
308 class BadConversion : public runtime_error {
309 public:
310         BadConversion(const string& s) : runtime_error(s){ }
311 };
312
313 //**********************************************************************************************************************
314 template<typename T>
315 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
316         
317                 istringstream i(s);
318                 char c;
319                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
320                         throw BadConversion(s);
321         
322 }
323 //**********************************************************************************************************************
324 template <typename T> int sgn(T val){ return (val > T(0)) - (val < T(0)); }
325 //**********************************************************************************************************************
326
327 template<typename T>
328 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
329         
330                 istringstream i(s);
331                 char c;
332                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
333                 {
334                         return false;
335                 } 
336                 return true;
337         
338 }
339
340 //**********************************************************************************************************************
341
342 template<typename T>
343 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
344         
345                 istringstream i(s);
346                 char c;
347                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
348                 {
349                         return false;
350                 } 
351                 return true;
352         
353 }
354 //**********************************************************************************************************************
355 template<typename T>
356 string toString(const T&x){
357         
358                 stringstream output;
359                 output << x;
360                 return output.str();
361         
362 }
363
364 //**********************************************************************************************************************
365
366 template<typename T>
367 string toHex(const T&x){
368         
369                 stringstream output;
370                 
371                 output << hex << x;
372
373                 return output.str();
374         
375 }
376 //**********************************************************************************************************************
377
378 template<typename T>
379 string toString(const T&x, int i){
380         
381                 stringstream output;
382                 
383                 output.precision(i);
384                 output << fixed << x;
385                 
386                 return output.str();
387         
388 }
389 //**********************************************************************************************************************
390
391 template<class T>
392 T fromString(const string& s){
393         istringstream stream (s);
394         T t;
395         stream >> t;
396         return t;
397 }
398
399 //**********************************************************************************************************************
400
401 #endif
402