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. */
53 /***********************************************************************/
55 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
58 #include <sys/resource.h>
62 #include <readline/readline.h>
63 #include <readline/history.h>
67 #include <conio.h> //allows unbuffered screen capture from stdin
68 #include <direct.h> //get cwd
76 #define exp(x) (exp((double) x))
77 #define sqrt(x) (sqrt((double) x))
78 #define log10(x) (log10((double) x))
79 #define log2(x) (log10(x)/log10(2))
80 #define isnan(x) ((x) != (x))
81 #define isinf(x) (fabs(x) == std::numeric_limits<double>::infinity())
83 typedef unsigned long ull;
93 IntNode(int lv, int rv, IntNode* l, IntNode* r) : lvalue(lv), rvalue(rv), left(l), right(r) {};
103 /************************************************************/
107 int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
108 clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
110 /************************************************************/
116 seqDist(int s1, int s2, float d) : seq1(s1), seq2(s2), dist(d) {}
119 //********************************************************************************************************************
120 //sorts lowest to highest
121 inline bool compareSequenceDistance(seqDist left, seqDist right){
122 return (left.dist < right.dist);
124 /***********************************************************************/
126 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
127 // works for now, but there should be a way to do it without killing the whole program
129 class BadConversion : public runtime_error {
131 BadConversion(const string& s) : runtime_error(s){ }
134 //**********************************************************************************************************************
137 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
140 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
141 throw BadConversion(s);
144 //**********************************************************************************************************************
147 inline bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
150 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
157 //**********************************************************************************************************************
160 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
163 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
165 cout << "unable to be converted into an integer.\n" << endl;
171 //**********************************************************************************************************************
174 string toString(const T&x){
180 //**********************************************************************************************************************
183 string toHex(const T&x){
190 //**********************************************************************************************************************
193 string toString(const T&x, int i){
197 output << fixed << x;
201 /***********************************************************************/
202 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
204 fileHandle.open(fileName.c_str(), ios::app);
206 cout << "Error: Could not open " << fileName << endl;
214 /***********************************************************************/
216 inline void gobble(istream& f){
219 while(isspace(d=f.get())) {;}
223 /***********************************************************************/
225 inline void gobble(istringstream& f){
228 while(isspace(d=f.get())) {;}
233 /***********************************************************************/
235 inline string getline(istringstream& fileHandle) {
240 while (!fileHandle.eof()) {
242 char c = fileHandle.get();
244 //are you at the end of the line
245 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
252 catch(exception& e) {
253 cout << "Error in mothur function getline" << endl;
257 /***********************************************************************/
259 inline string getline(ifstream& fileHandle) {
264 while (!fileHandle.eof()) {
266 char c = fileHandle.get();
268 //are you at the end of the line
269 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
276 catch(exception& e) {
277 cout << "Error in mothur function getline" << endl;
281 /***********************************************************************/
283 inline bool isTrue(string f){
285 if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) { return true; }
286 else { return false; }
289 /***********************************************************************/
291 inline float roundDist(float dist, int precision){
293 return int(dist * precision + 0.5)/float(precision);
296 /***********************************************************************/
298 inline float ceilDist(float dist, int precision){
300 return int(ceil(dist * precision))/float(precision);
304 /***********************************************************************/
306 inline int getNumNames(string names){
312 for(int i=0;i<names.size();i++){
323 /**************************************************************************************************/
325 inline vector<vector<double> > binomial(int maxOrder){
327 vector<vector<double> > binomial(maxOrder+1);
329 for(int i=0;i<=maxOrder;i++){
330 binomial[i].resize(maxOrder+1);
339 for(int i=2;i<=maxOrder;i++){
343 for(int i=2;i<=maxOrder;i++){
344 for(int j=1;j<=maxOrder;j++){
345 if(i==j){ binomial[i][j]=1; }
346 if(j>i) { binomial[i][j]=0; }
347 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
354 /***********************************************************************/
356 inline string getRootName(string longName){
358 string rootName = longName;
360 if(longName.find_last_of(".") != longName.npos){
361 int pos = longName.find_last_of('.')+1;
362 rootName = longName.substr(0, pos);
367 /***********************************************************************/
369 inline string getSimpleName(string longName){
371 string simpleName = longName;
374 found=longName.find_last_of("/\\");
376 if(found != longName.npos){
377 simpleName = longName.substr(found+1);
380 //if(longName.find_last_of("/") != longName.npos){
381 // int pos = longName.find_last_of('/')+1;
382 // simpleName = longName.substr(pos, longName.length());
388 /***********************************************************************/
390 inline int factorial(int num){
393 for (int i = 1; i <= num; i++) {
399 /**************************************************************************************************
401 double min(double x, double y)
407 /***********************************************************************/
409 inline string getPathName(string longName){
411 string rootPathName = longName;
413 if(longName.find_last_of("/\\") != longName.npos){
414 int pos = longName.find_last_of("/\\")+1;
415 rootPathName = longName.substr(0, pos);
420 /***********************************************************************/
422 inline string hasPath(string longName){
427 found=longName.find_last_of("~/\\");
429 if(found != longName.npos){
430 path = longName.substr(0, found+1);
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 cout << "Error: Could not open " << fileName << endl;
458 //check for blank file
460 if (fileHandle.eof()) { fileHandle.close(); return true; }
464 /***********************************************************************/
466 inline string getFullPathName(string fileName){
469 string path = hasPath(fileName);
473 if (path == "") { return fileName; } //its a simple name
474 else { //we need to complete the pathname
475 // ex. ../../../filename
476 // cwd = /user/work/desktop
479 //get current working directory
480 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
482 if (path.find("~") != -1) { //go to home directory
483 string homeDir = getenv ("HOME");
484 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
487 if (path.rfind("./") == -1) { return fileName; } //already complete name
488 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
490 char* cwdpath = new char[1024];
493 cwdpath=getcwd(cwdpath,size);
499 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
501 //break apart the current working directory
503 while (simpleCWD.find_first_of('/') != -1) {
504 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
505 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
508 //get last one // ex. ../../../filename = /user/work/desktop/filename
509 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
512 int index = dirs.size()-1;
514 while((pos = path.rfind("./")) != -1) { //while you don't have a complete path
515 if (pos == 0) { break; //you are at the end
516 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
517 path = path.substr(0, pos-1);
519 if (index == 0) { break; }
520 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
521 path = path.substr(0, pos);
522 }else if (pos == 1) { break; //you are at the end
523 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
526 for (int i = index; i >= 0; i--) {
527 newFileName = dirs[i] + "/" + newFileName;
530 newFileName = "/" + newFileName;
534 if (path.find("~") != -1) { //go to home directory
535 string homeDir = getenv ("HOMEPATH");
536 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
539 if (path.rfind(".\\") == -1) { return fileName; } //already complete name
540 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
542 char *cwdpath = NULL;
543 cwdpath = getcwd(NULL, 0); // or _getcwd
544 if ( cwdpath != NULL) { cwd = cwdpath; }
547 //break apart the current working directory
549 while (cwd.find_first_of('\\') != -1) {
550 string dir = cwd.substr(0,cwd.find_first_of('\\'));
551 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
556 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
558 int index = dirs.size()-1;
560 while((pos = path.rfind(".\\")) != -1) { //while you don't have a complete path
561 if (pos == 0) { break; //you are at the end
562 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
563 path = path.substr(0, pos-1);
565 if (index == 0) { break; }
566 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
567 path = path.substr(0, pos);
568 }else if (pos == 1) { break; //you are at the end
569 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
572 for (int i = index; i >= 0; i--) {
573 newFileName = dirs[i] + "\\" + newFileName;
582 catch(exception& e) {
583 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function getFullPathName. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
587 /***********************************************************************/
589 inline int openInputFile(string fileName, ifstream& fileHandle, string m){
592 string completeFileName = getFullPathName(fileName);
594 fileHandle.open(completeFileName.c_str());
598 //check for blank file
603 /***********************************************************************/
605 inline int openInputFile(string fileName, ifstream& fileHandle){
608 string completeFileName = getFullPathName(fileName);
610 fileHandle.open(completeFileName.c_str());
612 cout << "Error: Could not open " << completeFileName << endl;
616 //check for blank file
618 if (fileHandle.eof()) { cout << completeFileName << " is blank. Please correct." << endl; }
624 /***********************************************************************/
626 inline int renameFile(string oldName, string newName){
629 int exist = openInputFile(newName, inTest, "");
631 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
632 if (exist == 0) { //you could open it so you want to delete it
634 string command = "rm " + newName;
635 system(command.c_str());
638 string command = "mv " + oldName + " " + newName;
639 system(command.c_str());
641 remove(newName.c_str());
642 int renameOk = rename(oldName.c_str(), newName.c_str());
647 /***********************************************************************/
649 inline int openOutputFile(string fileName, ofstream& fileHandle){
651 string completeFileName = getFullPathName(fileName);
653 fileHandle.open(completeFileName.c_str(), ios::trunc);
655 cout << "Error: Could not open " << completeFileName << endl;
664 /***********************************************************************/
666 inline int getNumSeqs(ifstream& file){
668 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
673 /***********************************************************************/
674 inline void getNumSeqs(ifstream& file, int& numSeqs){
679 input = getline(file);
680 if (input.length() != 0) {
681 if(input[0] == '>'){ numSeqs++; }
686 /***********************************************************************/
688 inline bool inVector(string member, vector<string> group){
690 for (int i = 0; i < group.size(); i++) {
691 if (group[i] == member) { return true; }
696 /***********************************************************************/
698 //This function parses the estimator options and puts them in a vector
699 inline void splitAtChar(string& estim, vector<string>& container, char symbol) {
703 while (estim.find_first_of(symbol) != -1) {
704 individual = estim.substr(0,estim.find_first_of(symbol));
705 if ((estim.find_first_of(symbol)+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
706 estim = estim.substr(estim.find_first_of(symbol)+1, estim.length());
707 container.push_back(individual);
711 container.push_back(estim);
713 catch(exception& e) {
714 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
719 /***********************************************************************/
721 //This function parses the estimator options and puts them in a vector
722 inline void splitAtDash(string& estim, vector<string>& container) {
726 while (estim.find_first_of('-') != -1) {
727 individual = estim.substr(0,estim.find_first_of('-'));
728 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
729 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
730 container.push_back(individual);
734 container.push_back(estim);
736 catch(exception& e) {
737 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
742 /***********************************************************************/
743 //This function parses the label options and puts them in a set
744 inline void splitAtDash(string& estim, set<string>& container) {
748 while (estim.find_first_of('-') != -1) {
749 individual = estim.substr(0,estim.find_first_of('-'));
750 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
751 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
752 container.insert(individual);
756 container.insert(estim);
758 catch(exception& e) {
759 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
763 /***********************************************************************/
764 //This function parses the line options and puts them in a set
765 inline void splitAtDash(string& estim, set<int>& container) {
770 while (estim.find_first_of('-') != -1) {
771 individual = estim.substr(0,estim.find_first_of('-'));
772 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
773 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
774 convert(individual, lineNum); //convert the string to int
775 container.insert(lineNum);
779 convert(estim, lineNum); //convert the string to int
780 container.insert(lineNum);
782 catch(exception& e) {
783 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
787 /***********************************************************************/
788 //This function parses the a string and puts peices in a vector
789 inline void splitAtComma(string& estim, vector<string>& container) {
793 while (estim.find_first_of(',') != -1) {
794 individual = estim.substr(0,estim.find_first_of(','));
795 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
796 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
797 container.push_back(individual);
801 container.push_back(estim);
803 catch(exception& e) {
804 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtComma. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
808 /***********************************************************************/
810 //This function splits up the various option parameters
811 inline void splitAtComma(string& prefix, string& suffix){
813 prefix = suffix.substr(0,suffix.find_first_of(','));
814 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
815 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
817 while(suffix.at(0) == ' ')
818 suffix = suffix.substr(1, suffix.length());
822 catch(exception& e) {
823 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtComma. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
827 /***********************************************************************/
829 //This function separates the key value from the option value i.e. dist=96_...
830 inline void splitAtEquals(string& key, string& value){
832 if(value.find_first_of('=') != -1){
833 key = value.substr(0,value.find_first_of('='));
834 if ((value.find_first_of('=')+1) <= value.length()) {
835 value = value.substr(value.find_first_of('=')+1, value.length());
842 catch(exception& e) {
843 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtEquals. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
848 /**************************************************************************************************/
850 inline bool inUsersGroups(string groupname, vector<string> Groups) {
852 for (int i = 0; i < Groups.size(); i++) {
853 if (groupname == Groups[i]) { return true; }
857 catch(exception& e) {
858 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function inUsersGroups. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
862 /**************************************************************************************************/
863 //returns true if any of the strings in first vector are in second vector
864 inline bool inUsersGroups(vector<string> groupnames, vector<string> Groups) {
867 for (int i = 0; i < groupnames.size(); i++) {
868 if (inUsersGroups(groupnames[i], Groups)) { return true; }
872 catch(exception& e) {
873 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function inUsersGroups. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
877 /***********************************************************************/
878 //this function determines if the user has given us labels that are smaller than the given label.
879 //if so then it returns true so that the calling function can run the previous valid distance.
880 //it's a "smart" distance function. It also checks for invalid labels.
881 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
884 set<string>::iterator it;
885 vector<float> orderFloat;
886 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
887 map<string, float>::iterator it2;
889 bool smaller = false;
891 //unique is the smallest line
892 if (label == "unique") { return false; }
894 if (convertTestFloat(label, labelFloat)) {
895 convert(label, labelFloat);
896 }else { //cant convert
901 //go through users set and make them floats
902 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
905 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
907 orderFloat.push_back(temp);
909 }else if (*it == "unique") {
910 orderFloat.push_back(-1.0);
911 userMap["unique"] = -1.0;
913 if (errorOff == "") { cout << *it << " is not a valid label." << endl; }
914 userLabels.erase(*it);
920 sort(orderFloat.begin(), orderFloat.end());
922 /*************************************************/
923 //is this label bigger than any of the users labels
924 /*************************************************/
926 //loop through order until you find a label greater than label
927 for (int i = 0; i < orderFloat.size(); i++) {
928 if (orderFloat[i] < labelFloat) {
930 if (orderFloat[i] == -1) {
931 if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
932 userLabels.erase("unique");
935 if (errorOff == "") { cout << "Your file does not include the label " << endl; }
937 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
938 if (it2->second == orderFloat[i]) {
940 //remove small labels
945 if (errorOff == "") {cout << s << ". I will use the next smallest distance. " << endl; }
947 //since they are sorted once you find a bigger one stop looking
954 catch(exception& e) {
955 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function anyLabelsToProcess. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
960 /**************************************************************************************************/
961 inline void appendFiles(string temp, string filename) {
966 //open output file in append mode
967 openOutputFileAppend(filename, output);
968 int ableToOpen = openInputFile(temp, input, "no error");
970 if (ableToOpen == 0) { //you opened it
971 while(char c = input.get()){
972 if(input.eof()) { break; }
973 else { output << c; }
980 catch(exception& e) {
981 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function appendFiles. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
986 /**************************************************************************************************/
987 inline string sortFile(string distFile, string outputDir){
990 //if (outputDir == "") { outputDir += hasPath(distFile); }
991 string outfile = getRootName(distFile) + "sorted.dist";
994 //if you can, use the unix sort since its been optimized for years
995 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
996 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
997 system(command.c_str());
998 #else //you are stuck with my best attempt...
999 //windows sort does not have a way to specify a column, only a character in the line
1000 //since we cannot assume that the distance will always be at the the same character location on each line
1001 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
1003 //read in file line by file and put distance first
1004 string tempDistFile = distFile + ".temp";
1007 openInputFile(distFile, input);
1008 openOutputFile(tempDistFile, output);
1010 string firstName, secondName;
1013 input >> firstName >> secondName >> dist;
1014 output << dist << '\t' << firstName << '\t' << secondName << endl;
1021 //sort using windows sort
1022 string tempOutfile = outfile + ".temp";
1023 string command = "sort " + tempDistFile + " /O " + tempOutfile;
1024 system(command.c_str());
1026 //read in sorted file and put distance at end again
1028 openInputFile(tempOutfile, input2);
1029 openOutputFile(outfile, output);
1032 input2 >> dist >> firstName >> secondName;
1033 output << firstName << '\t' << secondName << '\t' << dist << endl;
1040 remove(tempDistFile.c_str());
1041 remove(tempOutfile.c_str());
1046 catch(exception& e) {
1047 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function sortfile. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
1051 /**************************************************************************************************/
1052 inline vector<unsigned long int> setFilePosFasta(string filename, int& num) {
1054 vector<unsigned long int> positions;
1056 openInputFile(filename, inFASTA);
1059 while(!inFASTA.eof()){
1060 input = getline(inFASTA);
1061 if (input.length() != 0) {
1062 if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
1064 gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
1068 num = positions.size();
1073 //get num bytes in file
1074 pFile = fopen (filename.c_str(),"rb");
1075 if (pFile==NULL) perror ("Error opening file");
1077 fseek (pFile, 0, SEEK_END);
1082 unsigned long int size = positions[(positions.size()-1)];
1084 openInputFile(filename, in);
1088 while(char c = in.get()){
1089 if(in.eof()) { break; }
1094 positions.push_back(size);
1098 /**************************************************************************************************/
1099 inline vector<unsigned long int> setFilePosEachLine(string filename, int& num) {
1101 vector<unsigned long int> positions;
1103 openInputFile(filename, in);
1107 unsigned long int lastpos = in.tellg();
1108 input = getline(in);
1109 if (input.length() != 0) {
1110 unsigned long int pos = in.tellg();
1111 if (pos != -1) { positions.push_back(pos - input.length() - 1); }
1112 else { positions.push_back(lastpos); }
1114 gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
1118 num = positions.size();
1121 unsigned long int size;
1123 //get num bytes in file
1124 pFile = fopen (filename.c_str(),"rb");
1125 if (pFile==NULL) perror ("Error opening file");
1127 fseek (pFile, 0, SEEK_END);
1132 positions.push_back(size);
1136 /**************************************************************************************************/
1138 inline vector<unsigned long int> divideFile(string filename, int& proc) {
1141 vector<unsigned long int> filePos;
1142 filePos.push_back(0);
1145 unsigned long int size;
1147 //get num bytes in file
1148 pFile = fopen (filename.c_str(),"rb");
1149 if (pFile==NULL) perror ("Error opening file");
1151 fseek (pFile, 0, SEEK_END);
1156 //estimate file breaks
1157 unsigned long int chunkSize = 0;
1158 chunkSize = size / proc;
1160 //file to small to divide by processors
1161 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1163 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1164 for (int i = 0; i < proc; i++) {
1165 unsigned long int spot = (i+1) * chunkSize;
1168 openInputFile(filename, in);
1172 unsigned long int newSpot = spot;
1175 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1178 //there was not another sequence before the end of the file
1179 if (newSpot == spot) { break; }
1180 else { filePos.push_back(newSpot); }
1186 filePos.push_back(size);
1188 //sanity check filePos
1189 for (int i = 0; i < (filePos.size()-1); i++) {
1190 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1193 proc = (filePos.size() - 1);
1197 catch(exception& e) {
1198 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function divideFile. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
1202 /**************************************************************************************************/
1203 inline bool checkReleaseVersion(ifstream& file, string version) {
1208 string line = getline(file);
1210 //before we added this check
1211 if (line[0] != '#') { good = false; }
1214 line = line.substr(1);
1216 vector<string> versionVector;
1217 splitAtChar(version, versionVector, '.');
1219 //check file version
1220 vector<string> linesVector;
1221 splitAtChar(line, linesVector, '.');
1223 if (versionVector.size() != linesVector.size()) { good = false; }
1225 for (int j = 0; j < versionVector.size(); j++) {
1227 convert(versionVector[j], num1);
1228 convert(linesVector[j], num2);
1230 //if mothurs version is newer than this files version, then we want to remake it
1231 if (num1 > num2) { good = false; break; }
1237 if (!good) { file.close(); }
1238 else { file.seekg(0); }
1242 catch(exception& e) {
1243 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function checkReleaseVersion. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
1247 /**************************************************************************************************/