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 /************************************************************/
181 compGroup(string s, string nm) : group1(s), group2(nm) {}
182 string getCombo() { return group1+"-"+group2; }
185 /***************************************************************/
186 struct spearmanRank {
190 spearmanRank(string n, float s) : name(n), score(s) {}
192 //***********************************************************************
193 inline bool compareIndexes(PDistCell left, PDistCell right){
194 return (left.index > right.index);
196 //********************************************************************************************************************
197 inline bool compareSpearman(spearmanRank left, spearmanRank right){
198 return (left.score < right.score);
200 //********************************************************************************************************************
201 //sorts highest to lowest
202 inline bool compareSeqPriorityNodes(seqPriorityNode left, seqPriorityNode right){
203 if (left.numIdentical > right.numIdentical) {
205 }else if (left.numIdentical == right.numIdentical) {
206 if (left.seq > right.seq) { return true; }
207 else { return false; }
212 /************************************************************/
213 //sorts lowest to highest
214 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
215 return (left.end < right.end);
217 //********************************************************************************************************************
218 //sorts lowest to highest
219 inline bool compareSequenceDistance(seqDist left, seqDist right){
220 return (left.dist < right.dist);
222 //********************************************************************************************************************
223 //returns sign of double
224 inline double sign(double temp){
226 if (temp > 0) { return 1.0; }
227 else if (temp < 0) { return -1.0; }
230 /***********************************************************************/
232 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
233 // works for now, but there should be a way to do it without killing the whole program
235 class BadConversion : public runtime_error {
237 BadConversion(const string& s) : runtime_error(s){ }
240 //**********************************************************************************************************************
242 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
246 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
247 throw BadConversion(s);
250 //**********************************************************************************************************************
251 template <typename T> int sgn(T val){ return (val > T(0)) - (val < T(0)); }
252 //**********************************************************************************************************************
255 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
259 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
267 //**********************************************************************************************************************
270 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
274 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
281 //**********************************************************************************************************************
283 string toString(const T&x){
291 //**********************************************************************************************************************
294 string toHex(const T&x){
303 //**********************************************************************************************************************
306 string toString(const T&x, int i){
311 output << fixed << x;
316 //**********************************************************************************************************************
319 T fromString(const string& s){
320 istringstream stream (s);
326 //**********************************************************************************************************************