5 * Created by westcott on 2/25/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "mothurout.h"
12 /******************************************************/
13 MothurOut* MothurOut::getInstance() {
14 if( _uniqueInstance == 0) {
15 _uniqueInstance = new MothurOut();
17 return _uniqueInstance;
19 /*********************************************************************************************/
20 void MothurOut::setFileName(string filename) {
22 logFileName = filename;
26 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
28 if (pid == 0) { //only one process should output to screen
31 openOutputFile(filename, out);
38 errorOut(e, "MothurOut", "setFileName");
42 /*********************************************************************************************/
43 void MothurOut::setDefaultPath(string pathname) {
46 //add / to name if needed
47 string lastChar = pathname.substr(pathname.length()-1);
48 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
49 if (lastChar != "/") { pathname += "/"; }
51 if (lastChar != "\\") { pathname += "\\"; }
54 defaultPath = pathname;
58 errorOut(e, "MothurOut", "setDefaultPath");
62 /*********************************************************************************************/
63 void MothurOut::closeLog() {
68 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
70 if (pid == 0) { //only one process should output to screen
80 errorOut(e, "MothurOut", "closeLog");
85 /*********************************************************************************************/
86 MothurOut::~MothurOut() {
92 errorOut(e, "MothurOut", "MothurOut");
96 /*********************************************************************************************/
97 void MothurOut::mothurOut(string output) {
102 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
104 if (pid == 0) { //only one process should output to screen
114 catch(exception& e) {
115 errorOut(e, "MothurOut", "MothurOut");
119 /*********************************************************************************************/
120 void MothurOut::mothurOutEndLine() {
124 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
126 if (pid == 0) { //only one process should output to screen
136 catch(exception& e) {
137 errorOut(e, "MothurOut", "MothurOutEndLine");
141 /*********************************************************************************************/
142 void MothurOut::mothurOutJustToLog(string output) {
146 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
148 if (pid == 0) { //only one process should output to screen
157 catch(exception& e) {
158 errorOut(e, "MothurOut", "MothurOutJustToLog");
162 /*********************************************************************************************/
163 void MothurOut::errorOut(exception& e, string object, string function) {
165 //mem_usage(vm, rss);
167 mothurOut("[ERROR]: ");
168 mothurOut(toString(e.what()));
169 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.");
172 /*********************************************************************************************/
173 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
174 // process_mem_usage(double &, double &) - takes two doubles by reference,
175 // attempts to read the system-dependent data for a process' virtual memory
176 // size and resident set size, and return the results in KB.
178 // On failure, returns 0.0, 0.0
179 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
180 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
185 // 'file' stat seems to give the most reliable results
187 ifstream stat_stream("/proc/self/stat",ios_base::in);
189 // dummy vars for leading entries in stat that we don't care about
191 string pid, comm, state, ppid, pgrp, session, tty_nr;
192 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
193 string utime, stime, cutime, cstime, priority, nice;
194 string O, itrealvalue, starttime;
196 // the two fields we want
201 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
202 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
203 >> utime >> stime >> cutime >> cstime >> priority >> nice
204 >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
206 long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
207 vm_usage = vsize / 1024.0;
208 resident_set = rss * page_size_kb;
210 mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
214 /* //windows memory usage
215 // Get the list of process identifiers.
216 DWORD aProcesses[1024], cbNeeded, cProcesses;
218 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
220 // Calculate how many process identifiers were returned.
221 cProcesses = cbNeeded / sizeof(DWORD);
223 // Print the memory usage for each process
224 for (int i = 0; i < cProcesses; i++ ) {
225 DWORD processID = aProcesses[i];
227 PROCESS_MEMORY_COUNTERS pmc;
229 HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
231 // Print the process identifier.
232 printf( "\nProcess ID: %u\n", processID);
234 if (NULL != hProcess) {
236 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
237 printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
238 printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
239 printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
240 printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
241 printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
242 printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
243 printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
244 printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
245 printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
247 CloseHandle(hProcess);
257 /***********************************************************************/
258 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
260 fileName = getFullPathName(fileName);
262 fileHandle.open(fileName.c_str(), ios::app);
264 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
271 catch(exception& e) {
272 errorOut(e, "MothurOut", "openOutputFileAppend");
276 /***********************************************************************/
277 void MothurOut::gobble(istream& f){
280 while(isspace(d=f.get())) {;}
283 catch(exception& e) {
284 errorOut(e, "MothurOut", "gobble");
288 /***********************************************************************/
289 void MothurOut::gobble(istringstream& f){
292 while(isspace(d=f.get())) {;}
295 catch(exception& e) {
296 errorOut(e, "MothurOut", "gobble");
301 /***********************************************************************/
303 string MothurOut::getline(istringstream& fileHandle) {
308 while (!fileHandle.eof()) {
310 char c = fileHandle.get();
312 //are you at the end of the line
313 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
320 catch(exception& e) {
321 errorOut(e, "MothurOut", "getline");
325 /***********************************************************************/
327 string MothurOut::getline(ifstream& fileHandle) {
332 while (!fileHandle.eof()) {
334 char c = fileHandle.get();
336 //are you at the end of the line
337 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
344 catch(exception& e) {
345 errorOut(e, "MothurOut", "getline");
349 /***********************************************************************/
351 string MothurOut::getRootName(string longName){
354 string rootName = longName;
356 if(longName.find_last_of(".") != longName.npos){
357 int pos = longName.find_last_of('.')+1;
358 rootName = longName.substr(0, pos);
363 catch(exception& e) {
364 errorOut(e, "MothurOut", "getRootName");
368 /***********************************************************************/
370 string MothurOut::getSimpleName(string longName){
372 string simpleName = longName;
375 found=longName.find_last_of("/\\");
377 if(found != longName.npos){
378 simpleName = longName.substr(found+1);
383 catch(exception& e) {
384 errorOut(e, "MothurOut", "getSimpleName");
389 /***********************************************************************/
391 string MothurOut::getPathName(string longName){
393 string rootPathName = longName;
395 if(longName.find_last_of("/\\") != longName.npos){
396 int pos = longName.find_last_of("/\\")+1;
397 rootPathName = longName.substr(0, pos);
402 catch(exception& e) {
403 errorOut(e, "MothurOut", "getPathName");
408 /***********************************************************************/
410 string MothurOut::hasPath(string longName){
415 found=longName.find_last_of("~/\\");
417 if(found != longName.npos){
418 path = longName.substr(0, found+1);
423 catch(exception& e) {
424 errorOut(e, "MothurOut", "hasPath");
429 /***********************************************************************/
431 string MothurOut::getExtension(string longName){
433 string extension = longName;
435 if(longName.find_last_of('.') != longName.npos){
436 int pos = longName.find_last_of('.');
437 extension = longName.substr(pos, longName.length());
442 catch(exception& e) {
443 errorOut(e, "MothurOut", "getExtension");
447 /***********************************************************************/
448 bool MothurOut::isBlank(string fileName){
451 fileName = getFullPathName(fileName);
454 fileHandle.open(fileName.c_str());
456 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
459 //check for blank file
461 if (fileHandle.eof()) { fileHandle.close(); return true; }
465 catch(exception& e) {
466 errorOut(e, "MothurOut", "isBlank");
470 /***********************************************************************/
472 string MothurOut::getFullPathName(string fileName){
475 string path = hasPath(fileName);
479 if (path == "") { return fileName; } //its a simple name
480 else { //we need to complete the pathname
481 // ex. ../../../filename
482 // cwd = /user/work/desktop
485 //get current working directory
486 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
488 if (path.find("~") != -1) { //go to home directory
489 string homeDir = getenv ("HOME");
490 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
493 if (path.rfind("./") == -1) { return fileName; } //already complete name
494 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
496 //char* cwdpath = new char[1024];
499 //cwdpath=getcwd(cwdpath,size);
502 char *cwdpath = NULL;
503 cwdpath = getcwd(NULL, 0); // or _getcwd
504 if ( cwdpath != NULL) { cwd = cwdpath; }
509 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
511 //break apart the current working directory
513 while (simpleCWD.find_first_of('/') != -1) {
514 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
515 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
518 //get last one // ex. ../../../filename = /user/work/desktop/filename
519 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
522 int index = dirs.size()-1;
524 while((pos = path.rfind("./")) != -1) { //while you don't have a complete path
525 if (pos == 0) { break; //you are at the end
526 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
527 path = path.substr(0, pos-1);
529 if (index == 0) { break; }
530 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
531 path = path.substr(0, pos);
532 }else if (pos == 1) { break; //you are at the end
533 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
536 for (int i = index; i >= 0; i--) {
537 newFileName = dirs[i] + "/" + newFileName;
540 newFileName = "/" + newFileName;
545 if (path.find("~") != -1) { //go to home directory
546 string homeDir = getenv ("HOMEPATH");
547 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
550 if (path.rfind(".\\") == -1) { return fileName; } //already complete name
551 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
553 char *cwdpath = NULL;
554 cwdpath = getcwd(NULL, 0); // or _getcwd
555 if ( cwdpath != NULL) { cwd = cwdpath; }
558 //break apart the current working directory
560 while (cwd.find_first_of('\\') != -1) {
561 string dir = cwd.substr(0,cwd.find_first_of('\\'));
562 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
567 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
569 int index = dirs.size()-1;
571 while((pos = path.rfind(".\\")) != -1) { //while you don't have a complete path
572 if (pos == 0) { break; //you are at the end
573 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
574 path = path.substr(0, pos-1);
576 if (index == 0) { break; }
577 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
578 path = path.substr(0, pos);
579 }else if (pos == 1) { break; //you are at the end
580 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
583 for (int i = index; i >= 0; i--) {
584 newFileName = dirs[i] + "\\" + newFileName;
593 catch(exception& e) {
594 errorOut(e, "MothurOut", "getFullPathName");
598 /***********************************************************************/
600 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
603 string completeFileName = getFullPathName(fileName);
605 fileHandle.open(completeFileName.c_str());
609 //check for blank file
614 catch(exception& e) {
615 errorOut(e, "MothurOut", "openInputFile - no Error");
619 /***********************************************************************/
621 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
624 string completeFileName = getFullPathName(fileName);
626 fileHandle.open(completeFileName.c_str());
628 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
632 //check for blank file
634 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
639 catch(exception& e) {
640 errorOut(e, "MothurOut", "openInputFile");
644 /***********************************************************************/
646 int MothurOut::renameFile(string oldName, string newName){
649 int exist = openInputFile(newName, inTest, "");
651 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
652 if (exist == 0) { //you could open it so you want to delete it
654 string command = "rm " + newName;
655 system(command.c_str());
658 string command = "mv " + oldName + " " + newName;
659 system(command.c_str());
661 remove(newName.c_str());
662 int renameOk = rename(oldName.c_str(), newName.c_str());
667 catch(exception& e) {
668 errorOut(e, "MothurOut", "renameFile");
673 /***********************************************************************/
675 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
678 string completeFileName = getFullPathName(fileName);
680 fileHandle.open(completeFileName.c_str(), ios::trunc);
682 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
689 catch(exception& e) {
690 errorOut(e, "MothurOut", "openOutputFile");
696 /**************************************************************************************************/
697 void MothurOut::appendFiles(string temp, string filename) {
702 //open output file in append mode
703 openOutputFileAppend(filename, output);
704 int ableToOpen = openInputFile(temp, input, "no error");
706 if (ableToOpen == 0) { //you opened it
707 while(char c = input.get()){
708 if(input.eof()) { break; }
709 else { output << c; }
716 catch(exception& e) {
717 errorOut(e, "MothurOut", "appendFiles");
722 /**************************************************************************************************/
723 string MothurOut::sortFile(string distFile, string outputDir){
726 //if (outputDir == "") { outputDir += hasPath(distFile); }
727 string outfile = getRootName(distFile) + "sorted.dist";
730 //if you can, use the unix sort since its been optimized for years
731 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
732 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
733 system(command.c_str());
734 #else //you are stuck with my best attempt...
735 //windows sort does not have a way to specify a column, only a character in the line
736 //since we cannot assume that the distance will always be at the the same character location on each line
737 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
739 //read in file line by file and put distance first
740 string tempDistFile = distFile + ".temp";
743 openInputFile(distFile, input);
744 openOutputFile(tempDistFile, output);
746 string firstName, secondName;
749 input >> firstName >> secondName >> dist;
750 output << dist << '\t' << firstName << '\t' << secondName << endl;
757 //sort using windows sort
758 string tempOutfile = outfile + ".temp";
759 string command = "sort " + tempDistFile + " /O " + tempOutfile;
760 system(command.c_str());
762 //read in sorted file and put distance at end again
764 openInputFile(tempOutfile, input2);
765 openOutputFile(outfile, output);
768 input2 >> dist >> firstName >> secondName;
769 output << firstName << '\t' << secondName << '\t' << dist << endl;
776 remove(tempDistFile.c_str());
777 remove(tempOutfile.c_str());
782 catch(exception& e) {
783 errorOut(e, "MothurOut", "sortFile");
787 /**************************************************************************************************/
788 vector<unsigned long int> MothurOut::setFilePosFasta(string filename, int& num) {
790 vector<unsigned long int> positions;
792 openInputFile(filename, inFASTA);
795 while(!inFASTA.eof()){
796 input = getline(inFASTA);
797 if (input.length() != 0) {
798 if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
800 gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
804 num = positions.size();
809 //get num bytes in file
810 pFile = fopen (filename.c_str(),"rb");
811 if (pFile==NULL) perror ("Error opening file");
813 fseek (pFile, 0, SEEK_END);
818 unsigned long int size = positions[(positions.size()-1)];
820 openInputFile(filename, in);
824 while(char c = in.get()){
825 if(in.eof()) { break; }
830 positions.push_back(size);
834 catch(exception& e) {
835 errorOut(e, "MothurOut", "setFilePosFasta");
839 /**************************************************************************************************/
840 vector<unsigned long int> MothurOut::setFilePosEachLine(string filename, int& num) {
842 filename = getFullPathName(filename);
844 vector<unsigned long int> positions;
846 openInputFile(filename, in);
850 unsigned long int lastpos = in.tellg();
852 if (input.length() != 0) {
853 unsigned long int pos = in.tellg();
854 if (pos != -1) { positions.push_back(pos - input.length() - 1); }
855 else { positions.push_back(lastpos); }
857 gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
861 num = positions.size();
864 unsigned long int size;
866 //get num bytes in file
867 pFile = fopen (filename.c_str(),"rb");
868 if (pFile==NULL) perror ("Error opening file");
870 fseek (pFile, 0, SEEK_END);
875 positions.push_back(size);
879 catch(exception& e) {
880 errorOut(e, "MothurOut", "setFilePosEachLine");
884 /**************************************************************************************************/
886 vector<unsigned long int> MothurOut::divideFile(string filename, int& proc) {
889 vector<unsigned long int> filePos;
890 filePos.push_back(0);
893 unsigned long int size;
895 filename = getFullPathName(filename);
897 //get num bytes in file
898 pFile = fopen (filename.c_str(),"rb");
899 if (pFile==NULL) perror ("Error opening file");
901 fseek (pFile, 0, SEEK_END);
906 //estimate file breaks
907 unsigned long int chunkSize = 0;
908 chunkSize = size / proc;
910 //file to small to divide by processors
911 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
913 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
914 for (int i = 0; i < proc; i++) {
915 unsigned long int spot = (i+1) * chunkSize;
918 openInputFile(filename, in);
922 unsigned long int newSpot = spot;
925 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
928 //there was not another sequence before the end of the file
929 unsigned long int sanityPos = in.tellg();
931 if (sanityPos == -1) { break; }
932 else { filePos.push_back(newSpot); }
938 filePos.push_back(size);
940 //sanity check filePos
941 for (int i = 0; i < (filePos.size()-1); i++) {
942 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
945 proc = (filePos.size() - 1);
949 catch(exception& e) {
950 errorOut(e, "MothurOut", "divideFile");
955 /***********************************************************************/
957 bool MothurOut::isTrue(string f){
960 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
962 if ((f == "TRUE") || (f == "T")) { return true; }
963 else { return false; }
965 catch(exception& e) {
966 errorOut(e, "MothurOut", "isTrue");
971 /***********************************************************************/
973 float MothurOut::roundDist(float dist, int precision){
975 return int(dist * precision + 0.5)/float(precision);
977 catch(exception& e) {
978 errorOut(e, "MothurOut", "roundDist");
982 /***********************************************************************/
984 float MothurOut::ceilDist(float dist, int precision){
986 return int(ceil(dist * precision))/float(precision);
988 catch(exception& e) {
989 errorOut(e, "MothurOut", "ceilDist");
994 /***********************************************************************/
996 int MothurOut::getNumNames(string names){
1002 for(int i=0;i<names.size();i++){
1003 if(names[i] == ','){
1011 catch(exception& e) {
1012 errorOut(e, "MothurOut", "getNumNames");
1017 /**************************************************************************************************/
1019 vector<vector<double> > MothurOut::binomial(int maxOrder){
1021 vector<vector<double> > binomial(maxOrder+1);
1023 for(int i=0;i<=maxOrder;i++){
1024 binomial[i].resize(maxOrder+1);
1033 for(int i=2;i<=maxOrder;i++){
1037 for(int i=2;i<=maxOrder;i++){
1038 for(int j=1;j<=maxOrder;j++){
1039 if(i==j){ binomial[i][j]=1; }
1040 if(j>i) { binomial[i][j]=0; }
1041 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
1048 catch(exception& e) {
1049 errorOut(e, "MothurOut", "binomial");
1054 /***********************************************************************/
1056 int MothurOut::factorial(int num){
1060 for (int i = 1; i <= num; i++) {
1066 catch(exception& e) {
1067 errorOut(e, "MothurOut", "factorial");
1071 /***********************************************************************/
1073 int MothurOut::getNumSeqs(ifstream& file){
1075 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1079 catch(exception& e) {
1080 errorOut(e, "MothurOut", "getNumSeqs");
1084 /***********************************************************************/
1085 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1090 input = getline(file);
1091 if (input.length() != 0) {
1092 if(input[0] == '>'){ numSeqs++; }
1096 catch(exception& e) {
1097 errorOut(e, "MothurOut", "getNumSeqs");
1101 /***********************************************************************/
1103 //This function parses the estimator options and puts them in a vector
1104 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1108 while (estim.find_first_of(symbol) != -1) {
1109 individual = estim.substr(0,estim.find_first_of(symbol));
1110 if ((estim.find_first_of(symbol)+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1111 estim = estim.substr(estim.find_first_of(symbol)+1, estim.length());
1112 container.push_back(individual);
1116 container.push_back(estim);
1118 catch(exception& e) {
1119 errorOut(e, "MothurOut", "splitAtChar");
1124 /***********************************************************************/
1126 //This function parses the estimator options and puts them in a vector
1127 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1129 string individual = "";
1130 int estimLength = estim.size();
1131 for(int i=0;i<estimLength;i++){
1132 if(estim[i] == '-'){
1133 container.push_back(individual);
1137 individual += estim[i];
1140 container.push_back(individual);
1143 /* string individual;
1145 while (estim.find_first_of('-') != -1) {
1146 individual = estim.substr(0,estim.find_first_of('-'));
1147 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1148 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1149 container.push_back(individual);
1153 container.push_back(estim); */
1155 catch(exception& e) {
1156 errorOut(e, "MothurOut", "splitAtDash");
1161 /***********************************************************************/
1162 //This function parses the label options and puts them in a set
1163 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1165 string individual = "";
1166 int estimLength = estim.size();
1167 for(int i=0;i<estimLength;i++){
1168 if(estim[i] == '-'){
1169 container.insert(individual);
1173 individual += estim[i];
1176 container.insert(individual);
1178 // string individual;
1180 // while (estim.find_first_of('-') != -1) {
1181 // individual = estim.substr(0,estim.find_first_of('-'));
1182 // if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1183 // estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1184 // container.insert(individual);
1188 // container.insert(estim);
1191 catch(exception& e) {
1192 errorOut(e, "MothurOut", "splitAtDash");
1196 /***********************************************************************/
1197 //This function parses the line options and puts them in a set
1198 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1203 while (estim.find_first_of('-') != -1) {
1204 individual = estim.substr(0,estim.find_first_of('-'));
1205 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1206 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1207 convert(individual, lineNum); //convert the string to int
1208 container.insert(lineNum);
1212 convert(estim, lineNum); //convert the string to int
1213 container.insert(lineNum);
1215 catch(exception& e) {
1216 errorOut(e, "MothurOut", "splitAtDash");
1220 /***********************************************************************/
1221 //This function parses the a string and puts peices in a vector
1222 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1224 string individual = "";
1225 int estimLength = estim.size();
1226 for(int i=0;i<estimLength;i++){
1227 if(estim[i] == ','){
1228 container.push_back(individual);
1232 individual += estim[i];
1235 container.push_back(individual);
1240 // string individual;
1242 // while (estim.find_first_of(',') != -1) {
1243 // individual = estim.substr(0,estim.find_first_of(','));
1244 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1245 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1246 // container.push_back(individual);
1250 // container.push_back(estim);
1252 catch(exception& e) {
1253 errorOut(e, "MothurOut", "splitAtComma");
1257 /***********************************************************************/
1259 //This function splits up the various option parameters
1260 void MothurOut::splitAtComma(string& prefix, string& suffix){
1262 prefix = suffix.substr(0,suffix.find_first_of(','));
1263 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
1264 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1266 while(suffix.at(0) == ' ')
1267 suffix = suffix.substr(1, suffix.length());
1271 catch(exception& e) {
1272 errorOut(e, "MothurOut", "splitAtComma");
1276 /***********************************************************************/
1278 //This function separates the key value from the option value i.e. dist=96_...
1279 void MothurOut::splitAtEquals(string& key, string& value){
1281 if(value.find_first_of('=') != -1){
1282 key = value.substr(0,value.find_first_of('='));
1283 if ((value.find_first_of('=')+1) <= value.length()) {
1284 value = value.substr(value.find_first_of('=')+1, value.length());
1291 catch(exception& e) {
1292 errorOut(e, "MothurOut", "splitAtEquals");
1297 /**************************************************************************************************/
1299 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1301 for (int i = 0; i < Groups.size(); i++) {
1302 if (groupname == Groups[i]) { return true; }
1306 catch(exception& e) {
1307 errorOut(e, "MothurOut", "inUsersGroups");
1311 /**************************************************************************************************/
1312 //returns true if any of the strings in first vector are in second vector
1313 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1316 for (int i = 0; i < groupnames.size(); i++) {
1317 if (inUsersGroups(groupnames[i], Groups)) { return true; }
1321 catch(exception& e) {
1322 errorOut(e, "MothurOut", "inUsersGroups");
1326 /***********************************************************************/
1327 //this function determines if the user has given us labels that are smaller than the given label.
1328 //if so then it returns true so that the calling function can run the previous valid distance.
1329 //it's a "smart" distance function. It also checks for invalid labels.
1330 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1333 set<string>::iterator it;
1334 vector<float> orderFloat;
1335 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
1336 map<string, float>::iterator it2;
1338 bool smaller = false;
1340 //unique is the smallest line
1341 if (label == "unique") { return false; }
1343 if (convertTestFloat(label, labelFloat)) {
1344 convert(label, labelFloat);
1345 }else { //cant convert
1350 //go through users set and make them floats
1351 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
1354 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1356 orderFloat.push_back(temp);
1357 userMap[*it] = temp;
1358 }else if (*it == "unique") {
1359 orderFloat.push_back(-1.0);
1360 userMap["unique"] = -1.0;
1362 if (errorOff == "") { cout << *it << " is not a valid label." << endl; }
1363 userLabels.erase(*it);
1369 sort(orderFloat.begin(), orderFloat.end());
1371 /*************************************************/
1372 //is this label bigger than any of the users labels
1373 /*************************************************/
1375 //loop through order until you find a label greater than label
1376 for (int i = 0; i < orderFloat.size(); i++) {
1377 if (orderFloat[i] < labelFloat) {
1379 if (orderFloat[i] == -1) {
1380 if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
1381 userLabels.erase("unique");
1384 if (errorOff == "") { cout << "Your file does not include the label " << endl; }
1386 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
1387 if (it2->second == orderFloat[i]) {
1389 //remove small labels
1390 userLabels.erase(s);
1394 if (errorOff == "") {cout << s << ". I will use the next smallest distance. " << endl; }
1396 //since they are sorted once you find a bigger one stop looking
1403 catch(exception& e) {
1404 errorOut(e, "MothurOut", "anyLabelsToProcess");
1409 /**************************************************************************************************/
1410 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1415 string line = getline(file);
1417 //before we added this check
1418 if (line[0] != '#') { good = false; }
1421 line = line.substr(1);
1423 vector<string> versionVector;
1424 splitAtChar(version, versionVector, '.');
1426 //check file version
1427 vector<string> linesVector;
1428 splitAtChar(line, linesVector, '.');
1430 if (versionVector.size() != linesVector.size()) { good = false; }
1432 for (int j = 0; j < versionVector.size(); j++) {
1434 convert(versionVector[j], num1);
1435 convert(linesVector[j], num2);
1437 //if mothurs version is newer than this files version, then we want to remake it
1438 if (num1 > num2) { good = false; break; }
1444 if (!good) { file.close(); }
1445 else { file.seekg(0); }
1449 catch(exception& e) {
1450 errorOut(e, "MothurOut", "checkReleaseVersion");
1454 /**************************************************************************************************/