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 /************************************************************/
218 oligosPair() { forward = ""; reverse = ""; }
219 oligosPair(string f, string r) : forward(f), reverse(r) {}
223 /************************************************************/
224 struct seqPriorityNode {
229 seqPriorityNode(int n, string s, string nm) : numIdentical(n), seq(s), name(nm) {}
230 ~seqPriorityNode() {}
232 /************************************************************/
237 compGroup(string s, string nm) : group1(s), group2(nm) {}
238 string getCombo() { return group1+"-"+group2; }
241 /***************************************************************/
242 struct spearmanRank {
246 spearmanRank(string n, float s) : name(n), score(s) {}
248 //***********************************************************************
249 inline bool compareIndexes(PDistCell left, PDistCell right){
250 return (left.index > right.index);
252 //********************************************************************************************************************
253 inline bool compareSpearman(spearmanRank left, spearmanRank right){
254 return (left.score < right.score);
256 //********************************************************************************************************************
257 inline double max(double left, double right){
258 if (left > right) { return left; }
259 else { return right; }
261 //********************************************************************************************************************
262 inline double max(int left, double right){
264 if (left > right) { return value; }
265 else { return right; }
267 //********************************************************************************************************************
268 inline double max(double left, int right){
269 double value = right;
270 if (left > value) { return left; }
271 else { return value; }
273 //********************************************************************************************************************
274 //sorts highest to lowest
275 inline bool compareSeqPriorityNodes(seqPriorityNode left, seqPriorityNode right){
276 if (left.numIdentical > right.numIdentical) {
278 }else if (left.numIdentical == right.numIdentical) {
279 if (left.seq > right.seq) { return true; }
280 else { return false; }
285 /************************************************************/
286 //sorts lowest to highest
287 inline bool compareDistLinePairs(distlinePair left, distlinePair right){
288 return (left.end < right.end);
290 //********************************************************************************************************************
291 //sorts lowest to highest
292 inline bool compareSequenceDistance(seqDist left, seqDist right){
293 return (left.dist < right.dist);
295 //********************************************************************************************************************
296 //returns sign of double
297 inline double sign(double temp){
299 if (temp > 0) { return 1.0; }
300 else if (temp < 0) { return -1.0; }
303 /***********************************************************************/
305 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
306 // works for now, but there should be a way to do it without killing the whole program
308 class BadConversion : public runtime_error {
310 BadConversion(const string& s) : runtime_error(s){ }
313 //**********************************************************************************************************************
315 void convert(const string& s, T& x, bool failIfLeftoverChars = true){
319 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
320 throw BadConversion(s);
323 //**********************************************************************************************************************
324 template <typename T> int sgn(T val){ return (val > T(0)) - (val < T(0)); }
325 //**********************************************************************************************************************
328 bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
332 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
340 //**********************************************************************************************************************
343 bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
347 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
354 //**********************************************************************************************************************
356 string toString(const T&x){
364 //**********************************************************************************************************************
367 string toHex(const T&x){
376 //**********************************************************************************************************************
379 string toString(const T&x, int i){
384 output << fixed << x;
389 //**********************************************************************************************************************
392 T fromString(const string& s){
393 istringstream stream (s);
399 //**********************************************************************************************************************