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 void MothurOut::printCurrentFiles() {
23 if (accnosfile != "") { mothurOut("accnos=" + accnosfile); mothurOutEndLine(); }
24 if (columnfile != "") { mothurOut("column=" + columnfile); mothurOutEndLine(); }
25 if (designfile != "") { mothurOut("design=" + designfile); mothurOutEndLine(); }
26 if (fastafile != "") { mothurOut("fasta=" + fastafile); mothurOutEndLine(); }
27 if (groupfile != "") { mothurOut("group=" + groupfile); mothurOutEndLine(); }
28 if (listfile != "") { mothurOut("list=" + listfile); mothurOutEndLine(); }
29 if (namefile != "") { mothurOut("name=" + namefile); mothurOutEndLine(); }
30 if (oligosfile != "") { mothurOut("oligos=" + oligosfile); mothurOutEndLine(); }
31 if (orderfile != "") { mothurOut("order=" + orderfile); mothurOutEndLine(); }
32 if (ordergroupfile != "") { mothurOut("ordergroup=" + ordergroupfile); mothurOutEndLine(); }
33 if (phylipfile != "") { mothurOut("phylip=" + phylipfile); mothurOutEndLine(); }
34 if (qualfile != "") { mothurOut("qfile=" + qualfile); mothurOutEndLine(); }
35 if (rabundfile != "") { mothurOut("rabund=" + rabundfile); mothurOutEndLine(); }
36 if (relabundfile != "") { mothurOut("relabund=" + relabundfile); mothurOutEndLine(); }
37 if (sabundfile != "") { mothurOut("sabund=" + sabundfile); mothurOutEndLine(); }
38 if (sfffile != "") { mothurOut("sff=" + sfffile); mothurOutEndLine(); }
39 if (sharedfile != "") { mothurOut("shared=" + sharedfile); mothurOutEndLine(); }
40 if (taxonomyfile != "") { mothurOut("taxonomy=" + taxonomyfile); mothurOutEndLine(); }
41 if (treefile != "") { mothurOut("tree=" + treefile); mothurOutEndLine(); }
42 if (flowfile != "") { mothurOut("flow=" + flowfile); mothurOutEndLine(); }
43 if (processors != "1") { mothurOut("processors=" + processors); mothurOutEndLine(); }
47 errorOut(e, "MothurOut", "printCurrentFiles");
51 /*********************************************************************************************/
52 bool MothurOut::hasCurrentFiles() {
54 bool hasCurrent = false;
56 if (accnosfile != "") { return true; }
57 if (columnfile != "") { return true; }
58 if (designfile != "") { return true; }
59 if (fastafile != "") { return true; }
60 if (groupfile != "") { return true; }
61 if (listfile != "") { return true; }
62 if (namefile != "") { return true; }
63 if (oligosfile != "") { return true; }
64 if (orderfile != "") { return true; }
65 if (ordergroupfile != "") { return true; }
66 if (phylipfile != "") { return true; }
67 if (qualfile != "") { return true; }
68 if (rabundfile != "") { return true; }
69 if (relabundfile != "") { return true; }
70 if (sabundfile != "") { return true; }
71 if (sfffile != "") { return true; }
72 if (sharedfile != "") { return true; }
73 if (taxonomyfile != "") { return true; }
74 if (treefile != "") { return true; }
75 if (flowfile != "") { return true; }
76 if (processors != "1") { return true; }
82 errorOut(e, "MothurOut", "hasCurrentFiles");
87 /*********************************************************************************************/
88 void MothurOut::clearCurrentFiles() {
112 catch(exception& e) {
113 errorOut(e, "MothurOut", "clearCurrentFiles");
117 /*********************************************************************************************/
118 void MothurOut::setFileName(string filename) {
120 logFileName = filename;
124 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
126 if (pid == 0) { //only one process should output to screen
129 openOutputFile(filename, out);
135 catch(exception& e) {
136 errorOut(e, "MothurOut", "setFileName");
140 /*********************************************************************************************/
141 void MothurOut::setDefaultPath(string pathname) {
144 //add / to name if needed
145 string lastChar = pathname.substr(pathname.length()-1);
146 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
147 if (lastChar != "/") { pathname += "/"; }
149 if (lastChar != "\\") { pathname += "\\"; }
152 defaultPath = pathname;
155 catch(exception& e) {
156 errorOut(e, "MothurOut", "setDefaultPath");
160 /*********************************************************************************************/
161 void MothurOut::setOutputDir(string pathname) {
163 outputDir = pathname;
165 catch(exception& e) {
166 errorOut(e, "MothurOut", "setOutputDir");
170 /*********************************************************************************************/
171 void MothurOut::closeLog() {
176 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
178 if (pid == 0) { //only one process should output to screen
187 catch(exception& e) {
188 errorOut(e, "MothurOut", "closeLog");
193 /*********************************************************************************************/
194 MothurOut::~MothurOut() {
199 catch(exception& e) {
200 errorOut(e, "MothurOut", "MothurOut");
204 /*********************************************************************************************/
205 void MothurOut::mothurOut(string output) {
210 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
212 if (pid == 0) { //only one process should output to screen
222 catch(exception& e) {
223 errorOut(e, "MothurOut", "MothurOut");
227 /*********************************************************************************************/
228 void MothurOut::mothurOutEndLine() {
232 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
234 if (pid == 0) { //only one process should output to screen
244 catch(exception& e) {
245 errorOut(e, "MothurOut", "MothurOutEndLine");
249 /*********************************************************************************************/
250 void MothurOut::mothurOutJustToLog(string output) {
254 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
256 if (pid == 0) { //only one process should output to screen
265 catch(exception& e) {
266 errorOut(e, "MothurOut", "MothurOutJustToLog");
270 /*********************************************************************************************/
271 void MothurOut::errorOut(exception& e, string object, string function) {
273 //mem_usage(vm, rss);
275 mothurOut("[ERROR]: ");
276 mothurOut(toString(e.what()));
277 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.");
280 /*********************************************************************************************/
281 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
282 // process_mem_usage(double &, double &) - takes two doubles by reference,
283 // attempts to read the system-dependent data for a process' virtual memory
284 // size and resident set size, and return the results in KB.
286 // On failure, returns 0.0, 0.0
287 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
288 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
293 // 'file' stat seems to give the most reliable results
295 ifstream stat_stream("/proc/self/stat",ios_base::in);
297 // dummy vars for leading entries in stat that we don't care about
299 string pid, comm, state, ppid, pgrp, session, tty_nr;
300 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
301 string utime, stime, cutime, cstime, priority, nice;
302 string O, itrealvalue, starttime;
304 // the two fields we want
309 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
310 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
311 >> utime >> stime >> cutime >> cstime >> priority >> nice
312 >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
314 long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
315 vm_usage = vsize / 1024.0;
316 resident_set = rss * page_size_kb;
318 mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
322 /* //windows memory usage
323 // Get the list of process identifiers.
324 DWORD aProcesses[1024], cbNeeded, cProcesses;
326 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
328 // Calculate how many process identifiers were returned.
329 cProcesses = cbNeeded / sizeof(DWORD);
331 // Print the memory usage for each process
332 for (int i = 0; i < cProcesses; i++ ) {
333 DWORD processID = aProcesses[i];
335 PROCESS_MEMORY_COUNTERS pmc;
337 HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
339 // Print the process identifier.
340 printf( "\nProcess ID: %u\n", processID);
342 if (NULL != hProcess) {
344 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
345 printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
346 printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
347 printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
348 printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
349 printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
350 printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
351 printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
352 printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
353 printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
355 CloseHandle(hProcess);
365 /***********************************************************************/
366 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
368 fileName = getFullPathName(fileName);
370 fileHandle.open(fileName.c_str(), ios::app);
372 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
379 catch(exception& e) {
380 errorOut(e, "MothurOut", "openOutputFileAppend");
384 /***********************************************************************/
385 void MothurOut::gobble(istream& f){
389 while(isspace(d=f.get())) { ;}
392 catch(exception& e) {
393 errorOut(e, "MothurOut", "gobble");
397 /***********************************************************************/
398 void MothurOut::gobble(istringstream& f){
401 while(isspace(d=f.get())) {;}
404 catch(exception& e) {
405 errorOut(e, "MothurOut", "gobble");
410 /***********************************************************************/
412 string MothurOut::getline(istringstream& fileHandle) {
417 while (!fileHandle.eof()) {
419 char c = fileHandle.get();
421 //are you at the end of the line
422 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
429 catch(exception& e) {
430 errorOut(e, "MothurOut", "getline");
434 /***********************************************************************/
436 string MothurOut::getline(ifstream& fileHandle) {
443 char c = fileHandle.get();
445 //are you at the end of the line
446 if ((c == '\n') || (c == '\r') || (c == '\f') || (c == EOF)){ break; }
453 catch(exception& e) {
454 errorOut(e, "MothurOut", "getline");
458 /***********************************************************************/
460 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
461 #ifdef USE_COMPRESSION
462 inline bool endsWith(string s, const char * suffix){
463 size_t suffixLength = strlen(suffix);
464 return s.size() >= suffixLength && s.substr(s.size() - suffixLength, suffixLength).compare(suffix) == 0;
469 string MothurOut::getRootName(string longName){
472 string rootName = longName;
474 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
475 #ifdef USE_COMPRESSION
476 if (endsWith(rootName, ".gz") || endsWith(rootName, ".bz2")) {
477 int pos = rootName.find_last_of('.');
478 rootName = rootName.substr(0, pos);
479 cerr << "shortening " << longName << " to " << rootName << "\n";
483 if(rootName.find_last_of(".") != rootName.npos){
484 int pos = rootName.find_last_of('.')+1;
485 rootName = rootName.substr(0, pos);
490 catch(exception& e) {
491 errorOut(e, "MothurOut", "getRootName");
495 /***********************************************************************/
497 string MothurOut::getSimpleName(string longName){
499 string simpleName = longName;
502 found=longName.find_last_of("/\\");
504 if(found != longName.npos){
505 simpleName = longName.substr(found+1);
510 catch(exception& e) {
511 errorOut(e, "MothurOut", "getSimpleName");
516 /***********************************************************************/
518 string MothurOut::getPathName(string longName){
520 string rootPathName = longName;
522 if(longName.find_last_of("/\\") != longName.npos){
523 int pos = longName.find_last_of("/\\")+1;
524 rootPathName = longName.substr(0, pos);
529 catch(exception& e) {
530 errorOut(e, "MothurOut", "getPathName");
535 /***********************************************************************/
537 string MothurOut::hasPath(string longName){
542 found=longName.find_last_of("~/\\");
544 if(found != longName.npos){
545 path = longName.substr(0, found+1);
550 catch(exception& e) {
551 errorOut(e, "MothurOut", "hasPath");
556 /***********************************************************************/
558 string MothurOut::getExtension(string longName){
560 string extension = longName;
562 if(longName.find_last_of('.') != longName.npos){
563 int pos = longName.find_last_of('.');
564 extension = longName.substr(pos, longName.length());
569 catch(exception& e) {
570 errorOut(e, "MothurOut", "getExtension");
574 /***********************************************************************/
575 bool MothurOut::isBlank(string fileName){
578 fileName = getFullPathName(fileName);
581 fileHandle.open(fileName.c_str());
583 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
586 //check for blank file
588 if (fileHandle.eof()) { fileHandle.close(); return true; }
593 catch(exception& e) {
594 errorOut(e, "MothurOut", "isBlank");
598 /***********************************************************************/
600 string MothurOut::getFullPathName(string fileName){
603 string path = hasPath(fileName);
607 if (path == "") { return fileName; } //its a simple name
608 else { //we need to complete the pathname
609 // ex. ../../../filename
610 // cwd = /user/work/desktop
613 //get current working directory
614 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
616 if (path.find("~") != -1) { //go to home directory
619 char *homepath = NULL;
620 homepath = getenv ("HOME");
621 if ( homepath != NULL) { homeDir = homepath; }
622 else { homeDir = ""; }
624 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
627 if (path.rfind("./") == string::npos) { return fileName; } //already complete name
628 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
630 //char* cwdpath = new char[1024];
632 //cwdpath=getcwd(cwdpath,size);
635 char *cwdpath = NULL;
636 cwdpath = getcwd(NULL, 0); // or _getcwd
637 if ( cwdpath != NULL) { cwd = cwdpath; }
643 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
645 //break apart the current working directory
647 while (simpleCWD.find_first_of('/') != string::npos) {
648 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
649 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
652 //get last one // ex. ../../../filename = /user/work/desktop/filename
653 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
656 int index = dirs.size()-1;
658 while((pos = path.rfind("./")) != string::npos) { //while you don't have a complete path
659 if (pos == 0) { break; //you are at the end
660 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
661 path = path.substr(0, pos-1);
663 if (index == 0) { break; }
664 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
665 path = path.substr(0, pos);
666 }else if (pos == 1) { break; //you are at the end
667 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
670 for (int i = index; i >= 0; i--) {
671 newFileName = dirs[i] + "/" + newFileName;
674 newFileName = "/" + newFileName;
678 if (path.find("~") != string::npos) { //go to home directory
679 string homeDir = getenv ("HOMEPATH");
680 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
683 if (path.rfind(".\\") == string::npos) { return fileName; } //already complete name
684 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
686 char *cwdpath = NULL;
687 cwdpath = getcwd(NULL, 0); // or _getcwd
688 if ( cwdpath != NULL) { cwd = cwdpath; }
691 //break apart the current working directory
693 while (cwd.find_first_of('\\') != -1) {
694 string dir = cwd.substr(0,cwd.find_first_of('\\'));
695 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
700 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
702 int index = dirs.size()-1;
704 while((pos = path.rfind(".\\")) != string::npos) { //while you don't have a complete path
705 if (pos == 0) { break; //you are at the end
706 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
707 path = path.substr(0, pos-1);
709 if (index == 0) { break; }
710 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
711 path = path.substr(0, pos);
712 }else if (pos == 1) { break; //you are at the end
713 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
716 for (int i = index; i >= 0; i--) {
717 newFileName = dirs[i] + "\\" + newFileName;
726 catch(exception& e) {
727 errorOut(e, "MothurOut", "getFullPathName");
731 /***********************************************************************/
733 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
736 string completeFileName = getFullPathName(fileName);
737 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
738 #ifdef USE_COMPRESSION
739 // check for gzipped or bzipped file
740 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
741 string tempName = string(tmpnam(0));
742 mkfifo(tempName.c_str(), 0666);
743 int fork_result = fork();
744 if (fork_result < 0) {
745 cerr << "Error forking.\n";
747 } else if (fork_result == 0) {
748 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
749 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
750 system(command.c_str());
751 cerr << "Done decompressing " << completeFileName << "\n";
752 remove(tempName.c_str());
755 cerr << "waiting on child process " << fork_result << "\n";
756 completeFileName = tempName;
761 fileHandle.open(completeFileName.c_str());
763 //mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
766 //check for blank file
771 catch(exception& e) {
772 errorOut(e, "MothurOut", "openInputFile - no Error");
776 /***********************************************************************/
778 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
782 string completeFileName = getFullPathName(fileName);
783 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
784 #ifdef USE_COMPRESSION
785 // check for gzipped or bzipped file
786 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
787 string tempName = string(tmpnam(0));
788 mkfifo(tempName.c_str(), 0666);
789 int fork_result = fork();
790 if (fork_result < 0) {
791 cerr << "Error forking.\n";
793 } else if (fork_result == 0) {
794 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
795 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
796 system(command.c_str());
797 cerr << "Done decompressing " << completeFileName << "\n";
798 remove(tempName.c_str());
801 cerr << "waiting on child process " << fork_result << "\n";
802 completeFileName = tempName;
808 fileHandle.open(completeFileName.c_str());
810 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
814 //check for blank file
816 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
821 catch(exception& e) {
822 errorOut(e, "MothurOut", "openInputFile");
826 /***********************************************************************/
828 int MothurOut::renameFile(string oldName, string newName){
831 int exist = openInputFile(newName, inTest, "");
833 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
834 if (exist == 0) { //you could open it so you want to delete it
836 string command = "rm " + newName;
837 system(command.c_str());
840 string command = "mv " + oldName + " " + newName;
841 system(command.c_str());
843 remove(newName.c_str());
844 int renameOk = rename(oldName.c_str(), newName.c_str());
849 catch(exception& e) {
850 errorOut(e, "MothurOut", "renameFile");
855 /***********************************************************************/
857 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
860 string completeFileName = getFullPathName(fileName);
861 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
862 #ifdef USE_COMPRESSION
863 // check for gzipped file
864 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
865 string tempName = string(tmpnam(0));
866 mkfifo(tempName.c_str(), 0666);
867 cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
868 int fork_result = fork();
869 if (fork_result < 0) {
870 cerr << "Error forking.\n";
872 } else if (fork_result == 0) {
873 string command = string(endsWith(completeFileName, ".gz") ? "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
874 system(command.c_str());
877 completeFileName = tempName;
882 fileHandle.open(completeFileName.c_str(), ios::trunc);
884 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
891 catch(exception& e) {
892 errorOut(e, "MothurOut", "openOutputFile");
898 /**************************************************************************************************/
899 void MothurOut::appendFiles(string temp, string filename) {
904 //open output file in append mode
905 openOutputFileAppend(filename, output);
906 int ableToOpen = openInputFile(temp, input, "no error");
907 //int ableToOpen = openInputFile(temp, input);
909 if (ableToOpen == 0) { //you opened it
910 while(char c = input.get()){
911 if(input.eof()) { break; }
912 else { output << c; }
919 catch(exception& e) {
920 errorOut(e, "MothurOut", "appendFiles");
925 /**************************************************************************************************/
926 string MothurOut::sortFile(string distFile, string outputDir){
929 //if (outputDir == "") { outputDir += hasPath(distFile); }
930 string outfile = getRootName(distFile) + "sorted.dist";
933 //if you can, use the unix sort since its been optimized for years
934 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
935 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
936 system(command.c_str());
937 #else //you are stuck with my best attempt...
938 //windows sort does not have a way to specify a column, only a character in the line
939 //since we cannot assume that the distance will always be at the the same character location on each line
940 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
942 //read in file line by file and put distance first
943 string tempDistFile = distFile + ".temp";
946 openInputFile(distFile, input);
947 openOutputFile(tempDistFile, output);
949 string firstName, secondName;
952 input >> firstName >> secondName >> dist;
953 output << dist << '\t' << firstName << '\t' << secondName << endl;
960 //sort using windows sort
961 string tempOutfile = outfile + ".temp";
962 string command = "sort " + tempDistFile + " /O " + tempOutfile;
963 system(command.c_str());
965 //read in sorted file and put distance at end again
967 openInputFile(tempOutfile, input2);
968 openOutputFile(outfile, output);
971 input2 >> dist >> firstName >> secondName;
972 output << firstName << '\t' << secondName << '\t' << dist << endl;
979 remove(tempDistFile.c_str());
980 remove(tempOutfile.c_str());
985 catch(exception& e) {
986 errorOut(e, "MothurOut", "sortFile");
990 /**************************************************************************************************/
991 vector<unsigned long int> MothurOut::setFilePosFasta(string filename, int& num) {
993 vector<unsigned long int> positions;
995 openInputFile(filename, inFASTA);
998 while(!inFASTA.eof()){
999 input = getline(inFASTA);
1000 if (input.length() != 0) {
1001 if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
1003 gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
1007 num = positions.size();
1012 //get num bytes in file
1013 pFile = fopen (filename.c_str(),"rb");
1014 if (pFile==NULL) perror ("Error opening file");
1016 fseek (pFile, 0, SEEK_END);
1021 unsigned long int size = positions[(positions.size()-1)];
1023 openInputFile(filename, in);
1028 if(in.eof()) { break; }
1033 positions.push_back(size);
1037 catch(exception& e) {
1038 errorOut(e, "MothurOut", "setFilePosFasta");
1042 /**************************************************************************************************/
1043 vector<unsigned long int> MothurOut::setFilePosEachLine(string filename, int& num) {
1045 filename = getFullPathName(filename);
1047 vector<unsigned long int> positions;
1049 openInputFile(filename, in);
1053 unsigned long int lastpos = in.tellg();
1054 input = getline(in);
1055 if (input.length() != 0) {
1056 unsigned long int pos = in.tellg();
1057 if (pos != -1) { positions.push_back(pos - input.length() - 1); }
1058 else { positions.push_back(lastpos); }
1060 gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
1064 num = positions.size();
1067 unsigned long int size;
1069 //get num bytes in file
1070 pFile = fopen (filename.c_str(),"rb");
1071 if (pFile==NULL) perror ("Error opening file");
1073 fseek (pFile, 0, SEEK_END);
1078 positions.push_back(size);
1082 catch(exception& e) {
1083 errorOut(e, "MothurOut", "setFilePosEachLine");
1087 /**************************************************************************************************/
1089 vector<unsigned long int> MothurOut::divideFile(string filename, int& proc) {
1092 vector<unsigned long int> filePos;
1093 filePos.push_back(0);
1096 unsigned long int size;
1098 filename = getFullPathName(filename);
1100 //get num bytes in file
1101 pFile = fopen (filename.c_str(),"rb");
1102 if (pFile==NULL) perror ("Error opening file");
1104 fseek (pFile, 0, SEEK_END);
1109 //estimate file breaks
1110 unsigned long int chunkSize = 0;
1111 chunkSize = size / proc;
1113 //file to small to divide by processors
1114 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1116 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1117 for (int i = 0; i < proc; i++) {
1118 unsigned long int spot = (i+1) * chunkSize;
1121 openInputFile(filename, in);
1125 unsigned long int newSpot = spot;
1128 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1131 //there was not another sequence before the end of the file
1132 unsigned long int sanityPos = in.tellg();
1134 if (sanityPos == -1) { break; }
1135 else { filePos.push_back(newSpot); }
1141 filePos.push_back(size);
1143 //sanity check filePos
1144 for (int i = 0; i < (filePos.size()-1); i++) {
1145 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1148 proc = (filePos.size() - 1);
1152 catch(exception& e) {
1153 errorOut(e, "MothurOut", "divideFile");
1157 /**************************************************************************************************/
1158 int MothurOut::divideFile(string filename, int& proc, vector<string>& files) {
1161 vector<unsigned long int> filePos = divideFile(filename, proc);
1163 for (int i = 0; i < (filePos.size()-1); i++) {
1167 openInputFile(filename, in);
1168 in.seekg(filePos[i]);
1169 unsigned long int size = filePos[(i+1)] - filePos[i];
1170 char* chunk = new char[size];
1171 in.read(chunk, size);
1175 string fileChunkName = filename + "." + toString(i) + ".tmp";
1177 openOutputFile(fileChunkName, out);
1179 out << chunk << endl;
1184 files.push_back(fileChunkName);
1189 catch(exception& e) {
1190 errorOut(e, "MothurOut", "divideFile");
1194 /***********************************************************************/
1196 bool MothurOut::isTrue(string f){
1199 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1201 if ((f == "TRUE") || (f == "T")) { return true; }
1202 else { return false; }
1204 catch(exception& e) {
1205 errorOut(e, "MothurOut", "isTrue");
1210 /***********************************************************************/
1212 float MothurOut::roundDist(float dist, int precision){
1214 return int(dist * precision + 0.5)/float(precision);
1216 catch(exception& e) {
1217 errorOut(e, "MothurOut", "roundDist");
1221 /***********************************************************************/
1223 float MothurOut::ceilDist(float dist, int precision){
1225 return int(ceil(dist * precision))/float(precision);
1227 catch(exception& e) {
1228 errorOut(e, "MothurOut", "ceilDist");
1232 /**********************************************************************************************************************/
1233 int MothurOut::readNames(string namefile, map<string, string>& nameMap) {
1238 openInputFile(namefile, in);
1241 if (control_pressed) { break; }
1243 string firstCol, secondCol;
1244 in >> firstCol >> secondCol; gobble(in);
1246 nameMap[firstCol] = secondCol;
1253 catch(exception& e) {
1254 errorOut(e, "MothurOut", "readNames");
1259 /**********************************************************************************************************************/
1260 map<string, int> MothurOut::readNames(string namefile) {
1263 map<string, int> nameMap;
1267 openInputFile(namefile, in);
1270 if (control_pressed) { break; }
1272 string firstCol, secondCol;
1273 in >> firstCol >> secondCol; gobble(in);
1275 int num = getNumNames(secondCol);
1277 nameMap[firstCol] = num;
1284 catch(exception& e) {
1285 errorOut(e, "MothurOut", "readNames");
1289 /**********************************************************************************************************************/
1290 int MothurOut::readNames(string namefile, vector<seqPriorityNode>& nameVector, map<string, string>& fastamap) {
1296 openInputFile(namefile, in);
1299 if (control_pressed) { break; }
1301 string firstCol, secondCol;
1302 in >> firstCol >> secondCol; gobble(in);
1304 int num = getNumNames(secondCol);
1306 map<string, string>::iterator it = fastamap.find(firstCol);
1307 if (it == fastamap.end()) {
1309 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
1311 seqPriorityNode temp(num, it->second, firstCol);
1312 nameVector.push_back(temp);
1320 catch(exception& e) {
1321 errorOut(e, "MothurOut", "readNames");
1326 /***********************************************************************/
1328 int MothurOut::getNumNames(string names){
1334 for(int i=0;i<names.size();i++){
1335 if(names[i] == ','){
1343 catch(exception& e) {
1344 errorOut(e, "MothurOut", "getNumNames");
1349 /**************************************************************************************************/
1351 vector<vector<double> > MothurOut::binomial(int maxOrder){
1353 vector<vector<double> > binomial(maxOrder+1);
1355 for(int i=0;i<=maxOrder;i++){
1356 binomial[i].resize(maxOrder+1);
1365 for(int i=2;i<=maxOrder;i++){
1369 for(int i=2;i<=maxOrder;i++){
1370 for(int j=1;j<=maxOrder;j++){
1371 if(i==j){ binomial[i][j]=1; }
1372 if(j>i) { binomial[i][j]=0; }
1373 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
1380 catch(exception& e) {
1381 errorOut(e, "MothurOut", "binomial");
1385 /**************************************************************************************************/
1386 unsigned int MothurOut::fromBase36(string base36){
1388 unsigned int num = 0;
1390 map<char, int> converts;
1455 while (i < base36.length()) {
1457 num = 36 * num + converts[c];
1464 catch(exception& e) {
1465 errorOut(e, "MothurOut", "fromBase36");
1469 /***********************************************************************/
1471 int MothurOut::factorial(int num){
1475 for (int i = 1; i <= num; i++) {
1481 catch(exception& e) {
1482 errorOut(e, "MothurOut", "factorial");
1486 /***********************************************************************/
1488 int MothurOut::getNumSeqs(ifstream& file){
1490 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1494 catch(exception& e) {
1495 errorOut(e, "MothurOut", "getNumSeqs");
1499 /***********************************************************************/
1500 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1505 input = getline(file);
1506 if (input.length() != 0) {
1507 if(input[0] == '>'){ numSeqs++; }
1511 catch(exception& e) {
1512 errorOut(e, "MothurOut", "getNumSeqs");
1516 /***********************************************************************/
1518 //This function parses the estimator options and puts them in a vector
1519 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1521 string individual = "";
1522 int estimLength = estim.size();
1523 for(int i=0;i<estimLength;i++){
1524 if(estim[i] == symbol){
1525 container.push_back(individual);
1529 individual += estim[i];
1532 container.push_back(individual);
1535 catch(exception& e) {
1536 errorOut(e, "MothurOut", "splitAtChar");
1541 /***********************************************************************/
1543 //This function parses the estimator options and puts them in a vector
1544 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1546 string individual = "";
1547 int estimLength = estim.size();
1548 for(int i=0;i<estimLength;i++){
1549 if(estim[i] == '-'){
1550 container.push_back(individual);
1554 individual += estim[i];
1557 container.push_back(individual);
1560 /* string individual;
1562 while (estim.find_first_of('-') != -1) {
1563 individual = estim.substr(0,estim.find_first_of('-'));
1564 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1565 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1566 container.push_back(individual);
1570 container.push_back(estim); */
1572 catch(exception& e) {
1573 errorOut(e, "MothurOut", "splitAtDash");
1578 /***********************************************************************/
1579 //This function parses the label options and puts them in a set
1580 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1582 string individual = "";
1583 int estimLength = estim.size();
1584 for(int i=0;i<estimLength;i++){
1585 if(estim[i] == '-'){
1586 container.insert(individual);
1590 individual += estim[i];
1593 container.insert(individual);
1595 // string individual;
1597 // while (estim.find_first_of('-') != -1) {
1598 // individual = estim.substr(0,estim.find_first_of('-'));
1599 // if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1600 // estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1601 // container.insert(individual);
1605 // container.insert(estim);
1608 catch(exception& e) {
1609 errorOut(e, "MothurOut", "splitAtDash");
1613 /***********************************************************************/
1614 //This function parses the line options and puts them in a set
1615 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1620 while (estim.find_first_of('-') != -1) {
1621 individual = estim.substr(0,estim.find_first_of('-'));
1622 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1623 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1624 convert(individual, lineNum); //convert the string to int
1625 container.insert(lineNum);
1629 convert(estim, lineNum); //convert the string to int
1630 container.insert(lineNum);
1632 catch(exception& e) {
1633 errorOut(e, "MothurOut", "splitAtDash");
1637 /***********************************************************************/
1638 //This function parses the a string and puts peices in a vector
1639 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1641 string individual = "";
1642 int estimLength = estim.size();
1643 for(int i=0;i<estimLength;i++){
1644 if(estim[i] == ','){
1645 container.push_back(individual);
1649 individual += estim[i];
1652 container.push_back(individual);
1657 // string individual;
1659 // while (estim.find_first_of(',') != -1) {
1660 // individual = estim.substr(0,estim.find_first_of(','));
1661 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1662 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1663 // container.push_back(individual);
1667 // container.push_back(estim);
1669 catch(exception& e) {
1670 errorOut(e, "MothurOut", "splitAtComma");
1674 /***********************************************************************/
1676 //This function splits up the various option parameters
1677 void MothurOut::splitAtComma(string& prefix, string& suffix){
1679 prefix = suffix.substr(0,suffix.find_first_of(','));
1680 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
1681 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1683 while(suffix.at(0) == ' ')
1684 suffix = suffix.substr(1, suffix.length());
1688 catch(exception& e) {
1689 errorOut(e, "MothurOut", "splitAtComma");
1693 /***********************************************************************/
1695 //This function separates the key value from the option value i.e. dist=96_...
1696 void MothurOut::splitAtEquals(string& key, string& value){
1698 if(value.find_first_of('=') != -1){
1699 key = value.substr(0,value.find_first_of('='));
1700 if ((value.find_first_of('=')+1) <= value.length()) {
1701 value = value.substr(value.find_first_of('=')+1, value.length());
1708 catch(exception& e) {
1709 errorOut(e, "MothurOut", "splitAtEquals");
1714 /**************************************************************************************************/
1716 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1718 for (int i = 0; i < Groups.size(); i++) {
1719 if (groupname == Groups[i]) { return true; }
1723 catch(exception& e) {
1724 errorOut(e, "MothurOut", "inUsersGroups");
1728 /**************************************************************************************************/
1729 //returns true if any of the strings in first vector are in second vector
1730 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1733 for (int i = 0; i < groupnames.size(); i++) {
1734 if (inUsersGroups(groupnames[i], Groups)) { return true; }
1738 catch(exception& e) {
1739 errorOut(e, "MothurOut", "inUsersGroups");
1743 /***********************************************************************/
1744 //this function determines if the user has given us labels that are smaller than the given label.
1745 //if so then it returns true so that the calling function can run the previous valid distance.
1746 //it's a "smart" distance function. It also checks for invalid labels.
1747 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1750 set<string>::iterator it;
1751 vector<float> orderFloat;
1752 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
1753 map<string, float>::iterator it2;
1755 bool smaller = false;
1757 //unique is the smallest line
1758 if (label == "unique") { return false; }
1760 if (convertTestFloat(label, labelFloat)) {
1761 convert(label, labelFloat);
1762 }else { //cant convert
1767 //go through users set and make them floats
1768 for(it = userLabels.begin(); it != userLabels.end();) {
1771 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1773 orderFloat.push_back(temp);
1774 userMap[*it] = temp;
1776 }else if (*it == "unique") {
1777 orderFloat.push_back(-1.0);
1778 userMap["unique"] = -1.0;
1781 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
1782 userLabels.erase(it++);
1787 sort(orderFloat.begin(), orderFloat.end());
1789 /*************************************************/
1790 //is this label bigger than any of the users labels
1791 /*************************************************/
1793 //loop through order until you find a label greater than label
1794 for (int i = 0; i < orderFloat.size(); i++) {
1795 if (orderFloat[i] < labelFloat) {
1797 if (orderFloat[i] == -1) {
1798 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
1799 userLabels.erase("unique");
1802 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
1804 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
1805 if (it2->second == orderFloat[i]) {
1807 //remove small labels
1808 userLabels.erase(s);
1812 if (errorOff == "") {mothurOut( s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
1814 //since they are sorted once you find a bigger one stop looking
1821 catch(exception& e) {
1822 errorOut(e, "MothurOut", "anyLabelsToProcess");
1827 /**************************************************************************************************/
1828 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1833 string line = getline(file);
1835 //before we added this check
1836 if (line[0] != '#') { good = false; }
1839 line = line.substr(1);
1841 vector<string> versionVector;
1842 splitAtChar(version, versionVector, '.');
1844 //check file version
1845 vector<string> linesVector;
1846 splitAtChar(line, linesVector, '.');
1848 if (versionVector.size() != linesVector.size()) { good = false; }
1850 for (int j = 0; j < versionVector.size(); j++) {
1852 convert(versionVector[j], num1);
1853 convert(linesVector[j], num2);
1855 //if mothurs version is newer than this files version, then we want to remake it
1856 if (num1 > num2) { good = false; break; }
1862 if (!good) { file.close(); }
1863 else { file.seekg(0); }
1867 catch(exception& e) {
1868 errorOut(e, "MothurOut", "checkReleaseVersion");
1872 /**************************************************************************************************/
1873 bool MothurOut::isContainingOnlyDigits(string input) {
1876 //are you a digit in ascii code
1877 for (int i = 0;i < input.length(); i++){
1878 if( input[i]>47 && input[i]<58){}
1879 else { return false; }
1884 catch(exception& e) {
1885 errorOut(e, "MothurOut", "isContainingOnlyDigits");
1889 /**************************************************************************************************/