]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
changes to dist.seqs and pairwise.seqs and cluster.classic no longer resizes
[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
40 //math
41 #include <cmath>
42 #include <math.h>
43 #include <algorithm>
44
45 //misc
46 #include <cerrno>
47 #include <ctime>
48 #include <limits>
49
50 #ifdef USE_MPI
51         #include "mpi.h"
52 #endif
53 /***********************************************************************/
54
55 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
56         #include <sys/wait.h>
57         #include <sys/time.h>
58         #include <sys/resource.h>
59         #include <unistd.h>
60         
61         #ifdef USE_READLINE
62                 #include <readline/readline.h>
63                 #include <readline/history.h>
64         #endif
65
66 #else
67         #include <conio.h> //allows unbuffered screen capture from stdin
68         #include <direct.h> //get cwd
69         #include <windows.h>
70         #include <psapi.h>
71
72 #endif
73
74 using namespace std;
75
76 #define exp(x) (exp((double) x))
77 #define sqrt(x) (sqrt((double) x))
78 #define log10(x) (log10((double) x))
79 #define log2(x) (log10(x)/log10(2))
80 #define isnan(x) ((x) != (x))
81 #define isinf(x) (fabs(x) == std::numeric_limits<double>::infinity())
82
83 typedef unsigned long ull;
84
85 struct IntNode {
86         int lvalue;
87         int rvalue;
88         int lcoef;
89         int rcoef;
90         IntNode* left;
91         IntNode* right;
92         
93         IntNode(int lv, int rv, IntNode* l, IntNode* r) : lvalue(lv), rvalue(rv), left(l), right(r) {};
94         IntNode() {};
95 };
96
97 struct ThreadNode {
98         int* pid;
99         IntNode* left;
100         IntNode* right;
101 };
102
103 /************************************************************/
104 struct clusterNode {
105         int numSeq;
106         int parent;
107         int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
108         clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
109 };
110 /************************************************************/
111 struct seqDist {
112         int seq1;
113         int seq2;
114         float dist;
115         seqDist() {}
116         seqDist(int s1, int s2, float d) : seq1(s1), seq2(s2), dist(d) {}
117         ~seqDist() {}
118 };
119 /************************************************************/
120 struct distlinePair {
121         int start;
122         int end;
123         
124 };
125 /************************************************************/
126 //sorts lowest to highest
127 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
128         return (left.end < right.end);  
129
130 //********************************************************************************************************************
131 //sorts lowest to highest
132 inline bool compareSequenceDistance(seqDist left, seqDist right){
133         return (left.dist < right.dist);        
134
135 /***********************************************************************/
136
137 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
138 // works for now, but there should be a way to do it without killing the whole program
139
140 class BadConversion : public runtime_error {
141 public:
142         BadConversion(const string& s) : runtime_error(s){ }
143 };
144
145 //**********************************************************************************************************************
146 template<typename T>
147 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
148         
149                 istringstream i(s);
150                 char c;
151                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
152                         throw BadConversion(s);
153         
154 }
155
156 //**********************************************************************************************************************
157
158 template<typename T>
159 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
160         
161                 istringstream i(s);
162                 char c;
163                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
164                 {
165                         return false;
166                 } 
167                 return true;
168         
169 }
170
171 //**********************************************************************************************************************
172
173 template<typename T>
174 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
175         
176                 istringstream i(s);
177                 char c;
178                 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
179                 {
180                         return false;
181                 } 
182                 return true;
183         
184 }
185 //**********************************************************************************************************************
186 template<typename T>
187 string toString(const T&x){
188         
189                 stringstream output;
190                 output << x;
191                 return output.str();
192         
193 }
194
195 //**********************************************************************************************************************
196
197 template<typename T>
198 string toHex(const T&x){
199         
200                 stringstream output;
201                 
202                 output << hex << x;
203
204                 return output.str();
205         
206 }
207 //**********************************************************************************************************************
208
209 template<typename T>
210 string toString(const T&x, int i){
211         
212                 stringstream output;
213                 
214                 output.precision(i);
215                 output << fixed << x;
216                 
217                 return output.str();
218         
219 }
220 //**********************************************************************************************************************
221
222 #endif
223