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. */
49 /***********************************************************************/
51 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
56 #include <readline/readline.h>
57 #include <readline/history.h>
60 //#include <readline/readline.h>
61 //#include <readline/history.h>
63 #include <conio.h> //allows unbuffered screen capture from stdin
68 #define exp(x) (exp((double) x))
69 #define sqrt(x) (sqrt((double) x))
70 #define log10(x) (log10((double) x))
71 #define log2(x) (log10(x)/log10(2))
72 #define isnan(x) ((x) != (x))
73 #define isinf(x) (fabs(x) == std::numeric_limits<double>::infinity())
76 typedef unsigned long ull;
86 IntNode(int lv, int rv, IntNode* l, IntNode* r) : lvalue(lv), rvalue(rv), left(l), right(r) {};
96 /************************************************************/
100 int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
101 clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
103 /************************************************************/
109 seqDist(int s1, int s2, float d) : seq1(s1), seq2(s2), dist(d) {}
112 //********************************************************************************************************************
113 //sorts lowest to highest
114 inline bool compareSequenceDistance(seqDist left, seqDist right){
115 return (left.dist < right.dist);
117 /***********************************************************************/
119 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
120 // works for now, but there should be a way to do it without killing the whole program
122 class BadConversion : public runtime_error {
124 BadConversion(const string& s) : runtime_error(s){ }
127 //**********************************************************************************************************************
130 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
133 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
134 throw BadConversion(s);
137 //**********************************************************************************************************************
140 inline bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
143 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
150 //**********************************************************************************************************************
153 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
156 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
158 cout << "unable to be converted into an integer.\n" << endl;
164 //**********************************************************************************************************************
167 string toString(const T&x){
173 //**********************************************************************************************************************
176 string toHex(const T&x){
183 //**********************************************************************************************************************
186 string toString(const T&x, int i){
190 output << fixed << x;
194 /***********************************************************************/
196 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
198 fileHandle.open(fileName.c_str(), ios::app);
200 cout << "Error: Could not open " << fileName << endl;
208 /***********************************************************************/
210 inline void gobble(istream& f){
213 while(isspace(d=f.get())) {;}
217 /***********************************************************************/
219 inline string getline(ifstream& fileHandle) {
224 while (!fileHandle.eof()) {
226 char c = fileHandle.get();
228 //are you at the end of the line
229 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
236 catch(exception& e) {
237 cout << "Error in mothur function getline" << endl;
242 /**************************************************************************************************/
244 inline void mothurOut(string message) {
247 string logFileName = "mothur.logFile";
248 openOutputFileAppend(logFileName, out);
255 catch(exception& e) {
256 cout << "Error in mothur class mothurOut" << endl;
260 /**************************************************************************************************/
262 inline void mothurOut(string message, string precision) {
265 string logFileName = "mothur.logFile";
266 openOutputFileAppend(logFileName, out);
268 cout << precision << message;
269 out << precision << message;
273 catch(exception& e) {
274 cout << "Error in mothur class mothurOut" << endl;
279 /**************************************************************************************************/
281 inline void mothurOutEndLine() {
284 string logFileName = "mothur.logFile";
285 openOutputFileAppend(logFileName, out);
292 catch(exception& e) {
293 cout << "error in mothur mothurOutEndLine" << endl;
299 /**************************************************************************************************/
301 inline void errorOut(exception& e, string object, string function) {
303 mothurOut("Error: ");
304 mothurOut(toString(e.what()));
305 mothurOut(" has occurred in the " + object + " class function " + function + ". Please contact Pat Schloss at mothur.bugs@gmail.com, and be sure to include the mothur.logFile with your inquiry.");
310 /***********************************************************************/
312 inline bool isTrue(string f){
314 if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
315 else { return false; }
318 /***********************************************************************/
320 inline float roundDist(float dist, int precision){
322 return int(dist * precision + 0.5)/float(precision);
326 /***********************************************************************/
328 inline int getNumNames(string names){
334 for(int i=0;i<names.size();i++){
345 /**************************************************************************************************/
347 inline vector<vector<double> > binomial(int maxOrder){
349 vector<vector<double> > binomial(maxOrder+1);
351 for(int i=0;i<=maxOrder;i++){
352 binomial[i].resize(maxOrder+1);
361 for(int i=2;i<=maxOrder;i++){
365 for(int i=2;i<=maxOrder;i++){
366 for(int j=1;j<=maxOrder;j++){
367 if(i==j){ binomial[i][j]=1; }
368 if(j>i) { binomial[i][j]=0; }
369 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
376 /***********************************************************************/
378 inline string getRootName(string longName){
380 string rootName = longName;
382 if(longName.find_last_of(".") != longName.npos){
383 int pos = longName.find_last_of('.')+1;
384 rootName = longName.substr(0, pos);
389 /***********************************************************************/
391 inline string getSimpleName(string longName){
393 string simpleName = longName;
395 if(longName.find_last_of("/") != longName.npos){
396 int pos = longName.find_last_of('/')+1;
397 simpleName = longName.substr(pos, longName.length());
403 /***********************************************************************/
405 inline int factorial(int num){
408 for (int i = 1; i <= num; i++) {
414 /**************************************************************************************************
416 double min(double x, double y)
422 /***********************************************************************/
424 inline string getPathName(string longName){
426 string rootPathName = longName;
428 if(longName.find_last_of('/') != longName.npos){
429 int pos = longName.find_last_of('/')+1;
430 rootPathName = longName.substr(0, pos);
436 /***********************************************************************/
438 inline string getExtension(string longName){
440 string extension = longName;
442 if(longName.find_last_of('.') != longName.npos){
443 int pos = longName.find_last_of('.');
444 extension = longName.substr(pos, longName.length());
449 /***********************************************************************/
450 inline bool isBlank(string fileName){
453 fileHandle.open(fileName.c_str());
455 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
458 //check for blank file
460 if (fileHandle.eof()) { fileHandle.close(); return true; }
464 /***********************************************************************/
466 inline int openInputFile(string fileName, ifstream& fileHandle, string m){
468 fileHandle.open(fileName.c_str());
470 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
474 //check for blank file
480 /***********************************************************************/
482 inline int openInputFile(string fileName, ifstream& fileHandle){
484 fileHandle.open(fileName.c_str());
486 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
490 //check for blank file
492 if (fileHandle.eof()) { mothurOut(fileName + " is blank. Please correct."); mothurOutEndLine(); return 1; }
499 /***********************************************************************/
501 inline int openOutputFile(string fileName, ofstream& fileHandle){
503 fileHandle.open(fileName.c_str(), ios::trunc);
505 mothurOut("Error: Could not open " + fileName); mothurOutEndLine();
514 /***********************************************************************/
516 inline int getNumSeqs(ifstream& file){
518 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
523 /***********************************************************************/
525 inline bool inVector(string member, vector<string> group){
527 for (int i = 0; i < group.size(); i++) {
528 if (group[i] == member) { return true; }
533 /***********************************************************************/
535 //This function parses the estimator options and puts them in a vector
536 inline void splitAtDash(string& estim, vector<string>& container) {
540 while (estim.find_first_of('-') != -1) {
541 individual = estim.substr(0,estim.find_first_of('-'));
542 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
543 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
544 container.push_back(individual);
548 container.push_back(estim);
550 catch(exception& e) {
551 errorOut(e, "mothur", "splitAtDash");
556 /***********************************************************************/
557 //This function parses the label options and puts them in a set
558 inline void splitAtDash(string& estim, set<string>& container) {
562 while (estim.find_first_of('-') != -1) {
563 individual = estim.substr(0,estim.find_first_of('-'));
564 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
565 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
566 container.insert(individual);
570 container.insert(estim);
572 catch(exception& e) {
573 errorOut(e, "mothur", "splitAtDash");
577 /***********************************************************************/
578 //This function parses the line options and puts them in a set
579 inline void splitAtDash(string& estim, set<int>& container) {
584 while (estim.find_first_of('-') != -1) {
585 individual = estim.substr(0,estim.find_first_of('-'));
586 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
587 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
588 convert(individual, lineNum); //convert the string to int
589 container.insert(lineNum);
593 convert(estim, lineNum); //convert the string to int
594 container.insert(lineNum);
596 catch(exception& e) {
597 errorOut(e, "mothur", "splitAtDash");
601 /***********************************************************************/
602 //This function parses the a string and puts peices in a vector
603 inline void splitAtComma(string& estim, vector<string>& container) {
607 while (estim.find_first_of(',') != -1) {
608 individual = estim.substr(0,estim.find_first_of(','));
609 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
610 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
611 container.push_back(individual);
615 container.push_back(estim);
617 catch(exception& e) {
618 errorOut(e, "mothur", "splitAtComma");
622 /***********************************************************************/
624 //This function splits up the various option parameters
625 inline void splitAtComma(string& prefix, string& suffix){
627 prefix = suffix.substr(0,suffix.find_first_of(','));
628 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
629 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
631 while(suffix.at(0) == ' ')
632 suffix = suffix.substr(1, suffix.length());
636 catch(exception& e) {
637 errorOut(e, "mothur", "splitAtComma");
641 /***********************************************************************/
643 //This function separates the key value from the option value i.e. dist=96_...
644 inline void splitAtEquals(string& key, string& value){
646 if(value.find_first_of('=') != -1){
647 key = value.substr(0,value.find_first_of('='));
648 if ((value.find_first_of('=')+1) <= value.length()) {
649 value = value.substr(value.find_first_of('=')+1, value.length());
656 catch(exception& e) {
657 errorOut(e, "mothur", "splitAtEquals");
661 /**************************************************************************************************/
663 inline bool inUsersGroups(string groupname, vector<string> Groups) {
665 for (int i = 0; i < Groups.size(); i++) {
666 if (groupname == Groups[i]) { return true; }
670 catch(exception& e) {
671 errorOut(e, "mothur", "inUsersGroups");
676 /**************************************************************************************************/
678 inline void mothurOutJustToLog(string message) {
681 string logFileName = "mothur.logFile";
682 openOutputFileAppend(logFileName, out);
688 catch(exception& e) {
689 errorOut(e, "mothur", "mothurOutJustToLog");
695 /**************************************************************************************************/
697 inline void mothurOut(float num) {
700 string logFileName = "mothur.logFile";
701 openOutputFileAppend(logFileName, out);
708 catch(exception& e) {
709 cout << "Error in mothur class mothurOut float" << endl;
713 /***********************************************************************/
714 inline void mothurOut(double value) {
717 string logFileName = "mothur.logFile";
718 openOutputFileAppend(logFileName, out);
725 catch(exception& e) {
726 cout << "Error in mothur class mothurOut double" << endl;
731 /***********************************************************************/
732 //this function determines if the user has given us labels that are smaller than the given label.
733 //if so then it returns true so that the calling function can run the previous valid distance.
734 //it's a "smart" distance function. It also checks for invalid labels.
735 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
737 set<string>::iterator it;
738 vector<float> orderFloat;
739 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
740 map<string, float>::iterator it2;
742 bool smaller = false;
744 //unique is the smallest line
745 if (label == "unique") { return false; }
746 else { convert(label, labelFloat); }
748 //go through users set and make them floats
749 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
752 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
754 orderFloat.push_back(temp);
756 }else if (*it == "unique") {
757 orderFloat.push_back(-1.0);
758 userMap["unique"] = -1.0;
760 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
761 userLabels.erase(*it);
767 sort(orderFloat.begin(), orderFloat.end());
769 /*************************************************/
770 //is this label bigger than any of the users labels
771 /*************************************************/
773 //loop through order until you find a label greater than label
774 for (int i = 0; i < orderFloat.size(); i++) {
775 if (orderFloat[i] < labelFloat) {
777 if (orderFloat[i] == -1) {
778 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
779 userLabels.erase("unique");
782 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
784 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
785 if (it2->second == orderFloat[i]) {
787 //remove small labels
792 if (errorOff == "") { mothurOut(s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
794 //since they are sorted once you find a bigger one stop looking
801 catch(exception& e) {
802 errorOut(e, "mothur", "anyLabelsToProcess");
807 /**************************************************************************************************/
808 inline void appendFiles(string temp, string filename) {
813 //open output file in append mode
814 openOutputFileAppend(filename, output);
815 openInputFile(temp, input);
817 while(char c = input.get()){
818 if(input.eof()) { break; }
819 else { output << c; }
825 catch(exception& e) {
826 errorOut(e, "mothur", "appendFiles");
831 /**************************************************************************************************/
832 inline string sortFile(string distFile){
834 string outfile = getRootName(distFile) + "sorted.dist";
836 //if you can, use the unix sort since its been optimized for years
837 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
838 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
839 system(command.c_str());
840 #else //you are stuck with my best attempt...
841 //windows sort does not have a way to specify a column, only a character in the line
842 //since we cannot assume that the distance will always be at the the same character location on each line
843 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
845 //read in file line by file and put distance first
846 string tempDistFile = distFile + ".temp";
849 openInputFile(distFile, input);
850 openOutputFile(tempDistFile, output);
852 string firstName, secondName;
855 input >> firstName >> secondName >> dist;
856 output << dist << '\t' << firstName << '\t' << secondName << endl;
863 //sort using windows sort
864 string tempOutfile = outfile + ".temp";
865 string command = "sort " + tempDistFile + " /O " + tempOutfile;
866 system(command.c_str());
868 //read in sorted file and put distance at end again
870 openInputFile(tempOutfile, input2);
871 openOutputFile(outfile, output);
874 input2 >> dist >> firstName >> secondName;
875 output << firstName << '\t' << secondName << '\t' << dist << endl;
882 remove(tempDistFile.c_str());
883 remove(tempOutfile.c_str());
888 catch(exception& e) {
889 errorOut(e, "mothur", "sortFile");
893 /**************************************************************************************************/