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){
281 while(isspace(d=f.get())) { ;}
284 catch(exception& e) {
285 errorOut(e, "MothurOut", "gobble");
289 /***********************************************************************/
290 void MothurOut::gobble(istringstream& f){
293 while(isspace(d=f.get())) {;}
296 catch(exception& e) {
297 errorOut(e, "MothurOut", "gobble");
302 /***********************************************************************/
304 string MothurOut::getline(istringstream& fileHandle) {
309 while (!fileHandle.eof()) {
311 char c = fileHandle.get();
313 //are you at the end of the line
314 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
321 catch(exception& e) {
322 errorOut(e, "MothurOut", "getline");
326 /***********************************************************************/
328 string MothurOut::getline(ifstream& fileHandle) {
333 while (!fileHandle.eof()) {
335 char c = fileHandle.get();
337 //are you at the end of the line
338 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
345 catch(exception& e) {
346 errorOut(e, "MothurOut", "getline");
350 /***********************************************************************/
352 #ifdef USE_COMPRESSION
353 inline bool endsWith(string s, const char * suffix){
354 size_t suffixLength = strlen(suffix);
355 return s.size() >= suffixLength && s.substr(s.size() - suffixLength, suffixLength).compare(suffix) == 0;
359 string MothurOut::getRootName(string longName){
362 string rootName = longName;
364 #ifdef USE_COMPRESSION
365 if (endsWith(rootName, ".gz") || endsWith(rootName, ".bz2")) {
366 int pos = rootName.find_last_of('.');
367 rootName = rootName.substr(0, pos);
368 cerr << "shortening " << longName << " to " << rootName << "\n";
372 if(rootName.find_last_of(".") != rootName.npos){
373 int pos = rootName.find_last_of('.')+1;
374 rootName = rootName.substr(0, pos);
379 catch(exception& e) {
380 errorOut(e, "MothurOut", "getRootName");
384 /***********************************************************************/
386 string MothurOut::getSimpleName(string longName){
388 string simpleName = longName;
391 found=longName.find_last_of("/\\");
393 if(found != longName.npos){
394 simpleName = longName.substr(found+1);
399 catch(exception& e) {
400 errorOut(e, "MothurOut", "getSimpleName");
405 /***********************************************************************/
407 string MothurOut::getPathName(string longName){
409 string rootPathName = longName;
411 if(longName.find_last_of("/\\") != longName.npos){
412 int pos = longName.find_last_of("/\\")+1;
413 rootPathName = longName.substr(0, pos);
418 catch(exception& e) {
419 errorOut(e, "MothurOut", "getPathName");
424 /***********************************************************************/
426 string MothurOut::hasPath(string longName){
431 found=longName.find_last_of("~/\\");
433 if(found != longName.npos){
434 path = longName.substr(0, found+1);
439 catch(exception& e) {
440 errorOut(e, "MothurOut", "hasPath");
445 /***********************************************************************/
447 string MothurOut::getExtension(string longName){
449 string extension = longName;
451 if(longName.find_last_of('.') != longName.npos){
452 int pos = longName.find_last_of('.');
453 extension = longName.substr(pos, longName.length());
458 catch(exception& e) {
459 errorOut(e, "MothurOut", "getExtension");
463 /***********************************************************************/
464 bool MothurOut::isBlank(string fileName){
467 fileName = getFullPathName(fileName);
470 fileHandle.open(fileName.c_str());
472 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
475 //check for blank file
477 if (fileHandle.eof()) { fileHandle.close(); return true; }
481 catch(exception& e) {
482 errorOut(e, "MothurOut", "isBlank");
486 /***********************************************************************/
488 string MothurOut::getFullPathName(string fileName){
491 string path = hasPath(fileName);
495 if (path == "") { return fileName; } //its a simple name
496 else { //we need to complete the pathname
497 // ex. ../../../filename
498 // cwd = /user/work/desktop
501 //get current working directory
502 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
504 if (path.find("~") != -1) { //go to home directory
507 char *homepath = NULL;
508 homepath = getenv ("HOME");
509 if ( homepath != NULL) { homeDir = homepath; }
510 else { homeDir = ""; }
512 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
515 if (path.rfind("./") == -1) { return fileName; } //already complete name
516 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
518 //char* cwdpath = new char[1024];
520 //cwdpath=getcwd(cwdpath,size);
523 char *cwdpath = NULL;
524 cwdpath = getcwd(NULL, 0); // or _getcwd
525 if ( cwdpath != NULL) { cwd = cwdpath; }
531 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
533 //break apart the current working directory
535 while (simpleCWD.find_first_of('/') != -1) {
536 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
537 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
540 //get last one // ex. ../../../filename = /user/work/desktop/filename
541 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
544 int index = dirs.size()-1;
546 while((pos = path.rfind("./")) != -1) { //while you don't have a complete path
547 if (pos == 0) { break; //you are at the end
548 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
549 path = path.substr(0, pos-1);
551 if (index == 0) { break; }
552 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
553 path = path.substr(0, pos);
554 }else if (pos == 1) { break; //you are at the end
555 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
558 for (int i = index; i >= 0; i--) {
559 newFileName = dirs[i] + "/" + newFileName;
562 newFileName = "/" + newFileName;
566 if (path.find("~") != -1) { //go to home directory
567 string homeDir = getenv ("HOMEPATH");
568 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
571 if (path.rfind(".\\") == -1) { return fileName; } //already complete name
572 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
574 char *cwdpath = NULL;
575 cwdpath = getcwd(NULL, 0); // or _getcwd
576 if ( cwdpath != NULL) { cwd = cwdpath; }
579 //break apart the current working directory
581 while (cwd.find_first_of('\\') != -1) {
582 string dir = cwd.substr(0,cwd.find_first_of('\\'));
583 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
588 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
590 int index = dirs.size()-1;
592 while((pos = path.rfind(".\\")) != -1) { //while you don't have a complete path
593 if (pos == 0) { break; //you are at the end
594 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
595 path = path.substr(0, pos-1);
597 if (index == 0) { break; }
598 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
599 path = path.substr(0, pos);
600 }else if (pos == 1) { break; //you are at the end
601 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
604 for (int i = index; i >= 0; i--) {
605 newFileName = dirs[i] + "\\" + newFileName;
614 catch(exception& e) {
615 errorOut(e, "MothurOut", "getFullPathName");
619 /***********************************************************************/
621 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
624 string completeFileName = getFullPathName(fileName);
626 #ifdef USE_COMPRESSION
627 // check for gzipped or bzipped file
628 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
629 string tempName = string(tmpnam(0));
630 mkfifo(tempName.c_str(), 0666);
631 int fork_result = fork();
632 if (fork_result < 0) {
633 cerr << "Error forking.\n";
635 } else if (fork_result == 0) {
636 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
637 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
638 system(command.c_str());
639 cerr << "Done decompressing " << completeFileName << "\n";
640 remove(tempName.c_str());
643 cerr << "waiting on child process " << fork_result << "\n";
644 completeFileName = tempName;
649 fileHandle.open(completeFileName.c_str());
653 //check for blank file
658 catch(exception& e) {
659 errorOut(e, "MothurOut", "openInputFile - no Error");
663 /***********************************************************************/
665 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
669 string completeFileName = getFullPathName(fileName);
671 #ifdef USE_COMPRESSION
672 // check for gzipped or bzipped file
673 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
674 string tempName = string(tmpnam(0));
675 mkfifo(tempName.c_str(), 0666);
676 int fork_result = fork();
677 if (fork_result < 0) {
678 cerr << "Error forking.\n";
680 } else if (fork_result == 0) {
681 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
682 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
683 system(command.c_str());
684 cerr << "Done decompressing " << completeFileName << "\n";
685 remove(tempName.c_str());
688 cerr << "waiting on child process " << fork_result << "\n";
689 completeFileName = tempName;
695 fileHandle.open(completeFileName.c_str());
697 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
701 //check for blank file
703 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
708 catch(exception& e) {
709 errorOut(e, "MothurOut", "openInputFile");
713 /***********************************************************************/
715 int MothurOut::renameFile(string oldName, string newName){
718 int exist = openInputFile(newName, inTest, "");
720 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
721 if (exist == 0) { //you could open it so you want to delete it
723 string command = "rm " + newName;
724 system(command.c_str());
727 string command = "mv " + oldName + " " + newName;
728 system(command.c_str());
730 remove(newName.c_str());
731 int renameOk = rename(oldName.c_str(), newName.c_str());
736 catch(exception& e) {
737 errorOut(e, "MothurOut", "renameFile");
742 /***********************************************************************/
744 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
747 string completeFileName = getFullPathName(fileName);
749 #ifdef USE_COMPRESSION
750 // check for gzipped file
751 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
752 string tempName = string(tmpnam(0));
753 mkfifo(tempName.c_str(), 0666);
754 cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
755 int fork_result = fork();
756 if (fork_result < 0) {
757 cerr << "Error forking.\n";
759 } else if (fork_result == 0) {
760 string command = string(endsWith(completeFileName, ".gz") ? "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
761 system(command.c_str());
764 completeFileName = tempName;
769 fileHandle.open(completeFileName.c_str(), ios::trunc);
771 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
778 catch(exception& e) {
779 errorOut(e, "MothurOut", "openOutputFile");
785 /**************************************************************************************************/
786 void MothurOut::appendFiles(string temp, string filename) {
791 //open output file in append mode
792 openOutputFileAppend(filename, output);
793 int ableToOpen = openInputFile(temp, input, "no error");
795 if (ableToOpen == 0) { //you opened it
796 while(char c = input.get()){
797 if(input.eof()) { break; }
798 else { output << c; }
805 catch(exception& e) {
806 errorOut(e, "MothurOut", "appendFiles");
811 /**************************************************************************************************/
812 string MothurOut::sortFile(string distFile, string outputDir){
815 //if (outputDir == "") { outputDir += hasPath(distFile); }
816 string outfile = getRootName(distFile) + "sorted.dist";
819 //if you can, use the unix sort since its been optimized for years
820 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
821 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
822 system(command.c_str());
823 #else //you are stuck with my best attempt...
824 //windows sort does not have a way to specify a column, only a character in the line
825 //since we cannot assume that the distance will always be at the the same character location on each line
826 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
828 //read in file line by file and put distance first
829 string tempDistFile = distFile + ".temp";
832 openInputFile(distFile, input);
833 openOutputFile(tempDistFile, output);
835 string firstName, secondName;
838 input >> firstName >> secondName >> dist;
839 output << dist << '\t' << firstName << '\t' << secondName << endl;
846 //sort using windows sort
847 string tempOutfile = outfile + ".temp";
848 string command = "sort " + tempDistFile + " /O " + tempOutfile;
849 system(command.c_str());
851 //read in sorted file and put distance at end again
853 openInputFile(tempOutfile, input2);
854 openOutputFile(outfile, output);
857 input2 >> dist >> firstName >> secondName;
858 output << firstName << '\t' << secondName << '\t' << dist << endl;
865 remove(tempDistFile.c_str());
866 remove(tempOutfile.c_str());
871 catch(exception& e) {
872 errorOut(e, "MothurOut", "sortFile");
876 /**************************************************************************************************/
877 vector<unsigned long int> MothurOut::setFilePosFasta(string filename, int& num) {
879 vector<unsigned long int> positions;
881 openInputFile(filename, inFASTA);
884 while(!inFASTA.eof()){
885 input = getline(inFASTA);
886 if (input.length() != 0) {
887 if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
889 gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
893 num = positions.size();
898 //get num bytes in file
899 pFile = fopen (filename.c_str(),"rb");
900 if (pFile==NULL) perror ("Error opening file");
902 fseek (pFile, 0, SEEK_END);
907 unsigned long int size = positions[(positions.size()-1)];
909 openInputFile(filename, in);
913 while(char c = in.get()){
914 if(in.eof()) { break; }
919 positions.push_back(size);
923 catch(exception& e) {
924 errorOut(e, "MothurOut", "setFilePosFasta");
928 /**************************************************************************************************/
929 vector<unsigned long int> MothurOut::setFilePosEachLine(string filename, int& num) {
931 filename = getFullPathName(filename);
933 vector<unsigned long int> positions;
935 openInputFile(filename, in);
939 unsigned long int lastpos = in.tellg();
941 if (input.length() != 0) {
942 unsigned long int pos = in.tellg();
943 if (pos != -1) { positions.push_back(pos - input.length() - 1); }
944 else { positions.push_back(lastpos); }
946 gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
950 num = positions.size();
953 unsigned long int size;
955 //get num bytes in file
956 pFile = fopen (filename.c_str(),"rb");
957 if (pFile==NULL) perror ("Error opening file");
959 fseek (pFile, 0, SEEK_END);
964 positions.push_back(size);
968 catch(exception& e) {
969 errorOut(e, "MothurOut", "setFilePosEachLine");
973 /**************************************************************************************************/
975 vector<unsigned long int> MothurOut::divideFile(string filename, int& proc) {
978 vector<unsigned long int> filePos;
979 filePos.push_back(0);
982 unsigned long int size;
984 filename = getFullPathName(filename);
986 //get num bytes in file
987 pFile = fopen (filename.c_str(),"rb");
988 if (pFile==NULL) perror ("Error opening file");
990 fseek (pFile, 0, SEEK_END);
995 //estimate file breaks
996 unsigned long int chunkSize = 0;
997 chunkSize = size / proc;
999 //file to small to divide by processors
1000 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1002 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1003 for (int i = 0; i < proc; i++) {
1004 unsigned long int spot = (i+1) * chunkSize;
1007 openInputFile(filename, in);
1011 unsigned long int newSpot = spot;
1014 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1017 //there was not another sequence before the end of the file
1018 unsigned long int sanityPos = in.tellg();
1020 if (sanityPos == -1) { break; }
1021 else { filePos.push_back(newSpot); }
1027 filePos.push_back(size);
1029 //sanity check filePos
1030 for (int i = 0; i < (filePos.size()-1); i++) {
1031 if (filePos[(i+1)] <= filePos[i]) { cout << "erasing " << (i+1) << endl; filePos.erase(filePos.begin()+(i+1)); i--; }
1034 proc = (filePos.size() - 1);
1038 catch(exception& e) {
1039 errorOut(e, "MothurOut", "divideFile");
1044 /***********************************************************************/
1046 bool MothurOut::isTrue(string f){
1049 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1051 if ((f == "TRUE") || (f == "T")) { return true; }
1052 else { return false; }
1054 catch(exception& e) {
1055 errorOut(e, "MothurOut", "isTrue");
1060 /***********************************************************************/
1062 float MothurOut::roundDist(float dist, int precision){
1064 return int(dist * precision + 0.5)/float(precision);
1066 catch(exception& e) {
1067 errorOut(e, "MothurOut", "roundDist");
1071 /***********************************************************************/
1073 float MothurOut::ceilDist(float dist, int precision){
1075 return int(ceil(dist * precision))/float(precision);
1077 catch(exception& e) {
1078 errorOut(e, "MothurOut", "ceilDist");
1083 /***********************************************************************/
1085 int MothurOut::getNumNames(string names){
1091 for(int i=0;i<names.size();i++){
1092 if(names[i] == ','){
1100 catch(exception& e) {
1101 errorOut(e, "MothurOut", "getNumNames");
1106 /**************************************************************************************************/
1108 vector<vector<double> > MothurOut::binomial(int maxOrder){
1110 vector<vector<double> > binomial(maxOrder+1);
1112 for(int i=0;i<=maxOrder;i++){
1113 binomial[i].resize(maxOrder+1);
1122 for(int i=2;i<=maxOrder;i++){
1126 for(int i=2;i<=maxOrder;i++){
1127 for(int j=1;j<=maxOrder;j++){
1128 if(i==j){ binomial[i][j]=1; }
1129 if(j>i) { binomial[i][j]=0; }
1130 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
1137 catch(exception& e) {
1138 errorOut(e, "MothurOut", "binomial");
1143 /***********************************************************************/
1145 int MothurOut::factorial(int num){
1149 for (int i = 1; i <= num; i++) {
1155 catch(exception& e) {
1156 errorOut(e, "MothurOut", "factorial");
1160 /***********************************************************************/
1162 int MothurOut::getNumSeqs(ifstream& file){
1164 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1168 catch(exception& e) {
1169 errorOut(e, "MothurOut", "getNumSeqs");
1173 /***********************************************************************/
1174 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1179 input = getline(file);
1180 if (input.length() != 0) {
1181 if(input[0] == '>'){ numSeqs++; }
1185 catch(exception& e) {
1186 errorOut(e, "MothurOut", "getNumSeqs");
1190 /***********************************************************************/
1192 //This function parses the estimator options and puts them in a vector
1193 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1195 string individual = "";
1196 int estimLength = estim.size();
1197 for(int i=0;i<estimLength;i++){
1198 if(estim[i] == symbol){
1199 container.push_back(individual);
1203 individual += estim[i];
1206 container.push_back(individual);
1210 while (estim.find_first_of(symbol) != -1) {
1211 individual = estim.substr(0,estim.find_first_of(symbol));
1212 if ((estim.find_first_of(symbol)+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1213 estim = estim.substr(estim.find_first_of(symbol)+1, estim.length());
1214 container.push_back(individual);
1218 container.push_back(estim); */
1220 catch(exception& e) {
1221 errorOut(e, "MothurOut", "splitAtChar");
1226 /***********************************************************************/
1228 //This function parses the estimator options and puts them in a vector
1229 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1231 string individual = "";
1232 int estimLength = estim.size();
1233 for(int i=0;i<estimLength;i++){
1234 if(estim[i] == '-'){
1235 container.push_back(individual);
1239 individual += estim[i];
1242 container.push_back(individual);
1245 /* string individual;
1247 while (estim.find_first_of('-') != -1) {
1248 individual = estim.substr(0,estim.find_first_of('-'));
1249 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1250 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1251 container.push_back(individual);
1255 container.push_back(estim); */
1257 catch(exception& e) {
1258 errorOut(e, "MothurOut", "splitAtDash");
1263 /***********************************************************************/
1264 //This function parses the label options and puts them in a set
1265 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1267 string individual = "";
1268 int estimLength = estim.size();
1269 for(int i=0;i<estimLength;i++){
1270 if(estim[i] == '-'){
1271 container.insert(individual);
1275 individual += estim[i];
1278 container.insert(individual);
1280 // string individual;
1282 // while (estim.find_first_of('-') != -1) {
1283 // individual = estim.substr(0,estim.find_first_of('-'));
1284 // if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1285 // estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1286 // container.insert(individual);
1290 // container.insert(estim);
1293 catch(exception& e) {
1294 errorOut(e, "MothurOut", "splitAtDash");
1298 /***********************************************************************/
1299 //This function parses the line options and puts them in a set
1300 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1305 while (estim.find_first_of('-') != -1) {
1306 individual = estim.substr(0,estim.find_first_of('-'));
1307 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1308 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1309 convert(individual, lineNum); //convert the string to int
1310 container.insert(lineNum);
1314 convert(estim, lineNum); //convert the string to int
1315 container.insert(lineNum);
1317 catch(exception& e) {
1318 errorOut(e, "MothurOut", "splitAtDash");
1322 /***********************************************************************/
1323 //This function parses the a string and puts peices in a vector
1324 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1326 string individual = "";
1327 int estimLength = estim.size();
1328 for(int i=0;i<estimLength;i++){
1329 if(estim[i] == ','){
1330 container.push_back(individual);
1334 individual += estim[i];
1337 container.push_back(individual);
1342 // string individual;
1344 // while (estim.find_first_of(',') != -1) {
1345 // individual = estim.substr(0,estim.find_first_of(','));
1346 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1347 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1348 // container.push_back(individual);
1352 // container.push_back(estim);
1354 catch(exception& e) {
1355 errorOut(e, "MothurOut", "splitAtComma");
1359 /***********************************************************************/
1361 //This function splits up the various option parameters
1362 void MothurOut::splitAtComma(string& prefix, string& suffix){
1364 prefix = suffix.substr(0,suffix.find_first_of(','));
1365 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
1366 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1368 while(suffix.at(0) == ' ')
1369 suffix = suffix.substr(1, suffix.length());
1373 catch(exception& e) {
1374 errorOut(e, "MothurOut", "splitAtComma");
1378 /***********************************************************************/
1380 //This function separates the key value from the option value i.e. dist=96_...
1381 void MothurOut::splitAtEquals(string& key, string& value){
1383 if(value.find_first_of('=') != -1){
1384 key = value.substr(0,value.find_first_of('='));
1385 if ((value.find_first_of('=')+1) <= value.length()) {
1386 value = value.substr(value.find_first_of('=')+1, value.length());
1393 catch(exception& e) {
1394 errorOut(e, "MothurOut", "splitAtEquals");
1399 /**************************************************************************************************/
1401 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1403 for (int i = 0; i < Groups.size(); i++) {
1404 if (groupname == Groups[i]) { return true; }
1408 catch(exception& e) {
1409 errorOut(e, "MothurOut", "inUsersGroups");
1413 /**************************************************************************************************/
1414 //returns true if any of the strings in first vector are in second vector
1415 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1418 for (int i = 0; i < groupnames.size(); i++) {
1419 if (inUsersGroups(groupnames[i], Groups)) { return true; }
1423 catch(exception& e) {
1424 errorOut(e, "MothurOut", "inUsersGroups");
1428 /***********************************************************************/
1429 //this function determines if the user has given us labels that are smaller than the given label.
1430 //if so then it returns true so that the calling function can run the previous valid distance.
1431 //it's a "smart" distance function. It also checks for invalid labels.
1432 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1435 set<string>::iterator it;
1436 vector<float> orderFloat;
1437 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
1438 map<string, float>::iterator it2;
1440 bool smaller = false;
1442 //unique is the smallest line
1443 if (label == "unique") { return false; }
1445 if (convertTestFloat(label, labelFloat)) {
1446 convert(label, labelFloat);
1447 }else { //cant convert
1452 //go through users set and make them floats
1453 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
1456 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1458 orderFloat.push_back(temp);
1459 userMap[*it] = temp;
1460 }else if (*it == "unique") {
1461 orderFloat.push_back(-1.0);
1462 userMap["unique"] = -1.0;
1464 if (errorOff == "") { cout << *it << " is not a valid label." << endl; }
1465 userLabels.erase(*it);
1471 sort(orderFloat.begin(), orderFloat.end());
1473 /*************************************************/
1474 //is this label bigger than any of the users labels
1475 /*************************************************/
1477 //loop through order until you find a label greater than label
1478 for (int i = 0; i < orderFloat.size(); i++) {
1479 if (orderFloat[i] < labelFloat) {
1481 if (orderFloat[i] == -1) {
1482 if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
1483 userLabels.erase("unique");
1486 if (errorOff == "") { cout << "Your file does not include the label " << endl; }
1488 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
1489 if (it2->second == orderFloat[i]) {
1491 //remove small labels
1492 userLabels.erase(s);
1496 if (errorOff == "") {cout << s << ". I will use the next smallest distance. " << endl; }
1498 //since they are sorted once you find a bigger one stop looking
1505 catch(exception& e) {
1506 errorOut(e, "MothurOut", "anyLabelsToProcess");
1511 /**************************************************************************************************/
1512 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1517 string line = getline(file);
1519 //before we added this check
1520 if (line[0] != '#') { good = false; }
1523 line = line.substr(1);
1525 vector<string> versionVector;
1526 splitAtChar(version, versionVector, '.');
1528 //check file version
1529 vector<string> linesVector;
1530 splitAtChar(line, linesVector, '.');
1532 if (versionVector.size() != linesVector.size()) { good = false; }
1534 for (int j = 0; j < versionVector.size(); j++) {
1536 convert(versionVector[j], num1);
1537 convert(linesVector[j], num2);
1539 //if mothurs version is newer than this files version, then we want to remake it
1540 if (num1 > num2) { good = false; break; }
1546 if (!good) { file.close(); }
1547 else { file.seekg(0); }
1551 catch(exception& e) {
1552 errorOut(e, "MothurOut", "checkReleaseVersion");
1556 /**************************************************************************************************/