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 fileHandle.open(fileName.c_str(), ios::app);
262 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
269 catch(exception& e) {
270 errorOut(e, "MothurOut", "openOutputFileAppend");
274 /***********************************************************************/
275 void MothurOut::gobble(istream& f){
278 while(isspace(d=f.get())) {;}
281 catch(exception& e) {
282 errorOut(e, "MothurOut", "gobble");
286 /***********************************************************************/
287 void MothurOut::gobble(istringstream& f){
290 while(isspace(d=f.get())) {;}
293 catch(exception& e) {
294 errorOut(e, "MothurOut", "gobble");
299 /***********************************************************************/
301 string MothurOut::getline(istringstream& fileHandle) {
306 while (!fileHandle.eof()) {
308 char c = fileHandle.get();
310 //are you at the end of the line
311 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
318 catch(exception& e) {
319 errorOut(e, "MothurOut", "getline");
323 /***********************************************************************/
325 string MothurOut::getline(ifstream& fileHandle) {
330 while (!fileHandle.eof()) {
332 char c = fileHandle.get();
334 //are you at the end of the line
335 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
342 catch(exception& e) {
343 errorOut(e, "MothurOut", "getline");
347 /***********************************************************************/
349 string MothurOut::getRootName(string longName){
352 string rootName = longName;
354 if(longName.find_last_of(".") != longName.npos){
355 int pos = longName.find_last_of('.')+1;
356 rootName = longName.substr(0, pos);
361 catch(exception& e) {
362 errorOut(e, "MothurOut", "getRootName");
366 /***********************************************************************/
368 string MothurOut::getSimpleName(string longName){
370 string simpleName = longName;
373 found=longName.find_last_of("/\\");
375 if(found != longName.npos){
376 simpleName = longName.substr(found+1);
381 catch(exception& e) {
382 errorOut(e, "MothurOut", "getSimpleName");
387 /***********************************************************************/
389 string MothurOut::getPathName(string longName){
391 string rootPathName = longName;
393 if(longName.find_last_of("/\\") != longName.npos){
394 int pos = longName.find_last_of("/\\")+1;
395 rootPathName = longName.substr(0, pos);
400 catch(exception& e) {
401 errorOut(e, "MothurOut", "getPathName");
406 /***********************************************************************/
408 string MothurOut::hasPath(string longName){
413 found=longName.find_last_of("~/\\");
415 if(found != longName.npos){
416 path = longName.substr(0, found+1);
421 catch(exception& e) {
422 errorOut(e, "MothurOut", "hasPath");
427 /***********************************************************************/
429 string MothurOut::getExtension(string longName){
431 string extension = longName;
433 if(longName.find_last_of('.') != longName.npos){
434 int pos = longName.find_last_of('.');
435 extension = longName.substr(pos, longName.length());
440 catch(exception& e) {
441 errorOut(e, "MothurOut", "getExtension");
445 /***********************************************************************/
446 bool MothurOut::isBlank(string fileName){
449 fileHandle.open(fileName.c_str());
451 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
454 //check for blank file
456 if (fileHandle.eof()) { fileHandle.close(); return true; }
460 catch(exception& e) {
461 errorOut(e, "MothurOut", "isBlank");
465 /***********************************************************************/
467 string MothurOut::getFullPathName(string fileName){
470 string path = hasPath(fileName);
474 if (path == "") { return fileName; } //its a simple name
475 else { //we need to complete the pathname
476 // ex. ../../../filename
477 // cwd = /user/work/desktop
480 //get current working directory
481 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
483 if (path.find("~") != -1) { //go to home directory
484 string homeDir = getenv ("HOME");
485 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
488 if (path.rfind("./") == -1) { return fileName; } //already complete name
489 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
491 char* cwdpath = new char[1024];
494 cwdpath=getcwd(cwdpath,size);
500 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
502 //break apart the current working directory
504 while (simpleCWD.find_first_of('/') != -1) {
505 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
506 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
509 //get last one // ex. ../../../filename = /user/work/desktop/filename
510 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
513 int index = dirs.size()-1;
515 while((pos = path.rfind("./")) != -1) { //while you don't have a complete path
516 if (pos == 0) { break; //you are at the end
517 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
518 path = path.substr(0, pos-1);
520 if (index == 0) { break; }
521 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
522 path = path.substr(0, pos);
523 }else if (pos == 1) { break; //you are at the end
524 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
527 for (int i = index; i >= 0; i--) {
528 newFileName = dirs[i] + "/" + newFileName;
531 newFileName = "/" + newFileName;
535 if (path.find("~") != -1) { //go to home directory
536 string homeDir = getenv ("HOMEPATH");
537 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
540 if (path.rfind(".\\") == -1) { return fileName; } //already complete name
541 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
543 char *cwdpath = NULL;
544 cwdpath = getcwd(NULL, 0); // or _getcwd
545 if ( cwdpath != NULL) { cwd = cwdpath; }
548 //break apart the current working directory
550 while (cwd.find_first_of('\\') != -1) {
551 string dir = cwd.substr(0,cwd.find_first_of('\\'));
552 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
557 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
559 int index = dirs.size()-1;
561 while((pos = path.rfind(".\\")) != -1) { //while you don't have a complete path
562 if (pos == 0) { break; //you are at the end
563 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
564 path = path.substr(0, pos-1);
566 if (index == 0) { break; }
567 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
568 path = path.substr(0, pos);
569 }else if (pos == 1) { break; //you are at the end
570 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
573 for (int i = index; i >= 0; i--) {
574 newFileName = dirs[i] + "\\" + newFileName;
583 catch(exception& e) {
584 errorOut(e, "MothurOut", "getFullPathName");
588 /***********************************************************************/
590 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
593 string completeFileName = getFullPathName(fileName);
595 fileHandle.open(completeFileName.c_str());
599 //check for blank file
604 catch(exception& e) {
605 errorOut(e, "MothurOut", "openInputFile - no Error");
609 /***********************************************************************/
611 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
614 string completeFileName = getFullPathName(fileName);
616 fileHandle.open(completeFileName.c_str());
618 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
622 //check for blank file
624 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
629 catch(exception& e) {
630 errorOut(e, "MothurOut", "openInputFile");
634 /***********************************************************************/
636 int MothurOut::renameFile(string oldName, string newName){
639 int exist = openInputFile(newName, inTest, "");
641 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
642 if (exist == 0) { //you could open it so you want to delete it
644 string command = "rm " + newName;
645 system(command.c_str());
648 string command = "mv " + oldName + " " + newName;
649 system(command.c_str());
651 remove(newName.c_str());
652 int renameOk = rename(oldName.c_str(), newName.c_str());
657 catch(exception& e) {
658 errorOut(e, "MothurOut", "renameFile");
663 /***********************************************************************/
665 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
668 string completeFileName = getFullPathName(fileName);
670 fileHandle.open(completeFileName.c_str(), ios::trunc);
672 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
679 catch(exception& e) {
680 errorOut(e, "MothurOut", "openOutputFile");
686 /**************************************************************************************************/
687 void MothurOut::appendFiles(string temp, string filename) {
692 //open output file in append mode
693 openOutputFileAppend(filename, output);
694 int ableToOpen = openInputFile(temp, input, "no error");
696 if (ableToOpen == 0) { //you opened it
697 while(char c = input.get()){
698 if(input.eof()) { break; }
699 else { output << c; }
706 catch(exception& e) {
707 errorOut(e, "MothurOut", "appendFiles");
712 /**************************************************************************************************/
713 string MothurOut::sortFile(string distFile, string outputDir){
716 //if (outputDir == "") { outputDir += hasPath(distFile); }
717 string outfile = getRootName(distFile) + "sorted.dist";
720 //if you can, use the unix sort since its been optimized for years
721 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
722 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
723 system(command.c_str());
724 #else //you are stuck with my best attempt...
725 //windows sort does not have a way to specify a column, only a character in the line
726 //since we cannot assume that the distance will always be at the the same character location on each line
727 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
729 //read in file line by file and put distance first
730 string tempDistFile = distFile + ".temp";
733 openInputFile(distFile, input);
734 openOutputFile(tempDistFile, output);
736 string firstName, secondName;
739 input >> firstName >> secondName >> dist;
740 output << dist << '\t' << firstName << '\t' << secondName << endl;
747 //sort using windows sort
748 string tempOutfile = outfile + ".temp";
749 string command = "sort " + tempDistFile + " /O " + tempOutfile;
750 system(command.c_str());
752 //read in sorted file and put distance at end again
754 openInputFile(tempOutfile, input2);
755 openOutputFile(outfile, output);
758 input2 >> dist >> firstName >> secondName;
759 output << firstName << '\t' << secondName << '\t' << dist << endl;
766 remove(tempDistFile.c_str());
767 remove(tempOutfile.c_str());
772 catch(exception& e) {
773 errorOut(e, "MothurOut", "sortFile");
777 /**************************************************************************************************/
778 vector<unsigned long int> MothurOut::setFilePosFasta(string filename, int& num) {
780 vector<unsigned long int> positions;
782 openInputFile(filename, inFASTA);
785 while(!inFASTA.eof()){
786 input = getline(inFASTA);
787 if (input.length() != 0) {
788 if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
790 gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
794 num = positions.size();
799 //get num bytes in file
800 pFile = fopen (filename.c_str(),"rb");
801 if (pFile==NULL) perror ("Error opening file");
803 fseek (pFile, 0, SEEK_END);
808 unsigned long int size = positions[(positions.size()-1)];
810 openInputFile(filename, in);
814 while(char c = in.get()){
815 if(in.eof()) { break; }
820 positions.push_back(size);
824 catch(exception& e) {
825 errorOut(e, "MothurOut", "setFilePosFasta");
829 /**************************************************************************************************/
830 vector<unsigned long int> MothurOut::setFilePosEachLine(string filename, int& num) {
833 vector<unsigned long int> positions;
835 openInputFile(filename, in);
839 unsigned long int lastpos = in.tellg();
841 if (input.length() != 0) {
842 unsigned long int pos = in.tellg();
843 if (pos != -1) { positions.push_back(pos - input.length() - 1); }
844 else { positions.push_back(lastpos); }
846 gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
850 num = positions.size();
853 unsigned long int size;
855 //get num bytes in file
856 pFile = fopen (filename.c_str(),"rb");
857 if (pFile==NULL) perror ("Error opening file");
859 fseek (pFile, 0, SEEK_END);
864 positions.push_back(size);
868 catch(exception& e) {
869 errorOut(e, "MothurOut", "setFilePosEachLine");
873 /**************************************************************************************************/
875 vector<unsigned long int> MothurOut::divideFile(string filename, int& proc) {
878 vector<unsigned long int> filePos;
879 filePos.push_back(0);
882 unsigned long int size;
884 //get num bytes in file
885 pFile = fopen (filename.c_str(),"rb");
886 if (pFile==NULL) perror ("Error opening file");
888 fseek (pFile, 0, SEEK_END);
893 //estimate file breaks
894 unsigned long int chunkSize = 0;
895 chunkSize = size / proc;
897 //file to small to divide by processors
898 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
900 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
901 for (int i = 0; i < proc; i++) {
902 unsigned long int spot = (i+1) * chunkSize;
905 openInputFile(filename, in);
909 unsigned long int newSpot = spot;
912 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
915 //there was not another sequence before the end of the file
916 unsigned long int sanityPos = in.tellg();
917 if (sanityPos = -1) { break; }
918 else { filePos.push_back(newSpot); }
924 filePos.push_back(size);
926 //sanity check filePos
927 for (int i = 0; i < (filePos.size()-1); i++) {
928 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
931 proc = (filePos.size() - 1);
935 catch(exception& e) {
936 errorOut(e, "MothurOut", "divideFile");
941 /***********************************************************************/
943 bool MothurOut::isTrue(string f){
946 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
948 if ((f == "TRUE") || (f == "T")) { return true; }
949 else { return false; }
951 catch(exception& e) {
952 errorOut(e, "MothurOut", "isTrue");
957 /***********************************************************************/
959 float MothurOut::roundDist(float dist, int precision){
961 return int(dist * precision + 0.5)/float(precision);
963 catch(exception& e) {
964 errorOut(e, "MothurOut", "roundDist");
968 /***********************************************************************/
970 float MothurOut::ceilDist(float dist, int precision){
972 return int(ceil(dist * precision))/float(precision);
974 catch(exception& e) {
975 errorOut(e, "MothurOut", "ceilDist");
980 /***********************************************************************/
982 int MothurOut::getNumNames(string names){
988 for(int i=0;i<names.size();i++){
997 catch(exception& e) {
998 errorOut(e, "MothurOut", "getNumNames");
1003 /**************************************************************************************************/
1005 vector<vector<double> > MothurOut::binomial(int maxOrder){
1007 vector<vector<double> > binomial(maxOrder+1);
1009 for(int i=0;i<=maxOrder;i++){
1010 binomial[i].resize(maxOrder+1);
1019 for(int i=2;i<=maxOrder;i++){
1023 for(int i=2;i<=maxOrder;i++){
1024 for(int j=1;j<=maxOrder;j++){
1025 if(i==j){ binomial[i][j]=1; }
1026 if(j>i) { binomial[i][j]=0; }
1027 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
1034 catch(exception& e) {
1035 errorOut(e, "MothurOut", "binomial");
1040 /***********************************************************************/
1042 int MothurOut::factorial(int num){
1046 for (int i = 1; i <= num; i++) {
1052 catch(exception& e) {
1053 errorOut(e, "MothurOut", "factorial");
1057 /***********************************************************************/
1059 int MothurOut::getNumSeqs(ifstream& file){
1061 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1065 catch(exception& e) {
1066 errorOut(e, "MothurOut", "getNumSeqs");
1070 /***********************************************************************/
1071 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1076 input = getline(file);
1077 if (input.length() != 0) {
1078 if(input[0] == '>'){ numSeqs++; }
1082 catch(exception& e) {
1083 errorOut(e, "MothurOut", "getNumSeqs");
1087 /***********************************************************************/
1089 //This function parses the estimator options and puts them in a vector
1090 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1094 while (estim.find_first_of(symbol) != -1) {
1095 individual = estim.substr(0,estim.find_first_of(symbol));
1096 if ((estim.find_first_of(symbol)+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1097 estim = estim.substr(estim.find_first_of(symbol)+1, estim.length());
1098 container.push_back(individual);
1102 container.push_back(estim);
1104 catch(exception& e) {
1105 errorOut(e, "MothurOut", "splitAtChar");
1110 /***********************************************************************/
1112 //This function parses the estimator options and puts them in a vector
1113 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1117 while (estim.find_first_of('-') != -1) {
1118 individual = estim.substr(0,estim.find_first_of('-'));
1119 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1120 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1121 container.push_back(individual);
1125 container.push_back(estim);
1127 catch(exception& e) {
1128 errorOut(e, "MothurOut", "splitAtDash");
1133 /***********************************************************************/
1134 //This function parses the label options and puts them in a set
1135 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1139 while (estim.find_first_of('-') != -1) {
1140 individual = estim.substr(0,estim.find_first_of('-'));
1141 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1142 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1143 container.insert(individual);
1147 container.insert(estim);
1149 catch(exception& e) {
1150 errorOut(e, "MothurOut", "splitAtDash");
1154 /***********************************************************************/
1155 //This function parses the line options and puts them in a set
1156 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1161 while (estim.find_first_of('-') != -1) {
1162 individual = estim.substr(0,estim.find_first_of('-'));
1163 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1164 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1165 convert(individual, lineNum); //convert the string to int
1166 container.insert(lineNum);
1170 convert(estim, lineNum); //convert the string to int
1171 container.insert(lineNum);
1173 catch(exception& e) {
1174 errorOut(e, "MothurOut", "splitAtDash");
1178 /***********************************************************************/
1179 //This function parses the a string and puts peices in a vector
1180 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1184 while (estim.find_first_of(',') != -1) {
1185 individual = estim.substr(0,estim.find_first_of(','));
1186 if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1187 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1188 container.push_back(individual);
1192 container.push_back(estim);
1194 catch(exception& e) {
1195 errorOut(e, "MothurOut", "splitAtComma");
1199 /***********************************************************************/
1201 //This function splits up the various option parameters
1202 void MothurOut::splitAtComma(string& prefix, string& suffix){
1204 prefix = suffix.substr(0,suffix.find_first_of(','));
1205 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
1206 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1208 while(suffix.at(0) == ' ')
1209 suffix = suffix.substr(1, suffix.length());
1213 catch(exception& e) {
1214 errorOut(e, "MothurOut", "splitAtComma");
1218 /***********************************************************************/
1220 //This function separates the key value from the option value i.e. dist=96_...
1221 void MothurOut::splitAtEquals(string& key, string& value){
1223 if(value.find_first_of('=') != -1){
1224 key = value.substr(0,value.find_first_of('='));
1225 if ((value.find_first_of('=')+1) <= value.length()) {
1226 value = value.substr(value.find_first_of('=')+1, value.length());
1233 catch(exception& e) {
1234 errorOut(e, "MothurOut", "splitAtEquals");
1239 /**************************************************************************************************/
1241 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1243 for (int i = 0; i < Groups.size(); i++) {
1244 if (groupname == Groups[i]) { return true; }
1248 catch(exception& e) {
1249 errorOut(e, "MothurOut", "inUsersGroups");
1253 /**************************************************************************************************/
1254 //returns true if any of the strings in first vector are in second vector
1255 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1258 for (int i = 0; i < groupnames.size(); i++) {
1259 if (inUsersGroups(groupnames[i], Groups)) { return true; }
1263 catch(exception& e) {
1264 errorOut(e, "MothurOut", "inUsersGroups");
1268 /***********************************************************************/
1269 //this function determines if the user has given us labels that are smaller than the given label.
1270 //if so then it returns true so that the calling function can run the previous valid distance.
1271 //it's a "smart" distance function. It also checks for invalid labels.
1272 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1275 set<string>::iterator it;
1276 vector<float> orderFloat;
1277 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
1278 map<string, float>::iterator it2;
1280 bool smaller = false;
1282 //unique is the smallest line
1283 if (label == "unique") { return false; }
1285 if (convertTestFloat(label, labelFloat)) {
1286 convert(label, labelFloat);
1287 }else { //cant convert
1292 //go through users set and make them floats
1293 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
1296 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1298 orderFloat.push_back(temp);
1299 userMap[*it] = temp;
1300 }else if (*it == "unique") {
1301 orderFloat.push_back(-1.0);
1302 userMap["unique"] = -1.0;
1304 if (errorOff == "") { cout << *it << " is not a valid label." << endl; }
1305 userLabels.erase(*it);
1311 sort(orderFloat.begin(), orderFloat.end());
1313 /*************************************************/
1314 //is this label bigger than any of the users labels
1315 /*************************************************/
1317 //loop through order until you find a label greater than label
1318 for (int i = 0; i < orderFloat.size(); i++) {
1319 if (orderFloat[i] < labelFloat) {
1321 if (orderFloat[i] == -1) {
1322 if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
1323 userLabels.erase("unique");
1326 if (errorOff == "") { cout << "Your file does not include the label " << endl; }
1328 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
1329 if (it2->second == orderFloat[i]) {
1331 //remove small labels
1332 userLabels.erase(s);
1336 if (errorOff == "") {cout << s << ". I will use the next smallest distance. " << endl; }
1338 //since they are sorted once you find a bigger one stop looking
1345 catch(exception& e) {
1346 errorOut(e, "MothurOut", "anyLabelsToProcess");
1351 /**************************************************************************************************/
1352 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1357 string line = getline(file);
1359 //before we added this check
1360 if (line[0] != '#') { good = false; }
1363 line = line.substr(1);
1365 vector<string> versionVector;
1366 splitAtChar(version, versionVector, '.');
1368 //check file version
1369 vector<string> linesVector;
1370 splitAtChar(line, linesVector, '.');
1372 if (versionVector.size() != linesVector.size()) { good = false; }
1374 for (int j = 0; j < versionVector.size(); j++) {
1376 convert(versionVector[j], num1);
1377 convert(linesVector[j], num2);
1379 //if mothurs version is newer than this files version, then we want to remake it
1380 if (num1 > num2) { good = false; break; }
1386 if (!good) { file.close(); }
1387 else { file.seekg(0); }
1391 catch(exception& e) {
1392 errorOut(e, "MothurOut", "checkReleaseVersion");
1396 /**************************************************************************************************/