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
1095 while(!input.eof()){
1096 char c = input.get();
1097 if(input.eof()) { break; }
1098 else { output << c; if (c == '\n') {numLines++;} }
1107 catch(exception& e) {
1108 errorOut(e, "MothurOut", "appendFiles");
1113 /**************************************************************************************************/
1114 string MothurOut::sortFile(string distFile, string outputDir){
1117 //if (outputDir == "") { outputDir += hasPath(distFile); }
1118 string outfile = getRootName(distFile) + "sorted.dist";
1121 //if you can, use the unix sort since its been optimized for years
1122 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1123 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
1124 system(command.c_str());
1125 #else //you are stuck with my best attempt...
1126 //windows sort does not have a way to specify a column, only a character in the line
1127 //since we cannot assume that the distance will always be at the the same character location on each line
1128 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
1130 //read in file line by file and put distance first
1131 string tempDistFile = distFile + ".temp";
1134 openInputFile(distFile, input);
1135 openOutputFile(tempDistFile, output);
1137 string firstName, secondName;
1140 input >> firstName >> secondName >> dist;
1141 output << dist << '\t' << firstName << '\t' << secondName << endl;
1148 //sort using windows sort
1149 string tempOutfile = outfile + ".temp";
1150 string command = "sort " + tempDistFile + " /O " + tempOutfile;
1151 system(command.c_str());
1153 //read in sorted file and put distance at end again
1155 openInputFile(tempOutfile, input2);
1156 openOutputFile(outfile, output);
1159 input2 >> dist >> firstName >> secondName;
1160 output << firstName << '\t' << secondName << '\t' << dist << endl;
1167 mothurRemove(tempDistFile);
1168 mothurRemove(tempOutfile);
1173 catch(exception& e) {
1174 errorOut(e, "MothurOut", "sortFile");
1178 /**************************************************************************************************/
1179 vector<unsigned long long> MothurOut::setFilePosFasta(string filename, int& num) {
1181 vector<unsigned long long> positions;
1183 //openInputFile(filename, inFASTA);
1184 inFASTA.open(filename.c_str(), ios::binary);
1187 unsigned long long count = 0;
1188 while(!inFASTA.eof()){
1189 //input = getline(inFASTA);
1190 //cout << input << '\t' << inFASTA.tellg() << endl;
1191 //if (input.length() != 0) {
1192 // if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); cout << (pos - input.length() - 1) << endl; }
1194 //gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
1195 char c = inFASTA.get(); count++;
1197 positions.push_back(count-1);
1198 //cout << count << endl;
1203 num = positions.size();
1208 //get num bytes in file
1209 pFile = fopen (filename.c_str(),"rb");
1210 if (pFile==NULL) perror ("Error opening file");
1212 fseek (pFile, 0, SEEK_END);
1217 unsigned long long size = positions[(positions.size()-1)];
1219 openInputFile(filename, in);
1224 if(in.eof()) { break; }
1229 positions.push_back(size);
1234 catch(exception& e) {
1235 errorOut(e, "MothurOut", "setFilePosFasta");
1239 /**************************************************************************************************/
1240 vector<unsigned long long> MothurOut::setFilePosEachLine(string filename, int& num) {
1242 filename = getFullPathName(filename);
1244 vector<unsigned long long> positions;
1246 //openInputFile(filename, in);
1247 in.open(filename.c_str(), ios::binary);
1250 unsigned long long count = 0;
1251 positions.push_back(0);
1254 //unsigned long long lastpos = in.tellg();
1255 //input = getline(in);
1256 //if (input.length() != 0) {
1257 //unsigned long long pos = in.tellg();
1258 //if (pos != -1) { positions.push_back(pos - input.length() - 1); }
1259 //else { positions.push_back(lastpos); }
1261 //gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
1264 //getline counting reads
1265 char d = in.get(); count++;
1266 while ((d != '\n') && (d != '\r') && (d != '\f') && (d != in.eof())) {
1267 //get next character
1273 d=in.get(); count++;
1274 while(isspace(d) && (d != in.eof())) { d=in.get(); count++;}
1276 positions.push_back(count-1);
1277 //cout << count-1 << endl;
1281 num = positions.size()-1;
1284 unsigned long long size;
1286 //get num bytes in file
1287 pFile = fopen (filename.c_str(),"rb");
1288 if (pFile==NULL) perror ("Error opening file");
1290 fseek (pFile, 0, SEEK_END);
1295 positions[(positions.size()-1)] = size;
1299 catch(exception& e) {
1300 errorOut(e, "MothurOut", "setFilePosEachLine");
1304 /**************************************************************************************************/
1306 vector<unsigned long long> MothurOut::divideFile(string filename, int& proc) {
1308 vector<unsigned long long> filePos;
1309 filePos.push_back(0);
1312 unsigned long long size;
1314 filename = getFullPathName(filename);
1316 //get num bytes in file
1317 pFile = fopen (filename.c_str(),"rb");
1318 if (pFile==NULL) perror ("Error opening file");
1320 fseek (pFile, 0, SEEK_END);
1325 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1327 //estimate file breaks
1328 unsigned long long chunkSize = 0;
1329 chunkSize = size / proc;
1331 //file to small to divide by processors
1332 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1334 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1335 for (int i = 0; i < proc; i++) {
1336 unsigned long long spot = (i+1) * chunkSize;
1339 openInputFile(filename, in);
1343 unsigned long long newSpot = spot;
1347 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1348 else if (int(c) == -1) { break; }
1352 //there was not another sequence before the end of the file
1353 unsigned long long sanityPos = in.tellg();
1355 if (sanityPos == -1) { break; }
1356 else { filePos.push_back(newSpot); }
1362 filePos.push_back(size);
1364 //sanity check filePos
1365 for (int i = 0; i < (filePos.size()-1); i++) {
1366 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1369 proc = (filePos.size() - 1);
1371 mothurOut("[ERROR]: Windows version should not be calling the divideFile function."); mothurOutEndLine();
1373 filePos.push_back(size);
1377 catch(exception& e) {
1378 errorOut(e, "MothurOut", "divideFile");
1382 /**************************************************************************************************/
1383 int MothurOut::divideFile(string filename, int& proc, vector<string>& files) {
1386 vector<unsigned long long> filePos = divideFile(filename, proc);
1388 for (int i = 0; i < (filePos.size()-1); i++) {
1392 openInputFile(filename, in);
1393 in.seekg(filePos[i]);
1394 unsigned long long size = filePos[(i+1)] - filePos[i];
1395 char* chunk = new char[size];
1396 in.read(chunk, size);
1400 string fileChunkName = filename + "." + toString(i) + ".tmp";
1402 openOutputFile(fileChunkName, out);
1404 out << chunk << endl;
1409 files.push_back(fileChunkName);
1414 catch(exception& e) {
1415 errorOut(e, "MothurOut", "divideFile");
1419 /***********************************************************************/
1421 bool MothurOut::isTrue(string f){
1424 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1426 if ((f == "TRUE") || (f == "T")) { return true; }
1427 else { return false; }
1429 catch(exception& e) {
1430 errorOut(e, "MothurOut", "isTrue");
1435 /***********************************************************************/
1437 float MothurOut::roundDist(float dist, int precision){
1439 return int(dist * precision + 0.5)/float(precision);
1441 catch(exception& e) {
1442 errorOut(e, "MothurOut", "roundDist");
1446 /***********************************************************************/
1448 float MothurOut::ceilDist(float dist, int precision){
1450 return int(ceil(dist * precision))/float(precision);
1452 catch(exception& e) {
1453 errorOut(e, "MothurOut", "ceilDist");
1457 /**********************************************************************************************************************/
1458 int MothurOut::readNames(string namefile, map<string, string>& nameMap) {
1463 openInputFile(namefile, in);
1466 if (control_pressed) { break; }
1468 string firstCol, secondCol;
1469 in >> firstCol >> secondCol; gobble(in);
1471 nameMap[firstCol] = secondCol;
1475 return nameMap.size();
1478 catch(exception& e) {
1479 errorOut(e, "MothurOut", "readNames");
1483 /**********************************************************************************************************************/
1484 int MothurOut::readNames(string namefile, map<string, vector<string> >& nameMap) {
1489 openInputFile(namefile, in);
1492 if (control_pressed) { break; }
1494 string firstCol, secondCol;
1495 in >> firstCol >> secondCol; gobble(in);
1497 vector<string> temp;
1498 splitAtComma(secondCol, temp);
1500 nameMap[firstCol] = temp;
1504 return nameMap.size();
1507 catch(exception& e) {
1508 errorOut(e, "MothurOut", "readNames");
1512 /**********************************************************************************************************************/
1513 map<string, int> MothurOut::readNames(string namefile) {
1516 map<string, int> nameMap;
1520 openInputFile(namefile, in);
1523 if (control_pressed) { break; }
1525 string firstCol, secondCol;
1526 in >> firstCol; gobble(in);
1527 in >> secondCol; gobble(in);
1529 int num = getNumNames(secondCol);
1531 nameMap[firstCol] = num;
1538 catch(exception& e) {
1539 errorOut(e, "MothurOut", "readNames");
1543 /**********************************************************************************************************************/
1544 int MothurOut::readNames(string namefile, vector<seqPriorityNode>& nameVector, map<string, string>& fastamap) {
1550 openInputFile(namefile, in);
1553 if (control_pressed) { break; }
1555 string firstCol, secondCol;
1556 in >> firstCol >> secondCol; gobble(in);
1558 int num = getNumNames(secondCol);
1560 map<string, string>::iterator it = fastamap.find(firstCol);
1561 if (it == fastamap.end()) {
1563 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
1565 seqPriorityNode temp(num, it->second, firstCol);
1566 nameVector.push_back(temp);
1574 catch(exception& e) {
1575 errorOut(e, "MothurOut", "readNames");
1580 /***********************************************************************/
1582 int MothurOut::getNumNames(string names){
1588 for(int i=0;i<names.size();i++){
1589 if(names[i] == ','){
1597 catch(exception& e) {
1598 errorOut(e, "MothurOut", "getNumNames");
1602 /***********************************************************************/
1604 int MothurOut::getNumChar(string line, char c){
1609 for(int i=0;i<line.size();i++){
1618 catch(exception& e) {
1619 errorOut(e, "MothurOut", "getNumChar");
1623 /***********************************************************************/
1624 int MothurOut::mothurRemove(string filename){
1626 filename = getFullPathName(filename);
1627 int error = remove(filename.c_str());
1629 // if (errno != ENOENT) { //ENOENT == file does not exist
1630 // string message = "Error deleting file " + filename;
1631 // perror(message.c_str());
1636 catch(exception& e) {
1637 errorOut(e, "MothurOut", "mothurRemove");
1641 /***********************************************************************/
1642 bool MothurOut::mothurConvert(string item, int& num){
1646 if (isNumeric1(item)) {
1651 mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
1652 commandInputsConvertError = true;
1657 catch(exception& e) {
1658 errorOut(e, "MothurOut", "mothurConvert");
1662 /***********************************************************************/
1663 bool MothurOut::isNumeric1(string stringToCheck){
1665 bool numeric = false;
1667 if(stringToCheck.find_first_not_of("0123456789.-") == string::npos) { numeric = true; }
1671 catch(exception& e) {
1672 errorOut(e, "MothurOut", "isNumeric1");
1677 /***********************************************************************/
1678 bool MothurOut::mothurConvert(string item, float& num){
1682 if (isNumeric1(item)) {
1687 mothurOut("[ERROR]: cannot convert " + item + " to a float."); mothurOutEndLine();
1688 commandInputsConvertError = true;
1693 catch(exception& e) {
1694 errorOut(e, "MothurOut", "mothurConvert");
1698 /***********************************************************************/
1699 bool MothurOut::mothurConvert(string item, double& num){
1703 if (isNumeric1(item)) {
1708 mothurOut("[ERROR]: cannot convert " + item + " to a double."); mothurOutEndLine();
1709 commandInputsConvertError = true;
1714 catch(exception& e) {
1715 errorOut(e, "MothurOut", "mothurConvert");
1719 /**************************************************************************************************/
1721 vector<vector<double> > MothurOut::binomial(int maxOrder){
1723 vector<vector<double> > binomial(maxOrder+1);
1725 for(int i=0;i<=maxOrder;i++){
1726 binomial[i].resize(maxOrder+1);
1735 for(int i=2;i<=maxOrder;i++){
1739 for(int i=2;i<=maxOrder;i++){
1740 for(int j=1;j<=maxOrder;j++){
1741 if(i==j){ binomial[i][j]=1; }
1742 if(j>i) { binomial[i][j]=0; }
1743 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
1750 catch(exception& e) {
1751 errorOut(e, "MothurOut", "binomial");
1755 /**************************************************************************************************/
1756 unsigned int MothurOut::fromBase36(string base36){
1758 unsigned int num = 0;
1760 map<char, int> converts;
1825 while (i < base36.length()) {
1827 num = 36 * num + converts[c];
1834 catch(exception& e) {
1835 errorOut(e, "MothurOut", "fromBase36");
1839 /***********************************************************************/
1841 int MothurOut::factorial(int num){
1845 for (int i = 1; i <= num; i++) {
1851 catch(exception& e) {
1852 errorOut(e, "MothurOut", "factorial");
1856 /***********************************************************************/
1858 int MothurOut::getNumSeqs(ifstream& file){
1860 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1864 catch(exception& e) {
1865 errorOut(e, "MothurOut", "getNumSeqs");
1869 /***********************************************************************/
1870 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1875 input = getline(file);
1876 if (input.length() != 0) {
1877 if(input[0] == '>'){ numSeqs++; }
1881 catch(exception& e) {
1882 errorOut(e, "MothurOut", "getNumSeqs");
1886 /***********************************************************************/
1888 //This function parses the estimator options and puts them in a vector
1889 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1891 string individual = "";
1892 int estimLength = estim.size();
1893 for(int i=0;i<estimLength;i++){
1894 if(estim[i] == symbol){
1895 container.push_back(individual);
1899 individual += estim[i];
1902 container.push_back(individual);
1905 catch(exception& e) {
1906 errorOut(e, "MothurOut", "splitAtChar");
1911 /***********************************************************************/
1913 //This function parses the estimator options and puts them in a vector
1914 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1916 string individual = "";
1917 int estimLength = estim.size();
1918 for(int i=0;i<estimLength;i++){
1919 if(estim[i] == '-'){
1920 container.push_back(individual);
1924 individual += estim[i];
1927 container.push_back(individual);
1930 /* string individual;
1932 while (estim.find_first_of('-') != -1) {
1933 individual = estim.substr(0,estim.find_first_of('-'));
1934 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1935 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1936 container.push_back(individual);
1940 container.push_back(estim); */
1942 catch(exception& e) {
1943 errorOut(e, "MothurOut", "splitAtDash");
1948 /***********************************************************************/
1949 //This function parses the label options and puts them in a set
1950 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1952 string individual = "";
1953 int estimLength = estim.size();
1954 for(int i=0;i<estimLength;i++){
1955 if(estim[i] == '-'){
1956 container.insert(individual);
1960 individual += estim[i];
1963 container.insert(individual);
1965 // string individual;
1967 // while (estim.find_first_of('-') != -1) {
1968 // individual = estim.substr(0,estim.find_first_of('-'));
1969 // if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1970 // estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1971 // container.insert(individual);
1975 // container.insert(estim);
1978 catch(exception& e) {
1979 errorOut(e, "MothurOut", "splitAtDash");
1983 /***********************************************************************/
1984 //This function parses the line options and puts them in a set
1985 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1990 while (estim.find_first_of('-') != -1) {
1991 individual = estim.substr(0,estim.find_first_of('-'));
1992 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1993 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1994 convert(individual, lineNum); //convert the string to int
1995 container.insert(lineNum);
1999 convert(estim, lineNum); //convert the string to int
2000 container.insert(lineNum);
2002 catch(exception& e) {
2003 errorOut(e, "MothurOut", "splitAtDash");
2007 /***********************************************************************/
2008 string MothurOut::makeList(vector<string>& names) {
2012 if (names.size() == 0) { return list; }
2014 for (int i = 0; i < names.size()-1; i++) { list += names[i] + ","; }
2017 list += names[names.size()-1];
2021 catch(exception& e) {
2022 errorOut(e, "MothurOut", "makeList");
2027 /***********************************************************************/
2028 //This function parses the a string and puts peices in a vector
2029 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
2031 string individual = "";
2032 int estimLength = estim.size();
2033 for(int i=0;i<estimLength;i++){
2034 if(estim[i] == ','){
2035 container.push_back(individual);
2039 individual += estim[i];
2042 container.push_back(individual);
2047 // string individual;
2049 // while (estim.find_first_of(',') != -1) {
2050 // individual = estim.substr(0,estim.find_first_of(','));
2051 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
2052 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
2053 // container.push_back(individual);
2057 // container.push_back(estim);
2059 catch(exception& e) {
2060 errorOut(e, "MothurOut", "splitAtComma");
2064 /***********************************************************************/
2065 //This function splits up the various option parameters
2066 void MothurOut::splitAtChar(string& prefix, string& suffix, char c){
2068 prefix = suffix.substr(0,suffix.find_first_of(c));
2069 if ((suffix.find_first_of(c)+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2070 suffix = suffix.substr(suffix.find_first_of(c)+1, suffix.length());
2072 while(suffix.at(0) == ' ')
2073 suffix = suffix.substr(1, suffix.length());
2077 catch(exception& e) {
2078 errorOut(e, "MothurOut", "splitAtComma");
2083 /***********************************************************************/
2085 //This function splits up the various option parameters
2086 void MothurOut::splitAtComma(string& prefix, string& suffix){
2088 prefix = suffix.substr(0,suffix.find_first_of(','));
2089 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2090 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
2092 while(suffix.at(0) == ' ')
2093 suffix = suffix.substr(1, suffix.length());
2097 catch(exception& e) {
2098 errorOut(e, "MothurOut", "splitAtComma");
2102 /***********************************************************************/
2104 //This function separates the key value from the option value i.e. dist=96_...
2105 void MothurOut::splitAtEquals(string& key, string& value){
2107 if(value.find_first_of('=') != -1){
2108 key = value.substr(0,value.find_first_of('='));
2109 if ((value.find_first_of('=')+1) <= value.length()) {
2110 value = value.substr(value.find_first_of('=')+1, value.length());
2117 catch(exception& e) {
2118 errorOut(e, "MothurOut", "splitAtEquals");
2123 /**************************************************************************************************/
2125 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
2127 for (int i = 0; i < Groups.size(); i++) {
2128 if (groupname == Groups[i]) { return true; }
2132 catch(exception& e) {
2133 errorOut(e, "MothurOut", "inUsersGroups");
2137 /**************************************************************************************************/
2138 //returns true if any of the strings in first vector are in second vector
2139 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
2142 for (int i = 0; i < groupnames.size(); i++) {
2143 if (inUsersGroups(groupnames[i], Groups)) { return true; }
2147 catch(exception& e) {
2148 errorOut(e, "MothurOut", "inUsersGroups");
2152 /***********************************************************************/
2153 //this function determines if the user has given us labels that are smaller than the given label.
2154 //if so then it returns true so that the calling function can run the previous valid distance.
2155 //it's a "smart" distance function. It also checks for invalid labels.
2156 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
2159 set<string>::iterator it;
2160 vector<float> orderFloat;
2161 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
2162 map<string, float>::iterator it2;
2164 bool smaller = false;
2166 //unique is the smallest line
2167 if (label == "unique") { return false; }
2169 if (convertTestFloat(label, labelFloat)) {
2170 convert(label, labelFloat);
2171 }else { //cant convert
2176 //go through users set and make them floats
2177 for(it = userLabels.begin(); it != userLabels.end();) {
2180 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
2182 orderFloat.push_back(temp);
2183 userMap[*it] = temp;
2185 }else if (*it == "unique") {
2186 orderFloat.push_back(-1.0);
2187 userMap["unique"] = -1.0;
2190 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
2191 userLabels.erase(it++);
2196 sort(orderFloat.begin(), orderFloat.end());
2198 /*************************************************/
2199 //is this label bigger than any of the users labels
2200 /*************************************************/
2202 //loop through order until you find a label greater than label
2203 for (int i = 0; i < orderFloat.size(); i++) {
2204 if (orderFloat[i] < labelFloat) {
2206 if (orderFloat[i] == -1) {
2207 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
2208 userLabels.erase("unique");
2211 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
2213 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
2214 if (it2->second == orderFloat[i]) {
2216 //remove small labels
2217 userLabels.erase(s);
2221 if (errorOff == "") {mothurOut( s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
2223 //since they are sorted once you find a bigger one stop looking
2230 catch(exception& e) {
2231 errorOut(e, "MothurOut", "anyLabelsToProcess");
2236 /**************************************************************************************************/
2237 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
2242 string line = getline(file);
2244 //before we added this check
2245 if (line[0] != '#') { good = false; }
2248 line = line.substr(1);
2250 vector<string> versionVector;
2251 splitAtChar(version, versionVector, '.');
2253 //check file version
2254 vector<string> linesVector;
2255 splitAtChar(line, linesVector, '.');
2257 if (versionVector.size() != linesVector.size()) { good = false; }
2259 for (int j = 0; j < versionVector.size(); j++) {
2261 convert(versionVector[j], num1);
2262 convert(linesVector[j], num2);
2264 //if mothurs version is newer than this files version, then we want to remake it
2265 if (num1 > num2) { good = false; break; }
2271 if (!good) { file.close(); }
2272 else { file.seekg(0); }
2276 catch(exception& e) {
2277 errorOut(e, "MothurOut", "checkReleaseVersion");
2281 /**************************************************************************************************/
2282 bool MothurOut::isContainingOnlyDigits(string input) {
2285 //are you a digit in ascii code
2286 for (int i = 0;i < input.length(); i++){
2287 if( input[i]>47 && input[i]<58){}
2288 else { return false; }
2293 catch(exception& e) {
2294 errorOut(e, "MothurOut", "isContainingOnlyDigits");
2298 /**************************************************************************************************/
2299 int MothurOut::removeConfidences(string& tax) {
2305 while (tax.find_first_of(';') != -1) {
2307 if (control_pressed) { return 0; }
2310 taxon = tax.substr(0,tax.find_first_of(';'));
2312 int pos = taxon.find_last_of('(');
2315 int pos2 = taxon.find_last_of(')');
2317 string confidenceScore = taxon.substr(pos+1, (pos2-(pos+1)));
2318 if (isNumeric1(confidenceScore)) {
2319 taxon = taxon.substr(0, pos); //rip off confidence
2325 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
2333 catch(exception& e) {
2334 errorOut(e, "MothurOut", "removeConfidences");
2338 /**************************************************************************************************/