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(); }
45 errorOut(e, "MothurOut", "printCurrentFiles");
49 /*********************************************************************************************/
50 bool MothurOut::hasCurrentFiles() {
52 bool hasCurrent = false;
54 if (accnosfile != "") { return true; }
55 if (columnfile != "") { return true; }
56 if (designfile != "") { return true; }
57 if (fastafile != "") { return true; }
58 if (groupfile != "") { return true; }
59 if (listfile != "") { return true; }
60 if (namefile != "") { return true; }
61 if (oligosfile != "") { return true; }
62 if (orderfile != "") { return true; }
63 if (ordergroupfile != "") { return true; }
64 if (phylipfile != "") { return true; }
65 if (qualfile != "") { return true; }
66 if (rabundfile != "") { return true; }
67 if (relabundfile != "") { return true; }
68 if (sabundfile != "") { return true; }
69 if (sfffile != "") { return true; }
70 if (sharedfile != "") { return true; }
71 if (taxonomyfile != "") { return true; }
72 if (treefile != "") { return true; }
78 errorOut(e, "MothurOut", "hasCurrentFiles");
83 /*********************************************************************************************/
84 void MothurOut::clearCurrentFiles() {
106 catch(exception& e) {
107 errorOut(e, "MothurOut", "clearCurrentFiles");
111 /*********************************************************************************************/
112 void MothurOut::setFileName(string filename) {
114 logFileName = filename;
118 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
120 if (pid == 0) { //only one process should output to screen
123 openOutputFile(filename, out);
129 catch(exception& e) {
130 errorOut(e, "MothurOut", "setFileName");
134 /*********************************************************************************************/
135 void MothurOut::setDefaultPath(string pathname) {
138 //add / to name if needed
139 string lastChar = pathname.substr(pathname.length()-1);
140 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
141 if (lastChar != "/") { pathname += "/"; }
143 if (lastChar != "\\") { pathname += "\\"; }
146 defaultPath = pathname;
149 catch(exception& e) {
150 errorOut(e, "MothurOut", "setDefaultPath");
154 /*********************************************************************************************/
155 void MothurOut::setOutputDir(string pathname) {
157 outputDir = pathname;
159 catch(exception& e) {
160 errorOut(e, "MothurOut", "setOutputDir");
164 /*********************************************************************************************/
165 void MothurOut::closeLog() {
170 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
172 if (pid == 0) { //only one process should output to screen
181 catch(exception& e) {
182 errorOut(e, "MothurOut", "closeLog");
187 /*********************************************************************************************/
188 MothurOut::~MothurOut() {
193 catch(exception& e) {
194 errorOut(e, "MothurOut", "MothurOut");
198 /*********************************************************************************************/
199 void MothurOut::mothurOut(string output) {
204 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
206 if (pid == 0) { //only one process should output to screen
216 catch(exception& e) {
217 errorOut(e, "MothurOut", "MothurOut");
221 /*********************************************************************************************/
222 void MothurOut::mothurOutEndLine() {
226 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
228 if (pid == 0) { //only one process should output to screen
238 catch(exception& e) {
239 errorOut(e, "MothurOut", "MothurOutEndLine");
243 /*********************************************************************************************/
244 void MothurOut::mothurOutJustToLog(string output) {
248 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
250 if (pid == 0) { //only one process should output to screen
259 catch(exception& e) {
260 errorOut(e, "MothurOut", "MothurOutJustToLog");
264 /*********************************************************************************************/
265 void MothurOut::errorOut(exception& e, string object, string function) {
267 //mem_usage(vm, rss);
269 mothurOut("[ERROR]: ");
270 mothurOut(toString(e.what()));
271 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.");
274 /*********************************************************************************************/
275 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
276 // process_mem_usage(double &, double &) - takes two doubles by reference,
277 // attempts to read the system-dependent data for a process' virtual memory
278 // size and resident set size, and return the results in KB.
280 // On failure, returns 0.0, 0.0
281 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
282 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
287 // 'file' stat seems to give the most reliable results
289 ifstream stat_stream("/proc/self/stat",ios_base::in);
291 // dummy vars for leading entries in stat that we don't care about
293 string pid, comm, state, ppid, pgrp, session, tty_nr;
294 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
295 string utime, stime, cutime, cstime, priority, nice;
296 string O, itrealvalue, starttime;
298 // the two fields we want
303 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
304 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
305 >> utime >> stime >> cutime >> cstime >> priority >> nice
306 >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
308 long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
309 vm_usage = vsize / 1024.0;
310 resident_set = rss * page_size_kb;
312 mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
316 /* //windows memory usage
317 // Get the list of process identifiers.
318 DWORD aProcesses[1024], cbNeeded, cProcesses;
320 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
322 // Calculate how many process identifiers were returned.
323 cProcesses = cbNeeded / sizeof(DWORD);
325 // Print the memory usage for each process
326 for (int i = 0; i < cProcesses; i++ ) {
327 DWORD processID = aProcesses[i];
329 PROCESS_MEMORY_COUNTERS pmc;
331 HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
333 // Print the process identifier.
334 printf( "\nProcess ID: %u\n", processID);
336 if (NULL != hProcess) {
338 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
339 printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
340 printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
341 printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
342 printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
343 printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
344 printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
345 printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
346 printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
347 printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
349 CloseHandle(hProcess);
359 /***********************************************************************/
360 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
362 fileName = getFullPathName(fileName);
364 fileHandle.open(fileName.c_str(), ios::app);
366 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
373 catch(exception& e) {
374 errorOut(e, "MothurOut", "openOutputFileAppend");
378 /***********************************************************************/
379 void MothurOut::gobble(istream& f){
383 while(isspace(d=f.get())) { ;}
386 catch(exception& e) {
387 errorOut(e, "MothurOut", "gobble");
391 /***********************************************************************/
392 void MothurOut::gobble(istringstream& f){
395 while(isspace(d=f.get())) {;}
398 catch(exception& e) {
399 errorOut(e, "MothurOut", "gobble");
404 /***********************************************************************/
406 string MothurOut::getline(istringstream& fileHandle) {
411 while (!fileHandle.eof()) {
413 char c = fileHandle.get();
415 //are you at the end of the line
416 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
423 catch(exception& e) {
424 errorOut(e, "MothurOut", "getline");
428 /***********************************************************************/
430 string MothurOut::getline(ifstream& fileHandle) {
435 while (!fileHandle.eof()) {
437 char c = fileHandle.get();
439 //are you at the end of the line
440 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
447 catch(exception& e) {
448 errorOut(e, "MothurOut", "getline");
452 /***********************************************************************/
454 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
455 #ifdef USE_COMPRESSION
456 inline bool endsWith(string s, const char * suffix){
457 size_t suffixLength = strlen(suffix);
458 return s.size() >= suffixLength && s.substr(s.size() - suffixLength, suffixLength).compare(suffix) == 0;
463 string MothurOut::getRootName(string longName){
466 string rootName = longName;
468 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
469 #ifdef USE_COMPRESSION
470 if (endsWith(rootName, ".gz") || endsWith(rootName, ".bz2")) {
471 int pos = rootName.find_last_of('.');
472 rootName = rootName.substr(0, pos);
473 cerr << "shortening " << longName << " to " << rootName << "\n";
477 if(rootName.find_last_of(".") != rootName.npos){
478 int pos = rootName.find_last_of('.')+1;
479 rootName = rootName.substr(0, pos);
484 catch(exception& e) {
485 errorOut(e, "MothurOut", "getRootName");
489 /***********************************************************************/
491 string MothurOut::getSimpleName(string longName){
493 string simpleName = longName;
496 found=longName.find_last_of("/\\");
498 if(found != longName.npos){
499 simpleName = longName.substr(found+1);
504 catch(exception& e) {
505 errorOut(e, "MothurOut", "getSimpleName");
510 /***********************************************************************/
512 string MothurOut::getPathName(string longName){
514 string rootPathName = longName;
516 if(longName.find_last_of("/\\") != longName.npos){
517 int pos = longName.find_last_of("/\\")+1;
518 rootPathName = longName.substr(0, pos);
523 catch(exception& e) {
524 errorOut(e, "MothurOut", "getPathName");
529 /***********************************************************************/
531 string MothurOut::hasPath(string longName){
536 found=longName.find_last_of("~/\\");
538 if(found != longName.npos){
539 path = longName.substr(0, found+1);
544 catch(exception& e) {
545 errorOut(e, "MothurOut", "hasPath");
550 /***********************************************************************/
552 string MothurOut::getExtension(string longName){
554 string extension = longName;
556 if(longName.find_last_of('.') != longName.npos){
557 int pos = longName.find_last_of('.');
558 extension = longName.substr(pos, longName.length());
563 catch(exception& e) {
564 errorOut(e, "MothurOut", "getExtension");
568 /***********************************************************************/
569 bool MothurOut::isBlank(string fileName){
572 fileName = getFullPathName(fileName);
575 fileHandle.open(fileName.c_str());
577 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
580 //check for blank file
582 if (fileHandle.eof()) { fileHandle.close(); return true; }
587 catch(exception& e) {
588 errorOut(e, "MothurOut", "isBlank");
592 /***********************************************************************/
594 string MothurOut::getFullPathName(string fileName){
597 string path = hasPath(fileName);
601 if (path == "") { return fileName; } //its a simple name
602 else { //we need to complete the pathname
603 // ex. ../../../filename
604 // cwd = /user/work/desktop
607 //get current working directory
608 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
610 if (path.find("~") != -1) { //go to home directory
613 char *homepath = NULL;
614 homepath = getenv ("HOME");
615 if ( homepath != NULL) { homeDir = homepath; }
616 else { homeDir = ""; }
618 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
621 if (path.rfind("./") == string::npos) { return fileName; } //already complete name
622 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
624 //char* cwdpath = new char[1024];
626 //cwdpath=getcwd(cwdpath,size);
629 char *cwdpath = NULL;
630 cwdpath = getcwd(NULL, 0); // or _getcwd
631 if ( cwdpath != NULL) { cwd = cwdpath; }
637 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
639 //break apart the current working directory
641 while (simpleCWD.find_first_of('/') != string::npos) {
642 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
643 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
646 //get last one // ex. ../../../filename = /user/work/desktop/filename
647 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
650 int index = dirs.size()-1;
652 while((pos = path.rfind("./")) != string::npos) { //while you don't have a complete path
653 if (pos == 0) { break; //you are at the end
654 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
655 path = path.substr(0, pos-1);
657 if (index == 0) { break; }
658 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
659 path = path.substr(0, pos);
660 }else if (pos == 1) { break; //you are at the end
661 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
664 for (int i = index; i >= 0; i--) {
665 newFileName = dirs[i] + "/" + newFileName;
668 newFileName = "/" + newFileName;
672 if (path.find("~") != string::npos) { //go to home directory
673 string homeDir = getenv ("HOMEPATH");
674 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
677 if (path.rfind(".\\") == string::npos) { return fileName; } //already complete name
678 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
680 char *cwdpath = NULL;
681 cwdpath = getcwd(NULL, 0); // or _getcwd
682 if ( cwdpath != NULL) { cwd = cwdpath; }
685 //break apart the current working directory
687 while (cwd.find_first_of('\\') != -1) {
688 string dir = cwd.substr(0,cwd.find_first_of('\\'));
689 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
694 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
696 int index = dirs.size()-1;
698 while((pos = path.rfind(".\\")) != string::npos) { //while you don't have a complete path
699 if (pos == 0) { break; //you are at the end
700 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
701 path = path.substr(0, pos-1);
703 if (index == 0) { break; }
704 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
705 path = path.substr(0, pos);
706 }else if (pos == 1) { break; //you are at the end
707 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
710 for (int i = index; i >= 0; i--) {
711 newFileName = dirs[i] + "\\" + newFileName;
720 catch(exception& e) {
721 errorOut(e, "MothurOut", "getFullPathName");
725 /***********************************************************************/
727 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
730 string completeFileName = getFullPathName(fileName);
731 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
732 #ifdef USE_COMPRESSION
733 // check for gzipped or bzipped file
734 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
735 string tempName = string(tmpnam(0));
736 mkfifo(tempName.c_str(), 0666);
737 int fork_result = fork();
738 if (fork_result < 0) {
739 cerr << "Error forking.\n";
741 } else if (fork_result == 0) {
742 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
743 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
744 system(command.c_str());
745 cerr << "Done decompressing " << completeFileName << "\n";
746 remove(tempName.c_str());
749 cerr << "waiting on child process " << fork_result << "\n";
750 completeFileName = tempName;
755 fileHandle.open(completeFileName.c_str());
757 //mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
760 //check for blank file
765 catch(exception& e) {
766 errorOut(e, "MothurOut", "openInputFile - no Error");
770 /***********************************************************************/
772 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
776 string completeFileName = getFullPathName(fileName);
777 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
778 #ifdef USE_COMPRESSION
779 // check for gzipped or bzipped file
780 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
781 string tempName = string(tmpnam(0));
782 mkfifo(tempName.c_str(), 0666);
783 int fork_result = fork();
784 if (fork_result < 0) {
785 cerr << "Error forking.\n";
787 } else if (fork_result == 0) {
788 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
789 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
790 system(command.c_str());
791 cerr << "Done decompressing " << completeFileName << "\n";
792 remove(tempName.c_str());
795 cerr << "waiting on child process " << fork_result << "\n";
796 completeFileName = tempName;
802 fileHandle.open(completeFileName.c_str());
804 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
808 //check for blank file
810 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
815 catch(exception& e) {
816 errorOut(e, "MothurOut", "openInputFile");
820 /***********************************************************************/
822 int MothurOut::renameFile(string oldName, string newName){
825 int exist = openInputFile(newName, inTest, "");
827 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
828 if (exist == 0) { //you could open it so you want to delete it
830 string command = "rm " + newName;
831 system(command.c_str());
834 string command = "mv " + oldName + " " + newName;
835 system(command.c_str());
837 remove(newName.c_str());
838 int renameOk = rename(oldName.c_str(), newName.c_str());
843 catch(exception& e) {
844 errorOut(e, "MothurOut", "renameFile");
849 /***********************************************************************/
851 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
854 string completeFileName = getFullPathName(fileName);
855 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
856 #ifdef USE_COMPRESSION
857 // check for gzipped file
858 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
859 string tempName = string(tmpnam(0));
860 mkfifo(tempName.c_str(), 0666);
861 cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
862 int fork_result = fork();
863 if (fork_result < 0) {
864 cerr << "Error forking.\n";
866 } else if (fork_result == 0) {
867 string command = string(endsWith(completeFileName, ".gz") ? "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
868 system(command.c_str());
871 completeFileName = tempName;
876 fileHandle.open(completeFileName.c_str(), ios::trunc);
878 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
885 catch(exception& e) {
886 errorOut(e, "MothurOut", "openOutputFile");
892 /**************************************************************************************************/
893 void MothurOut::appendFiles(string temp, string filename) {
898 //open output file in append mode
899 openOutputFileAppend(filename, output);
900 int ableToOpen = openInputFile(temp, input, "no error");
901 //int ableToOpen = openInputFile(temp, input);
903 if (ableToOpen == 0) { //you opened it
904 while(char c = input.get()){
905 if(input.eof()) { break; }
906 else { output << c; }
913 catch(exception& e) {
914 errorOut(e, "MothurOut", "appendFiles");
919 /**************************************************************************************************/
920 string MothurOut::sortFile(string distFile, string outputDir){
923 //if (outputDir == "") { outputDir += hasPath(distFile); }
924 string outfile = getRootName(distFile) + "sorted.dist";
927 //if you can, use the unix sort since its been optimized for years
928 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
929 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
930 system(command.c_str());
931 #else //you are stuck with my best attempt...
932 //windows sort does not have a way to specify a column, only a character in the line
933 //since we cannot assume that the distance will always be at the the same character location on each line
934 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
936 //read in file line by file and put distance first
937 string tempDistFile = distFile + ".temp";
940 openInputFile(distFile, input);
941 openOutputFile(tempDistFile, output);
943 string firstName, secondName;
946 input >> firstName >> secondName >> dist;
947 output << dist << '\t' << firstName << '\t' << secondName << endl;
954 //sort using windows sort
955 string tempOutfile = outfile + ".temp";
956 string command = "sort " + tempDistFile + " /O " + tempOutfile;
957 system(command.c_str());
959 //read in sorted file and put distance at end again
961 openInputFile(tempOutfile, input2);
962 openOutputFile(outfile, output);
965 input2 >> dist >> firstName >> secondName;
966 output << firstName << '\t' << secondName << '\t' << dist << endl;
973 remove(tempDistFile.c_str());
974 remove(tempOutfile.c_str());
979 catch(exception& e) {
980 errorOut(e, "MothurOut", "sortFile");
984 /**************************************************************************************************/
985 vector<unsigned long int> MothurOut::setFilePosFasta(string filename, int& num) {
987 vector<unsigned long int> positions;
989 openInputFile(filename, inFASTA);
992 while(!inFASTA.eof()){
993 input = getline(inFASTA);
994 if (input.length() != 0) {
995 if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
997 gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
1001 num = positions.size();
1006 //get num bytes in file
1007 pFile = fopen (filename.c_str(),"rb");
1008 if (pFile==NULL) perror ("Error opening file");
1010 fseek (pFile, 0, SEEK_END);
1015 unsigned long int size = positions[(positions.size()-1)];
1017 openInputFile(filename, in);
1022 if(in.eof()) { break; }
1027 positions.push_back(size);
1031 catch(exception& e) {
1032 errorOut(e, "MothurOut", "setFilePosFasta");
1036 /**************************************************************************************************/
1037 vector<unsigned long int> MothurOut::setFilePosEachLine(string filename, int& num) {
1039 filename = getFullPathName(filename);
1041 vector<unsigned long int> positions;
1043 openInputFile(filename, in);
1047 unsigned long int lastpos = in.tellg();
1048 input = getline(in);
1049 if (input.length() != 0) {
1050 unsigned long int pos = in.tellg();
1051 if (pos != -1) { positions.push_back(pos - input.length() - 1); }
1052 else { positions.push_back(lastpos); }
1054 gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
1058 num = positions.size();
1061 unsigned long int size;
1063 //get num bytes in file
1064 pFile = fopen (filename.c_str(),"rb");
1065 if (pFile==NULL) perror ("Error opening file");
1067 fseek (pFile, 0, SEEK_END);
1072 positions.push_back(size);
1076 catch(exception& e) {
1077 errorOut(e, "MothurOut", "setFilePosEachLine");
1081 /**************************************************************************************************/
1083 vector<unsigned long int> MothurOut::divideFile(string filename, int& proc) {
1086 vector<unsigned long int> filePos;
1087 filePos.push_back(0);
1090 unsigned long int size;
1092 filename = getFullPathName(filename);
1094 //get num bytes in file
1095 pFile = fopen (filename.c_str(),"rb");
1096 if (pFile==NULL) perror ("Error opening file");
1098 fseek (pFile, 0, SEEK_END);
1103 //estimate file breaks
1104 unsigned long int chunkSize = 0;
1105 chunkSize = size / proc;
1107 //file to small to divide by processors
1108 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1110 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1111 for (int i = 0; i < proc; i++) {
1112 unsigned long int spot = (i+1) * chunkSize;
1115 openInputFile(filename, in);
1119 unsigned long int newSpot = spot;
1122 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1125 //there was not another sequence before the end of the file
1126 unsigned long int sanityPos = in.tellg();
1128 if (sanityPos == -1) { break; }
1129 else { filePos.push_back(newSpot); }
1135 filePos.push_back(size);
1137 //sanity check filePos
1138 for (int i = 0; i < (filePos.size()-1); i++) {
1139 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1142 proc = (filePos.size() - 1);
1146 catch(exception& e) {
1147 errorOut(e, "MothurOut", "divideFile");
1152 /***********************************************************************/
1154 bool MothurOut::isTrue(string f){
1157 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1159 if ((f == "TRUE") || (f == "T")) { return true; }
1160 else { return false; }
1162 catch(exception& e) {
1163 errorOut(e, "MothurOut", "isTrue");
1168 /***********************************************************************/
1170 float MothurOut::roundDist(float dist, int precision){
1172 return int(dist * precision + 0.5)/float(precision);
1174 catch(exception& e) {
1175 errorOut(e, "MothurOut", "roundDist");
1179 /***********************************************************************/
1181 float MothurOut::ceilDist(float dist, int precision){
1183 return int(ceil(dist * precision))/float(precision);
1185 catch(exception& e) {
1186 errorOut(e, "MothurOut", "ceilDist");
1190 /**********************************************************************************************************************/
1191 map<string, int> MothurOut::readNames(string namefile) {
1194 map<string, int> nameMap;
1198 openInputFile(namefile, in);
1201 if (control_pressed) { break; }
1203 string firstCol, secondCol;
1204 in >> firstCol >> secondCol; gobble(in);
1206 int num = getNumNames(secondCol);
1208 nameMap[firstCol] = num;
1215 catch(exception& e) {
1216 errorOut(e, "MothurOut", "readNames");
1221 /***********************************************************************/
1223 int MothurOut::getNumNames(string names){
1229 for(int i=0;i<names.size();i++){
1230 if(names[i] == ','){
1238 catch(exception& e) {
1239 errorOut(e, "MothurOut", "getNumNames");
1244 /**************************************************************************************************/
1246 vector<vector<double> > MothurOut::binomial(int maxOrder){
1248 vector<vector<double> > binomial(maxOrder+1);
1250 for(int i=0;i<=maxOrder;i++){
1251 binomial[i].resize(maxOrder+1);
1260 for(int i=2;i<=maxOrder;i++){
1264 for(int i=2;i<=maxOrder;i++){
1265 for(int j=1;j<=maxOrder;j++){
1266 if(i==j){ binomial[i][j]=1; }
1267 if(j>i) { binomial[i][j]=0; }
1268 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
1275 catch(exception& e) {
1276 errorOut(e, "MothurOut", "binomial");
1280 /**************************************************************************************************/
1281 unsigned int MothurOut::fromBase36(string base36){
1283 unsigned int num = 0;
1285 map<char, int> converts;
1350 while (i < base36.length()) {
1352 num = 36 * num + converts[c];
1359 catch(exception& e) {
1360 errorOut(e, "MothurOut", "fromBase36");
1364 /***********************************************************************/
1366 int MothurOut::factorial(int num){
1370 for (int i = 1; i <= num; i++) {
1376 catch(exception& e) {
1377 errorOut(e, "MothurOut", "factorial");
1381 /***********************************************************************/
1383 int MothurOut::getNumSeqs(ifstream& file){
1385 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1389 catch(exception& e) {
1390 errorOut(e, "MothurOut", "getNumSeqs");
1394 /***********************************************************************/
1395 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1400 input = getline(file);
1401 if (input.length() != 0) {
1402 if(input[0] == '>'){ numSeqs++; }
1406 catch(exception& e) {
1407 errorOut(e, "MothurOut", "getNumSeqs");
1411 /***********************************************************************/
1413 //This function parses the estimator options and puts them in a vector
1414 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1416 string individual = "";
1417 int estimLength = estim.size();
1418 for(int i=0;i<estimLength;i++){
1419 if(estim[i] == symbol){
1420 container.push_back(individual);
1424 individual += estim[i];
1427 container.push_back(individual);
1430 catch(exception& e) {
1431 errorOut(e, "MothurOut", "splitAtChar");
1436 /***********************************************************************/
1438 //This function parses the estimator options and puts them in a vector
1439 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1441 string individual = "";
1442 int estimLength = estim.size();
1443 for(int i=0;i<estimLength;i++){
1444 if(estim[i] == '-'){
1445 container.push_back(individual);
1449 individual += estim[i];
1452 container.push_back(individual);
1455 /* string individual;
1457 while (estim.find_first_of('-') != -1) {
1458 individual = estim.substr(0,estim.find_first_of('-'));
1459 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1460 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1461 container.push_back(individual);
1465 container.push_back(estim); */
1467 catch(exception& e) {
1468 errorOut(e, "MothurOut", "splitAtDash");
1473 /***********************************************************************/
1474 //This function parses the label options and puts them in a set
1475 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1477 string individual = "";
1478 int estimLength = estim.size();
1479 for(int i=0;i<estimLength;i++){
1480 if(estim[i] == '-'){
1481 container.insert(individual);
1485 individual += estim[i];
1488 container.insert(individual);
1490 // string individual;
1492 // while (estim.find_first_of('-') != -1) {
1493 // individual = estim.substr(0,estim.find_first_of('-'));
1494 // if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1495 // estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1496 // container.insert(individual);
1500 // container.insert(estim);
1503 catch(exception& e) {
1504 errorOut(e, "MothurOut", "splitAtDash");
1508 /***********************************************************************/
1509 //This function parses the line options and puts them in a set
1510 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1515 while (estim.find_first_of('-') != -1) {
1516 individual = estim.substr(0,estim.find_first_of('-'));
1517 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1518 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1519 convert(individual, lineNum); //convert the string to int
1520 container.insert(lineNum);
1524 convert(estim, lineNum); //convert the string to int
1525 container.insert(lineNum);
1527 catch(exception& e) {
1528 errorOut(e, "MothurOut", "splitAtDash");
1532 /***********************************************************************/
1533 //This function parses the a string and puts peices in a vector
1534 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1536 string individual = "";
1537 int estimLength = estim.size();
1538 for(int i=0;i<estimLength;i++){
1539 if(estim[i] == ','){
1540 container.push_back(individual);
1544 individual += estim[i];
1547 container.push_back(individual);
1552 // string individual;
1554 // while (estim.find_first_of(',') != -1) {
1555 // individual = estim.substr(0,estim.find_first_of(','));
1556 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1557 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1558 // container.push_back(individual);
1562 // container.push_back(estim);
1564 catch(exception& e) {
1565 errorOut(e, "MothurOut", "splitAtComma");
1569 /***********************************************************************/
1571 //This function splits up the various option parameters
1572 void MothurOut::splitAtComma(string& prefix, string& suffix){
1574 prefix = suffix.substr(0,suffix.find_first_of(','));
1575 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
1576 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1578 while(suffix.at(0) == ' ')
1579 suffix = suffix.substr(1, suffix.length());
1583 catch(exception& e) {
1584 errorOut(e, "MothurOut", "splitAtComma");
1588 /***********************************************************************/
1590 //This function separates the key value from the option value i.e. dist=96_...
1591 void MothurOut::splitAtEquals(string& key, string& value){
1593 if(value.find_first_of('=') != -1){
1594 key = value.substr(0,value.find_first_of('='));
1595 if ((value.find_first_of('=')+1) <= value.length()) {
1596 value = value.substr(value.find_first_of('=')+1, value.length());
1603 catch(exception& e) {
1604 errorOut(e, "MothurOut", "splitAtEquals");
1609 /**************************************************************************************************/
1611 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1613 for (int i = 0; i < Groups.size(); i++) {
1614 if (groupname == Groups[i]) { return true; }
1618 catch(exception& e) {
1619 errorOut(e, "MothurOut", "inUsersGroups");
1623 /**************************************************************************************************/
1624 //returns true if any of the strings in first vector are in second vector
1625 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1628 for (int i = 0; i < groupnames.size(); i++) {
1629 if (inUsersGroups(groupnames[i], Groups)) { return true; }
1633 catch(exception& e) {
1634 errorOut(e, "MothurOut", "inUsersGroups");
1638 /***********************************************************************/
1639 //this function determines if the user has given us labels that are smaller than the given label.
1640 //if so then it returns true so that the calling function can run the previous valid distance.
1641 //it's a "smart" distance function. It also checks for invalid labels.
1642 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1645 set<string>::iterator it;
1646 vector<float> orderFloat;
1647 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
1648 map<string, float>::iterator it2;
1650 bool smaller = false;
1652 //unique is the smallest line
1653 if (label == "unique") { return false; }
1655 if (convertTestFloat(label, labelFloat)) {
1656 convert(label, labelFloat);
1657 }else { //cant convert
1662 //go through users set and make them floats
1663 for(it = userLabels.begin(); it != userLabels.end();) {
1666 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1668 orderFloat.push_back(temp);
1669 userMap[*it] = temp;
1671 }else if (*it == "unique") {
1672 orderFloat.push_back(-1.0);
1673 userMap["unique"] = -1.0;
1676 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
1677 userLabels.erase(it++);
1682 sort(orderFloat.begin(), orderFloat.end());
1684 /*************************************************/
1685 //is this label bigger than any of the users labels
1686 /*************************************************/
1688 //loop through order until you find a label greater than label
1689 for (int i = 0; i < orderFloat.size(); i++) {
1690 if (orderFloat[i] < labelFloat) {
1692 if (orderFloat[i] == -1) {
1693 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
1694 userLabels.erase("unique");
1697 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
1699 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
1700 if (it2->second == orderFloat[i]) {
1702 //remove small labels
1703 userLabels.erase(s);
1707 if (errorOff == "") {mothurOut( s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
1709 //since they are sorted once you find a bigger one stop looking
1716 catch(exception& e) {
1717 errorOut(e, "MothurOut", "anyLabelsToProcess");
1722 /**************************************************************************************************/
1723 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1728 string line = getline(file);
1730 //before we added this check
1731 if (line[0] != '#') { good = false; }
1734 line = line.substr(1);
1736 vector<string> versionVector;
1737 splitAtChar(version, versionVector, '.');
1739 //check file version
1740 vector<string> linesVector;
1741 splitAtChar(line, linesVector, '.');
1743 if (versionVector.size() != linesVector.size()) { good = false; }
1745 for (int j = 0; j < versionVector.size(); j++) {
1747 convert(versionVector[j], num1);
1748 convert(linesVector[j], num2);
1750 //if mothurs version is newer than this files version, then we want to remake it
1751 if (num1 > num2) { good = false; break; }
1757 if (!good) { file.close(); }
1758 else { file.seekg(0); }
1762 catch(exception& e) {
1763 errorOut(e, "MothurOut", "checkReleaseVersion");
1767 /**************************************************************************************************/
1768 bool MothurOut::isContainingOnlyDigits(string input) {
1771 //are you a digit in ascii code
1772 for (int i = 0;i < input.length(); i++){
1773 if( input[i]>47 && input[i]<58){}
1774 else { return false; }
1779 catch(exception& e) {
1780 errorOut(e, "MothurOut", "isContainingOnlyDigits");
1784 /**************************************************************************************************/