]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
modified reportfile class
[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 spearmanRank {
130         string name;
131         float score;
132         
133         spearmanRank(string n, float s) : name(n), score(s) {}
134 };
135 //********************************************************************************************************************
136 //sorts highest to lowest
137 inline bool compareSpearman(spearmanRank left, spearmanRank right){
138         return (left.score > right.score);      
139
140 //********************************************************************************************************************
141 //sorts lowest to highest
142 inline bool compareSpearmanReverse(spearmanRank left, spearmanRank right){
143         return (left.score < right.score);      
144
145 /************************************************************/
146 //sorts lowest to highest
147 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
148         return (left.end < right.end);  
149
150 //********************************************************************************************************************
151 //sorts lowest to highest
152 inline bool compareSequenceDistance(seqDist left, seqDist right){
153         return (left.dist < right.dist);        
154
155 /***********************************************************************/
156
157 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
158 // works for now, but there should be a way to do it without killing the whole program
159
160 class BadConversion : public runtime_error {
161 public:
162         BadConversion(const string& s) : runtime_error(s){ }
163 };
164
165 //**********************************************************************************************************************
166 template<typename T>
167 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
168         
169                 istringstream i(s);
170                 char c;
171                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
172                         throw BadConversion(s);
173         
174 }
175
176 //**********************************************************************************************************************
177
178 template<typename T>
179 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
180         
181                 istringstream i(s);
182                 char c;
183                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
184                 {
185                         return false;
186                 } 
187                 return true;
188         
189 }
190
191 //**********************************************************************************************************************
192
193 template<typename T>
194 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
195         
196                 istringstream i(s);
197                 char c;
198                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
199                 {
200                         return false;
201                 } 
202                 return true;
203         
204 }
205 //**********************************************************************************************************************
206 template<typename T>
207 string toString(const T&x){
208         
209                 stringstream output;
210                 output << x;
211                 return output.str();
212         
213 }
214
215 //**********************************************************************************************************************
216
217 template<typename T>
218 string toHex(const T&x){
219         
220                 stringstream output;
221                 
222                 output << hex << x;
223
224                 return output.str();
225         
226 }
227 //**********************************************************************************************************************
228
229 template<typename T>
230 string toString(const T&x, int i){
231         
232                 stringstream output;
233                 
234                 output.precision(i);
235                 output << fixed << x;
236                 
237                 return output.str();
238         
239 }
240 //**********************************************************************************************************************
241
242 template<class T>
243 T fromString(const string& s){
244         istringstream stream (s);
245         T t;
246         stream >> t;
247         return t;
248 }
249
250 //**********************************************************************************************************************
251
252 #endif
253