5 * Created by westcott on 2/25/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "mothurout.h"
13 /******************************************************/
14 MothurOut* MothurOut::getInstance() {
15 if( _uniqueInstance == 0) {
16 _uniqueInstance = new MothurOut();
18 return _uniqueInstance;
20 /*********************************************************************************************/
21 void MothurOut::printCurrentFiles() {
23 if (accnosfile != "") { mothurOut("accnos=" + accnosfile); mothurOutEndLine(); }
24 if (columnfile != "") { mothurOut("column=" + columnfile); mothurOutEndLine(); }
25 if (designfile != "") { mothurOut("design=" + designfile); mothurOutEndLine(); }
26 if (fastafile != "") { mothurOut("fasta=" + fastafile); mothurOutEndLine(); }
27 if (groupfile != "") { mothurOut("group=" + groupfile); mothurOutEndLine(); }
28 if (listfile != "") { mothurOut("list=" + listfile); mothurOutEndLine(); }
29 if (namefile != "") { mothurOut("name=" + namefile); mothurOutEndLine(); }
30 if (oligosfile != "") { mothurOut("oligos=" + oligosfile); mothurOutEndLine(); }
31 if (orderfile != "") { mothurOut("order=" + orderfile); mothurOutEndLine(); }
32 if (ordergroupfile != "") { mothurOut("ordergroup=" + ordergroupfile); mothurOutEndLine(); }
33 if (phylipfile != "") { mothurOut("phylip=" + phylipfile); mothurOutEndLine(); }
34 if (qualfile != "") { mothurOut("qfile=" + qualfile); mothurOutEndLine(); }
35 if (rabundfile != "") { mothurOut("rabund=" + rabundfile); mothurOutEndLine(); }
36 if (relabundfile != "") { mothurOut("relabund=" + relabundfile); mothurOutEndLine(); }
37 if (sabundfile != "") { mothurOut("sabund=" + sabundfile); mothurOutEndLine(); }
38 if (sfffile != "") { mothurOut("sff=" + sfffile); mothurOutEndLine(); }
39 if (sharedfile != "") { mothurOut("shared=" + sharedfile); mothurOutEndLine(); }
40 if (taxonomyfile != "") { mothurOut("taxonomy=" + taxonomyfile); mothurOutEndLine(); }
41 if (treefile != "") { mothurOut("tree=" + treefile); mothurOutEndLine(); }
42 if (flowfile != "") { mothurOut("flow=" + flowfile); mothurOutEndLine(); }
43 if (biomfile != "") { mothurOut("biom=" + biomfile); mothurOutEndLine(); }
44 if (processors != "1") { mothurOut("processors=" + processors); mothurOutEndLine(); }
48 errorOut(e, "MothurOut", "printCurrentFiles");
52 /*********************************************************************************************/
53 bool MothurOut::hasCurrentFiles() {
55 bool hasCurrent = false;
57 if (accnosfile != "") { return true; }
58 if (columnfile != "") { return true; }
59 if (designfile != "") { return true; }
60 if (fastafile != "") { return true; }
61 if (groupfile != "") { return true; }
62 if (listfile != "") { return true; }
63 if (namefile != "") { return true; }
64 if (oligosfile != "") { return true; }
65 if (orderfile != "") { return true; }
66 if (ordergroupfile != "") { return true; }
67 if (phylipfile != "") { return true; }
68 if (qualfile != "") { return true; }
69 if (rabundfile != "") { return true; }
70 if (relabundfile != "") { return true; }
71 if (sabundfile != "") { return true; }
72 if (sfffile != "") { return true; }
73 if (sharedfile != "") { return true; }
74 if (taxonomyfile != "") { return true; }
75 if (treefile != "") { return true; }
76 if (flowfile != "") { return true; }
77 if (biomfile != "") { return true; }
78 if (processors != "1") { return true; }
84 errorOut(e, "MothurOut", "hasCurrentFiles");
89 /*********************************************************************************************/
90 void MothurOut::clearCurrentFiles() {
115 catch(exception& e) {
116 errorOut(e, "MothurOut", "clearCurrentFiles");
120 /***********************************************************************/
121 string MothurOut::findProgramPath(string programName){
124 string envPath = getenv("PATH");
127 //delimiting path char
129 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
135 //break apart path variable by ':'
137 splitAtChar(envPath, dirs, delim);
139 if (debug) { mothurOut("[DEBUG]: dir's in path: \n"); }
141 //get path related to mothur
142 for (int i = 0; i < dirs.size(); i++) {
144 if (debug) { mothurOut("[DEBUG]: " + dirs[i] + "\n"); }
146 //to lower so we can find it
147 string tempLower = "";
148 for (int j = 0; j < dirs[i].length(); j++) { tempLower += tolower(dirs[i][j]); }
150 //is this mothurs path?
151 if (tempLower.find(programName) != -1) { pPath = dirs[i]; break; }
154 if (debug) { mothurOut("[DEBUG]: programPath = " + pPath + "\n"); }
157 //add programName so it looks like what argv would look like
158 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
159 pPath += "/" + programName;
161 pPath += "\\" + programName;
164 //okay programName is not in the path, so the folder programName is in must be in the path
165 //lets find out which one
167 //get path related to the program
168 for (int i = 0; i < dirs.size(); i++) {
170 if (debug) { mothurOut("[DEBUG]: looking in " + dirs[i] + " for " + programName + " \n"); }
172 //is this the programs path?
174 string tempIn = dirs[i];
175 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
176 tempIn += "/" + programName;
178 tempIn += "\\" + programName;
180 openInputFile(tempIn, in, "");
182 //if this file exists
183 if (in) { in.close(); pPath = tempIn; if (debug) { mothurOut("[DEBUG]: found it, programPath = " + pPath + "\n"); } break; }
190 catch(exception& e) {
191 errorOut(e, "MothurOut", "findProgramPath");
195 /*********************************************************************************************/
196 void MothurOut::setFileName(string filename) {
198 logFileName = filename;
202 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
204 if (pid == 0) { //only one process should output to screen
207 openOutputFile(filename, out);
213 catch(exception& e) {
214 errorOut(e, "MothurOut", "setFileName");
218 /*********************************************************************************************/
219 void MothurOut::setDefaultPath(string pathname) {
222 //add / to name if needed
223 string lastChar = pathname.substr(pathname.length()-1);
224 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
225 if (lastChar != "/") { pathname += "/"; }
227 if (lastChar != "\\") { pathname += "\\"; }
230 defaultPath = pathname;
233 catch(exception& e) {
234 errorOut(e, "MothurOut", "setDefaultPath");
238 /*********************************************************************************************/
239 void MothurOut::setOutputDir(string pathname) {
241 outputDir = pathname;
243 catch(exception& e) {
244 errorOut(e, "MothurOut", "setOutputDir");
248 /*********************************************************************************************/
249 void MothurOut::closeLog() {
254 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
256 if (pid == 0) { //only one process should output to screen
265 catch(exception& e) {
266 errorOut(e, "MothurOut", "closeLog");
271 /*********************************************************************************************/
272 MothurOut::~MothurOut() {
277 catch(exception& e) {
278 errorOut(e, "MothurOut", "MothurOut");
282 /*********************************************************************************************/
283 void MothurOut::mothurOut(string output) {
288 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
290 if (pid == 0) { //only one process should output to screen
300 catch(exception& e) {
301 errorOut(e, "MothurOut", "MothurOut");
305 /*********************************************************************************************/
306 void MothurOut::mothurOutEndLine() {
310 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
312 if (pid == 0) { //only one process should output to screen
322 catch(exception& e) {
323 errorOut(e, "MothurOut", "MothurOutEndLine");
327 /*********************************************************************************************/
328 void MothurOut::mothurOut(string output, ofstream& outputFile) {
333 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
335 if (pid == 0) { //only one process should output to screen
340 outputFile << output;
348 catch(exception& e) {
349 errorOut(e, "MothurOut", "MothurOut");
353 /*********************************************************************************************/
354 void MothurOut::mothurOutEndLine(ofstream& outputFile) {
358 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
360 if (pid == 0) { //only one process should output to screen
371 catch(exception& e) {
372 errorOut(e, "MothurOut", "MothurOutEndLine");
376 /*********************************************************************************************/
377 void MothurOut::mothurOutJustToLog(string output) {
381 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
383 if (pid == 0) { //only one process should output to screen
392 catch(exception& e) {
393 errorOut(e, "MothurOut", "MothurOutJustToLog");
397 /*********************************************************************************************/
398 void MothurOut::errorOut(exception& e, string object, string function) {
400 //mem_usage(vm, rss);
402 mothurOut("[ERROR]: ");
403 mothurOut(toString(e.what()));
404 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.");
407 /*********************************************************************************************/
408 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
409 // process_mem_usage(double &, double &) - takes two doubles by reference,
410 // attempts to read the system-dependent data for a process' virtual memory
411 // size and resident set size, and return the results in KB.
413 // On failure, returns 0.0, 0.0
414 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
415 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
420 // 'file' stat seems to give the most reliable results
422 ifstream stat_stream("/proc/self/stat",ios_base::in);
424 // dummy vars for leading entries in stat that we don't care about
426 string pid, comm, state, ppid, pgrp, session, tty_nr;
427 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
428 string utime, stime, cutime, cstime, priority, nice;
429 string O, itrealvalue, starttime;
431 // the two fields we want
436 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
437 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
438 >> utime >> stime >> cutime >> cstime >> priority >> nice
439 >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
441 long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
442 vm_usage = vsize / 1024.0;
443 resident_set = rss * page_size_kb;
445 mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
449 /* //windows memory usage
450 // Get the list of process identifiers.
451 DWORD aProcesses[1024], cbNeeded, cProcesses;
453 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
455 // Calculate how many process identifiers were returned.
456 cProcesses = cbNeeded / sizeof(DWORD);
458 // Print the memory usage for each process
459 for (int i = 0; i < cProcesses; i++ ) {
460 DWORD processID = aProcesses[i];
462 PROCESS_MEMORY_COUNTERS pmc;
464 HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
466 // Print the process identifier.
467 printf( "\nProcess ID: %u\n", processID);
469 if (NULL != hProcess) {
471 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
472 printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
473 printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
474 printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
475 printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
476 printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
477 printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
478 printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
479 printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
480 printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
482 CloseHandle(hProcess);
492 /***********************************************************************/
493 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
495 fileName = getFullPathName(fileName);
497 fileHandle.open(fileName.c_str(), ios::app);
499 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
506 catch(exception& e) {
507 errorOut(e, "MothurOut", "openOutputFileAppend");
511 /***********************************************************************/
512 void MothurOut::gobble(istream& f){
516 while(isspace(d=f.get())) { ;}
517 if(!f.eof()) { f.putback(d); }
519 catch(exception& e) {
520 errorOut(e, "MothurOut", "gobble");
524 /***********************************************************************/
525 void MothurOut::gobble(istringstream& f){
528 while(isspace(d=f.get())) {;}
529 if(!f.eof()) { f.putback(d); }
531 catch(exception& e) {
532 errorOut(e, "MothurOut", "gobble");
537 /***********************************************************************/
539 string MothurOut::getline(istringstream& fileHandle) {
544 while (!fileHandle.eof()) {
546 char c = fileHandle.get();
548 //are you at the end of the line
549 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
556 catch(exception& e) {
557 errorOut(e, "MothurOut", "getline");
561 /***********************************************************************/
563 string MothurOut::getline(ifstream& fileHandle) {
570 char c = fileHandle.get();
572 //are you at the end of the line
573 if ((c == '\n') || (c == '\r') || (c == '\f') || (c == EOF)){ break; }
580 catch(exception& e) {
581 errorOut(e, "MothurOut", "getline");
585 /***********************************************************************/
587 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
588 #ifdef USE_COMPRESSION
589 inline bool endsWith(string s, const char * suffix){
590 size_t suffixLength = strlen(suffix);
591 return s.size() >= suffixLength && s.substr(s.size() - suffixLength, suffixLength).compare(suffix) == 0;
596 string MothurOut::getRootName(string longName){
599 string rootName = longName;
601 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
602 #ifdef USE_COMPRESSION
603 if (endsWith(rootName, ".gz") || endsWith(rootName, ".bz2")) {
604 int pos = rootName.find_last_of('.');
605 rootName = rootName.substr(0, pos);
606 cerr << "shortening " << longName << " to " << rootName << "\n";
610 if(rootName.find_last_of(".") != rootName.npos){
611 int pos = rootName.find_last_of('.')+1;
612 rootName = rootName.substr(0, pos);
617 catch(exception& e) {
618 errorOut(e, "MothurOut", "getRootName");
622 /***********************************************************************/
624 string MothurOut::getSimpleName(string longName){
626 string simpleName = longName;
629 found=longName.find_last_of("/\\");
631 if(found != longName.npos){
632 simpleName = longName.substr(found+1);
637 catch(exception& e) {
638 errorOut(e, "MothurOut", "getSimpleName");
643 /***********************************************************************/
645 int MothurOut::getRandomIndex(int highest){
648 int random = (int) ((float)(highest+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
652 catch(exception& e) {
653 errorOut(e, "MothurOut", "getRandomIndex");
658 /**********************************************************************/
660 string MothurOut::getPathName(string longName){
662 string rootPathName = longName;
664 if(longName.find_last_of("/\\") != longName.npos){
665 int pos = longName.find_last_of("/\\")+1;
666 rootPathName = longName.substr(0, pos);
671 catch(exception& e) {
672 errorOut(e, "MothurOut", "getPathName");
677 /***********************************************************************/
679 bool MothurOut::dirCheck(string& dirName){
685 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
690 //add / to name if needed
691 string lastChar = dirName.substr(dirName.length()-1);
692 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
693 if (lastChar != "/") { dirName += "/"; }
695 if (lastChar != "\\") { dirName += "\\"; }
698 //test to make sure directory exists
699 dirName = getFullPathName(dirName);
700 string outTemp = dirName + tag + "temp";
702 out.open(outTemp.c_str(), ios::trunc);
704 mothurOut(dirName + " directory does not exist or is not writable."); mothurOutEndLine();
707 mothurRemove(outTemp);
713 catch(exception& e) {
714 errorOut(e, "MothurOut", "dirCheck");
719 /***********************************************************************/
721 string MothurOut::hasPath(string longName){
726 found=longName.find_last_of("~/\\");
728 if(found != longName.npos){
729 path = longName.substr(0, found+1);
734 catch(exception& e) {
735 errorOut(e, "MothurOut", "hasPath");
740 /***********************************************************************/
742 string MothurOut::getExtension(string longName){
744 string extension = "";
746 if(longName.find_last_of('.') != longName.npos){
747 int pos = longName.find_last_of('.');
748 extension = longName.substr(pos, longName.length());
753 catch(exception& e) {
754 errorOut(e, "MothurOut", "getExtension");
758 /***********************************************************************/
759 bool MothurOut::isBlank(string fileName){
762 fileName = getFullPathName(fileName);
765 fileHandle.open(fileName.c_str());
767 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
770 //check for blank file
772 if (fileHandle.eof()) { fileHandle.close(); return true; }
777 catch(exception& e) {
778 errorOut(e, "MothurOut", "isBlank");
782 /***********************************************************************/
784 string MothurOut::getFullPathName(string fileName){
787 string path = hasPath(fileName);
791 if (path == "") { return fileName; } //its a simple name
792 else { //we need to complete the pathname
793 // ex. ../../../filename
794 // cwd = /user/work/desktop
797 //get current working directory
798 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
800 if (path.find("~") != -1) { //go to home directory
803 char *homepath = NULL;
804 homepath = getenv ("HOME");
805 if ( homepath != NULL) { homeDir = homepath; }
806 else { homeDir = ""; }
808 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
811 if (path.rfind("./") == string::npos) { return fileName; } //already complete name
812 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
814 //char* cwdpath = new char[1024];
816 //cwdpath=getcwd(cwdpath,size);
819 char *cwdpath = NULL;
820 cwdpath = getcwd(NULL, 0); // or _getcwd
821 if ( cwdpath != NULL) { cwd = cwdpath; }
827 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
829 //break apart the current working directory
831 while (simpleCWD.find_first_of('/') != string::npos) {
832 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
833 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
836 //get last one // ex. ../../../filename = /user/work/desktop/filename
837 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
840 int index = dirs.size()-1;
842 while((pos = path.rfind("./")) != string::npos) { //while you don't have a complete path
843 if (pos == 0) { break; //you are at the end
844 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
845 path = path.substr(0, pos-1);
847 if (index == 0) { break; }
848 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
849 path = path.substr(0, pos);
850 }else if (pos == 1) { break; //you are at the end
851 }else { mothurOut("cannot resolve path for " + fileName + "\n"); return fileName; }
854 for (int i = index; i >= 0; i--) {
855 newFileName = dirs[i] + "/" + newFileName;
858 newFileName = "/" + newFileName;
862 if (path.find("~") != string::npos) { //go to home directory
863 string homeDir = getenv ("HOMEPATH");
864 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
867 if (path.rfind(".\\") == string::npos) { return fileName; } //already complete name
868 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
870 char *cwdpath = NULL;
871 cwdpath = getcwd(NULL, 0); // or _getcwd
872 if ( cwdpath != NULL) { cwd = cwdpath; }
875 //break apart the current working directory
877 while (cwd.find_first_of('\\') != -1) {
878 string dir = cwd.substr(0,cwd.find_first_of('\\'));
879 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
884 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
886 int index = dirs.size()-1;
888 while((pos = path.rfind(".\\")) != string::npos) { //while you don't have a complete path
889 if (pos == 0) { break; //you are at the end
890 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
891 path = path.substr(0, pos-1);
893 if (index == 0) { break; }
894 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
895 path = path.substr(0, pos);
896 }else if (pos == 1) { break; //you are at the end
897 }else { mothurOut("cannot resolve path for " + fileName + "\n"); return fileName; }
900 for (int i = index; i >= 0; i--) {
901 newFileName = dirs[i] + "\\" + newFileName;
910 catch(exception& e) {
911 errorOut(e, "MothurOut", "getFullPathName");
915 /***********************************************************************/
917 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
920 string completeFileName = getFullPathName(fileName);
921 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
922 #ifdef USE_COMPRESSION
923 // check for gzipped or bzipped file
924 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
925 string tempName = string(tmpnam(0));
926 mkfifo(tempName.c_str(), 0666);
927 int fork_result = fork();
928 if (fork_result < 0) {
929 cerr << "Error forking.\n";
931 } else if (fork_result == 0) {
932 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
933 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
934 system(command.c_str());
935 cerr << "Done decompressing " << completeFileName << "\n";
936 mothurRemove(tempName);
939 cerr << "waiting on child process " << fork_result << "\n";
940 completeFileName = tempName;
945 fileHandle.open(completeFileName.c_str());
947 //mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
950 //check for blank file
955 catch(exception& e) {
956 errorOut(e, "MothurOut", "openInputFile - no Error");
960 /***********************************************************************/
962 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
966 string completeFileName = getFullPathName(fileName);
967 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
968 #ifdef USE_COMPRESSION
969 // check for gzipped or bzipped file
970 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
971 string tempName = string(tmpnam(0));
972 mkfifo(tempName.c_str(), 0666);
973 int fork_result = fork();
974 if (fork_result < 0) {
975 cerr << "Error forking.\n";
977 } else if (fork_result == 0) {
978 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
979 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
980 system(command.c_str());
981 cerr << "Done decompressing " << completeFileName << "\n";
982 mothurRemove(tempName);
985 cerr << "waiting on child process " << fork_result << "\n";
986 completeFileName = tempName;
992 fileHandle.open(completeFileName.c_str());
994 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
998 //check for blank file
1000 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
1005 catch(exception& e) {
1006 errorOut(e, "MothurOut", "openInputFile");
1010 /***********************************************************************/
1012 int MothurOut::renameFile(string oldName, string newName){
1015 int exist = openInputFile(newName, inTest, "");
1018 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1019 if (exist == 0) { //you could open it so you want to delete it
1020 string command = "rm " + newName;
1021 system(command.c_str());
1024 string command = "mv " + oldName + " " + newName;
1025 system(command.c_str());
1027 mothurRemove(newName);
1028 int renameOk = rename(oldName.c_str(), newName.c_str());
1033 catch(exception& e) {
1034 errorOut(e, "MothurOut", "renameFile");
1039 /***********************************************************************/
1041 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
1044 string completeFileName = getFullPathName(fileName);
1045 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1046 #ifdef USE_COMPRESSION
1047 // check for gzipped file
1048 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
1049 string tempName = string(tmpnam(0));
1050 mkfifo(tempName.c_str(), 0666);
1051 cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
1052 int fork_result = fork();
1053 if (fork_result < 0) {
1054 cerr << "Error forking.\n";
1056 } else if (fork_result == 0) {
1057 string command = string(endsWith(completeFileName, ".gz") ? "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
1058 system(command.c_str());
1061 completeFileName = tempName;
1066 fileHandle.open(completeFileName.c_str(), ios::trunc);
1068 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1075 catch(exception& e) {
1076 errorOut(e, "MothurOut", "openOutputFile");
1082 /**************************************************************************************************/
1083 int MothurOut::appendFiles(string temp, string filename) {
1088 //open output file in append mode
1089 openOutputFileAppend(filename, output);
1090 int ableToOpen = openInputFile(temp, input, "no error");
1091 //int ableToOpen = openInputFile(temp, input);
1094 if (ableToOpen == 0) { //you opened it
1097 while (!input.eof()) {
1098 input.read(buffer, 4096);
1099 output.write(buffer, input.gcount());
1100 //count number of lines
1101 for (int i = 0; i < input.gcount(); i++) { if (buffer[i] == '\n') {numLines++;} }
1110 catch(exception& e) {
1111 errorOut(e, "MothurOut", "appendFiles");
1116 /**************************************************************************************************/
1117 string MothurOut::sortFile(string distFile, string outputDir){
1120 //if (outputDir == "") { outputDir += hasPath(distFile); }
1121 string outfile = getRootName(distFile) + "sorted.dist";
1124 //if you can, use the unix sort since its been optimized for years
1125 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1126 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
1127 system(command.c_str());
1128 #else //you are stuck with my best attempt...
1129 //windows sort does not have a way to specify a column, only a character in the line
1130 //since we cannot assume that the distance will always be at the the same character location on each line
1131 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
1133 //read in file line by file and put distance first
1134 string tempDistFile = distFile + ".temp";
1137 openInputFile(distFile, input);
1138 openOutputFile(tempDistFile, output);
1140 string firstName, secondName;
1143 input >> firstName >> secondName >> dist;
1144 output << dist << '\t' << firstName << '\t' << secondName << endl;
1151 //sort using windows sort
1152 string tempOutfile = outfile + ".temp";
1153 string command = "sort " + tempDistFile + " /O " + tempOutfile;
1154 system(command.c_str());
1156 //read in sorted file and put distance at end again
1158 openInputFile(tempOutfile, input2);
1159 openOutputFile(outfile, output);
1162 input2 >> dist >> firstName >> secondName;
1163 output << firstName << '\t' << secondName << '\t' << dist << endl;
1170 mothurRemove(tempDistFile);
1171 mothurRemove(tempOutfile);
1176 catch(exception& e) {
1177 errorOut(e, "MothurOut", "sortFile");
1181 /**************************************************************************************************/
1182 vector<unsigned long long> MothurOut::setFilePosFasta(string filename, int& num) {
1184 vector<unsigned long long> positions;
1186 //openInputFile(filename, inFASTA);
1187 inFASTA.open(filename.c_str(), ios::binary);
1190 unsigned long long count = 0;
1191 while(!inFASTA.eof()){
1192 //input = getline(inFASTA);
1193 //cout << input << '\t' << inFASTA.tellg() << endl;
1194 //if (input.length() != 0) {
1195 // if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); cout << (pos - input.length() - 1) << endl; }
1197 //gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
1198 char c = inFASTA.get(); count++;
1200 positions.push_back(count-1);
1201 //cout << count << endl;
1206 num = positions.size();
1211 //get num bytes in file
1212 pFile = fopen (filename.c_str(),"rb");
1213 if (pFile==NULL) perror ("Error opening file");
1215 fseek (pFile, 0, SEEK_END);
1220 unsigned long long size = positions[(positions.size()-1)];
1222 openInputFile(filename, in);
1227 if(in.eof()) { break; }
1232 positions.push_back(size);
1237 catch(exception& e) {
1238 errorOut(e, "MothurOut", "setFilePosFasta");
1242 /**************************************************************************************************/
1243 vector<unsigned long long> MothurOut::setFilePosEachLine(string filename, int& num) {
1245 filename = getFullPathName(filename);
1247 vector<unsigned long long> positions;
1249 //openInputFile(filename, in);
1250 in.open(filename.c_str(), ios::binary);
1253 unsigned long long count = 0;
1254 positions.push_back(0);
1257 //unsigned long long lastpos = in.tellg();
1258 //input = getline(in);
1259 //if (input.length() != 0) {
1260 //unsigned long long pos = in.tellg();
1261 //if (pos != -1) { positions.push_back(pos - input.length() - 1); }
1262 //else { positions.push_back(lastpos); }
1264 //gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
1267 //getline counting reads
1268 char d = in.get(); count++;
1269 while ((d != '\n') && (d != '\r') && (d != '\f') && (d != in.eof())) {
1270 //get next character
1276 d=in.get(); count++;
1277 while(isspace(d) && (d != in.eof())) { d=in.get(); count++;}
1279 positions.push_back(count-1);
1280 //cout << count-1 << endl;
1284 num = positions.size()-1;
1287 unsigned long long size;
1289 //get num bytes in file
1290 pFile = fopen (filename.c_str(),"rb");
1291 if (pFile==NULL) perror ("Error opening file");
1293 fseek (pFile, 0, SEEK_END);
1298 positions[(positions.size()-1)] = size;
1302 catch(exception& e) {
1303 errorOut(e, "MothurOut", "setFilePosEachLine");
1307 /**************************************************************************************************/
1309 vector<unsigned long long> MothurOut::divideFile(string filename, int& proc) {
1311 vector<unsigned long long> filePos;
1312 filePos.push_back(0);
1315 unsigned long long size;
1317 filename = getFullPathName(filename);
1319 //get num bytes in file
1320 pFile = fopen (filename.c_str(),"rb");
1321 if (pFile==NULL) perror ("Error opening file");
1323 fseek (pFile, 0, SEEK_END);
1328 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1330 //estimate file breaks
1331 unsigned long long chunkSize = 0;
1332 chunkSize = size / proc;
1334 //file to small to divide by processors
1335 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1337 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1338 for (int i = 0; i < proc; i++) {
1339 unsigned long long spot = (i+1) * chunkSize;
1342 openInputFile(filename, in);
1346 unsigned long long newSpot = spot;
1350 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1351 else if (int(c) == -1) { break; }
1355 //there was not another sequence before the end of the file
1356 unsigned long long sanityPos = in.tellg();
1358 if (sanityPos == -1) { break; }
1359 else { filePos.push_back(newSpot); }
1365 filePos.push_back(size);
1367 //sanity check filePos
1368 for (int i = 0; i < (filePos.size()-1); i++) {
1369 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1372 proc = (filePos.size() - 1);
1374 mothurOut("[ERROR]: Windows version should not be calling the divideFile function."); mothurOutEndLine();
1376 filePos.push_back(size);
1380 catch(exception& e) {
1381 errorOut(e, "MothurOut", "divideFile");
1385 /**************************************************************************************************/
1386 int MothurOut::divideFile(string filename, int& proc, vector<string>& files) {
1389 vector<unsigned long long> filePos = divideFile(filename, proc);
1391 for (int i = 0; i < (filePos.size()-1); i++) {
1395 openInputFile(filename, in);
1396 in.seekg(filePos[i]);
1397 unsigned long long size = filePos[(i+1)] - filePos[i];
1398 char* chunk = new char[size];
1399 in.read(chunk, size);
1403 string fileChunkName = filename + "." + toString(i) + ".tmp";
1405 openOutputFile(fileChunkName, out);
1407 out << chunk << endl;
1412 files.push_back(fileChunkName);
1417 catch(exception& e) {
1418 errorOut(e, "MothurOut", "divideFile");
1422 /***********************************************************************/
1424 bool MothurOut::isTrue(string f){
1427 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1429 if ((f == "TRUE") || (f == "T")) { return true; }
1430 else { return false; }
1432 catch(exception& e) {
1433 errorOut(e, "MothurOut", "isTrue");
1438 /***********************************************************************/
1440 float MothurOut::roundDist(float dist, int precision){
1442 return int(dist * precision + 0.5)/float(precision);
1444 catch(exception& e) {
1445 errorOut(e, "MothurOut", "roundDist");
1449 /***********************************************************************/
1451 float MothurOut::ceilDist(float dist, int precision){
1453 return int(ceil(dist * precision))/float(precision);
1455 catch(exception& e) {
1456 errorOut(e, "MothurOut", "ceilDist");
1460 /***********************************************************************/
1462 vector<string> MothurOut::splitWhiteSpace(string& rest, char buffer[], int size){
1464 vector<string> pieces;
1466 for (int i = 0; i < size; i++) {
1467 if (!isspace(buffer[i])) { rest += buffer[i]; }
1469 pieces.push_back(rest); rest = "";
1470 while (i < size) { //gobble white space
1471 if (isspace(buffer[i])) { i++; }
1472 else { rest = buffer[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1479 catch(exception& e) {
1480 errorOut(e, "MothurOut", "parsePieces");
1484 /**********************************************************************************************************************/
1485 int MothurOut::readNames(string namefile, map<string, string>& nameMap) {
1490 openInputFile(namefile, in);
1494 bool pairDone = false;
1495 bool columnOne = true;
1496 string firstCol, secondCol;
1499 if (control_pressed) { break; }
1501 in.read(buffer, 4096);
1502 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1504 for (int i = 0; i < pieces.size(); i++) {
1505 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1506 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1508 if (pairDone) { nameMap[firstCol] = secondCol; pairDone = false; }
1513 return nameMap.size();
1516 catch(exception& e) {
1517 errorOut(e, "MothurOut", "readNames");
1521 /**********************************************************************************************************************/
1522 int MothurOut::readNames(string namefile, map<string, vector<string> >& nameMap) {
1527 openInputFile(namefile, in);
1531 bool pairDone = false;
1532 bool columnOne = true;
1533 string firstCol, secondCol;
1536 if (control_pressed) { break; }
1538 in.read(buffer, 4096);
1539 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1541 for (int i = 0; i < pieces.size(); i++) {
1542 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1543 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1546 vector<string> temp;
1547 splitAtComma(secondCol, temp);
1548 nameMap[firstCol] = temp;
1555 return nameMap.size();
1557 catch(exception& e) {
1558 errorOut(e, "MothurOut", "readNames");
1562 /**********************************************************************************************************************/
1563 map<string, int> MothurOut::readNames(string namefile) {
1566 map<string, int> nameMap;
1570 openInputFile(namefile, in);
1574 bool pairDone = false;
1575 bool columnOne = true;
1576 string firstCol, secondCol;
1579 if (control_pressed) { break; }
1581 in.read(buffer, 4096);
1582 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1584 for (int i = 0; i < pieces.size(); i++) {
1585 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1586 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1589 int num = getNumNames(secondCol);
1590 nameMap[firstCol] = num;
1600 catch(exception& e) {
1601 errorOut(e, "MothurOut", "readNames");
1605 /**********************************************************************************************************************/
1606 int MothurOut::readNames(string namefile, vector<seqPriorityNode>& nameVector, map<string, string>& fastamap) {
1612 openInputFile(namefile, in);
1616 bool pairDone = false;
1617 bool columnOne = true;
1618 string firstCol, secondCol;
1621 if (control_pressed) { break; }
1623 in.read(buffer, 4096);
1624 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1626 for (int i = 0; i < pieces.size(); i++) {
1627 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1628 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1631 int num = getNumNames(secondCol);
1633 map<string, string>::iterator it = fastamap.find(firstCol);
1634 if (it == fastamap.end()) {
1636 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
1638 seqPriorityNode temp(num, it->second, firstCol);
1639 nameVector.push_back(temp);
1650 catch(exception& e) {
1651 errorOut(e, "MothurOut", "readNames");
1656 /***********************************************************************/
1658 int MothurOut::getNumNames(string names){
1664 for(int i=0;i<names.size();i++){
1665 if(names[i] == ','){
1673 catch(exception& e) {
1674 errorOut(e, "MothurOut", "getNumNames");
1678 /***********************************************************************/
1680 int MothurOut::getNumChar(string line, char c){
1685 for(int i=0;i<line.size();i++){
1694 catch(exception& e) {
1695 errorOut(e, "MothurOut", "getNumChar");
1699 /***********************************************************************/
1700 int MothurOut::mothurRemove(string filename){
1702 filename = getFullPathName(filename);
1703 int error = remove(filename.c_str());
1705 // if (errno != ENOENT) { //ENOENT == file does not exist
1706 // string message = "Error deleting file " + filename;
1707 // perror(message.c_str());
1712 catch(exception& e) {
1713 errorOut(e, "MothurOut", "mothurRemove");
1717 /***********************************************************************/
1718 bool MothurOut::mothurConvert(string item, int& num){
1722 if (isNumeric1(item)) {
1727 mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
1728 commandInputsConvertError = true;
1733 catch(exception& e) {
1734 errorOut(e, "MothurOut", "mothurConvert");
1738 /***********************************************************************/
1739 bool MothurOut::isNumeric1(string stringToCheck){
1741 bool numeric = false;
1743 if(stringToCheck.find_first_not_of("0123456789.-") == string::npos) { numeric = true; }
1747 catch(exception& e) {
1748 errorOut(e, "MothurOut", "isNumeric1");
1753 /***********************************************************************/
1754 bool MothurOut::mothurConvert(string item, float& num){
1758 if (isNumeric1(item)) {
1763 mothurOut("[ERROR]: cannot convert " + item + " to a float."); mothurOutEndLine();
1764 commandInputsConvertError = true;
1769 catch(exception& e) {
1770 errorOut(e, "MothurOut", "mothurConvert");
1774 /***********************************************************************/
1775 bool MothurOut::mothurConvert(string item, double& num){
1779 if (isNumeric1(item)) {
1784 mothurOut("[ERROR]: cannot convert " + item + " to a double."); mothurOutEndLine();
1785 commandInputsConvertError = true;
1790 catch(exception& e) {
1791 errorOut(e, "MothurOut", "mothurConvert");
1795 /**************************************************************************************************/
1797 vector<vector<double> > MothurOut::binomial(int maxOrder){
1799 vector<vector<double> > binomial(maxOrder+1);
1801 for(int i=0;i<=maxOrder;i++){
1802 binomial[i].resize(maxOrder+1);
1811 for(int i=2;i<=maxOrder;i++){
1815 for(int i=2;i<=maxOrder;i++){
1816 for(int j=1;j<=maxOrder;j++){
1817 if(i==j){ binomial[i][j]=1; }
1818 if(j>i) { binomial[i][j]=0; }
1819 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
1826 catch(exception& e) {
1827 errorOut(e, "MothurOut", "binomial");
1831 /**************************************************************************************************/
1832 unsigned int MothurOut::fromBase36(string base36){
1834 unsigned int num = 0;
1836 map<char, int> converts;
1901 while (i < base36.length()) {
1903 num = 36 * num + converts[c];
1910 catch(exception& e) {
1911 errorOut(e, "MothurOut", "fromBase36");
1915 /***********************************************************************/
1917 int MothurOut::factorial(int num){
1921 for (int i = 1; i <= num; i++) {
1927 catch(exception& e) {
1928 errorOut(e, "MothurOut", "factorial");
1932 /***********************************************************************/
1934 int MothurOut::getNumSeqs(ifstream& file){
1936 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1940 catch(exception& e) {
1941 errorOut(e, "MothurOut", "getNumSeqs");
1945 /***********************************************************************/
1946 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1951 input = getline(file);
1952 if (input.length() != 0) {
1953 if(input[0] == '>'){ numSeqs++; }
1957 catch(exception& e) {
1958 errorOut(e, "MothurOut", "getNumSeqs");
1962 /***********************************************************************/
1964 //This function parses the estimator options and puts them in a vector
1965 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1967 string individual = "";
1968 int estimLength = estim.size();
1969 for(int i=0;i<estimLength;i++){
1970 if(estim[i] == symbol){
1971 container.push_back(individual);
1975 individual += estim[i];
1978 container.push_back(individual);
1981 catch(exception& e) {
1982 errorOut(e, "MothurOut", "splitAtChar");
1987 /***********************************************************************/
1989 //This function parses the estimator options and puts them in a vector
1990 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1992 string individual = "";
1993 int estimLength = estim.size();
1994 for(int i=0;i<estimLength;i++){
1995 if(estim[i] == '-'){
1996 container.push_back(individual);
2000 individual += estim[i];
2003 container.push_back(individual);
2006 /* string individual;
2008 while (estim.find_first_of('-') != -1) {
2009 individual = estim.substr(0,estim.find_first_of('-'));
2010 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
2011 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
2012 container.push_back(individual);
2016 container.push_back(estim); */
2018 catch(exception& e) {
2019 errorOut(e, "MothurOut", "splitAtDash");
2024 /***********************************************************************/
2025 //This function parses the label options and puts them in a set
2026 void MothurOut::splitAtDash(string& estim, set<string>& container) {
2028 string individual = "";
2029 int estimLength = estim.size();
2030 for(int i=0;i<estimLength;i++){
2031 if(estim[i] == '-'){
2032 container.insert(individual);
2036 individual += estim[i];
2039 container.insert(individual);
2041 // string individual;
2043 // while (estim.find_first_of('-') != -1) {
2044 // individual = estim.substr(0,estim.find_first_of('-'));
2045 // if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
2046 // estim = estim.substr(estim.find_first_of('-')+1, estim.length());
2047 // container.insert(individual);
2051 // container.insert(estim);
2054 catch(exception& e) {
2055 errorOut(e, "MothurOut", "splitAtDash");
2059 /***********************************************************************/
2060 //This function parses the line options and puts them in a set
2061 void MothurOut::splitAtDash(string& estim, set<int>& container) {
2066 while (estim.find_first_of('-') != -1) {
2067 individual = estim.substr(0,estim.find_first_of('-'));
2068 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
2069 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
2070 convert(individual, lineNum); //convert the string to int
2071 container.insert(lineNum);
2075 convert(estim, lineNum); //convert the string to int
2076 container.insert(lineNum);
2078 catch(exception& e) {
2079 errorOut(e, "MothurOut", "splitAtDash");
2083 /***********************************************************************/
2084 string MothurOut::makeList(vector<string>& names) {
2088 if (names.size() == 0) { return list; }
2090 for (int i = 0; i < names.size()-1; i++) { list += names[i] + ","; }
2093 list += names[names.size()-1];
2097 catch(exception& e) {
2098 errorOut(e, "MothurOut", "makeList");
2103 /***********************************************************************/
2104 //This function parses the a string and puts peices in a vector
2105 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
2107 string individual = "";
2108 int estimLength = estim.size();
2109 for(int i=0;i<estimLength;i++){
2110 if(estim[i] == ','){
2111 container.push_back(individual);
2115 individual += estim[i];
2118 container.push_back(individual);
2123 // string individual;
2125 // while (estim.find_first_of(',') != -1) {
2126 // individual = estim.substr(0,estim.find_first_of(','));
2127 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
2128 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
2129 // container.push_back(individual);
2133 // container.push_back(estim);
2135 catch(exception& e) {
2136 errorOut(e, "MothurOut", "splitAtComma");
2140 /***********************************************************************/
2141 //This function splits up the various option parameters
2142 void MothurOut::splitAtChar(string& prefix, string& suffix, char c){
2144 prefix = suffix.substr(0,suffix.find_first_of(c));
2145 if ((suffix.find_first_of(c)+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2146 suffix = suffix.substr(suffix.find_first_of(c)+1, suffix.length());
2148 while(suffix.at(0) == ' ')
2149 suffix = suffix.substr(1, suffix.length());
2153 catch(exception& e) {
2154 errorOut(e, "MothurOut", "splitAtComma");
2159 /***********************************************************************/
2161 //This function splits up the various option parameters
2162 void MothurOut::splitAtComma(string& prefix, string& suffix){
2164 prefix = suffix.substr(0,suffix.find_first_of(','));
2165 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2166 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
2168 while(suffix.at(0) == ' ')
2169 suffix = suffix.substr(1, suffix.length());
2173 catch(exception& e) {
2174 errorOut(e, "MothurOut", "splitAtComma");
2178 /***********************************************************************/
2180 //This function separates the key value from the option value i.e. dist=96_...
2181 void MothurOut::splitAtEquals(string& key, string& value){
2183 if(value.find_first_of('=') != -1){
2184 key = value.substr(0,value.find_first_of('='));
2185 if ((value.find_first_of('=')+1) <= value.length()) {
2186 value = value.substr(value.find_first_of('=')+1, value.length());
2193 catch(exception& e) {
2194 errorOut(e, "MothurOut", "splitAtEquals");
2199 /**************************************************************************************************/
2201 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
2203 for (int i = 0; i < Groups.size(); i++) {
2204 if (groupname == Groups[i]) { return true; }
2208 catch(exception& e) {
2209 errorOut(e, "MothurOut", "inUsersGroups");
2213 /**************************************************************************************************/
2214 //returns true if any of the strings in first vector are in second vector
2215 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
2218 for (int i = 0; i < groupnames.size(); i++) {
2219 if (inUsersGroups(groupnames[i], Groups)) { return true; }
2223 catch(exception& e) {
2224 errorOut(e, "MothurOut", "inUsersGroups");
2228 /***********************************************************************/
2229 //this function determines if the user has given us labels that are smaller than the given label.
2230 //if so then it returns true so that the calling function can run the previous valid distance.
2231 //it's a "smart" distance function. It also checks for invalid labels.
2232 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
2235 set<string>::iterator it;
2236 vector<float> orderFloat;
2237 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
2238 map<string, float>::iterator it2;
2240 bool smaller = false;
2242 //unique is the smallest line
2243 if (label == "unique") { return false; }
2245 if (convertTestFloat(label, labelFloat)) {
2246 convert(label, labelFloat);
2247 }else { //cant convert
2252 //go through users set and make them floats
2253 for(it = userLabels.begin(); it != userLabels.end();) {
2256 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
2258 orderFloat.push_back(temp);
2259 userMap[*it] = temp;
2261 }else if (*it == "unique") {
2262 orderFloat.push_back(-1.0);
2263 userMap["unique"] = -1.0;
2266 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
2267 userLabels.erase(it++);
2272 sort(orderFloat.begin(), orderFloat.end());
2274 /*************************************************/
2275 //is this label bigger than any of the users labels
2276 /*************************************************/
2278 //loop through order until you find a label greater than label
2279 for (int i = 0; i < orderFloat.size(); i++) {
2280 if (orderFloat[i] < labelFloat) {
2282 if (orderFloat[i] == -1) {
2283 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
2284 userLabels.erase("unique");
2287 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
2289 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
2290 if (it2->second == orderFloat[i]) {
2292 //remove small labels
2293 userLabels.erase(s);
2297 if (errorOff == "") {mothurOut( s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
2299 //since they are sorted once you find a bigger one stop looking
2306 catch(exception& e) {
2307 errorOut(e, "MothurOut", "anyLabelsToProcess");
2312 /**************************************************************************************************/
2313 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
2318 string line = getline(file);
2320 //before we added this check
2321 if (line[0] != '#') { good = false; }
2324 line = line.substr(1);
2326 vector<string> versionVector;
2327 splitAtChar(version, versionVector, '.');
2329 //check file version
2330 vector<string> linesVector;
2331 splitAtChar(line, linesVector, '.');
2333 if (versionVector.size() != linesVector.size()) { good = false; }
2335 for (int j = 0; j < versionVector.size(); j++) {
2337 convert(versionVector[j], num1);
2338 convert(linesVector[j], num2);
2340 //if mothurs version is newer than this files version, then we want to remake it
2341 if (num1 > num2) { good = false; break; }
2347 if (!good) { file.close(); }
2348 else { file.seekg(0); }
2352 catch(exception& e) {
2353 errorOut(e, "MothurOut", "checkReleaseVersion");
2357 /**************************************************************************************************/
2358 bool MothurOut::isContainingOnlyDigits(string input) {
2361 //are you a digit in ascii code
2362 for (int i = 0;i < input.length(); i++){
2363 if( input[i]>47 && input[i]<58){}
2364 else { return false; }
2369 catch(exception& e) {
2370 errorOut(e, "MothurOut", "isContainingOnlyDigits");
2374 /**************************************************************************************************/
2375 int MothurOut::removeConfidences(string& tax) {
2381 while (tax.find_first_of(';') != -1) {
2383 if (control_pressed) { return 0; }
2386 taxon = tax.substr(0,tax.find_first_of(';'));
2388 int pos = taxon.find_last_of('(');
2391 int pos2 = taxon.find_last_of(')');
2393 string confidenceScore = taxon.substr(pos+1, (pos2-(pos+1)));
2394 if (isNumeric1(confidenceScore)) {
2395 taxon = taxon.substr(0, pos); //rip off confidence
2401 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
2409 catch(exception& e) {
2410 errorOut(e, "MothurOut", "removeConfidences");
2414 /**************************************************************************************************/