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 set<string> MothurOut::getCurrentTypes() {
25 types.insert("fasta");
26 types.insert("accnos");
27 types.insert("column");
28 types.insert("design");
29 types.insert("group");
32 types.insert("oligos");
33 types.insert("order");
34 types.insert("ordergroup");
35 types.insert("phylip");
36 types.insert("qfile");
37 types.insert("relabund");
38 types.insert("sabund");
39 types.insert("rabund");
41 types.insert("shared");
42 types.insert("taxonomy");
46 types.insert("processors");
51 errorOut(e, "MothurOut", "getCurrentTypes");
55 /*********************************************************************************************/
56 void MothurOut::printCurrentFiles() {
60 if (accnosfile != "") { mothurOut("accnos=" + accnosfile); mothurOutEndLine(); }
61 if (columnfile != "") { mothurOut("column=" + columnfile); mothurOutEndLine(); }
62 if (designfile != "") { mothurOut("design=" + designfile); mothurOutEndLine(); }
63 if (fastafile != "") { mothurOut("fasta=" + fastafile); mothurOutEndLine(); }
64 if (groupfile != "") { mothurOut("group=" + groupfile); mothurOutEndLine(); }
65 if (listfile != "") { mothurOut("list=" + listfile); mothurOutEndLine(); }
66 if (namefile != "") { mothurOut("name=" + namefile); mothurOutEndLine(); }
67 if (oligosfile != "") { mothurOut("oligos=" + oligosfile); mothurOutEndLine(); }
68 if (orderfile != "") { mothurOut("order=" + orderfile); mothurOutEndLine(); }
69 if (ordergroupfile != "") { mothurOut("ordergroup=" + ordergroupfile); mothurOutEndLine(); }
70 if (phylipfile != "") { mothurOut("phylip=" + phylipfile); mothurOutEndLine(); }
71 if (qualfile != "") { mothurOut("qfile=" + qualfile); mothurOutEndLine(); }
72 if (rabundfile != "") { mothurOut("rabund=" + rabundfile); mothurOutEndLine(); }
73 if (relabundfile != "") { mothurOut("relabund=" + relabundfile); mothurOutEndLine(); }
74 if (sabundfile != "") { mothurOut("sabund=" + sabundfile); mothurOutEndLine(); }
75 if (sfffile != "") { mothurOut("sff=" + sfffile); mothurOutEndLine(); }
76 if (sharedfile != "") { mothurOut("shared=" + sharedfile); mothurOutEndLine(); }
77 if (taxonomyfile != "") { mothurOut("taxonomy=" + taxonomyfile); mothurOutEndLine(); }
78 if (treefile != "") { mothurOut("tree=" + treefile); mothurOutEndLine(); }
79 if (flowfile != "") { mothurOut("flow=" + flowfile); mothurOutEndLine(); }
80 if (biomfile != "") { mothurOut("biom=" + biomfile); mothurOutEndLine(); }
81 if (processors != "1") { mothurOut("processors=" + processors); mothurOutEndLine(); }
85 errorOut(e, "MothurOut", "printCurrentFiles");
89 /*********************************************************************************************/
90 bool MothurOut::hasCurrentFiles() {
92 bool hasCurrent = false;
94 if (accnosfile != "") { return true; }
95 if (columnfile != "") { return true; }
96 if (designfile != "") { return true; }
97 if (fastafile != "") { return true; }
98 if (groupfile != "") { return true; }
99 if (listfile != "") { return true; }
100 if (namefile != "") { return true; }
101 if (oligosfile != "") { return true; }
102 if (orderfile != "") { return true; }
103 if (ordergroupfile != "") { return true; }
104 if (phylipfile != "") { return true; }
105 if (qualfile != "") { return true; }
106 if (rabundfile != "") { return true; }
107 if (relabundfile != "") { return true; }
108 if (sabundfile != "") { return true; }
109 if (sfffile != "") { return true; }
110 if (sharedfile != "") { return true; }
111 if (taxonomyfile != "") { return true; }
112 if (treefile != "") { return true; }
113 if (flowfile != "") { return true; }
114 if (biomfile != "") { return true; }
115 if (processors != "1") { return true; }
120 catch(exception& e) {
121 errorOut(e, "MothurOut", "hasCurrentFiles");
126 /*********************************************************************************************/
127 void MothurOut::clearCurrentFiles() {
152 catch(exception& e) {
153 errorOut(e, "MothurOut", "clearCurrentFiles");
157 /***********************************************************************/
158 string MothurOut::findProgramPath(string programName){
161 string envPath = getenv("PATH");
164 //delimiting path char
166 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
172 //break apart path variable by ':'
174 splitAtChar(envPath, dirs, delim);
176 if (debug) { mothurOut("[DEBUG]: dir's in path: \n"); }
178 //get path related to mothur
179 for (int i = 0; i < dirs.size(); i++) {
181 if (debug) { mothurOut("[DEBUG]: " + dirs[i] + "\n"); }
183 //to lower so we can find it
184 string tempLower = "";
185 for (int j = 0; j < dirs[i].length(); j++) { tempLower += tolower(dirs[i][j]); }
187 //is this mothurs path?
188 if (tempLower.find(programName) != -1) { pPath = dirs[i]; break; }
191 if (debug) { mothurOut("[DEBUG]: programPath = " + pPath + "\n"); }
194 //add programName so it looks like what argv would look like
195 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
196 pPath += "/" + programName;
198 pPath += "\\" + programName;
201 //okay programName is not in the path, so the folder programName is in must be in the path
202 //lets find out which one
204 //get path related to the program
205 for (int i = 0; i < dirs.size(); i++) {
207 if (debug) { mothurOut("[DEBUG]: looking in " + dirs[i] + " for " + programName + " \n"); }
209 //is this the programs path?
211 string tempIn = dirs[i];
212 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
213 tempIn += "/" + programName;
215 tempIn += "\\" + programName;
217 openInputFile(tempIn, in, "");
219 //if this file exists
220 if (in) { in.close(); pPath = tempIn; if (debug) { mothurOut("[DEBUG]: found it, programPath = " + pPath + "\n"); } break; }
227 catch(exception& e) {
228 errorOut(e, "MothurOut", "findProgramPath");
232 /*********************************************************************************************/
233 void MothurOut::setFileName(string filename) {
235 logFileName = filename;
239 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
241 if (pid == 0) { //only one process should output to screen
244 openOutputFile(filename, out);
250 catch(exception& e) {
251 errorOut(e, "MothurOut", "setFileName");
255 /*********************************************************************************************/
256 void MothurOut::setDefaultPath(string pathname) {
259 //add / to name if needed
260 string lastChar = pathname.substr(pathname.length()-1);
261 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
262 if (lastChar != "/") { pathname += "/"; }
264 if (lastChar != "\\") { pathname += "\\"; }
267 defaultPath = pathname;
270 catch(exception& e) {
271 errorOut(e, "MothurOut", "setDefaultPath");
275 /*********************************************************************************************/
276 void MothurOut::setOutputDir(string pathname) {
278 outputDir = pathname;
280 catch(exception& e) {
281 errorOut(e, "MothurOut", "setOutputDir");
285 /*********************************************************************************************/
286 void MothurOut::closeLog() {
291 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
293 if (pid == 0) { //only one process should output to screen
302 catch(exception& e) {
303 errorOut(e, "MothurOut", "closeLog");
308 /*********************************************************************************************/
309 MothurOut::~MothurOut() {
314 catch(exception& e) {
315 errorOut(e, "MothurOut", "MothurOut");
319 /*********************************************************************************************/
320 void MothurOut::mothurOut(string output) {
325 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
327 if (pid == 0) { //only one process should output to screen
337 catch(exception& e) {
338 errorOut(e, "MothurOut", "MothurOut");
342 /*********************************************************************************************/
343 void MothurOut::mothurOutEndLine() {
347 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
349 if (pid == 0) { //only one process should output to screen
359 catch(exception& e) {
360 errorOut(e, "MothurOut", "MothurOutEndLine");
364 /*********************************************************************************************/
365 void MothurOut::mothurOut(string output, ofstream& outputFile) {
370 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
372 if (pid == 0) { //only one process should output to screen
377 outputFile << output;
385 catch(exception& e) {
386 errorOut(e, "MothurOut", "MothurOut");
390 /*********************************************************************************************/
391 void MothurOut::mothurOutEndLine(ofstream& outputFile) {
395 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
397 if (pid == 0) { //only one process should output to screen
408 catch(exception& e) {
409 errorOut(e, "MothurOut", "MothurOutEndLine");
413 /*********************************************************************************************/
414 void MothurOut::mothurOutJustToLog(string output) {
418 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
420 if (pid == 0) { //only one process should output to screen
429 catch(exception& e) {
430 errorOut(e, "MothurOut", "MothurOutJustToLog");
434 /*********************************************************************************************/
435 void MothurOut::errorOut(exception& e, string object, string function) {
437 //mem_usage(vm, rss);
439 mothurOut("[ERROR]: ");
440 mothurOut(toString(e.what()));
441 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.");
444 /*********************************************************************************************/
445 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
446 // process_mem_usage(double &, double &) - takes two doubles by reference,
447 // attempts to read the system-dependent data for a process' virtual memory
448 // size and resident set size, and return the results in KB.
450 // On failure, returns 0.0, 0.0
451 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
452 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
457 // 'file' stat seems to give the most reliable results
459 ifstream stat_stream("/proc/self/stat",ios_base::in);
461 // dummy vars for leading entries in stat that we don't care about
463 string pid, comm, state, ppid, pgrp, session, tty_nr;
464 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
465 string utime, stime, cutime, cstime, priority, nice;
466 string O, itrealvalue, starttime;
468 // the two fields we want
473 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
474 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
475 >> utime >> stime >> cutime >> cstime >> priority >> nice
476 >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
478 long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
479 vm_usage = vsize / 1024.0;
480 resident_set = rss * page_size_kb;
482 mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
486 /* //windows memory usage
487 // Get the list of process identifiers.
488 DWORD aProcesses[1024], cbNeeded, cProcesses;
490 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
492 // Calculate how many process identifiers were returned.
493 cProcesses = cbNeeded / sizeof(DWORD);
495 // Print the memory usage for each process
496 for (int i = 0; i < cProcesses; i++ ) {
497 DWORD processID = aProcesses[i];
499 PROCESS_MEMORY_COUNTERS pmc;
501 HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
503 // Print the process identifier.
504 printf( "\nProcess ID: %u\n", processID);
506 if (NULL != hProcess) {
508 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
509 printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
510 printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
511 printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
512 printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
513 printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
514 printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
515 printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
516 printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
517 printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
519 CloseHandle(hProcess);
529 /***********************************************************************/
530 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
532 fileName = getFullPathName(fileName);
534 fileHandle.open(fileName.c_str(), ios::app);
536 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
543 catch(exception& e) {
544 errorOut(e, "MothurOut", "openOutputFileAppend");
548 /***********************************************************************/
549 void MothurOut::gobble(istream& f){
553 while(isspace(d=f.get())) { ;}
554 if(!f.eof()) { f.putback(d); }
556 catch(exception& e) {
557 errorOut(e, "MothurOut", "gobble");
561 /***********************************************************************/
562 void MothurOut::gobble(istringstream& f){
565 while(isspace(d=f.get())) {;}
566 if(!f.eof()) { f.putback(d); }
568 catch(exception& e) {
569 errorOut(e, "MothurOut", "gobble");
574 /***********************************************************************/
576 string MothurOut::getline(istringstream& fileHandle) {
581 while (!fileHandle.eof()) {
583 char c = fileHandle.get();
585 //are you at the end of the line
586 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
593 catch(exception& e) {
594 errorOut(e, "MothurOut", "getline");
598 /***********************************************************************/
600 string MothurOut::getline(ifstream& fileHandle) {
607 char c = fileHandle.get();
609 //are you at the end of the line
610 if ((c == '\n') || (c == '\r') || (c == '\f') || (c == EOF)){ break; }
617 catch(exception& e) {
618 errorOut(e, "MothurOut", "getline");
622 /***********************************************************************/
624 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
625 #ifdef USE_COMPRESSION
626 inline bool endsWith(string s, const char * suffix){
627 size_t suffixLength = strlen(suffix);
628 return s.size() >= suffixLength && s.substr(s.size() - suffixLength, suffixLength).compare(suffix) == 0;
633 string MothurOut::getRootName(string longName){
636 string rootName = longName;
638 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
639 #ifdef USE_COMPRESSION
640 if (endsWith(rootName, ".gz") || endsWith(rootName, ".bz2")) {
641 int pos = rootName.find_last_of('.');
642 rootName = rootName.substr(0, pos);
643 cerr << "shortening " << longName << " to " << rootName << "\n";
647 if(rootName.find_last_of(".") != rootName.npos){
648 int pos = rootName.find_last_of('.')+1;
649 rootName = rootName.substr(0, pos);
654 catch(exception& e) {
655 errorOut(e, "MothurOut", "getRootName");
659 /***********************************************************************/
661 string MothurOut::getSimpleName(string longName){
663 string simpleName = longName;
666 found=longName.find_last_of("/\\");
668 if(found != longName.npos){
669 simpleName = longName.substr(found+1);
674 catch(exception& e) {
675 errorOut(e, "MothurOut", "getSimpleName");
680 /***********************************************************************/
682 int MothurOut::getRandomIndex(int highest){
685 int random = (int) ((float)(highest+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
689 catch(exception& e) {
690 errorOut(e, "MothurOut", "getRandomIndex");
695 /**********************************************************************/
697 string MothurOut::getPathName(string longName){
699 string rootPathName = longName;
701 if(longName.find_last_of("/\\") != longName.npos){
702 int pos = longName.find_last_of("/\\")+1;
703 rootPathName = longName.substr(0, pos);
708 catch(exception& e) {
709 errorOut(e, "MothurOut", "getPathName");
714 /***********************************************************************/
716 bool MothurOut::dirCheck(string& dirName){
722 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
727 //add / to name if needed
728 string lastChar = dirName.substr(dirName.length()-1);
729 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
730 if (lastChar != "/") { dirName += "/"; }
732 if (lastChar != "\\") { dirName += "\\"; }
735 //test to make sure directory exists
736 dirName = getFullPathName(dirName);
737 string outTemp = dirName + tag + "temp";
739 out.open(outTemp.c_str(), ios::trunc);
741 mothurOut(dirName + " directory does not exist or is not writable."); mothurOutEndLine();
744 mothurRemove(outTemp);
750 catch(exception& e) {
751 errorOut(e, "MothurOut", "dirCheck");
756 /***********************************************************************/
758 string MothurOut::hasPath(string longName){
763 found=longName.find_last_of("~/\\");
765 if(found != longName.npos){
766 path = longName.substr(0, found+1);
771 catch(exception& e) {
772 errorOut(e, "MothurOut", "hasPath");
777 /***********************************************************************/
779 string MothurOut::getExtension(string longName){
781 string extension = "";
783 if(longName.find_last_of('.') != longName.npos){
784 int pos = longName.find_last_of('.');
785 extension = longName.substr(pos, longName.length());
790 catch(exception& e) {
791 errorOut(e, "MothurOut", "getExtension");
795 /***********************************************************************/
796 bool MothurOut::isBlank(string fileName){
799 fileName = getFullPathName(fileName);
802 fileHandle.open(fileName.c_str());
804 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
807 //check for blank file
809 if (fileHandle.eof()) { fileHandle.close(); return true; }
814 catch(exception& e) {
815 errorOut(e, "MothurOut", "isBlank");
819 /***********************************************************************/
821 string MothurOut::getFullPathName(string fileName){
824 string path = hasPath(fileName);
828 if (path == "") { return fileName; } //its a simple name
829 else { //we need to complete the pathname
830 // ex. ../../../filename
831 // cwd = /user/work/desktop
834 //get current working directory
835 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
837 if (path.find("~") != -1) { //go to home directory
840 char *homepath = NULL;
841 homepath = getenv ("HOME");
842 if ( homepath != NULL) { homeDir = homepath; }
843 else { homeDir = ""; }
845 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
848 if (path.rfind("./") == string::npos) { return fileName; } //already complete name
849 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
851 //char* cwdpath = new char[1024];
853 //cwdpath=getcwd(cwdpath,size);
856 char *cwdpath = NULL;
857 cwdpath = getcwd(NULL, 0); // or _getcwd
858 if ( cwdpath != NULL) { cwd = cwdpath; }
864 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
866 //break apart the current working directory
868 while (simpleCWD.find_first_of('/') != string::npos) {
869 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
870 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
873 //get last one // ex. ../../../filename = /user/work/desktop/filename
874 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
877 int index = dirs.size()-1;
879 while((pos = path.rfind("./")) != string::npos) { //while you don't have a complete path
880 if (pos == 0) { break; //you are at the end
881 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
882 path = path.substr(0, pos-1);
884 if (index == 0) { break; }
885 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
886 path = path.substr(0, pos);
887 }else if (pos == 1) { break; //you are at the end
888 }else { mothurOut("cannot resolve path for " + fileName + "\n"); return fileName; }
891 for (int i = index; i >= 0; i--) {
892 newFileName = dirs[i] + "/" + newFileName;
895 newFileName = "/" + newFileName;
899 if (path.find("~") != string::npos) { //go to home directory
900 string homeDir = getenv ("HOMEPATH");
901 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
904 if (path.rfind(".\\") == string::npos) { return fileName; } //already complete name
905 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
907 char *cwdpath = NULL;
908 cwdpath = getcwd(NULL, 0); // or _getcwd
909 if ( cwdpath != NULL) { cwd = cwdpath; }
912 //break apart the current working directory
914 while (cwd.find_first_of('\\') != -1) {
915 string dir = cwd.substr(0,cwd.find_first_of('\\'));
916 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
921 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
923 int index = dirs.size()-1;
925 while((pos = path.rfind(".\\")) != string::npos) { //while you don't have a complete path
926 if (pos == 0) { break; //you are at the end
927 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
928 path = path.substr(0, pos-1);
930 if (index == 0) { break; }
931 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
932 path = path.substr(0, pos);
933 }else if (pos == 1) { break; //you are at the end
934 }else { mothurOut("cannot resolve path for " + fileName + "\n"); return fileName; }
937 for (int i = index; i >= 0; i--) {
938 newFileName = dirs[i] + "\\" + newFileName;
947 catch(exception& e) {
948 errorOut(e, "MothurOut", "getFullPathName");
952 /***********************************************************************/
954 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
957 string completeFileName = getFullPathName(fileName);
958 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
959 #ifdef USE_COMPRESSION
960 // check for gzipped or bzipped file
961 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
962 string tempName = string(tmpnam(0));
963 mkfifo(tempName.c_str(), 0666);
964 int fork_result = fork();
965 if (fork_result < 0) {
966 cerr << "Error forking.\n";
968 } else if (fork_result == 0) {
969 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
970 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
971 system(command.c_str());
972 cerr << "Done decompressing " << completeFileName << "\n";
973 mothurRemove(tempName);
976 cerr << "waiting on child process " << fork_result << "\n";
977 completeFileName = tempName;
982 fileHandle.open(completeFileName.c_str());
984 //mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
987 //check for blank file
992 catch(exception& e) {
993 errorOut(e, "MothurOut", "openInputFile - no Error");
997 /***********************************************************************/
999 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
1002 //get full path name
1003 string completeFileName = getFullPathName(fileName);
1004 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1005 #ifdef USE_COMPRESSION
1006 // check for gzipped or bzipped file
1007 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
1008 string tempName = string(tmpnam(0));
1009 mkfifo(tempName.c_str(), 0666);
1010 int fork_result = fork();
1011 if (fork_result < 0) {
1012 cerr << "Error forking.\n";
1014 } else if (fork_result == 0) {
1015 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
1016 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
1017 system(command.c_str());
1018 cerr << "Done decompressing " << completeFileName << "\n";
1019 mothurRemove(tempName);
1022 cerr << "waiting on child process " << fork_result << "\n";
1023 completeFileName = tempName;
1029 fileHandle.open(completeFileName.c_str());
1031 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1035 //check for blank file
1037 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
1042 catch(exception& e) {
1043 errorOut(e, "MothurOut", "openInputFile");
1047 /***********************************************************************/
1049 int MothurOut::renameFile(string oldName, string newName){
1052 int exist = openInputFile(newName, inTest, "");
1055 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1056 if (exist == 0) { //you could open it so you want to delete it
1057 string command = "rm " + newName;
1058 system(command.c_str());
1061 string command = "mv " + oldName + " " + newName;
1062 system(command.c_str());
1064 mothurRemove(newName);
1065 int renameOk = rename(oldName.c_str(), newName.c_str());
1070 catch(exception& e) {
1071 errorOut(e, "MothurOut", "renameFile");
1076 /***********************************************************************/
1078 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
1081 string completeFileName = getFullPathName(fileName);
1082 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1083 #ifdef USE_COMPRESSION
1084 // check for gzipped file
1085 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
1086 string tempName = string(tmpnam(0));
1087 mkfifo(tempName.c_str(), 0666);
1088 cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
1089 int fork_result = fork();
1090 if (fork_result < 0) {
1091 cerr << "Error forking.\n";
1093 } else if (fork_result == 0) {
1094 string command = string(endsWith(completeFileName, ".gz") ? "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
1095 system(command.c_str());
1098 completeFileName = tempName;
1103 fileHandle.open(completeFileName.c_str(), ios::trunc);
1105 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1112 catch(exception& e) {
1113 errorOut(e, "MothurOut", "openOutputFile");
1119 /**************************************************************************************************/
1120 int MothurOut::appendFiles(string temp, string filename) {
1125 //open output file in append mode
1126 openOutputFileAppend(filename, output);
1127 int ableToOpen = openInputFile(temp, input, "no error");
1128 //int ableToOpen = openInputFile(temp, input);
1131 if (ableToOpen == 0) { //you opened it
1134 while (!input.eof()) {
1135 input.read(buffer, 4096);
1136 output.write(buffer, input.gcount());
1137 //count number of lines
1138 for (int i = 0; i < input.gcount(); i++) { if (buffer[i] == '\n') {numLines++;} }
1147 catch(exception& e) {
1148 errorOut(e, "MothurOut", "appendFiles");
1153 /**************************************************************************************************/
1154 string MothurOut::sortFile(string distFile, string outputDir){
1157 //if (outputDir == "") { outputDir += hasPath(distFile); }
1158 string outfile = getRootName(distFile) + "sorted.dist";
1161 //if you can, use the unix sort since its been optimized for years
1162 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1163 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
1164 system(command.c_str());
1165 #else //you are stuck with my best attempt...
1166 //windows sort does not have a way to specify a column, only a character in the line
1167 //since we cannot assume that the distance will always be at the the same character location on each line
1168 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
1170 //read in file line by file and put distance first
1171 string tempDistFile = distFile + ".temp";
1174 openInputFile(distFile, input);
1175 openOutputFile(tempDistFile, output);
1177 string firstName, secondName;
1180 input >> firstName >> secondName >> dist;
1181 output << dist << '\t' << firstName << '\t' << secondName << endl;
1188 //sort using windows sort
1189 string tempOutfile = outfile + ".temp";
1190 string command = "sort " + tempDistFile + " /O " + tempOutfile;
1191 system(command.c_str());
1193 //read in sorted file and put distance at end again
1195 openInputFile(tempOutfile, input2);
1196 openOutputFile(outfile, output);
1199 input2 >> dist >> firstName >> secondName;
1200 output << firstName << '\t' << secondName << '\t' << dist << endl;
1207 mothurRemove(tempDistFile);
1208 mothurRemove(tempOutfile);
1213 catch(exception& e) {
1214 errorOut(e, "MothurOut", "sortFile");
1218 /**************************************************************************************************/
1219 vector<unsigned long long> MothurOut::setFilePosFasta(string filename, int& num) {
1221 vector<unsigned long long> positions;
1223 //openInputFile(filename, inFASTA);
1224 inFASTA.open(filename.c_str(), ios::binary);
1227 unsigned long long count = 0;
1228 while(!inFASTA.eof()){
1229 //input = getline(inFASTA);
1230 //cout << input << '\t' << inFASTA.tellg() << endl;
1231 //if (input.length() != 0) {
1232 // if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); cout << (pos - input.length() - 1) << endl; }
1234 //gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
1235 char c = inFASTA.get(); count++;
1237 positions.push_back(count-1);
1238 //cout << count << endl;
1243 num = positions.size();
1248 //get num bytes in file
1249 pFile = fopen (filename.c_str(),"rb");
1250 if (pFile==NULL) perror ("Error opening file");
1252 fseek (pFile, 0, SEEK_END);
1257 unsigned long long size = positions[(positions.size()-1)];
1259 openInputFile(filename, in);
1264 if(in.eof()) { break; }
1269 positions.push_back(size);
1274 catch(exception& e) {
1275 errorOut(e, "MothurOut", "setFilePosFasta");
1279 /**************************************************************************************************/
1280 vector<unsigned long long> MothurOut::setFilePosEachLine(string filename, int& num) {
1282 filename = getFullPathName(filename);
1284 vector<unsigned long long> positions;
1286 //openInputFile(filename, in);
1287 in.open(filename.c_str(), ios::binary);
1290 unsigned long long count = 0;
1291 positions.push_back(0);
1294 //unsigned long long lastpos = in.tellg();
1295 //input = getline(in);
1296 //if (input.length() != 0) {
1297 //unsigned long long pos = in.tellg();
1298 //if (pos != -1) { positions.push_back(pos - input.length() - 1); }
1299 //else { positions.push_back(lastpos); }
1301 //gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
1304 //getline counting reads
1305 char d = in.get(); count++;
1306 while ((d != '\n') && (d != '\r') && (d != '\f') && (d != in.eof())) {
1307 //get next character
1313 d=in.get(); count++;
1314 while(isspace(d) && (d != in.eof())) { d=in.get(); count++;}
1316 positions.push_back(count-1);
1317 //cout << count-1 << endl;
1321 num = positions.size()-1;
1324 unsigned long long size;
1326 //get num bytes in file
1327 pFile = fopen (filename.c_str(),"rb");
1328 if (pFile==NULL) perror ("Error opening file");
1330 fseek (pFile, 0, SEEK_END);
1335 positions[(positions.size()-1)] = size;
1339 catch(exception& e) {
1340 errorOut(e, "MothurOut", "setFilePosEachLine");
1344 /**************************************************************************************************/
1346 vector<unsigned long long> MothurOut::divideFile(string filename, int& proc) {
1348 vector<unsigned long long> filePos;
1349 filePos.push_back(0);
1352 unsigned long long size;
1354 filename = getFullPathName(filename);
1356 //get num bytes in file
1357 pFile = fopen (filename.c_str(),"rb");
1358 if (pFile==NULL) perror ("Error opening file");
1360 fseek (pFile, 0, SEEK_END);
1365 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1367 //estimate file breaks
1368 unsigned long long chunkSize = 0;
1369 chunkSize = size / proc;
1371 //file to small to divide by processors
1372 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1374 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1375 for (int i = 0; i < proc; i++) {
1376 unsigned long long spot = (i+1) * chunkSize;
1379 openInputFile(filename, in);
1383 unsigned long long newSpot = spot;
1387 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1388 else if (int(c) == -1) { break; }
1392 //there was not another sequence before the end of the file
1393 unsigned long long sanityPos = in.tellg();
1395 if (sanityPos == -1) { break; }
1396 else { filePos.push_back(newSpot); }
1402 filePos.push_back(size);
1404 //sanity check filePos
1405 for (int i = 0; i < (filePos.size()-1); i++) {
1406 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1409 proc = (filePos.size() - 1);
1411 mothurOut("[ERROR]: Windows version should not be calling the divideFile function."); mothurOutEndLine();
1413 filePos.push_back(size);
1417 catch(exception& e) {
1418 errorOut(e, "MothurOut", "divideFile");
1422 /**************************************************************************************************/
1423 int MothurOut::divideFile(string filename, int& proc, vector<string>& files) {
1426 vector<unsigned long long> filePos = divideFile(filename, proc);
1428 for (int i = 0; i < (filePos.size()-1); i++) {
1432 openInputFile(filename, in);
1433 in.seekg(filePos[i]);
1434 unsigned long long size = filePos[(i+1)] - filePos[i];
1435 char* chunk = new char[size];
1436 in.read(chunk, size);
1440 string fileChunkName = filename + "." + toString(i) + ".tmp";
1442 openOutputFile(fileChunkName, out);
1444 out << chunk << endl;
1449 files.push_back(fileChunkName);
1454 catch(exception& e) {
1455 errorOut(e, "MothurOut", "divideFile");
1459 /***********************************************************************/
1461 bool MothurOut::isTrue(string f){
1464 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1466 if ((f == "TRUE") || (f == "T")) { return true; }
1467 else { return false; }
1469 catch(exception& e) {
1470 errorOut(e, "MothurOut", "isTrue");
1475 /***********************************************************************/
1477 float MothurOut::roundDist(float dist, int precision){
1479 return int(dist * precision + 0.5)/float(precision);
1481 catch(exception& e) {
1482 errorOut(e, "MothurOut", "roundDist");
1486 /***********************************************************************/
1488 float MothurOut::ceilDist(float dist, int precision){
1490 return int(ceil(dist * precision))/float(precision);
1492 catch(exception& e) {
1493 errorOut(e, "MothurOut", "ceilDist");
1497 /***********************************************************************/
1499 vector<string> MothurOut::splitWhiteSpace(string& rest, char buffer[], int size){
1501 vector<string> pieces;
1503 for (int i = 0; i < size; i++) {
1504 if (!isspace(buffer[i])) { rest += buffer[i]; }
1506 pieces.push_back(rest); rest = "";
1507 while (i < size) { //gobble white space
1508 if (isspace(buffer[i])) { i++; }
1509 else { rest = buffer[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1516 catch(exception& e) {
1517 errorOut(e, "MothurOut", "splitWhiteSpace");
1521 /***********************************************************************/
1522 vector<string> MothurOut::splitWhiteSpace(string input){
1524 vector<string> pieces;
1527 for (int i = 0; i < input.length(); i++) {
1528 if (!isspace(input[i])) { rest += input[i]; }
1530 pieces.push_back(rest); rest = "";
1531 while (i < input.length()) { //gobble white space
1532 if (isspace(input[i])) { i++; }
1533 else { rest = input[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1538 if (rest != "") { pieces.push_back(rest); }
1542 catch(exception& e) {
1543 errorOut(e, "MothurOut", "splitWhiteSpace");
1547 //**********************************************************************************************************************
1548 int MothurOut::readTax(string namefile, map<string, string>& taxMap) {
1553 openInputFile(namefile, in);
1557 bool pairDone = false;
1558 bool columnOne = true;
1559 string firstCol, secondCol;
1562 if (control_pressed) { break; }
1564 in.read(buffer, 4096);
1565 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1567 for (int i = 0; i < pieces.size(); i++) {
1568 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1569 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1572 //are there confidence scores, if so remove them
1573 if (secondCol.find_first_of('(') != -1) { removeConfidences(secondCol); }
1574 taxMap[firstCol] = secondCol;
1575 if (debug) { mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n"); }
1582 return taxMap.size();
1585 catch(exception& e) {
1586 errorOut(e, "MothurOut", "readTax");
1590 /**********************************************************************************************************************/
1591 int MothurOut::readNames(string namefile, map<string, string>& nameMap, bool redund) {
1596 openInputFile(namefile, in);
1600 bool pairDone = false;
1601 bool columnOne = true;
1602 string firstCol, secondCol;
1605 if (control_pressed) { break; }
1607 in.read(buffer, 4096);
1608 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1610 for (int i = 0; i < pieces.size(); i++) {
1611 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1612 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1615 //parse names into vector
1616 vector<string> theseNames;
1617 splitAtComma(secondCol, theseNames);
1618 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1625 return nameMap.size();
1628 catch(exception& e) {
1629 errorOut(e, "MothurOut", "readNames");
1633 /**********************************************************************************************************************/
1634 int MothurOut::readNames(string namefile, map<string, string>& nameMap, map<string, int>& nameCount) {
1636 nameMap.clear(); nameCount.clear();
1639 openInputFile(namefile, in);
1643 bool pairDone = false;
1644 bool columnOne = true;
1645 string firstCol, secondCol;
1648 if (control_pressed) { break; }
1650 in.read(buffer, 4096);
1651 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1653 for (int i = 0; i < pieces.size(); i++) {
1654 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1655 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1658 //parse names into vector
1659 vector<string> theseNames;
1660 splitAtComma(secondCol, theseNames);
1661 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1662 nameCount[firstCol] = theseNames.size();
1669 return nameMap.size();
1672 catch(exception& e) {
1673 errorOut(e, "MothurOut", "readNames");
1677 /**********************************************************************************************************************/
1678 int MothurOut::readNames(string namefile, map<string, string>& nameMap) {
1683 openInputFile(namefile, in);
1687 bool pairDone = false;
1688 bool columnOne = true;
1689 string firstCol, secondCol;
1692 if (control_pressed) { break; }
1694 in.read(buffer, 4096);
1695 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1697 for (int i = 0; i < pieces.size(); i++) {
1698 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1699 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1701 if (pairDone) { nameMap[firstCol] = secondCol; pairDone = false; }
1706 return nameMap.size();
1709 catch(exception& e) {
1710 errorOut(e, "MothurOut", "readNames");
1714 /**********************************************************************************************************************/
1715 int MothurOut::readNames(string namefile, map<string, vector<string> >& nameMap) {
1720 openInputFile(namefile, in);
1724 bool pairDone = false;
1725 bool columnOne = true;
1726 string firstCol, secondCol;
1729 if (control_pressed) { break; }
1731 in.read(buffer, 4096);
1732 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1734 for (int i = 0; i < pieces.size(); i++) {
1735 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1736 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1739 vector<string> temp;
1740 splitAtComma(secondCol, temp);
1741 nameMap[firstCol] = temp;
1748 return nameMap.size();
1750 catch(exception& e) {
1751 errorOut(e, "MothurOut", "readNames");
1755 /**********************************************************************************************************************/
1756 map<string, int> MothurOut::readNames(string namefile) {
1759 map<string, int> nameMap;
1763 openInputFile(namefile, in);
1767 bool pairDone = false;
1768 bool columnOne = true;
1769 string firstCol, secondCol;
1772 if (control_pressed) { break; }
1774 in.read(buffer, 4096);
1775 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1777 for (int i = 0; i < pieces.size(); i++) {
1778 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1779 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1782 int num = getNumNames(secondCol);
1783 nameMap[firstCol] = num;
1793 catch(exception& e) {
1794 errorOut(e, "MothurOut", "readNames");
1798 /**********************************************************************************************************************/
1799 int MothurOut::readNames(string namefile, vector<seqPriorityNode>& nameVector, map<string, string>& fastamap) {
1805 openInputFile(namefile, in);
1809 bool pairDone = false;
1810 bool columnOne = true;
1811 string firstCol, secondCol;
1814 if (control_pressed) { break; }
1816 in.read(buffer, 4096);
1817 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1819 for (int i = 0; i < pieces.size(); i++) {
1820 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1821 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1824 int num = getNumNames(secondCol);
1826 map<string, string>::iterator it = fastamap.find(firstCol);
1827 if (it == fastamap.end()) {
1829 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
1831 seqPriorityNode temp(num, it->second, firstCol);
1832 nameVector.push_back(temp);
1843 catch(exception& e) {
1844 errorOut(e, "MothurOut", "readNames");
1848 //**********************************************************************************************************************
1849 set<string> MothurOut::readAccnos(string accnosfile){
1853 openInputFile(accnosfile, in);
1860 if (control_pressed) { break; }
1862 in.read(buffer, 4096);
1863 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1865 for (int i = 0; i < pieces.size(); i++) { names.insert(pieces[i]); }
1871 catch(exception& e) {
1872 errorOut(e, "MothurOut", "readAccnos");
1876 //**********************************************************************************************************************
1877 int MothurOut::readAccnos(string accnosfile, vector<string>& names){
1881 openInputFile(accnosfile, in);
1888 if (control_pressed) { break; }
1890 in.read(buffer, 4096);
1891 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1893 for (int i = 0; i < pieces.size(); i++) { names.push_back(pieces[i]); }
1899 catch(exception& e) {
1900 errorOut(e, "MothurOut", "readAccnos");
1904 /***********************************************************************/
1906 int MothurOut::getNumNames(string names){
1912 for(int i=0;i<names.size();i++){
1913 if(names[i] == ','){
1921 catch(exception& e) {
1922 errorOut(e, "MothurOut", "getNumNames");
1926 /***********************************************************************/
1928 int MothurOut::getNumChar(string line, char c){
1933 for(int i=0;i<line.size();i++){
1942 catch(exception& e) {
1943 errorOut(e, "MothurOut", "getNumChar");
1947 /***********************************************************************/
1948 int MothurOut::mothurRemove(string filename){
1950 filename = getFullPathName(filename);
1951 int error = remove(filename.c_str());
1953 // if (errno != ENOENT) { //ENOENT == file does not exist
1954 // string message = "Error deleting file " + filename;
1955 // perror(message.c_str());
1960 catch(exception& e) {
1961 errorOut(e, "MothurOut", "mothurRemove");
1965 /***********************************************************************/
1966 bool MothurOut::mothurConvert(string item, int& num){
1970 if (isNumeric1(item)) {
1975 mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
1976 commandInputsConvertError = true;
1981 catch(exception& e) {
1982 errorOut(e, "MothurOut", "mothurConvert");
1986 /***********************************************************************/
1987 bool MothurOut::isNumeric1(string stringToCheck){
1989 bool numeric = false;
1991 if(stringToCheck.find_first_not_of("0123456789.-") == string::npos) { numeric = true; }
1995 catch(exception& e) {
1996 errorOut(e, "MothurOut", "isNumeric1");
2001 /***********************************************************************/
2002 bool MothurOut::mothurConvert(string item, float& num){
2006 if (isNumeric1(item)) {
2011 mothurOut("[ERROR]: cannot convert " + item + " to a float."); mothurOutEndLine();
2012 commandInputsConvertError = true;
2017 catch(exception& e) {
2018 errorOut(e, "MothurOut", "mothurConvert");
2022 /***********************************************************************/
2023 bool MothurOut::mothurConvert(string item, double& num){
2027 if (isNumeric1(item)) {
2032 mothurOut("[ERROR]: cannot convert " + item + " to a double."); mothurOutEndLine();
2033 commandInputsConvertError = true;
2038 catch(exception& e) {
2039 errorOut(e, "MothurOut", "mothurConvert");
2043 /**************************************************************************************************/
2045 vector<vector<double> > MothurOut::binomial(int maxOrder){
2047 vector<vector<double> > binomial(maxOrder+1);
2049 for(int i=0;i<=maxOrder;i++){
2050 binomial[i].resize(maxOrder+1);
2059 for(int i=2;i<=maxOrder;i++){
2063 for(int i=2;i<=maxOrder;i++){
2064 for(int j=1;j<=maxOrder;j++){
2065 if(i==j){ binomial[i][j]=1; }
2066 if(j>i) { binomial[i][j]=0; }
2067 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
2074 catch(exception& e) {
2075 errorOut(e, "MothurOut", "binomial");
2079 /**************************************************************************************************/
2080 unsigned int MothurOut::fromBase36(string base36){
2082 unsigned int num = 0;
2084 map<char, int> converts;
2149 while (i < base36.length()) {
2151 num = 36 * num + converts[c];
2158 catch(exception& e) {
2159 errorOut(e, "MothurOut", "fromBase36");
2163 /***********************************************************************/
2165 int MothurOut::factorial(int num){
2169 for (int i = 1; i <= num; i++) {
2175 catch(exception& e) {
2176 errorOut(e, "MothurOut", "factorial");
2180 /***********************************************************************/
2182 int MothurOut::getNumSeqs(ifstream& file){
2184 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
2188 catch(exception& e) {
2189 errorOut(e, "MothurOut", "getNumSeqs");
2193 /***********************************************************************/
2194 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
2199 input = getline(file);
2200 if (input.length() != 0) {
2201 if(input[0] == '>'){ numSeqs++; }
2205 catch(exception& e) {
2206 errorOut(e, "MothurOut", "getNumSeqs");
2210 /***********************************************************************/
2212 //This function parses the estimator options and puts them in a vector
2213 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
2215 string individual = "";
2216 int estimLength = estim.size();
2217 for(int i=0;i<estimLength;i++){
2218 if(estim[i] == symbol){
2219 container.push_back(individual);
2223 individual += estim[i];
2226 container.push_back(individual);
2229 catch(exception& e) {
2230 errorOut(e, "MothurOut", "splitAtChar");
2235 /***********************************************************************/
2237 //This function parses the estimator options and puts them in a vector
2238 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
2240 string individual = "";
2241 int estimLength = estim.size();
2242 for(int i=0;i<estimLength;i++){
2243 if(estim[i] == '-'){
2244 container.push_back(individual);
2248 individual += estim[i];
2251 container.push_back(individual);
2254 /* string individual;
2256 while (estim.find_first_of('-') != -1) {
2257 individual = estim.substr(0,estim.find_first_of('-'));
2258 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
2259 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
2260 container.push_back(individual);
2264 container.push_back(estim); */
2266 catch(exception& e) {
2267 errorOut(e, "MothurOut", "splitAtDash");
2272 /***********************************************************************/
2273 //This function parses the label options and puts them in a set
2274 void MothurOut::splitAtDash(string& estim, set<string>& container) {
2276 string individual = "";
2277 int estimLength = estim.size();
2278 for(int i=0;i<estimLength;i++){
2279 if(estim[i] == '-'){
2280 container.insert(individual);
2284 individual += estim[i];
2287 container.insert(individual);
2289 // string individual;
2291 // while (estim.find_first_of('-') != -1) {
2292 // individual = estim.substr(0,estim.find_first_of('-'));
2293 // if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
2294 // estim = estim.substr(estim.find_first_of('-')+1, estim.length());
2295 // container.insert(individual);
2299 // container.insert(estim);
2302 catch(exception& e) {
2303 errorOut(e, "MothurOut", "splitAtDash");
2307 /***********************************************************************/
2308 //This function parses the line options and puts them in a set
2309 void MothurOut::splitAtDash(string& estim, set<int>& container) {
2314 while (estim.find_first_of('-') != -1) {
2315 individual = estim.substr(0,estim.find_first_of('-'));
2316 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
2317 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
2318 convert(individual, lineNum); //convert the string to int
2319 container.insert(lineNum);
2323 convert(estim, lineNum); //convert the string to int
2324 container.insert(lineNum);
2326 catch(exception& e) {
2327 errorOut(e, "MothurOut", "splitAtDash");
2331 /***********************************************************************/
2332 string MothurOut::makeList(vector<string>& names) {
2336 if (names.size() == 0) { return list; }
2338 for (int i = 0; i < names.size()-1; i++) { list += names[i] + ","; }
2341 list += names[names.size()-1];
2345 catch(exception& e) {
2346 errorOut(e, "MothurOut", "makeList");
2351 /***********************************************************************/
2352 //This function parses the a string and puts peices in a vector
2353 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
2355 string individual = "";
2356 int estimLength = estim.size();
2357 for(int i=0;i<estimLength;i++){
2358 if(estim[i] == ','){
2359 container.push_back(individual);
2363 individual += estim[i];
2366 container.push_back(individual);
2371 // string individual;
2373 // while (estim.find_first_of(',') != -1) {
2374 // individual = estim.substr(0,estim.find_first_of(','));
2375 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
2376 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
2377 // container.push_back(individual);
2381 // container.push_back(estim);
2383 catch(exception& e) {
2384 errorOut(e, "MothurOut", "splitAtComma");
2388 /***********************************************************************/
2389 //This function splits up the various option parameters
2390 void MothurOut::splitAtChar(string& prefix, string& suffix, char c){
2392 prefix = suffix.substr(0,suffix.find_first_of(c));
2393 if ((suffix.find_first_of(c)+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2394 suffix = suffix.substr(suffix.find_first_of(c)+1, suffix.length());
2396 while(suffix.at(0) == ' ')
2397 suffix = suffix.substr(1, suffix.length());
2401 catch(exception& e) {
2402 errorOut(e, "MothurOut", "splitAtComma");
2407 /***********************************************************************/
2409 //This function splits up the various option parameters
2410 void MothurOut::splitAtComma(string& prefix, string& suffix){
2412 prefix = suffix.substr(0,suffix.find_first_of(','));
2413 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2414 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
2416 while(suffix.at(0) == ' ')
2417 suffix = suffix.substr(1, suffix.length());
2421 catch(exception& e) {
2422 errorOut(e, "MothurOut", "splitAtComma");
2426 /***********************************************************************/
2428 //This function separates the key value from the option value i.e. dist=96_...
2429 void MothurOut::splitAtEquals(string& key, string& value){
2431 if(value.find_first_of('=') != -1){
2432 key = value.substr(0,value.find_first_of('='));
2433 if ((value.find_first_of('=')+1) <= value.length()) {
2434 value = value.substr(value.find_first_of('=')+1, value.length());
2441 catch(exception& e) {
2442 errorOut(e, "MothurOut", "splitAtEquals");
2447 /**************************************************************************************************/
2449 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
2451 for (int i = 0; i < Groups.size(); i++) {
2452 if (groupname == Groups[i]) { return true; }
2456 catch(exception& e) {
2457 errorOut(e, "MothurOut", "inUsersGroups");
2461 /**************************************************************************************************/
2462 //returns true if any of the strings in first vector are in second vector
2463 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
2466 for (int i = 0; i < groupnames.size(); i++) {
2467 if (inUsersGroups(groupnames[i], Groups)) { return true; }
2471 catch(exception& e) {
2472 errorOut(e, "MothurOut", "inUsersGroups");
2476 /***********************************************************************/
2477 //this function determines if the user has given us labels that are smaller than the given label.
2478 //if so then it returns true so that the calling function can run the previous valid distance.
2479 //it's a "smart" distance function. It also checks for invalid labels.
2480 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
2483 set<string>::iterator it;
2484 vector<float> orderFloat;
2485 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
2486 map<string, float>::iterator it2;
2488 bool smaller = false;
2490 //unique is the smallest line
2491 if (label == "unique") { return false; }
2493 if (convertTestFloat(label, labelFloat)) {
2494 convert(label, labelFloat);
2495 }else { //cant convert
2500 //go through users set and make them floats
2501 for(it = userLabels.begin(); it != userLabels.end();) {
2504 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
2506 orderFloat.push_back(temp);
2507 userMap[*it] = temp;
2509 }else if (*it == "unique") {
2510 orderFloat.push_back(-1.0);
2511 userMap["unique"] = -1.0;
2514 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
2515 userLabels.erase(it++);
2520 sort(orderFloat.begin(), orderFloat.end());
2522 /*************************************************/
2523 //is this label bigger than any of the users labels
2524 /*************************************************/
2526 //loop through order until you find a label greater than label
2527 for (int i = 0; i < orderFloat.size(); i++) {
2528 if (orderFloat[i] < labelFloat) {
2530 if (orderFloat[i] == -1) {
2531 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
2532 userLabels.erase("unique");
2535 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
2537 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
2538 if (it2->second == orderFloat[i]) {
2540 //remove small labels
2541 userLabels.erase(s);
2545 if (errorOff == "") {mothurOut( s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
2547 //since they are sorted once you find a bigger one stop looking
2554 catch(exception& e) {
2555 errorOut(e, "MothurOut", "anyLabelsToProcess");
2560 /**************************************************************************************************/
2561 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
2566 string line = getline(file);
2568 //before we added this check
2569 if (line[0] != '#') { good = false; }
2572 line = line.substr(1);
2574 vector<string> versionVector;
2575 splitAtChar(version, versionVector, '.');
2577 //check file version
2578 vector<string> linesVector;
2579 splitAtChar(line, linesVector, '.');
2581 if (versionVector.size() != linesVector.size()) { good = false; }
2583 for (int j = 0; j < versionVector.size(); j++) {
2585 convert(versionVector[j], num1);
2586 convert(linesVector[j], num2);
2588 //if mothurs version is newer than this files version, then we want to remake it
2589 if (num1 > num2) { good = false; break; }
2595 if (!good) { file.close(); }
2596 else { file.seekg(0); }
2600 catch(exception& e) {
2601 errorOut(e, "MothurOut", "checkReleaseVersion");
2605 /**************************************************************************************************/
2606 bool MothurOut::isContainingOnlyDigits(string input) {
2609 //are you a digit in ascii code
2610 for (int i = 0;i < input.length(); i++){
2611 if( input[i]>47 && input[i]<58){}
2612 else { return false; }
2617 catch(exception& e) {
2618 errorOut(e, "MothurOut", "isContainingOnlyDigits");
2622 /**************************************************************************************************/
2623 int MothurOut::removeConfidences(string& tax) {
2629 while (tax.find_first_of(';') != -1) {
2631 if (control_pressed) { return 0; }
2634 taxon = tax.substr(0,tax.find_first_of(';'));
2636 int pos = taxon.find_last_of('(');
2639 int pos2 = taxon.find_last_of(')');
2641 string confidenceScore = taxon.substr(pos+1, (pos2-(pos+1)));
2642 if (isNumeric1(confidenceScore)) {
2643 taxon = taxon.substr(0, pos); //rip off confidence
2649 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
2657 catch(exception& e) {
2658 errorOut(e, "MothurOut", "removeConfidences");
2662 /**************************************************************************************************/