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 /************************************************************/
134 int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
135 clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
137 /************************************************************/
143 seqDist(int s1, int s2, double d) : seq1(s1), seq2(s2), dist(d) {}
146 /************************************************************/
147 struct distlinePair {
152 /************************************************************/
153 struct seqPriorityNode {
158 seqPriorityNode(int n, string s, string nm) : numIdentical(n), seq(s), name(nm) {}
159 ~seqPriorityNode() {}
161 /***************************************************************/
162 struct spearmanRank {
166 spearmanRank(string n, float s) : name(n), score(s) {}
168 //***********************************************************************
169 inline bool compareIndexes(PDistCell left, PDistCell right){
170 return (left.index > right.index);
172 //********************************************************************************************************************
173 //sorts highest to lowest
174 inline bool compareSpearman(spearmanRank left, spearmanRank right){
175 return (left.score < right.score);
177 //********************************************************************************************************************
178 //sorts highest to lowest
179 inline bool compareSeqPriorityNodes(seqPriorityNode left, seqPriorityNode right){
180 return (left.numIdentical > right.numIdentical);
182 //********************************************************************************************************************
183 //sorts lowest to highest
184 inline bool compareSpearmanReverse(spearmanRank left, spearmanRank right){
185 return (left.score < right.score);
187 /************************************************************/
188 //sorts lowest to highest
189 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
190 return (left.end < right.end);
192 //********************************************************************************************************************
193 //sorts lowest to highest
194 inline bool compareSequenceDistance(seqDist left, seqDist right){
195 return (left.dist < right.dist);
197 /***********************************************************************/
199 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
200 // works for now, but there should be a way to do it without killing the whole program
202 class BadConversion : public runtime_error {
204 BadConversion(const string& s) : runtime_error(s){ }
207 //**********************************************************************************************************************
209 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
213 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
214 throw BadConversion(s);
217 //**********************************************************************************************************************
218 template <typename T> int sgn(T val){ return (val > T(0)) - (val < T(0)); }
219 //**********************************************************************************************************************
222 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
226 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
234 //**********************************************************************************************************************
237 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
241 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
248 //**********************************************************************************************************************
250 string toString(const T&x){
258 //**********************************************************************************************************************
261 string toHex(const T&x){
270 //**********************************************************************************************************************
273 string toString(const T&x, int i){
278 output << fixed << x;
283 //**********************************************************************************************************************
286 T fromString(const string& s){
287 istringstream stream (s);
293 //**********************************************************************************************************************