]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
added mantel command
[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/stat.h>
61         #include <unistd.h>
62         
63         #ifdef USE_READLINE
64                 #include <readline/readline.h>
65                 #include <readline/history.h>
66         #endif
67
68 #else
69         #include <conio.h> //allows unbuffered screen capture from stdin
70         #include <direct.h> //get cwd
71         #include <windows.h>
72         #include <psapi.h>
73
74 #endif
75
76 using namespace std;
77
78 #define exp(x) (exp((double) x))
79 #define sqrt(x) (sqrt((double) x))
80 #define log10(x) (log10((double) x))
81 #define log2(x) (log10(x)/log10(2))
82 #define isnan(x) ((x) != (x))
83 #define isinf(x) (fabs(x) == std::numeric_limits<double>::infinity())
84
85 typedef unsigned long ull;
86
87 struct IntNode {
88         int lvalue;
89         int rvalue;
90         int lcoef;
91         int rcoef;
92         IntNode* left;
93         IntNode* right;
94         
95         IntNode(int lv, int rv, IntNode* l, IntNode* r) : lvalue(lv), rvalue(rv), left(l), right(r) {};
96         IntNode() {};
97 };
98
99 struct ThreadNode {
100         int* pid;
101         IntNode* left;
102         IntNode* right;
103 };
104
105 /************************************************************/
106 struct clusterNode {
107         int numSeq;
108         int parent;
109         int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
110         clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
111 };
112 /************************************************************/
113 struct seqDist {
114         int seq1;
115         int seq2;
116         float dist;
117         seqDist() {}
118         seqDist(int s1, int s2, float d) : seq1(s1), seq2(s2), dist(d) {}
119         ~seqDist() {}
120 };
121 /************************************************************/
122 struct distlinePair {
123         int start;
124         int end;
125         
126 };
127 /***************************************************************/
128 struct spearmanRank {
129         string name;
130         float score;
131         
132         spearmanRank(string n, float s) : name(n), score(s) {}
133 };
134 //********************************************************************************************************************
135 //sorts highest to lowest
136 inline bool compareSpearman(spearmanRank left, spearmanRank right){
137         return (left.score > right.score);      
138
139 //********************************************************************************************************************
140 //sorts lowest to highest
141 inline bool compareSpearmanReverse(spearmanRank left, spearmanRank right){
142         return (left.score < right.score);      
143
144 /************************************************************/
145 //sorts lowest to highest
146 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
147         return (left.end < right.end);  
148
149 //********************************************************************************************************************
150 //sorts lowest to highest
151 inline bool compareSequenceDistance(seqDist left, seqDist right){
152         return (left.dist < right.dist);        
153
154 /***********************************************************************/
155
156 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
157 // works for now, but there should be a way to do it without killing the whole program
158
159 class BadConversion : public runtime_error {
160 public:
161         BadConversion(const string& s) : runtime_error(s){ }
162 };
163
164 //**********************************************************************************************************************
165 template<typename T>
166 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
167         
168                 istringstream i(s);
169                 char c;
170                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
171                         throw BadConversion(s);
172         
173 }
174
175 //**********************************************************************************************************************
176
177 template<typename T>
178 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
179         
180                 istringstream i(s);
181                 char c;
182                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
183                 {
184                         return false;
185                 } 
186                 return true;
187         
188 }
189
190 //**********************************************************************************************************************
191
192 template<typename T>
193 bool convertTest(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 template<typename T>
206 string toString(const T&x){
207         
208                 stringstream output;
209                 output << x;
210                 return output.str();
211         
212 }
213
214 //**********************************************************************************************************************
215
216 template<typename T>
217 string toHex(const T&x){
218         
219                 stringstream output;
220                 
221                 output << hex << x;
222
223                 return output.str();
224         
225 }
226 //**********************************************************************************************************************
227
228 template<typename T>
229 string toString(const T&x, int i){
230         
231                 stringstream output;
232                 
233                 output.precision(i);
234                 output << fixed << x;
235                 
236                 return output.str();
237         
238 }
239 //**********************************************************************************************************************
240
241 #endif
242