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, 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.");
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 /**************************************************************************************************/
1434 vector<unsigned long long> MothurOut::divideFilePerLine(string filename, int& proc) {
1436 vector<unsigned long long> filePos;
1437 filePos.push_back(0);
1440 unsigned long long size;
1442 filename = getFullPathName(filename);
1444 //get num bytes in file
1445 pFile = fopen (filename.c_str(),"rb");
1446 if (pFile==NULL) perror ("Error opening file");
1448 fseek (pFile, 0, SEEK_END);
1453 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1455 //estimate file breaks
1456 unsigned long long chunkSize = 0;
1457 chunkSize = size / proc;
1459 //file to small to divide by processors
1460 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1462 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1463 for (int i = 0; i < proc; i++) {
1464 unsigned long long spot = (i+1) * chunkSize;
1467 openInputFile(filename, in);
1470 //look for next line break
1471 unsigned long long newSpot = spot;
1475 if ((c == '\n') || (c == '\r') || (c == '\f')) { gobble(in); newSpot = in.tellg(); break; }
1476 else if (int(c) == -1) { break; }
1479 //there was not another line before the end of the file
1480 unsigned long long sanityPos = in.tellg();
1482 if (sanityPos == -1) { break; }
1483 else { filePos.push_back(newSpot); }
1489 filePos.push_back(size);
1491 //sanity check filePos
1492 for (int i = 0; i < (filePos.size()-1); i++) {
1493 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1496 proc = (filePos.size() - 1);
1498 mothurOut("[ERROR]: Windows version should not be calling the divideFile function."); mothurOutEndLine();
1500 filePos.push_back(size);
1504 catch(exception& e) {
1505 errorOut(e, "MothurOut", "divideFile");
1509 /**************************************************************************************************/
1510 int MothurOut::divideFile(string filename, int& proc, vector<string>& files) {
1513 vector<unsigned long long> filePos = divideFile(filename, proc);
1515 for (int i = 0; i < (filePos.size()-1); i++) {
1519 openInputFile(filename, in);
1520 in.seekg(filePos[i]);
1521 unsigned long long size = filePos[(i+1)] - filePos[i];
1522 char* chunk = new char[size];
1523 in.read(chunk, size);
1527 string fileChunkName = filename + "." + toString(i) + ".tmp";
1529 openOutputFile(fileChunkName, out);
1531 out << chunk << endl;
1536 files.push_back(fileChunkName);
1541 catch(exception& e) {
1542 errorOut(e, "MothurOut", "divideFile");
1546 /***********************************************************************/
1548 bool MothurOut::isTrue(string f){
1551 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1553 if ((f == "TRUE") || (f == "T")) { return true; }
1554 else { return false; }
1556 catch(exception& e) {
1557 errorOut(e, "MothurOut", "isTrue");
1562 /***********************************************************************/
1564 float MothurOut::roundDist(float dist, int precision){
1566 return int(dist * precision + 0.5)/float(precision);
1568 catch(exception& e) {
1569 errorOut(e, "MothurOut", "roundDist");
1573 /***********************************************************************/
1575 float MothurOut::ceilDist(float dist, int precision){
1577 return int(ceil(dist * precision))/float(precision);
1579 catch(exception& e) {
1580 errorOut(e, "MothurOut", "ceilDist");
1584 /***********************************************************************/
1586 vector<string> MothurOut::splitWhiteSpace(string& rest, char buffer[], int size){
1588 vector<string> pieces;
1590 for (int i = 0; i < size; i++) {
1591 if (!isspace(buffer[i])) { rest += buffer[i]; }
1593 if (rest != "") { pieces.push_back(rest); rest = ""; }
1594 while (i < size) { //gobble white space
1595 if (isspace(buffer[i])) { i++; }
1596 else { rest = buffer[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1603 catch(exception& e) {
1604 errorOut(e, "MothurOut", "splitWhiteSpace");
1608 /***********************************************************************/
1609 vector<string> MothurOut::splitWhiteSpace(string input){
1611 vector<string> pieces;
1614 for (int i = 0; i < input.length(); i++) {
1615 if (!isspace(input[i])) { rest += input[i]; }
1617 if (rest != "") { pieces.push_back(rest); rest = ""; }
1618 while (i < input.length()) { //gobble white space
1619 if (isspace(input[i])) { i++; }
1620 else { rest = input[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1625 if (rest != "") { pieces.push_back(rest); }
1629 catch(exception& e) {
1630 errorOut(e, "MothurOut", "splitWhiteSpace");
1634 /***********************************************************************/
1635 vector<string> MothurOut::splitWhiteSpaceWithQuotes(string input){
1637 vector<string> pieces;
1640 int pos = input.find('\'');
1641 int pos2 = input.find('\"');
1643 if ((pos == string::npos) && (pos2 == string::npos)) { return splitWhiteSpace(input); } //no quotes to worry about
1645 for (int i = 0; i < input.length(); i++) {
1646 if ((input[i] == '\'') || (input[i] == '\"') || (rest == "\'") || (rest == "\"")) { //grab everything til end or next ' or "
1648 for (int j = i+1; j < input.length(); j++) {
1649 if ((input[j] == '\'') || (input[j] == '\"')) { //then quit
1653 }else { rest += input[j]; }
1655 }else if (!isspace(input[i])) { rest += input[i]; }
1657 if (rest != "") { pieces.push_back(rest); rest = ""; }
1658 while (i < input.length()) { //gobble white space
1659 if (isspace(input[i])) { i++; }
1660 else { rest = input[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1665 if (rest != "") { pieces.push_back(rest); }
1669 catch(exception& e) {
1670 errorOut(e, "MothurOut", "splitWhiteSpace");
1674 //**********************************************************************************************************************
1675 int MothurOut::readTax(string namefile, map<string, string>& taxMap) {
1679 openInputFile(namefile, in);
1683 bool pairDone = false;
1684 bool columnOne = true;
1685 string firstCol, secondCol;
1688 if (control_pressed) { break; }
1690 in.read(buffer, 4096);
1691 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1693 for (int i = 0; i < pieces.size(); i++) {
1694 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1695 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1698 checkName(firstCol);
1699 //are there confidence scores, if so remove them
1700 if (secondCol.find_first_of('(') != -1) { removeConfidences(secondCol); }
1701 map<string, string>::iterator itTax = taxMap.find(firstCol);
1703 if(itTax == taxMap.end()) {
1704 bool ignore = false;
1705 if (secondCol != "") { if (secondCol[secondCol.length()-1] != ';') { mothurOut("[ERROR]: " + firstCol + " is missing the final ';', ignoring.\n"); ignore=true; }
1707 if (!ignore) { taxMap[firstCol] = secondCol; }
1708 if (debug) { mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n"); }
1710 mothurOut("[ERROR]: " + firstCol + " is already in your taxonomy file, names must be unique./n"); control_pressed = true;
1719 vector<string> pieces = splitWhiteSpace(rest);
1721 for (int i = 0; i < pieces.size(); i++) {
1722 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1723 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1726 checkName(firstCol);
1727 //are there confidence scores, if so remove them
1728 if (secondCol.find_first_of('(') != -1) { removeConfidences(secondCol); }
1729 map<string, string>::iterator itTax = taxMap.find(firstCol);
1731 if(itTax == taxMap.end()) {
1732 bool ignore = false;
1733 if (secondCol != "") { if (secondCol[secondCol.length()-1] != ';') { mothurOut("[ERROR]: " + firstCol + " is missing the final ';', ignoring.\n"); ignore=true; }
1735 if (!ignore) { taxMap[firstCol] = secondCol; }
1736 if (debug) { mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n"); }
1738 mothurOut("[ERROR]: " + firstCol + " is already in your taxonomy file, names must be unique./n"); control_pressed = true;
1746 return taxMap.size();
1749 catch(exception& e) {
1750 errorOut(e, "MothurOut", "readTax");
1754 /**********************************************************************************************************************/
1755 int MothurOut::readNames(string namefile, map<string, string>& nameMap, bool redund) {
1759 openInputFile(namefile, in);
1763 bool pairDone = false;
1764 bool columnOne = true;
1765 string firstCol, secondCol;
1768 if (control_pressed) { break; }
1770 in.read(buffer, 4096);
1771 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1773 for (int i = 0; i < pieces.size(); i++) {
1774 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1775 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1778 checkName(firstCol);
1779 checkName(secondCol);
1781 //parse names into vector
1782 vector<string> theseNames;
1783 splitAtComma(secondCol, theseNames);
1784 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1792 vector<string> pieces = splitWhiteSpace(rest);
1794 for (int i = 0; i < pieces.size(); i++) {
1795 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1796 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1799 checkName(firstCol);
1800 checkName(secondCol);
1802 //parse names into vector
1803 vector<string> theseNames;
1804 splitAtComma(secondCol, theseNames);
1805 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1811 return nameMap.size();
1814 catch(exception& e) {
1815 errorOut(e, "MothurOut", "readNames");
1819 /**********************************************************************************************************************/
1820 int MothurOut::readNames(string namefile, map<string, string>& nameMap, int flip) {
1824 openInputFile(namefile, in);
1828 bool pairDone = false;
1829 bool columnOne = true;
1830 string firstCol, secondCol;
1833 if (control_pressed) { break; }
1835 in.read(buffer, 4096);
1836 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1838 for (int i = 0; i < pieces.size(); i++) {
1839 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1840 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1843 checkName(firstCol);
1844 checkName(secondCol);
1845 nameMap[secondCol] = firstCol;
1853 vector<string> pieces = splitWhiteSpace(rest);
1855 for (int i = 0; i < pieces.size(); i++) {
1856 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1857 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1860 checkName(firstCol);
1861 checkName(secondCol);
1862 nameMap[secondCol] = firstCol;
1868 return nameMap.size();
1871 catch(exception& e) {
1872 errorOut(e, "MothurOut", "readNames");
1876 /**********************************************************************************************************************/
1877 int MothurOut::readNames(string namefile, map<string, string>& nameMap, map<string, int>& nameCount) {
1879 nameMap.clear(); nameCount.clear();
1882 openInputFile(namefile, in);
1886 bool pairDone = false;
1887 bool columnOne = true;
1888 string firstCol, secondCol;
1891 if (control_pressed) { break; }
1893 in.read(buffer, 4096);
1894 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1896 for (int i = 0; i < pieces.size(); i++) {
1897 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1898 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1901 checkName(firstCol);
1902 checkName(secondCol);
1903 //parse names into vector
1904 vector<string> theseNames;
1905 splitAtComma(secondCol, theseNames);
1906 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1907 nameCount[firstCol] = theseNames.size();
1915 vector<string> pieces = splitWhiteSpace(rest);
1917 for (int i = 0; i < pieces.size(); i++) {
1918 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1919 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1922 checkName(firstCol);
1923 checkName(secondCol);
1924 //parse names into vector
1925 vector<string> theseNames;
1926 splitAtComma(secondCol, theseNames);
1927 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1928 nameCount[firstCol] = theseNames.size();
1934 return nameMap.size();
1937 catch(exception& e) {
1938 errorOut(e, "MothurOut", "readNames");
1942 /**********************************************************************************************************************/
1943 int MothurOut::readNames(string namefile, map<string, string>& nameMap) {
1947 openInputFile(namefile, in);
1951 bool pairDone = false;
1952 bool columnOne = true;
1953 string firstCol, secondCol;
1956 if (control_pressed) { break; }
1958 in.read(buffer, 4096);
1959 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1961 for (int i = 0; i < pieces.size(); i++) {
1962 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1963 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1966 checkName(firstCol);
1967 checkName(secondCol);
1968 nameMap[firstCol] = secondCol; pairDone = false; }
1974 vector<string> pieces = splitWhiteSpace(rest);
1976 for (int i = 0; i < pieces.size(); i++) {
1977 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1978 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1981 checkName(firstCol);
1982 checkName(secondCol);
1983 nameMap[firstCol] = secondCol; pairDone = false; }
1987 return nameMap.size();
1990 catch(exception& e) {
1991 errorOut(e, "MothurOut", "readNames");
1995 /**********************************************************************************************************************/
1996 int MothurOut::readNames(string namefile, map<string, vector<string> >& nameMap) {
2000 openInputFile(namefile, in);
2004 bool pairDone = false;
2005 bool columnOne = true;
2006 string firstCol, secondCol;
2009 if (control_pressed) { break; }
2011 in.read(buffer, 4096);
2012 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2014 for (int i = 0; i < pieces.size(); i++) {
2015 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2016 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2019 checkName(firstCol);
2020 checkName(secondCol);
2021 vector<string> temp;
2022 splitAtComma(secondCol, temp);
2023 nameMap[firstCol] = temp;
2031 vector<string> pieces = splitWhiteSpace(rest);
2033 for (int i = 0; i < pieces.size(); i++) {
2034 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2035 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2038 checkName(firstCol);
2039 checkName(secondCol);
2040 vector<string> temp;
2041 splitAtComma(secondCol, temp);
2042 nameMap[firstCol] = temp;
2048 return nameMap.size();
2050 catch(exception& e) {
2051 errorOut(e, "MothurOut", "readNames");
2055 /**********************************************************************************************************************/
2056 map<string, int> MothurOut::readNames(string namefile) {
2058 map<string, int> nameMap;
2062 openInputFile(namefile, in);
2066 bool pairDone = false;
2067 bool columnOne = true;
2068 string firstCol, secondCol;
2071 if (control_pressed) { break; }
2073 in.read(buffer, 4096);
2074 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2076 for (int i = 0; i < pieces.size(); i++) {
2077 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2078 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2081 checkName(firstCol);
2082 checkName(secondCol);
2083 int num = getNumNames(secondCol);
2084 nameMap[firstCol] = num;
2092 vector<string> pieces = splitWhiteSpace(rest);
2093 for (int i = 0; i < pieces.size(); i++) {
2094 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2095 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2098 checkName(firstCol);
2099 checkName(secondCol);
2100 int num = getNumNames(secondCol);
2101 nameMap[firstCol] = num;
2110 catch(exception& e) {
2111 errorOut(e, "MothurOut", "readNames");
2115 /**********************************************************************************************************************/
2116 map<string, int> MothurOut::readNames(string namefile, unsigned long int& numSeqs) {
2118 map<string, int> nameMap;
2123 openInputFile(namefile, in);
2127 bool pairDone = false;
2128 bool columnOne = true;
2129 string firstCol, secondCol;
2132 if (control_pressed) { break; }
2134 in.read(buffer, 4096);
2135 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2137 for (int i = 0; i < pieces.size(); i++) {
2138 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2139 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2142 checkName(firstCol);
2143 checkName(secondCol);
2144 int num = getNumNames(secondCol);
2145 nameMap[firstCol] = num;
2154 vector<string> pieces = splitWhiteSpace(rest);
2155 for (int i = 0; i < pieces.size(); i++) {
2156 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2157 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2160 checkName(firstCol);
2161 checkName(secondCol);
2162 int num = getNumNames(secondCol);
2163 nameMap[firstCol] = num;
2173 catch(exception& e) {
2174 errorOut(e, "MothurOut", "readNames");
2178 /************************************************************/
2179 int MothurOut::checkName(string& name) {
2181 for (int i = 0; i < name.length(); i++) {
2182 if (name[i] == ':') { name[i] = '_'; changedSeqNames = true; }
2186 catch(exception& e) {
2187 errorOut(e, "MothurOut", "checkName");
2191 /**********************************************************************************************************************/
2192 int MothurOut::readNames(string namefile, vector<seqPriorityNode>& nameVector, map<string, string>& fastamap) {
2198 openInputFile(namefile, in);
2202 bool pairDone = false;
2203 bool columnOne = true;
2204 string firstCol, secondCol;
2207 if (control_pressed) { break; }
2209 in.read(buffer, 4096);
2210 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2212 for (int i = 0; i < pieces.size(); i++) {
2213 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2214 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2217 checkName(firstCol);
2218 checkName(secondCol);
2219 int num = getNumNames(secondCol);
2221 map<string, string>::iterator it = fastamap.find(firstCol);
2222 if (it == fastamap.end()) {
2224 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
2226 seqPriorityNode temp(num, it->second, firstCol);
2227 nameVector.push_back(temp);
2237 vector<string> pieces = splitWhiteSpace(rest);
2239 for (int i = 0; i < pieces.size(); i++) {
2240 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2241 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2244 checkName(firstCol);
2245 checkName(secondCol);
2246 int num = getNumNames(secondCol);
2248 map<string, string>::iterator it = fastamap.find(firstCol);
2249 if (it == fastamap.end()) {
2251 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
2253 seqPriorityNode temp(num, it->second, firstCol);
2254 nameVector.push_back(temp);
2263 catch(exception& e) {
2264 errorOut(e, "MothurOut", "readNames");
2268 //**********************************************************************************************************************
2269 set<string> MothurOut::readAccnos(string accnosfile){
2273 openInputFile(accnosfile, in);
2280 if (control_pressed) { break; }
2282 in.read(buffer, 4096);
2283 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2285 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.insert(pieces[i]); }
2290 vector<string> pieces = splitWhiteSpace(rest);
2291 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.insert(pieces[i]); }
2295 catch(exception& e) {
2296 errorOut(e, "MothurOut", "readAccnos");
2300 //**********************************************************************************************************************
2301 int MothurOut::readAccnos(string accnosfile, vector<string>& names){
2305 openInputFile(accnosfile, in);
2312 if (control_pressed) { break; }
2314 in.read(buffer, 4096);
2315 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2317 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.push_back(pieces[i]); }
2322 vector<string> pieces = splitWhiteSpace(rest);
2323 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.push_back(pieces[i]); }
2328 catch(exception& e) {
2329 errorOut(e, "MothurOut", "readAccnos");
2333 /***********************************************************************/
2335 int MothurOut::getNumNames(string names){
2341 for(int i=0;i<names.size();i++){
2342 if(names[i] == ','){
2350 catch(exception& e) {
2351 errorOut(e, "MothurOut", "getNumNames");
2355 /***********************************************************************/
2357 int MothurOut::getNumChar(string line, char c){
2362 for(int i=0;i<line.size();i++){
2371 catch(exception& e) {
2372 errorOut(e, "MothurOut", "getNumChar");
2376 //**********************************************************************************************************************
2377 bool MothurOut::isSubset(vector<string> bigset, vector<string> subset) {
2381 if (subset.size() > bigset.size()) { return false; }
2383 //check if each guy in suset is also in bigset
2384 for (int i = 0; i < subset.size(); i++) {
2386 for (int j = 0; j < bigset.size(); j++) {
2387 if (subset[i] == bigset[j]) { match = true; break; }
2390 //you have a guy in subset that had no match in bigset
2391 if (match == false) { return false; }
2397 catch(exception& e) {
2398 errorOut(e, "MothurOut", "isSubset");
2402 /***********************************************************************/
2403 int MothurOut::mothurRemove(string filename){
2405 filename = getFullPathName(filename);
2406 int error = remove(filename.c_str());
2408 // if (errno != ENOENT) { //ENOENT == file does not exist
2409 // string message = "Error deleting file " + filename;
2410 // perror(message.c_str());
2415 catch(exception& e) {
2416 errorOut(e, "MothurOut", "mothurRemove");
2420 /***********************************************************************/
2421 bool MothurOut::mothurConvert(string item, int& num){
2425 if (isNumeric1(item)) {
2430 mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2431 commandInputsConvertError = true;
2436 catch(exception& e) {
2437 errorOut(e, "MothurOut", "mothurConvert");
2441 /***********************************************************************/
2442 bool MothurOut::mothurConvert(string item, intDist& num){
2446 if (isNumeric1(item)) {
2451 mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2452 commandInputsConvertError = true;
2457 catch(exception& e) {
2458 errorOut(e, "MothurOut", "mothurConvert");
2463 /***********************************************************************/
2464 bool MothurOut::isNumeric1(string stringToCheck){
2466 bool numeric = false;
2468 if(stringToCheck.find_first_not_of("0123456789.-") == string::npos) { numeric = true; }
2472 catch(exception& e) {
2473 errorOut(e, "MothurOut", "isNumeric1");
2478 /***********************************************************************/
2479 bool MothurOut::mothurConvert(string item, float& num){
2483 if (isNumeric1(item)) {
2488 mothurOut("[ERROR]: cannot convert " + item + " to a float."); mothurOutEndLine();
2489 commandInputsConvertError = true;
2494 catch(exception& e) {
2495 errorOut(e, "MothurOut", "mothurConvert");
2499 /***********************************************************************/
2500 bool MothurOut::mothurConvert(string item, double& num){
2504 if (isNumeric1(item)) {
2509 mothurOut("[ERROR]: cannot convert " + item + " to a double."); mothurOutEndLine();
2510 commandInputsConvertError = true;
2515 catch(exception& e) {
2516 errorOut(e, "MothurOut", "mothurConvert");
2520 /**************************************************************************************************/
2522 vector<vector<double> > MothurOut::binomial(int maxOrder){
2524 vector<vector<double> > binomial(maxOrder+1);
2526 for(int i=0;i<=maxOrder;i++){
2527 binomial[i].resize(maxOrder+1);
2536 for(int i=2;i<=maxOrder;i++){
2540 for(int i=2;i<=maxOrder;i++){
2541 for(int j=1;j<=maxOrder;j++){
2542 if(i==j){ binomial[i][j]=1; }
2543 if(j>i) { binomial[i][j]=0; }
2544 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
2551 catch(exception& e) {
2552 errorOut(e, "MothurOut", "binomial");
2556 /**************************************************************************************************/
2557 unsigned int MothurOut::fromBase36(string base36){
2559 unsigned int num = 0;
2561 map<char, int> converts;
2626 while (i < base36.length()) {
2628 num = 36 * num + converts[c];
2635 catch(exception& e) {
2636 errorOut(e, "MothurOut", "fromBase36");
2640 /***********************************************************************/
2642 int MothurOut::factorial(int num){
2646 for (int i = 1; i <= num; i++) {
2652 catch(exception& e) {
2653 errorOut(e, "MothurOut", "factorial");
2657 /***********************************************************************/
2659 int MothurOut::getNumSeqs(ifstream& file){
2661 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
2665 catch(exception& e) {
2666 errorOut(e, "MothurOut", "getNumSeqs");
2670 /***********************************************************************/
2671 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
2676 input = getline(file);
2677 if (input.length() != 0) {
2678 if(input[0] == '>'){ numSeqs++; }
2682 catch(exception& e) {
2683 errorOut(e, "MothurOut", "getNumSeqs");
2687 /***********************************************************************/
2689 //This function parses the estimator options and puts them in a vector
2690 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
2693 if (symbol == '-') { splitAtDash(estim, container); return; }
2695 string individual = "";
2696 int estimLength = estim.size();
2697 for(int i=0;i<estimLength;i++){
2698 if(estim[i] == symbol){
2699 container.push_back(individual);
2703 individual += estim[i];
2706 container.push_back(individual);
2709 catch(exception& e) {
2710 errorOut(e, "MothurOut", "splitAtChar");
2715 /***********************************************************************/
2717 //This function parses the estimator options and puts them in a vector
2718 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
2720 string individual = "";
2721 int estimLength = estim.size();
2722 bool prevEscape = false;
2723 /*for(int i=0;i<estimLength;i++){
2725 individual += estim[i];
2729 if(estim[i] == '\\'){
2732 else if(estim[i] == '-'){
2733 container.push_back(individual);
2738 individual += estim[i];
2745 for(int i=0;i<estimLength;i++){
2746 if(estim[i] == '-'){
2747 if (prevEscape) { individual += estim[i]; prevEscape = false; } //add in dash because it was escaped.
2749 container.push_back(individual);
2752 }else if(estim[i] == '\\'){
2753 if (i < estimLength-1) {
2754 if (estim[i+1] == '-') { prevEscape=true; } //are you a backslash before a dash, if yes ignore
2755 else { individual += estim[i]; prevEscape = false; } //if no, add in
2756 }else { individual += estim[i]; }
2758 individual += estim[i];
2764 container.push_back(individual);
2766 catch(exception& e) {
2767 errorOut(e, "MothurOut", "splitAtDash");
2772 /***********************************************************************/
2773 //This function parses the label options and puts them in a set
2774 void MothurOut::splitAtDash(string& estim, set<string>& container) {
2776 string individual = "";
2777 int estimLength = estim.size();
2778 bool prevEscape = false;
2780 for(int i=0;i<estimLength;i++){
2782 individual += estim[i];
2786 if(estim[i] == '\\'){
2789 else if(estim[i] == '-'){
2790 container.insert(individual);
2795 individual += estim[i];
2802 for(int i=0;i<estimLength;i++){
2803 if(estim[i] == '-'){
2804 if (prevEscape) { individual += estim[i]; prevEscape = false; } //add in dash because it was escaped.
2806 container.insert(individual);
2809 }else if(estim[i] == '\\'){
2810 if (i < estimLength-1) {
2811 if (estim[i+1] == '-') { prevEscape=true; } //are you a backslash before a dash, if yes ignore
2812 else { individual += estim[i]; prevEscape = false; } //if no, add in
2813 }else { individual += estim[i]; }
2815 individual += estim[i];
2818 container.insert(individual);
2821 catch(exception& e) {
2822 errorOut(e, "MothurOut", "splitAtDash");
2826 /***********************************************************************/
2827 //This function parses the line options and puts them in a set
2828 void MothurOut::splitAtDash(string& estim, set<int>& container) {
2830 string individual = "";
2832 int estimLength = estim.size();
2833 bool prevEscape = false;
2835 for(int i=0;i<estimLength;i++){
2837 individual += estim[i];
2841 if(estim[i] == '\\'){
2844 else if(estim[i] == '-'){
2845 convert(individual, lineNum); //convert the string to int
2846 container.insert(lineNum);
2851 individual += estim[i];
2857 for(int i=0;i<estimLength;i++){
2858 if(estim[i] == '-'){
2859 if (prevEscape) { individual += estim[i]; prevEscape = false; } //add in dash because it was escaped.
2861 convert(individual, lineNum); //convert the string to int
2862 container.insert(lineNum);
2865 }else if(estim[i] == '\\'){
2866 if (i < estimLength-1) {
2867 if (estim[i+1] == '-') { prevEscape=true; } //are you a backslash before a dash, if yes ignore
2868 else { individual += estim[i]; prevEscape = false; } //if no, add in
2869 }else { individual += estim[i]; }
2871 individual += estim[i];
2875 convert(individual, lineNum); //convert the string to int
2876 container.insert(lineNum);
2878 catch(exception& e) {
2879 errorOut(e, "MothurOut", "splitAtDash");
2884 /***********************************************************************/
2885 string MothurOut::makeList(vector<string>& names) {
2889 if (names.size() == 0) { return list; }
2891 for (int i = 0; i < names.size()-1; i++) { list += names[i] + ","; }
2894 list += names[names.size()-1];
2898 catch(exception& e) {
2899 errorOut(e, "MothurOut", "makeList");
2904 /***********************************************************************/
2905 //This function parses the a string and puts peices in a vector
2906 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
2908 string individual = "";
2909 int estimLength = estim.size();
2910 for(int i=0;i<estimLength;i++){
2911 if(estim[i] == ','){
2912 container.push_back(individual);
2916 individual += estim[i];
2919 container.push_back(individual);
2924 // string individual;
2926 // while (estim.find_first_of(',') != -1) {
2927 // individual = estim.substr(0,estim.find_first_of(','));
2928 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
2929 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
2930 // container.push_back(individual);
2934 // container.push_back(estim);
2936 catch(exception& e) {
2937 errorOut(e, "MothurOut", "splitAtComma");
2941 /***********************************************************************/
2942 //This function splits up the various option parameters
2943 void MothurOut::splitAtChar(string& prefix, string& suffix, char c){
2945 prefix = suffix.substr(0,suffix.find_first_of(c));
2946 if ((suffix.find_first_of(c)+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2947 suffix = suffix.substr(suffix.find_first_of(c)+1, suffix.length());
2949 while(suffix.at(0) == ' ')
2950 suffix = suffix.substr(1, suffix.length());
2951 }else { suffix = ""; }
2954 catch(exception& e) {
2955 errorOut(e, "MothurOut", "splitAtChar");
2960 /***********************************************************************/
2962 //This function splits up the various option parameters
2963 void MothurOut::splitAtComma(string& prefix, string& suffix){
2965 prefix = suffix.substr(0,suffix.find_first_of(','));
2966 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2967 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
2969 while(suffix.at(0) == ' ')
2970 suffix = suffix.substr(1, suffix.length());
2971 }else { suffix = ""; }
2974 catch(exception& e) {
2975 errorOut(e, "MothurOut", "splitAtComma");
2979 /***********************************************************************/
2981 //This function separates the key value from the option value i.e. dist=96_...
2982 void MothurOut::splitAtEquals(string& key, string& value){
2984 if(value.find_first_of('=') != -1){
2985 key = value.substr(0,value.find_first_of('='));
2986 if ((value.find_first_of('=')+1) <= value.length()) {
2987 value = value.substr(value.find_first_of('=')+1, value.length());
2994 catch(exception& e) {
2995 errorOut(e, "MothurOut", "splitAtEquals");
3000 /**************************************************************************************************/
3002 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
3004 for (int i = 0; i < Groups.size(); i++) {
3005 if (groupname == Groups[i]) { return true; }
3009 catch(exception& e) {
3010 errorOut(e, "MothurOut", "inUsersGroups");
3014 /**************************************************************************************************/
3016 bool MothurOut::inUsersGroups(vector<int> set, vector< vector<int> > sets) {
3018 for (int i = 0; i < sets.size(); i++) {
3019 if (set == sets[i]) { return true; }
3023 catch(exception& e) {
3024 errorOut(e, "MothurOut", "inUsersGroups");
3028 /**************************************************************************************************/
3030 bool MothurOut::inUsersGroups(int groupname, vector<int> Groups) {
3032 for (int i = 0; i < Groups.size(); i++) {
3033 if (groupname == Groups[i]) { return true; }
3037 catch(exception& e) {
3038 errorOut(e, "MothurOut", "inUsersGroups");
3043 /**************************************************************************************************/
3044 //returns true if any of the strings in first vector are in second vector
3045 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
3048 for (int i = 0; i < groupnames.size(); i++) {
3049 if (inUsersGroups(groupnames[i], Groups)) { return true; }
3053 catch(exception& e) {
3054 errorOut(e, "MothurOut", "inUsersGroups");
3058 /***********************************************************************/
3059 //this function determines if the user has given us labels that are smaller than the given label.
3060 //if so then it returns true so that the calling function can run the previous valid distance.
3061 //it's a "smart" distance function. It also checks for invalid labels.
3062 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
3065 set<string>::iterator it;
3066 vector<float> orderFloat;
3067 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
3068 map<string, float>::iterator it2;
3070 bool smaller = false;
3072 //unique is the smallest line
3073 if (label == "unique") { return false; }
3075 if (convertTestFloat(label, labelFloat)) {
3076 convert(label, labelFloat);
3077 }else { //cant convert
3082 //go through users set and make them floats
3083 for(it = userLabels.begin(); it != userLabels.end();) {
3086 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
3088 orderFloat.push_back(temp);
3089 userMap[*it] = temp;
3091 }else if (*it == "unique") {
3092 orderFloat.push_back(-1.0);
3093 userMap["unique"] = -1.0;
3096 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
3097 userLabels.erase(it++);
3102 sort(orderFloat.begin(), orderFloat.end());
3104 /*************************************************/
3105 //is this label bigger than any of the users labels
3106 /*************************************************/
3108 //loop through order until you find a label greater than label
3109 for (int i = 0; i < orderFloat.size(); i++) {
3110 if (orderFloat[i] < labelFloat) {
3112 if (orderFloat[i] == -1) {
3113 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
3114 userLabels.erase("unique");
3117 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
3119 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
3120 if (it2->second == orderFloat[i]) {
3122 //remove small labels
3123 userLabels.erase(s);
3127 if (errorOff == "") {mothurOut( s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
3129 //since they are sorted once you find a bigger one stop looking
3136 catch(exception& e) {
3137 errorOut(e, "MothurOut", "anyLabelsToProcess");
3142 /**************************************************************************************************/
3143 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
3148 string line = getline(file);
3150 //before we added this check
3151 if (line[0] != '#') { good = false; }
3154 line = line.substr(1);
3156 vector<string> versionVector;
3157 splitAtChar(version, versionVector, '.');
3159 //check file version
3160 vector<string> linesVector;
3161 splitAtChar(line, linesVector, '.');
3163 if (versionVector.size() != linesVector.size()) { good = false; }
3165 for (int j = 0; j < versionVector.size(); j++) {
3167 convert(versionVector[j], num1);
3168 convert(linesVector[j], num2);
3170 //if mothurs version is newer than this files version, then we want to remake it
3171 if (num1 > num2) { good = false; break; }
3177 if (!good) { file.close(); }
3178 else { file.seekg(0); }
3182 catch(exception& e) {
3183 errorOut(e, "MothurOut", "checkReleaseVersion");
3187 /**************************************************************************************************/
3188 vector<double> MothurOut::getAverages(vector< vector<double> >& dists) {
3190 vector<double> averages; //averages.resize(numComp, 0.0);
3191 for (int i = 0; i < dists[0].size(); i++) { averages.push_back(0.0); }
3193 for (int thisIter = 0; thisIter < dists.size(); thisIter++) {
3194 for (int i = 0; i < dists[thisIter].size(); i++) {
3195 averages[i] += dists[thisIter][i];
3200 for (int i = 0; i < averages.size(); i++) { averages[i] /= (double) dists.size(); }
3204 catch(exception& e) {
3205 errorOut(e, "MothurOut", "getAverages");
3209 /**************************************************************************************************/
3210 vector<double> MothurOut::getStandardDeviation(vector< vector<double> >& dists) {
3213 vector<double> averages = getAverages(dists);
3215 //find standard deviation
3216 vector<double> stdDev; //stdDev.resize(numComp, 0.0);
3217 for (int i = 0; i < dists[0].size(); i++) { stdDev.push_back(0.0); }
3219 for (int thisIter = 0; thisIter < dists.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3220 for (int j = 0; j < dists[thisIter].size(); j++) {
3221 stdDev[j] += ((dists[thisIter][j] - averages[j]) * (dists[thisIter][j] - averages[j]));
3224 for (int i = 0; i < stdDev.size(); i++) {
3225 stdDev[i] /= (double) dists.size();
3226 stdDev[i] = sqrt(stdDev[i]);
3231 catch(exception& e) {
3232 errorOut(e, "MothurOut", "getAverages");
3236 /**************************************************************************************************/
3237 vector<double> MothurOut::getStandardDeviation(vector< vector<double> >& dists, vector<double>& averages) {
3239 //find standard deviation
3240 vector<double> stdDev; //stdDev.resize(numComp, 0.0);
3241 for (int i = 0; i < dists[0].size(); i++) { stdDev.push_back(0.0); }
3243 for (int thisIter = 0; thisIter < dists.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3244 for (int j = 0; j < dists[thisIter].size(); j++) {
3245 stdDev[j] += ((dists[thisIter][j] - averages[j]) * (dists[thisIter][j] - averages[j]));
3248 for (int i = 0; i < stdDev.size(); i++) {
3249 stdDev[i] /= (double) dists.size();
3250 stdDev[i] = sqrt(stdDev[i]);
3255 catch(exception& e) {
3256 errorOut(e, "MothurOut", "getAverages");
3260 /**************************************************************************************************/
3261 vector< vector<seqDist> > MothurOut::getAverages(vector< vector< vector<seqDist> > >& calcDistsTotals, string mode) {
3264 vector< vector<seqDist> > calcAverages; //calcAverages.resize(calcDistsTotals[0].size());
3265 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3266 //calcAverages[i].resize(calcDistsTotals[0][i].size());
3267 vector<seqDist> temp;
3268 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3270 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3271 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3272 tempDist.dist = 0.0;
3273 temp.push_back(tempDist);
3275 calcAverages.push_back(temp);
3278 if (mode == "average") {
3279 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //sum all groups dists for each calculator
3280 for (int i = 0; i < calcAverages.size(); i++) { //initialize sums to zero.
3281 for (int j = 0; j < calcAverages[i].size(); j++) {
3282 calcAverages[i][j].dist += calcDistsTotals[thisIter][i][j].dist;
3287 for (int i = 0; i < calcAverages.size(); i++) { //finds average.
3288 for (int j = 0; j < calcAverages[i].size(); j++) {
3289 calcAverages[i][j].dist /= (float) calcDistsTotals.size();
3292 }else { //find median
3293 for (int i = 0; i < calcAverages.size(); i++) { //for each calc
3294 for (int j = 0; j < calcAverages[i].size(); j++) { //for each comparison
3295 vector<double> dists;
3296 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //for each subsample
3297 dists.push_back(calcDistsTotals[thisIter][i][j].dist);
3299 sort(dists.begin(), dists.end());
3300 calcAverages[i][j].dist = dists[(calcDistsTotals.size()/2)];
3305 return calcAverages;
3307 catch(exception& e) {
3308 errorOut(e, "MothurOut", "getAverages");
3312 /**************************************************************************************************/
3313 vector< vector<seqDist> > MothurOut::getAverages(vector< vector< vector<seqDist> > >& calcDistsTotals) {
3316 vector< vector<seqDist> > calcAverages; //calcAverages.resize(calcDistsTotals[0].size());
3317 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3318 //calcAverages[i].resize(calcDistsTotals[0][i].size());
3319 vector<seqDist> temp;
3320 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3322 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3323 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3324 tempDist.dist = 0.0;
3325 temp.push_back(tempDist);
3327 calcAverages.push_back(temp);
3331 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //sum all groups dists for each calculator
3332 for (int i = 0; i < calcAverages.size(); i++) { //initialize sums to zero.
3333 for (int j = 0; j < calcAverages[i].size(); j++) {
3334 calcAverages[i][j].dist += calcDistsTotals[thisIter][i][j].dist;
3339 for (int i = 0; i < calcAverages.size(); i++) { //finds average.
3340 for (int j = 0; j < calcAverages[i].size(); j++) {
3341 calcAverages[i][j].dist /= (float) calcDistsTotals.size();
3345 return calcAverages;
3347 catch(exception& e) {
3348 errorOut(e, "MothurOut", "getAverages");
3352 /**************************************************************************************************/
3353 vector< vector<seqDist> > MothurOut::getStandardDeviation(vector< vector< vector<seqDist> > >& calcDistsTotals) {
3356 vector< vector<seqDist> > calcAverages = getAverages(calcDistsTotals);
3358 //find standard deviation
3359 vector< vector<seqDist> > stdDev;
3360 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3361 vector<seqDist> temp;
3362 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3364 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3365 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3366 tempDist.dist = 0.0;
3367 temp.push_back(tempDist);
3369 stdDev.push_back(temp);
3372 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3373 for (int i = 0; i < stdDev.size(); i++) {
3374 for (int j = 0; j < stdDev[i].size(); j++) {
3375 stdDev[i][j].dist += ((calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist) * (calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist));
3380 for (int i = 0; i < stdDev.size(); i++) { //finds average.
3381 for (int j = 0; j < stdDev[i].size(); j++) {
3382 stdDev[i][j].dist /= (float) calcDistsTotals.size();
3383 stdDev[i][j].dist = sqrt(stdDev[i][j].dist);
3389 catch(exception& e) {
3390 errorOut(e, "MothurOut", "getAverages");
3394 /**************************************************************************************************/
3395 vector< vector<seqDist> > MothurOut::getStandardDeviation(vector< vector< vector<seqDist> > >& calcDistsTotals, vector< vector<seqDist> >& calcAverages) {
3397 //find standard deviation
3398 vector< vector<seqDist> > stdDev;
3399 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3400 vector<seqDist> temp;
3401 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3403 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3404 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3405 tempDist.dist = 0.0;
3406 temp.push_back(tempDist);
3408 stdDev.push_back(temp);
3411 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3412 for (int i = 0; i < stdDev.size(); i++) {
3413 for (int j = 0; j < stdDev[i].size(); j++) {
3414 stdDev[i][j].dist += ((calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist) * (calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist));
3419 for (int i = 0; i < stdDev.size(); i++) { //finds average.
3420 for (int j = 0; j < stdDev[i].size(); j++) {
3421 stdDev[i][j].dist /= (float) calcDistsTotals.size();
3422 stdDev[i][j].dist = sqrt(stdDev[i][j].dist);
3428 catch(exception& e) {
3429 errorOut(e, "MothurOut", "getAverages");
3434 /**************************************************************************************************/
3435 bool MothurOut::isContainingOnlyDigits(string input) {
3438 //are you a digit in ascii code
3439 for (int i = 0;i < input.length(); i++){
3440 if( input[i]>47 && input[i]<58){}
3441 else { return false; }
3446 catch(exception& e) {
3447 errorOut(e, "MothurOut", "isContainingOnlyDigits");
3451 /**************************************************************************************************/
3452 int MothurOut::removeConfidences(string& tax) {
3458 while (tax.find_first_of(';') != -1) {
3460 if (control_pressed) { return 0; }
3463 taxon = tax.substr(0,tax.find_first_of(';'));
3465 int pos = taxon.find_last_of('(');
3468 int pos2 = taxon.find_last_of(')');
3470 string confidenceScore = taxon.substr(pos+1, (pos2-(pos+1)));
3471 if (isNumeric1(confidenceScore)) {
3472 taxon = taxon.substr(0, pos); //rip off confidence
3478 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
3486 catch(exception& e) {
3487 errorOut(e, "MothurOut", "removeConfidences");
3491 /**************************************************************************************************/
3492 string MothurOut::removeQuotes(string tax) {
3498 for (int i = 0; i < tax.length(); i++) {
3500 if (control_pressed) { return newTax; }
3502 if ((tax[i] != '\'') && (tax[i] != '\"')) { newTax += tax[i]; }
3508 catch(exception& e) {
3509 errorOut(e, "MothurOut", "removeQuotes");
3513 /**************************************************************************************************/
3514 // function for calculating standard deviation
3515 double MothurOut::getStandardDeviation(vector<int>& featureVector){
3519 for (int i = 0; i < featureVector.size(); i++) { average += featureVector[i]; }
3520 average /= (double) featureVector.size();
3522 //find standard deviation
3524 for (int i = 0; i < featureVector.size(); i++) { //compute the difference of each dist from the mean, and square the result of each
3525 stdDev += ((featureVector[i] - average) * (featureVector[i] - average));
3528 stdDev /= (double) featureVector.size();
3529 stdDev = sqrt(stdDev);
3533 catch(exception& e) {
3534 errorOut(e, "MothurOut", "getStandardDeviation");
3538 /**************************************************************************************************/