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 string errorType = toString(e.what());
445 int pos = errorType.find("bad_alloc");
446 mothurOut("[ERROR]: ");
447 mothurOut(errorType);
449 if (pos == string::npos) { //not bad_alloc
450 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.");
453 if (object == "cluster"){
454 mothurOut(" has occurred in the " + object + " class function " + function + ". This error indicates your computer is running out of memory. There are two common causes for this, file size and format.\n\nFile Size:\nThe cluster command loads your distance matrix into RAM, and your distance file is most likely too large to fit in RAM. There are two options to help with this. The first is to use a cutoff. By using a cutoff mothur will only load distances that are below the cutoff. If that is still not enough, there is a command called cluster.split, http://www.mothur.org/wiki/cluster.split which divides the distance matrix, and clusters the smaller pieces separately. You may also be able to reduce the size of the original distance matrix by using the commands outlined in the Schloss SOP, http://www.mothur.org/wiki/Schloss_SOP. \n\nWrong Format:\nThis error can be caused by trying to read a column formatted distance matrix using the phylip parameter. By default, the dist.seqs command generates a column formatted distance matrix. To make a phylip formatted matrix set the dist.seqs command parameter output to lt. \n\nIf you are uable to resolve the issue, please contact Pat Schloss at mothur.bugs@gmail.com, and be sure to include the mothur.logFile with your inquiry.");
455 }else if (object == "shhh.flows"){
456 mothurOut(" has occurred in the " + object + " class function " + function + ". This error indicates your computer is running out of memory. The shhh.flows command is very memory intensive. This error is most commonly caused by trying to process a dataset too large, using multiple processors, or failing to run trim.flows before shhh.flows. If you are running our 32bit version, your memory usage is limited to 4G. If you have more than 4G of RAM and are running a 64bit OS, using our 64bit version may resolve your issue. If you are using multiple processors, try running the command with processors=1, the more processors you use the more memory is required. Running trim.flows with an oligos file, and then shhh.flows with the file option may also resolve the issue. If for some reason you are unable to run shhh.flows with your data, a good alternative is to use the trim.seqs command using a 50-bp sliding window and to trim the sequence when the average quality score over that window drops below 35. Our results suggest that the sequencing error rates by this method are very good, but not quite as good as by shhh.flows and that the resulting sequences tend to be a bit shorter. If you are uable to resolve the issue, please contact Pat Schloss at mothur.bugs@gmail.com, and be sure to include the mothur.logFile with your inquiry. ");
458 mothurOut(" has occurred in the " + object + " class function " + function + ". This error indicates your computer is running out of memory. This is most commonly caused by trying to process a dataset too large, using multiple processors, or a file format issue. If you are running our 32bit version, your memory usage is limited to 4G. If you have more than 4G of RAM and are running a 64bit OS, using our 64bit version may resolve your issue. If you are using multiple processors, try running the command with processors=1, the more processors you use the more memory is required. Also, you may be able to reduce the size of your dataset by using the commands outlined in the Schloss SOP, http://www.mothur.org/wiki/Schloss_SOP. If you are uable to resolve the issue, please contact Pat Schloss at mothur.bugs@gmail.com, and be sure to include the mothur.logFile with your inquiry.");
462 /*********************************************************************************************/
463 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
464 // process_mem_usage(double &, double &) - takes two doubles by reference,
465 // attempts to read the system-dependent data for a process' virtual memory
466 // size and resident set size, and return the results in KB.
468 // On failure, returns 0.0, 0.0
469 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
470 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
475 // 'file' stat seems to give the most reliable results
477 ifstream stat_stream("/proc/self/stat",ios_base::in);
479 // dummy vars for leading entries in stat that we don't care about
481 string pid, comm, state, ppid, pgrp, session, tty_nr;
482 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
483 string utime, stime, cutime, cstime, priority, nice;
484 string O, itrealvalue, starttime;
486 // the two fields we want
491 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
492 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
493 >> utime >> stime >> cutime >> cstime >> priority >> nice
494 >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
496 long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
497 vm_usage = vsize / 1024.0;
498 resident_set = rss * page_size_kb;
500 mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
504 /* //windows memory usage
505 // Get the list of process identifiers.
506 DWORD aProcesses[1024], cbNeeded, cProcesses;
508 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
510 // Calculate how many process identifiers were returned.
511 cProcesses = cbNeeded / sizeof(DWORD);
513 // Print the memory usage for each process
514 for (int i = 0; i < cProcesses; i++ ) {
515 DWORD processID = aProcesses[i];
517 PROCESS_MEMORY_COUNTERS pmc;
519 HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
521 // Print the process identifier.
522 printf( "\nProcess ID: %u\n", processID);
524 if (NULL != hProcess) {
526 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
527 printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
528 printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
529 printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
530 printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
531 printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
532 printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
533 printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
534 printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
535 printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
537 CloseHandle(hProcess);
547 /***********************************************************************/
548 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
550 fileName = getFullPathName(fileName);
552 fileHandle.open(fileName.c_str(), ios::app);
554 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
561 catch(exception& e) {
562 errorOut(e, "MothurOut", "openOutputFileAppend");
566 /***********************************************************************/
567 void MothurOut::gobble(istream& f){
571 while(isspace(d=f.get())) { ;}
572 if(!f.eof()) { f.putback(d); }
574 catch(exception& e) {
575 errorOut(e, "MothurOut", "gobble");
579 /***********************************************************************/
580 void MothurOut::gobble(istringstream& f){
583 while(isspace(d=f.get())) {;}
584 if(!f.eof()) { f.putback(d); }
586 catch(exception& e) {
587 errorOut(e, "MothurOut", "gobble");
592 /***********************************************************************/
594 string MothurOut::getline(istringstream& fileHandle) {
599 while (!fileHandle.eof()) {
601 char c = fileHandle.get();
603 //are you at the end of the line
604 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
611 catch(exception& e) {
612 errorOut(e, "MothurOut", "getline");
616 /***********************************************************************/
618 string MothurOut::getline(ifstream& fileHandle) {
625 char c = fileHandle.get();
627 //are you at the end of the line
628 if ((c == '\n') || (c == '\r') || (c == '\f') || (c == EOF)){ break; }
635 catch(exception& e) {
636 errorOut(e, "MothurOut", "getline");
640 /***********************************************************************/
642 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
643 #ifdef USE_COMPRESSION
644 inline bool endsWith(string s, const char * suffix){
645 size_t suffixLength = strlen(suffix);
646 return s.size() >= suffixLength && s.substr(s.size() - suffixLength, suffixLength).compare(suffix) == 0;
651 string MothurOut::getRootName(string longName){
654 string rootName = longName;
656 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
657 #ifdef USE_COMPRESSION
658 if (endsWith(rootName, ".gz") || endsWith(rootName, ".bz2")) {
659 int pos = rootName.find_last_of('.');
660 rootName = rootName.substr(0, pos);
661 cerr << "shortening " << longName << " to " << rootName << "\n";
665 if(rootName.find_last_of(".") != rootName.npos){
666 int pos = rootName.find_last_of('.')+1;
667 rootName = rootName.substr(0, pos);
672 catch(exception& e) {
673 errorOut(e, "MothurOut", "getRootName");
677 /***********************************************************************/
679 string MothurOut::getSimpleName(string longName){
681 string simpleName = longName;
684 found=longName.find_last_of("/\\");
686 if(found != longName.npos){
687 simpleName = longName.substr(found+1);
692 catch(exception& e) {
693 errorOut(e, "MothurOut", "getSimpleName");
698 /***********************************************************************/
700 int MothurOut::getRandomIndex(int highest){
703 int random = (int) ((float)(highest+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
707 catch(exception& e) {
708 errorOut(e, "MothurOut", "getRandomIndex");
713 /**********************************************************************/
715 string MothurOut::getPathName(string longName){
717 string rootPathName = longName;
719 if(longName.find_last_of("/\\") != longName.npos){
720 int pos = longName.find_last_of("/\\")+1;
721 rootPathName = longName.substr(0, pos);
726 catch(exception& e) {
727 errorOut(e, "MothurOut", "getPathName");
732 /***********************************************************************/
734 bool MothurOut::dirCheck(string& dirName){
740 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
745 //add / to name if needed
746 string lastChar = dirName.substr(dirName.length()-1);
747 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
748 if (lastChar != "/") { dirName += "/"; }
750 if (lastChar != "\\") { dirName += "\\"; }
753 //test to make sure directory exists
754 dirName = getFullPathName(dirName);
755 string outTemp = dirName + tag + "temp";
757 out.open(outTemp.c_str(), ios::trunc);
759 mothurOut(dirName + " directory does not exist or is not writable."); mothurOutEndLine();
762 mothurRemove(outTemp);
768 catch(exception& e) {
769 errorOut(e, "MothurOut", "dirCheck");
774 /***********************************************************************/
776 string MothurOut::hasPath(string longName){
781 found=longName.find_last_of("~/\\");
783 if(found != longName.npos){
784 path = longName.substr(0, found+1);
789 catch(exception& e) {
790 errorOut(e, "MothurOut", "hasPath");
795 /***********************************************************************/
797 string MothurOut::getExtension(string longName){
799 string extension = "";
801 if(longName.find_last_of('.') != longName.npos){
802 int pos = longName.find_last_of('.');
803 extension = longName.substr(pos, longName.length());
808 catch(exception& e) {
809 errorOut(e, "MothurOut", "getExtension");
813 /***********************************************************************/
814 bool MothurOut::isBlank(string fileName){
817 fileName = getFullPathName(fileName);
820 fileHandle.open(fileName.c_str());
822 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
825 //check for blank file
827 if (fileHandle.eof()) { fileHandle.close(); return true; }
832 catch(exception& e) {
833 errorOut(e, "MothurOut", "isBlank");
837 /***********************************************************************/
839 string MothurOut::getFullPathName(string fileName){
842 string path = hasPath(fileName);
846 if (path == "") { return fileName; } //its a simple name
847 else { //we need to complete the pathname
848 // ex. ../../../filename
849 // cwd = /user/work/desktop
852 //get current working directory
853 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
855 if (path.find("~") != -1) { //go to home directory
858 char *homepath = NULL;
859 homepath = getenv ("HOME");
860 if ( homepath != NULL) { homeDir = homepath; }
861 else { homeDir = ""; }
863 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
866 if (path.rfind("./") == string::npos) { return fileName; } //already complete name
867 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
869 //char* cwdpath = new char[1024];
871 //cwdpath=getcwd(cwdpath,size);
874 char *cwdpath = NULL;
875 cwdpath = getcwd(NULL, 0); // or _getcwd
876 if ( cwdpath != NULL) { cwd = cwdpath; }
882 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
884 //break apart the current working directory
886 while (simpleCWD.find_first_of('/') != string::npos) {
887 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
888 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
891 //get last one // ex. ../../../filename = /user/work/desktop/filename
892 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
895 int index = dirs.size()-1;
897 while((pos = path.rfind("./")) != string::npos) { //while you don't have a complete path
898 if (pos == 0) { break; //you are at the end
899 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
900 path = path.substr(0, pos-1);
902 if (index == 0) { break; }
903 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
904 path = path.substr(0, pos);
905 }else if (pos == 1) { break; //you are at the end
906 }else { mothurOut("cannot resolve path for " + fileName + "\n"); return fileName; }
909 for (int i = index; i >= 0; i--) {
910 newFileName = dirs[i] + "/" + newFileName;
913 newFileName = "/" + newFileName;
917 if (path.find("~") != string::npos) { //go to home directory
918 string homeDir = getenv ("HOMEPATH");
919 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
922 if (path.rfind(".\\") == string::npos) { return fileName; } //already complete name
923 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
925 char *cwdpath = NULL;
926 cwdpath = getcwd(NULL, 0); // or _getcwd
927 if ( cwdpath != NULL) { cwd = cwdpath; }
930 //break apart the current working directory
932 while (cwd.find_first_of('\\') != -1) {
933 string dir = cwd.substr(0,cwd.find_first_of('\\'));
934 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
939 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
941 int index = dirs.size()-1;
943 while((pos = path.rfind(".\\")) != string::npos) { //while you don't have a complete path
944 if (pos == 0) { break; //you are at the end
945 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
946 path = path.substr(0, pos-1);
948 if (index == 0) { break; }
949 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
950 path = path.substr(0, pos);
951 }else if (pos == 1) { break; //you are at the end
952 }else { mothurOut("cannot resolve path for " + fileName + "\n"); return fileName; }
955 for (int i = index; i >= 0; i--) {
956 newFileName = dirs[i] + "\\" + newFileName;
965 catch(exception& e) {
966 errorOut(e, "MothurOut", "getFullPathName");
970 /***********************************************************************/
972 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
975 string completeFileName = getFullPathName(fileName);
976 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
977 #ifdef USE_COMPRESSION
978 // check for gzipped or bzipped file
979 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
980 string tempName = string(tmpnam(0));
981 mkfifo(tempName.c_str(), 0666);
982 int fork_result = fork();
983 if (fork_result < 0) {
984 cerr << "Error forking.\n";
986 } else if (fork_result == 0) {
987 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
988 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
989 system(command.c_str());
990 cerr << "Done decompressing " << completeFileName << "\n";
991 mothurRemove(tempName);
994 cerr << "waiting on child process " << fork_result << "\n";
995 completeFileName = tempName;
1000 fileHandle.open(completeFileName.c_str());
1002 //mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1005 //check for blank file
1010 catch(exception& e) {
1011 errorOut(e, "MothurOut", "openInputFile - no Error");
1015 /***********************************************************************/
1017 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
1020 //get full path name
1021 string completeFileName = getFullPathName(fileName);
1022 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1023 #ifdef USE_COMPRESSION
1024 // check for gzipped or bzipped file
1025 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
1026 string tempName = string(tmpnam(0));
1027 mkfifo(tempName.c_str(), 0666);
1028 int fork_result = fork();
1029 if (fork_result < 0) {
1030 cerr << "Error forking.\n";
1032 } else if (fork_result == 0) {
1033 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
1034 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
1035 system(command.c_str());
1036 cerr << "Done decompressing " << completeFileName << "\n";
1037 mothurRemove(tempName);
1040 cerr << "waiting on child process " << fork_result << "\n";
1041 completeFileName = tempName;
1047 fileHandle.open(completeFileName.c_str());
1049 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1053 //check for blank file
1055 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
1060 catch(exception& e) {
1061 errorOut(e, "MothurOut", "openInputFile");
1065 /***********************************************************************/
1067 int MothurOut::renameFile(string oldName, string newName){
1070 if (oldName == newName) { return 0; }
1073 int exist = openInputFile(newName, inTest, "");
1076 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1077 if (exist == 0) { //you could open it so you want to delete it
1078 string command = "rm " + newName;
1079 system(command.c_str());
1082 string command = "mv " + oldName + " " + newName;
1083 system(command.c_str());
1085 mothurRemove(newName);
1086 int renameOk = rename(oldName.c_str(), newName.c_str());
1091 catch(exception& e) {
1092 errorOut(e, "MothurOut", "renameFile");
1097 /***********************************************************************/
1099 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
1102 string completeFileName = getFullPathName(fileName);
1103 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1104 #ifdef USE_COMPRESSION
1105 // check for gzipped file
1106 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
1107 string tempName = string(tmpnam(0));
1108 mkfifo(tempName.c_str(), 0666);
1109 cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
1110 int fork_result = fork();
1111 if (fork_result < 0) {
1112 cerr << "Error forking.\n";
1114 } else if (fork_result == 0) {
1115 string command = string(endsWith(completeFileName, ".gz") ? "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
1116 system(command.c_str());
1119 completeFileName = tempName;
1124 fileHandle.open(completeFileName.c_str(), ios::trunc);
1126 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1133 catch(exception& e) {
1134 errorOut(e, "MothurOut", "openOutputFile");
1140 /**************************************************************************************************/
1141 int MothurOut::appendFiles(string temp, string filename) {
1146 //open output file in append mode
1147 openOutputFileAppend(filename, output);
1148 int ableToOpen = openInputFile(temp, input, "no error");
1149 //int ableToOpen = openInputFile(temp, input);
1152 if (ableToOpen == 0) { //you opened it
1155 while (!input.eof()) {
1156 input.read(buffer, 4096);
1157 output.write(buffer, input.gcount());
1158 //count number of lines
1159 for (int i = 0; i < input.gcount(); i++) { if (buffer[i] == '\n') {numLines++;} }
1168 catch(exception& e) {
1169 errorOut(e, "MothurOut", "appendFiles");
1173 /**************************************************************************************************/
1174 int MothurOut::appendFilesWithoutHeaders(string temp, string filename) {
1179 //open output file in append mode
1180 openOutputFileAppend(filename, output);
1181 int ableToOpen = openInputFile(temp, input, "no error");
1182 //int ableToOpen = openInputFile(temp, input);
1185 if (ableToOpen == 0) { //you opened it
1187 string headers = getline(input); gobble(input);
1188 if (debug) { mothurOut("[DEBUG]: skipping headers " + headers +'\n'); }
1191 while (!input.eof()) {
1192 input.read(buffer, 4096);
1193 output.write(buffer, input.gcount());
1194 //count number of lines
1195 for (int i = 0; i < input.gcount(); i++) { if (buffer[i] == '\n') {numLines++;} }
1204 catch(exception& e) {
1205 errorOut(e, "MothurOut", "appendFiles");
1209 /**************************************************************************************************/
1210 string MothurOut::sortFile(string distFile, string outputDir){
1213 //if (outputDir == "") { outputDir += hasPath(distFile); }
1214 string outfile = getRootName(distFile) + "sorted.dist";
1217 //if you can, use the unix sort since its been optimized for years
1218 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1219 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
1220 system(command.c_str());
1221 #else //you are stuck with my best attempt...
1222 //windows sort does not have a way to specify a column, only a character in the line
1223 //since we cannot assume that the distance will always be at the the same character location on each line
1224 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
1226 //read in file line by file and put distance first
1227 string tempDistFile = distFile + ".temp";
1230 openInputFile(distFile, input);
1231 openOutputFile(tempDistFile, output);
1233 string firstName, secondName;
1235 while (!input.eof()) {
1236 input >> firstName >> secondName >> dist;
1237 output << dist << '\t' << firstName << '\t' << secondName << endl;
1244 //sort using windows sort
1245 string tempOutfile = outfile + ".temp";
1246 string command = "sort " + tempDistFile + " /O " + tempOutfile;
1247 system(command.c_str());
1249 //read in sorted file and put distance at end again
1252 openInputFile(tempOutfile, input2);
1253 openOutputFile(outfile, output2);
1255 while (!input2.eof()) {
1256 input2 >> dist >> firstName >> secondName;
1257 output2 << firstName << '\t' << secondName << '\t' << dist << endl;
1264 mothurRemove(tempDistFile);
1265 mothurRemove(tempOutfile);
1270 catch(exception& e) {
1271 errorOut(e, "MothurOut", "sortFile");
1275 /**************************************************************************************************/
1276 vector<unsigned long long> MothurOut::setFilePosFasta(string filename, int& num) {
1278 vector<unsigned long long> positions;
1280 //openInputFile(filename, inFASTA);
1281 inFASTA.open(filename.c_str(), ios::binary);
1284 unsigned long long count = 0;
1285 while(!inFASTA.eof()){
1286 //input = getline(inFASTA);
1287 //cout << input << '\t' << inFASTA.tellg() << endl;
1288 //if (input.length() != 0) {
1289 // if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); cout << (pos - input.length() - 1) << endl; }
1291 //gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
1292 char c = inFASTA.get(); count++;
1294 positions.push_back(count-1);
1295 //cout << count << endl;
1300 num = positions.size();
1305 //get num bytes in file
1306 pFile = fopen (filename.c_str(),"rb");
1307 if (pFile==NULL) perror ("Error opening file");
1309 fseek (pFile, 0, SEEK_END);
1314 unsigned long long size = positions[(positions.size()-1)];
1316 openInputFile(filename, in);
1321 if(in.eof()) { break; }
1326 positions.push_back(size);
1331 catch(exception& e) {
1332 errorOut(e, "MothurOut", "setFilePosFasta");
1336 /**************************************************************************************************/
1337 vector<unsigned long long> MothurOut::setFilePosEachLine(string filename, int& num) {
1339 filename = getFullPathName(filename);
1341 vector<unsigned long long> positions;
1343 //openInputFile(filename, in);
1344 in.open(filename.c_str(), ios::binary);
1347 unsigned long long count = 0;
1348 positions.push_back(0);
1351 //getline counting reads
1352 char d = in.get(); count++;
1353 while ((d != '\n') && (d != '\r') && (d != '\f') && (d != in.eof())) {
1354 //get next character
1360 d=in.get(); count++;
1361 while(isspace(d) && (d != in.eof())) { d=in.get(); count++;}
1363 positions.push_back(count-1);
1364 //cout << count-1 << endl;
1368 num = positions.size()-1;
1371 unsigned long long size;
1373 //get num bytes in file
1374 pFile = fopen (filename.c_str(),"rb");
1375 if (pFile==NULL) perror ("Error opening file");
1377 fseek (pFile, 0, SEEK_END);
1382 positions[(positions.size()-1)] = size;
1386 catch(exception& e) {
1387 errorOut(e, "MothurOut", "setFilePosEachLine");
1391 /**************************************************************************************************/
1393 vector<unsigned long long> MothurOut::divideFile(string filename, int& proc) {
1395 vector<unsigned long long> filePos;
1396 filePos.push_back(0);
1399 unsigned long long size;
1401 filename = getFullPathName(filename);
1403 //get num bytes in file
1404 pFile = fopen (filename.c_str(),"rb");
1405 if (pFile==NULL) perror ("Error opening file");
1407 fseek (pFile, 0, SEEK_END);
1412 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1414 //estimate file breaks
1415 unsigned long long chunkSize = 0;
1416 chunkSize = size / proc;
1418 //file to small to divide by processors
1419 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1421 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1422 for (int i = 0; i < proc; i++) {
1423 unsigned long long spot = (i+1) * chunkSize;
1426 openInputFile(filename, in);
1430 unsigned long long newSpot = spot;
1434 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1435 else if (int(c) == -1) { break; }
1439 //there was not another sequence before the end of the file
1440 unsigned long long sanityPos = in.tellg();
1442 if (sanityPos == -1) { break; }
1443 else { filePos.push_back(newSpot); }
1449 filePos.push_back(size);
1451 //sanity check filePos
1452 for (int i = 0; i < (filePos.size()-1); i++) {
1453 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1456 proc = (filePos.size() - 1);
1458 mothurOut("[ERROR]: Windows version should not be calling the divideFile function."); mothurOutEndLine();
1460 filePos.push_back(size);
1464 catch(exception& e) {
1465 errorOut(e, "MothurOut", "divideFile");
1469 /**************************************************************************************************/
1471 vector<unsigned long long> MothurOut::divideFilePerLine(string filename, int& proc) {
1473 vector<unsigned long long> filePos;
1474 filePos.push_back(0);
1477 unsigned long long size;
1479 filename = getFullPathName(filename);
1481 //get num bytes in file
1482 pFile = fopen (filename.c_str(),"rb");
1483 if (pFile==NULL) perror ("Error opening file");
1485 fseek (pFile, 0, SEEK_END);
1490 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1492 //estimate file breaks
1493 unsigned long long chunkSize = 0;
1494 chunkSize = size / proc;
1496 //file to small to divide by processors
1497 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1499 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1500 for (int i = 0; i < proc; i++) {
1501 unsigned long long spot = (i+1) * chunkSize;
1504 openInputFile(filename, in);
1507 //look for next line break
1508 unsigned long long newSpot = spot;
1512 if ((c == '\n') || (c == '\r') || (c == '\f')) { gobble(in); newSpot = in.tellg(); break; }
1513 else if (int(c) == -1) { break; }
1516 //there was not another line before the end of the file
1517 unsigned long long sanityPos = in.tellg();
1519 if (sanityPos == -1) { break; }
1520 else { filePos.push_back(newSpot); }
1526 filePos.push_back(size);
1528 //sanity check filePos
1529 for (int i = 0; i < (filePos.size()-1); i++) {
1530 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1533 proc = (filePos.size() - 1);
1535 mothurOut("[ERROR]: Windows version should not be calling the divideFile function."); mothurOutEndLine();
1537 filePos.push_back(size);
1541 catch(exception& e) {
1542 errorOut(e, "MothurOut", "divideFile");
1546 /**************************************************************************************************/
1547 int MothurOut::divideFile(string filename, int& proc, vector<string>& files) {
1550 vector<unsigned long long> filePos = divideFile(filename, proc);
1552 for (int i = 0; i < (filePos.size()-1); i++) {
1556 openInputFile(filename, in);
1557 in.seekg(filePos[i]);
1558 unsigned long long size = filePos[(i+1)] - filePos[i];
1559 char* chunk = new char[size];
1560 in.read(chunk, size);
1564 string fileChunkName = filename + "." + toString(i) + ".tmp";
1566 openOutputFile(fileChunkName, out);
1568 out << chunk << endl;
1573 files.push_back(fileChunkName);
1578 catch(exception& e) {
1579 errorOut(e, "MothurOut", "divideFile");
1583 /***********************************************************************/
1585 bool MothurOut::isTrue(string f){
1588 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1590 if ((f == "TRUE") || (f == "T")) { return true; }
1591 else { return false; }
1593 catch(exception& e) {
1594 errorOut(e, "MothurOut", "isTrue");
1599 /***********************************************************************/
1601 float MothurOut::roundDist(float dist, int precision){
1603 return int(dist * precision + 0.5)/float(precision);
1605 catch(exception& e) {
1606 errorOut(e, "MothurOut", "roundDist");
1610 /***********************************************************************/
1612 float MothurOut::ceilDist(float dist, int precision){
1614 return int(ceil(dist * precision))/float(precision);
1616 catch(exception& e) {
1617 errorOut(e, "MothurOut", "ceilDist");
1621 /***********************************************************************/
1623 vector<string> MothurOut::splitWhiteSpace(string& rest, char buffer[], int size){
1625 vector<string> pieces;
1627 for (int i = 0; i < size; i++) {
1628 if (!isspace(buffer[i])) { rest += buffer[i]; }
1630 if (rest != "") { pieces.push_back(rest); rest = ""; }
1631 while (i < size) { //gobble white space
1632 if (isspace(buffer[i])) { i++; }
1633 else { rest = buffer[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1640 catch(exception& e) {
1641 errorOut(e, "MothurOut", "splitWhiteSpace");
1645 /***********************************************************************/
1646 vector<string> MothurOut::splitWhiteSpace(string input){
1648 vector<string> pieces;
1651 for (int i = 0; i < input.length(); i++) {
1652 if (!isspace(input[i])) { rest += input[i]; }
1654 if (rest != "") { pieces.push_back(rest); rest = ""; }
1655 while (i < input.length()) { //gobble white space
1656 if (isspace(input[i])) { i++; }
1657 else { rest = input[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1662 if (rest != "") { pieces.push_back(rest); }
1666 catch(exception& e) {
1667 errorOut(e, "MothurOut", "splitWhiteSpace");
1671 /***********************************************************************/
1672 vector<string> MothurOut::splitWhiteSpaceWithQuotes(string input){
1674 vector<string> pieces;
1677 int pos = input.find('\'');
1678 int pos2 = input.find('\"');
1680 if ((pos == string::npos) && (pos2 == string::npos)) { return splitWhiteSpace(input); } //no quotes to worry about
1682 for (int i = 0; i < input.length(); i++) {
1683 if ((input[i] == '\'') || (input[i] == '\"') || (rest == "\'") || (rest == "\"")) { //grab everything til end or next ' or "
1685 for (int j = i+1; j < input.length(); j++) {
1686 if ((input[j] == '\'') || (input[j] == '\"')) { //then quit
1690 }else { rest += input[j]; }
1692 }else if (!isspace(input[i])) { rest += input[i]; }
1694 if (rest != "") { pieces.push_back(rest); rest = ""; }
1695 while (i < input.length()) { //gobble white space
1696 if (isspace(input[i])) { i++; }
1697 else { rest = input[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1702 if (rest != "") { pieces.push_back(rest); }
1706 catch(exception& e) {
1707 errorOut(e, "MothurOut", "splitWhiteSpace");
1711 //**********************************************************************************************************************
1712 int MothurOut::readTax(string namefile, map<string, string>& taxMap) {
1716 openInputFile(namefile, in);
1720 bool pairDone = false;
1721 bool columnOne = true;
1722 string firstCol, secondCol;
1725 if (control_pressed) { break; }
1727 in.read(buffer, 4096);
1728 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1730 for (int i = 0; i < pieces.size(); i++) {
1731 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1732 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1735 checkName(firstCol);
1736 //are there confidence scores, if so remove them
1737 if (secondCol.find_first_of('(') != -1) { removeConfidences(secondCol); }
1738 map<string, string>::iterator itTax = taxMap.find(firstCol);
1740 if(itTax == taxMap.end()) {
1741 bool ignore = false;
1742 if (secondCol != "") { if (secondCol[secondCol.length()-1] != ';') { mothurOut("[ERROR]: " + firstCol + " is missing the final ';', ignoring.\n"); ignore=true; }
1744 if (!ignore) { taxMap[firstCol] = secondCol; }
1745 if (debug) { mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n"); }
1747 mothurOut("[ERROR]: " + firstCol + " is already in your taxonomy file, names must be unique./n"); control_pressed = true;
1756 vector<string> pieces = splitWhiteSpace(rest);
1758 for (int i = 0; i < pieces.size(); i++) {
1759 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1760 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1763 checkName(firstCol);
1764 //are there confidence scores, if so remove them
1765 if (secondCol.find_first_of('(') != -1) { removeConfidences(secondCol); }
1766 map<string, string>::iterator itTax = taxMap.find(firstCol);
1768 if(itTax == taxMap.end()) {
1769 bool ignore = false;
1770 if (secondCol != "") { if (secondCol[secondCol.length()-1] != ';') { mothurOut("[ERROR]: " + firstCol + " is missing the final ';', ignoring.\n"); ignore=true; }
1772 if (!ignore) { taxMap[firstCol] = secondCol; }
1773 if (debug) { mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n"); }
1775 mothurOut("[ERROR]: " + firstCol + " is already in your taxonomy file, names must be unique./n"); control_pressed = true;
1783 return taxMap.size();
1786 catch(exception& e) {
1787 errorOut(e, "MothurOut", "readTax");
1791 /**********************************************************************************************************************/
1792 int MothurOut::readNames(string namefile, map<string, string>& nameMap, bool redund) {
1796 openInputFile(namefile, in);
1800 bool pairDone = false;
1801 bool columnOne = true;
1802 string firstCol, secondCol;
1805 if (control_pressed) { break; }
1807 in.read(buffer, 4096);
1808 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1810 for (int i = 0; i < pieces.size(); i++) {
1811 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1812 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1815 checkName(firstCol);
1816 checkName(secondCol);
1818 //parse names into vector
1819 vector<string> theseNames;
1820 splitAtComma(secondCol, theseNames);
1821 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1829 vector<string> pieces = splitWhiteSpace(rest);
1831 for (int i = 0; i < pieces.size(); i++) {
1832 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1833 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1836 checkName(firstCol);
1837 checkName(secondCol);
1839 //parse names into vector
1840 vector<string> theseNames;
1841 splitAtComma(secondCol, theseNames);
1842 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1848 return nameMap.size();
1851 catch(exception& e) {
1852 errorOut(e, "MothurOut", "readNames");
1856 /**********************************************************************************************************************/
1857 int MothurOut::readNames(string namefile, map<string, string>& nameMap, int flip) {
1861 openInputFile(namefile, in);
1865 bool pairDone = false;
1866 bool columnOne = true;
1867 string firstCol, secondCol;
1870 if (control_pressed) { break; }
1872 in.read(buffer, 4096);
1873 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1875 for (int i = 0; i < pieces.size(); i++) {
1876 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1877 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1880 checkName(firstCol);
1881 checkName(secondCol);
1882 nameMap[secondCol] = firstCol;
1890 vector<string> pieces = splitWhiteSpace(rest);
1892 for (int i = 0; i < pieces.size(); i++) {
1893 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1894 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1897 checkName(firstCol);
1898 checkName(secondCol);
1899 nameMap[secondCol] = firstCol;
1905 return nameMap.size();
1908 catch(exception& e) {
1909 errorOut(e, "MothurOut", "readNames");
1913 /**********************************************************************************************************************/
1914 int MothurOut::readNames(string namefile, map<string, string>& nameMap, map<string, int>& nameCount) {
1916 nameMap.clear(); nameCount.clear();
1919 openInputFile(namefile, in);
1923 bool pairDone = false;
1924 bool columnOne = true;
1925 string firstCol, secondCol;
1928 if (control_pressed) { break; }
1930 in.read(buffer, 4096);
1931 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1933 for (int i = 0; i < pieces.size(); i++) {
1934 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1935 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1938 checkName(firstCol);
1939 checkName(secondCol);
1940 //parse names into vector
1941 vector<string> theseNames;
1942 splitAtComma(secondCol, theseNames);
1943 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1944 nameCount[firstCol] = theseNames.size();
1952 vector<string> pieces = splitWhiteSpace(rest);
1954 for (int i = 0; i < pieces.size(); i++) {
1955 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1956 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1959 checkName(firstCol);
1960 checkName(secondCol);
1961 //parse names into vector
1962 vector<string> theseNames;
1963 splitAtComma(secondCol, theseNames);
1964 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1965 nameCount[firstCol] = theseNames.size();
1971 return nameMap.size();
1974 catch(exception& e) {
1975 errorOut(e, "MothurOut", "readNames");
1979 /**********************************************************************************************************************/
1980 int MothurOut::readNames(string namefile, map<string, string>& nameMap) {
1984 openInputFile(namefile, in);
1988 bool pairDone = false;
1989 bool columnOne = true;
1990 string firstCol, secondCol;
1993 if (control_pressed) { break; }
1995 in.read(buffer, 4096);
1996 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1998 for (int i = 0; i < pieces.size(); i++) {
1999 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2000 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2003 checkName(firstCol);
2004 checkName(secondCol);
2005 nameMap[firstCol] = secondCol; pairDone = false; }
2011 vector<string> pieces = splitWhiteSpace(rest);
2013 for (int i = 0; i < pieces.size(); i++) {
2014 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2015 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2018 checkName(firstCol);
2019 checkName(secondCol);
2020 nameMap[firstCol] = secondCol; pairDone = false; }
2024 return nameMap.size();
2027 catch(exception& e) {
2028 errorOut(e, "MothurOut", "readNames");
2032 /**********************************************************************************************************************/
2033 int MothurOut::readNames(string namefile, map<string, vector<string> >& nameMap) {
2037 openInputFile(namefile, in);
2041 bool pairDone = false;
2042 bool columnOne = true;
2043 string firstCol, secondCol;
2046 if (control_pressed) { break; }
2048 in.read(buffer, 4096);
2049 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2051 for (int i = 0; i < pieces.size(); i++) {
2052 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2053 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2056 checkName(firstCol);
2057 checkName(secondCol);
2058 vector<string> temp;
2059 splitAtComma(secondCol, temp);
2060 nameMap[firstCol] = temp;
2068 vector<string> pieces = splitWhiteSpace(rest);
2070 for (int i = 0; i < pieces.size(); i++) {
2071 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2072 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2075 checkName(firstCol);
2076 checkName(secondCol);
2077 vector<string> temp;
2078 splitAtComma(secondCol, temp);
2079 nameMap[firstCol] = temp;
2085 return nameMap.size();
2087 catch(exception& e) {
2088 errorOut(e, "MothurOut", "readNames");
2092 /**********************************************************************************************************************/
2093 map<string, int> MothurOut::readNames(string namefile) {
2095 map<string, int> nameMap;
2099 openInputFile(namefile, in);
2103 bool pairDone = false;
2104 bool columnOne = true;
2105 string firstCol, secondCol;
2108 if (control_pressed) { break; }
2110 in.read(buffer, 4096);
2111 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2113 for (int i = 0; i < pieces.size(); i++) {
2114 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2115 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2118 checkName(firstCol);
2119 checkName(secondCol);
2120 int num = getNumNames(secondCol);
2121 nameMap[firstCol] = num;
2129 vector<string> pieces = splitWhiteSpace(rest);
2130 for (int i = 0; i < pieces.size(); i++) {
2131 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2132 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2135 checkName(firstCol);
2136 checkName(secondCol);
2137 int num = getNumNames(secondCol);
2138 nameMap[firstCol] = num;
2147 catch(exception& e) {
2148 errorOut(e, "MothurOut", "readNames");
2152 /**********************************************************************************************************************/
2153 map<string, int> MothurOut::readNames(string namefile, unsigned long int& numSeqs) {
2155 map<string, int> nameMap;
2160 openInputFile(namefile, in);
2164 bool pairDone = false;
2165 bool columnOne = true;
2166 string firstCol, secondCol;
2169 if (control_pressed) { break; }
2171 in.read(buffer, 4096);
2172 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2174 for (int i = 0; i < pieces.size(); i++) {
2175 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2176 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2179 checkName(firstCol);
2180 checkName(secondCol);
2181 int num = getNumNames(secondCol);
2182 nameMap[firstCol] = num;
2191 vector<string> pieces = splitWhiteSpace(rest);
2192 for (int i = 0; i < pieces.size(); i++) {
2193 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2194 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2197 checkName(firstCol);
2198 checkName(secondCol);
2199 int num = getNumNames(secondCol);
2200 nameMap[firstCol] = num;
2210 catch(exception& e) {
2211 errorOut(e, "MothurOut", "readNames");
2215 /************************************************************/
2216 int MothurOut::checkName(string& name) {
2218 for (int i = 0; i < name.length(); i++) {
2219 if (name[i] == ':') { name[i] = '_'; changedSeqNames = true; }
2223 catch(exception& e) {
2224 errorOut(e, "MothurOut", "checkName");
2228 /**********************************************************************************************************************/
2229 int MothurOut::readNames(string namefile, vector<seqPriorityNode>& nameVector, map<string, string>& fastamap) {
2235 openInputFile(namefile, in);
2239 bool pairDone = false;
2240 bool columnOne = true;
2241 string firstCol, secondCol;
2244 if (control_pressed) { break; }
2246 in.read(buffer, 4096);
2247 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2249 for (int i = 0; i < pieces.size(); i++) {
2250 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2251 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2254 checkName(firstCol);
2255 checkName(secondCol);
2256 int num = getNumNames(secondCol);
2258 map<string, string>::iterator it = fastamap.find(firstCol);
2259 if (it == fastamap.end()) {
2261 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
2263 seqPriorityNode temp(num, it->second, firstCol);
2264 nameVector.push_back(temp);
2274 vector<string> pieces = splitWhiteSpace(rest);
2276 for (int i = 0; i < pieces.size(); i++) {
2277 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2278 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2281 checkName(firstCol);
2282 checkName(secondCol);
2283 int num = getNumNames(secondCol);
2285 map<string, string>::iterator it = fastamap.find(firstCol);
2286 if (it == fastamap.end()) {
2288 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
2290 seqPriorityNode temp(num, it->second, firstCol);
2291 nameVector.push_back(temp);
2300 catch(exception& e) {
2301 errorOut(e, "MothurOut", "readNames");
2305 //**********************************************************************************************************************
2306 set<string> MothurOut::readAccnos(string accnosfile){
2310 openInputFile(accnosfile, in);
2317 if (control_pressed) { break; }
2319 in.read(buffer, 4096);
2320 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2322 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.insert(pieces[i]); }
2327 vector<string> pieces = splitWhiteSpace(rest);
2328 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.insert(pieces[i]); }
2332 catch(exception& e) {
2333 errorOut(e, "MothurOut", "readAccnos");
2337 //**********************************************************************************************************************
2338 int MothurOut::readAccnos(string accnosfile, vector<string>& names){
2342 openInputFile(accnosfile, in);
2349 if (control_pressed) { break; }
2351 in.read(buffer, 4096);
2352 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2354 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.push_back(pieces[i]); }
2359 vector<string> pieces = splitWhiteSpace(rest);
2360 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.push_back(pieces[i]); }
2365 catch(exception& e) {
2366 errorOut(e, "MothurOut", "readAccnos");
2370 /***********************************************************************/
2372 int MothurOut::getNumNames(string names){
2378 for(int i=0;i<names.size();i++){
2379 if(names[i] == ','){
2387 catch(exception& e) {
2388 errorOut(e, "MothurOut", "getNumNames");
2392 /***********************************************************************/
2394 int MothurOut::getNumChar(string line, char c){
2399 for(int i=0;i<line.size();i++){
2408 catch(exception& e) {
2409 errorOut(e, "MothurOut", "getNumChar");
2413 //**********************************************************************************************************************
2414 bool MothurOut::isSubset(vector<string> bigset, vector<string> subset) {
2418 if (subset.size() > bigset.size()) { return false; }
2420 //check if each guy in suset is also in bigset
2421 for (int i = 0; i < subset.size(); i++) {
2423 for (int j = 0; j < bigset.size(); j++) {
2424 if (subset[i] == bigset[j]) { match = true; break; }
2427 //you have a guy in subset that had no match in bigset
2428 if (match == false) { return false; }
2434 catch(exception& e) {
2435 errorOut(e, "MothurOut", "isSubset");
2439 /***********************************************************************/
2440 int MothurOut::mothurRemove(string filename){
2442 filename = getFullPathName(filename);
2443 int error = remove(filename.c_str());
2445 // if (errno != ENOENT) { //ENOENT == file does not exist
2446 // string message = "Error deleting file " + filename;
2447 // perror(message.c_str());
2452 catch(exception& e) {
2453 errorOut(e, "MothurOut", "mothurRemove");
2457 /***********************************************************************/
2458 bool MothurOut::mothurConvert(string item, int& num){
2462 if (isNumeric1(item)) {
2467 mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2468 commandInputsConvertError = true;
2473 catch(exception& e) {
2474 errorOut(e, "MothurOut", "mothurConvert");
2478 /***********************************************************************/
2479 bool MothurOut::mothurConvert(string item, intDist& num){
2483 if (isNumeric1(item)) {
2488 mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2489 commandInputsConvertError = true;
2494 catch(exception& e) {
2495 errorOut(e, "MothurOut", "mothurConvert");
2500 /***********************************************************************/
2501 bool MothurOut::isNumeric1(string stringToCheck){
2503 bool numeric = false;
2505 if(stringToCheck.find_first_not_of("0123456789.-") == string::npos) { numeric = true; }
2509 catch(exception& e) {
2510 errorOut(e, "MothurOut", "isNumeric1");
2515 /***********************************************************************/
2516 bool MothurOut::mothurConvert(string item, float& num){
2520 if (isNumeric1(item)) {
2525 mothurOut("[ERROR]: cannot convert " + item + " to a float."); mothurOutEndLine();
2526 commandInputsConvertError = true;
2531 catch(exception& e) {
2532 errorOut(e, "MothurOut", "mothurConvert");
2536 /***********************************************************************/
2537 bool MothurOut::mothurConvert(string item, double& num){
2541 if (isNumeric1(item)) {
2546 mothurOut("[ERROR]: cannot convert " + item + " to a double."); mothurOutEndLine();
2547 commandInputsConvertError = true;
2552 catch(exception& e) {
2553 errorOut(e, "MothurOut", "mothurConvert");
2557 /**************************************************************************************************/
2559 vector<vector<double> > MothurOut::binomial(int maxOrder){
2561 vector<vector<double> > binomial(maxOrder+1);
2563 for(int i=0;i<=maxOrder;i++){
2564 binomial[i].resize(maxOrder+1);
2573 for(int i=2;i<=maxOrder;i++){
2577 for(int i=2;i<=maxOrder;i++){
2578 for(int j=1;j<=maxOrder;j++){
2579 if(i==j){ binomial[i][j]=1; }
2580 if(j>i) { binomial[i][j]=0; }
2581 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
2588 catch(exception& e) {
2589 errorOut(e, "MothurOut", "binomial");
2593 /**************************************************************************************************/
2594 unsigned int MothurOut::fromBase36(string base36){
2596 unsigned int num = 0;
2598 map<char, int> converts;
2663 while (i < base36.length()) {
2665 num = 36 * num + converts[c];
2672 catch(exception& e) {
2673 errorOut(e, "MothurOut", "fromBase36");
2677 /***********************************************************************/
2679 int MothurOut::factorial(int num){
2683 for (int i = 1; i <= num; i++) {
2689 catch(exception& e) {
2690 errorOut(e, "MothurOut", "factorial");
2694 /***********************************************************************/
2696 int MothurOut::getNumSeqs(ifstream& file){
2698 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
2702 catch(exception& e) {
2703 errorOut(e, "MothurOut", "getNumSeqs");
2707 /***********************************************************************/
2708 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
2713 input = getline(file);
2714 if (input.length() != 0) {
2715 if(input[0] == '>'){ numSeqs++; }
2719 catch(exception& e) {
2720 errorOut(e, "MothurOut", "getNumSeqs");
2724 /***********************************************************************/
2726 //This function parses the estimator options and puts them in a vector
2727 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
2730 if (symbol == '-') { splitAtDash(estim, container); return; }
2732 string individual = "";
2733 int estimLength = estim.size();
2734 for(int i=0;i<estimLength;i++){
2735 if(estim[i] == symbol){
2736 container.push_back(individual);
2740 individual += estim[i];
2743 container.push_back(individual);
2746 catch(exception& e) {
2747 errorOut(e, "MothurOut", "splitAtChar");
2752 /***********************************************************************/
2754 //This function parses the estimator options and puts them in a vector
2755 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
2757 string individual = "";
2758 int estimLength = estim.size();
2759 bool prevEscape = false;
2760 /*for(int i=0;i<estimLength;i++){
2762 individual += estim[i];
2766 if(estim[i] == '\\'){
2769 else if(estim[i] == '-'){
2770 container.push_back(individual);
2775 individual += estim[i];
2782 for(int i=0;i<estimLength;i++){
2783 if(estim[i] == '-'){
2784 if (prevEscape) { individual += estim[i]; prevEscape = false; } //add in dash because it was escaped.
2786 container.push_back(individual);
2789 }else if(estim[i] == '\\'){
2790 if (i < estimLength-1) {
2791 if (estim[i+1] == '-') { prevEscape=true; } //are you a backslash before a dash, if yes ignore
2792 else { individual += estim[i]; prevEscape = false; } //if no, add in
2793 }else { individual += estim[i]; }
2795 individual += estim[i];
2801 container.push_back(individual);
2803 catch(exception& e) {
2804 errorOut(e, "MothurOut", "splitAtDash");
2809 /***********************************************************************/
2810 //This function parses the label options and puts them in a set
2811 void MothurOut::splitAtDash(string& estim, set<string>& container) {
2813 string individual = "";
2814 int estimLength = estim.size();
2815 bool prevEscape = false;
2817 for(int i=0;i<estimLength;i++){
2819 individual += estim[i];
2823 if(estim[i] == '\\'){
2826 else if(estim[i] == '-'){
2827 container.insert(individual);
2832 individual += estim[i];
2839 for(int i=0;i<estimLength;i++){
2840 if(estim[i] == '-'){
2841 if (prevEscape) { individual += estim[i]; prevEscape = false; } //add in dash because it was escaped.
2843 container.insert(individual);
2846 }else if(estim[i] == '\\'){
2847 if (i < estimLength-1) {
2848 if (estim[i+1] == '-') { prevEscape=true; } //are you a backslash before a dash, if yes ignore
2849 else { individual += estim[i]; prevEscape = false; } //if no, add in
2850 }else { individual += estim[i]; }
2852 individual += estim[i];
2855 container.insert(individual);
2858 catch(exception& e) {
2859 errorOut(e, "MothurOut", "splitAtDash");
2863 /***********************************************************************/
2864 //This function parses the line options and puts them in a set
2865 void MothurOut::splitAtDash(string& estim, set<int>& container) {
2867 string individual = "";
2869 int estimLength = estim.size();
2870 bool prevEscape = false;
2872 for(int i=0;i<estimLength;i++){
2874 individual += estim[i];
2878 if(estim[i] == '\\'){
2881 else if(estim[i] == '-'){
2882 convert(individual, lineNum); //convert the string to int
2883 container.insert(lineNum);
2888 individual += estim[i];
2894 for(int i=0;i<estimLength;i++){
2895 if(estim[i] == '-'){
2896 if (prevEscape) { individual += estim[i]; prevEscape = false; } //add in dash because it was escaped.
2898 convert(individual, lineNum); //convert the string to int
2899 container.insert(lineNum);
2902 }else if(estim[i] == '\\'){
2903 if (i < estimLength-1) {
2904 if (estim[i+1] == '-') { prevEscape=true; } //are you a backslash before a dash, if yes ignore
2905 else { individual += estim[i]; prevEscape = false; } //if no, add in
2906 }else { individual += estim[i]; }
2908 individual += estim[i];
2912 convert(individual, lineNum); //convert the string to int
2913 container.insert(lineNum);
2915 catch(exception& e) {
2916 errorOut(e, "MothurOut", "splitAtDash");
2921 /***********************************************************************/
2922 string MothurOut::makeList(vector<string>& names) {
2926 if (names.size() == 0) { return list; }
2928 for (int i = 0; i < names.size()-1; i++) { list += names[i] + ","; }
2931 list += names[names.size()-1];
2935 catch(exception& e) {
2936 errorOut(e, "MothurOut", "makeList");
2941 /***********************************************************************/
2942 //This function parses the a string and puts peices in a vector
2943 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
2945 string individual = "";
2946 int estimLength = estim.size();
2947 for(int i=0;i<estimLength;i++){
2948 if(estim[i] == ','){
2949 container.push_back(individual);
2953 individual += estim[i];
2956 container.push_back(individual);
2961 // string individual;
2963 // while (estim.find_first_of(',') != -1) {
2964 // individual = estim.substr(0,estim.find_first_of(','));
2965 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
2966 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
2967 // container.push_back(individual);
2971 // container.push_back(estim);
2973 catch(exception& e) {
2974 errorOut(e, "MothurOut", "splitAtComma");
2978 /***********************************************************************/
2979 //This function splits up the various option parameters
2980 void MothurOut::splitAtChar(string& prefix, string& suffix, char c){
2982 prefix = suffix.substr(0,suffix.find_first_of(c));
2983 if ((suffix.find_first_of(c)+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2984 suffix = suffix.substr(suffix.find_first_of(c)+1, suffix.length());
2986 while(suffix.at(0) == ' ')
2987 suffix = suffix.substr(1, suffix.length());
2988 }else { suffix = ""; }
2991 catch(exception& e) {
2992 errorOut(e, "MothurOut", "splitAtChar");
2997 /***********************************************************************/
2999 //This function splits up the various option parameters
3000 void MothurOut::splitAtComma(string& prefix, string& suffix){
3002 prefix = suffix.substr(0,suffix.find_first_of(','));
3003 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
3004 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
3006 while(suffix.at(0) == ' ')
3007 suffix = suffix.substr(1, suffix.length());
3008 }else { suffix = ""; }
3011 catch(exception& e) {
3012 errorOut(e, "MothurOut", "splitAtComma");
3016 /***********************************************************************/
3018 //This function separates the key value from the option value i.e. dist=96_...
3019 void MothurOut::splitAtEquals(string& key, string& value){
3021 if(value.find_first_of('=') != -1){
3022 key = value.substr(0,value.find_first_of('='));
3023 if ((value.find_first_of('=')+1) <= value.length()) {
3024 value = value.substr(value.find_first_of('=')+1, value.length());
3031 catch(exception& e) {
3032 errorOut(e, "MothurOut", "splitAtEquals");
3037 /**************************************************************************************************/
3039 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
3041 for (int i = 0; i < Groups.size(); i++) {
3042 if (groupname == Groups[i]) { return true; }
3046 catch(exception& e) {
3047 errorOut(e, "MothurOut", "inUsersGroups");
3051 /**************************************************************************************************/
3053 bool MothurOut::inUsersGroups(vector<int> set, vector< vector<int> > sets) {
3055 for (int i = 0; i < sets.size(); i++) {
3056 if (set == sets[i]) { return true; }
3060 catch(exception& e) {
3061 errorOut(e, "MothurOut", "inUsersGroups");
3065 /**************************************************************************************************/
3067 bool MothurOut::inUsersGroups(int groupname, vector<int> Groups) {
3069 for (int i = 0; i < Groups.size(); i++) {
3070 if (groupname == Groups[i]) { return true; }
3074 catch(exception& e) {
3075 errorOut(e, "MothurOut", "inUsersGroups");
3080 /**************************************************************************************************/
3081 //returns true if any of the strings in first vector are in second vector
3082 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
3085 for (int i = 0; i < groupnames.size(); i++) {
3086 if (inUsersGroups(groupnames[i], Groups)) { return true; }
3090 catch(exception& e) {
3091 errorOut(e, "MothurOut", "inUsersGroups");
3095 /***********************************************************************/
3096 //this function determines if the user has given us labels that are smaller than the given label.
3097 //if so then it returns true so that the calling function can run the previous valid distance.
3098 //it's a "smart" distance function. It also checks for invalid labels.
3099 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
3102 set<string>::iterator it;
3103 vector<float> orderFloat;
3104 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
3105 map<string, float>::iterator it2;
3107 bool smaller = false;
3109 //unique is the smallest line
3110 if (label == "unique") { return false; }
3112 if (convertTestFloat(label, labelFloat)) {
3113 convert(label, labelFloat);
3114 }else { //cant convert
3119 //go through users set and make them floats
3120 for(it = userLabels.begin(); it != userLabels.end();) {
3123 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
3125 orderFloat.push_back(temp);
3126 userMap[*it] = temp;
3128 }else if (*it == "unique") {
3129 orderFloat.push_back(-1.0);
3130 userMap["unique"] = -1.0;
3133 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
3134 userLabels.erase(it++);
3139 sort(orderFloat.begin(), orderFloat.end());
3141 /*************************************************/
3142 //is this label bigger than any of the users labels
3143 /*************************************************/
3145 //loop through order until you find a label greater than label
3146 for (int i = 0; i < orderFloat.size(); i++) {
3147 if (orderFloat[i] < labelFloat) {
3149 if (orderFloat[i] == -1) {
3150 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
3151 userLabels.erase("unique");
3154 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
3156 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
3157 if (it2->second == orderFloat[i]) {
3159 //remove small labels
3160 userLabels.erase(s);
3164 if (errorOff == "") {mothurOut( s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
3166 //since they are sorted once you find a bigger one stop looking
3173 catch(exception& e) {
3174 errorOut(e, "MothurOut", "anyLabelsToProcess");
3179 /**************************************************************************************************/
3180 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
3185 string line = getline(file);
3187 //before we added this check
3188 if (line[0] != '#') { good = false; }
3191 line = line.substr(1);
3193 vector<string> versionVector;
3194 splitAtChar(version, versionVector, '.');
3196 //check file version
3197 vector<string> linesVector;
3198 splitAtChar(line, linesVector, '.');
3200 if (versionVector.size() != linesVector.size()) { good = false; }
3202 for (int j = 0; j < versionVector.size(); j++) {
3204 convert(versionVector[j], num1);
3205 convert(linesVector[j], num2);
3207 //if mothurs version is newer than this files version, then we want to remake it
3208 if (num1 > num2) { good = false; break; }
3214 if (!good) { file.close(); }
3215 else { file.seekg(0); }
3219 catch(exception& e) {
3220 errorOut(e, "MothurOut", "checkReleaseVersion");
3224 /**************************************************************************************************/
3225 vector<double> MothurOut::getAverages(vector< vector<double> >& dists) {
3227 vector<double> averages; //averages.resize(numComp, 0.0);
3228 for (int i = 0; i < dists[0].size(); i++) { averages.push_back(0.0); }
3230 for (int thisIter = 0; thisIter < dists.size(); thisIter++) {
3231 for (int i = 0; i < dists[thisIter].size(); i++) {
3232 averages[i] += dists[thisIter][i];
3237 for (int i = 0; i < averages.size(); i++) { averages[i] /= (double) dists.size(); }
3241 catch(exception& e) {
3242 errorOut(e, "MothurOut", "getAverages");
3246 /**************************************************************************************************/
3247 vector<double> MothurOut::getStandardDeviation(vector< vector<double> >& dists) {
3250 vector<double> averages = getAverages(dists);
3252 //find standard deviation
3253 vector<double> stdDev; //stdDev.resize(numComp, 0.0);
3254 for (int i = 0; i < dists[0].size(); i++) { stdDev.push_back(0.0); }
3256 for (int thisIter = 0; thisIter < dists.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3257 for (int j = 0; j < dists[thisIter].size(); j++) {
3258 stdDev[j] += ((dists[thisIter][j] - averages[j]) * (dists[thisIter][j] - averages[j]));
3261 for (int i = 0; i < stdDev.size(); i++) {
3262 stdDev[i] /= (double) dists.size();
3263 stdDev[i] = sqrt(stdDev[i]);
3268 catch(exception& e) {
3269 errorOut(e, "MothurOut", "getAverages");
3273 /**************************************************************************************************/
3274 vector<double> MothurOut::getStandardDeviation(vector< vector<double> >& dists, vector<double>& averages) {
3276 //find standard deviation
3277 vector<double> stdDev; //stdDev.resize(numComp, 0.0);
3278 for (int i = 0; i < dists[0].size(); i++) { stdDev.push_back(0.0); }
3280 for (int thisIter = 0; thisIter < dists.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3281 for (int j = 0; j < dists[thisIter].size(); j++) {
3282 stdDev[j] += ((dists[thisIter][j] - averages[j]) * (dists[thisIter][j] - averages[j]));
3285 for (int i = 0; i < stdDev.size(); i++) {
3286 stdDev[i] /= (double) dists.size();
3287 stdDev[i] = sqrt(stdDev[i]);
3292 catch(exception& e) {
3293 errorOut(e, "MothurOut", "getAverages");
3297 /**************************************************************************************************/
3298 vector< vector<seqDist> > MothurOut::getAverages(vector< vector< vector<seqDist> > >& calcDistsTotals, string mode) {
3301 vector< vector<seqDist> > calcAverages; //calcAverages.resize(calcDistsTotals[0].size());
3302 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3303 //calcAverages[i].resize(calcDistsTotals[0][i].size());
3304 vector<seqDist> temp;
3305 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3307 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3308 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3309 tempDist.dist = 0.0;
3310 temp.push_back(tempDist);
3312 calcAverages.push_back(temp);
3315 if (mode == "average") {
3316 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //sum all groups dists for each calculator
3317 for (int i = 0; i < calcAverages.size(); i++) { //initialize sums to zero.
3318 for (int j = 0; j < calcAverages[i].size(); j++) {
3319 calcAverages[i][j].dist += calcDistsTotals[thisIter][i][j].dist;
3324 for (int i = 0; i < calcAverages.size(); i++) { //finds average.
3325 for (int j = 0; j < calcAverages[i].size(); j++) {
3326 calcAverages[i][j].dist /= (float) calcDistsTotals.size();
3329 }else { //find median
3330 for (int i = 0; i < calcAverages.size(); i++) { //for each calc
3331 for (int j = 0; j < calcAverages[i].size(); j++) { //for each comparison
3332 vector<double> dists;
3333 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //for each subsample
3334 dists.push_back(calcDistsTotals[thisIter][i][j].dist);
3336 sort(dists.begin(), dists.end());
3337 calcAverages[i][j].dist = dists[(calcDistsTotals.size()/2)];
3342 return calcAverages;
3344 catch(exception& e) {
3345 errorOut(e, "MothurOut", "getAverages");
3349 /**************************************************************************************************/
3350 vector< vector<seqDist> > MothurOut::getAverages(vector< vector< vector<seqDist> > >& calcDistsTotals) {
3353 vector< vector<seqDist> > calcAverages; //calcAverages.resize(calcDistsTotals[0].size());
3354 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3355 //calcAverages[i].resize(calcDistsTotals[0][i].size());
3356 vector<seqDist> temp;
3357 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3359 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3360 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3361 tempDist.dist = 0.0;
3362 temp.push_back(tempDist);
3364 calcAverages.push_back(temp);
3368 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //sum all groups dists for each calculator
3369 for (int i = 0; i < calcAverages.size(); i++) { //initialize sums to zero.
3370 for (int j = 0; j < calcAverages[i].size(); j++) {
3371 calcAverages[i][j].dist += calcDistsTotals[thisIter][i][j].dist;
3376 for (int i = 0; i < calcAverages.size(); i++) { //finds average.
3377 for (int j = 0; j < calcAverages[i].size(); j++) {
3378 calcAverages[i][j].dist /= (float) calcDistsTotals.size();
3382 return calcAverages;
3384 catch(exception& e) {
3385 errorOut(e, "MothurOut", "getAverages");
3389 /**************************************************************************************************/
3390 vector< vector<seqDist> > MothurOut::getStandardDeviation(vector< vector< vector<seqDist> > >& calcDistsTotals) {
3393 vector< vector<seqDist> > calcAverages = getAverages(calcDistsTotals);
3395 //find standard deviation
3396 vector< vector<seqDist> > stdDev;
3397 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3398 vector<seqDist> temp;
3399 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3401 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3402 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3403 tempDist.dist = 0.0;
3404 temp.push_back(tempDist);
3406 stdDev.push_back(temp);
3409 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3410 for (int i = 0; i < stdDev.size(); i++) {
3411 for (int j = 0; j < stdDev[i].size(); j++) {
3412 stdDev[i][j].dist += ((calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist) * (calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist));
3417 for (int i = 0; i < stdDev.size(); i++) { //finds average.
3418 for (int j = 0; j < stdDev[i].size(); j++) {
3419 stdDev[i][j].dist /= (float) calcDistsTotals.size();
3420 stdDev[i][j].dist = sqrt(stdDev[i][j].dist);
3426 catch(exception& e) {
3427 errorOut(e, "MothurOut", "getAverages");
3431 /**************************************************************************************************/
3432 vector< vector<seqDist> > MothurOut::getStandardDeviation(vector< vector< vector<seqDist> > >& calcDistsTotals, vector< vector<seqDist> >& calcAverages) {
3434 //find standard deviation
3435 vector< vector<seqDist> > stdDev;
3436 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3437 vector<seqDist> temp;
3438 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3440 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3441 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3442 tempDist.dist = 0.0;
3443 temp.push_back(tempDist);
3445 stdDev.push_back(temp);
3448 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3449 for (int i = 0; i < stdDev.size(); i++) {
3450 for (int j = 0; j < stdDev[i].size(); j++) {
3451 stdDev[i][j].dist += ((calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist) * (calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist));
3456 for (int i = 0; i < stdDev.size(); i++) { //finds average.
3457 for (int j = 0; j < stdDev[i].size(); j++) {
3458 stdDev[i][j].dist /= (float) calcDistsTotals.size();
3459 stdDev[i][j].dist = sqrt(stdDev[i][j].dist);
3465 catch(exception& e) {
3466 errorOut(e, "MothurOut", "getAverages");
3471 /**************************************************************************************************/
3472 bool MothurOut::isContainingOnlyDigits(string input) {
3475 //are you a digit in ascii code
3476 for (int i = 0;i < input.length(); i++){
3477 if( input[i]>47 && input[i]<58){}
3478 else { return false; }
3483 catch(exception& e) {
3484 errorOut(e, "MothurOut", "isContainingOnlyDigits");
3488 /**************************************************************************************************/
3489 int MothurOut::removeConfidences(string& tax) {
3495 while (tax.find_first_of(';') != -1) {
3497 if (control_pressed) { return 0; }
3500 taxon = tax.substr(0,tax.find_first_of(';'));
3502 int pos = taxon.find_last_of('(');
3505 int pos2 = taxon.find_last_of(')');
3507 string confidenceScore = taxon.substr(pos+1, (pos2-(pos+1)));
3508 if (isNumeric1(confidenceScore)) {
3509 taxon = taxon.substr(0, pos); //rip off confidence
3515 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
3523 catch(exception& e) {
3524 errorOut(e, "MothurOut", "removeConfidences");
3528 /**************************************************************************************************/
3529 string MothurOut::removeQuotes(string tax) {
3535 for (int i = 0; i < tax.length(); i++) {
3537 if (control_pressed) { return newTax; }
3539 if ((tax[i] != '\'') && (tax[i] != '\"')) { newTax += tax[i]; }
3545 catch(exception& e) {
3546 errorOut(e, "MothurOut", "removeQuotes");
3550 /**************************************************************************************************/
3551 // function for calculating standard deviation
3552 double MothurOut::getStandardDeviation(vector<int>& featureVector){
3556 for (int i = 0; i < featureVector.size(); i++) { average += featureVector[i]; }
3557 average /= (double) featureVector.size();
3559 //find standard deviation
3561 for (int i = 0; i < featureVector.size(); i++) { //compute the difference of each dist from the mean, and square the result of each
3562 stdDev += ((featureVector[i] - average) * (featureVector[i] - average));
3565 stdDev /= (double) featureVector.size();
3566 stdDev = sqrt(stdDev);
3570 catch(exception& e) {
3571 errorOut(e, "MothurOut", "getStandardDeviation");
3575 /**************************************************************************************************/