]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
fixed bug with trim.flows that was adding flow files names to the .flow.files file...
[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 //**********************************************************************************************************************
194
195 template<typename T>
196 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
197         
198                 istringstream i(s);
199                 char c;
200                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
201                 {
202                         return false;
203                 } 
204                 return true;
205         
206 }
207
208 //**********************************************************************************************************************
209
210 template<typename T>
211 bool convertTest(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 template<typename T>
224 string toString(const T&x){
225         
226                 stringstream output;
227                 output << x;
228                 return output.str();
229         
230 }
231
232 //**********************************************************************************************************************
233
234 template<typename T>
235 string toHex(const T&x){
236         
237                 stringstream output;
238                 
239                 output << hex << x;
240
241                 return output.str();
242         
243 }
244 //**********************************************************************************************************************
245
246 template<typename T>
247 string toString(const T&x, int i){
248         
249                 stringstream output;
250                 
251                 output.precision(i);
252                 output << fixed << x;
253                 
254                 return output.str();
255         
256 }
257 //**********************************************************************************************************************
258
259 template<class T>
260 T fromString(const string& s){
261         istringstream stream (s);
262         T t;
263         stream >> t;
264         return t;
265 }
266
267 //**********************************************************************************************************************
268
269 #endif
270