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
505 string homeDir = getenv ("HOME");
506 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
509 if (path.rfind("./") == -1) { return fileName; } //already complete name
510 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
512 char* cwdpath = new char[1024];
515 cwdpath=getcwd(cwdpath,size);
521 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
523 //break apart the current working directory
525 while (simpleCWD.find_first_of('/') != -1) {
526 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
527 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
530 //get last one // ex. ../../../filename = /user/work/desktop/filename
531 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
534 int index = dirs.size()-1;
536 while((pos = path.rfind("./")) != -1) { //while you don't have a complete path
537 if (pos == 0) { break; //you are at the end
538 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
539 path = path.substr(0, pos-1);
541 if (index == 0) { break; }
542 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
543 path = path.substr(0, pos);
544 }else if (pos == 1) { break; //you are at the end
545 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
548 for (int i = index; i >= 0; i--) {
549 newFileName = dirs[i] + "/" + newFileName;
552 newFileName = "/" + newFileName;
556 if (path.find("~") != -1) { //go to home directory
557 string homeDir = getenv ("HOMEPATH");
558 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
561 if (path.rfind(".\\") == -1) { return fileName; } //already complete name
562 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
564 char *cwdpath = NULL;
565 cwdpath = getcwd(NULL, 0); // or _getcwd
566 if ( cwdpath != NULL) { cwd = cwdpath; }
569 //break apart the current working directory
571 while (cwd.find_first_of('\\') != -1) {
572 string dir = cwd.substr(0,cwd.find_first_of('\\'));
573 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
578 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
580 int index = dirs.size()-1;
582 while((pos = path.rfind(".\\")) != -1) { //while you don't have a complete path
583 if (pos == 0) { break; //you are at the end
584 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
585 path = path.substr(0, pos-1);
587 if (index == 0) { break; }
588 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
589 path = path.substr(0, pos);
590 }else if (pos == 1) { break; //you are at the end
591 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
594 for (int i = index; i >= 0; i--) {
595 newFileName = dirs[i] + "\\" + newFileName;
604 catch(exception& e) {
605 errorOut(e, "MothurOut", "getFullPathName");
609 /***********************************************************************/
611 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
614 string completeFileName = getFullPathName(fileName);
616 #ifdef USE_COMPRESSION
617 // check for gzipped or bzipped file
618 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
619 string tempName = string(tmpnam(0));
620 mkfifo(tempName.c_str(), 0666);
621 int fork_result = fork();
622 if (fork_result < 0) {
623 cerr << "Error forking.\n";
625 } else if (fork_result == 0) {
626 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
627 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
628 system(command.c_str());
629 cerr << "Done decompressing " << completeFileName << "\n";
630 remove(tempName.c_str());
633 cerr << "waiting on child process " << fork_result << "\n";
634 completeFileName = tempName;
639 fileHandle.open(completeFileName.c_str());
643 //check for blank file
648 catch(exception& e) {
649 errorOut(e, "MothurOut", "openInputFile - no Error");
653 /***********************************************************************/
655 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
659 string completeFileName = getFullPathName(fileName);
661 #ifdef USE_COMPRESSION
662 // check for gzipped or bzipped file
663 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
664 string tempName = string(tmpnam(0));
665 mkfifo(tempName.c_str(), 0666);
666 int fork_result = fork();
667 if (fork_result < 0) {
668 cerr << "Error forking.\n";
670 } else if (fork_result == 0) {
671 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
672 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
673 system(command.c_str());
674 cerr << "Done decompressing " << completeFileName << "\n";
675 remove(tempName.c_str());
678 cerr << "waiting on child process " << fork_result << "\n";
679 completeFileName = tempName;
685 fileHandle.open(completeFileName.c_str());
687 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
691 //check for blank file
693 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
698 catch(exception& e) {
699 errorOut(e, "MothurOut", "openInputFile");
703 /***********************************************************************/
705 int MothurOut::renameFile(string oldName, string newName){
708 int exist = openInputFile(newName, inTest, "");
710 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
711 if (exist == 0) { //you could open it so you want to delete it
713 string command = "rm " + newName;
714 system(command.c_str());
717 string command = "mv " + oldName + " " + newName;
718 system(command.c_str());
720 remove(newName.c_str());
721 int renameOk = rename(oldName.c_str(), newName.c_str());
726 catch(exception& e) {
727 errorOut(e, "MothurOut", "renameFile");
732 /***********************************************************************/
734 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
737 string completeFileName = getFullPathName(fileName);
739 #ifdef USE_COMPRESSION
740 // check for gzipped file
741 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
742 string tempName = string(tmpnam(0));
743 mkfifo(tempName.c_str(), 0666);
744 cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
745 int fork_result = fork();
746 if (fork_result < 0) {
747 cerr << "Error forking.\n";
749 } else if (fork_result == 0) {
750 string command = string(endsWith(completeFileName, ".gz") ? "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
751 system(command.c_str());
754 completeFileName = tempName;
759 fileHandle.open(completeFileName.c_str(), ios::trunc);
761 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
768 catch(exception& e) {
769 errorOut(e, "MothurOut", "openOutputFile");
775 /**************************************************************************************************/
776 void MothurOut::appendFiles(string temp, string filename) {
781 //open output file in append mode
782 openOutputFileAppend(filename, output);
783 int ableToOpen = openInputFile(temp, input, "no error");
785 if (ableToOpen == 0) { //you opened it
786 while(char c = input.get()){
787 if(input.eof()) { break; }
788 else { output << c; }
795 catch(exception& e) {
796 errorOut(e, "MothurOut", "appendFiles");
801 /**************************************************************************************************/
802 string MothurOut::sortFile(string distFile, string outputDir){
805 //if (outputDir == "") { outputDir += hasPath(distFile); }
806 string outfile = getRootName(distFile) + "sorted.dist";
809 //if you can, use the unix sort since its been optimized for years
810 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
811 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
812 system(command.c_str());
813 #else //you are stuck with my best attempt...
814 //windows sort does not have a way to specify a column, only a character in the line
815 //since we cannot assume that the distance will always be at the the same character location on each line
816 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
818 //read in file line by file and put distance first
819 string tempDistFile = distFile + ".temp";
822 openInputFile(distFile, input);
823 openOutputFile(tempDistFile, output);
825 string firstName, secondName;
828 input >> firstName >> secondName >> dist;
829 output << dist << '\t' << firstName << '\t' << secondName << endl;
836 //sort using windows sort
837 string tempOutfile = outfile + ".temp";
838 string command = "sort " + tempDistFile + " /O " + tempOutfile;
839 system(command.c_str());
841 //read in sorted file and put distance at end again
843 openInputFile(tempOutfile, input2);
844 openOutputFile(outfile, output);
847 input2 >> dist >> firstName >> secondName;
848 output << firstName << '\t' << secondName << '\t' << dist << endl;
855 remove(tempDistFile.c_str());
856 remove(tempOutfile.c_str());
861 catch(exception& e) {
862 errorOut(e, "MothurOut", "sortFile");
866 /**************************************************************************************************/
867 vector<unsigned long int> MothurOut::setFilePosFasta(string filename, int& num) {
869 vector<unsigned long int> positions;
871 openInputFile(filename, inFASTA);
874 while(!inFASTA.eof()){
875 input = getline(inFASTA);
876 if (input.length() != 0) {
877 if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
879 gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
883 num = positions.size();
888 //get num bytes in file
889 pFile = fopen (filename.c_str(),"rb");
890 if (pFile==NULL) perror ("Error opening file");
892 fseek (pFile, 0, SEEK_END);
897 unsigned long int size = positions[(positions.size()-1)];
899 openInputFile(filename, in);
903 while(char c = in.get()){
904 if(in.eof()) { break; }
909 positions.push_back(size);
913 catch(exception& e) {
914 errorOut(e, "MothurOut", "setFilePosFasta");
918 /**************************************************************************************************/
919 vector<unsigned long int> MothurOut::setFilePosEachLine(string filename, int& num) {
921 filename = getFullPathName(filename);
923 vector<unsigned long int> positions;
925 openInputFile(filename, in);
929 unsigned long int lastpos = in.tellg();
931 if (input.length() != 0) {
932 unsigned long int pos = in.tellg();
933 if (pos != -1) { positions.push_back(pos - input.length() - 1); }
934 else { positions.push_back(lastpos); }
936 gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
940 num = positions.size();
943 unsigned long int size;
945 //get num bytes in file
946 pFile = fopen (filename.c_str(),"rb");
947 if (pFile==NULL) perror ("Error opening file");
949 fseek (pFile, 0, SEEK_END);
954 positions.push_back(size);
958 catch(exception& e) {
959 errorOut(e, "MothurOut", "setFilePosEachLine");
963 /**************************************************************************************************/
965 vector<unsigned long int> MothurOut::divideFile(string filename, int& proc) {
968 vector<unsigned long int> filePos;
969 filePos.push_back(0);
972 unsigned long int size;
974 filename = getFullPathName(filename);
976 //get num bytes in file
977 pFile = fopen (filename.c_str(),"rb");
978 if (pFile==NULL) perror ("Error opening file");
980 fseek (pFile, 0, SEEK_END);
985 //estimate file breaks
986 unsigned long int chunkSize = 0;
987 chunkSize = size / proc;
989 //file to small to divide by processors
990 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
992 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
993 for (int i = 0; i < proc; i++) {
994 unsigned long int spot = (i+1) * chunkSize;
997 openInputFile(filename, in);
1001 unsigned long int newSpot = spot;
1004 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1007 //there was not another sequence before the end of the file
1008 unsigned long int sanityPos = in.tellg();
1010 if (sanityPos == -1) { break; }
1011 else { filePos.push_back(newSpot); }
1017 filePos.push_back(size);
1019 //sanity check filePos
1020 for (int i = 0; i < (filePos.size()-1); i++) {
1021 if (filePos[(i+1)] <= filePos[i]) { cout << "erasing " << (i+1) << endl; filePos.erase(filePos.begin()+(i+1)); i--; }
1024 proc = (filePos.size() - 1);
1028 catch(exception& e) {
1029 errorOut(e, "MothurOut", "divideFile");
1034 /***********************************************************************/
1036 bool MothurOut::isTrue(string f){
1039 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1041 if ((f == "TRUE") || (f == "T")) { return true; }
1042 else { return false; }
1044 catch(exception& e) {
1045 errorOut(e, "MothurOut", "isTrue");
1050 /***********************************************************************/
1052 float MothurOut::roundDist(float dist, int precision){
1054 return int(dist * precision + 0.5)/float(precision);
1056 catch(exception& e) {
1057 errorOut(e, "MothurOut", "roundDist");
1061 /***********************************************************************/
1063 float MothurOut::ceilDist(float dist, int precision){
1065 return int(ceil(dist * precision))/float(precision);
1067 catch(exception& e) {
1068 errorOut(e, "MothurOut", "ceilDist");
1073 /***********************************************************************/
1075 int MothurOut::getNumNames(string names){
1081 for(int i=0;i<names.size();i++){
1082 if(names[i] == ','){
1090 catch(exception& e) {
1091 errorOut(e, "MothurOut", "getNumNames");
1096 /**************************************************************************************************/
1098 vector<vector<double> > MothurOut::binomial(int maxOrder){
1100 vector<vector<double> > binomial(maxOrder+1);
1102 for(int i=0;i<=maxOrder;i++){
1103 binomial[i].resize(maxOrder+1);
1112 for(int i=2;i<=maxOrder;i++){
1116 for(int i=2;i<=maxOrder;i++){
1117 for(int j=1;j<=maxOrder;j++){
1118 if(i==j){ binomial[i][j]=1; }
1119 if(j>i) { binomial[i][j]=0; }
1120 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
1127 catch(exception& e) {
1128 errorOut(e, "MothurOut", "binomial");
1133 /***********************************************************************/
1135 int MothurOut::factorial(int num){
1139 for (int i = 1; i <= num; i++) {
1145 catch(exception& e) {
1146 errorOut(e, "MothurOut", "factorial");
1150 /***********************************************************************/
1152 int MothurOut::getNumSeqs(ifstream& file){
1154 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1158 catch(exception& e) {
1159 errorOut(e, "MothurOut", "getNumSeqs");
1163 /***********************************************************************/
1164 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1169 input = getline(file);
1170 if (input.length() != 0) {
1171 if(input[0] == '>'){ numSeqs++; }
1175 catch(exception& e) {
1176 errorOut(e, "MothurOut", "getNumSeqs");
1180 /***********************************************************************/
1182 //This function parses the estimator options and puts them in a vector
1183 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1187 while (estim.find_first_of(symbol) != -1) {
1188 individual = estim.substr(0,estim.find_first_of(symbol));
1189 if ((estim.find_first_of(symbol)+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1190 estim = estim.substr(estim.find_first_of(symbol)+1, estim.length());
1191 container.push_back(individual);
1195 container.push_back(estim);
1197 catch(exception& e) {
1198 errorOut(e, "MothurOut", "splitAtChar");
1203 /***********************************************************************/
1205 //This function parses the estimator options and puts them in a vector
1206 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1208 string individual = "";
1209 int estimLength = estim.size();
1210 for(int i=0;i<estimLength;i++){
1211 if(estim[i] == '-'){
1212 container.push_back(individual);
1216 individual += estim[i];
1219 container.push_back(individual);
1222 /* string individual;
1224 while (estim.find_first_of('-') != -1) {
1225 individual = estim.substr(0,estim.find_first_of('-'));
1226 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1227 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1228 container.push_back(individual);
1232 container.push_back(estim); */
1234 catch(exception& e) {
1235 errorOut(e, "MothurOut", "splitAtDash");
1240 /***********************************************************************/
1241 //This function parses the label options and puts them in a set
1242 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1244 string individual = "";
1245 int estimLength = estim.size();
1246 for(int i=0;i<estimLength;i++){
1247 if(estim[i] == '-'){
1248 container.insert(individual);
1252 individual += estim[i];
1255 container.insert(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.insert(individual);
1267 // container.insert(estim);
1270 catch(exception& e) {
1271 errorOut(e, "MothurOut", "splitAtDash");
1275 /***********************************************************************/
1276 //This function parses the line options and puts them in a set
1277 void MothurOut::splitAtDash(string& estim, set<int>& container) {
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 convert(individual, lineNum); //convert the string to int
1287 container.insert(lineNum);
1291 convert(estim, lineNum); //convert the string to int
1292 container.insert(lineNum);
1294 catch(exception& e) {
1295 errorOut(e, "MothurOut", "splitAtDash");
1299 /***********************************************************************/
1300 //This function parses the a string and puts peices in a vector
1301 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1303 string individual = "";
1304 int estimLength = estim.size();
1305 for(int i=0;i<estimLength;i++){
1306 if(estim[i] == ','){
1307 container.push_back(individual);
1311 individual += estim[i];
1314 container.push_back(individual);
1319 // string individual;
1321 // while (estim.find_first_of(',') != -1) {
1322 // individual = estim.substr(0,estim.find_first_of(','));
1323 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1324 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1325 // container.push_back(individual);
1329 // container.push_back(estim);
1331 catch(exception& e) {
1332 errorOut(e, "MothurOut", "splitAtComma");
1336 /***********************************************************************/
1338 //This function splits up the various option parameters
1339 void MothurOut::splitAtComma(string& prefix, string& suffix){
1341 prefix = suffix.substr(0,suffix.find_first_of(','));
1342 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
1343 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1345 while(suffix.at(0) == ' ')
1346 suffix = suffix.substr(1, suffix.length());
1350 catch(exception& e) {
1351 errorOut(e, "MothurOut", "splitAtComma");
1355 /***********************************************************************/
1357 //This function separates the key value from the option value i.e. dist=96_...
1358 void MothurOut::splitAtEquals(string& key, string& value){
1360 if(value.find_first_of('=') != -1){
1361 key = value.substr(0,value.find_first_of('='));
1362 if ((value.find_first_of('=')+1) <= value.length()) {
1363 value = value.substr(value.find_first_of('=')+1, value.length());
1370 catch(exception& e) {
1371 errorOut(e, "MothurOut", "splitAtEquals");
1376 /**************************************************************************************************/
1378 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1380 for (int i = 0; i < Groups.size(); i++) {
1381 if (groupname == Groups[i]) { return true; }
1385 catch(exception& e) {
1386 errorOut(e, "MothurOut", "inUsersGroups");
1390 /**************************************************************************************************/
1391 //returns true if any of the strings in first vector are in second vector
1392 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1395 for (int i = 0; i < groupnames.size(); i++) {
1396 if (inUsersGroups(groupnames[i], Groups)) { return true; }
1400 catch(exception& e) {
1401 errorOut(e, "MothurOut", "inUsersGroups");
1405 /***********************************************************************/
1406 //this function determines if the user has given us labels that are smaller than the given label.
1407 //if so then it returns true so that the calling function can run the previous valid distance.
1408 //it's a "smart" distance function. It also checks for invalid labels.
1409 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1412 set<string>::iterator it;
1413 vector<float> orderFloat;
1414 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
1415 map<string, float>::iterator it2;
1417 bool smaller = false;
1419 //unique is the smallest line
1420 if (label == "unique") { return false; }
1422 if (convertTestFloat(label, labelFloat)) {
1423 convert(label, labelFloat);
1424 }else { //cant convert
1429 //go through users set and make them floats
1430 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
1433 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1435 orderFloat.push_back(temp);
1436 userMap[*it] = temp;
1437 }else if (*it == "unique") {
1438 orderFloat.push_back(-1.0);
1439 userMap["unique"] = -1.0;
1441 if (errorOff == "") { cout << *it << " is not a valid label." << endl; }
1442 userLabels.erase(*it);
1448 sort(orderFloat.begin(), orderFloat.end());
1450 /*************************************************/
1451 //is this label bigger than any of the users labels
1452 /*************************************************/
1454 //loop through order until you find a label greater than label
1455 for (int i = 0; i < orderFloat.size(); i++) {
1456 if (orderFloat[i] < labelFloat) {
1458 if (orderFloat[i] == -1) {
1459 if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
1460 userLabels.erase("unique");
1463 if (errorOff == "") { cout << "Your file does not include the label " << endl; }
1465 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
1466 if (it2->second == orderFloat[i]) {
1468 //remove small labels
1469 userLabels.erase(s);
1473 if (errorOff == "") {cout << s << ". I will use the next smallest distance. " << endl; }
1475 //since they are sorted once you find a bigger one stop looking
1482 catch(exception& e) {
1483 errorOut(e, "MothurOut", "anyLabelsToProcess");
1488 /**************************************************************************************************/
1489 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1494 string line = getline(file);
1496 //before we added this check
1497 if (line[0] != '#') { good = false; }
1500 line = line.substr(1);
1502 vector<string> versionVector;
1503 splitAtChar(version, versionVector, '.');
1505 //check file version
1506 vector<string> linesVector;
1507 splitAtChar(line, linesVector, '.');
1509 if (versionVector.size() != linesVector.size()) { good = false; }
1511 for (int j = 0; j < versionVector.size(); j++) {
1513 convert(versionVector[j], num1);
1514 convert(linesVector[j], num2);
1516 //if mothurs version is newer than this files version, then we want to remake it
1517 if (num1 > num2) { good = false; break; }
1523 if (!good) { file.close(); }
1524 else { file.seekg(0); }
1528 catch(exception& e) {
1529 errorOut(e, "MothurOut", "checkReleaseVersion");
1533 /**************************************************************************************************/