10 * Created by Sarah Westcott on 2/19/09.
11 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
15 /* This file contains all the standard incudes we use in the project as well as some common utilities. */
55 /***********************************************************************/
57 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
60 #include <sys/resource.h>
61 #include <sys/types.h>
66 #include <readline/readline.h>
67 #include <readline/history.h>
71 #include <conio.h> //allows unbuffered screen capture from stdin
72 #include <direct.h> //get cwd
82 #define exp(x) (exp((double) x))
83 #define sqrt(x) (sqrt((double) x))
84 #define log10(x) (log10((double) x))
85 #define log2(x) (log10(x)/log10(2))
86 #define isnan(x) ((x) != (x))
87 #define isinf(x) (fabs(x) == std::numeric_limits<double>::infinity())
90 typedef unsigned long ull;
91 typedef unsigned short intDist;
101 IntNode(int lv, int rv, IntNode* l, IntNode* r) : lvalue(lv), rvalue(rv), left(l), right(r) {};
116 prob = 0; reverseProb = 0;
118 diffPair(float p, float rp) {
123 /***********************************************************************/
127 PDistCell() : index(0), dist(0) {};
128 PDistCell(ull c, float d) : index(c), dist(d) {}
130 /***********************************************************************/
135 consTax() : name(""), taxonomy("unknown"), abundance(0) {};
136 consTax(string n, string t, int a) : name(n), taxonomy(t), abundance(a) {}
138 /***********************************************************************/
142 consTax2() : taxonomy("unknown"), abundance(0) {};
143 consTax2(string t, int a) : taxonomy(t), abundance(a) {}
145 /************************************************************/
149 int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
150 clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
152 /************************************************************/
158 seqDist(int s1, int s2, double d) : seq1(s1), seq2(s2), dist(d) {}
161 /************************************************************/
162 struct distlinePair {
167 /************************************************************/
168 struct seqPriorityNode {
173 seqPriorityNode(int n, string s, string nm) : numIdentical(n), seq(s), name(nm) {}
174 ~seqPriorityNode() {}
176 /***************************************************************/
177 struct spearmanRank {
181 spearmanRank(string n, float s) : name(n), score(s) {}
183 //***********************************************************************
184 inline bool compareIndexes(PDistCell left, PDistCell right){
185 return (left.index > right.index);
187 //********************************************************************************************************************
188 inline bool compareSpearman(spearmanRank left, spearmanRank right){
189 return (left.score < right.score);
191 //********************************************************************************************************************
192 //sorts highest to lowest
193 inline bool compareSeqPriorityNodes(seqPriorityNode left, seqPriorityNode right){
194 if (left.numIdentical > right.numIdentical) {
196 }else if (left.numIdentical == right.numIdentical) {
197 if (left.seq > right.seq) { return true; }
198 else { return false; }
203 /************************************************************/
204 //sorts lowest to highest
205 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
206 return (left.end < right.end);
208 //********************************************************************************************************************
209 //sorts lowest to highest
210 inline bool compareSequenceDistance(seqDist left, seqDist right){
211 return (left.dist < right.dist);
213 /***********************************************************************/
215 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
216 // works for now, but there should be a way to do it without killing the whole program
218 class BadConversion : public runtime_error {
220 BadConversion(const string& s) : runtime_error(s){ }
223 //**********************************************************************************************************************
225 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
229 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
230 throw BadConversion(s);
233 //**********************************************************************************************************************
234 template <typename T> int sgn(T val){ return (val > T(0)) - (val < T(0)); }
235 //**********************************************************************************************************************
238 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
242 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
250 //**********************************************************************************************************************
253 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
257 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
264 //**********************************************************************************************************************
266 string toString(const T&x){
274 //**********************************************************************************************************************
277 string toHex(const T&x){
286 //**********************************************************************************************************************
289 string toString(const T&x, int i){
294 output << fixed << x;
299 //**********************************************************************************************************************
302 T fromString(const string& s){
303 istringstream stream (s);
309 //**********************************************************************************************************************