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");
1174 /**************************************************************************************************/
1175 string MothurOut::sortFile(string distFile, string outputDir){
1178 //if (outputDir == "") { outputDir += hasPath(distFile); }
1179 string outfile = getRootName(distFile) + "sorted.dist";
1182 //if you can, use the unix sort since its been optimized for years
1183 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1184 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
1185 system(command.c_str());
1186 #else //you are stuck with my best attempt...
1187 //windows sort does not have a way to specify a column, only a character in the line
1188 //since we cannot assume that the distance will always be at the the same character location on each line
1189 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
1191 //read in file line by file and put distance first
1192 string tempDistFile = distFile + ".temp";
1195 openInputFile(distFile, input);
1196 openOutputFile(tempDistFile, output);
1198 string firstName, secondName;
1200 while (!input.eof()) {
1201 input >> firstName >> secondName >> dist;
1202 output << dist << '\t' << firstName << '\t' << secondName << endl;
1209 //sort using windows sort
1210 string tempOutfile = outfile + ".temp";
1211 string command = "sort " + tempDistFile + " /O " + tempOutfile;
1212 system(command.c_str());
1214 //read in sorted file and put distance at end again
1217 openInputFile(tempOutfile, input2);
1218 openOutputFile(outfile, output2);
1220 while (!input2.eof()) {
1221 input2 >> dist >> firstName >> secondName;
1222 output2 << firstName << '\t' << secondName << '\t' << dist << endl;
1229 mothurRemove(tempDistFile);
1230 mothurRemove(tempOutfile);
1235 catch(exception& e) {
1236 errorOut(e, "MothurOut", "sortFile");
1240 /**************************************************************************************************/
1241 vector<unsigned long long> MothurOut::setFilePosFasta(string filename, int& num) {
1243 vector<unsigned long long> positions;
1245 //openInputFile(filename, inFASTA);
1246 inFASTA.open(filename.c_str(), ios::binary);
1249 unsigned long long count = 0;
1250 while(!inFASTA.eof()){
1251 //input = getline(inFASTA);
1252 //cout << input << '\t' << inFASTA.tellg() << endl;
1253 //if (input.length() != 0) {
1254 // if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); cout << (pos - input.length() - 1) << endl; }
1256 //gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
1257 char c = inFASTA.get(); count++;
1259 positions.push_back(count-1);
1260 //cout << count << endl;
1265 num = positions.size();
1270 //get num bytes in file
1271 pFile = fopen (filename.c_str(),"rb");
1272 if (pFile==NULL) perror ("Error opening file");
1274 fseek (pFile, 0, SEEK_END);
1279 unsigned long long size = positions[(positions.size()-1)];
1281 openInputFile(filename, in);
1286 if(in.eof()) { break; }
1291 positions.push_back(size);
1296 catch(exception& e) {
1297 errorOut(e, "MothurOut", "setFilePosFasta");
1301 /**************************************************************************************************/
1302 vector<unsigned long long> MothurOut::setFilePosEachLine(string filename, int& num) {
1304 filename = getFullPathName(filename);
1306 vector<unsigned long long> positions;
1308 //openInputFile(filename, in);
1309 in.open(filename.c_str(), ios::binary);
1312 unsigned long long count = 0;
1313 positions.push_back(0);
1316 //getline counting reads
1317 char d = in.get(); count++;
1318 while ((d != '\n') && (d != '\r') && (d != '\f') && (d != in.eof())) {
1319 //get next character
1325 d=in.get(); count++;
1326 while(isspace(d) && (d != in.eof())) { d=in.get(); count++;}
1328 positions.push_back(count-1);
1329 //cout << count-1 << endl;
1333 num = positions.size()-1;
1336 unsigned long long size;
1338 //get num bytes in file
1339 pFile = fopen (filename.c_str(),"rb");
1340 if (pFile==NULL) perror ("Error opening file");
1342 fseek (pFile, 0, SEEK_END);
1347 positions[(positions.size()-1)] = size;
1351 catch(exception& e) {
1352 errorOut(e, "MothurOut", "setFilePosEachLine");
1356 /**************************************************************************************************/
1358 vector<unsigned long long> MothurOut::divideFile(string filename, int& proc) {
1360 vector<unsigned long long> filePos;
1361 filePos.push_back(0);
1364 unsigned long long size;
1366 filename = getFullPathName(filename);
1368 //get num bytes in file
1369 pFile = fopen (filename.c_str(),"rb");
1370 if (pFile==NULL) perror ("Error opening file");
1372 fseek (pFile, 0, SEEK_END);
1377 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1379 //estimate file breaks
1380 unsigned long long chunkSize = 0;
1381 chunkSize = size / proc;
1383 //file to small to divide by processors
1384 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1386 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1387 for (int i = 0; i < proc; i++) {
1388 unsigned long long spot = (i+1) * chunkSize;
1391 openInputFile(filename, in);
1395 unsigned long long newSpot = spot;
1399 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1400 else if (int(c) == -1) { break; }
1404 //there was not another sequence before the end of the file
1405 unsigned long long sanityPos = in.tellg();
1407 if (sanityPos == -1) { break; }
1408 else { filePos.push_back(newSpot); }
1414 filePos.push_back(size);
1416 //sanity check filePos
1417 for (int i = 0; i < (filePos.size()-1); i++) {
1418 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1421 proc = (filePos.size() - 1);
1423 mothurOut("[ERROR]: Windows version should not be calling the divideFile function."); mothurOutEndLine();
1425 filePos.push_back(size);
1429 catch(exception& e) {
1430 errorOut(e, "MothurOut", "divideFile");
1434 /**************************************************************************************************/
1436 vector<unsigned long long> MothurOut::divideFilePerLine(string filename, int& proc) {
1438 vector<unsigned long long> filePos;
1439 filePos.push_back(0);
1442 unsigned long long size;
1444 filename = getFullPathName(filename);
1446 //get num bytes in file
1447 pFile = fopen (filename.c_str(),"rb");
1448 if (pFile==NULL) perror ("Error opening file");
1450 fseek (pFile, 0, SEEK_END);
1455 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1457 //estimate file breaks
1458 unsigned long long chunkSize = 0;
1459 chunkSize = size / proc;
1461 //file to small to divide by processors
1462 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1464 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1465 for (int i = 0; i < proc; i++) {
1466 unsigned long long spot = (i+1) * chunkSize;
1469 openInputFile(filename, in);
1472 //look for next line break
1473 unsigned long long newSpot = spot;
1477 if ((c == '\n') || (c == '\r') || (c == '\f')) { gobble(in); newSpot = in.tellg(); break; }
1478 else if (int(c) == -1) { break; }
1481 //there was not another line before the end of the file
1482 unsigned long long sanityPos = in.tellg();
1484 if (sanityPos == -1) { break; }
1485 else { filePos.push_back(newSpot); }
1491 filePos.push_back(size);
1493 //sanity check filePos
1494 for (int i = 0; i < (filePos.size()-1); i++) {
1495 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1498 proc = (filePos.size() - 1);
1500 mothurOut("[ERROR]: Windows version should not be calling the divideFile function."); mothurOutEndLine();
1502 filePos.push_back(size);
1506 catch(exception& e) {
1507 errorOut(e, "MothurOut", "divideFile");
1511 /**************************************************************************************************/
1512 int MothurOut::divideFile(string filename, int& proc, vector<string>& files) {
1515 vector<unsigned long long> filePos = divideFile(filename, proc);
1517 for (int i = 0; i < (filePos.size()-1); i++) {
1521 openInputFile(filename, in);
1522 in.seekg(filePos[i]);
1523 unsigned long long size = filePos[(i+1)] - filePos[i];
1524 char* chunk = new char[size];
1525 in.read(chunk, size);
1529 string fileChunkName = filename + "." + toString(i) + ".tmp";
1531 openOutputFile(fileChunkName, out);
1533 out << chunk << endl;
1538 files.push_back(fileChunkName);
1543 catch(exception& e) {
1544 errorOut(e, "MothurOut", "divideFile");
1548 /***********************************************************************/
1550 bool MothurOut::isTrue(string f){
1553 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1555 if ((f == "TRUE") || (f == "T")) { return true; }
1556 else { return false; }
1558 catch(exception& e) {
1559 errorOut(e, "MothurOut", "isTrue");
1564 /***********************************************************************/
1566 float MothurOut::roundDist(float dist, int precision){
1568 return int(dist * precision + 0.5)/float(precision);
1570 catch(exception& e) {
1571 errorOut(e, "MothurOut", "roundDist");
1575 /***********************************************************************/
1577 float MothurOut::ceilDist(float dist, int precision){
1579 return int(ceil(dist * precision))/float(precision);
1581 catch(exception& e) {
1582 errorOut(e, "MothurOut", "ceilDist");
1586 /***********************************************************************/
1588 vector<string> MothurOut::splitWhiteSpace(string& rest, char buffer[], int size){
1590 vector<string> pieces;
1592 for (int i = 0; i < size; i++) {
1593 if (!isspace(buffer[i])) { rest += buffer[i]; }
1595 if (rest != "") { pieces.push_back(rest); rest = ""; }
1596 while (i < size) { //gobble white space
1597 if (isspace(buffer[i])) { i++; }
1598 else { rest = buffer[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1605 catch(exception& e) {
1606 errorOut(e, "MothurOut", "splitWhiteSpace");
1610 /***********************************************************************/
1611 vector<string> MothurOut::splitWhiteSpace(string input){
1613 vector<string> pieces;
1616 for (int i = 0; i < input.length(); i++) {
1617 if (!isspace(input[i])) { rest += input[i]; }
1619 if (rest != "") { pieces.push_back(rest); rest = ""; }
1620 while (i < input.length()) { //gobble white space
1621 if (isspace(input[i])) { i++; }
1622 else { rest = input[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1627 if (rest != "") { pieces.push_back(rest); }
1631 catch(exception& e) {
1632 errorOut(e, "MothurOut", "splitWhiteSpace");
1636 /***********************************************************************/
1637 vector<string> MothurOut::splitWhiteSpaceWithQuotes(string input){
1639 vector<string> pieces;
1642 int pos = input.find('\'');
1643 int pos2 = input.find('\"');
1645 if ((pos == string::npos) && (pos2 == string::npos)) { return splitWhiteSpace(input); } //no quotes to worry about
1647 for (int i = 0; i < input.length(); i++) {
1648 if ((input[i] == '\'') || (input[i] == '\"') || (rest == "\'") || (rest == "\"")) { //grab everything til end or next ' or "
1650 for (int j = i+1; j < input.length(); j++) {
1651 if ((input[j] == '\'') || (input[j] == '\"')) { //then quit
1655 }else { rest += input[j]; }
1657 }else if (!isspace(input[i])) { rest += input[i]; }
1659 if (rest != "") { pieces.push_back(rest); rest = ""; }
1660 while (i < input.length()) { //gobble white space
1661 if (isspace(input[i])) { i++; }
1662 else { rest = input[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1667 if (rest != "") { pieces.push_back(rest); }
1671 catch(exception& e) {
1672 errorOut(e, "MothurOut", "splitWhiteSpace");
1676 //**********************************************************************************************************************
1677 int MothurOut::readTax(string namefile, map<string, string>& taxMap) {
1681 openInputFile(namefile, in);
1685 bool pairDone = false;
1686 bool columnOne = true;
1687 string firstCol, secondCol;
1690 if (control_pressed) { break; }
1692 in.read(buffer, 4096);
1693 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1695 for (int i = 0; i < pieces.size(); i++) {
1696 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1697 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1700 checkName(firstCol);
1701 //are there confidence scores, if so remove them
1702 if (secondCol.find_first_of('(') != -1) { removeConfidences(secondCol); }
1703 map<string, string>::iterator itTax = taxMap.find(firstCol);
1705 if(itTax == taxMap.end()) {
1706 bool ignore = false;
1707 if (secondCol != "") { if (secondCol[secondCol.length()-1] != ';') { mothurOut("[ERROR]: " + firstCol + " is missing the final ';', ignoring.\n"); ignore=true; }
1709 if (!ignore) { taxMap[firstCol] = secondCol; }
1710 if (debug) { mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n"); }
1712 mothurOut("[ERROR]: " + firstCol + " is already in your taxonomy file, names must be unique./n"); control_pressed = true;
1721 vector<string> pieces = splitWhiteSpace(rest);
1723 for (int i = 0; i < pieces.size(); i++) {
1724 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1725 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1728 checkName(firstCol);
1729 //are there confidence scores, if so remove them
1730 if (secondCol.find_first_of('(') != -1) { removeConfidences(secondCol); }
1731 map<string, string>::iterator itTax = taxMap.find(firstCol);
1733 if(itTax == taxMap.end()) {
1734 bool ignore = false;
1735 if (secondCol != "") { if (secondCol[secondCol.length()-1] != ';') { mothurOut("[ERROR]: " + firstCol + " is missing the final ';', ignoring.\n"); ignore=true; }
1737 if (!ignore) { taxMap[firstCol] = secondCol; }
1738 if (debug) { mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n"); }
1740 mothurOut("[ERROR]: " + firstCol + " is already in your taxonomy file, names must be unique./n"); control_pressed = true;
1748 return taxMap.size();
1751 catch(exception& e) {
1752 errorOut(e, "MothurOut", "readTax");
1756 /**********************************************************************************************************************/
1757 int MothurOut::readNames(string namefile, map<string, string>& nameMap, bool redund) {
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 checkName(firstCol);
1781 checkName(secondCol);
1783 //parse names into vector
1784 vector<string> theseNames;
1785 splitAtComma(secondCol, theseNames);
1786 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1794 vector<string> pieces = splitWhiteSpace(rest);
1796 for (int i = 0; i < pieces.size(); i++) {
1797 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1798 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1801 checkName(firstCol);
1802 checkName(secondCol);
1804 //parse names into vector
1805 vector<string> theseNames;
1806 splitAtComma(secondCol, theseNames);
1807 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1813 return nameMap.size();
1816 catch(exception& e) {
1817 errorOut(e, "MothurOut", "readNames");
1821 /**********************************************************************************************************************/
1822 int MothurOut::readNames(string namefile, map<string, string>& nameMap, int flip) {
1826 openInputFile(namefile, in);
1830 bool pairDone = false;
1831 bool columnOne = true;
1832 string firstCol, secondCol;
1835 if (control_pressed) { break; }
1837 in.read(buffer, 4096);
1838 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1840 for (int i = 0; i < pieces.size(); i++) {
1841 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1842 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1845 checkName(firstCol);
1846 checkName(secondCol);
1847 nameMap[secondCol] = firstCol;
1855 vector<string> pieces = splitWhiteSpace(rest);
1857 for (int i = 0; i < pieces.size(); i++) {
1858 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1859 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1862 checkName(firstCol);
1863 checkName(secondCol);
1864 nameMap[secondCol] = firstCol;
1870 return nameMap.size();
1873 catch(exception& e) {
1874 errorOut(e, "MothurOut", "readNames");
1878 /**********************************************************************************************************************/
1879 int MothurOut::readNames(string namefile, map<string, string>& nameMap, map<string, int>& nameCount) {
1881 nameMap.clear(); nameCount.clear();
1884 openInputFile(namefile, in);
1888 bool pairDone = false;
1889 bool columnOne = true;
1890 string firstCol, secondCol;
1893 if (control_pressed) { break; }
1895 in.read(buffer, 4096);
1896 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1898 for (int i = 0; i < pieces.size(); i++) {
1899 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1900 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1903 checkName(firstCol);
1904 checkName(secondCol);
1905 //parse names into vector
1906 vector<string> theseNames;
1907 splitAtComma(secondCol, theseNames);
1908 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1909 nameCount[firstCol] = theseNames.size();
1917 vector<string> pieces = splitWhiteSpace(rest);
1919 for (int i = 0; i < pieces.size(); i++) {
1920 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1921 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1924 checkName(firstCol);
1925 checkName(secondCol);
1926 //parse names into vector
1927 vector<string> theseNames;
1928 splitAtComma(secondCol, theseNames);
1929 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1930 nameCount[firstCol] = theseNames.size();
1936 return nameMap.size();
1939 catch(exception& e) {
1940 errorOut(e, "MothurOut", "readNames");
1944 /**********************************************************************************************************************/
1945 int MothurOut::readNames(string namefile, map<string, string>& nameMap) {
1949 openInputFile(namefile, in);
1953 bool pairDone = false;
1954 bool columnOne = true;
1955 string firstCol, secondCol;
1958 if (control_pressed) { break; }
1960 in.read(buffer, 4096);
1961 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1963 for (int i = 0; i < pieces.size(); i++) {
1964 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1965 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1968 checkName(firstCol);
1969 checkName(secondCol);
1970 nameMap[firstCol] = secondCol; pairDone = false; }
1976 vector<string> pieces = splitWhiteSpace(rest);
1978 for (int i = 0; i < pieces.size(); i++) {
1979 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1980 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1983 checkName(firstCol);
1984 checkName(secondCol);
1985 nameMap[firstCol] = secondCol; pairDone = false; }
1989 return nameMap.size();
1992 catch(exception& e) {
1993 errorOut(e, "MothurOut", "readNames");
1997 /**********************************************************************************************************************/
1998 int MothurOut::readNames(string namefile, map<string, vector<string> >& nameMap) {
2002 openInputFile(namefile, in);
2006 bool pairDone = false;
2007 bool columnOne = true;
2008 string firstCol, secondCol;
2011 if (control_pressed) { break; }
2013 in.read(buffer, 4096);
2014 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2016 for (int i = 0; i < pieces.size(); i++) {
2017 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2018 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2021 checkName(firstCol);
2022 checkName(secondCol);
2023 vector<string> temp;
2024 splitAtComma(secondCol, temp);
2025 nameMap[firstCol] = temp;
2033 vector<string> pieces = splitWhiteSpace(rest);
2035 for (int i = 0; i < pieces.size(); i++) {
2036 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2037 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2040 checkName(firstCol);
2041 checkName(secondCol);
2042 vector<string> temp;
2043 splitAtComma(secondCol, temp);
2044 nameMap[firstCol] = temp;
2050 return nameMap.size();
2052 catch(exception& e) {
2053 errorOut(e, "MothurOut", "readNames");
2057 /**********************************************************************************************************************/
2058 map<string, int> MothurOut::readNames(string namefile) {
2060 map<string, int> nameMap;
2064 openInputFile(namefile, in);
2068 bool pairDone = false;
2069 bool columnOne = true;
2070 string firstCol, secondCol;
2073 if (control_pressed) { break; }
2075 in.read(buffer, 4096);
2076 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2078 for (int i = 0; i < pieces.size(); i++) {
2079 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2080 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2083 checkName(firstCol);
2084 checkName(secondCol);
2085 int num = getNumNames(secondCol);
2086 nameMap[firstCol] = num;
2094 vector<string> pieces = splitWhiteSpace(rest);
2095 for (int i = 0; i < pieces.size(); i++) {
2096 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2097 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2100 checkName(firstCol);
2101 checkName(secondCol);
2102 int num = getNumNames(secondCol);
2103 nameMap[firstCol] = num;
2112 catch(exception& e) {
2113 errorOut(e, "MothurOut", "readNames");
2117 /**********************************************************************************************************************/
2118 map<string, int> MothurOut::readNames(string namefile, unsigned long int& numSeqs) {
2120 map<string, int> nameMap;
2125 openInputFile(namefile, in);
2129 bool pairDone = false;
2130 bool columnOne = true;
2131 string firstCol, secondCol;
2134 if (control_pressed) { break; }
2136 in.read(buffer, 4096);
2137 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2139 for (int i = 0; i < pieces.size(); i++) {
2140 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2141 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2144 checkName(firstCol);
2145 checkName(secondCol);
2146 int num = getNumNames(secondCol);
2147 nameMap[firstCol] = num;
2156 vector<string> pieces = splitWhiteSpace(rest);
2157 for (int i = 0; i < pieces.size(); i++) {
2158 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2159 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2162 checkName(firstCol);
2163 checkName(secondCol);
2164 int num = getNumNames(secondCol);
2165 nameMap[firstCol] = num;
2175 catch(exception& e) {
2176 errorOut(e, "MothurOut", "readNames");
2180 /************************************************************/
2181 int MothurOut::checkName(string& name) {
2183 for (int i = 0; i < name.length(); i++) {
2184 if (name[i] == ':') { name[i] = '_'; changedSeqNames = true; }
2188 catch(exception& e) {
2189 errorOut(e, "MothurOut", "checkName");
2193 /**********************************************************************************************************************/
2194 int MothurOut::readNames(string namefile, vector<seqPriorityNode>& nameVector, map<string, string>& fastamap) {
2200 openInputFile(namefile, in);
2204 bool pairDone = false;
2205 bool columnOne = true;
2206 string firstCol, secondCol;
2209 if (control_pressed) { break; }
2211 in.read(buffer, 4096);
2212 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2214 for (int i = 0; i < pieces.size(); i++) {
2215 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2216 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2219 checkName(firstCol);
2220 checkName(secondCol);
2221 int num = getNumNames(secondCol);
2223 map<string, string>::iterator it = fastamap.find(firstCol);
2224 if (it == fastamap.end()) {
2226 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
2228 seqPriorityNode temp(num, it->second, firstCol);
2229 nameVector.push_back(temp);
2239 vector<string> pieces = splitWhiteSpace(rest);
2241 for (int i = 0; i < pieces.size(); i++) {
2242 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2243 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2246 checkName(firstCol);
2247 checkName(secondCol);
2248 int num = getNumNames(secondCol);
2250 map<string, string>::iterator it = fastamap.find(firstCol);
2251 if (it == fastamap.end()) {
2253 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
2255 seqPriorityNode temp(num, it->second, firstCol);
2256 nameVector.push_back(temp);
2265 catch(exception& e) {
2266 errorOut(e, "MothurOut", "readNames");
2270 //**********************************************************************************************************************
2271 set<string> MothurOut::readAccnos(string accnosfile){
2275 openInputFile(accnosfile, in);
2282 if (control_pressed) { break; }
2284 in.read(buffer, 4096);
2285 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2287 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.insert(pieces[i]); }
2292 vector<string> pieces = splitWhiteSpace(rest);
2293 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.insert(pieces[i]); }
2297 catch(exception& e) {
2298 errorOut(e, "MothurOut", "readAccnos");
2302 //**********************************************************************************************************************
2303 int MothurOut::readAccnos(string accnosfile, vector<string>& names){
2307 openInputFile(accnosfile, in);
2314 if (control_pressed) { break; }
2316 in.read(buffer, 4096);
2317 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2319 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.push_back(pieces[i]); }
2324 vector<string> pieces = splitWhiteSpace(rest);
2325 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.push_back(pieces[i]); }
2330 catch(exception& e) {
2331 errorOut(e, "MothurOut", "readAccnos");
2335 /***********************************************************************/
2337 int MothurOut::getNumNames(string names){
2343 for(int i=0;i<names.size();i++){
2344 if(names[i] == ','){
2352 catch(exception& e) {
2353 errorOut(e, "MothurOut", "getNumNames");
2357 /***********************************************************************/
2359 int MothurOut::getNumChar(string line, char c){
2364 for(int i=0;i<line.size();i++){
2373 catch(exception& e) {
2374 errorOut(e, "MothurOut", "getNumChar");
2378 //**********************************************************************************************************************
2379 bool MothurOut::isSubset(vector<string> bigset, vector<string> subset) {
2383 if (subset.size() > bigset.size()) { return false; }
2385 //check if each guy in suset is also in bigset
2386 for (int i = 0; i < subset.size(); i++) {
2388 for (int j = 0; j < bigset.size(); j++) {
2389 if (subset[i] == bigset[j]) { match = true; break; }
2392 //you have a guy in subset that had no match in bigset
2393 if (match == false) { return false; }
2399 catch(exception& e) {
2400 errorOut(e, "MothurOut", "isSubset");
2404 /***********************************************************************/
2405 int MothurOut::mothurRemove(string filename){
2407 filename = getFullPathName(filename);
2408 int error = remove(filename.c_str());
2410 // if (errno != ENOENT) { //ENOENT == file does not exist
2411 // string message = "Error deleting file " + filename;
2412 // perror(message.c_str());
2417 catch(exception& e) {
2418 errorOut(e, "MothurOut", "mothurRemove");
2422 /***********************************************************************/
2423 bool MothurOut::mothurConvert(string item, int& num){
2427 if (isNumeric1(item)) {
2432 mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2433 commandInputsConvertError = true;
2438 catch(exception& e) {
2439 errorOut(e, "MothurOut", "mothurConvert");
2443 /***********************************************************************/
2444 bool MothurOut::mothurConvert(string item, intDist& num){
2448 if (isNumeric1(item)) {
2453 mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2454 commandInputsConvertError = true;
2459 catch(exception& e) {
2460 errorOut(e, "MothurOut", "mothurConvert");
2465 /***********************************************************************/
2466 bool MothurOut::isNumeric1(string stringToCheck){
2468 bool numeric = false;
2470 if(stringToCheck.find_first_not_of("0123456789.-") == string::npos) { numeric = true; }
2474 catch(exception& e) {
2475 errorOut(e, "MothurOut", "isNumeric1");
2480 /***********************************************************************/
2481 bool MothurOut::mothurConvert(string item, float& num){
2485 if (isNumeric1(item)) {
2490 mothurOut("[ERROR]: cannot convert " + item + " to a float."); mothurOutEndLine();
2491 commandInputsConvertError = true;
2496 catch(exception& e) {
2497 errorOut(e, "MothurOut", "mothurConvert");
2501 /***********************************************************************/
2502 bool MothurOut::mothurConvert(string item, double& num){
2506 if (isNumeric1(item)) {
2511 mothurOut("[ERROR]: cannot convert " + item + " to a double."); mothurOutEndLine();
2512 commandInputsConvertError = true;
2517 catch(exception& e) {
2518 errorOut(e, "MothurOut", "mothurConvert");
2522 /**************************************************************************************************/
2524 vector<vector<double> > MothurOut::binomial(int maxOrder){
2526 vector<vector<double> > binomial(maxOrder+1);
2528 for(int i=0;i<=maxOrder;i++){
2529 binomial[i].resize(maxOrder+1);
2538 for(int i=2;i<=maxOrder;i++){
2542 for(int i=2;i<=maxOrder;i++){
2543 for(int j=1;j<=maxOrder;j++){
2544 if(i==j){ binomial[i][j]=1; }
2545 if(j>i) { binomial[i][j]=0; }
2546 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
2553 catch(exception& e) {
2554 errorOut(e, "MothurOut", "binomial");
2558 /**************************************************************************************************/
2559 unsigned int MothurOut::fromBase36(string base36){
2561 unsigned int num = 0;
2563 map<char, int> converts;
2628 while (i < base36.length()) {
2630 num = 36 * num + converts[c];
2637 catch(exception& e) {
2638 errorOut(e, "MothurOut", "fromBase36");
2642 /***********************************************************************/
2644 int MothurOut::factorial(int num){
2648 for (int i = 1; i <= num; i++) {
2654 catch(exception& e) {
2655 errorOut(e, "MothurOut", "factorial");
2659 /***********************************************************************/
2661 int MothurOut::getNumSeqs(ifstream& file){
2663 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
2667 catch(exception& e) {
2668 errorOut(e, "MothurOut", "getNumSeqs");
2672 /***********************************************************************/
2673 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
2678 input = getline(file);
2679 if (input.length() != 0) {
2680 if(input[0] == '>'){ numSeqs++; }
2684 catch(exception& e) {
2685 errorOut(e, "MothurOut", "getNumSeqs");
2689 /***********************************************************************/
2691 //This function parses the estimator options and puts them in a vector
2692 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
2695 if (symbol == '-') { splitAtDash(estim, container); return; }
2697 string individual = "";
2698 int estimLength = estim.size();
2699 for(int i=0;i<estimLength;i++){
2700 if(estim[i] == symbol){
2701 container.push_back(individual);
2705 individual += estim[i];
2708 container.push_back(individual);
2711 catch(exception& e) {
2712 errorOut(e, "MothurOut", "splitAtChar");
2717 /***********************************************************************/
2719 //This function parses the estimator options and puts them in a vector
2720 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
2722 string individual = "";
2723 int estimLength = estim.size();
2724 bool prevEscape = false;
2725 /*for(int i=0;i<estimLength;i++){
2727 individual += estim[i];
2731 if(estim[i] == '\\'){
2734 else if(estim[i] == '-'){
2735 container.push_back(individual);
2740 individual += estim[i];
2747 for(int i=0;i<estimLength;i++){
2748 if(estim[i] == '-'){
2749 if (prevEscape) { individual += estim[i]; prevEscape = false; } //add in dash because it was escaped.
2751 container.push_back(individual);
2754 }else if(estim[i] == '\\'){
2755 if (i < estimLength-1) {
2756 if (estim[i+1] == '-') { prevEscape=true; } //are you a backslash before a dash, if yes ignore
2757 else { individual += estim[i]; prevEscape = false; } //if no, add in
2758 }else { individual += estim[i]; }
2760 individual += estim[i];
2766 container.push_back(individual);
2768 catch(exception& e) {
2769 errorOut(e, "MothurOut", "splitAtDash");
2774 /***********************************************************************/
2775 //This function parses the label options and puts them in a set
2776 void MothurOut::splitAtDash(string& estim, set<string>& container) {
2778 string individual = "";
2779 int estimLength = estim.size();
2780 bool prevEscape = false;
2782 for(int i=0;i<estimLength;i++){
2784 individual += estim[i];
2788 if(estim[i] == '\\'){
2791 else if(estim[i] == '-'){
2792 container.insert(individual);
2797 individual += estim[i];
2804 for(int i=0;i<estimLength;i++){
2805 if(estim[i] == '-'){
2806 if (prevEscape) { individual += estim[i]; prevEscape = false; } //add in dash because it was escaped.
2808 container.insert(individual);
2811 }else if(estim[i] == '\\'){
2812 if (i < estimLength-1) {
2813 if (estim[i+1] == '-') { prevEscape=true; } //are you a backslash before a dash, if yes ignore
2814 else { individual += estim[i]; prevEscape = false; } //if no, add in
2815 }else { individual += estim[i]; }
2817 individual += estim[i];
2820 container.insert(individual);
2823 catch(exception& e) {
2824 errorOut(e, "MothurOut", "splitAtDash");
2828 /***********************************************************************/
2829 //This function parses the line options and puts them in a set
2830 void MothurOut::splitAtDash(string& estim, set<int>& container) {
2832 string individual = "";
2834 int estimLength = estim.size();
2835 bool prevEscape = false;
2837 for(int i=0;i<estimLength;i++){
2839 individual += estim[i];
2843 if(estim[i] == '\\'){
2846 else if(estim[i] == '-'){
2847 convert(individual, lineNum); //convert the string to int
2848 container.insert(lineNum);
2853 individual += estim[i];
2859 for(int i=0;i<estimLength;i++){
2860 if(estim[i] == '-'){
2861 if (prevEscape) { individual += estim[i]; prevEscape = false; } //add in dash because it was escaped.
2863 convert(individual, lineNum); //convert the string to int
2864 container.insert(lineNum);
2867 }else if(estim[i] == '\\'){
2868 if (i < estimLength-1) {
2869 if (estim[i+1] == '-') { prevEscape=true; } //are you a backslash before a dash, if yes ignore
2870 else { individual += estim[i]; prevEscape = false; } //if no, add in
2871 }else { individual += estim[i]; }
2873 individual += estim[i];
2877 convert(individual, lineNum); //convert the string to int
2878 container.insert(lineNum);
2880 catch(exception& e) {
2881 errorOut(e, "MothurOut", "splitAtDash");
2886 /***********************************************************************/
2887 string MothurOut::makeList(vector<string>& names) {
2891 if (names.size() == 0) { return list; }
2893 for (int i = 0; i < names.size()-1; i++) { list += names[i] + ","; }
2896 list += names[names.size()-1];
2900 catch(exception& e) {
2901 errorOut(e, "MothurOut", "makeList");
2906 /***********************************************************************/
2907 //This function parses the a string and puts peices in a vector
2908 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
2910 string individual = "";
2911 int estimLength = estim.size();
2912 for(int i=0;i<estimLength;i++){
2913 if(estim[i] == ','){
2914 container.push_back(individual);
2918 individual += estim[i];
2921 container.push_back(individual);
2926 // string individual;
2928 // while (estim.find_first_of(',') != -1) {
2929 // individual = estim.substr(0,estim.find_first_of(','));
2930 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
2931 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
2932 // container.push_back(individual);
2936 // container.push_back(estim);
2938 catch(exception& e) {
2939 errorOut(e, "MothurOut", "splitAtComma");
2943 /***********************************************************************/
2944 //This function splits up the various option parameters
2945 void MothurOut::splitAtChar(string& prefix, string& suffix, char c){
2947 prefix = suffix.substr(0,suffix.find_first_of(c));
2948 if ((suffix.find_first_of(c)+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2949 suffix = suffix.substr(suffix.find_first_of(c)+1, suffix.length());
2951 while(suffix.at(0) == ' ')
2952 suffix = suffix.substr(1, suffix.length());
2953 }else { suffix = ""; }
2956 catch(exception& e) {
2957 errorOut(e, "MothurOut", "splitAtChar");
2962 /***********************************************************************/
2964 //This function splits up the various option parameters
2965 void MothurOut::splitAtComma(string& prefix, string& suffix){
2967 prefix = suffix.substr(0,suffix.find_first_of(','));
2968 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2969 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
2971 while(suffix.at(0) == ' ')
2972 suffix = suffix.substr(1, suffix.length());
2973 }else { suffix = ""; }
2976 catch(exception& e) {
2977 errorOut(e, "MothurOut", "splitAtComma");
2981 /***********************************************************************/
2983 //This function separates the key value from the option value i.e. dist=96_...
2984 void MothurOut::splitAtEquals(string& key, string& value){
2986 if(value.find_first_of('=') != -1){
2987 key = value.substr(0,value.find_first_of('='));
2988 if ((value.find_first_of('=')+1) <= value.length()) {
2989 value = value.substr(value.find_first_of('=')+1, value.length());
2996 catch(exception& e) {
2997 errorOut(e, "MothurOut", "splitAtEquals");
3002 /**************************************************************************************************/
3004 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
3006 for (int i = 0; i < Groups.size(); i++) {
3007 if (groupname == Groups[i]) { return true; }
3011 catch(exception& e) {
3012 errorOut(e, "MothurOut", "inUsersGroups");
3016 /**************************************************************************************************/
3018 bool MothurOut::inUsersGroups(vector<int> set, vector< vector<int> > sets) {
3020 for (int i = 0; i < sets.size(); i++) {
3021 if (set == sets[i]) { return true; }
3025 catch(exception& e) {
3026 errorOut(e, "MothurOut", "inUsersGroups");
3030 /**************************************************************************************************/
3032 bool MothurOut::inUsersGroups(int groupname, vector<int> Groups) {
3034 for (int i = 0; i < Groups.size(); i++) {
3035 if (groupname == Groups[i]) { return true; }
3039 catch(exception& e) {
3040 errorOut(e, "MothurOut", "inUsersGroups");
3045 /**************************************************************************************************/
3046 //returns true if any of the strings in first vector are in second vector
3047 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
3050 for (int i = 0; i < groupnames.size(); i++) {
3051 if (inUsersGroups(groupnames[i], Groups)) { return true; }
3055 catch(exception& e) {
3056 errorOut(e, "MothurOut", "inUsersGroups");
3060 /***********************************************************************/
3061 //this function determines if the user has given us labels that are smaller than the given label.
3062 //if so then it returns true so that the calling function can run the previous valid distance.
3063 //it's a "smart" distance function. It also checks for invalid labels.
3064 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
3067 set<string>::iterator it;
3068 vector<float> orderFloat;
3069 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
3070 map<string, float>::iterator it2;
3072 bool smaller = false;
3074 //unique is the smallest line
3075 if (label == "unique") { return false; }
3077 if (convertTestFloat(label, labelFloat)) {
3078 convert(label, labelFloat);
3079 }else { //cant convert
3084 //go through users set and make them floats
3085 for(it = userLabels.begin(); it != userLabels.end();) {
3088 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
3090 orderFloat.push_back(temp);
3091 userMap[*it] = temp;
3093 }else if (*it == "unique") {
3094 orderFloat.push_back(-1.0);
3095 userMap["unique"] = -1.0;
3098 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
3099 userLabels.erase(it++);
3104 sort(orderFloat.begin(), orderFloat.end());
3106 /*************************************************/
3107 //is this label bigger than any of the users labels
3108 /*************************************************/
3110 //loop through order until you find a label greater than label
3111 for (int i = 0; i < orderFloat.size(); i++) {
3112 if (orderFloat[i] < labelFloat) {
3114 if (orderFloat[i] == -1) {
3115 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
3116 userLabels.erase("unique");
3119 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
3121 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
3122 if (it2->second == orderFloat[i]) {
3124 //remove small labels
3125 userLabels.erase(s);
3129 if (errorOff == "") {mothurOut( s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
3131 //since they are sorted once you find a bigger one stop looking
3138 catch(exception& e) {
3139 errorOut(e, "MothurOut", "anyLabelsToProcess");
3144 /**************************************************************************************************/
3145 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
3150 string line = getline(file);
3152 //before we added this check
3153 if (line[0] != '#') { good = false; }
3156 line = line.substr(1);
3158 vector<string> versionVector;
3159 splitAtChar(version, versionVector, '.');
3161 //check file version
3162 vector<string> linesVector;
3163 splitAtChar(line, linesVector, '.');
3165 if (versionVector.size() != linesVector.size()) { good = false; }
3167 for (int j = 0; j < versionVector.size(); j++) {
3169 convert(versionVector[j], num1);
3170 convert(linesVector[j], num2);
3172 //if mothurs version is newer than this files version, then we want to remake it
3173 if (num1 > num2) { good = false; break; }
3179 if (!good) { file.close(); }
3180 else { file.seekg(0); }
3184 catch(exception& e) {
3185 errorOut(e, "MothurOut", "checkReleaseVersion");
3189 /**************************************************************************************************/
3190 vector<double> MothurOut::getAverages(vector< vector<double> >& dists) {
3192 vector<double> averages; //averages.resize(numComp, 0.0);
3193 for (int i = 0; i < dists[0].size(); i++) { averages.push_back(0.0); }
3195 for (int thisIter = 0; thisIter < dists.size(); thisIter++) {
3196 for (int i = 0; i < dists[thisIter].size(); i++) {
3197 averages[i] += dists[thisIter][i];
3202 for (int i = 0; i < averages.size(); i++) { averages[i] /= (double) dists.size(); }
3206 catch(exception& e) {
3207 errorOut(e, "MothurOut", "getAverages");
3211 /**************************************************************************************************/
3212 vector<double> MothurOut::getStandardDeviation(vector< vector<double> >& dists) {
3215 vector<double> averages = getAverages(dists);
3217 //find standard deviation
3218 vector<double> stdDev; //stdDev.resize(numComp, 0.0);
3219 for (int i = 0; i < dists[0].size(); i++) { stdDev.push_back(0.0); }
3221 for (int thisIter = 0; thisIter < dists.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3222 for (int j = 0; j < dists[thisIter].size(); j++) {
3223 stdDev[j] += ((dists[thisIter][j] - averages[j]) * (dists[thisIter][j] - averages[j]));
3226 for (int i = 0; i < stdDev.size(); i++) {
3227 stdDev[i] /= (double) dists.size();
3228 stdDev[i] = sqrt(stdDev[i]);
3233 catch(exception& e) {
3234 errorOut(e, "MothurOut", "getAverages");
3238 /**************************************************************************************************/
3239 vector<double> MothurOut::getStandardDeviation(vector< vector<double> >& dists, vector<double>& averages) {
3241 //find standard deviation
3242 vector<double> stdDev; //stdDev.resize(numComp, 0.0);
3243 for (int i = 0; i < dists[0].size(); i++) { stdDev.push_back(0.0); }
3245 for (int thisIter = 0; thisIter < dists.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3246 for (int j = 0; j < dists[thisIter].size(); j++) {
3247 stdDev[j] += ((dists[thisIter][j] - averages[j]) * (dists[thisIter][j] - averages[j]));
3250 for (int i = 0; i < stdDev.size(); i++) {
3251 stdDev[i] /= (double) dists.size();
3252 stdDev[i] = sqrt(stdDev[i]);
3257 catch(exception& e) {
3258 errorOut(e, "MothurOut", "getAverages");
3262 /**************************************************************************************************/
3263 vector< vector<seqDist> > MothurOut::getAverages(vector< vector< vector<seqDist> > >& calcDistsTotals, string mode) {
3266 vector< vector<seqDist> > calcAverages; //calcAverages.resize(calcDistsTotals[0].size());
3267 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3268 //calcAverages[i].resize(calcDistsTotals[0][i].size());
3269 vector<seqDist> temp;
3270 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3272 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3273 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3274 tempDist.dist = 0.0;
3275 temp.push_back(tempDist);
3277 calcAverages.push_back(temp);
3280 if (mode == "average") {
3281 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //sum all groups dists for each calculator
3282 for (int i = 0; i < calcAverages.size(); i++) { //initialize sums to zero.
3283 for (int j = 0; j < calcAverages[i].size(); j++) {
3284 calcAverages[i][j].dist += calcDistsTotals[thisIter][i][j].dist;
3289 for (int i = 0; i < calcAverages.size(); i++) { //finds average.
3290 for (int j = 0; j < calcAverages[i].size(); j++) {
3291 calcAverages[i][j].dist /= (float) calcDistsTotals.size();
3294 }else { //find median
3295 for (int i = 0; i < calcAverages.size(); i++) { //for each calc
3296 for (int j = 0; j < calcAverages[i].size(); j++) { //for each comparison
3297 vector<double> dists;
3298 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //for each subsample
3299 dists.push_back(calcDistsTotals[thisIter][i][j].dist);
3301 sort(dists.begin(), dists.end());
3302 calcAverages[i][j].dist = dists[(calcDistsTotals.size()/2)];
3307 return calcAverages;
3309 catch(exception& e) {
3310 errorOut(e, "MothurOut", "getAverages");
3314 /**************************************************************************************************/
3315 vector< vector<seqDist> > MothurOut::getAverages(vector< vector< vector<seqDist> > >& calcDistsTotals) {
3318 vector< vector<seqDist> > calcAverages; //calcAverages.resize(calcDistsTotals[0].size());
3319 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3320 //calcAverages[i].resize(calcDistsTotals[0][i].size());
3321 vector<seqDist> temp;
3322 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3324 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3325 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3326 tempDist.dist = 0.0;
3327 temp.push_back(tempDist);
3329 calcAverages.push_back(temp);
3333 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //sum all groups dists for each calculator
3334 for (int i = 0; i < calcAverages.size(); i++) { //initialize sums to zero.
3335 for (int j = 0; j < calcAverages[i].size(); j++) {
3336 calcAverages[i][j].dist += calcDistsTotals[thisIter][i][j].dist;
3341 for (int i = 0; i < calcAverages.size(); i++) { //finds average.
3342 for (int j = 0; j < calcAverages[i].size(); j++) {
3343 calcAverages[i][j].dist /= (float) calcDistsTotals.size();
3347 return calcAverages;
3349 catch(exception& e) {
3350 errorOut(e, "MothurOut", "getAverages");
3354 /**************************************************************************************************/
3355 vector< vector<seqDist> > MothurOut::getStandardDeviation(vector< vector< vector<seqDist> > >& calcDistsTotals) {
3358 vector< vector<seqDist> > calcAverages = getAverages(calcDistsTotals);
3360 //find standard deviation
3361 vector< vector<seqDist> > stdDev;
3362 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3363 vector<seqDist> temp;
3364 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3366 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3367 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3368 tempDist.dist = 0.0;
3369 temp.push_back(tempDist);
3371 stdDev.push_back(temp);
3374 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3375 for (int i = 0; i < stdDev.size(); i++) {
3376 for (int j = 0; j < stdDev[i].size(); j++) {
3377 stdDev[i][j].dist += ((calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist) * (calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist));
3382 for (int i = 0; i < stdDev.size(); i++) { //finds average.
3383 for (int j = 0; j < stdDev[i].size(); j++) {
3384 stdDev[i][j].dist /= (float) calcDistsTotals.size();
3385 stdDev[i][j].dist = sqrt(stdDev[i][j].dist);
3391 catch(exception& e) {
3392 errorOut(e, "MothurOut", "getAverages");
3396 /**************************************************************************************************/
3397 vector< vector<seqDist> > MothurOut::getStandardDeviation(vector< vector< vector<seqDist> > >& calcDistsTotals, vector< vector<seqDist> >& calcAverages) {
3399 //find standard deviation
3400 vector< vector<seqDist> > stdDev;
3401 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3402 vector<seqDist> temp;
3403 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3405 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3406 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3407 tempDist.dist = 0.0;
3408 temp.push_back(tempDist);
3410 stdDev.push_back(temp);
3413 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3414 for (int i = 0; i < stdDev.size(); i++) {
3415 for (int j = 0; j < stdDev[i].size(); j++) {
3416 stdDev[i][j].dist += ((calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist) * (calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist));
3421 for (int i = 0; i < stdDev.size(); i++) { //finds average.
3422 for (int j = 0; j < stdDev[i].size(); j++) {
3423 stdDev[i][j].dist /= (float) calcDistsTotals.size();
3424 stdDev[i][j].dist = sqrt(stdDev[i][j].dist);
3430 catch(exception& e) {
3431 errorOut(e, "MothurOut", "getAverages");
3436 /**************************************************************************************************/
3437 bool MothurOut::isContainingOnlyDigits(string input) {
3440 //are you a digit in ascii code
3441 for (int i = 0;i < input.length(); i++){
3442 if( input[i]>47 && input[i]<58){}
3443 else { return false; }
3448 catch(exception& e) {
3449 errorOut(e, "MothurOut", "isContainingOnlyDigits");
3453 /**************************************************************************************************/
3454 int MothurOut::removeConfidences(string& tax) {
3460 while (tax.find_first_of(';') != -1) {
3462 if (control_pressed) { return 0; }
3465 taxon = tax.substr(0,tax.find_first_of(';'));
3467 int pos = taxon.find_last_of('(');
3470 int pos2 = taxon.find_last_of(')');
3472 string confidenceScore = taxon.substr(pos+1, (pos2-(pos+1)));
3473 if (isNumeric1(confidenceScore)) {
3474 taxon = taxon.substr(0, pos); //rip off confidence
3480 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
3488 catch(exception& e) {
3489 errorOut(e, "MothurOut", "removeConfidences");
3493 /**************************************************************************************************/
3494 string MothurOut::removeQuotes(string tax) {
3500 for (int i = 0; i < tax.length(); i++) {
3502 if (control_pressed) { return newTax; }
3504 if ((tax[i] != '\'') && (tax[i] != '\"')) { newTax += tax[i]; }
3510 catch(exception& e) {
3511 errorOut(e, "MothurOut", "removeQuotes");
3515 /**************************************************************************************************/
3516 // function for calculating standard deviation
3517 double MothurOut::getStandardDeviation(vector<int>& featureVector){
3521 for (int i = 0; i < featureVector.size(); i++) { average += featureVector[i]; }
3522 average /= (double) featureVector.size();
3524 //find standard deviation
3526 for (int i = 0; i < featureVector.size(); i++) { //compute the difference of each dist from the mean, and square the result of each
3527 stdDev += ((featureVector[i] - average) * (featureVector[i] - average));
3530 stdDev /= (double) featureVector.size();
3531 stdDev = sqrt(stdDev);
3535 catch(exception& e) {
3536 errorOut(e, "MothurOut", "getStandardDeviation");
3540 /**************************************************************************************************/