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.");
456 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, 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. 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.");
460 /*********************************************************************************************/
461 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
462 // process_mem_usage(double &, double &) - takes two doubles by reference,
463 // attempts to read the system-dependent data for a process' virtual memory
464 // size and resident set size, and return the results in KB.
466 // On failure, returns 0.0, 0.0
467 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
468 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
473 // 'file' stat seems to give the most reliable results
475 ifstream stat_stream("/proc/self/stat",ios_base::in);
477 // dummy vars for leading entries in stat that we don't care about
479 string pid, comm, state, ppid, pgrp, session, tty_nr;
480 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
481 string utime, stime, cutime, cstime, priority, nice;
482 string O, itrealvalue, starttime;
484 // the two fields we want
489 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
490 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
491 >> utime >> stime >> cutime >> cstime >> priority >> nice
492 >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
494 long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
495 vm_usage = vsize / 1024.0;
496 resident_set = rss * page_size_kb;
498 mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
502 /* //windows memory usage
503 // Get the list of process identifiers.
504 DWORD aProcesses[1024], cbNeeded, cProcesses;
506 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
508 // Calculate how many process identifiers were returned.
509 cProcesses = cbNeeded / sizeof(DWORD);
511 // Print the memory usage for each process
512 for (int i = 0; i < cProcesses; i++ ) {
513 DWORD processID = aProcesses[i];
515 PROCESS_MEMORY_COUNTERS pmc;
517 HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
519 // Print the process identifier.
520 printf( "\nProcess ID: %u\n", processID);
522 if (NULL != hProcess) {
524 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
525 printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
526 printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
527 printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
528 printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
529 printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
530 printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
531 printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
532 printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
533 printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
535 CloseHandle(hProcess);
545 /***********************************************************************/
546 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
548 fileName = getFullPathName(fileName);
550 fileHandle.open(fileName.c_str(), ios::app);
552 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
559 catch(exception& e) {
560 errorOut(e, "MothurOut", "openOutputFileAppend");
564 /***********************************************************************/
565 void MothurOut::gobble(istream& f){
569 while(isspace(d=f.get())) { ;}
570 if(!f.eof()) { f.putback(d); }
572 catch(exception& e) {
573 errorOut(e, "MothurOut", "gobble");
577 /***********************************************************************/
578 void MothurOut::gobble(istringstream& f){
581 while(isspace(d=f.get())) {;}
582 if(!f.eof()) { f.putback(d); }
584 catch(exception& e) {
585 errorOut(e, "MothurOut", "gobble");
590 /***********************************************************************/
592 string MothurOut::getline(istringstream& fileHandle) {
597 while (!fileHandle.eof()) {
599 char c = fileHandle.get();
601 //are you at the end of the line
602 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
609 catch(exception& e) {
610 errorOut(e, "MothurOut", "getline");
614 /***********************************************************************/
616 string MothurOut::getline(ifstream& fileHandle) {
623 char c = fileHandle.get();
625 //are you at the end of the line
626 if ((c == '\n') || (c == '\r') || (c == '\f') || (c == EOF)){ break; }
633 catch(exception& e) {
634 errorOut(e, "MothurOut", "getline");
638 /***********************************************************************/
640 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
641 #ifdef USE_COMPRESSION
642 inline bool endsWith(string s, const char * suffix){
643 size_t suffixLength = strlen(suffix);
644 return s.size() >= suffixLength && s.substr(s.size() - suffixLength, suffixLength).compare(suffix) == 0;
649 string MothurOut::getRootName(string longName){
652 string rootName = longName;
654 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
655 #ifdef USE_COMPRESSION
656 if (endsWith(rootName, ".gz") || endsWith(rootName, ".bz2")) {
657 int pos = rootName.find_last_of('.');
658 rootName = rootName.substr(0, pos);
659 cerr << "shortening " << longName << " to " << rootName << "\n";
663 if(rootName.find_last_of(".") != rootName.npos){
664 int pos = rootName.find_last_of('.')+1;
665 rootName = rootName.substr(0, pos);
670 catch(exception& e) {
671 errorOut(e, "MothurOut", "getRootName");
675 /***********************************************************************/
677 string MothurOut::getSimpleName(string longName){
679 string simpleName = longName;
682 found=longName.find_last_of("/\\");
684 if(found != longName.npos){
685 simpleName = longName.substr(found+1);
690 catch(exception& e) {
691 errorOut(e, "MothurOut", "getSimpleName");
696 /***********************************************************************/
698 int MothurOut::getRandomIndex(int highest){
701 int random = (int) ((float)(highest+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
705 catch(exception& e) {
706 errorOut(e, "MothurOut", "getRandomIndex");
711 /**********************************************************************/
713 string MothurOut::getPathName(string longName){
715 string rootPathName = longName;
717 if(longName.find_last_of("/\\") != longName.npos){
718 int pos = longName.find_last_of("/\\")+1;
719 rootPathName = longName.substr(0, pos);
724 catch(exception& e) {
725 errorOut(e, "MothurOut", "getPathName");
730 /***********************************************************************/
732 bool MothurOut::dirCheck(string& dirName){
738 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
743 //add / to name if needed
744 string lastChar = dirName.substr(dirName.length()-1);
745 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
746 if (lastChar != "/") { dirName += "/"; }
748 if (lastChar != "\\") { dirName += "\\"; }
751 //test to make sure directory exists
752 dirName = getFullPathName(dirName);
753 string outTemp = dirName + tag + "temp";
755 out.open(outTemp.c_str(), ios::trunc);
757 mothurOut(dirName + " directory does not exist or is not writable."); mothurOutEndLine();
760 mothurRemove(outTemp);
766 catch(exception& e) {
767 errorOut(e, "MothurOut", "dirCheck");
772 /***********************************************************************/
774 string MothurOut::hasPath(string longName){
779 found=longName.find_last_of("~/\\");
781 if(found != longName.npos){
782 path = longName.substr(0, found+1);
787 catch(exception& e) {
788 errorOut(e, "MothurOut", "hasPath");
793 /***********************************************************************/
795 string MothurOut::getExtension(string longName){
797 string extension = "";
799 if(longName.find_last_of('.') != longName.npos){
800 int pos = longName.find_last_of('.');
801 extension = longName.substr(pos, longName.length());
806 catch(exception& e) {
807 errorOut(e, "MothurOut", "getExtension");
811 /***********************************************************************/
812 bool MothurOut::isBlank(string fileName){
815 fileName = getFullPathName(fileName);
818 fileHandle.open(fileName.c_str());
820 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
823 //check for blank file
825 if (fileHandle.eof()) { fileHandle.close(); return true; }
830 catch(exception& e) {
831 errorOut(e, "MothurOut", "isBlank");
835 /***********************************************************************/
837 string MothurOut::getFullPathName(string fileName){
840 string path = hasPath(fileName);
844 if (path == "") { return fileName; } //its a simple name
845 else { //we need to complete the pathname
846 // ex. ../../../filename
847 // cwd = /user/work/desktop
850 //get current working directory
851 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
853 if (path.find("~") != -1) { //go to home directory
856 char *homepath = NULL;
857 homepath = getenv ("HOME");
858 if ( homepath != NULL) { homeDir = homepath; }
859 else { homeDir = ""; }
861 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
864 if (path.rfind("./") == string::npos) { return fileName; } //already complete name
865 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
867 //char* cwdpath = new char[1024];
869 //cwdpath=getcwd(cwdpath,size);
872 char *cwdpath = NULL;
873 cwdpath = getcwd(NULL, 0); // or _getcwd
874 if ( cwdpath != NULL) { cwd = cwdpath; }
880 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
882 //break apart the current working directory
884 while (simpleCWD.find_first_of('/') != string::npos) {
885 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
886 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
889 //get last one // ex. ../../../filename = /user/work/desktop/filename
890 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
893 int index = dirs.size()-1;
895 while((pos = path.rfind("./")) != string::npos) { //while you don't have a complete path
896 if (pos == 0) { break; //you are at the end
897 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
898 path = path.substr(0, pos-1);
900 if (index == 0) { break; }
901 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
902 path = path.substr(0, pos);
903 }else if (pos == 1) { break; //you are at the end
904 }else { mothurOut("cannot resolve path for " + fileName + "\n"); return fileName; }
907 for (int i = index; i >= 0; i--) {
908 newFileName = dirs[i] + "/" + newFileName;
911 newFileName = "/" + newFileName;
915 if (path.find("~") != string::npos) { //go to home directory
916 string homeDir = getenv ("HOMEPATH");
917 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
920 if (path.rfind(".\\") == string::npos) { return fileName; } //already complete name
921 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
923 char *cwdpath = NULL;
924 cwdpath = getcwd(NULL, 0); // or _getcwd
925 if ( cwdpath != NULL) { cwd = cwdpath; }
928 //break apart the current working directory
930 while (cwd.find_first_of('\\') != -1) {
931 string dir = cwd.substr(0,cwd.find_first_of('\\'));
932 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
937 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
939 int index = dirs.size()-1;
941 while((pos = path.rfind(".\\")) != string::npos) { //while you don't have a complete path
942 if (pos == 0) { break; //you are at the end
943 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
944 path = path.substr(0, pos-1);
946 if (index == 0) { break; }
947 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
948 path = path.substr(0, pos);
949 }else if (pos == 1) { break; //you are at the end
950 }else { mothurOut("cannot resolve path for " + fileName + "\n"); return fileName; }
953 for (int i = index; i >= 0; i--) {
954 newFileName = dirs[i] + "\\\\" + newFileName;
963 catch(exception& e) {
964 errorOut(e, "MothurOut", "getFullPathName");
968 /***********************************************************************/
970 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
973 string completeFileName = getFullPathName(fileName);
974 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
975 #ifdef USE_COMPRESSION
976 // check for gzipped or bzipped file
977 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
978 string tempName = string(tmpnam(0));
979 mkfifo(tempName.c_str(), 0666);
980 int fork_result = fork();
981 if (fork_result < 0) {
982 cerr << "Error forking.\n";
984 } else if (fork_result == 0) {
985 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
986 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
987 system(command.c_str());
988 cerr << "Done decompressing " << completeFileName << "\n";
989 mothurRemove(tempName);
992 cerr << "waiting on child process " << fork_result << "\n";
993 completeFileName = tempName;
998 fileHandle.open(completeFileName.c_str());
1000 //mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1003 //check for blank file
1008 catch(exception& e) {
1009 errorOut(e, "MothurOut", "openInputFile - no Error");
1013 /***********************************************************************/
1015 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
1018 //get full path name
1019 string completeFileName = getFullPathName(fileName);
1020 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1021 #ifdef USE_COMPRESSION
1022 // check for gzipped or bzipped file
1023 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
1024 string tempName = string(tmpnam(0));
1025 mkfifo(tempName.c_str(), 0666);
1026 int fork_result = fork();
1027 if (fork_result < 0) {
1028 cerr << "Error forking.\n";
1030 } else if (fork_result == 0) {
1031 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
1032 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
1033 system(command.c_str());
1034 cerr << "Done decompressing " << completeFileName << "\n";
1035 mothurRemove(tempName);
1038 cerr << "waiting on child process " << fork_result << "\n";
1039 completeFileName = tempName;
1045 fileHandle.open(completeFileName.c_str());
1047 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1051 //check for blank file
1053 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
1058 catch(exception& e) {
1059 errorOut(e, "MothurOut", "openInputFile");
1063 /***********************************************************************/
1065 int MothurOut::renameFile(string oldName, string newName){
1068 if (oldName == newName) { return 0; }
1071 int exist = openInputFile(newName, inTest, "");
1074 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1075 if (exist == 0) { //you could open it so you want to delete it
1076 string command = "rm " + newName;
1077 system(command.c_str());
1080 string command = "mv " + oldName + " " + newName;
1081 system(command.c_str());
1083 mothurRemove(newName);
1084 int renameOk = rename(oldName.c_str(), newName.c_str());
1089 catch(exception& e) {
1090 errorOut(e, "MothurOut", "renameFile");
1095 /***********************************************************************/
1097 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
1100 string completeFileName = getFullPathName(fileName);
1101 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1102 #ifdef USE_COMPRESSION
1103 // check for gzipped file
1104 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
1105 string tempName = string(tmpnam(0));
1106 mkfifo(tempName.c_str(), 0666);
1107 cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
1108 int fork_result = fork();
1109 if (fork_result < 0) {
1110 cerr << "Error forking.\n";
1112 } else if (fork_result == 0) {
1113 string command = string(endsWith(completeFileName, ".gz") ? "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
1114 system(command.c_str());
1117 completeFileName = tempName;
1122 fileHandle.open(completeFileName.c_str(), ios::trunc);
1124 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1131 catch(exception& e) {
1132 errorOut(e, "MothurOut", "openOutputFile");
1138 /**************************************************************************************************/
1139 int MothurOut::appendFiles(string temp, string filename) {
1144 //open output file in append mode
1145 openOutputFileAppend(filename, output);
1146 int ableToOpen = openInputFile(temp, input, "no error");
1147 //int ableToOpen = openInputFile(temp, input);
1150 if (ableToOpen == 0) { //you opened it
1153 while (!input.eof()) {
1154 input.read(buffer, 4096);
1155 output.write(buffer, input.gcount());
1156 //count number of lines
1157 for (int i = 0; i < input.gcount(); i++) { if (buffer[i] == '\n') {numLines++;} }
1166 catch(exception& e) {
1167 errorOut(e, "MothurOut", "appendFiles");
1172 /**************************************************************************************************/
1173 string MothurOut::sortFile(string distFile, string outputDir){
1176 //if (outputDir == "") { outputDir += hasPath(distFile); }
1177 string outfile = getRootName(distFile) + "sorted.dist";
1180 //if you can, use the unix sort since its been optimized for years
1181 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1182 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
1183 system(command.c_str());
1184 #else //you are stuck with my best attempt...
1185 //windows sort does not have a way to specify a column, only a character in the line
1186 //since we cannot assume that the distance will always be at the the same character location on each line
1187 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
1189 //read in file line by file and put distance first
1190 string tempDistFile = distFile + ".temp";
1193 openInputFile(distFile, input);
1194 openOutputFile(tempDistFile, output);
1196 string firstName, secondName;
1198 while (!input.eof()) {
1199 input >> firstName >> secondName >> dist;
1200 output << dist << '\t' << firstName << '\t' << secondName << endl;
1207 //sort using windows sort
1208 string tempOutfile = outfile + ".temp";
1209 string command = "sort " + tempDistFile + " /O " + tempOutfile;
1210 system(command.c_str());
1212 //read in sorted file and put distance at end again
1215 openInputFile(tempOutfile, input2);
1216 openOutputFile(outfile, output2);
1218 while (!input2.eof()) {
1219 input2 >> dist >> firstName >> secondName;
1220 output2 << firstName << '\t' << secondName << '\t' << dist << endl;
1227 mothurRemove(tempDistFile);
1228 mothurRemove(tempOutfile);
1233 catch(exception& e) {
1234 errorOut(e, "MothurOut", "sortFile");
1238 /**************************************************************************************************/
1239 vector<unsigned long long> MothurOut::setFilePosFasta(string filename, int& num) {
1241 vector<unsigned long long> positions;
1243 //openInputFile(filename, inFASTA);
1244 inFASTA.open(filename.c_str(), ios::binary);
1247 unsigned long long count = 0;
1248 while(!inFASTA.eof()){
1249 //input = getline(inFASTA);
1250 //cout << input << '\t' << inFASTA.tellg() << endl;
1251 //if (input.length() != 0) {
1252 // if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); cout << (pos - input.length() - 1) << endl; }
1254 //gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
1255 char c = inFASTA.get(); count++;
1257 positions.push_back(count-1);
1258 //cout << count << endl;
1263 num = positions.size();
1268 //get num bytes in file
1269 pFile = fopen (filename.c_str(),"rb");
1270 if (pFile==NULL) perror ("Error opening file");
1272 fseek (pFile, 0, SEEK_END);
1277 unsigned long long size = positions[(positions.size()-1)];
1279 openInputFile(filename, in);
1284 if(in.eof()) { break; }
1289 positions.push_back(size);
1294 catch(exception& e) {
1295 errorOut(e, "MothurOut", "setFilePosFasta");
1299 /**************************************************************************************************/
1300 vector<unsigned long long> MothurOut::setFilePosEachLine(string filename, int& num) {
1302 filename = getFullPathName(filename);
1304 vector<unsigned long long> positions;
1306 //openInputFile(filename, in);
1307 in.open(filename.c_str(), ios::binary);
1310 unsigned long long count = 0;
1311 positions.push_back(0);
1314 //getline counting reads
1315 char d = in.get(); count++;
1316 while ((d != '\n') && (d != '\r') && (d != '\f') && (d != in.eof())) {
1317 //get next character
1323 d=in.get(); count++;
1324 while(isspace(d) && (d != in.eof())) { d=in.get(); count++;}
1326 positions.push_back(count-1);
1327 //cout << count-1 << endl;
1331 num = positions.size()-1;
1334 unsigned long long size;
1336 //get num bytes in file
1337 pFile = fopen (filename.c_str(),"rb");
1338 if (pFile==NULL) perror ("Error opening file");
1340 fseek (pFile, 0, SEEK_END);
1345 positions[(positions.size()-1)] = size;
1349 catch(exception& e) {
1350 errorOut(e, "MothurOut", "setFilePosEachLine");
1354 /**************************************************************************************************/
1356 vector<unsigned long long> MothurOut::divideFile(string filename, int& proc) {
1358 vector<unsigned long long> filePos;
1359 filePos.push_back(0);
1362 unsigned long long size;
1364 filename = getFullPathName(filename);
1366 //get num bytes in file
1367 pFile = fopen (filename.c_str(),"rb");
1368 if (pFile==NULL) perror ("Error opening file");
1370 fseek (pFile, 0, SEEK_END);
1375 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1377 //estimate file breaks
1378 unsigned long long chunkSize = 0;
1379 chunkSize = size / proc;
1381 //file to small to divide by processors
1382 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1384 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1385 for (int i = 0; i < proc; i++) {
1386 unsigned long long spot = (i+1) * chunkSize;
1389 openInputFile(filename, in);
1393 unsigned long long newSpot = spot;
1397 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1398 else if (int(c) == -1) { break; }
1402 //there was not another sequence before the end of the file
1403 unsigned long long sanityPos = in.tellg();
1405 if (sanityPos == -1) { break; }
1406 else { filePos.push_back(newSpot); }
1412 filePos.push_back(size);
1414 //sanity check filePos
1415 for (int i = 0; i < (filePos.size()-1); i++) {
1416 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1419 proc = (filePos.size() - 1);
1421 mothurOut("[ERROR]: Windows version should not be calling the divideFile function."); mothurOutEndLine();
1423 filePos.push_back(size);
1427 catch(exception& e) {
1428 errorOut(e, "MothurOut", "divideFile");
1432 /**************************************************************************************************/
1433 int MothurOut::divideFile(string filename, int& proc, vector<string>& files) {
1436 vector<unsigned long long> filePos = divideFile(filename, proc);
1438 for (int i = 0; i < (filePos.size()-1); i++) {
1442 openInputFile(filename, in);
1443 in.seekg(filePos[i]);
1444 unsigned long long size = filePos[(i+1)] - filePos[i];
1445 char* chunk = new char[size];
1446 in.read(chunk, size);
1450 string fileChunkName = filename + "." + toString(i) + ".tmp";
1452 openOutputFile(fileChunkName, out);
1454 out << chunk << endl;
1459 files.push_back(fileChunkName);
1464 catch(exception& e) {
1465 errorOut(e, "MothurOut", "divideFile");
1469 /***********************************************************************/
1471 bool MothurOut::isTrue(string f){
1474 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1476 if ((f == "TRUE") || (f == "T")) { return true; }
1477 else { return false; }
1479 catch(exception& e) {
1480 errorOut(e, "MothurOut", "isTrue");
1485 /***********************************************************************/
1487 float MothurOut::roundDist(float dist, int precision){
1489 return int(dist * precision + 0.5)/float(precision);
1491 catch(exception& e) {
1492 errorOut(e, "MothurOut", "roundDist");
1496 /***********************************************************************/
1498 float MothurOut::ceilDist(float dist, int precision){
1500 return int(ceil(dist * precision))/float(precision);
1502 catch(exception& e) {
1503 errorOut(e, "MothurOut", "ceilDist");
1507 /***********************************************************************/
1509 vector<string> MothurOut::splitWhiteSpace(string& rest, char buffer[], int size){
1511 vector<string> pieces;
1513 for (int i = 0; i < size; i++) {
1514 if (!isspace(buffer[i])) { rest += buffer[i]; }
1516 if (rest != "") { pieces.push_back(rest); rest = ""; }
1517 while (i < size) { //gobble white space
1518 if (isspace(buffer[i])) { i++; }
1519 else { rest = buffer[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1526 catch(exception& e) {
1527 errorOut(e, "MothurOut", "splitWhiteSpace");
1531 /***********************************************************************/
1532 vector<string> MothurOut::splitWhiteSpace(string input){
1534 vector<string> pieces;
1537 for (int i = 0; i < input.length(); i++) {
1538 if (!isspace(input[i])) { rest += input[i]; }
1540 if (rest != "") { pieces.push_back(rest); rest = ""; }
1541 while (i < input.length()) { //gobble white space
1542 if (isspace(input[i])) { i++; }
1543 else { rest = input[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1548 if (rest != "") { pieces.push_back(rest); }
1552 catch(exception& e) {
1553 errorOut(e, "MothurOut", "splitWhiteSpace");
1557 /***********************************************************************/
1558 vector<string> MothurOut::splitWhiteSpaceWithQuotes(string input){
1560 vector<string> pieces;
1563 int pos = input.find('\'');
1564 int pos2 = input.find('\"');
1566 if ((pos == string::npos) && (pos2 == string::npos)) { return splitWhiteSpace(input); } //no quotes to worry about
1568 for (int i = 0; i < input.length(); i++) {
1569 if ((input[i] == '\'') || (input[i] == '\"') || (rest == "\'") || (rest == "\"")) { //grab everything til end or next ' or "
1571 for (int j = i+1; j < input.length(); j++) {
1572 if ((input[j] == '\'') || (input[j] == '\"')) { //then quit
1576 }else { rest += input[j]; }
1578 }else if (!isspace(input[i])) { rest += input[i]; }
1580 if (rest != "") { pieces.push_back(rest); rest = ""; }
1581 while (i < input.length()) { //gobble white space
1582 if (isspace(input[i])) { i++; }
1583 else { rest = input[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1588 if (rest != "") { pieces.push_back(rest); }
1592 catch(exception& e) {
1593 errorOut(e, "MothurOut", "splitWhiteSpace");
1597 //**********************************************************************************************************************
1598 int MothurOut::readTax(string namefile, map<string, string>& taxMap) {
1602 openInputFile(namefile, in);
1606 bool pairDone = false;
1607 bool columnOne = true;
1608 string firstCol, secondCol;
1611 if (control_pressed) { break; }
1613 in.read(buffer, 4096);
1614 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1616 for (int i = 0; i < pieces.size(); i++) {
1617 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1618 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1621 checkName(firstCol);
1622 //are there confidence scores, if so remove them
1623 if (secondCol.find_first_of('(') != -1) { removeConfidences(secondCol); }
1624 map<string, string>::iterator itTax = taxMap.find(firstCol);
1626 if(itTax == taxMap.end()) {
1627 bool ignore = false;
1628 if (secondCol != "") { if (secondCol[secondCol.length()-1] != ';') { mothurOut("[ERROR]: " + firstCol + " is missing the final ';', ignoring.\n"); ignore=true; }
1630 if (!ignore) { taxMap[firstCol] = secondCol; }
1631 if (debug) { mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n"); }
1633 mothurOut("[ERROR]: " + firstCol + " is already in your taxonomy file, names must be unique./n"); control_pressed = true;
1642 vector<string> pieces = splitWhiteSpace(rest);
1644 for (int i = 0; i < pieces.size(); i++) {
1645 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1646 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1649 checkName(firstCol);
1650 //are there confidence scores, if so remove them
1651 if (secondCol.find_first_of('(') != -1) { removeConfidences(secondCol); }
1652 map<string, string>::iterator itTax = taxMap.find(firstCol);
1654 if(itTax == taxMap.end()) {
1655 bool ignore = false;
1656 if (secondCol != "") { if (secondCol[secondCol.length()-1] != ';') { mothurOut("[ERROR]: " + firstCol + " is missing the final ';', ignoring.\n"); ignore=true; }
1658 if (!ignore) { taxMap[firstCol] = secondCol; }
1659 if (debug) { mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n"); }
1661 mothurOut("[ERROR]: " + firstCol + " is already in your taxonomy file, names must be unique./n"); control_pressed = true;
1669 return taxMap.size();
1672 catch(exception& e) {
1673 errorOut(e, "MothurOut", "readTax");
1677 /**********************************************************************************************************************/
1678 int MothurOut::readNames(string namefile, map<string, string>& nameMap, bool redund) {
1682 openInputFile(namefile, in);
1686 bool pairDone = false;
1687 bool columnOne = true;
1688 string firstCol, secondCol;
1691 if (control_pressed) { break; }
1693 in.read(buffer, 4096);
1694 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1696 for (int i = 0; i < pieces.size(); i++) {
1697 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1698 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1701 checkName(firstCol);
1702 checkName(secondCol);
1704 //parse names into vector
1705 vector<string> theseNames;
1706 splitAtComma(secondCol, theseNames);
1707 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1715 vector<string> pieces = splitWhiteSpace(rest);
1717 for (int i = 0; i < pieces.size(); i++) {
1718 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1719 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1722 checkName(firstCol);
1723 checkName(secondCol);
1725 //parse names into vector
1726 vector<string> theseNames;
1727 splitAtComma(secondCol, theseNames);
1728 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1734 return nameMap.size();
1737 catch(exception& e) {
1738 errorOut(e, "MothurOut", "readNames");
1742 /**********************************************************************************************************************/
1743 int MothurOut::readNames(string namefile, map<string, string>& nameMap, int flip) {
1747 openInputFile(namefile, in);
1751 bool pairDone = false;
1752 bool columnOne = true;
1753 string firstCol, secondCol;
1756 if (control_pressed) { break; }
1758 in.read(buffer, 4096);
1759 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1761 for (int i = 0; i < pieces.size(); i++) {
1762 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1763 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1766 checkName(firstCol);
1767 checkName(secondCol);
1768 nameMap[secondCol] = firstCol;
1776 vector<string> pieces = splitWhiteSpace(rest);
1778 for (int i = 0; i < pieces.size(); i++) {
1779 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1780 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1783 checkName(firstCol);
1784 checkName(secondCol);
1785 nameMap[secondCol] = firstCol;
1791 return nameMap.size();
1794 catch(exception& e) {
1795 errorOut(e, "MothurOut", "readNames");
1799 /**********************************************************************************************************************/
1800 int MothurOut::readNames(string namefile, map<string, string>& nameMap, map<string, int>& nameCount) {
1802 nameMap.clear(); nameCount.clear();
1805 openInputFile(namefile, in);
1809 bool pairDone = false;
1810 bool columnOne = true;
1811 string firstCol, secondCol;
1814 if (control_pressed) { break; }
1816 in.read(buffer, 4096);
1817 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1819 for (int i = 0; i < pieces.size(); i++) {
1820 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1821 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1824 checkName(firstCol);
1825 checkName(secondCol);
1826 //parse names into vector
1827 vector<string> theseNames;
1828 splitAtComma(secondCol, theseNames);
1829 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1830 nameCount[firstCol] = theseNames.size();
1838 vector<string> pieces = splitWhiteSpace(rest);
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 //parse names into vector
1848 vector<string> theseNames;
1849 splitAtComma(secondCol, theseNames);
1850 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1851 nameCount[firstCol] = theseNames.size();
1857 return nameMap.size();
1860 catch(exception& e) {
1861 errorOut(e, "MothurOut", "readNames");
1865 /**********************************************************************************************************************/
1866 int MothurOut::readNames(string namefile, map<string, string>& nameMap) {
1870 openInputFile(namefile, in);
1874 bool pairDone = false;
1875 bool columnOne = true;
1876 string firstCol, secondCol;
1879 if (control_pressed) { break; }
1881 in.read(buffer, 4096);
1882 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1884 for (int i = 0; i < pieces.size(); i++) {
1885 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1886 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1889 checkName(firstCol);
1890 checkName(secondCol);
1891 nameMap[firstCol] = secondCol; pairDone = false; }
1897 vector<string> pieces = splitWhiteSpace(rest);
1899 for (int i = 0; i < pieces.size(); i++) {
1900 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1901 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1904 checkName(firstCol);
1905 checkName(secondCol);
1906 nameMap[firstCol] = secondCol; pairDone = false; }
1910 return nameMap.size();
1913 catch(exception& e) {
1914 errorOut(e, "MothurOut", "readNames");
1918 /**********************************************************************************************************************/
1919 int MothurOut::readNames(string namefile, map<string, vector<string> >& nameMap) {
1923 openInputFile(namefile, in);
1927 bool pairDone = false;
1928 bool columnOne = true;
1929 string firstCol, secondCol;
1932 if (control_pressed) { break; }
1934 in.read(buffer, 4096);
1935 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1937 for (int i = 0; i < pieces.size(); i++) {
1938 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1939 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1942 checkName(firstCol);
1943 checkName(secondCol);
1944 vector<string> temp;
1945 splitAtComma(secondCol, temp);
1946 nameMap[firstCol] = temp;
1954 vector<string> pieces = splitWhiteSpace(rest);
1956 for (int i = 0; i < pieces.size(); i++) {
1957 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1958 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1961 checkName(firstCol);
1962 checkName(secondCol);
1963 vector<string> temp;
1964 splitAtComma(secondCol, temp);
1965 nameMap[firstCol] = temp;
1971 return nameMap.size();
1973 catch(exception& e) {
1974 errorOut(e, "MothurOut", "readNames");
1978 /**********************************************************************************************************************/
1979 map<string, int> MothurOut::readNames(string namefile) {
1981 map<string, int> nameMap;
1985 openInputFile(namefile, in);
1989 bool pairDone = false;
1990 bool columnOne = true;
1991 string firstCol, secondCol;
1994 if (control_pressed) { break; }
1996 in.read(buffer, 4096);
1997 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1999 for (int i = 0; i < pieces.size(); i++) {
2000 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2001 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2004 checkName(firstCol);
2005 checkName(secondCol);
2006 int num = getNumNames(secondCol);
2007 nameMap[firstCol] = num;
2015 vector<string> pieces = splitWhiteSpace(rest);
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 int num = getNumNames(secondCol);
2024 nameMap[firstCol] = num;
2033 catch(exception& e) {
2034 errorOut(e, "MothurOut", "readNames");
2038 /**********************************************************************************************************************/
2039 map<string, int> MothurOut::readNames(string namefile, unsigned long int& numSeqs) {
2041 map<string, int> nameMap;
2046 openInputFile(namefile, in);
2050 bool pairDone = false;
2051 bool columnOne = true;
2052 string firstCol, secondCol;
2055 if (control_pressed) { break; }
2057 in.read(buffer, 4096);
2058 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2060 for (int i = 0; i < pieces.size(); i++) {
2061 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2062 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2065 checkName(firstCol);
2066 checkName(secondCol);
2067 int num = getNumNames(secondCol);
2068 nameMap[firstCol] = num;
2077 vector<string> pieces = splitWhiteSpace(rest);
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;
2096 catch(exception& e) {
2097 errorOut(e, "MothurOut", "readNames");
2101 /************************************************************/
2102 int MothurOut::checkName(string& name) {
2104 for (int i = 0; i < name.length(); i++) {
2105 if (name[i] == ':') { name[i] = '_'; changedSeqNames = true; }
2109 catch(exception& e) {
2110 errorOut(e, "MothurOut", "checkName");
2114 /**********************************************************************************************************************/
2115 int MothurOut::readNames(string namefile, vector<seqPriorityNode>& nameVector, map<string, string>& fastamap) {
2121 openInputFile(namefile, in);
2125 bool pairDone = false;
2126 bool columnOne = true;
2127 string firstCol, secondCol;
2130 if (control_pressed) { break; }
2132 in.read(buffer, 4096);
2133 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2135 for (int i = 0; i < pieces.size(); i++) {
2136 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2137 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2140 checkName(firstCol);
2141 checkName(secondCol);
2142 int num = getNumNames(secondCol);
2144 map<string, string>::iterator it = fastamap.find(firstCol);
2145 if (it == fastamap.end()) {
2147 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
2149 seqPriorityNode temp(num, it->second, firstCol);
2150 nameVector.push_back(temp);
2160 vector<string> pieces = splitWhiteSpace(rest);
2162 for (int i = 0; i < pieces.size(); i++) {
2163 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2164 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2167 checkName(firstCol);
2168 checkName(secondCol);
2169 int num = getNumNames(secondCol);
2171 map<string, string>::iterator it = fastamap.find(firstCol);
2172 if (it == fastamap.end()) {
2174 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
2176 seqPriorityNode temp(num, it->second, firstCol);
2177 nameVector.push_back(temp);
2186 catch(exception& e) {
2187 errorOut(e, "MothurOut", "readNames");
2191 //**********************************************************************************************************************
2192 set<string> MothurOut::readAccnos(string accnosfile){
2196 openInputFile(accnosfile, in);
2203 if (control_pressed) { break; }
2205 in.read(buffer, 4096);
2206 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2208 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.insert(pieces[i]); }
2213 vector<string> pieces = splitWhiteSpace(rest);
2214 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.insert(pieces[i]); }
2218 catch(exception& e) {
2219 errorOut(e, "MothurOut", "readAccnos");
2223 //**********************************************************************************************************************
2224 int MothurOut::readAccnos(string accnosfile, vector<string>& names){
2228 openInputFile(accnosfile, in);
2235 if (control_pressed) { break; }
2237 in.read(buffer, 4096);
2238 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2240 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.push_back(pieces[i]); }
2245 vector<string> pieces = splitWhiteSpace(rest);
2246 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.push_back(pieces[i]); }
2251 catch(exception& e) {
2252 errorOut(e, "MothurOut", "readAccnos");
2256 /***********************************************************************/
2258 int MothurOut::getNumNames(string names){
2264 for(int i=0;i<names.size();i++){
2265 if(names[i] == ','){
2273 catch(exception& e) {
2274 errorOut(e, "MothurOut", "getNumNames");
2278 /***********************************************************************/
2280 int MothurOut::getNumChar(string line, char c){
2285 for(int i=0;i<line.size();i++){
2294 catch(exception& e) {
2295 errorOut(e, "MothurOut", "getNumChar");
2299 //**********************************************************************************************************************
2300 bool MothurOut::isSubset(vector<string> bigset, vector<string> subset) {
2304 if (subset.size() > bigset.size()) { return false; }
2306 //check if each guy in suset is also in bigset
2307 for (int i = 0; i < subset.size(); i++) {
2309 for (int j = 0; j < bigset.size(); j++) {
2310 if (subset[i] == bigset[j]) { match = true; break; }
2313 //you have a guy in subset that had no match in bigset
2314 if (match == false) { return false; }
2320 catch(exception& e) {
2321 errorOut(e, "MothurOut", "isSubset");
2325 /***********************************************************************/
2326 int MothurOut::mothurRemove(string filename){
2328 filename = getFullPathName(filename);
2329 int error = remove(filename.c_str());
2331 // if (errno != ENOENT) { //ENOENT == file does not exist
2332 // string message = "Error deleting file " + filename;
2333 // perror(message.c_str());
2338 catch(exception& e) {
2339 errorOut(e, "MothurOut", "mothurRemove");
2343 /***********************************************************************/
2344 bool MothurOut::mothurConvert(string item, int& num){
2348 if (isNumeric1(item)) {
2353 mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2354 commandInputsConvertError = true;
2359 catch(exception& e) {
2360 errorOut(e, "MothurOut", "mothurConvert");
2364 /***********************************************************************/
2365 bool MothurOut::mothurConvert(string item, intDist& num){
2369 if (isNumeric1(item)) {
2374 mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2375 commandInputsConvertError = true;
2380 catch(exception& e) {
2381 errorOut(e, "MothurOut", "mothurConvert");
2386 /***********************************************************************/
2387 bool MothurOut::isNumeric1(string stringToCheck){
2389 bool numeric = false;
2391 if(stringToCheck.find_first_not_of("0123456789.-") == string::npos) { numeric = true; }
2395 catch(exception& e) {
2396 errorOut(e, "MothurOut", "isNumeric1");
2401 /***********************************************************************/
2402 bool MothurOut::mothurConvert(string item, float& num){
2406 if (isNumeric1(item)) {
2411 mothurOut("[ERROR]: cannot convert " + item + " to a float."); mothurOutEndLine();
2412 commandInputsConvertError = true;
2417 catch(exception& e) {
2418 errorOut(e, "MothurOut", "mothurConvert");
2422 /***********************************************************************/
2423 bool MothurOut::mothurConvert(string item, double& num){
2427 if (isNumeric1(item)) {
2432 mothurOut("[ERROR]: cannot convert " + item + " to a double."); mothurOutEndLine();
2433 commandInputsConvertError = true;
2438 catch(exception& e) {
2439 errorOut(e, "MothurOut", "mothurConvert");
2443 /**************************************************************************************************/
2445 vector<vector<double> > MothurOut::binomial(int maxOrder){
2447 vector<vector<double> > binomial(maxOrder+1);
2449 for(int i=0;i<=maxOrder;i++){
2450 binomial[i].resize(maxOrder+1);
2459 for(int i=2;i<=maxOrder;i++){
2463 for(int i=2;i<=maxOrder;i++){
2464 for(int j=1;j<=maxOrder;j++){
2465 if(i==j){ binomial[i][j]=1; }
2466 if(j>i) { binomial[i][j]=0; }
2467 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
2474 catch(exception& e) {
2475 errorOut(e, "MothurOut", "binomial");
2479 /**************************************************************************************************/
2480 unsigned int MothurOut::fromBase36(string base36){
2482 unsigned int num = 0;
2484 map<char, int> converts;
2549 while (i < base36.length()) {
2551 num = 36 * num + converts[c];
2558 catch(exception& e) {
2559 errorOut(e, "MothurOut", "fromBase36");
2563 /***********************************************************************/
2565 int MothurOut::factorial(int num){
2569 for (int i = 1; i <= num; i++) {
2575 catch(exception& e) {
2576 errorOut(e, "MothurOut", "factorial");
2580 /***********************************************************************/
2582 int MothurOut::getNumSeqs(ifstream& file){
2584 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
2588 catch(exception& e) {
2589 errorOut(e, "MothurOut", "getNumSeqs");
2593 /***********************************************************************/
2594 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
2599 input = getline(file);
2600 if (input.length() != 0) {
2601 if(input[0] == '>'){ numSeqs++; }
2605 catch(exception& e) {
2606 errorOut(e, "MothurOut", "getNumSeqs");
2610 /***********************************************************************/
2612 //This function parses the estimator options and puts them in a vector
2613 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
2616 if (symbol == '-') { splitAtDash(estim, container); return; }
2618 string individual = "";
2619 int estimLength = estim.size();
2620 for(int i=0;i<estimLength;i++){
2621 if(estim[i] == symbol){
2622 container.push_back(individual);
2626 individual += estim[i];
2629 container.push_back(individual);
2632 catch(exception& e) {
2633 errorOut(e, "MothurOut", "splitAtChar");
2638 /***********************************************************************/
2640 //This function parses the estimator options and puts them in a vector
2641 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
2643 string individual = "";
2644 int estimLength = estim.size();
2645 bool prevEscape = false;
2646 for(int i=0;i<estimLength;i++){
2648 individual += estim[i];
2652 if(estim[i] == '\\'){
2655 else if(estim[i] == '-'){
2656 container.push_back(individual);
2661 individual += estim[i];
2666 container.push_back(individual);
2668 catch(exception& e) {
2669 errorOut(e, "MothurOut", "splitAtDash");
2674 /***********************************************************************/
2675 //This function parses the label options and puts them in a set
2676 void MothurOut::splitAtDash(string& estim, set<string>& container) {
2678 string individual = "";
2679 int estimLength = estim.size();
2680 bool prevEscape = false;
2681 for(int i=0;i<estimLength;i++){
2683 individual += estim[i];
2687 if(estim[i] == '\\'){
2690 else if(estim[i] == '-'){
2691 container.insert(individual);
2696 individual += estim[i];
2701 container.insert(individual);
2704 catch(exception& e) {
2705 errorOut(e, "MothurOut", "splitAtDash");
2709 /***********************************************************************/
2710 //This function parses the line options and puts them in a set
2711 void MothurOut::splitAtDash(string& estim, set<int>& container) {
2713 string individual = "";
2715 int estimLength = estim.size();
2716 bool prevEscape = false;
2717 for(int i=0;i<estimLength;i++){
2719 individual += estim[i];
2723 if(estim[i] == '\\'){
2726 else if(estim[i] == '-'){
2727 convert(individual, lineNum); //convert the string to int
2728 container.insert(lineNum);
2733 individual += estim[i];
2738 convert(individual, lineNum); //convert the string to int
2739 container.insert(lineNum);
2741 catch(exception& e) {
2742 errorOut(e, "MothurOut", "splitAtDash");
2746 /***********************************************************************/
2747 string MothurOut::makeList(vector<string>& names) {
2751 if (names.size() == 0) { return list; }
2753 for (int i = 0; i < names.size()-1; i++) { list += names[i] + ","; }
2756 list += names[names.size()-1];
2760 catch(exception& e) {
2761 errorOut(e, "MothurOut", "makeList");
2766 /***********************************************************************/
2767 //This function parses the a string and puts peices in a vector
2768 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
2770 string individual = "";
2771 int estimLength = estim.size();
2772 for(int i=0;i<estimLength;i++){
2773 if(estim[i] == ','){
2774 container.push_back(individual);
2778 individual += estim[i];
2781 container.push_back(individual);
2786 // string individual;
2788 // while (estim.find_first_of(',') != -1) {
2789 // individual = estim.substr(0,estim.find_first_of(','));
2790 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
2791 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
2792 // container.push_back(individual);
2796 // container.push_back(estim);
2798 catch(exception& e) {
2799 errorOut(e, "MothurOut", "splitAtComma");
2803 /***********************************************************************/
2804 //This function splits up the various option parameters
2805 void MothurOut::splitAtChar(string& prefix, string& suffix, char c){
2807 prefix = suffix.substr(0,suffix.find_first_of(c));
2808 if ((suffix.find_first_of(c)+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2809 suffix = suffix.substr(suffix.find_first_of(c)+1, suffix.length());
2811 while(suffix.at(0) == ' ')
2812 suffix = suffix.substr(1, suffix.length());
2816 catch(exception& e) {
2817 errorOut(e, "MothurOut", "splitAtComma");
2822 /***********************************************************************/
2824 //This function splits up the various option parameters
2825 void MothurOut::splitAtComma(string& prefix, string& suffix){
2827 prefix = suffix.substr(0,suffix.find_first_of(','));
2828 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2829 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
2831 while(suffix.at(0) == ' ')
2832 suffix = suffix.substr(1, suffix.length());
2836 catch(exception& e) {
2837 errorOut(e, "MothurOut", "splitAtComma");
2841 /***********************************************************************/
2843 //This function separates the key value from the option value i.e. dist=96_...
2844 void MothurOut::splitAtEquals(string& key, string& value){
2846 if(value.find_first_of('=') != -1){
2847 key = value.substr(0,value.find_first_of('='));
2848 if ((value.find_first_of('=')+1) <= value.length()) {
2849 value = value.substr(value.find_first_of('=')+1, value.length());
2856 catch(exception& e) {
2857 errorOut(e, "MothurOut", "splitAtEquals");
2862 /**************************************************************************************************/
2864 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
2866 for (int i = 0; i < Groups.size(); i++) {
2867 if (groupname == Groups[i]) { return true; }
2871 catch(exception& e) {
2872 errorOut(e, "MothurOut", "inUsersGroups");
2876 /**************************************************************************************************/
2878 bool MothurOut::inUsersGroups(vector<int> set, vector< vector<int> > sets) {
2880 for (int i = 0; i < sets.size(); i++) {
2881 if (set == sets[i]) { return true; }
2885 catch(exception& e) {
2886 errorOut(e, "MothurOut", "inUsersGroups");
2890 /**************************************************************************************************/
2892 bool MothurOut::inUsersGroups(int groupname, vector<int> Groups) {
2894 for (int i = 0; i < Groups.size(); i++) {
2895 if (groupname == Groups[i]) { return true; }
2899 catch(exception& e) {
2900 errorOut(e, "MothurOut", "inUsersGroups");
2905 /**************************************************************************************************/
2906 //returns true if any of the strings in first vector are in second vector
2907 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
2910 for (int i = 0; i < groupnames.size(); i++) {
2911 if (inUsersGroups(groupnames[i], Groups)) { return true; }
2915 catch(exception& e) {
2916 errorOut(e, "MothurOut", "inUsersGroups");
2920 /***********************************************************************/
2921 //this function determines if the user has given us labels that are smaller than the given label.
2922 //if so then it returns true so that the calling function can run the previous valid distance.
2923 //it's a "smart" distance function. It also checks for invalid labels.
2924 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
2927 set<string>::iterator it;
2928 vector<float> orderFloat;
2929 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
2930 map<string, float>::iterator it2;
2932 bool smaller = false;
2934 //unique is the smallest line
2935 if (label == "unique") { return false; }
2937 if (convertTestFloat(label, labelFloat)) {
2938 convert(label, labelFloat);
2939 }else { //cant convert
2944 //go through users set and make them floats
2945 for(it = userLabels.begin(); it != userLabels.end();) {
2948 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
2950 orderFloat.push_back(temp);
2951 userMap[*it] = temp;
2953 }else if (*it == "unique") {
2954 orderFloat.push_back(-1.0);
2955 userMap["unique"] = -1.0;
2958 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
2959 userLabels.erase(it++);
2964 sort(orderFloat.begin(), orderFloat.end());
2966 /*************************************************/
2967 //is this label bigger than any of the users labels
2968 /*************************************************/
2970 //loop through order until you find a label greater than label
2971 for (int i = 0; i < orderFloat.size(); i++) {
2972 if (orderFloat[i] < labelFloat) {
2974 if (orderFloat[i] == -1) {
2975 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
2976 userLabels.erase("unique");
2979 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
2981 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
2982 if (it2->second == orderFloat[i]) {
2984 //remove small labels
2985 userLabels.erase(s);
2989 if (errorOff == "") {mothurOut( s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
2991 //since they are sorted once you find a bigger one stop looking
2998 catch(exception& e) {
2999 errorOut(e, "MothurOut", "anyLabelsToProcess");
3004 /**************************************************************************************************/
3005 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
3010 string line = getline(file);
3012 //before we added this check
3013 if (line[0] != '#') { good = false; }
3016 line = line.substr(1);
3018 vector<string> versionVector;
3019 splitAtChar(version, versionVector, '.');
3021 //check file version
3022 vector<string> linesVector;
3023 splitAtChar(line, linesVector, '.');
3025 if (versionVector.size() != linesVector.size()) { good = false; }
3027 for (int j = 0; j < versionVector.size(); j++) {
3029 convert(versionVector[j], num1);
3030 convert(linesVector[j], num2);
3032 //if mothurs version is newer than this files version, then we want to remake it
3033 if (num1 > num2) { good = false; break; }
3039 if (!good) { file.close(); }
3040 else { file.seekg(0); }
3044 catch(exception& e) {
3045 errorOut(e, "MothurOut", "checkReleaseVersion");
3049 /**************************************************************************************************/
3050 vector<double> MothurOut::getAverages(vector< vector<double> >& dists) {
3052 vector<double> averages; //averages.resize(numComp, 0.0);
3053 for (int i = 0; i < dists[0].size(); i++) { averages.push_back(0.0); }
3055 for (int thisIter = 0; thisIter < dists.size(); thisIter++) {
3056 for (int i = 0; i < dists[thisIter].size(); i++) {
3057 averages[i] += dists[thisIter][i];
3062 for (int i = 0; i < averages.size(); i++) { averages[i] /= (double) dists.size(); }
3066 catch(exception& e) {
3067 errorOut(e, "MothurOut", "getAverages");
3071 /**************************************************************************************************/
3072 vector<double> MothurOut::getStandardDeviation(vector< vector<double> >& dists) {
3075 vector<double> averages = getAverages(dists);
3077 //find standard deviation
3078 vector<double> stdDev; //stdDev.resize(numComp, 0.0);
3079 for (int i = 0; i < dists[0].size(); i++) { stdDev.push_back(0.0); }
3081 for (int thisIter = 0; thisIter < dists.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3082 for (int j = 0; j < dists[thisIter].size(); j++) {
3083 stdDev[j] += ((dists[thisIter][j] - averages[j]) * (dists[thisIter][j] - averages[j]));
3086 for (int i = 0; i < stdDev.size(); i++) {
3087 stdDev[i] /= (double) dists.size();
3088 stdDev[i] = sqrt(stdDev[i]);
3093 catch(exception& e) {
3094 errorOut(e, "MothurOut", "getAverages");
3098 /**************************************************************************************************/
3099 vector<double> MothurOut::getStandardDeviation(vector< vector<double> >& dists, vector<double>& averages) {
3101 //find standard deviation
3102 vector<double> stdDev; //stdDev.resize(numComp, 0.0);
3103 for (int i = 0; i < dists[0].size(); i++) { stdDev.push_back(0.0); }
3105 for (int thisIter = 0; thisIter < dists.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3106 for (int j = 0; j < dists[thisIter].size(); j++) {
3107 stdDev[j] += ((dists[thisIter][j] - averages[j]) * (dists[thisIter][j] - averages[j]));
3110 for (int i = 0; i < stdDev.size(); i++) {
3111 stdDev[i] /= (double) dists.size();
3112 stdDev[i] = sqrt(stdDev[i]);
3117 catch(exception& e) {
3118 errorOut(e, "MothurOut", "getAverages");
3122 /**************************************************************************************************/
3123 vector< vector<seqDist> > MothurOut::getAverages(vector< vector< vector<seqDist> > >& calcDistsTotals, string mode) {
3126 vector< vector<seqDist> > calcAverages; //calcAverages.resize(calcDistsTotals[0].size());
3127 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3128 //calcAverages[i].resize(calcDistsTotals[0][i].size());
3129 vector<seqDist> temp;
3130 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3132 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3133 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3134 tempDist.dist = 0.0;
3135 temp.push_back(tempDist);
3137 calcAverages.push_back(temp);
3140 if (mode == "average") {
3141 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //sum all groups dists for each calculator
3142 for (int i = 0; i < calcAverages.size(); i++) { //initialize sums to zero.
3143 for (int j = 0; j < calcAverages[i].size(); j++) {
3144 calcAverages[i][j].dist += calcDistsTotals[thisIter][i][j].dist;
3149 for (int i = 0; i < calcAverages.size(); i++) { //finds average.
3150 for (int j = 0; j < calcAverages[i].size(); j++) {
3151 calcAverages[i][j].dist /= (float) calcDistsTotals.size();
3154 }else { //find median
3155 for (int i = 0; i < calcAverages.size(); i++) { //for each calc
3156 for (int j = 0; j < calcAverages[i].size(); j++) { //for each comparison
3157 vector<double> dists;
3158 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //for each subsample
3159 dists.push_back(calcDistsTotals[thisIter][i][j].dist);
3161 sort(dists.begin(), dists.end());
3162 calcAverages[i][j].dist = dists[(calcDistsTotals.size()/2)];
3167 return calcAverages;
3169 catch(exception& e) {
3170 errorOut(e, "MothurOut", "getAverages");
3174 /**************************************************************************************************/
3175 vector< vector<seqDist> > MothurOut::getAverages(vector< vector< vector<seqDist> > >& calcDistsTotals) {
3178 vector< vector<seqDist> > calcAverages; //calcAverages.resize(calcDistsTotals[0].size());
3179 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3180 //calcAverages[i].resize(calcDistsTotals[0][i].size());
3181 vector<seqDist> temp;
3182 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3184 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3185 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3186 tempDist.dist = 0.0;
3187 temp.push_back(tempDist);
3189 calcAverages.push_back(temp);
3193 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //sum all groups dists for each calculator
3194 for (int i = 0; i < calcAverages.size(); i++) { //initialize sums to zero.
3195 for (int j = 0; j < calcAverages[i].size(); j++) {
3196 calcAverages[i][j].dist += calcDistsTotals[thisIter][i][j].dist;
3201 for (int i = 0; i < calcAverages.size(); i++) { //finds average.
3202 for (int j = 0; j < calcAverages[i].size(); j++) {
3203 calcAverages[i][j].dist /= (float) calcDistsTotals.size();
3207 return calcAverages;
3209 catch(exception& e) {
3210 errorOut(e, "MothurOut", "getAverages");
3214 /**************************************************************************************************/
3215 vector< vector<seqDist> > MothurOut::getStandardDeviation(vector< vector< vector<seqDist> > >& calcDistsTotals) {
3218 vector< vector<seqDist> > calcAverages = getAverages(calcDistsTotals);
3220 //find standard deviation
3221 vector< vector<seqDist> > stdDev;
3222 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3223 vector<seqDist> temp;
3224 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3226 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3227 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3228 tempDist.dist = 0.0;
3229 temp.push_back(tempDist);
3231 stdDev.push_back(temp);
3234 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3235 for (int i = 0; i < stdDev.size(); i++) {
3236 for (int j = 0; j < stdDev[i].size(); j++) {
3237 stdDev[i][j].dist += ((calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist) * (calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist));
3242 for (int i = 0; i < stdDev.size(); i++) { //finds average.
3243 for (int j = 0; j < stdDev[i].size(); j++) {
3244 stdDev[i][j].dist /= (float) calcDistsTotals.size();
3245 stdDev[i][j].dist = sqrt(stdDev[i][j].dist);
3251 catch(exception& e) {
3252 errorOut(e, "MothurOut", "getAverages");
3256 /**************************************************************************************************/
3257 vector< vector<seqDist> > MothurOut::getStandardDeviation(vector< vector< vector<seqDist> > >& calcDistsTotals, vector< vector<seqDist> >& calcAverages) {
3259 //find standard deviation
3260 vector< vector<seqDist> > stdDev;
3261 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3262 vector<seqDist> temp;
3263 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3265 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3266 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3267 tempDist.dist = 0.0;
3268 temp.push_back(tempDist);
3270 stdDev.push_back(temp);
3273 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3274 for (int i = 0; i < stdDev.size(); i++) {
3275 for (int j = 0; j < stdDev[i].size(); j++) {
3276 stdDev[i][j].dist += ((calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist) * (calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist));
3281 for (int i = 0; i < stdDev.size(); i++) { //finds average.
3282 for (int j = 0; j < stdDev[i].size(); j++) {
3283 stdDev[i][j].dist /= (float) calcDistsTotals.size();
3284 stdDev[i][j].dist = sqrt(stdDev[i][j].dist);
3290 catch(exception& e) {
3291 errorOut(e, "MothurOut", "getAverages");
3296 /**************************************************************************************************/
3297 bool MothurOut::isContainingOnlyDigits(string input) {
3300 //are you a digit in ascii code
3301 for (int i = 0;i < input.length(); i++){
3302 if( input[i]>47 && input[i]<58){}
3303 else { return false; }
3308 catch(exception& e) {
3309 errorOut(e, "MothurOut", "isContainingOnlyDigits");
3313 /**************************************************************************************************/
3314 int MothurOut::removeConfidences(string& tax) {
3320 while (tax.find_first_of(';') != -1) {
3322 if (control_pressed) { return 0; }
3325 taxon = tax.substr(0,tax.find_first_of(';'));
3327 int pos = taxon.find_last_of('(');
3330 int pos2 = taxon.find_last_of(')');
3332 string confidenceScore = taxon.substr(pos+1, (pos2-(pos+1)));
3333 if (isNumeric1(confidenceScore)) {
3334 taxon = taxon.substr(0, pos); //rip off confidence
3340 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
3348 catch(exception& e) {
3349 errorOut(e, "MothurOut", "removeConfidences");
3353 /**************************************************************************************************/
3354 string MothurOut::removeQuotes(string tax) {
3360 for (int i = 0; i < tax.length(); i++) {
3362 if (control_pressed) { return newTax; }
3364 if ((tax[i] != '\'') && (tax[i] != '\"')) { newTax += tax[i]; }
3370 catch(exception& e) {
3371 errorOut(e, "MothurOut", "removeQuotes");
3375 /**************************************************************************************************/
3376 // function for calculating standard deviation
3377 double MothurOut::getStandardDeviation(vector<int>& featureVector){
3381 for (int i = 0; i < featureVector.size(); i++) { average += featureVector[i]; }
3382 average /= (double) featureVector.size();
3384 //find standard deviation
3386 for (int i = 0; i < featureVector.size(); i++) { //compute the difference of each dist from the mean, and square the result of each
3387 stdDev += ((featureVector[i] - average) * (featureVector[i] - average));
3390 stdDev /= (double) featureVector.size();
3391 stdDev = sqrt(stdDev);
3395 catch(exception& e) {
3396 errorOut(e, "MothurOut", "getStandardDeviation");
3400 /**************************************************************************************************/