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) {
124 /**********************************************************/
125 struct CommonHeader {
126 unsigned int magicNumber;
128 unsigned long long indexOffset;
129 unsigned int indexLength;
130 unsigned int numReads;
131 unsigned short headerLength;
132 unsigned short keyLength;
133 unsigned short numFlowsPerRead;
134 int flogramFormatCode;
135 string flowChars; //length depends on number flow reads
136 string keySequence; //length depends on key length
138 CommonHeader(){ magicNumber=0; indexOffset=0; indexLength=0; numReads=0; headerLength=0; keyLength=0; numFlowsPerRead=0; flogramFormatCode='s'; }
141 /**********************************************************/
143 unsigned short headerLength;
144 unsigned short nameLength;
145 unsigned int numBases;
146 unsigned short clipQualLeft;
147 unsigned short clipQualRight;
148 unsigned short clipAdapterLeft;
149 unsigned short clipAdapterRight;
150 string name; //length depends on nameLength
155 Header() { headerLength=0; nameLength=0; numBases=0; clipQualLeft=0; clipQualRight=0; clipAdapterLeft=0; clipAdapterRight=0; }
158 /**********************************************************/
160 vector<unsigned short> flowgram;
161 vector<unsigned int> flowIndex;
163 vector<unsigned int> qualScores;
169 /***********************************************************************/
173 PDistCell() : index(0), dist(0) {};
174 PDistCell(ull c, float d) : index(c), dist(d) {}
176 /***********************************************************************/
181 consTax() : name(""), taxonomy("unknown"), abundance(0) {};
182 consTax(string n, string t, int a) : name(n), taxonomy(t), abundance(a) {}
184 /***********************************************************************/
188 consTax2() : taxonomy("unknown"), abundance(0) {};
189 consTax2(string t, int a) : taxonomy(t), abundance(a) {}
191 /************************************************************/
195 int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
196 clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
198 /************************************************************/
204 seqDist(int s1, int s2, double d) : seq1(s1), seq2(s2), dist(d) {}
207 /************************************************************/
208 struct distlinePair {
213 /************************************************************/
214 struct seqPriorityNode {
219 seqPriorityNode(int n, string s, string nm) : numIdentical(n), seq(s), name(nm) {}
220 ~seqPriorityNode() {}
222 /************************************************************/
227 compGroup(string s, string nm) : group1(s), group2(nm) {}
228 string getCombo() { return group1+"-"+group2; }
231 /***************************************************************/
232 struct spearmanRank {
236 spearmanRank(string n, float s) : name(n), score(s) {}
238 //***********************************************************************
239 inline bool compareIndexes(PDistCell left, PDistCell right){
240 return (left.index > right.index);
242 //********************************************************************************************************************
243 inline bool compareSpearman(spearmanRank left, spearmanRank right){
244 return (left.score < right.score);
246 //********************************************************************************************************************
247 //sorts highest to lowest
248 inline bool compareSeqPriorityNodes(seqPriorityNode left, seqPriorityNode right){
249 if (left.numIdentical > right.numIdentical) {
251 }else if (left.numIdentical == right.numIdentical) {
252 if (left.seq > right.seq) { return true; }
253 else { return false; }
258 /************************************************************/
259 //sorts lowest to highest
260 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
261 return (left.end < right.end);
263 //********************************************************************************************************************
264 //sorts lowest to highest
265 inline bool compareSequenceDistance(seqDist left, seqDist right){
266 return (left.dist < right.dist);
268 //********************************************************************************************************************
269 //returns sign of double
270 inline double sign(double temp){
272 if (temp > 0) { return 1.0; }
273 else if (temp < 0) { return -1.0; }
276 /***********************************************************************/
278 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
279 // works for now, but there should be a way to do it without killing the whole program
281 class BadConversion : public runtime_error {
283 BadConversion(const string& s) : runtime_error(s){ }
286 //**********************************************************************************************************************
288 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
292 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
293 throw BadConversion(s);
296 //**********************************************************************************************************************
297 template <typename T> int sgn(T val){ return (val > T(0)) - (val < T(0)); }
298 //**********************************************************************************************************************
301 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
305 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
313 //**********************************************************************************************************************
316 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
320 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
327 //**********************************************************************************************************************
329 string toString(const T&x){
337 //**********************************************************************************************************************
340 string toHex(const T&x){
349 //**********************************************************************************************************************
352 string toString(const T&x, int i){
357 output << fixed << x;
362 //**********************************************************************************************************************
365 T fromString(const string& s){
366 istringstream stream (s);
372 //**********************************************************************************************************************