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::setOutputDir(string pathname) {
68 errorOut(e, "MothurOut", "setOutputDir");
72 /*********************************************************************************************/
73 void MothurOut::closeLog() {
78 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
80 if (pid == 0) { //only one process should output to screen
90 errorOut(e, "MothurOut", "closeLog");
95 /*********************************************************************************************/
96 MothurOut::~MothurOut() {
101 catch(exception& e) {
102 errorOut(e, "MothurOut", "MothurOut");
106 /*********************************************************************************************/
107 void MothurOut::mothurOut(string output) {
112 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
114 if (pid == 0) { //only one process should output to screen
124 catch(exception& e) {
125 errorOut(e, "MothurOut", "MothurOut");
129 /*********************************************************************************************/
130 void MothurOut::mothurOutEndLine() {
134 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
136 if (pid == 0) { //only one process should output to screen
146 catch(exception& e) {
147 errorOut(e, "MothurOut", "MothurOutEndLine");
151 /*********************************************************************************************/
152 void MothurOut::mothurOutJustToLog(string output) {
156 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
158 if (pid == 0) { //only one process should output to screen
167 catch(exception& e) {
168 errorOut(e, "MothurOut", "MothurOutJustToLog");
172 /*********************************************************************************************/
173 void MothurOut::errorOut(exception& e, string object, string function) {
175 //mem_usage(vm, rss);
177 mothurOut("[ERROR]: ");
178 mothurOut(toString(e.what()));
179 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.");
182 /*********************************************************************************************/
183 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
184 // process_mem_usage(double &, double &) - takes two doubles by reference,
185 // attempts to read the system-dependent data for a process' virtual memory
186 // size and resident set size, and return the results in KB.
188 // On failure, returns 0.0, 0.0
189 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
190 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
195 // 'file' stat seems to give the most reliable results
197 ifstream stat_stream("/proc/self/stat",ios_base::in);
199 // dummy vars for leading entries in stat that we don't care about
201 string pid, comm, state, ppid, pgrp, session, tty_nr;
202 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
203 string utime, stime, cutime, cstime, priority, nice;
204 string O, itrealvalue, starttime;
206 // the two fields we want
211 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
212 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
213 >> utime >> stime >> cutime >> cstime >> priority >> nice
214 >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
216 long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
217 vm_usage = vsize / 1024.0;
218 resident_set = rss * page_size_kb;
220 mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
224 /* //windows memory usage
225 // Get the list of process identifiers.
226 DWORD aProcesses[1024], cbNeeded, cProcesses;
228 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
230 // Calculate how many process identifiers were returned.
231 cProcesses = cbNeeded / sizeof(DWORD);
233 // Print the memory usage for each process
234 for (int i = 0; i < cProcesses; i++ ) {
235 DWORD processID = aProcesses[i];
237 PROCESS_MEMORY_COUNTERS pmc;
239 HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
241 // Print the process identifier.
242 printf( "\nProcess ID: %u\n", processID);
244 if (NULL != hProcess) {
246 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
247 printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
248 printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
249 printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
250 printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
251 printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
252 printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
253 printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
254 printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
255 printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
257 CloseHandle(hProcess);
267 /***********************************************************************/
268 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
270 fileName = getFullPathName(fileName);
272 fileHandle.open(fileName.c_str(), ios::app);
274 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
281 catch(exception& e) {
282 errorOut(e, "MothurOut", "openOutputFileAppend");
286 /***********************************************************************/
287 void MothurOut::gobble(istream& f){
291 while(isspace(d=f.get())) { ;}
294 catch(exception& e) {
295 errorOut(e, "MothurOut", "gobble");
299 /***********************************************************************/
300 void MothurOut::gobble(istringstream& f){
303 while(isspace(d=f.get())) {;}
306 catch(exception& e) {
307 errorOut(e, "MothurOut", "gobble");
312 /***********************************************************************/
314 string MothurOut::getline(istringstream& fileHandle) {
319 while (!fileHandle.eof()) {
321 char c = fileHandle.get();
323 //are you at the end of the line
324 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
331 catch(exception& e) {
332 errorOut(e, "MothurOut", "getline");
336 /***********************************************************************/
338 string MothurOut::getline(ifstream& fileHandle) {
343 while (!fileHandle.eof()) {
345 char c = fileHandle.get();
347 //are you at the end of the line
348 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
355 catch(exception& e) {
356 errorOut(e, "MothurOut", "getline");
360 /***********************************************************************/
362 #ifdef USE_COMPRESSION
363 inline bool endsWith(string s, const char * suffix){
364 size_t suffixLength = strlen(suffix);
365 return s.size() >= suffixLength && s.substr(s.size() - suffixLength, suffixLength).compare(suffix) == 0;
369 string MothurOut::getRootName(string longName){
372 string rootName = longName;
374 #ifdef USE_COMPRESSION
375 if (endsWith(rootName, ".gz") || endsWith(rootName, ".bz2")) {
376 int pos = rootName.find_last_of('.');
377 rootName = rootName.substr(0, pos);
378 cerr << "shortening " << longName << " to " << rootName << "\n";
382 if(rootName.find_last_of(".") != rootName.npos){
383 int pos = rootName.find_last_of('.')+1;
384 rootName = rootName.substr(0, pos);
389 catch(exception& e) {
390 errorOut(e, "MothurOut", "getRootName");
394 /***********************************************************************/
396 string MothurOut::getSimpleName(string longName){
398 string simpleName = longName;
401 found=longName.find_last_of("/\\");
403 if(found != longName.npos){
404 simpleName = longName.substr(found+1);
409 catch(exception& e) {
410 errorOut(e, "MothurOut", "getSimpleName");
415 /***********************************************************************/
417 string MothurOut::getPathName(string longName){
419 string rootPathName = longName;
421 if(longName.find_last_of("/\\") != longName.npos){
422 int pos = longName.find_last_of("/\\")+1;
423 rootPathName = longName.substr(0, pos);
428 catch(exception& e) {
429 errorOut(e, "MothurOut", "getPathName");
434 /***********************************************************************/
436 string MothurOut::hasPath(string longName){
441 found=longName.find_last_of("~/\\");
443 if(found != longName.npos){
444 path = longName.substr(0, found+1);
449 catch(exception& e) {
450 errorOut(e, "MothurOut", "hasPath");
455 /***********************************************************************/
457 string MothurOut::getExtension(string longName){
459 string extension = longName;
461 if(longName.find_last_of('.') != longName.npos){
462 int pos = longName.find_last_of('.');
463 extension = longName.substr(pos, longName.length());
468 catch(exception& e) {
469 errorOut(e, "MothurOut", "getExtension");
473 /***********************************************************************/
474 bool MothurOut::isBlank(string fileName){
477 fileName = getFullPathName(fileName);
480 fileHandle.open(fileName.c_str());
482 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
485 //check for blank file
487 if (fileHandle.eof()) { fileHandle.close(); return true; }
491 catch(exception& e) {
492 errorOut(e, "MothurOut", "isBlank");
496 /***********************************************************************/
498 string MothurOut::getFullPathName(string fileName){
501 string path = hasPath(fileName);
505 if (path == "") { return fileName; } //its a simple name
506 else { //we need to complete the pathname
507 // ex. ../../../filename
508 // cwd = /user/work/desktop
511 //get current working directory
512 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
514 if (path.find("~") != -1) { //go to home directory
517 char *homepath = NULL;
518 homepath = getenv ("HOME");
519 if ( homepath != NULL) { homeDir = homepath; }
520 else { homeDir = ""; }
522 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
525 if (path.rfind("./") == -1) { return fileName; } //already complete name
526 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
528 //char* cwdpath = new char[1024];
530 //cwdpath=getcwd(cwdpath,size);
533 char *cwdpath = NULL;
534 cwdpath = getcwd(NULL, 0); // or _getcwd
535 if ( cwdpath != NULL) { cwd = cwdpath; }
541 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
543 //break apart the current working directory
545 while (simpleCWD.find_first_of('/') != -1) {
546 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
547 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
550 //get last one // ex. ../../../filename = /user/work/desktop/filename
551 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
554 int index = dirs.size()-1;
556 while((pos = path.rfind("./")) != -1) { //while you don't have a complete path
557 if (pos == 0) { break; //you are at the end
558 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
559 path = path.substr(0, pos-1);
561 if (index == 0) { break; }
562 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
563 path = path.substr(0, pos);
564 }else if (pos == 1) { break; //you are at the end
565 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
568 for (int i = index; i >= 0; i--) {
569 newFileName = dirs[i] + "/" + newFileName;
572 newFileName = "/" + newFileName;
576 if (path.find("~") != -1) { //go to home directory
577 string homeDir = getenv ("HOMEPATH");
578 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
581 if (path.rfind(".\\") == -1) { return fileName; } //already complete name
582 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
584 char *cwdpath = NULL;
585 cwdpath = getcwd(NULL, 0); // or _getcwd
586 if ( cwdpath != NULL) { cwd = cwdpath; }
589 //break apart the current working directory
591 while (cwd.find_first_of('\\') != -1) {
592 string dir = cwd.substr(0,cwd.find_first_of('\\'));
593 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
598 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
600 int index = dirs.size()-1;
602 while((pos = path.rfind(".\\")) != -1) { //while you don't have a complete path
603 if (pos == 0) { break; //you are at the end
604 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
605 path = path.substr(0, pos-1);
607 if (index == 0) { break; }
608 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
609 path = path.substr(0, pos);
610 }else if (pos == 1) { break; //you are at the end
611 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
614 for (int i = index; i >= 0; i--) {
615 newFileName = dirs[i] + "\\" + newFileName;
624 catch(exception& e) {
625 errorOut(e, "MothurOut", "getFullPathName");
629 /***********************************************************************/
631 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
634 string completeFileName = getFullPathName(fileName);
636 #ifdef USE_COMPRESSION
637 // check for gzipped or bzipped file
638 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
639 string tempName = string(tmpnam(0));
640 mkfifo(tempName.c_str(), 0666);
641 int fork_result = fork();
642 if (fork_result < 0) {
643 cerr << "Error forking.\n";
645 } else if (fork_result == 0) {
646 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
647 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
648 system(command.c_str());
649 cerr << "Done decompressing " << completeFileName << "\n";
650 remove(tempName.c_str());
653 cerr << "waiting on child process " << fork_result << "\n";
654 completeFileName = tempName;
659 fileHandle.open(completeFileName.c_str());
661 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
664 //check for blank file
669 catch(exception& e) {
670 errorOut(e, "MothurOut", "openInputFile - no Error");
674 /***********************************************************************/
676 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
680 string completeFileName = getFullPathName(fileName);
682 #ifdef USE_COMPRESSION
683 // check for gzipped or bzipped file
684 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
685 string tempName = string(tmpnam(0));
686 mkfifo(tempName.c_str(), 0666);
687 int fork_result = fork();
688 if (fork_result < 0) {
689 cerr << "Error forking.\n";
691 } else if (fork_result == 0) {
692 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
693 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
694 system(command.c_str());
695 cerr << "Done decompressing " << completeFileName << "\n";
696 remove(tempName.c_str());
699 cerr << "waiting on child process " << fork_result << "\n";
700 completeFileName = tempName;
706 fileHandle.open(completeFileName.c_str());
708 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
712 //check for blank file
714 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
719 catch(exception& e) {
720 errorOut(e, "MothurOut", "openInputFile");
724 /***********************************************************************/
726 int MothurOut::renameFile(string oldName, string newName){
729 int exist = openInputFile(newName, inTest, "");
731 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
732 if (exist == 0) { //you could open it so you want to delete it
734 string command = "rm " + newName;
735 system(command.c_str());
738 string command = "mv " + oldName + " " + newName;
739 system(command.c_str());
741 remove(newName.c_str());
742 int renameOk = rename(oldName.c_str(), newName.c_str());
747 catch(exception& e) {
748 errorOut(e, "MothurOut", "renameFile");
753 /***********************************************************************/
755 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
758 string completeFileName = getFullPathName(fileName);
760 #ifdef USE_COMPRESSION
761 // check for gzipped file
762 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
763 string tempName = string(tmpnam(0));
764 mkfifo(tempName.c_str(), 0666);
765 cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
766 int fork_result = fork();
767 if (fork_result < 0) {
768 cerr << "Error forking.\n";
770 } else if (fork_result == 0) {
771 string command = string(endsWith(completeFileName, ".gz") ? "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
772 system(command.c_str());
775 completeFileName = tempName;
780 fileHandle.open(completeFileName.c_str(), ios::trunc);
782 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
789 catch(exception& e) {
790 errorOut(e, "MothurOut", "openOutputFile");
796 /**************************************************************************************************/
797 void MothurOut::appendFiles(string temp, string filename) {
802 //open output file in append mode
803 openOutputFileAppend(filename, output);
804 int ableToOpen = openInputFile(temp, input, "no error");
805 //int ableToOpen = openInputFile(temp, input);
807 if (ableToOpen == 0) { //you opened it
808 while(char c = input.get()){
809 if(input.eof()) { break; }
810 else { output << c; }
817 catch(exception& e) {
818 errorOut(e, "MothurOut", "appendFiles");
823 /**************************************************************************************************/
824 string MothurOut::sortFile(string distFile, string outputDir){
827 //if (outputDir == "") { outputDir += hasPath(distFile); }
828 string outfile = getRootName(distFile) + "sorted.dist";
831 //if you can, use the unix sort since its been optimized for years
832 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
833 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
834 system(command.c_str());
835 #else //you are stuck with my best attempt...
836 //windows sort does not have a way to specify a column, only a character in the line
837 //since we cannot assume that the distance will always be at the the same character location on each line
838 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
840 //read in file line by file and put distance first
841 string tempDistFile = distFile + ".temp";
844 openInputFile(distFile, input);
845 openOutputFile(tempDistFile, output);
847 string firstName, secondName;
850 input >> firstName >> secondName >> dist;
851 output << dist << '\t' << firstName << '\t' << secondName << endl;
858 //sort using windows sort
859 string tempOutfile = outfile + ".temp";
860 string command = "sort " + tempDistFile + " /O " + tempOutfile;
861 system(command.c_str());
863 //read in sorted file and put distance at end again
865 openInputFile(tempOutfile, input2);
866 openOutputFile(outfile, output);
869 input2 >> dist >> firstName >> secondName;
870 output << firstName << '\t' << secondName << '\t' << dist << endl;
877 remove(tempDistFile.c_str());
878 remove(tempOutfile.c_str());
883 catch(exception& e) {
884 errorOut(e, "MothurOut", "sortFile");
888 /**************************************************************************************************/
889 vector<unsigned long int> MothurOut::setFilePosFasta(string filename, int& num) {
891 vector<unsigned long int> positions;
893 openInputFile(filename, inFASTA);
896 while(!inFASTA.eof()){
897 input = getline(inFASTA);
898 if (input.length() != 0) {
899 if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
901 gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
905 num = positions.size();
910 //get num bytes in file
911 pFile = fopen (filename.c_str(),"rb");
912 if (pFile==NULL) perror ("Error opening file");
914 fseek (pFile, 0, SEEK_END);
919 unsigned long int size = positions[(positions.size()-1)];
921 openInputFile(filename, in);
925 while(char c = in.get()){
926 if(in.eof()) { break; }
931 positions.push_back(size);
935 catch(exception& e) {
936 errorOut(e, "MothurOut", "setFilePosFasta");
940 /**************************************************************************************************/
941 vector<unsigned long int> MothurOut::setFilePosEachLine(string filename, int& num) {
943 filename = getFullPathName(filename);
945 vector<unsigned long int> positions;
947 openInputFile(filename, in);
951 unsigned long int lastpos = in.tellg();
953 if (input.length() != 0) {
954 unsigned long int pos = in.tellg();
955 if (pos != -1) { positions.push_back(pos - input.length() - 1); }
956 else { positions.push_back(lastpos); }
958 gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
962 num = positions.size();
965 unsigned long int size;
967 //get num bytes in file
968 pFile = fopen (filename.c_str(),"rb");
969 if (pFile==NULL) perror ("Error opening file");
971 fseek (pFile, 0, SEEK_END);
976 positions.push_back(size);
980 catch(exception& e) {
981 errorOut(e, "MothurOut", "setFilePosEachLine");
985 /**************************************************************************************************/
987 vector<unsigned long int> MothurOut::divideFile(string filename, int& proc) {
990 vector<unsigned long int> filePos;
991 filePos.push_back(0);
994 unsigned long int size;
996 filename = getFullPathName(filename);
998 //get num bytes in file
999 pFile = fopen (filename.c_str(),"rb");
1000 if (pFile==NULL) perror ("Error opening file");
1002 fseek (pFile, 0, SEEK_END);
1007 //estimate file breaks
1008 unsigned long int chunkSize = 0;
1009 chunkSize = size / proc;
1011 //file to small to divide by processors
1012 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1014 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1015 for (int i = 0; i < proc; i++) {
1016 unsigned long int spot = (i+1) * chunkSize;
1019 openInputFile(filename, in);
1023 unsigned long int newSpot = spot;
1026 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1029 //there was not another sequence before the end of the file
1030 unsigned long int sanityPos = in.tellg();
1032 if (sanityPos == -1) { break; }
1033 else { filePos.push_back(newSpot); }
1039 filePos.push_back(size);
1041 //sanity check filePos
1042 for (int i = 0; i < (filePos.size()-1); i++) {
1043 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1046 proc = (filePos.size() - 1);
1050 catch(exception& e) {
1051 errorOut(e, "MothurOut", "divideFile");
1056 /***********************************************************************/
1058 bool MothurOut::isTrue(string f){
1061 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1063 if ((f == "TRUE") || (f == "T")) { return true; }
1064 else { return false; }
1066 catch(exception& e) {
1067 errorOut(e, "MothurOut", "isTrue");
1072 /***********************************************************************/
1074 float MothurOut::roundDist(float dist, int precision){
1076 return int(dist * precision + 0.5)/float(precision);
1078 catch(exception& e) {
1079 errorOut(e, "MothurOut", "roundDist");
1083 /***********************************************************************/
1085 float MothurOut::ceilDist(float dist, int precision){
1087 return int(ceil(dist * precision))/float(precision);
1089 catch(exception& e) {
1090 errorOut(e, "MothurOut", "ceilDist");
1095 /***********************************************************************/
1097 int MothurOut::getNumNames(string names){
1103 for(int i=0;i<names.size();i++){
1104 if(names[i] == ','){
1112 catch(exception& e) {
1113 errorOut(e, "MothurOut", "getNumNames");
1118 /**************************************************************************************************/
1120 vector<vector<double> > MothurOut::binomial(int maxOrder){
1122 vector<vector<double> > binomial(maxOrder+1);
1124 for(int i=0;i<=maxOrder;i++){
1125 binomial[i].resize(maxOrder+1);
1134 for(int i=2;i<=maxOrder;i++){
1138 for(int i=2;i<=maxOrder;i++){
1139 for(int j=1;j<=maxOrder;j++){
1140 if(i==j){ binomial[i][j]=1; }
1141 if(j>i) { binomial[i][j]=0; }
1142 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
1149 catch(exception& e) {
1150 errorOut(e, "MothurOut", "binomial");
1155 /***********************************************************************/
1157 int MothurOut::factorial(int num){
1161 for (int i = 1; i <= num; i++) {
1167 catch(exception& e) {
1168 errorOut(e, "MothurOut", "factorial");
1172 /***********************************************************************/
1174 int MothurOut::getNumSeqs(ifstream& file){
1176 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1180 catch(exception& e) {
1181 errorOut(e, "MothurOut", "getNumSeqs");
1185 /***********************************************************************/
1186 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1191 input = getline(file);
1192 if (input.length() != 0) {
1193 if(input[0] == '>'){ numSeqs++; }
1197 catch(exception& e) {
1198 errorOut(e, "MothurOut", "getNumSeqs");
1202 /***********************************************************************/
1204 //This function parses the estimator options and puts them in a vector
1205 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1207 string individual = "";
1208 int estimLength = estim.size();
1209 for(int i=0;i<estimLength;i++){
1210 if(estim[i] == symbol){
1211 container.push_back(individual);
1215 individual += estim[i];
1218 container.push_back(individual);
1222 while (estim.find_first_of(symbol) != -1) {
1223 individual = estim.substr(0,estim.find_first_of(symbol));
1224 if ((estim.find_first_of(symbol)+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1225 estim = estim.substr(estim.find_first_of(symbol)+1, estim.length());
1226 container.push_back(individual);
1230 container.push_back(estim); */
1232 catch(exception& e) {
1233 errorOut(e, "MothurOut", "splitAtChar");
1238 /***********************************************************************/
1240 //This function parses the estimator options and puts them in a vector
1241 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1243 string individual = "";
1244 int estimLength = estim.size();
1245 for(int i=0;i<estimLength;i++){
1246 if(estim[i] == '-'){
1247 container.push_back(individual);
1251 individual += estim[i];
1254 container.push_back(individual);
1257 /* string individual;
1259 while (estim.find_first_of('-') != -1) {
1260 individual = estim.substr(0,estim.find_first_of('-'));
1261 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1262 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1263 container.push_back(individual);
1267 container.push_back(estim); */
1269 catch(exception& e) {
1270 errorOut(e, "MothurOut", "splitAtDash");
1275 /***********************************************************************/
1276 //This function parses the label options and puts them in a set
1277 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1279 string individual = "";
1280 int estimLength = estim.size();
1281 for(int i=0;i<estimLength;i++){
1282 if(estim[i] == '-'){
1283 container.insert(individual);
1287 individual += estim[i];
1290 container.insert(individual);
1292 // string individual;
1294 // while (estim.find_first_of('-') != -1) {
1295 // individual = estim.substr(0,estim.find_first_of('-'));
1296 // if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1297 // estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1298 // container.insert(individual);
1302 // container.insert(estim);
1305 catch(exception& e) {
1306 errorOut(e, "MothurOut", "splitAtDash");
1310 /***********************************************************************/
1311 //This function parses the line options and puts them in a set
1312 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1317 while (estim.find_first_of('-') != -1) {
1318 individual = estim.substr(0,estim.find_first_of('-'));
1319 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1320 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1321 convert(individual, lineNum); //convert the string to int
1322 container.insert(lineNum);
1326 convert(estim, lineNum); //convert the string to int
1327 container.insert(lineNum);
1329 catch(exception& e) {
1330 errorOut(e, "MothurOut", "splitAtDash");
1334 /***********************************************************************/
1335 //This function parses the a string and puts peices in a vector
1336 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1338 string individual = "";
1339 int estimLength = estim.size();
1340 for(int i=0;i<estimLength;i++){
1341 if(estim[i] == ','){
1342 container.push_back(individual);
1346 individual += estim[i];
1349 container.push_back(individual);
1354 // string individual;
1356 // while (estim.find_first_of(',') != -1) {
1357 // individual = estim.substr(0,estim.find_first_of(','));
1358 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1359 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1360 // container.push_back(individual);
1364 // container.push_back(estim);
1366 catch(exception& e) {
1367 errorOut(e, "MothurOut", "splitAtComma");
1371 /***********************************************************************/
1373 //This function splits up the various option parameters
1374 void MothurOut::splitAtComma(string& prefix, string& suffix){
1376 prefix = suffix.substr(0,suffix.find_first_of(','));
1377 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
1378 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1380 while(suffix.at(0) == ' ')
1381 suffix = suffix.substr(1, suffix.length());
1385 catch(exception& e) {
1386 errorOut(e, "MothurOut", "splitAtComma");
1390 /***********************************************************************/
1392 //This function separates the key value from the option value i.e. dist=96_...
1393 void MothurOut::splitAtEquals(string& key, string& value){
1395 if(value.find_first_of('=') != -1){
1396 key = value.substr(0,value.find_first_of('='));
1397 if ((value.find_first_of('=')+1) <= value.length()) {
1398 value = value.substr(value.find_first_of('=')+1, value.length());
1405 catch(exception& e) {
1406 errorOut(e, "MothurOut", "splitAtEquals");
1411 /**************************************************************************************************/
1413 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1415 for (int i = 0; i < Groups.size(); i++) {
1416 if (groupname == Groups[i]) { return true; }
1420 catch(exception& e) {
1421 errorOut(e, "MothurOut", "inUsersGroups");
1425 /**************************************************************************************************/
1426 //returns true if any of the strings in first vector are in second vector
1427 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1430 for (int i = 0; i < groupnames.size(); i++) {
1431 if (inUsersGroups(groupnames[i], Groups)) { return true; }
1435 catch(exception& e) {
1436 errorOut(e, "MothurOut", "inUsersGroups");
1440 /***********************************************************************/
1441 //this function determines if the user has given us labels that are smaller than the given label.
1442 //if so then it returns true so that the calling function can run the previous valid distance.
1443 //it's a "smart" distance function. It also checks for invalid labels.
1444 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1447 set<string>::iterator it;
1448 vector<float> orderFloat;
1449 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
1450 map<string, float>::iterator it2;
1452 bool smaller = false;
1454 //unique is the smallest line
1455 if (label == "unique") { return false; }
1457 if (convertTestFloat(label, labelFloat)) {
1458 convert(label, labelFloat);
1459 }else { //cant convert
1464 //go through users set and make them floats
1465 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
1468 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1470 orderFloat.push_back(temp);
1471 userMap[*it] = temp;
1472 }else if (*it == "unique") {
1473 orderFloat.push_back(-1.0);
1474 userMap["unique"] = -1.0;
1476 if (errorOff == "") { cout << *it << " is not a valid label." << endl; }
1477 userLabels.erase(*it);
1483 sort(orderFloat.begin(), orderFloat.end());
1485 /*************************************************/
1486 //is this label bigger than any of the users labels
1487 /*************************************************/
1489 //loop through order until you find a label greater than label
1490 for (int i = 0; i < orderFloat.size(); i++) {
1491 if (orderFloat[i] < labelFloat) {
1493 if (orderFloat[i] == -1) {
1494 if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
1495 userLabels.erase("unique");
1498 if (errorOff == "") { cout << "Your file does not include the label " << endl; }
1500 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
1501 if (it2->second == orderFloat[i]) {
1503 //remove small labels
1504 userLabels.erase(s);
1508 if (errorOff == "") {cout << s << ". I will use the next smallest distance. " << endl; }
1510 //since they are sorted once you find a bigger one stop looking
1517 catch(exception& e) {
1518 errorOut(e, "MothurOut", "anyLabelsToProcess");
1523 /**************************************************************************************************/
1524 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1529 string line = getline(file);
1531 //before we added this check
1532 if (line[0] != '#') { good = false; }
1535 line = line.substr(1);
1537 vector<string> versionVector;
1538 splitAtChar(version, versionVector, '.');
1540 //check file version
1541 vector<string> linesVector;
1542 splitAtChar(line, linesVector, '.');
1544 if (versionVector.size() != linesVector.size()) { good = false; }
1546 for (int j = 0; j < versionVector.size(); j++) {
1548 convert(versionVector[j], num1);
1549 convert(linesVector[j], num2);
1551 //if mothurs version is newer than this files version, then we want to remake it
1552 if (num1 > num2) { good = false; break; }
1558 if (!good) { file.close(); }
1559 else { file.seekg(0); }
1563 catch(exception& e) {
1564 errorOut(e, "MothurOut", "checkReleaseVersion");
1569 /**************************************************************************************************/