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("summary");
27 types.insert("accnos");
28 types.insert("column");
29 types.insert("design");
30 types.insert("group");
33 types.insert("oligos");
34 types.insert("order");
35 types.insert("ordergroup");
36 types.insert("phylip");
37 types.insert("qfile");
38 types.insert("relabund");
39 types.insert("sabund");
40 types.insert("rabund");
42 types.insert("shared");
43 types.insert("taxonomy");
47 types.insert("count");
48 types.insert("processors");
53 errorOut(e, "MothurOut", "getCurrentTypes");
57 /*********************************************************************************************/
58 void MothurOut::printCurrentFiles() {
62 if (accnosfile != "") { mothurOut("accnos=" + accnosfile); mothurOutEndLine(); }
63 if (columnfile != "") { mothurOut("column=" + columnfile); mothurOutEndLine(); }
64 if (designfile != "") { mothurOut("design=" + designfile); mothurOutEndLine(); }
65 if (fastafile != "") { mothurOut("fasta=" + fastafile); mothurOutEndLine(); }
66 if (groupfile != "") { mothurOut("group=" + groupfile); mothurOutEndLine(); }
67 if (listfile != "") { mothurOut("list=" + listfile); mothurOutEndLine(); }
68 if (namefile != "") { mothurOut("name=" + namefile); mothurOutEndLine(); }
69 if (oligosfile != "") { mothurOut("oligos=" + oligosfile); mothurOutEndLine(); }
70 if (orderfile != "") { mothurOut("order=" + orderfile); mothurOutEndLine(); }
71 if (ordergroupfile != "") { mothurOut("ordergroup=" + ordergroupfile); mothurOutEndLine(); }
72 if (phylipfile != "") { mothurOut("phylip=" + phylipfile); mothurOutEndLine(); }
73 if (qualfile != "") { mothurOut("qfile=" + qualfile); mothurOutEndLine(); }
74 if (rabundfile != "") { mothurOut("rabund=" + rabundfile); mothurOutEndLine(); }
75 if (relabundfile != "") { mothurOut("relabund=" + relabundfile); mothurOutEndLine(); }
76 if (sabundfile != "") { mothurOut("sabund=" + sabundfile); mothurOutEndLine(); }
77 if (sfffile != "") { mothurOut("sff=" + sfffile); mothurOutEndLine(); }
78 if (sharedfile != "") { mothurOut("shared=" + sharedfile); mothurOutEndLine(); }
79 if (taxonomyfile != "") { mothurOut("taxonomy=" + taxonomyfile); mothurOutEndLine(); }
80 if (treefile != "") { mothurOut("tree=" + treefile); mothurOutEndLine(); }
81 if (flowfile != "") { mothurOut("flow=" + flowfile); mothurOutEndLine(); }
82 if (biomfile != "") { mothurOut("biom=" + biomfile); mothurOutEndLine(); }
83 if (counttablefile != "") { mothurOut("count=" + counttablefile); mothurOutEndLine(); }
84 if (processors != "1") { mothurOut("processors=" + processors); mothurOutEndLine(); }
85 if (summaryfile != "") { mothurOut("summary=" + summaryfile); mothurOutEndLine(); }
89 errorOut(e, "MothurOut", "printCurrentFiles");
93 /*********************************************************************************************/
94 bool MothurOut::hasCurrentFiles() {
96 bool hasCurrent = false;
98 if (accnosfile != "") { return true; }
99 if (columnfile != "") { return true; }
100 if (designfile != "") { return true; }
101 if (fastafile != "") { return true; }
102 if (groupfile != "") { return true; }
103 if (listfile != "") { return true; }
104 if (namefile != "") { return true; }
105 if (oligosfile != "") { return true; }
106 if (orderfile != "") { return true; }
107 if (ordergroupfile != "") { return true; }
108 if (phylipfile != "") { return true; }
109 if (qualfile != "") { return true; }
110 if (rabundfile != "") { return true; }
111 if (relabundfile != "") { return true; }
112 if (sabundfile != "") { return true; }
113 if (sfffile != "") { return true; }
114 if (sharedfile != "") { return true; }
115 if (taxonomyfile != "") { return true; }
116 if (treefile != "") { return true; }
117 if (flowfile != "") { return true; }
118 if (biomfile != "") { return true; }
119 if (counttablefile != "") { return true; }
120 if (summaryfile != "") { return true; }
121 if (processors != "1") { return true; }
126 catch(exception& e) {
127 errorOut(e, "MothurOut", "hasCurrentFiles");
132 /*********************************************************************************************/
133 void MothurOut::clearCurrentFiles() {
160 catch(exception& e) {
161 errorOut(e, "MothurOut", "clearCurrentFiles");
165 /***********************************************************************/
166 string MothurOut::findProgramPath(string programName){
169 string envPath = getenv("PATH");
172 //delimiting path char
174 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
180 //break apart path variable by ':'
182 splitAtChar(envPath, dirs, delim);
184 if (debug) { mothurOut("[DEBUG]: dir's in path: \n"); }
186 //get path related to mothur
187 for (int i = 0; i < dirs.size(); i++) {
189 if (debug) { mothurOut("[DEBUG]: " + dirs[i] + "\n"); }
191 //to lower so we can find it
192 string tempLower = "";
193 for (int j = 0; j < dirs[i].length(); j++) { tempLower += tolower(dirs[i][j]); }
195 //is this mothurs path?
196 if (tempLower.find(programName) != -1) { pPath = dirs[i]; break; }
199 if (debug) { mothurOut("[DEBUG]: programPath = " + pPath + "\n"); }
202 //add programName so it looks like what argv would look like
203 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
204 pPath += "/" + programName;
206 pPath += "\\" + programName;
209 //okay programName is not in the path, so the folder programName is in must be in the path
210 //lets find out which one
212 //get path related to the program
213 for (int i = 0; i < dirs.size(); i++) {
215 if (debug) { mothurOut("[DEBUG]: looking in " + dirs[i] + " for " + programName + " \n"); }
217 //is this the programs path?
219 string tempIn = dirs[i];
220 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
221 tempIn += "/" + programName;
223 tempIn += "\\" + programName;
225 openInputFile(tempIn, in, "");
227 //if this file exists
228 if (in) { in.close(); pPath = tempIn; if (debug) { mothurOut("[DEBUG]: found it, programPath = " + pPath + "\n"); } break; }
235 catch(exception& e) {
236 errorOut(e, "MothurOut", "findProgramPath");
240 /*********************************************************************************************/
241 void MothurOut::setFileName(string filename) {
243 logFileName = filename;
247 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
249 if (pid == 0) { //only one process should output to screen
252 openOutputFile(filename, out);
258 catch(exception& e) {
259 errorOut(e, "MothurOut", "setFileName");
263 /*********************************************************************************************/
264 void MothurOut::setDefaultPath(string pathname) {
267 //add / to name if needed
268 string lastChar = pathname.substr(pathname.length()-1);
269 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
270 if (lastChar != "/") { pathname += "/"; }
272 if (lastChar != "\\") { pathname += "\\"; }
275 defaultPath = pathname;
278 catch(exception& e) {
279 errorOut(e, "MothurOut", "setDefaultPath");
283 /*********************************************************************************************/
284 void MothurOut::setOutputDir(string pathname) {
286 outputDir = pathname;
288 catch(exception& e) {
289 errorOut(e, "MothurOut", "setOutputDir");
293 /*********************************************************************************************/
294 void MothurOut::closeLog() {
299 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
301 if (pid == 0) { //only one process should output to screen
310 catch(exception& e) {
311 errorOut(e, "MothurOut", "closeLog");
316 /*********************************************************************************************/
317 MothurOut::~MothurOut() {
322 catch(exception& e) {
323 errorOut(e, "MothurOut", "MothurOut");
327 /*********************************************************************************************/
328 void MothurOut::mothurOut(string output) {
333 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
335 if (pid == 0) { //only one process should output to screen
345 catch(exception& e) {
346 errorOut(e, "MothurOut", "MothurOut");
350 /*********************************************************************************************/
351 void MothurOut::mothurOutEndLine() {
355 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
357 if (pid == 0) { //only one process should output to screen
367 catch(exception& e) {
368 errorOut(e, "MothurOut", "MothurOutEndLine");
372 /*********************************************************************************************/
373 void MothurOut::mothurOut(string output, ofstream& outputFile) {
378 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
380 if (pid == 0) { //only one process should output to screen
385 outputFile << output;
393 catch(exception& e) {
394 errorOut(e, "MothurOut", "MothurOut");
398 /*********************************************************************************************/
399 void MothurOut::mothurOutEndLine(ofstream& outputFile) {
403 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
405 if (pid == 0) { //only one process should output to screen
416 catch(exception& e) {
417 errorOut(e, "MothurOut", "MothurOutEndLine");
421 /*********************************************************************************************/
422 void MothurOut::mothurOutJustToLog(string output) {
426 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
428 if (pid == 0) { //only one process should output to screen
437 catch(exception& e) {
438 errorOut(e, "MothurOut", "MothurOutJustToLog");
442 /*********************************************************************************************/
443 void MothurOut::errorOut(exception& e, string object, string function) {
445 //mem_usage(vm, rss);
447 string errorType = toString(e.what());
449 int pos = errorType.find("bad_alloc");
450 mothurOut("[ERROR]: ");
451 mothurOut(errorType);
453 if (pos == string::npos) { //not bad_alloc
454 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.");
457 if (object == "cluster"){
458 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.");
459 }else if (object == "shhh.flows"){
460 mothurOut(" has occurred in the " + object + " class function " + function + ". This error indicates your computer is running out of memory. The shhh.flows command is very memory intensive. This error is most commonly caused by trying to process a dataset too large, using multiple processors, or failing to run trim.flows before shhh.flows. If you are running our 32bit version, your memory usage is limited to 4G. If you have more than 4G of RAM and are running a 64bit OS, using our 64bit version may resolve your issue. If you are using multiple processors, try running the command with processors=1, the more processors you use the more memory is required. Running trim.flows with an oligos file, and then shhh.flows with the file option may also resolve the issue. If for some reason you are unable to run shhh.flows with your data, a good alternative is to use the trim.seqs command using a 50-bp sliding window and to trim the sequence when the average quality score over that window drops below 35. Our results suggest that the sequencing error rates by this method are very good, but not quite as good as by shhh.flows and that the resulting sequences tend to be a bit shorter. If you are uable to resolve the issue, please contact Pat Schloss at mothur.bugs@gmail.com, and be sure to include the mothur.logFile with your inquiry. ");
462 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.");
466 /*********************************************************************************************/
467 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
468 // process_mem_usage(double &, double &) - takes two doubles by reference,
469 // attempts to read the system-dependent data for a process' virtual memory
470 // size and resident set size, and return the results in KB.
472 // On failure, returns 0.0, 0.0
473 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
474 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
479 // 'file' stat seems to give the most reliable results
481 ifstream stat_stream("/proc/self/stat",ios_base::in);
483 // dummy vars for leading entries in stat that we don't care about
485 string pid, comm, state, ppid, pgrp, session, tty_nr;
486 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
487 string utime, stime, cutime, cstime, priority, nice;
488 string O, itrealvalue, starttime;
490 // the two fields we want
495 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
496 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
497 >> utime >> stime >> cutime >> cstime >> priority >> nice
498 >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
500 long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
501 vm_usage = vsize / 1024.0;
502 resident_set = rss * page_size_kb;
504 mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
508 /* //windows memory usage
509 // Get the list of process identifiers.
510 DWORD aProcesses[1024], cbNeeded, cProcesses;
512 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
514 // Calculate how many process identifiers were returned.
515 cProcesses = cbNeeded / sizeof(DWORD);
517 // Print the memory usage for each process
518 for (int i = 0; i < cProcesses; i++ ) {
519 DWORD processID = aProcesses[i];
521 PROCESS_MEMORY_COUNTERS pmc;
523 HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
525 // Print the process identifier.
526 printf( "\nProcess ID: %u\n", processID);
528 if (NULL != hProcess) {
530 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
531 printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
532 printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
533 printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
534 printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
535 printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
536 printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
537 printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
538 printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
539 printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
541 CloseHandle(hProcess);
551 /***********************************************************************/
552 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
554 fileName = getFullPathName(fileName);
556 fileHandle.open(fileName.c_str(), ios::app);
558 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
565 catch(exception& e) {
566 errorOut(e, "MothurOut", "openOutputFileAppend");
570 /***********************************************************************/
571 void MothurOut::gobble(istream& f){
575 while(isspace(d=f.get())) { ;}
576 if(!f.eof()) { f.putback(d); }
578 catch(exception& e) {
579 errorOut(e, "MothurOut", "gobble");
583 /***********************************************************************/
584 void MothurOut::gobble(istringstream& f){
587 while(isspace(d=f.get())) {;}
588 if(!f.eof()) { f.putback(d); }
590 catch(exception& e) {
591 errorOut(e, "MothurOut", "gobble");
596 /***********************************************************************/
598 string MothurOut::getline(istringstream& fileHandle) {
603 while (!fileHandle.eof()) {
605 char c = fileHandle.get();
607 //are you at the end of the line
608 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
615 catch(exception& e) {
616 errorOut(e, "MothurOut", "getline");
620 /***********************************************************************/
622 string MothurOut::getline(ifstream& fileHandle) {
629 char c = fileHandle.get();
631 //are you at the end of the line
632 if ((c == '\n') || (c == '\r') || (c == '\f') || (c == EOF)){ break; }
639 catch(exception& e) {
640 errorOut(e, "MothurOut", "getline");
644 /***********************************************************************/
646 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
647 #ifdef USE_COMPRESSION
648 inline bool endsWith(string s, const char * suffix){
649 size_t suffixLength = strlen(suffix);
650 return s.size() >= suffixLength && s.substr(s.size() - suffixLength, suffixLength).compare(suffix) == 0;
655 string MothurOut::getRootName(string longName){
658 string rootName = longName;
660 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
661 #ifdef USE_COMPRESSION
662 if (endsWith(rootName, ".gz") || endsWith(rootName, ".bz2")) {
663 int pos = rootName.find_last_of('.');
664 rootName = rootName.substr(0, pos);
665 cerr << "shortening " << longName << " to " << rootName << "\n";
669 if(rootName.find_last_of(".") != rootName.npos){
670 int pos = rootName.find_last_of('.')+1;
671 rootName = rootName.substr(0, pos);
676 catch(exception& e) {
677 errorOut(e, "MothurOut", "getRootName");
681 /***********************************************************************/
683 string MothurOut::getSimpleName(string longName){
685 string simpleName = longName;
688 found=longName.find_last_of("/\\");
690 if(found != longName.npos){
691 simpleName = longName.substr(found+1);
696 catch(exception& e) {
697 errorOut(e, "MothurOut", "getSimpleName");
702 /***********************************************************************/
704 int MothurOut::getRandomIndex(int highest){
707 int random = (int) ((float)(highest+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
711 catch(exception& e) {
712 errorOut(e, "MothurOut", "getRandomIndex");
717 /**********************************************************************/
719 string MothurOut::getPathName(string longName){
721 string rootPathName = longName;
723 if(longName.find_last_of("/\\") != longName.npos){
724 int pos = longName.find_last_of("/\\")+1;
725 rootPathName = longName.substr(0, pos);
730 catch(exception& e) {
731 errorOut(e, "MothurOut", "getPathName");
736 /***********************************************************************/
738 bool MothurOut::dirCheck(string& dirName){
744 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
749 //add / to name if needed
750 string lastChar = dirName.substr(dirName.length()-1);
751 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
752 if (lastChar != "/") { dirName += "/"; }
754 if (lastChar != "\\") { dirName += "\\"; }
757 //test to make sure directory exists
758 dirName = getFullPathName(dirName);
759 string outTemp = dirName + tag + "temp";
761 out.open(outTemp.c_str(), ios::trunc);
763 mothurOut(dirName + " directory does not exist or is not writable."); mothurOutEndLine();
766 mothurRemove(outTemp);
772 catch(exception& e) {
773 errorOut(e, "MothurOut", "dirCheck");
778 /***********************************************************************/
780 string MothurOut::hasPath(string longName){
785 found=longName.find_last_of("~/\\");
787 if(found != longName.npos){
788 path = longName.substr(0, found+1);
793 catch(exception& e) {
794 errorOut(e, "MothurOut", "hasPath");
799 /***********************************************************************/
801 string MothurOut::getExtension(string longName){
803 string extension = "";
805 if(longName.find_last_of('.') != longName.npos){
806 int pos = longName.find_last_of('.');
807 extension = longName.substr(pos, longName.length());
812 catch(exception& e) {
813 errorOut(e, "MothurOut", "getExtension");
817 /***********************************************************************/
818 bool MothurOut::isBlank(string fileName){
821 fileName = getFullPathName(fileName);
824 fileHandle.open(fileName.c_str());
826 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
829 //check for blank file
831 if (fileHandle.eof()) { fileHandle.close(); return true; }
836 catch(exception& e) {
837 errorOut(e, "MothurOut", "isBlank");
841 /***********************************************************************/
843 string MothurOut::getFullPathName(string fileName){
846 string path = hasPath(fileName);
850 if (path == "") { return fileName; } //its a simple name
851 else { //we need to complete the pathname
852 // ex. ../../../filename
853 // cwd = /user/work/desktop
856 //get current working directory
857 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
859 if (path.find("~") != -1) { //go to home directory
862 char *homepath = NULL;
863 homepath = getenv ("HOME");
864 if ( homepath != NULL) { homeDir = homepath; }
865 else { homeDir = ""; }
867 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
870 if (path.rfind("./") == string::npos) { return fileName; } //already complete name
871 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
873 //char* cwdpath = new char[1024];
875 //cwdpath=getcwd(cwdpath,size);
878 char *cwdpath = NULL;
879 cwdpath = getcwd(NULL, 0); // or _getcwd
880 if ( cwdpath != NULL) { cwd = cwdpath; }
886 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
888 //break apart the current working directory
890 while (simpleCWD.find_first_of('/') != string::npos) {
891 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
892 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
895 //get last one // ex. ../../../filename = /user/work/desktop/filename
896 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
899 int index = dirs.size()-1;
901 while((pos = path.rfind("./")) != string::npos) { //while you don't have a complete path
902 if (pos == 0) { break; //you are at the end
903 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
904 path = path.substr(0, pos-1);
906 if (index == 0) { break; }
907 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
908 path = path.substr(0, pos);
909 }else if (pos == 1) { break; //you are at the end
910 }else { mothurOut("cannot resolve path for " + fileName + "\n"); return fileName; }
913 for (int i = index; i >= 0; i--) {
914 newFileName = dirs[i] + "/" + newFileName;
917 newFileName = "/" + newFileName;
921 if (path.find("~") != string::npos) { //go to home directory
922 string homeDir = getenv ("HOMEPATH");
923 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
926 if (path.rfind(".\\") == string::npos) { return fileName; } //already complete name
927 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
929 char *cwdpath = NULL;
930 cwdpath = getcwd(NULL, 0); // or _getcwd
931 if ( cwdpath != NULL) { cwd = cwdpath; }
934 //break apart the current working directory
936 while (cwd.find_first_of('\\') != -1) {
937 string dir = cwd.substr(0,cwd.find_first_of('\\'));
938 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
943 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
945 int index = dirs.size()-1;
947 while((pos = path.rfind(".\\")) != string::npos) { //while you don't have a complete path
948 if (pos == 0) { break; //you are at the end
949 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
950 path = path.substr(0, pos-1);
952 if (index == 0) { break; }
953 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
954 path = path.substr(0, pos);
955 }else if (pos == 1) { break; //you are at the end
956 }else { mothurOut("cannot resolve path for " + fileName + "\n"); return fileName; }
959 for (int i = index; i >= 0; i--) {
960 newFileName = dirs[i] + "\\" + newFileName;
969 catch(exception& e) {
970 errorOut(e, "MothurOut", "getFullPathName");
974 /***********************************************************************/
976 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
979 string completeFileName = getFullPathName(fileName);
980 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
981 #ifdef USE_COMPRESSION
982 // check for gzipped or bzipped file
983 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
984 string tempName = string(tmpnam(0));
985 mkfifo(tempName.c_str(), 0666);
986 int fork_result = fork();
987 if (fork_result < 0) {
988 cerr << "Error forking.\n";
990 } else if (fork_result == 0) {
991 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
992 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
993 system(command.c_str());
994 cerr << "Done decompressing " << completeFileName << "\n";
995 mothurRemove(tempName);
998 cerr << "waiting on child process " << fork_result << "\n";
999 completeFileName = tempName;
1004 fileHandle.open(completeFileName.c_str());
1006 //mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1009 //check for blank file
1014 catch(exception& e) {
1015 errorOut(e, "MothurOut", "openInputFile - no Error");
1019 /***********************************************************************/
1021 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
1024 //get full path name
1025 string completeFileName = getFullPathName(fileName);
1026 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1027 #ifdef USE_COMPRESSION
1028 // check for gzipped or bzipped file
1029 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
1030 string tempName = string(tmpnam(0));
1031 mkfifo(tempName.c_str(), 0666);
1032 int fork_result = fork();
1033 if (fork_result < 0) {
1034 cerr << "Error forking.\n";
1036 } else if (fork_result == 0) {
1037 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
1038 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
1039 system(command.c_str());
1040 cerr << "Done decompressing " << completeFileName << "\n";
1041 mothurRemove(tempName);
1044 cerr << "waiting on child process " << fork_result << "\n";
1045 completeFileName = tempName;
1051 fileHandle.open(completeFileName.c_str());
1053 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1057 //check for blank file
1059 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
1064 catch(exception& e) {
1065 errorOut(e, "MothurOut", "openInputFile");
1069 /***********************************************************************/
1071 int MothurOut::renameFile(string oldName, string newName){
1074 if (oldName == newName) { return 0; }
1077 int exist = openInputFile(newName, inTest, "");
1080 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1081 if (exist == 0) { //you could open it so you want to delete it
1082 string command = "rm " + newName;
1083 system(command.c_str());
1086 string command = "mv " + oldName + " " + newName;
1087 system(command.c_str());
1089 mothurRemove(newName);
1090 int renameOk = rename(oldName.c_str(), newName.c_str());
1095 catch(exception& e) {
1096 errorOut(e, "MothurOut", "renameFile");
1101 /***********************************************************************/
1103 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
1106 string completeFileName = getFullPathName(fileName);
1107 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1108 #ifdef USE_COMPRESSION
1109 // check for gzipped file
1110 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
1111 string tempName = string(tmpnam(0));
1112 mkfifo(tempName.c_str(), 0666);
1113 cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
1114 int fork_result = fork();
1115 if (fork_result < 0) {
1116 cerr << "Error forking.\n";
1118 } else if (fork_result == 0) {
1119 string command = string(endsWith(completeFileName, ".gz") ? "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
1120 system(command.c_str());
1123 completeFileName = tempName;
1128 fileHandle.open(completeFileName.c_str(), ios::trunc);
1130 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1137 catch(exception& e) {
1138 errorOut(e, "MothurOut", "openOutputFile");
1144 /**************************************************************************************************/
1145 int MothurOut::appendFiles(string temp, string filename) {
1150 //open output file in append mode
1151 openOutputFileAppend(filename, output);
1152 int ableToOpen = openInputFile(temp, input, "no error");
1153 //int ableToOpen = openInputFile(temp, input);
1156 if (ableToOpen == 0) { //you opened it
1159 while (!input.eof()) {
1160 input.read(buffer, 4096);
1161 output.write(buffer, input.gcount());
1162 //count number of lines
1163 for (int i = 0; i < input.gcount(); i++) { if (buffer[i] == '\n') {numLines++;} }
1172 catch(exception& e) {
1173 errorOut(e, "MothurOut", "appendFiles");
1177 /**************************************************************************************************/
1178 int MothurOut::appendFilesWithoutHeaders(string temp, string filename) {
1183 //open output file in append mode
1184 openOutputFileAppend(filename, output);
1185 int ableToOpen = openInputFile(temp, input, "no error");
1186 //int ableToOpen = openInputFile(temp, input);
1189 if (ableToOpen == 0) { //you opened it
1191 string headers = getline(input); gobble(input);
1192 if (debug) { mothurOut("[DEBUG]: skipping headers " + headers +'\n'); }
1195 while (!input.eof()) {
1196 input.read(buffer, 4096);
1197 output.write(buffer, input.gcount());
1198 //count number of lines
1199 for (int i = 0; i < input.gcount(); i++) { if (buffer[i] == '\n') {numLines++;} }
1208 catch(exception& e) {
1209 errorOut(e, "MothurOut", "appendFiles");
1213 /**************************************************************************************************/
1214 string MothurOut::sortFile(string distFile, string outputDir){
1217 //if (outputDir == "") { outputDir += hasPath(distFile); }
1218 string outfile = getRootName(distFile) + "sorted.dist";
1221 //if you can, use the unix sort since its been optimized for years
1222 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1223 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
1224 system(command.c_str());
1225 #else //you are stuck with my best attempt...
1226 //windows sort does not have a way to specify a column, only a character in the line
1227 //since we cannot assume that the distance will always be at the the same character location on each line
1228 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
1230 //read in file line by file and put distance first
1231 string tempDistFile = distFile + ".temp";
1234 openInputFile(distFile, input);
1235 openOutputFile(tempDistFile, output);
1237 string firstName, secondName;
1239 while (!input.eof()) {
1240 input >> firstName >> secondName >> dist;
1241 output << dist << '\t' << firstName << '\t' << secondName << endl;
1248 //sort using windows sort
1249 string tempOutfile = outfile + ".temp";
1250 string command = "sort " + tempDistFile + " /O " + tempOutfile;
1251 system(command.c_str());
1253 //read in sorted file and put distance at end again
1256 openInputFile(tempOutfile, input2);
1257 openOutputFile(outfile, output2);
1259 while (!input2.eof()) {
1260 input2 >> dist >> firstName >> secondName;
1261 output2 << firstName << '\t' << secondName << '\t' << dist << endl;
1268 mothurRemove(tempDistFile);
1269 mothurRemove(tempOutfile);
1274 catch(exception& e) {
1275 errorOut(e, "MothurOut", "sortFile");
1279 /**************************************************************************************************/
1280 vector<unsigned long long> MothurOut::setFilePosFasta(string filename, int& num) {
1282 vector<unsigned long long> positions;
1284 //openInputFile(filename, inFASTA);
1285 inFASTA.open(filename.c_str(), ios::binary);
1288 unsigned long long count = 0;
1289 while(!inFASTA.eof()){
1290 //input = getline(inFASTA);
1291 //cout << input << '\t' << inFASTA.tellg() << endl;
1292 //if (input.length() != 0) {
1293 // if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); cout << (pos - input.length() - 1) << endl; }
1295 //gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
1296 char c = inFASTA.get(); count++;
1298 positions.push_back(count-1);
1299 //cout << count << endl;
1304 num = positions.size();
1309 //get num bytes in file
1310 pFile = fopen (filename.c_str(),"rb");
1311 if (pFile==NULL) perror ("Error opening file");
1313 fseek (pFile, 0, SEEK_END);
1318 unsigned long long size = positions[(positions.size()-1)];
1320 openInputFile(filename, in);
1325 if(in.eof()) { break; }
1330 positions.push_back(size);
1335 catch(exception& e) {
1336 errorOut(e, "MothurOut", "setFilePosFasta");
1340 /**************************************************************************************************/
1341 vector<unsigned long long> MothurOut::setFilePosEachLine(string filename, int& num) {
1343 filename = getFullPathName(filename);
1345 vector<unsigned long long> positions;
1347 //openInputFile(filename, in);
1348 in.open(filename.c_str(), ios::binary);
1351 unsigned long long count = 0;
1352 positions.push_back(0);
1355 //getline counting reads
1356 char d = in.get(); count++;
1357 while ((d != '\n') && (d != '\r') && (d != '\f') && (d != in.eof())) {
1358 //get next character
1364 d=in.get(); count++;
1365 while(isspace(d) && (d != in.eof())) { d=in.get(); count++;}
1367 positions.push_back(count-1);
1368 //cout << count-1 << endl;
1372 num = positions.size()-1;
1375 unsigned long long size;
1377 //get num bytes in file
1378 pFile = fopen (filename.c_str(),"rb");
1379 if (pFile==NULL) perror ("Error opening file");
1381 fseek (pFile, 0, SEEK_END);
1386 positions[(positions.size()-1)] = size;
1390 catch(exception& e) {
1391 errorOut(e, "MothurOut", "setFilePosEachLine");
1395 /**************************************************************************************************/
1397 vector<unsigned long long> MothurOut::divideFile(string filename, int& proc) {
1399 vector<unsigned long long> filePos;
1400 filePos.push_back(0);
1403 unsigned long long size;
1405 filename = getFullPathName(filename);
1407 //get num bytes in file
1408 pFile = fopen (filename.c_str(),"rb");
1409 if (pFile==NULL) perror ("Error opening file");
1411 fseek (pFile, 0, SEEK_END);
1416 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1418 //estimate file breaks
1419 unsigned long long chunkSize = 0;
1420 chunkSize = size / proc;
1422 //file to small to divide by processors
1423 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1425 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1426 for (int i = 0; i < proc; i++) {
1427 unsigned long long spot = (i+1) * chunkSize;
1430 openInputFile(filename, in);
1434 unsigned long long newSpot = spot;
1438 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1439 else if (int(c) == -1) { break; }
1443 //there was not another sequence before the end of the file
1444 unsigned long long sanityPos = in.tellg();
1446 if (sanityPos == -1) { break; }
1447 else { filePos.push_back(newSpot); }
1453 filePos.push_back(size);
1455 //sanity check filePos
1456 for (int i = 0; i < (filePos.size()-1); i++) {
1457 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1460 proc = (filePos.size() - 1);
1462 mothurOut("[ERROR]: Windows version should not be calling the divideFile function."); mothurOutEndLine();
1464 filePos.push_back(size);
1468 catch(exception& e) {
1469 errorOut(e, "MothurOut", "divideFile");
1473 /**************************************************************************************************/
1475 vector<unsigned long long> MothurOut::divideFilePerLine(string filename, int& proc) {
1477 vector<unsigned long long> filePos;
1478 filePos.push_back(0);
1481 unsigned long long size;
1483 filename = getFullPathName(filename);
1485 //get num bytes in file
1486 pFile = fopen (filename.c_str(),"rb");
1487 if (pFile==NULL) perror ("Error opening file");
1489 fseek (pFile, 0, SEEK_END);
1494 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1496 //estimate file breaks
1497 unsigned long long chunkSize = 0;
1498 chunkSize = size / proc;
1500 //file to small to divide by processors
1501 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1503 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1504 for (int i = 0; i < proc; i++) {
1505 unsigned long long spot = (i+1) * chunkSize;
1508 openInputFile(filename, in);
1511 //look for next line break
1512 unsigned long long newSpot = spot;
1516 if ((c == '\n') || (c == '\r') || (c == '\f')) { gobble(in); newSpot = in.tellg(); break; }
1517 else if (int(c) == -1) { break; }
1520 //there was not another line before the end of the file
1521 unsigned long long sanityPos = in.tellg();
1523 if (sanityPos == -1) { break; }
1524 else { filePos.push_back(newSpot); }
1530 filePos.push_back(size);
1532 //sanity check filePos
1533 for (int i = 0; i < (filePos.size()-1); i++) {
1534 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1537 proc = (filePos.size() - 1);
1539 mothurOut("[ERROR]: Windows version should not be calling the divideFile function."); mothurOutEndLine();
1541 filePos.push_back(size);
1545 catch(exception& e) {
1546 errorOut(e, "MothurOut", "divideFile");
1550 /**************************************************************************************************/
1551 int MothurOut::divideFile(string filename, int& proc, vector<string>& files) {
1554 vector<unsigned long long> filePos = divideFile(filename, proc);
1556 for (int i = 0; i < (filePos.size()-1); i++) {
1560 openInputFile(filename, in);
1561 in.seekg(filePos[i]);
1562 unsigned long long size = filePos[(i+1)] - filePos[i];
1563 char* chunk = new char[size];
1564 in.read(chunk, size);
1568 string fileChunkName = filename + "." + toString(i) + ".tmp";
1570 openOutputFile(fileChunkName, out);
1572 out << chunk << endl;
1577 files.push_back(fileChunkName);
1582 catch(exception& e) {
1583 errorOut(e, "MothurOut", "divideFile");
1587 /***********************************************************************/
1589 bool MothurOut::isTrue(string f){
1592 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1594 if ((f == "TRUE") || (f == "T")) { return true; }
1595 else { return false; }
1597 catch(exception& e) {
1598 errorOut(e, "MothurOut", "isTrue");
1603 /***********************************************************************/
1605 float MothurOut::roundDist(float dist, int precision){
1607 return int(dist * precision + 0.5)/float(precision);
1609 catch(exception& e) {
1610 errorOut(e, "MothurOut", "roundDist");
1614 /***********************************************************************/
1616 float MothurOut::ceilDist(float dist, int precision){
1618 return int(ceil(dist * precision))/float(precision);
1620 catch(exception& e) {
1621 errorOut(e, "MothurOut", "ceilDist");
1625 /***********************************************************************/
1627 vector<string> MothurOut::splitWhiteSpace(string& rest, char buffer[], int size){
1629 vector<string> pieces;
1631 for (int i = 0; i < size; i++) {
1632 if (!isspace(buffer[i])) { rest += buffer[i]; }
1634 if (rest != "") { pieces.push_back(rest); rest = ""; }
1635 while (i < size) { //gobble white space
1636 if (isspace(buffer[i])) { i++; }
1637 else { rest = buffer[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1644 catch(exception& e) {
1645 errorOut(e, "MothurOut", "splitWhiteSpace");
1649 /***********************************************************************/
1650 vector<string> MothurOut::splitWhiteSpace(string input){
1652 vector<string> pieces;
1655 for (int i = 0; i < input.length(); i++) {
1656 if (!isspace(input[i])) { rest += input[i]; }
1658 if (rest != "") { pieces.push_back(rest); rest = ""; }
1659 while (i < input.length()) { //gobble white space
1660 if (isspace(input[i])) { i++; }
1661 else { rest = input[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1666 if (rest != "") { pieces.push_back(rest); }
1670 catch(exception& e) {
1671 errorOut(e, "MothurOut", "splitWhiteSpace");
1675 /***********************************************************************/
1676 vector<string> MothurOut::splitWhiteSpaceWithQuotes(string input){
1678 vector<string> pieces;
1681 int pos = input.find('\'');
1682 int pos2 = input.find('\"');
1684 if ((pos == string::npos) && (pos2 == string::npos)) { return splitWhiteSpace(input); } //no quotes to worry about
1686 for (int i = 0; i < input.length(); i++) {
1687 if ((input[i] == '\'') || (input[i] == '\"') || (rest == "\'") || (rest == "\"")) { //grab everything til end or next ' or "
1689 for (int j = i+1; j < input.length(); j++) {
1690 if ((input[j] == '\'') || (input[j] == '\"')) { //then quit
1694 }else { rest += input[j]; }
1696 }else if (!isspace(input[i])) { rest += input[i]; }
1698 if (rest != "") { pieces.push_back(rest); rest = ""; }
1699 while (i < input.length()) { //gobble white space
1700 if (isspace(input[i])) { i++; }
1701 else { rest = input[i]; break; } //cout << "next piece buffer = " << nextPiece << endl;
1706 if (rest != "") { pieces.push_back(rest); }
1710 catch(exception& e) {
1711 errorOut(e, "MothurOut", "splitWhiteSpace");
1715 //**********************************************************************************************************************
1716 int MothurOut::readTax(string namefile, map<string, string>& taxMap) {
1720 openInputFile(namefile, in);
1724 bool pairDone = false;
1725 bool columnOne = true;
1726 string firstCol, secondCol;
1729 if (control_pressed) { break; }
1731 in.read(buffer, 4096);
1732 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1734 for (int i = 0; i < pieces.size(); i++) {
1735 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1736 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1739 checkName(firstCol);
1740 //are there confidence scores, if so remove them
1741 if (secondCol.find_first_of('(') != -1) { removeConfidences(secondCol); }
1742 map<string, string>::iterator itTax = taxMap.find(firstCol);
1744 if(itTax == taxMap.end()) {
1745 bool ignore = false;
1746 if (secondCol != "") { if (secondCol[secondCol.length()-1] != ';') { mothurOut("[ERROR]: " + firstCol + " is missing the final ';', ignoring.\n"); ignore=true; }
1748 if (!ignore) { taxMap[firstCol] = secondCol; }
1749 if (debug) { mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n"); }
1751 mothurOut("[ERROR]: " + firstCol + " is already in your taxonomy file, names must be unique./n"); control_pressed = true;
1760 vector<string> pieces = splitWhiteSpace(rest);
1762 for (int i = 0; i < pieces.size(); i++) {
1763 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1764 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1767 checkName(firstCol);
1768 //are there confidence scores, if so remove them
1769 if (secondCol.find_first_of('(') != -1) { removeConfidences(secondCol); }
1770 map<string, string>::iterator itTax = taxMap.find(firstCol);
1772 if(itTax == taxMap.end()) {
1773 bool ignore = false;
1774 if (secondCol != "") { if (secondCol[secondCol.length()-1] != ';') { mothurOut("[ERROR]: " + firstCol + " is missing the final ';', ignoring.\n"); ignore=true; }
1776 if (!ignore) { taxMap[firstCol] = secondCol; }
1777 if (debug) { mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n"); }
1779 mothurOut("[ERROR]: " + firstCol + " is already in your taxonomy file, names must be unique./n"); control_pressed = true;
1787 return taxMap.size();
1790 catch(exception& e) {
1791 errorOut(e, "MothurOut", "readTax");
1795 /**********************************************************************************************************************/
1796 int MothurOut::readNames(string namefile, map<string, string>& nameMap, bool redund) {
1800 openInputFile(namefile, in);
1804 bool pairDone = false;
1805 bool columnOne = true;
1806 string firstCol, secondCol;
1809 if (control_pressed) { break; }
1811 in.read(buffer, 4096);
1812 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1814 for (int i = 0; i < pieces.size(); i++) {
1815 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1816 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1819 checkName(firstCol);
1820 checkName(secondCol);
1822 //parse names into vector
1823 vector<string> theseNames;
1824 splitAtComma(secondCol, theseNames);
1825 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1833 vector<string> pieces = splitWhiteSpace(rest);
1835 for (int i = 0; i < pieces.size(); i++) {
1836 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1837 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1840 checkName(firstCol);
1841 checkName(secondCol);
1843 //parse names into vector
1844 vector<string> theseNames;
1845 splitAtComma(secondCol, theseNames);
1846 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1852 return nameMap.size();
1855 catch(exception& e) {
1856 errorOut(e, "MothurOut", "readNames");
1860 /**********************************************************************************************************************/
1861 int MothurOut::readNames(string namefile, map<string, string>& nameMap, int flip) {
1865 openInputFile(namefile, in);
1869 bool pairDone = false;
1870 bool columnOne = true;
1871 string firstCol, secondCol;
1874 if (control_pressed) { break; }
1876 in.read(buffer, 4096);
1877 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1879 for (int i = 0; i < pieces.size(); i++) {
1880 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1881 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1884 checkName(firstCol);
1885 checkName(secondCol);
1886 nameMap[secondCol] = firstCol;
1894 vector<string> pieces = splitWhiteSpace(rest);
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 nameMap[secondCol] = firstCol;
1909 return nameMap.size();
1912 catch(exception& e) {
1913 errorOut(e, "MothurOut", "readNames");
1917 /**********************************************************************************************************************/
1918 int MothurOut::readNames(string namefile, map<string, string>& nameMap, map<string, int>& nameCount) {
1920 nameMap.clear(); nameCount.clear();
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 //parse names into vector
1945 vector<string> theseNames;
1946 splitAtComma(secondCol, theseNames);
1947 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1948 nameCount[firstCol] = theseNames.size();
1956 vector<string> pieces = splitWhiteSpace(rest);
1958 for (int i = 0; i < pieces.size(); i++) {
1959 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
1960 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1963 checkName(firstCol);
1964 checkName(secondCol);
1965 //parse names into vector
1966 vector<string> theseNames;
1967 splitAtComma(secondCol, theseNames);
1968 for (int i = 0; i < theseNames.size(); i++) { nameMap[theseNames[i]] = firstCol; }
1969 nameCount[firstCol] = theseNames.size();
1975 return nameMap.size();
1978 catch(exception& e) {
1979 errorOut(e, "MothurOut", "readNames");
1983 /**********************************************************************************************************************/
1984 int MothurOut::readNames(string namefile, map<string, string>& nameMap) {
1988 openInputFile(namefile, in);
1992 bool pairDone = false;
1993 bool columnOne = true;
1994 string firstCol, secondCol;
1997 if (control_pressed) { break; }
1999 in.read(buffer, 4096);
2000 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2002 for (int i = 0; i < pieces.size(); i++) {
2003 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2004 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2007 checkName(firstCol);
2008 checkName(secondCol);
2009 nameMap[firstCol] = secondCol; pairDone = false; }
2015 vector<string> pieces = splitWhiteSpace(rest);
2017 for (int i = 0; i < pieces.size(); i++) {
2018 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2019 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2022 checkName(firstCol);
2023 checkName(secondCol);
2024 nameMap[firstCol] = secondCol; pairDone = false; }
2028 return nameMap.size();
2031 catch(exception& e) {
2032 errorOut(e, "MothurOut", "readNames");
2036 /**********************************************************************************************************************/
2037 int MothurOut::readNames(string namefile, map<string, vector<string> >& nameMap) {
2041 openInputFile(namefile, in);
2045 bool pairDone = false;
2046 bool columnOne = true;
2047 string firstCol, secondCol;
2050 if (control_pressed) { break; }
2052 in.read(buffer, 4096);
2053 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2055 for (int i = 0; i < pieces.size(); i++) {
2056 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2057 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2060 checkName(firstCol);
2061 checkName(secondCol);
2062 vector<string> temp;
2063 splitAtComma(secondCol, temp);
2064 nameMap[firstCol] = temp;
2072 vector<string> pieces = splitWhiteSpace(rest);
2074 for (int i = 0; i < pieces.size(); i++) {
2075 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2076 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2079 checkName(firstCol);
2080 checkName(secondCol);
2081 vector<string> temp;
2082 splitAtComma(secondCol, temp);
2083 nameMap[firstCol] = temp;
2089 return nameMap.size();
2091 catch(exception& e) {
2092 errorOut(e, "MothurOut", "readNames");
2096 /**********************************************************************************************************************/
2097 map<string, int> MothurOut::readNames(string namefile) {
2099 map<string, int> nameMap;
2103 openInputFile(namefile, in);
2107 bool pairDone = false;
2108 bool columnOne = true;
2109 string firstCol, secondCol;
2112 if (control_pressed) { break; }
2114 in.read(buffer, 4096);
2115 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2117 for (int i = 0; i < pieces.size(); i++) {
2118 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2119 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2122 checkName(firstCol);
2123 checkName(secondCol);
2124 int num = getNumNames(secondCol);
2125 nameMap[firstCol] = num;
2133 vector<string> pieces = splitWhiteSpace(rest);
2134 for (int i = 0; i < pieces.size(); i++) {
2135 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2136 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2139 checkName(firstCol);
2140 checkName(secondCol);
2141 int num = getNumNames(secondCol);
2142 nameMap[firstCol] = num;
2151 catch(exception& e) {
2152 errorOut(e, "MothurOut", "readNames");
2156 /**********************************************************************************************************************/
2157 map<string, int> MothurOut::readNames(string namefile, unsigned long int& numSeqs) {
2159 map<string, int> nameMap;
2164 openInputFile(namefile, in);
2168 bool pairDone = false;
2169 bool columnOne = true;
2170 string firstCol, secondCol;
2173 if (control_pressed) { break; }
2175 in.read(buffer, 4096);
2176 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2178 for (int i = 0; i < pieces.size(); i++) {
2179 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2180 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2183 checkName(firstCol);
2184 checkName(secondCol);
2185 int num = getNumNames(secondCol);
2186 nameMap[firstCol] = num;
2195 vector<string> pieces = splitWhiteSpace(rest);
2196 for (int i = 0; i < pieces.size(); i++) {
2197 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2198 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2201 checkName(firstCol);
2202 checkName(secondCol);
2203 int num = getNumNames(secondCol);
2204 nameMap[firstCol] = num;
2214 catch(exception& e) {
2215 errorOut(e, "MothurOut", "readNames");
2219 /************************************************************/
2220 int MothurOut::checkName(string& name) {
2222 for (int i = 0; i < name.length(); i++) {
2223 if (name[i] == ':') { name[i] = '_'; changedSeqNames = true; }
2227 catch(exception& e) {
2228 errorOut(e, "MothurOut", "checkName");
2232 /**********************************************************************************************************************/
2233 int MothurOut::readNames(string namefile, vector<seqPriorityNode>& nameVector, map<string, string>& fastamap) {
2239 openInputFile(namefile, in);
2243 bool pairDone = false;
2244 bool columnOne = true;
2245 string firstCol, secondCol;
2248 if (control_pressed) { break; }
2250 in.read(buffer, 4096);
2251 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2253 for (int i = 0; i < pieces.size(); i++) {
2254 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2255 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2258 checkName(firstCol);
2259 checkName(secondCol);
2260 int num = getNumNames(secondCol);
2262 map<string, string>::iterator it = fastamap.find(firstCol);
2263 if (it == fastamap.end()) {
2265 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
2267 seqPriorityNode temp(num, it->second, firstCol);
2268 nameVector.push_back(temp);
2278 vector<string> pieces = splitWhiteSpace(rest);
2280 for (int i = 0; i < pieces.size(); i++) {
2281 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
2282 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
2285 checkName(firstCol);
2286 checkName(secondCol);
2287 int num = getNumNames(secondCol);
2289 map<string, string>::iterator it = fastamap.find(firstCol);
2290 if (it == fastamap.end()) {
2292 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
2294 seqPriorityNode temp(num, it->second, firstCol);
2295 nameVector.push_back(temp);
2304 catch(exception& e) {
2305 errorOut(e, "MothurOut", "readNames");
2309 //**********************************************************************************************************************
2310 set<string> MothurOut::readAccnos(string accnosfile){
2314 openInputFile(accnosfile, in);
2321 if (control_pressed) { break; }
2323 in.read(buffer, 4096);
2324 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2326 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.insert(pieces[i]); }
2331 vector<string> pieces = splitWhiteSpace(rest);
2332 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.insert(pieces[i]); }
2336 catch(exception& e) {
2337 errorOut(e, "MothurOut", "readAccnos");
2341 //**********************************************************************************************************************
2342 int MothurOut::readAccnos(string accnosfile, vector<string>& names){
2346 openInputFile(accnosfile, in);
2353 if (control_pressed) { break; }
2355 in.read(buffer, 4096);
2356 vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2358 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.push_back(pieces[i]); }
2363 vector<string> pieces = splitWhiteSpace(rest);
2364 for (int i = 0; i < pieces.size(); i++) { checkName(pieces[i]); names.push_back(pieces[i]); }
2369 catch(exception& e) {
2370 errorOut(e, "MothurOut", "readAccnos");
2374 /***********************************************************************/
2376 int MothurOut::getNumNames(string names){
2382 for(int i=0;i<names.size();i++){
2383 if(names[i] == ','){
2391 catch(exception& e) {
2392 errorOut(e, "MothurOut", "getNumNames");
2396 /***********************************************************************/
2398 int MothurOut::getNumChar(string line, char c){
2403 for(int i=0;i<line.size();i++){
2412 catch(exception& e) {
2413 errorOut(e, "MothurOut", "getNumChar");
2417 //**********************************************************************************************************************
2418 bool MothurOut::isSubset(vector<string> bigset, vector<string> subset) {
2422 if (subset.size() > bigset.size()) { return false; }
2424 //check if each guy in suset is also in bigset
2425 for (int i = 0; i < subset.size(); i++) {
2427 for (int j = 0; j < bigset.size(); j++) {
2428 if (subset[i] == bigset[j]) { match = true; break; }
2431 //you have a guy in subset that had no match in bigset
2432 if (match == false) { return false; }
2438 catch(exception& e) {
2439 errorOut(e, "MothurOut", "isSubset");
2443 /***********************************************************************/
2444 int MothurOut::mothurRemove(string filename){
2446 filename = getFullPathName(filename);
2447 int error = remove(filename.c_str());
2449 // if (errno != ENOENT) { //ENOENT == file does not exist
2450 // string message = "Error deleting file " + filename;
2451 // perror(message.c_str());
2456 catch(exception& e) {
2457 errorOut(e, "MothurOut", "mothurRemove");
2461 /***********************************************************************/
2462 bool MothurOut::mothurConvert(string item, int& num){
2466 if (isNumeric1(item)) {
2471 mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2472 commandInputsConvertError = true;
2477 catch(exception& e) {
2478 errorOut(e, "MothurOut", "mothurConvert");
2482 /***********************************************************************/
2483 bool MothurOut::mothurConvert(string item, intDist& num){
2487 if (isNumeric1(item)) {
2492 mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2493 commandInputsConvertError = true;
2498 catch(exception& e) {
2499 errorOut(e, "MothurOut", "mothurConvert");
2504 /***********************************************************************/
2505 bool MothurOut::isNumeric1(string stringToCheck){
2507 bool numeric = false;
2509 if(stringToCheck.find_first_not_of("0123456789.-") == string::npos) { numeric = true; }
2513 catch(exception& e) {
2514 errorOut(e, "MothurOut", "isNumeric1");
2519 /***********************************************************************/
2520 bool MothurOut::mothurConvert(string item, float& num){
2524 if (isNumeric1(item)) {
2529 mothurOut("[ERROR]: cannot convert " + item + " to a float."); mothurOutEndLine();
2530 commandInputsConvertError = true;
2535 catch(exception& e) {
2536 errorOut(e, "MothurOut", "mothurConvert");
2540 /***********************************************************************/
2541 bool MothurOut::mothurConvert(string item, double& num){
2545 if (isNumeric1(item)) {
2550 mothurOut("[ERROR]: cannot convert " + item + " to a double."); mothurOutEndLine();
2551 commandInputsConvertError = true;
2556 catch(exception& e) {
2557 errorOut(e, "MothurOut", "mothurConvert");
2561 /**************************************************************************************************/
2563 vector<vector<double> > MothurOut::binomial(int maxOrder){
2565 vector<vector<double> > binomial(maxOrder+1);
2567 for(int i=0;i<=maxOrder;i++){
2568 binomial[i].resize(maxOrder+1);
2577 for(int i=2;i<=maxOrder;i++){
2581 for(int i=2;i<=maxOrder;i++){
2582 for(int j=1;j<=maxOrder;j++){
2583 if(i==j){ binomial[i][j]=1; }
2584 if(j>i) { binomial[i][j]=0; }
2585 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
2592 catch(exception& e) {
2593 errorOut(e, "MothurOut", "binomial");
2597 /**************************************************************************************************/
2598 unsigned int MothurOut::fromBase36(string base36){
2600 unsigned int num = 0;
2602 map<char, int> converts;
2667 while (i < base36.length()) {
2669 num = 36 * num + converts[c];
2676 catch(exception& e) {
2677 errorOut(e, "MothurOut", "fromBase36");
2681 /***********************************************************************/
2683 int MothurOut::factorial(int num){
2687 for (int i = 1; i <= num; i++) {
2693 catch(exception& e) {
2694 errorOut(e, "MothurOut", "factorial");
2698 /***********************************************************************/
2700 int MothurOut::getNumSeqs(ifstream& file){
2702 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
2706 catch(exception& e) {
2707 errorOut(e, "MothurOut", "getNumSeqs");
2711 /***********************************************************************/
2712 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
2717 input = getline(file);
2718 if (input.length() != 0) {
2719 if(input[0] == '>'){ numSeqs++; }
2723 catch(exception& e) {
2724 errorOut(e, "MothurOut", "getNumSeqs");
2728 /***********************************************************************/
2730 //This function parses the estimator options and puts them in a vector
2731 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
2734 if (symbol == '-') { splitAtDash(estim, container); return; }
2736 string individual = "";
2737 int estimLength = estim.size();
2738 for(int i=0;i<estimLength;i++){
2739 if(estim[i] == symbol){
2740 container.push_back(individual);
2744 individual += estim[i];
2747 container.push_back(individual);
2750 catch(exception& e) {
2751 errorOut(e, "MothurOut", "splitAtChar");
2756 /***********************************************************************/
2758 //This function parses the estimator options and puts them in a vector
2759 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
2761 string individual = "";
2762 int estimLength = estim.size();
2763 bool prevEscape = false;
2764 /*for(int i=0;i<estimLength;i++){
2766 individual += estim[i];
2770 if(estim[i] == '\\'){
2773 else if(estim[i] == '-'){
2774 container.push_back(individual);
2779 individual += estim[i];
2786 for(int i=0;i<estimLength;i++){
2787 if(estim[i] == '-'){
2788 if (prevEscape) { individual += estim[i]; prevEscape = false; } //add in dash because it was escaped.
2790 container.push_back(individual);
2793 }else if(estim[i] == '\\'){
2794 if (i < estimLength-1) {
2795 if (estim[i+1] == '-') { prevEscape=true; } //are you a backslash before a dash, if yes ignore
2796 else { individual += estim[i]; prevEscape = false; } //if no, add in
2797 }else { individual += estim[i]; }
2799 individual += estim[i];
2805 container.push_back(individual);
2807 catch(exception& e) {
2808 errorOut(e, "MothurOut", "splitAtDash");
2813 /***********************************************************************/
2814 //This function parses the label options and puts them in a set
2815 void MothurOut::splitAtDash(string& estim, set<string>& container) {
2817 string individual = "";
2818 int estimLength = estim.size();
2819 bool prevEscape = false;
2821 for(int i=0;i<estimLength;i++){
2823 individual += estim[i];
2827 if(estim[i] == '\\'){
2830 else if(estim[i] == '-'){
2831 container.insert(individual);
2836 individual += estim[i];
2843 for(int i=0;i<estimLength;i++){
2844 if(estim[i] == '-'){
2845 if (prevEscape) { individual += estim[i]; prevEscape = false; } //add in dash because it was escaped.
2847 container.insert(individual);
2850 }else if(estim[i] == '\\'){
2851 if (i < estimLength-1) {
2852 if (estim[i+1] == '-') { prevEscape=true; } //are you a backslash before a dash, if yes ignore
2853 else { individual += estim[i]; prevEscape = false; } //if no, add in
2854 }else { individual += estim[i]; }
2856 individual += estim[i];
2859 container.insert(individual);
2862 catch(exception& e) {
2863 errorOut(e, "MothurOut", "splitAtDash");
2867 /***********************************************************************/
2868 //This function parses the line options and puts them in a set
2869 void MothurOut::splitAtDash(string& estim, set<int>& container) {
2871 string individual = "";
2873 int estimLength = estim.size();
2874 bool prevEscape = false;
2876 for(int i=0;i<estimLength;i++){
2878 individual += estim[i];
2882 if(estim[i] == '\\'){
2885 else if(estim[i] == '-'){
2886 convert(individual, lineNum); //convert the string to int
2887 container.insert(lineNum);
2892 individual += estim[i];
2898 for(int i=0;i<estimLength;i++){
2899 if(estim[i] == '-'){
2900 if (prevEscape) { individual += estim[i]; prevEscape = false; } //add in dash because it was escaped.
2902 convert(individual, lineNum); //convert the string to int
2903 container.insert(lineNum);
2906 }else if(estim[i] == '\\'){
2907 if (i < estimLength-1) {
2908 if (estim[i+1] == '-') { prevEscape=true; } //are you a backslash before a dash, if yes ignore
2909 else { individual += estim[i]; prevEscape = false; } //if no, add in
2910 }else { individual += estim[i]; }
2912 individual += estim[i];
2916 convert(individual, lineNum); //convert the string to int
2917 container.insert(lineNum);
2919 catch(exception& e) {
2920 errorOut(e, "MothurOut", "splitAtDash");
2925 /***********************************************************************/
2926 string MothurOut::makeList(vector<string>& names) {
2930 if (names.size() == 0) { return list; }
2932 for (int i = 0; i < names.size()-1; i++) { list += names[i] + ","; }
2935 list += names[names.size()-1];
2939 catch(exception& e) {
2940 errorOut(e, "MothurOut", "makeList");
2945 /***********************************************************************/
2946 //This function parses the a string and puts peices in a vector
2947 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
2949 string individual = "";
2950 int estimLength = estim.size();
2951 for(int i=0;i<estimLength;i++){
2952 if(estim[i] == ','){
2953 container.push_back(individual);
2957 individual += estim[i];
2960 container.push_back(individual);
2965 // string individual;
2967 // while (estim.find_first_of(',') != -1) {
2968 // individual = estim.substr(0,estim.find_first_of(','));
2969 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
2970 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
2971 // container.push_back(individual);
2975 // container.push_back(estim);
2977 catch(exception& e) {
2978 errorOut(e, "MothurOut", "splitAtComma");
2982 /***********************************************************************/
2983 //This function splits up the various option parameters
2984 void MothurOut::splitAtChar(string& prefix, string& suffix, char c){
2986 prefix = suffix.substr(0,suffix.find_first_of(c));
2987 if ((suffix.find_first_of(c)+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
2988 suffix = suffix.substr(suffix.find_first_of(c)+1, suffix.length());
2990 while(suffix.at(0) == ' ')
2991 suffix = suffix.substr(1, suffix.length());
2992 }else { suffix = ""; }
2995 catch(exception& e) {
2996 errorOut(e, "MothurOut", "splitAtChar");
3001 /***********************************************************************/
3003 //This function splits up the various option parameters
3004 void MothurOut::splitAtComma(string& prefix, string& suffix){
3006 prefix = suffix.substr(0,suffix.find_first_of(','));
3007 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
3008 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
3010 while(suffix.at(0) == ' ')
3011 suffix = suffix.substr(1, suffix.length());
3012 }else { suffix = ""; }
3015 catch(exception& e) {
3016 errorOut(e, "MothurOut", "splitAtComma");
3020 /***********************************************************************/
3022 //This function separates the key value from the option value i.e. dist=96_...
3023 void MothurOut::splitAtEquals(string& key, string& value){
3025 if(value.find_first_of('=') != -1){
3026 key = value.substr(0,value.find_first_of('='));
3027 if ((value.find_first_of('=')+1) <= value.length()) {
3028 value = value.substr(value.find_first_of('=')+1, value.length());
3035 catch(exception& e) {
3036 errorOut(e, "MothurOut", "splitAtEquals");
3041 /**************************************************************************************************/
3043 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
3045 for (int i = 0; i < Groups.size(); i++) {
3046 if (groupname == Groups[i]) { return true; }
3050 catch(exception& e) {
3051 errorOut(e, "MothurOut", "inUsersGroups");
3055 /**************************************************************************************************/
3057 bool MothurOut::inUsersGroups(vector<int> set, vector< vector<int> > sets) {
3059 for (int i = 0; i < sets.size(); i++) {
3060 if (set == sets[i]) { return true; }
3064 catch(exception& e) {
3065 errorOut(e, "MothurOut", "inUsersGroups");
3069 /**************************************************************************************************/
3071 bool MothurOut::inUsersGroups(int groupname, vector<int> Groups) {
3073 for (int i = 0; i < Groups.size(); i++) {
3074 if (groupname == Groups[i]) { return true; }
3078 catch(exception& e) {
3079 errorOut(e, "MothurOut", "inUsersGroups");
3084 /**************************************************************************************************/
3085 //returns true if any of the strings in first vector are in second vector
3086 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
3089 for (int i = 0; i < groupnames.size(); i++) {
3090 if (inUsersGroups(groupnames[i], Groups)) { return true; }
3094 catch(exception& e) {
3095 errorOut(e, "MothurOut", "inUsersGroups");
3099 /***********************************************************************/
3100 //this function determines if the user has given us labels that are smaller than the given label.
3101 //if so then it returns true so that the calling function can run the previous valid distance.
3102 //it's a "smart" distance function. It also checks for invalid labels.
3103 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
3106 set<string>::iterator it;
3107 vector<float> orderFloat;
3108 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
3109 map<string, float>::iterator it2;
3111 bool smaller = false;
3113 //unique is the smallest line
3114 if (label == "unique") { return false; }
3116 if (convertTestFloat(label, labelFloat)) {
3117 convert(label, labelFloat);
3118 }else { //cant convert
3123 //go through users set and make them floats
3124 for(it = userLabels.begin(); it != userLabels.end();) {
3127 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
3129 orderFloat.push_back(temp);
3130 userMap[*it] = temp;
3132 }else if (*it == "unique") {
3133 orderFloat.push_back(-1.0);
3134 userMap["unique"] = -1.0;
3137 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
3138 userLabels.erase(it++);
3143 sort(orderFloat.begin(), orderFloat.end());
3145 /*************************************************/
3146 //is this label bigger than any of the users labels
3147 /*************************************************/
3149 //loop through order until you find a label greater than label
3150 for (int i = 0; i < orderFloat.size(); i++) {
3151 if (orderFloat[i] < labelFloat) {
3153 if (orderFloat[i] == -1) {
3154 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
3155 userLabels.erase("unique");
3158 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
3160 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
3161 if (it2->second == orderFloat[i]) {
3163 //remove small labels
3164 userLabels.erase(s);
3168 if (errorOff == "") {mothurOut( s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
3170 //since they are sorted once you find a bigger one stop looking
3177 catch(exception& e) {
3178 errorOut(e, "MothurOut", "anyLabelsToProcess");
3183 /**************************************************************************************************/
3184 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
3189 string line = getline(file);
3191 //before we added this check
3192 if (line[0] != '#') { good = false; }
3195 line = line.substr(1);
3197 vector<string> versionVector;
3198 splitAtChar(version, versionVector, '.');
3200 //check file version
3201 vector<string> linesVector;
3202 splitAtChar(line, linesVector, '.');
3204 if (versionVector.size() != linesVector.size()) { good = false; }
3206 for (int j = 0; j < versionVector.size(); j++) {
3208 convert(versionVector[j], num1);
3209 convert(linesVector[j], num2);
3211 //if mothurs version is newer than this files version, then we want to remake it
3212 if (num1 > num2) { good = false; break; }
3218 if (!good) { file.close(); }
3219 else { file.seekg(0); }
3223 catch(exception& e) {
3224 errorOut(e, "MothurOut", "checkReleaseVersion");
3228 /**************************************************************************************************/
3229 vector<double> MothurOut::getAverages(vector< vector<double> >& dists) {
3231 vector<double> averages; //averages.resize(numComp, 0.0);
3232 for (int i = 0; i < dists[0].size(); i++) { averages.push_back(0.0); }
3234 for (int thisIter = 0; thisIter < dists.size(); thisIter++) {
3235 for (int i = 0; i < dists[thisIter].size(); i++) {
3236 averages[i] += dists[thisIter][i];
3241 for (int i = 0; i < averages.size(); i++) { averages[i] /= (double) dists.size(); }
3245 catch(exception& e) {
3246 errorOut(e, "MothurOut", "getAverages");
3250 /**************************************************************************************************/
3251 vector<double> MothurOut::getStandardDeviation(vector< vector<double> >& dists) {
3254 vector<double> averages = getAverages(dists);
3256 //find standard deviation
3257 vector<double> stdDev; //stdDev.resize(numComp, 0.0);
3258 for (int i = 0; i < dists[0].size(); i++) { stdDev.push_back(0.0); }
3260 for (int thisIter = 0; thisIter < dists.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3261 for (int j = 0; j < dists[thisIter].size(); j++) {
3262 stdDev[j] += ((dists[thisIter][j] - averages[j]) * (dists[thisIter][j] - averages[j]));
3265 for (int i = 0; i < stdDev.size(); i++) {
3266 stdDev[i] /= (double) dists.size();
3267 stdDev[i] = sqrt(stdDev[i]);
3272 catch(exception& e) {
3273 errorOut(e, "MothurOut", "getAverages");
3277 /**************************************************************************************************/
3278 vector<double> MothurOut::getStandardDeviation(vector< vector<double> >& dists, vector<double>& averages) {
3280 //find standard deviation
3281 vector<double> stdDev; //stdDev.resize(numComp, 0.0);
3282 for (int i = 0; i < dists[0].size(); i++) { stdDev.push_back(0.0); }
3284 for (int thisIter = 0; thisIter < dists.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3285 for (int j = 0; j < dists[thisIter].size(); j++) {
3286 stdDev[j] += ((dists[thisIter][j] - averages[j]) * (dists[thisIter][j] - averages[j]));
3289 for (int i = 0; i < stdDev.size(); i++) {
3290 stdDev[i] /= (double) dists.size();
3291 stdDev[i] = sqrt(stdDev[i]);
3296 catch(exception& e) {
3297 errorOut(e, "MothurOut", "getAverages");
3301 /**************************************************************************************************/
3302 vector< vector<seqDist> > MothurOut::getAverages(vector< vector< vector<seqDist> > >& calcDistsTotals, string mode) {
3305 vector< vector<seqDist> > calcAverages; //calcAverages.resize(calcDistsTotals[0].size());
3306 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3307 //calcAverages[i].resize(calcDistsTotals[0][i].size());
3308 vector<seqDist> temp;
3309 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3311 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3312 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3313 tempDist.dist = 0.0;
3314 temp.push_back(tempDist);
3316 calcAverages.push_back(temp);
3319 if (mode == "average") {
3320 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //sum all groups dists for each calculator
3321 for (int i = 0; i < calcAverages.size(); i++) { //initialize sums to zero.
3322 for (int j = 0; j < calcAverages[i].size(); j++) {
3323 calcAverages[i][j].dist += calcDistsTotals[thisIter][i][j].dist;
3328 for (int i = 0; i < calcAverages.size(); i++) { //finds average.
3329 for (int j = 0; j < calcAverages[i].size(); j++) {
3330 calcAverages[i][j].dist /= (float) calcDistsTotals.size();
3333 }else { //find median
3334 for (int i = 0; i < calcAverages.size(); i++) { //for each calc
3335 for (int j = 0; j < calcAverages[i].size(); j++) { //for each comparison
3336 vector<double> dists;
3337 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //for each subsample
3338 dists.push_back(calcDistsTotals[thisIter][i][j].dist);
3340 sort(dists.begin(), dists.end());
3341 calcAverages[i][j].dist = dists[(calcDistsTotals.size()/2)];
3346 return calcAverages;
3348 catch(exception& e) {
3349 errorOut(e, "MothurOut", "getAverages");
3353 /**************************************************************************************************/
3354 vector< vector<seqDist> > MothurOut::getAverages(vector< vector< vector<seqDist> > >& calcDistsTotals) {
3357 vector< vector<seqDist> > calcAverages; //calcAverages.resize(calcDistsTotals[0].size());
3358 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3359 //calcAverages[i].resize(calcDistsTotals[0][i].size());
3360 vector<seqDist> temp;
3361 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3363 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3364 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3365 tempDist.dist = 0.0;
3366 temp.push_back(tempDist);
3368 calcAverages.push_back(temp);
3372 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //sum all groups dists for each calculator
3373 for (int i = 0; i < calcAverages.size(); i++) { //initialize sums to zero.
3374 for (int j = 0; j < calcAverages[i].size(); j++) {
3375 calcAverages[i][j].dist += calcDistsTotals[thisIter][i][j].dist;
3380 for (int i = 0; i < calcAverages.size(); i++) { //finds average.
3381 for (int j = 0; j < calcAverages[i].size(); j++) {
3382 calcAverages[i][j].dist /= (float) calcDistsTotals.size();
3386 return calcAverages;
3388 catch(exception& e) {
3389 errorOut(e, "MothurOut", "getAverages");
3393 /**************************************************************************************************/
3394 vector< vector<seqDist> > MothurOut::getStandardDeviation(vector< vector< vector<seqDist> > >& calcDistsTotals) {
3397 vector< vector<seqDist> > calcAverages = getAverages(calcDistsTotals);
3399 //find standard deviation
3400 vector< vector<seqDist> > stdDev;
3401 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3402 vector<seqDist> temp;
3403 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3405 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3406 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3407 tempDist.dist = 0.0;
3408 temp.push_back(tempDist);
3410 stdDev.push_back(temp);
3413 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3414 for (int i = 0; i < stdDev.size(); i++) {
3415 for (int j = 0; j < stdDev[i].size(); j++) {
3416 stdDev[i][j].dist += ((calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist) * (calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist));
3421 for (int i = 0; i < stdDev.size(); i++) { //finds average.
3422 for (int j = 0; j < stdDev[i].size(); j++) {
3423 stdDev[i][j].dist /= (float) calcDistsTotals.size();
3424 stdDev[i][j].dist = sqrt(stdDev[i][j].dist);
3430 catch(exception& e) {
3431 errorOut(e, "MothurOut", "getAverages");
3435 /**************************************************************************************************/
3436 vector< vector<seqDist> > MothurOut::getStandardDeviation(vector< vector< vector<seqDist> > >& calcDistsTotals, vector< vector<seqDist> >& calcAverages) {
3438 //find standard deviation
3439 vector< vector<seqDist> > stdDev;
3440 for (int i = 0; i < calcDistsTotals[0].size(); i++) { //initialize sums to zero.
3441 vector<seqDist> temp;
3442 for (int j = 0; j < calcDistsTotals[0][i].size(); j++) {
3444 tempDist.seq1 = calcDistsTotals[0][i][j].seq1;
3445 tempDist.seq2 = calcDistsTotals[0][i][j].seq2;
3446 tempDist.dist = 0.0;
3447 temp.push_back(tempDist);
3449 stdDev.push_back(temp);
3452 for (int thisIter = 0; thisIter < calcDistsTotals.size(); thisIter++) { //compute the difference of each dist from the mean, and square the result of each
3453 for (int i = 0; i < stdDev.size(); i++) {
3454 for (int j = 0; j < stdDev[i].size(); j++) {
3455 stdDev[i][j].dist += ((calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist) * (calcDistsTotals[thisIter][i][j].dist - calcAverages[i][j].dist));
3460 for (int i = 0; i < stdDev.size(); i++) { //finds average.
3461 for (int j = 0; j < stdDev[i].size(); j++) {
3462 stdDev[i][j].dist /= (float) calcDistsTotals.size();
3463 stdDev[i][j].dist = sqrt(stdDev[i][j].dist);
3469 catch(exception& e) {
3470 errorOut(e, "MothurOut", "getAverages");
3475 /**************************************************************************************************/
3476 bool MothurOut::isContainingOnlyDigits(string input) {
3479 //are you a digit in ascii code
3480 for (int i = 0;i < input.length(); i++){
3481 if( input[i]>47 && input[i]<58){}
3482 else { return false; }
3487 catch(exception& e) {
3488 errorOut(e, "MothurOut", "isContainingOnlyDigits");
3492 /**************************************************************************************************/
3493 int MothurOut::removeConfidences(string& tax) {
3499 while (tax.find_first_of(';') != -1) {
3501 if (control_pressed) { return 0; }
3504 taxon = tax.substr(0,tax.find_first_of(';'));
3506 int pos = taxon.find_last_of('(');
3509 int pos2 = taxon.find_last_of(')');
3511 string confidenceScore = taxon.substr(pos+1, (pos2-(pos+1)));
3512 if (isNumeric1(confidenceScore)) {
3513 taxon = taxon.substr(0, pos); //rip off confidence
3519 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
3527 catch(exception& e) {
3528 errorOut(e, "MothurOut", "removeConfidences");
3532 /**************************************************************************************************/
3533 string MothurOut::removeQuotes(string tax) {
3539 for (int i = 0; i < tax.length(); i++) {
3541 if (control_pressed) { return newTax; }
3543 if ((tax[i] != '\'') && (tax[i] != '\"')) { newTax += tax[i]; }
3549 catch(exception& e) {
3550 errorOut(e, "MothurOut", "removeQuotes");
3554 /**************************************************************************************************/
3555 // function for calculating standard deviation
3556 double MothurOut::getStandardDeviation(vector<int>& featureVector){
3560 for (int i = 0; i < featureVector.size(); i++) { average += featureVector[i]; }
3561 average /= (double) featureVector.size();
3563 //find standard deviation
3565 for (int i = 0; i < featureVector.size(); i++) { //compute the difference of each dist from the mean, and square the result of each
3566 stdDev += ((featureVector[i] - average) * (featureVector[i] - average));
3569 stdDev /= (double) featureVector.size();
3570 stdDev = sqrt(stdDev);
3574 catch(exception& e) {
3575 errorOut(e, "MothurOut", "getStandardDeviation");
3579 /**************************************************************************************************/