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);
505 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
507 //break apart the current working directory
509 while (simpleCWD.find_first_of('/') != -1) {
510 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
511 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
514 //get last one // ex. ../../../filename = /user/work/desktop/filename
515 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
518 int index = dirs.size()-1;
520 while((pos = path.rfind("./")) != -1) { //while you don't have a complete path
521 if (pos == 0) { break; //you are at the end
522 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
523 path = path.substr(0, pos-1);
525 if (index == 0) { break; }
526 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
527 path = path.substr(0, pos);
528 }else if (pos == 1) { break; //you are at the end
529 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
532 for (int i = index; i >= 0; i--) {
533 newFileName = dirs[i] + "/" + newFileName;
536 newFileName = "/" + newFileName;
540 if (path.find("~") != -1) { //go to home directory
541 string homeDir = getenv ("HOMEPATH");
542 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
545 if (path.rfind(".\\") == -1) { return fileName; } //already complete name
546 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
548 char *cwdpath = NULL;
549 cwdpath = getcwd(NULL, 0); // or _getcwd
550 if ( cwdpath != NULL) { cwd = cwdpath; }
553 //break apart the current working directory
555 while (cwd.find_first_of('\\') != -1) {
556 string dir = cwd.substr(0,cwd.find_first_of('\\'));
557 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
562 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
564 int index = dirs.size()-1;
566 while((pos = path.rfind(".\\")) != -1) { //while you don't have a complete path
567 if (pos == 0) { break; //you are at the end
568 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
569 path = path.substr(0, pos-1);
571 if (index == 0) { break; }
572 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
573 path = path.substr(0, pos);
574 }else if (pos == 1) { break; //you are at the end
575 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
578 for (int i = index; i >= 0; i--) {
579 newFileName = dirs[i] + "\\" + newFileName;
588 catch(exception& e) {
589 errorOut(e, "MothurOut", "getFullPathName");
593 /***********************************************************************/
595 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
598 string completeFileName = getFullPathName(fileName);
600 fileHandle.open(completeFileName.c_str());
604 //check for blank file
609 catch(exception& e) {
610 errorOut(e, "MothurOut", "openInputFile - no Error");
614 /***********************************************************************/
616 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
619 string completeFileName = getFullPathName(fileName);
621 fileHandle.open(completeFileName.c_str());
623 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
627 //check for blank file
629 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
634 catch(exception& e) {
635 errorOut(e, "MothurOut", "openInputFile");
639 /***********************************************************************/
641 int MothurOut::renameFile(string oldName, string newName){
644 int exist = openInputFile(newName, inTest, "");
646 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
647 if (exist == 0) { //you could open it so you want to delete it
649 string command = "rm " + newName;
650 system(command.c_str());
653 string command = "mv " + oldName + " " + newName;
654 system(command.c_str());
656 remove(newName.c_str());
657 int renameOk = rename(oldName.c_str(), newName.c_str());
662 catch(exception& e) {
663 errorOut(e, "MothurOut", "renameFile");
668 /***********************************************************************/
670 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
673 string completeFileName = getFullPathName(fileName);
675 fileHandle.open(completeFileName.c_str(), ios::trunc);
677 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
684 catch(exception& e) {
685 errorOut(e, "MothurOut", "openOutputFile");
691 /**************************************************************************************************/
692 void MothurOut::appendFiles(string temp, string filename) {
697 //open output file in append mode
698 openOutputFileAppend(filename, output);
699 int ableToOpen = openInputFile(temp, input, "no error");
701 if (ableToOpen == 0) { //you opened it
702 while(char c = input.get()){
703 if(input.eof()) { break; }
704 else { output << c; }
711 catch(exception& e) {
712 errorOut(e, "MothurOut", "appendFiles");
717 /**************************************************************************************************/
718 string MothurOut::sortFile(string distFile, string outputDir){
721 //if (outputDir == "") { outputDir += hasPath(distFile); }
722 string outfile = getRootName(distFile) + "sorted.dist";
725 //if you can, use the unix sort since its been optimized for years
726 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
727 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
728 system(command.c_str());
729 #else //you are stuck with my best attempt...
730 //windows sort does not have a way to specify a column, only a character in the line
731 //since we cannot assume that the distance will always be at the the same character location on each line
732 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
734 //read in file line by file and put distance first
735 string tempDistFile = distFile + ".temp";
738 openInputFile(distFile, input);
739 openOutputFile(tempDistFile, output);
741 string firstName, secondName;
744 input >> firstName >> secondName >> dist;
745 output << dist << '\t' << firstName << '\t' << secondName << endl;
752 //sort using windows sort
753 string tempOutfile = outfile + ".temp";
754 string command = "sort " + tempDistFile + " /O " + tempOutfile;
755 system(command.c_str());
757 //read in sorted file and put distance at end again
759 openInputFile(tempOutfile, input2);
760 openOutputFile(outfile, output);
763 input2 >> dist >> firstName >> secondName;
764 output << firstName << '\t' << secondName << '\t' << dist << endl;
771 remove(tempDistFile.c_str());
772 remove(tempOutfile.c_str());
777 catch(exception& e) {
778 errorOut(e, "MothurOut", "sortFile");
782 /**************************************************************************************************/
783 vector<unsigned long int> MothurOut::setFilePosFasta(string filename, int& num) {
785 vector<unsigned long int> positions;
787 openInputFile(filename, inFASTA);
790 while(!inFASTA.eof()){
791 input = getline(inFASTA);
792 if (input.length() != 0) {
793 if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
795 gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
799 num = positions.size();
804 //get num bytes in file
805 pFile = fopen (filename.c_str(),"rb");
806 if (pFile==NULL) perror ("Error opening file");
808 fseek (pFile, 0, SEEK_END);
813 unsigned long int size = positions[(positions.size()-1)];
815 openInputFile(filename, in);
819 while(char c = in.get()){
820 if(in.eof()) { break; }
825 positions.push_back(size);
829 catch(exception& e) {
830 errorOut(e, "MothurOut", "setFilePosFasta");
834 /**************************************************************************************************/
835 vector<unsigned long int> MothurOut::setFilePosEachLine(string filename, int& num) {
837 filename = getFullPathName(filename);
839 vector<unsigned long int> positions;
841 openInputFile(filename, in);
845 unsigned long int lastpos = in.tellg();
847 if (input.length() != 0) {
848 unsigned long int pos = in.tellg();
849 if (pos != -1) { positions.push_back(pos - input.length() - 1); }
850 else { positions.push_back(lastpos); }
852 gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
856 num = positions.size();
859 unsigned long int size;
861 //get num bytes in file
862 pFile = fopen (filename.c_str(),"rb");
863 if (pFile==NULL) perror ("Error opening file");
865 fseek (pFile, 0, SEEK_END);
870 positions.push_back(size);
874 catch(exception& e) {
875 errorOut(e, "MothurOut", "setFilePosEachLine");
879 /**************************************************************************************************/
881 vector<unsigned long int> MothurOut::divideFile(string filename, int& proc) {
884 vector<unsigned long int> filePos;
885 filePos.push_back(0);
888 unsigned long int size;
890 filename = getFullPathName(filename);
892 //get num bytes in file
893 pFile = fopen (filename.c_str(),"rb");
894 if (pFile==NULL) perror ("Error opening file");
896 fseek (pFile, 0, SEEK_END);
901 //estimate file breaks
902 unsigned long int chunkSize = 0;
903 chunkSize = size / proc;
905 //file to small to divide by processors
906 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
908 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
909 for (int i = 0; i < proc; i++) {
910 unsigned long int spot = (i+1) * chunkSize;
913 openInputFile(filename, in);
917 unsigned long int newSpot = spot;
920 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
923 //there was not another sequence before the end of the file
924 unsigned long int sanityPos = in.tellg();
926 if (sanityPos == -1) { break; }
927 else { filePos.push_back(newSpot); }
933 filePos.push_back(size);
935 //sanity check filePos
936 for (int i = 0; i < (filePos.size()-1); i++) {
937 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
940 proc = (filePos.size() - 1);
944 catch(exception& e) {
945 errorOut(e, "MothurOut", "divideFile");
950 /***********************************************************************/
952 bool MothurOut::isTrue(string f){
955 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
957 if ((f == "TRUE") || (f == "T")) { return true; }
958 else { return false; }
960 catch(exception& e) {
961 errorOut(e, "MothurOut", "isTrue");
966 /***********************************************************************/
968 float MothurOut::roundDist(float dist, int precision){
970 return int(dist * precision + 0.5)/float(precision);
972 catch(exception& e) {
973 errorOut(e, "MothurOut", "roundDist");
977 /***********************************************************************/
979 float MothurOut::ceilDist(float dist, int precision){
981 return int(ceil(dist * precision))/float(precision);
983 catch(exception& e) {
984 errorOut(e, "MothurOut", "ceilDist");
989 /***********************************************************************/
991 int MothurOut::getNumNames(string names){
997 for(int i=0;i<names.size();i++){
1006 catch(exception& e) {
1007 errorOut(e, "MothurOut", "getNumNames");
1012 /**************************************************************************************************/
1014 vector<vector<double> > MothurOut::binomial(int maxOrder){
1016 vector<vector<double> > binomial(maxOrder+1);
1018 for(int i=0;i<=maxOrder;i++){
1019 binomial[i].resize(maxOrder+1);
1028 for(int i=2;i<=maxOrder;i++){
1032 for(int i=2;i<=maxOrder;i++){
1033 for(int j=1;j<=maxOrder;j++){
1034 if(i==j){ binomial[i][j]=1; }
1035 if(j>i) { binomial[i][j]=0; }
1036 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
1043 catch(exception& e) {
1044 errorOut(e, "MothurOut", "binomial");
1049 /***********************************************************************/
1051 int MothurOut::factorial(int num){
1055 for (int i = 1; i <= num; i++) {
1061 catch(exception& e) {
1062 errorOut(e, "MothurOut", "factorial");
1066 /***********************************************************************/
1068 int MothurOut::getNumSeqs(ifstream& file){
1070 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1074 catch(exception& e) {
1075 errorOut(e, "MothurOut", "getNumSeqs");
1079 /***********************************************************************/
1080 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1085 input = getline(file);
1086 if (input.length() != 0) {
1087 if(input[0] == '>'){ numSeqs++; }
1091 catch(exception& e) {
1092 errorOut(e, "MothurOut", "getNumSeqs");
1096 /***********************************************************************/
1098 //This function parses the estimator options and puts them in a vector
1099 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1103 while (estim.find_first_of(symbol) != -1) {
1104 individual = estim.substr(0,estim.find_first_of(symbol));
1105 if ((estim.find_first_of(symbol)+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1106 estim = estim.substr(estim.find_first_of(symbol)+1, estim.length());
1107 container.push_back(individual);
1111 container.push_back(estim);
1113 catch(exception& e) {
1114 errorOut(e, "MothurOut", "splitAtChar");
1119 /***********************************************************************/
1121 //This function parses the estimator options and puts them in a vector
1122 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1124 string individual = "";
1125 int estimLength = estim.size();
1126 for(int i=0;i<estimLength;i++){
1127 if(estim[i] == '-'){
1128 container.push_back(individual);
1132 individual += estim[i];
1135 container.push_back(individual);
1138 /* string individual;
1140 while (estim.find_first_of('-') != -1) {
1141 individual = estim.substr(0,estim.find_first_of('-'));
1142 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1143 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1144 container.push_back(individual);
1148 container.push_back(estim); */
1150 catch(exception& e) {
1151 errorOut(e, "MothurOut", "splitAtDash");
1156 /***********************************************************************/
1157 //This function parses the label options and puts them in a set
1158 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1160 string individual = "";
1161 int estimLength = estim.size();
1162 for(int i=0;i<estimLength;i++){
1163 if(estim[i] == '-'){
1164 container.insert(individual);
1168 individual += estim[i];
1171 container.insert(individual);
1173 // string individual;
1175 // while (estim.find_first_of('-') != -1) {
1176 // individual = estim.substr(0,estim.find_first_of('-'));
1177 // if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1178 // estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1179 // container.insert(individual);
1183 // container.insert(estim);
1186 catch(exception& e) {
1187 errorOut(e, "MothurOut", "splitAtDash");
1191 /***********************************************************************/
1192 //This function parses the line options and puts them in a set
1193 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1198 while (estim.find_first_of('-') != -1) {
1199 individual = estim.substr(0,estim.find_first_of('-'));
1200 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1201 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1202 convert(individual, lineNum); //convert the string to int
1203 container.insert(lineNum);
1207 convert(estim, lineNum); //convert the string to int
1208 container.insert(lineNum);
1210 catch(exception& e) {
1211 errorOut(e, "MothurOut", "splitAtDash");
1215 /***********************************************************************/
1216 //This function parses the a string and puts peices in a vector
1217 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1219 string individual = "";
1220 int estimLength = estim.size();
1221 for(int i=0;i<estimLength;i++){
1222 if(estim[i] == ','){
1223 container.push_back(individual);
1227 individual += estim[i];
1230 container.push_back(individual);
1235 // string individual;
1237 // while (estim.find_first_of(',') != -1) {
1238 // individual = estim.substr(0,estim.find_first_of(','));
1239 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1240 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1241 // container.push_back(individual);
1245 // container.push_back(estim);
1247 catch(exception& e) {
1248 errorOut(e, "MothurOut", "splitAtComma");
1252 /***********************************************************************/
1254 //This function splits up the various option parameters
1255 void MothurOut::splitAtComma(string& prefix, string& suffix){
1257 prefix = suffix.substr(0,suffix.find_first_of(','));
1258 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
1259 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1261 while(suffix.at(0) == ' ')
1262 suffix = suffix.substr(1, suffix.length());
1266 catch(exception& e) {
1267 errorOut(e, "MothurOut", "splitAtComma");
1271 /***********************************************************************/
1273 //This function separates the key value from the option value i.e. dist=96_...
1274 void MothurOut::splitAtEquals(string& key, string& value){
1276 if(value.find_first_of('=') != -1){
1277 key = value.substr(0,value.find_first_of('='));
1278 if ((value.find_first_of('=')+1) <= value.length()) {
1279 value = value.substr(value.find_first_of('=')+1, value.length());
1286 catch(exception& e) {
1287 errorOut(e, "MothurOut", "splitAtEquals");
1292 /**************************************************************************************************/
1294 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1296 for (int i = 0; i < Groups.size(); i++) {
1297 if (groupname == Groups[i]) { return true; }
1301 catch(exception& e) {
1302 errorOut(e, "MothurOut", "inUsersGroups");
1306 /**************************************************************************************************/
1307 //returns true if any of the strings in first vector are in second vector
1308 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1311 for (int i = 0; i < groupnames.size(); i++) {
1312 if (inUsersGroups(groupnames[i], Groups)) { return true; }
1316 catch(exception& e) {
1317 errorOut(e, "MothurOut", "inUsersGroups");
1321 /***********************************************************************/
1322 //this function determines if the user has given us labels that are smaller than the given label.
1323 //if so then it returns true so that the calling function can run the previous valid distance.
1324 //it's a "smart" distance function. It also checks for invalid labels.
1325 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1328 set<string>::iterator it;
1329 vector<float> orderFloat;
1330 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
1331 map<string, float>::iterator it2;
1333 bool smaller = false;
1335 //unique is the smallest line
1336 if (label == "unique") { return false; }
1338 if (convertTestFloat(label, labelFloat)) {
1339 convert(label, labelFloat);
1340 }else { //cant convert
1345 //go through users set and make them floats
1346 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
1349 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1351 orderFloat.push_back(temp);
1352 userMap[*it] = temp;
1353 }else if (*it == "unique") {
1354 orderFloat.push_back(-1.0);
1355 userMap["unique"] = -1.0;
1357 if (errorOff == "") { cout << *it << " is not a valid label." << endl; }
1358 userLabels.erase(*it);
1364 sort(orderFloat.begin(), orderFloat.end());
1366 /*************************************************/
1367 //is this label bigger than any of the users labels
1368 /*************************************************/
1370 //loop through order until you find a label greater than label
1371 for (int i = 0; i < orderFloat.size(); i++) {
1372 if (orderFloat[i] < labelFloat) {
1374 if (orderFloat[i] == -1) {
1375 if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
1376 userLabels.erase("unique");
1379 if (errorOff == "") { cout << "Your file does not include the label " << endl; }
1381 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
1382 if (it2->second == orderFloat[i]) {
1384 //remove small labels
1385 userLabels.erase(s);
1389 if (errorOff == "") {cout << s << ". I will use the next smallest distance. " << endl; }
1391 //since they are sorted once you find a bigger one stop looking
1398 catch(exception& e) {
1399 errorOut(e, "MothurOut", "anyLabelsToProcess");
1404 /**************************************************************************************************/
1405 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1410 string line = getline(file);
1412 //before we added this check
1413 if (line[0] != '#') { good = false; }
1416 line = line.substr(1);
1418 vector<string> versionVector;
1419 splitAtChar(version, versionVector, '.');
1421 //check file version
1422 vector<string> linesVector;
1423 splitAtChar(line, linesVector, '.');
1425 if (versionVector.size() != linesVector.size()) { good = false; }
1427 for (int j = 0; j < versionVector.size(); j++) {
1429 convert(versionVector[j], num1);
1430 convert(linesVector[j], num2);
1432 //if mothurs version is newer than this files version, then we want to remake it
1433 if (num1 > num2) { good = false; break; }
1439 if (!good) { file.close(); }
1440 else { file.seekg(0); }
1444 catch(exception& e) {
1445 errorOut(e, "MothurOut", "checkReleaseVersion");
1449 /**************************************************************************************************/