]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
added paralellization for windows to dist.seqs and summary.seqs
[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 typedef unsigned long ull;
89
90 struct IntNode {
91         int lvalue;
92         int rvalue;
93         int lcoef;
94         int rcoef;
95         IntNode* left;
96         IntNode* right;
97         
98         IntNode(int lv, int rv, IntNode* l, IntNode* r) : lvalue(lv), rvalue(rv), left(l), right(r) {};
99         IntNode() {};
100 };
101
102 struct ThreadNode {
103         int* pid;
104         IntNode* left;
105         IntNode* right;
106 };
107
108 /************************************************************/
109 struct clusterNode {
110         int numSeq;
111         int parent;
112         int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
113         clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
114 };
115 /************************************************************/
116 struct seqDist {
117         int seq1;
118         int seq2;
119         float dist;
120         seqDist() {}
121         seqDist(int s1, int s2, float d) : seq1(s1), seq2(s2), dist(d) {}
122         ~seqDist() {}
123 };
124 /************************************************************/
125 struct distlinePair {
126         int start;
127         int end;
128         
129 };
130 /************************************************************/
131 struct seqPriorityNode {
132         int numIdentical;
133         string seq;
134         string name;
135         seqPriorityNode() {}
136         seqPriorityNode(int n, string s, string nm) : numIdentical(n), seq(s), name(nm) {}
137         ~seqPriorityNode() {}
138 };
139 /***************************************************************/
140 struct spearmanRank {
141         string name;
142         float score;
143         
144         spearmanRank(string n, float s) : name(n), score(s) {}
145 };
146 //********************************************************************************************************************
147 //sorts highest to lowest
148 inline bool compareSpearman(spearmanRank left, spearmanRank right){
149         return (left.score > right.score);      
150
151 //********************************************************************************************************************
152 //sorts highest to lowest
153 inline bool compareSeqPriorityNodes(seqPriorityNode left, seqPriorityNode right){
154         return (left.numIdentical > right.numIdentical);        
155
156 //********************************************************************************************************************
157 //sorts lowest to highest
158 inline bool compareSpearmanReverse(spearmanRank left, spearmanRank right){
159         return (left.score < right.score);      
160
161 /************************************************************/
162 //sorts lowest to highest
163 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
164         return (left.end < right.end);  
165
166 //********************************************************************************************************************
167 //sorts lowest to highest
168 inline bool compareSequenceDistance(seqDist left, seqDist right){
169         return (left.dist < right.dist);        
170
171 /***********************************************************************/
172
173 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
174 // works for now, but there should be a way to do it without killing the whole program
175
176 class BadConversion : public runtime_error {
177 public:
178         BadConversion(const string& s) : runtime_error(s){ }
179 };
180
181 //**********************************************************************************************************************
182 template<typename T>
183 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
184         
185                 istringstream i(s);
186                 char c;
187                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
188                         throw BadConversion(s);
189         
190 }
191
192 //**********************************************************************************************************************
193
194 template<typename T>
195 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
196         
197                 istringstream i(s);
198                 char c;
199                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
200                 {
201                         return false;
202                 } 
203                 return true;
204         
205 }
206
207 //**********************************************************************************************************************
208
209 template<typename T>
210 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
211         
212                 istringstream i(s);
213                 char c;
214                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
215                 {
216                         return false;
217                 } 
218                 return true;
219         
220 }
221 //**********************************************************************************************************************
222 template<typename T>
223 string toString(const T&x){
224         
225                 stringstream output;
226                 output << x;
227                 return output.str();
228         
229 }
230
231 //**********************************************************************************************************************
232
233 template<typename T>
234 string toHex(const T&x){
235         
236                 stringstream output;
237                 
238                 output << hex << x;
239
240                 return output.str();
241         
242 }
243 //**********************************************************************************************************************
244
245 template<typename T>
246 string toString(const T&x, int i){
247         
248                 stringstream output;
249                 
250                 output.precision(i);
251                 output << fixed << x;
252                 
253                 return output.str();
254         
255 }
256 //**********************************************************************************************************************
257
258 template<class T>
259 T fromString(const string& s){
260         istringstream stream (s);
261         T t;
262         stream >> t;
263         return t;
264 }
265
266 //**********************************************************************************************************************
267
268 #endif
269