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("count");
47 types.insert("processors");
52 errorOut(e, "MothurOut", "getCurrentTypes");
56 /*********************************************************************************************/
57 void MothurOut::printCurrentFiles() {
61 if (accnosfile != "") { mothurOut("accnos=" + accnosfile); mothurOutEndLine(); }
62 if (columnfile != "") { mothurOut("column=" + columnfile); mothurOutEndLine(); }
63 if (designfile != "") { mothurOut("design=" + designfile); mothurOutEndLine(); }
64 if (fastafile != "") { mothurOut("fasta=" + fastafile); mothurOutEndLine(); }
65 if (groupfile != "") { mothurOut("group=" + groupfile); mothurOutEndLine(); }
66 if (listfile != "") { mothurOut("list=" + listfile); mothurOutEndLine(); }
67 if (namefile != "") { mothurOut("name=" + namefile); mothurOutEndLine(); }
68 if (oligosfile != "") { mothurOut("oligos=" + oligosfile); mothurOutEndLine(); }
69 if (orderfile != "") { mothurOut("order=" + orderfile); mothurOutEndLine(); }
70 if (ordergroupfile != "") { mothurOut("ordergroup=" + ordergroupfile); mothurOutEndLine(); }
71 if (phylipfile != "") { mothurOut("phylip=" + phylipfile); mothurOutEndLine(); }
72 if (qualfile != "") { mothurOut("qfile=" + qualfile); mothurOutEndLine(); }
73 if (rabundfile != "") { mothurOut("rabund=" + rabundfile); mothurOutEndLine(); }
74 if (relabundfile != "") { mothurOut("relabund=" + relabundfile); mothurOutEndLine(); }
75 if (sabundfile != "") { mothurOut("sabund=" + sabundfile); mothurOutEndLine(); }
76 if (sfffile != "") { mothurOut("sff=" + sfffile); mothurOutEndLine(); }
77 if (sharedfile != "") { mothurOut("shared=" + sharedfile); mothurOutEndLine(); }
78 if (taxonomyfile != "") { mothurOut("taxonomy=" + taxonomyfile); mothurOutEndLine(); }
79 if (treefile != "") { mothurOut("tree=" + treefile); mothurOutEndLine(); }
80 if (flowfile != "") { mothurOut("flow=" + flowfile); mothurOutEndLine(); }
81 if (biomfile != "") { mothurOut("biom=" + biomfile); mothurOutEndLine(); }
82 if (counttablefile != "") { mothurOut("count=" + counttablefile); mothurOutEndLine(); }
83 if (processors != "1") { mothurOut("processors=" + processors); mothurOutEndLine(); }
87 errorOut(e, "MothurOut", "printCurrentFiles");
91 /*********************************************************************************************/
92 bool MothurOut::hasCurrentFiles() {
94 bool hasCurrent = false;
96 if (accnosfile != "") { return true; }
97 if (columnfile != "") { return true; }
98 if (designfile != "") { return true; }
99 if (fastafile != "") { return true; }
100 if (groupfile != "") { return true; }
101 if (listfile != "") { return true; }
102 if (namefile != "") { return true; }
103 if (oligosfile != "") { return true; }
104 if (orderfile != "") { return true; }
105 if (ordergroupfile != "") { return true; }
106 if (phylipfile != "") { return true; }
107 if (qualfile != "") { return true; }
108 if (rabundfile != "") { return true; }
109 if (relabundfile != "") { return true; }
110 if (sabundfile != "") { return true; }
111 if (sfffile != "") { return true; }
112 if (sharedfile != "") { return true; }
113 if (taxonomyfile != "") { return true; }
114 if (treefile != "") { return true; }
115 if (flowfile != "") { return true; }
116 if (biomfile != "") { return true; }
117 if (counttablefile != "") { return true; }
118 if (processors != "1") { return true; }
123 catch(exception& e) {
124 errorOut(e, "MothurOut", "hasCurrentFiles");
129 /*********************************************************************************************/
130 void MothurOut::clearCurrentFiles() {
156 catch(exception& e) {
157 errorOut(e, "MothurOut", "clearCurrentFiles");
161 /***********************************************************************/
162 string MothurOut::findProgramPath(string programName){
165 string envPath = getenv("PATH");
168 //delimiting path char
170 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
176 //break apart path variable by ':'
178 splitAtChar(envPath, dirs, delim);
180 if (debug) { mothurOut("[DEBUG]: dir's in path: \n"); }
182 //get path related to mothur
183 for (int i = 0; i < dirs.size(); i++) {
185 if (debug) { mothurOut("[DEBUG]: " + dirs[i] + "\n"); }
187 //to lower so we can find it
188 string tempLower = "";
189 for (int j = 0; j < dirs[i].length(); j++) { tempLower += tolower(dirs[i][j]); }
191 //is this mothurs path?
192 if (tempLower.find(programName) != -1) { pPath = dirs[i]; break; }
195 if (debug) { mothurOut("[DEBUG]: programPath = " + pPath + "\n"); }
198 //add programName so it looks like what argv would look like
199 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
200 pPath += "/" + programName;
202 pPath += "\\" + programName;
205 //okay programName is not in the path, so the folder programName is in must be in the path
206 //lets find out which one
208 //get path related to the program
209 for (int i = 0; i < dirs.size(); i++) {
211 if (debug) { mothurOut("[DEBUG]: looking in " + dirs[i] + " for " + programName + " \n"); }
213 //is this the programs path?
215 string tempIn = dirs[i];
216 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
217 tempIn += "/" + programName;
219 tempIn += "\\" + programName;
221 openInputFile(tempIn, in, "");
223 //if this file exists
224 if (in) { in.close(); pPath = tempIn; if (debug) { mothurOut("[DEBUG]: found it, programPath = " + pPath + "\n"); } break; }
231 catch(exception& e) {
232 errorOut(e, "MothurOut", "findProgramPath");
236 /*********************************************************************************************/
237 void MothurOut::setFileName(string filename) {
239 logFileName = filename;
243 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
245 if (pid == 0) { //only one process should output to screen
248 openOutputFile(filename, out);
254 catch(exception& e) {
255 errorOut(e, "MothurOut", "setFileName");
259 /*********************************************************************************************/
260 void MothurOut::setDefaultPath(string pathname) {
263 //add / to name if needed
264 string lastChar = pathname.substr(pathname.length()-1);
265 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
266 if (lastChar != "/") { pathname += "/"; }
268 if (lastChar != "\\") { pathname += "\\"; }
271 defaultPath = pathname;
274 catch(exception& e) {
275 errorOut(e, "MothurOut", "setDefaultPath");
279 /*********************************************************************************************/
280 void MothurOut::setOutputDir(string pathname) {
282 outputDir = pathname;
284 catch(exception& e) {
285 errorOut(e, "MothurOut", "setOutputDir");
289 /*********************************************************************************************/
290 void MothurOut::closeLog() {
295 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
297 if (pid == 0) { //only one process should output to screen
306 catch(exception& e) {
307 errorOut(e, "MothurOut", "closeLog");
312 /*********************************************************************************************/
313 MothurOut::~MothurOut() {
318 catch(exception& e) {
319 errorOut(e, "MothurOut", "MothurOut");
323 /*********************************************************************************************/
324 void MothurOut::mothurOut(string output) {
329 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
331 if (pid == 0) { //only one process should output to screen
341 catch(exception& e) {
342 errorOut(e, "MothurOut", "MothurOut");
346 /*********************************************************************************************/
347 void MothurOut::mothurOutEndLine() {
351 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
353 if (pid == 0) { //only one process should output to screen
363 catch(exception& e) {
364 errorOut(e, "MothurOut", "MothurOutEndLine");
368 /*********************************************************************************************/
369 void MothurOut::mothurOut(string output, ofstream& outputFile) {
374 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
376 if (pid == 0) { //only one process should output to screen
381 outputFile << output;
389 catch(exception& e) {
390 errorOut(e, "MothurOut", "MothurOut");
394 /*********************************************************************************************/
395 void MothurOut::mothurOutEndLine(ofstream& outputFile) {
399 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
401 if (pid == 0) { //only one process should output to screen
412 catch(exception& e) {
413 errorOut(e, "MothurOut", "MothurOutEndLine");
417 /*********************************************************************************************/
418 void MothurOut::mothurOutJustToLog(string output) {
422 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
424 if (pid == 0) { //only one process should output to screen
433 catch(exception& e) {
434 errorOut(e, "MothurOut", "MothurOutJustToLog");
438 /*********************************************************************************************/
439 void MothurOut::errorOut(exception& e, string object, string function) {
441 //mem_usage(vm, rss);
443 mothurOut("[ERROR]: ");
444 mothurOut(toString(e.what()));
445 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.");
448 /*********************************************************************************************/
449 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
450 // process_mem_usage(double &, double &) - takes two doubles by reference,
451 // attempts to read the system-dependent data for a process' virtual memory
452 // size and resident set size, and return the results in KB.
454 // On failure, returns 0.0, 0.0
455 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
456 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
461 // 'file' stat seems to give the most reliable results
463 ifstream stat_stream("/proc/self/stat",ios_base::in);
465 // dummy vars for leading entries in stat that we don't care about
467 string pid, comm, state, ppid, pgrp, session, tty_nr;
468 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
469 string utime, stime, cutime, cstime, priority, nice;
470 string O, itrealvalue, starttime;
472 // the two fields we want
477 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
478 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
479 >> utime >> stime >> cutime >> cstime >> priority >> nice
480 >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
482 long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
483 vm_usage = vsize / 1024.0;
484 resident_set = rss * page_size_kb;
486 mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
490 /* //windows memory usage
491 // Get the list of process identifiers.
492 DWORD aProcesses[1024], cbNeeded, cProcesses;
494 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
496 // Calculate how many process identifiers were returned.
497 cProcesses = cbNeeded / sizeof(DWORD);
499 // Print the memory usage for each process
500 for (int i = 0; i < cProcesses; i++ ) {
501 DWORD processID = aProcesses[i];
503 PROCESS_MEMORY_COUNTERS pmc;
505 HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
507 // Print the process identifier.
508 printf( "\nProcess ID: %u\n", processID);
510 if (NULL != hProcess) {
512 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
513 printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
514 printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
515 printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
516 printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
517 printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
518 printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
519 printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
520 printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
521 printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
523 CloseHandle(hProcess);
533 /***********************************************************************/
534 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
536 fileName = getFullPathName(fileName);
538 fileHandle.open(fileName.c_str(), ios::app);
540 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
547 catch(exception& e) {
548 errorOut(e, "MothurOut", "openOutputFileAppend");
552 /***********************************************************************/
553 void MothurOut::gobble(istream& f){
557 while(isspace(d=f.get())) { ;}
558 if(!f.eof()) { f.putback(d); }
560 catch(exception& e) {
561 errorOut(e, "MothurOut", "gobble");
565 /***********************************************************************/
566 void MothurOut::gobble(istringstream& f){
569 while(isspace(d=f.get())) {;}
570 if(!f.eof()) { f.putback(d); }
572 catch(exception& e) {
573 errorOut(e, "MothurOut", "gobble");
578 /***********************************************************************/
580 string MothurOut::getline(istringstream& fileHandle) {
585 while (!fileHandle.eof()) {
587 char c = fileHandle.get();
589 //are you at the end of the line
590 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
597 catch(exception& e) {
598 errorOut(e, "MothurOut", "getline");
602 /***********************************************************************/
604 string MothurOut::getline(ifstream& fileHandle) {
611 char c = fileHandle.get();
613 //are you at the end of the line
614 if ((c == '\n') || (c == '\r') || (c == '\f') || (c == EOF)){ break; }
621 catch(exception& e) {
622 errorOut(e, "MothurOut", "getline");
626 /***********************************************************************/
628 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
629 #ifdef USE_COMPRESSION
630 inline bool endsWith(string s, const char * suffix){
631 size_t suffixLength = strlen(suffix);
632 return s.size() >= suffixLength && s.substr(s.size() - suffixLength, suffixLength).compare(suffix) == 0;
637 string MothurOut::getRootName(string longName){
640 string rootName = longName;
642 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
643 #ifdef USE_COMPRESSION
644 if (endsWith(rootName, ".gz") || endsWith(rootName, ".bz2")) {
645 int pos = rootName.find_last_of('.');
646 rootName = rootName.substr(0, pos);
647 cerr << "shortening " << longName << " to " << rootName << "\n";
651 if(rootName.find_last_of(".") != rootName.npos){
652 int pos = rootName.find_last_of('.')+1;
653 rootName = rootName.substr(0, pos);
658 catch(exception& e) {
659 errorOut(e, "MothurOut", "getRootName");
663 /***********************************************************************/
665 string MothurOut::getSimpleName(string longName){
667 string simpleName = longName;
670 found=longName.find_last_of("/\\");
672 if(found != longName.npos){
673 simpleName = longName.substr(found+1);
678 catch(exception& e) {
679 errorOut(e, "MothurOut", "getSimpleName");
684 /***********************************************************************/
686 int MothurOut::getRandomIndex(int highest){
689 int random = (int) ((float)(highest+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
693 catch(exception& e) {
694 errorOut(e, "MothurOut", "getRandomIndex");
699 /**********************************************************************/
701 string MothurOut::getPathName(string longName){
703 string rootPathName = longName;
705 if(longName.find_last_of("/\\") != longName.npos){
706 int pos = longName.find_last_of("/\\")+1;
707 rootPathName = longName.substr(0, pos);
712 catch(exception& e) {
713 errorOut(e, "MothurOut", "getPathName");
718 /***********************************************************************/
720 bool MothurOut::dirCheck(string& dirName){
726 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
731 //add / to name if needed
732 string lastChar = dirName.substr(dirName.length()-1);
733 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
734 if (lastChar != "/") { dirName += "/"; }
736 if (lastChar != "\\") { dirName += "\\"; }
739 //test to make sure directory exists
740 dirName = getFullPathName(dirName);
741 string outTemp = dirName + tag + "temp";
743 out.open(outTemp.c_str(), ios::trunc);
745 mothurOut(dirName + " directory does not exist or is not writable."); mothurOutEndLine();
748 mothurRemove(outTemp);
754 catch(exception& e) {
755 errorOut(e, "MothurOut", "dirCheck");
760 /***********************************************************************/
762 string MothurOut::hasPath(string longName){
767 found=longName.find_last_of("~/\\");
769 if(found != longName.npos){
770 path = longName.substr(0, found+1);
775 catch(exception& e) {
776 errorOut(e, "MothurOut", "hasPath");
781 /***********************************************************************/
783 string MothurOut::getExtension(string longName){
785 string extension = "";
787 if(longName.find_last_of('.') != longName.npos){
788 int pos = longName.find_last_of('.');
789 extension = longName.substr(pos, longName.length());
794 catch(exception& e) {
795 errorOut(e, "MothurOut", "getExtension");
799 /***********************************************************************/
800 bool MothurOut::isBlank(string fileName){
803 fileName = getFullPathName(fileName);
806 fileHandle.open(fileName.c_str());
808 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
811 //check for blank file
813 if (fileHandle.eof()) { fileHandle.close(); return true; }
818 catch(exception& e) {
819 errorOut(e, "MothurOut", "isBlank");
823 /***********************************************************************/
825 string MothurOut::getFullPathName(string fileName){
828 string path = hasPath(fileName);
832 if (path == "") { return fileName; } //its a simple name
833 else { //we need to complete the pathname
834 // ex. ../../../filename
835 // cwd = /user/work/desktop
838 //get current working directory
839 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
841 if (path.find("~") != -1) { //go to home directory
844 char *homepath = NULL;
845 homepath = getenv ("HOME");
846 if ( homepath != NULL) { homeDir = homepath; }
847 else { homeDir = ""; }
849 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
852 if (path.rfind("./") == string::npos) { return fileName; } //already complete name
853 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
855 //char* cwdpath = new char[1024];
857 //cwdpath=getcwd(cwdpath,size);
860 char *cwdpath = NULL;
861 cwdpath = getcwd(NULL, 0); // or _getcwd
862 if ( cwdpath != NULL) { cwd = cwdpath; }
868 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
870 //break apart the current working directory
872 while (simpleCWD.find_first_of('/') != string::npos) {
873 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
874 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
877 //get last one // ex. ../../../filename = /user/work/desktop/filename
878 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
881 int index = dirs.size()-1;
883 while((pos = path.rfind("./")) != string::npos) { //while you don't have a complete path
884 if (pos == 0) { break; //you are at the end
885 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
886 path = path.substr(0, pos-1);
888 if (index == 0) { break; }
889 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
890 path = path.substr(0, pos);
891 }else if (pos == 1) { break; //you are at the end
892 }else { mothurOut("cannot resolve path for " + fileName + "\n"); return fileName; }
895 for (int i = index; i >= 0; i--) {
896 newFileName = dirs[i] + "/" + newFileName;
899 newFileName = "/" + newFileName;
903 if (path.find("~") != string::npos) { //go to home directory
904 string homeDir = getenv ("HOMEPATH");
905 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
908 if (path.rfind(".\\") == string::npos) { return fileName; } //already complete name
909 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
911 char *cwdpath = NULL;
912 cwdpath = getcwd(NULL, 0); // or _getcwd
913 if ( cwdpath != NULL) { cwd = cwdpath; }
916 //break apart the current working directory
918 while (cwd.find_first_of('\\') != -1) {
919 string dir = cwd.substr(0,cwd.find_first_of('\\'));
920 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
925 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
927 int index = dirs.size()-1;
929 while((pos = path.rfind(".\\")) != string::npos) { //while you don't have a complete path
930 if (pos == 0) { break; //you are at the end
931 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
932 path = path.substr(0, pos-1);
934 if (index == 0) { break; }
935 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
936 path = path.substr(0, pos);
937 }else if (pos == 1) { break; //you are at the end
938 }else { mothurOut("cannot resolve path for " + fileName + "\n"); return fileName; }
941 for (int i = index; i >= 0; i--) {
942 newFileName = dirs[i] + "\\\\" + newFileName;
951 catch(exception& e) {
952 errorOut(e, "MothurOut", "getFullPathName");
956 /***********************************************************************/
958 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
961 string completeFileName = getFullPathName(fileName);
962 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
963 #ifdef USE_COMPRESSION
964 // check for gzipped or bzipped file
965 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
966 string tempName = string(tmpnam(0));
967 mkfifo(tempName.c_str(), 0666);
968 int fork_result = fork();
969 if (fork_result < 0) {
970 cerr << "Error forking.\n";
972 } else if (fork_result == 0) {
973 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
974 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
975 system(command.c_str());
976 cerr << "Done decompressing " << completeFileName << "\n";
977 mothurRemove(tempName);
980 cerr << "waiting on child process " << fork_result << "\n";
981 completeFileName = tempName;
986 fileHandle.open(completeFileName.c_str());
988 //mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
991 //check for blank file
996 catch(exception& e) {
997 errorOut(e, "MothurOut", "openInputFile - no Error");
1001 /***********************************************************************/
1003 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
1006 //get full path name
1007 string completeFileName = getFullPathName(fileName);
1008 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1009 #ifdef USE_COMPRESSION
1010 // check for gzipped or bzipped file
1011 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
1012 string tempName = string(tmpnam(0));
1013 mkfifo(tempName.c_str(), 0666);
1014 int fork_result = fork();
1015 if (fork_result < 0) {
1016 cerr << "Error forking.\n";
1018 } else if (fork_result == 0) {
1019 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
1020 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
1021 system(command.c_str());
1022 cerr << "Done decompressing " << completeFileName << "\n";
1023 mothurRemove(tempName);
1026 cerr << "waiting on child process " << fork_result << "\n";
1027 completeFileName = tempName;
1033 fileHandle.open(completeFileName.c_str());
1035 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1039 //check for blank file
1041 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
1046 catch(exception& e) {
1047 errorOut(e, "MothurOut", "openInputFile");
1051 /***********************************************************************/
1053 int MothurOut::renameFile(string oldName, string newName){
1056 if (oldName == newName) { return 0; }
1059 int exist = openInputFile(newName, inTest, "");
1062 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1063 if (exist == 0) { //you could open it so you want to delete it
1064 string command = "rm " + newName;
1065 system(command.c_str());
1068 string command = "mv " + oldName + " " + newName;
1069 system(command.c_str());
1071 mothurRemove(newName);
1072 int renameOk = rename(oldName.c_str(), newName.c_str());
1077 catch(exception& e) {
1078 errorOut(e, "MothurOut", "renameFile");
1083 /***********************************************************************/
1085 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
1088 string completeFileName = getFullPathName(fileName);
1089 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1090 #ifdef USE_COMPRESSION
1091 // check for gzipped file
1092 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
1093 string tempName = string(tmpnam(0));
1094 mkfifo(tempName.c_str(), 0666);
1095 cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
1096 int fork_result = fork();
1097 if (fork_result < 0) {
1098 cerr << "Error forking.\n";
1100 } else if (fork_result == 0) {
1101 string command = string(endsWith(completeFileName, ".gz") ? "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
1102 system(command.c_str());
1105 completeFileName = tempName;
1110 fileHandle.open(completeFileName.c_str(), ios::trunc);
1112 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1119 catch(exception& e) {
1120 errorOut(e, "MothurOut", "openOutputFile");
1126 /**************************************************************************************************/
1127 int MothurOut::appendFiles(string temp, string filename) {
1132 //open output file in append mode
1133 openOutputFileAppend(filename, output);
1134 int ableToOpen = openInputFile(temp, input, "no error");
1135 //int ableToOpen = openInputFile(temp, input);
1138 if (ableToOpen == 0) { //you opened it
1141 while (!input.eof()) {
1142 input.read(buffer, 4096);
1143 output.write(buffer, input.gcount());
1144 //count number of lines
1145 for (int i = 0; i < input.gcount(); i++) { if (buffer[i] == '\n') {numLines++;} }
1154 catch(exception& e) {
1155 errorOut(e, "MothurOut", "appendFiles");
1160 /**************************************************************************************************/
1161 string MothurOut::sortFile(string distFile, string outputDir){
1164 //if (outputDir == "") { outputDir += hasPath(distFile); }
1165 string outfile = getRootName(distFile) + "sorted.dist";
1168 //if you can, use the unix sort since its been optimized for years
1169 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1170 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
1171 system(command.c_str());
1172 #else //you are stuck with my best attempt...
1173 //windows sort does not have a way to specify a column, only a character in the line
1174 //since we cannot assume that the distance will always be at the the same character location on each line
1175 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
1177 //read in file line by file and put distance first
1178 string tempDistFile = distFile + ".temp";
1181 openInputFile(distFile, input);
1182 openOutputFile(tempDistFile, output);
1184 string firstName, secondName;
1187 input >> firstName >> secondName >> dist;
1188 output << dist << '\t' << firstName << '\t' << secondName << endl;
1195 //sort using windows sort
1196 string tempOutfile = outfile + ".temp";
1197 string command = "sort " + tempDistFile + " /O " + tempOutfile;
1198 system(command.c_str());
1200 //read in sorted file and put distance at end again
1202 openInputFile(tempOutfile, input2);
1203 openOutputFile(outfile, output);
1206 input2 >> dist >> firstName >> secondName;
1207 output << firstName << '\t' << secondName << '\t' << dist << endl;
1214 mothurRemove(tempDistFile);
1215 mothurRemove(tempOutfile);
1220 catch(exception& e) {
1221 errorOut(e, "MothurOut", "sortFile");
1225 /**************************************************************************************************/
1226 vector<unsigned long long> MothurOut::setFilePosFasta(string filename, int& num) {
1228 vector<unsigned long long> positions;
1230 //openInputFile(filename, inFASTA);
1231 inFASTA.open(filename.c_str(), ios::binary);
1234 unsigned long long count = 0;
1235 while(!inFASTA.eof()){
1236 //input = getline(inFASTA);
1237 //cout << input << '\t' << inFASTA.tellg() << endl;
1238 //if (input.length() != 0) {
1239 // if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); cout << (pos - input.length() - 1) << endl; }
1241 //gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
1242 char c = inFASTA.get(); count++;
1244 positions.push_back(count-1);
1245 //cout << count << endl;
1250 num = positions.size();
1255 //get num bytes in file
1256 pFile = fopen (filename.c_str(),"rb");
1257 if (pFile==NULL) perror ("Error opening file");
1259 fseek (pFile, 0, SEEK_END);
1264 unsigned long long size = positions[(positions.size()-1)];
1266 openInputFile(filename, in);
1271 if(in.eof()) { break; }
1276 positions.push_back(size);
1281 catch(exception& e) {
1282 errorOut(e, "MothurOut", "setFilePosFasta");
1286 /**************************************************************************************************/
1287 vector<unsigned long long> MothurOut::setFilePosEachLine(string filename, int& num) {
1289 filename = getFullPathName(filename);
1291 vector<unsigned long long> positions;
1293 //openInputFile(filename, in);
1294 in.open(filename.c_str(), ios::binary);
1297 unsigned long long count = 0;
1298 positions.push_back(0);
1301 //getline counting reads
1302 char d = in.get(); count++;
1303 while ((d != '\n') && (d != '\r') && (d != '\f') && (d != in.eof())) {
1304 //get next character
1310 d=in.get(); count++;
1311 while(isspace(d) && (d != in.eof())) { d=in.get(); count++;}
1313 positions.push_back(count-1);
1314 //cout << count-1 << endl;
1318 num = positions.size()-1;
1321 unsigned long long size;
1323 //get num bytes in file
1324 pFile = fopen (filename.c_str(),"rb");
1325 if (pFile==NULL) perror ("Error opening file");
1327 fseek (pFile, 0, SEEK_END);
1332 positions[(positions.size()-1)] = size;
1336 catch(exception& e) {
1337 errorOut(e, "MothurOut", "setFilePosEachLine");
1341 /**************************************************************************************************/
1343 vector<unsigned long long> MothurOut::divideFile(string filename, int& proc) {
1345 vector<unsigned long long> filePos;
1346 filePos.push_back(0);
1349 unsigned long long size;
1351 filename = getFullPathName(filename);
1353 //get num bytes in file
1354 pFile = fopen (filename.c_str(),"rb");
1355 if (pFile==NULL) perror ("Error opening file");
1357 fseek (pFile, 0, SEEK_END);
1362 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1364 //estimate file breaks
1365 unsigned long long chunkSize = 0;
1366 chunkSize = size / proc;
1368 //file to small to divide by processors
1369 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1371 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1372 for (int i = 0; i < proc; i++) {
1373 unsigned long long spot = (i+1) * chunkSize;
1376 openInputFile(filename, in);
1380 unsigned long long newSpot = spot;
1384 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1385 else if (int(c) == -1) { break; }
1389 //there was not another sequence before the end of the file
1390 unsigned long long sanityPos = in.tellg();
1392 if (sanityPos == -1) { break; }
1393 else { filePos.push_back(newSpot); }
1399 filePos.push_back(size);
1401 //sanity check filePos
1402 for (int i = 0; i < (filePos.size()-1); i++) {
1403 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1406 proc = (filePos.size() - 1);
1408 mothurOut("[ERROR]: Windows version should not be calling the divideFile function."); mothurOutEndLine();
1410 filePos.push_back(size);
1414 catch(exception& e) {
1415 errorOut(e, "MothurOut", "divideFile");
1419 /**************************************************************************************************/
1420 int MothurOut::divideFile(string filename, int& proc, vector<string>& files) {
1423 vector<unsigned long long> filePos = divideFile(filename, proc);
1425 for (int i = 0; i < (filePos.size()-1); i++) {
1429 openInputFile(filename, in);
1430 in.seekg(filePos[i]);
1431 unsigned long long size = filePos[(i+1)] - filePos[i];
1432 char* chunk = new char[size];
1433 in.read(chunk, size);
1437 string fileChunkName = filename + "." + toString(i) + ".tmp";
1439 openOutputFile(fileChunkName, out);
1441 out << chunk << endl;
1446 files.push_back(fileChunkName);
1451 catch(exception& e) {
1452 errorOut(e, "MothurOut", "divideFile");
1456 /***********************************************************************/
1458 bool MothurOut::isTrue(string f){
1461 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1463 if ((f == "TRUE") || (f == "T")) { return true; }
1464 else { return false; }
1466 catch(exception& e) {
1467 errorOut(e, "MothurOut", "isTrue");
1472 /***********************************************************************/
1474 float MothurOut::roundDist(float dist, int precision){
1476 return int(dist * precision + 0.5)/float(precision);
1478 catch(exception& e) {
1479 errorOut(e, "MothurOut", "roundDist");
1483 /***********************************************************************/
1485 float MothurOut::ceilDist(float dist, int precision){
1487 return int(ceil(dist * precision))/float(precision);
1489 catch(exception& e) {
1490 errorOut(e, "MothurOut", "ceilDist");
1494 /***********************************************************************/
1496 vector<string> MothurOut::splitWhiteSpace(string& rest, char buffer[], int size){
1498 vector<string> pieces;
1500 for (int i = 0; i < size; i++) {
1501 if (!isspace(buffer[i])) { rest += buffer[i]; }
1503 if (rest != "") { pieces.push_back(rest); rest = ""; }
1504 while (i < size) { //gobble white space
1505 if (isspace(buffer[i])) { i++; }
1506 else { rest = buffer[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1513 catch(exception& e) {
1514 errorOut(e, "MothurOut", "splitWhiteSpace");
1518 /***********************************************************************/
1519 vector<string> MothurOut::splitWhiteSpace(string input){
1521 vector<string> pieces;
1524 for (int i = 0; i < input.length(); i++) {
1525 if (!isspace(input[i])) { rest += input[i]; }
1527 if (rest != "") { pieces.push_back(rest); rest = ""; }
1528 while (i < input.length()) { //gobble white space
1529 if (isspace(input[i])) { i++; }
1530 else { rest = input[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1535 if (rest != "") { pieces.push_back(rest); }
1539 catch(exception& e) {
1540 errorOut(e, "MothurOut", "splitWhiteSpace");
1544 /***********************************************************************/
1545 vector<string> MothurOut::splitWhiteSpaceWithQuotes(string input){
1547 vector<string> pieces;
1550 int pos = input.find('\'');
1551 int pos2 = input.find('\"');
1553 if ((pos == string::npos) && (pos2 == string::npos)) { return splitWhiteSpace(input); } //no quotes to worry about
1555 for (int i = 0; i < input.length(); i++) {
1556 if ((input[i] == '\'') || (input[i] == '\"') || (rest == "\'") || (rest == "\"")) { //grab everything til end or next ' or "
1558 for (int j = i+1; j < input.length(); j++) {
1559 if ((input[j] == '\'') || (input[j] == '\"')) { //then quit
1563 }else { rest += input[j]; }
1565 }else if (!isspace(input[i])) { rest += input[i]; }
1567 if (rest != "") { pieces.push_back(rest); rest = ""; }
1568 while (i < input.length()) { //gobble white space
1569 if (isspace(input[i])) { i++; }
1570 else { rest = input[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1575 if (rest != "") { pieces.push_back(rest); }
1579 catch(exception& e) {
1580 errorOut(e, "MothurOut", "splitWhiteSpace");
1584 //**********************************************************************************************************************
1585 int MothurOut::readTax(string namefile, map<string, string>& taxMap) {
1589 openInputFile(namefile, in);
1593 bool pairDone = false;
1594 bool columnOne = true;
1595 string firstCol, secondCol;
1598 if (control_pressed) { break; }
1600 in.read(buffer, 4096);
1601 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1603 for (int i = 0; i < pieces.size(); i++) {
1604 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1605 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1608 //are there confidence scores, if so remove them
1609 if (secondCol.find_first_of('(') != -1) { removeConfidences(secondCol); }
1610 taxMap[firstCol] = secondCol;
1611 if (debug) { mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n"); }
1619 vector<string> pieces = splitWhiteSpace(rest);
1621 for (int i = 0; i < pieces.size(); i++) {
1622 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1623 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1626 //are there confidence scores, if so remove them
1627 if (secondCol.find_first_of('(') != -1) { removeConfidences(secondCol); }
1628 taxMap[firstCol] = secondCol;
1629 if (debug) { mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n"); }
1635 return taxMap.size();
1638 catch(exception& e) {
1639 errorOut(e, "MothurOut", "readTax");
1643 /**********************************************************************************************************************/
1644 int MothurOut::readNames(string namefile, map<string, string>& nameMap, bool redund) {
1648 openInputFile(namefile, in);
1652 bool pairDone = false;
1653 bool columnOne = true;
1654 string firstCol, secondCol;
1657 if (control_pressed) { break; }
1659 in.read(buffer, 4096);
1660 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1662 for (int i = 0; i < pieces.size(); i++) {
1663 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1664 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1667 //parse names into vector
1668 vector<string> theseNames;
1669 splitAtComma(secondCol, theseNames);
1670 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1678 vector<string> pieces = splitWhiteSpace(rest);
1680 for (int i = 0; i < pieces.size(); i++) {
1681 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1682 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1685 //parse names into vector
1686 vector<string> theseNames;
1687 splitAtComma(secondCol, theseNames);
1688 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1694 return nameMap.size();
1697 catch(exception& e) {
1698 errorOut(e, "MothurOut", "readNames");
1702 /**********************************************************************************************************************/
1703 int MothurOut::readNames(string namefile, map<string, string>& nameMap, int flip) {
1707 openInputFile(namefile, in);
1711 bool pairDone = false;
1712 bool columnOne = true;
1713 string firstCol, secondCol;
1716 if (control_pressed) { break; }
1718 in.read(buffer, 4096);
1719 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1721 for (int i = 0; i < pieces.size(); i++) {
1722 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1723 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1726 nameMap[secondCol] = firstCol;
1734 vector<string> pieces = splitWhiteSpace(rest);
1736 for (int i = 0; i < pieces.size(); i++) {
1737 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1738 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1741 nameMap[secondCol] = firstCol;
1747 return nameMap.size();
1750 catch(exception& e) {
1751 errorOut(e, "MothurOut", "readNames");
1755 /**********************************************************************************************************************/
1756 int MothurOut::readNames(string namefile, map<string, string>& nameMap, map<string, int>& nameCount) {
1758 nameMap.clear(); nameCount.clear();
1761 openInputFile(namefile, in);
1765 bool pairDone = false;
1766 bool columnOne = true;
1767 string firstCol, secondCol;
1770 if (control_pressed) { break; }
1772 in.read(buffer, 4096);
1773 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1775 for (int i = 0; i < pieces.size(); i++) {
1776 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1777 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1780 //parse names into vector
1781 vector<string> theseNames;
1782 splitAtComma(secondCol, theseNames);
1783 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1784 nameCount[firstCol] = theseNames.size();
1792 vector<string> pieces = splitWhiteSpace(rest);
1794 for (int i = 0; i < pieces.size(); i++) {
1795 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1796 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1799 //parse names into vector
1800 vector<string> theseNames;
1801 splitAtComma(secondCol, theseNames);
1802 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1803 nameCount[firstCol] = theseNames.size();
1809 return nameMap.size();
1812 catch(exception& e) {
1813 errorOut(e, "MothurOut", "readNames");
1817 /**********************************************************************************************************************/
1818 int MothurOut::readNames(string namefile, map<string, string>& nameMap) {
1822 openInputFile(namefile, in);
1826 bool pairDone = false;
1827 bool columnOne = true;
1828 string firstCol, secondCol;
1831 if (control_pressed) { break; }
1833 in.read(buffer, 4096);
1834 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1836 for (int i = 0; i < pieces.size(); i++) {
1837 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1838 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1840 if (pairDone) { nameMap[firstCol] = secondCol; pairDone = false; }
1846 vector<string> pieces = splitWhiteSpace(rest);
1848 for (int i = 0; i < pieces.size(); i++) {
1849 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1850 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1852 if (pairDone) { nameMap[firstCol] = secondCol; pairDone = false; }
1856 return nameMap.size();
1859 catch(exception& e) {
1860 errorOut(e, "MothurOut", "readNames");
1864 /**********************************************************************************************************************/
1865 int MothurOut::readNames(string namefile, map<string, vector<string> >& nameMap) {
1869 openInputFile(namefile, in);
1873 bool pairDone = false;
1874 bool columnOne = true;
1875 string firstCol, secondCol;
1878 if (control_pressed) { break; }
1880 in.read(buffer, 4096);
1881 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1883 for (int i = 0; i < pieces.size(); i++) {
1884 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1885 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1888 vector<string> temp;
1889 splitAtComma(secondCol, temp);
1890 nameMap[firstCol] = temp;
1898 vector<string> pieces = splitWhiteSpace(rest);
1900 for (int i = 0; i < pieces.size(); i++) {
1901 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1902 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1905 vector<string> temp;
1906 splitAtComma(secondCol, temp);
1907 nameMap[firstCol] = temp;
1913 return nameMap.size();
1915 catch(exception& e) {
1916 errorOut(e, "MothurOut", "readNames");
1920 /**********************************************************************************************************************/
1921 map<string, int> MothurOut::readNames(string namefile) {
1923 map<string, int> nameMap;
1927 openInputFile(namefile, in);
1931 bool pairDone = false;
1932 bool columnOne = true;
1933 string firstCol, secondCol;
1936 if (control_pressed) { break; }
1938 in.read(buffer, 4096);
1939 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1941 for (int i = 0; i < pieces.size(); i++) {
1942 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1943 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1946 int num = getNumNames(secondCol);
1947 nameMap[firstCol] = num;
1955 vector<string> pieces = splitWhiteSpace(rest);
1956 for (int i = 0; i < pieces.size(); i++) {
1957 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1958 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1961 int num = getNumNames(secondCol);
1962 nameMap[firstCol] = num;
1971 catch(exception& e) {
1972 errorOut(e, "MothurOut", "readNames");
1976 /**********************************************************************************************************************/
1977 int MothurOut::readNames(string namefile, vector<seqPriorityNode>& nameVector, map<string, string>& fastamap) {
1983 openInputFile(namefile, in);
1987 bool pairDone = false;
1988 bool columnOne = true;
1989 string firstCol, secondCol;
1992 if (control_pressed) { break; }
1994 in.read(buffer, 4096);
1995 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1997 for (int i = 0; i < pieces.size(); i++) {
1998 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1999 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2002 int num = getNumNames(secondCol);
2004 map<string, string>::iterator it = fastamap.find(firstCol);
2005 if (it == fastamap.end()) {
2007 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
2009 seqPriorityNode temp(num, it->second, firstCol);
2010 nameVector.push_back(temp);
2020 vector<string> pieces = splitWhiteSpace(rest);
2022 for (int i = 0; i < pieces.size(); i++) {
2023 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2024 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2027 int num = getNumNames(secondCol);
2029 map<string, string>::iterator it = fastamap.find(firstCol);
2030 if (it == fastamap.end()) {
2032 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
2034 seqPriorityNode temp(num, it->second, firstCol);
2035 nameVector.push_back(temp);
2044 catch(exception& e) {
2045 errorOut(e, "MothurOut", "readNames");
2049 //**********************************************************************************************************************
2050 set<string> MothurOut::readAccnos(string accnosfile){
2054 openInputFile(accnosfile, in);
2061 if (control_pressed) { break; }
2063 in.read(buffer, 4096);
2064 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2066 for (int i = 0; i < pieces.size(); i++) { names.insert(pieces[i]); }
2071 vector<string> pieces = splitWhiteSpace(rest);
2072 for (int i = 0; i < pieces.size(); i++) { names.insert(pieces[i]); }
2076 catch(exception& e) {
2077 errorOut(e, "MothurOut", "readAccnos");
2081 //**********************************************************************************************************************
2082 int MothurOut::readAccnos(string accnosfile, vector<string>& names){
2086 openInputFile(accnosfile, in);
2093 if (control_pressed) { break; }
2095 in.read(buffer, 4096);
2096 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2098 for (int i = 0; i < pieces.size(); i++) { names.push_back(pieces[i]); }
2103 vector<string> pieces = splitWhiteSpace(rest);
2104 for (int i = 0; i < pieces.size(); i++) { names.push_back(pieces[i]); }
2109 catch(exception& e) {
2110 errorOut(e, "MothurOut", "readAccnos");
2114 /***********************************************************************/
2116 int MothurOut::getNumNames(string names){
2122 for(int i=0;i<names.size();i++){
2123 if(names[i] == ','){
2131 catch(exception& e) {
2132 errorOut(e, "MothurOut", "getNumNames");
2136 /***********************************************************************/
2138 int MothurOut::getNumChar(string line, char c){
2143 for(int i=0;i<line.size();i++){
2152 catch(exception& e) {
2153 errorOut(e, "MothurOut", "getNumChar");
2157 //**********************************************************************************************************************
2158 bool MothurOut::isSubset(vector<string> bigset, vector<string> subset) {
2162 if (subset.size() > bigset.size()) { return false; }
2164 //check if each guy in suset is also in bigset
2165 for (int i = 0; i < subset.size(); i++) {
2167 for (int j = 0; j < bigset.size(); j++) {
2168 if (subset[i] == bigset[j]) { match = true; break; }
2171 //you have a guy in subset that had no match in bigset
2172 if (match == false) { return false; }
2178 catch(exception& e) {
2179 errorOut(e, "MothurOut", "isSubset");
2183 /***********************************************************************/
2184 int MothurOut::mothurRemove(string filename){
2186 filename = getFullPathName(filename);
2187 int error = remove(filename.c_str());
2189 // if (errno != ENOENT) { //ENOENT == file does not exist
2190 // string message = "Error deleting file " + filename;
2191 // perror(message.c_str());
2196 catch(exception& e) {
2197 errorOut(e, "MothurOut", "mothurRemove");
2201 /***********************************************************************/
2202 bool MothurOut::mothurConvert(string item, int& num){
2206 if (isNumeric1(item)) {
2211 mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2212 commandInputsConvertError = true;
2217 catch(exception& e) {
2218 errorOut(e, "MothurOut", "mothurConvert");
2222 /***********************************************************************/
2223 bool MothurOut::mothurConvert(string item, intDist& num){
2227 if (isNumeric1(item)) {
2232 mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2233 commandInputsConvertError = true;
2238 catch(exception& e) {
2239 errorOut(e, "MothurOut", "mothurConvert");
2244 /***********************************************************************/
2245 bool MothurOut::isNumeric1(string stringToCheck){
2247 bool numeric = false;
2249 if(stringToCheck.find_first_not_of("0123456789.-") == string::npos) { numeric = true; }
2253 catch(exception& e) {
2254 errorOut(e, "MothurOut", "isNumeric1");
2259 /***********************************************************************/
2260 bool MothurOut::mothurConvert(string item, float& num){
2264 if (isNumeric1(item)) {
2269 mothurOut("[ERROR]: cannot convert " + item + " to a float."); mothurOutEndLine();
2270 commandInputsConvertError = true;
2275 catch(exception& e) {
2276 errorOut(e, "MothurOut", "mothurConvert");
2280 /***********************************************************************/
2281 bool MothurOut::mothurConvert(string item, double& num){
2285 if (isNumeric1(item)) {
2290 mothurOut("[ERROR]: cannot convert " + item + " to a double."); mothurOutEndLine();
2291 commandInputsConvertError = true;
2296 catch(exception& e) {
2297 errorOut(e, "MothurOut", "mothurConvert");
2301 /**************************************************************************************************/
2303 vector<vector<double> > MothurOut::binomial(int maxOrder){
2305 vector<vector<double> > binomial(maxOrder+1);
2307 for(int i=0;i<=maxOrder;i++){
2308 binomial[i].resize(maxOrder+1);
2317 for(int i=2;i<=maxOrder;i++){
2321 for(int i=2;i<=maxOrder;i++){
2322 for(int j=1;j<=maxOrder;j++){
2323 if(i==j){ binomial[i][j]=1; }
2324 if(j>i) { binomial[i][j]=0; }
2325 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
2332 catch(exception& e) {
2333 errorOut(e, "MothurOut", "binomial");
2337 /**************************************************************************************************/
2338 unsigned int MothurOut::fromBase36(string base36){
2340 unsigned int num = 0;
2342 map<char, int> converts;
2407 while (i < base36.length()) {
2409 num = 36 * num + converts[c];
2416 catch(exception& e) {
2417 errorOut(e, "MothurOut", "fromBase36");
2421 /***********************************************************************/
2423 int MothurOut::factorial(int num){
2427 for (int i = 1; i <= num; i++) {
2433 catch(exception& e) {
2434 errorOut(e, "MothurOut", "factorial");
2438 /***********************************************************************/
2440 int MothurOut::getNumSeqs(ifstream& file){
2442 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
2446 catch(exception& e) {
2447 errorOut(e, "MothurOut", "getNumSeqs");
2451 /***********************************************************************/
2452 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
2457 input = getline(file);
2458 if (input.length() != 0) {
2459 if(input[0] == '>'){ numSeqs++; }
2463 catch(exception& e) {
2464 errorOut(e, "MothurOut", "getNumSeqs");
2468 /***********************************************************************/
2470 //This function parses the estimator options and puts them in a vector
2471 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
2473 string individual = "";
2474 int estimLength = estim.size();
2475 for(int i=0;i<estimLength;i++){
2476 if(estim[i] == symbol){
2477 container.push_back(individual);
2481 individual += estim[i];
2484 container.push_back(individual);
2487 catch(exception& e) {
2488 errorOut(e, "MothurOut", "splitAtChar");
2493 /***********************************************************************/
2495 //This function parses the estimator options and puts them in a vector
2496 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
2498 string individual = "";
2499 int estimLength = estim.size();
2500 bool prevEscape = false;
2501 for(int i=0;i<estimLength;i++){
2503 individual += estim[i];
2507 if(estim[i] == '\\'){
2510 else if(estim[i] == '-'){
2511 container.push_back(individual);
2516 individual += estim[i];
2521 container.push_back(individual);
2523 catch(exception& e) {
2524 errorOut(e, "MothurOut", "splitAtDash");
2529 /***********************************************************************/
2530 //This function parses the label options and puts them in a set
2531 void MothurOut::splitAtDash(string& estim, set<string>& container) {
2533 string individual = "";
2534 int estimLength = estim.size();
2535 bool prevEscape = false;
2536 for(int i=0;i<estimLength;i++){
2538 individual += estim[i];
2542 if(estim[i] == '\\'){
2545 else if(estim[i] == '-'){
2546 container.insert(individual);
2551 individual += estim[i];
2556 container.insert(individual);
2559 catch(exception& e) {
2560 errorOut(e, "MothurOut", "splitAtDash");
2564 /***********************************************************************/
2565 //This function parses the line options and puts them in a set
2566 void MothurOut::splitAtDash(string& estim, set<int>& container) {
2568 string individual = "";
2570 int estimLength = estim.size();
2571 bool prevEscape = false;
2572 for(int i=0;i<estimLength;i++){
2574 individual += estim[i];
2578 if(estim[i] == '\\'){
2581 else if(estim[i] == '-'){
2582 convert(individual, lineNum); //convert the string to int
2583 container.insert(lineNum);
2588 individual += estim[i];
2593 convert(individual, lineNum); //convert the string to int
2594 container.insert(lineNum);
2596 catch(exception& e) {
2597 errorOut(e, "MothurOut", "splitAtDash");
2601 /***********************************************************************/
2602 string MothurOut::makeList(vector<string>& names) {
2606 if (names.size() == 0) { return list; }
2608 for (int i = 0; i < names.size()-1; i++) { list += names[i] + ","; }
2611 list += names[names.size()-1];
2615 catch(exception& e) {
2616 errorOut(e, "MothurOut", "makeList");
2621 /***********************************************************************/
2622 //This function parses the a string and puts peices in a vector
2623 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
2625 string individual = "";
2626 int estimLength = estim.size();
2627 for(int i=0;i<estimLength;i++){
2628 if(estim[i] == ','){
2629 container.push_back(individual);
2633 individual += estim[i];
2636 container.push_back(individual);
2641 // string individual;
2643 // while (estim.find_first_of(',') != -1) {
2644 // individual = estim.substr(0,estim.find_first_of(','));
2645 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
2646 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
2647 // container.push_back(individual);
2651 // container.push_back(estim);
2653 catch(exception& e) {
2654 errorOut(e, "MothurOut", "splitAtComma");
2658 /***********************************************************************/
2659 //This function splits up the various option parameters
2660 void MothurOut::splitAtChar(string& prefix, string& suffix, char c){
2662 prefix = suffix.substr(0,suffix.find_first_of(c));
2663 if ((suffix.find_first_of(c)+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2664 suffix = suffix.substr(suffix.find_first_of(c)+1, suffix.length());
2666 while(suffix.at(0) == ' ')
2667 suffix = suffix.substr(1, suffix.length());
2671 catch(exception& e) {
2672 errorOut(e, "MothurOut", "splitAtComma");
2677 /***********************************************************************/
2679 //This function splits up the various option parameters
2680 void MothurOut::splitAtComma(string& prefix, string& suffix){
2682 prefix = suffix.substr(0,suffix.find_first_of(','));
2683 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2684 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
2686 while(suffix.at(0) == ' ')
2687 suffix = suffix.substr(1, suffix.length());
2691 catch(exception& e) {
2692 errorOut(e, "MothurOut", "splitAtComma");
2696 /***********************************************************************/
2698 //This function separates the key value from the option value i.e. dist=96_...
2699 void MothurOut::splitAtEquals(string& key, string& value){
2701 if(value.find_first_of('=') != -1){
2702 key = value.substr(0,value.find_first_of('='));
2703 if ((value.find_first_of('=')+1) <= value.length()) {
2704 value = value.substr(value.find_first_of('=')+1, value.length());
2711 catch(exception& e) {
2712 errorOut(e, "MothurOut", "splitAtEquals");
2717 /**************************************************************************************************/
2719 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
2721 for (int i = 0; i < Groups.size(); i++) {
2722 if (groupname == Groups[i]) { return true; }
2726 catch(exception& e) {
2727 errorOut(e, "MothurOut", "inUsersGroups");
2731 /**************************************************************************************************/
2733 bool MothurOut::inUsersGroups(vector<int> set, vector< vector<int> > sets) {
2735 for (int i = 0; i < sets.size(); i++) {
2736 if (set == sets[i]) { return true; }
2740 catch(exception& e) {
2741 errorOut(e, "MothurOut", "inUsersGroups");
2745 /**************************************************************************************************/
2747 bool MothurOut::inUsersGroups(int groupname, vector<int> Groups) {
2749 for (int i = 0; i < Groups.size(); i++) {
2750 if (groupname == Groups[i]) { return true; }
2754 catch(exception& e) {
2755 errorOut(e, "MothurOut", "inUsersGroups");
2760 /**************************************************************************************************/
2761 //returns true if any of the strings in first vector are in second vector
2762 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
2765 for (int i = 0; i < groupnames.size(); i++) {
2766 if (inUsersGroups(groupnames[i], Groups)) { return true; }
2770 catch(exception& e) {
2771 errorOut(e, "MothurOut", "inUsersGroups");
2775 /***********************************************************************/
2776 //this function determines if the user has given us labels that are smaller than the given label.
2777 //if so then it returns true so that the calling function can run the previous valid distance.
2778 //it's a "smart" distance function. It also checks for invalid labels.
2779 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
2782 set<string>::iterator it;
2783 vector<float> orderFloat;
2784 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
2785 map<string, float>::iterator it2;
2787 bool smaller = false;
2789 //unique is the smallest line
2790 if (label == "unique") { return false; }
2792 if (convertTestFloat(label, labelFloat)) {
2793 convert(label, labelFloat);
2794 }else { //cant convert
2799 //go through users set and make them floats
2800 for(it = userLabels.begin(); it != userLabels.end();) {
2803 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
2805 orderFloat.push_back(temp);
2806 userMap[*it] = temp;
2808 }else if (*it == "unique") {
2809 orderFloat.push_back(-1.0);
2810 userMap["unique"] = -1.0;
2813 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
2814 userLabels.erase(it++);
2819 sort(orderFloat.begin(), orderFloat.end());
2821 /*************************************************/
2822 //is this label bigger than any of the users labels
2823 /*************************************************/
2825 //loop through order until you find a label greater than label
2826 for (int i = 0; i < orderFloat.size(); i++) {
2827 if (orderFloat[i] < labelFloat) {
2829 if (orderFloat[i] == -1) {
2830 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
2831 userLabels.erase("unique");
2834 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
2836 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
2837 if (it2->second == orderFloat[i]) {
2839 //remove small labels
2840 userLabels.erase(s);
2844 if (errorOff == "") {mothurOut( s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
2846 //since they are sorted once you find a bigger one stop looking
2853 catch(exception& e) {
2854 errorOut(e, "MothurOut", "anyLabelsToProcess");
2859 /**************************************************************************************************/
2860 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
2865 string line = getline(file);
2867 //before we added this check
2868 if (line[0] != '#') { good = false; }
2871 line = line.substr(1);
2873 vector<string> versionVector;
2874 splitAtChar(version, versionVector, '.');
2876 //check file version
2877 vector<string> linesVector;
2878 splitAtChar(line, linesVector, '.');
2880 if (versionVector.size() != linesVector.size()) { good = false; }
2882 for (int j = 0; j < versionVector.size(); j++) {
2884 convert(versionVector[j], num1);
2885 convert(linesVector[j], num2);
2887 //if mothurs version is newer than this files version, then we want to remake it
2888 if (num1 > num2) { good = false; break; }
2894 if (!good) { file.close(); }
2895 else { file.seekg(0); }
2899 catch(exception& e) {
2900 errorOut(e, "MothurOut", "checkReleaseVersion");
2904 /**************************************************************************************************/
2905 bool MothurOut::isContainingOnlyDigits(string input) {
2908 //are you a digit in ascii code
2909 for (int i = 0;i < input.length(); i++){
2910 if( input[i]>47 && input[i]<58){}
2911 else { return false; }
2916 catch(exception& e) {
2917 errorOut(e, "MothurOut", "isContainingOnlyDigits");
2921 /**************************************************************************************************/
2922 int MothurOut::removeConfidences(string& tax) {
2928 while (tax.find_first_of(';') != -1) {
2930 if (control_pressed) { return 0; }
2933 taxon = tax.substr(0,tax.find_first_of(';'));
2935 int pos = taxon.find_last_of('(');
2938 int pos2 = taxon.find_last_of(')');
2940 string confidenceScore = taxon.substr(pos+1, (pos2-(pos+1)));
2941 if (isNumeric1(confidenceScore)) {
2942 taxon = taxon.substr(0, pos); //rip off confidence
2948 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
2956 catch(exception& e) {
2957 errorOut(e, "MothurOut", "removeConfidences");
2961 /**************************************************************************************************/
2962 string MothurOut::removeQuotes(string tax) {
2968 for (int i = 0; i < tax.length(); i++) {
2970 if (control_pressed) { return newTax; }
2972 if ((tax[i] != '\'') && (tax[i] != '\"')) { newTax += tax[i]; }
2978 catch(exception& e) {
2979 errorOut(e, "MothurOut", "removeQuotes");
2983 /**************************************************************************************************/
2984 // function for calculating standard deviation
2985 double MothurOut::getStandardDeviation(vector<int>& featureVector){
2989 for (int i = 0; i < featureVector.size(); i++) { average += featureVector[i]; }
2990 average /= (double) featureVector.size();
2992 //find standard deviation
2994 for (int i = 0; i < featureVector.size(); i++) { //compute the difference of each dist from the mean, and square the result of each
2995 stdDev += ((featureVector[i] - average) * (featureVector[i] - average));
2998 stdDev /= (double) featureVector.size();
2999 stdDev = sqrt(stdDev);
3003 catch(exception& e) {
3004 errorOut(e, "MothurOut", "getStandardDeviation");
3008 /**************************************************************************************************/