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 if (left.numIdentical > right.numIdentical) {
182 }else if (left.numIdentical == right.numIdentical) {
183 if (left.seq > right.seq) { return true; }
184 else { return false; }
188 //********************************************************************************************************************
189 //sorts lowest to highest
190 inline bool compareSpearmanReverse(spearmanRank left, spearmanRank right){
191 return (left.score < right.score);
193 /************************************************************/
194 //sorts lowest to highest
195 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
196 return (left.end < right.end);
198 //********************************************************************************************************************
199 //sorts lowest to highest
200 inline bool compareSequenceDistance(seqDist left, seqDist right){
201 return (left.dist < right.dist);
203 /***********************************************************************/
205 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
206 // works for now, but there should be a way to do it without killing the whole program
208 class BadConversion : public runtime_error {
210 BadConversion(const string& s) : runtime_error(s){ }
213 //**********************************************************************************************************************
215 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
219 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
220 throw BadConversion(s);
223 //**********************************************************************************************************************
224 template <typename T> int sgn(T val){ return (val > T(0)) - (val < T(0)); }
225 //**********************************************************************************************************************
228 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
232 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
240 //**********************************************************************************************************************
243 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
247 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
254 //**********************************************************************************************************************
256 string toString(const T&x){
264 //**********************************************************************************************************************
267 string toHex(const T&x){
276 //**********************************************************************************************************************
279 string toString(const T&x, int i){
284 output << fixed << x;
289 //**********************************************************************************************************************
292 T fromString(const string& s){
293 istringstream stream (s);
299 //**********************************************************************************************************************