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::mothurOut(string output, ofstream& outputFile) {
255 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
257 if (pid == 0) { //only one process should output to screen
262 outputFile << output;
268 catch(exception& e) {
269 errorOut(e, "MothurOut", "MothurOut");
273 /*********************************************************************************************/
274 void MothurOut::mothurOutEndLine(ofstream& outputFile) {
278 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
280 if (pid == 0) { //only one process should output to screen
291 catch(exception& e) {
292 errorOut(e, "MothurOut", "MothurOutEndLine");
296 /*********************************************************************************************/
297 void MothurOut::mothurOutJustToLog(string output) {
301 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
303 if (pid == 0) { //only one process should output to screen
312 catch(exception& e) {
313 errorOut(e, "MothurOut", "MothurOutJustToLog");
317 /*********************************************************************************************/
318 void MothurOut::errorOut(exception& e, string object, string function) {
320 //mem_usage(vm, rss);
322 mothurOut("[ERROR]: ");
323 mothurOut(toString(e.what()));
324 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.");
327 /*********************************************************************************************/
328 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
329 // process_mem_usage(double &, double &) - takes two doubles by reference,
330 // attempts to read the system-dependent data for a process' virtual memory
331 // size and resident set size, and return the results in KB.
333 // On failure, returns 0.0, 0.0
334 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
335 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
340 // 'file' stat seems to give the most reliable results
342 ifstream stat_stream("/proc/self/stat",ios_base::in);
344 // dummy vars for leading entries in stat that we don't care about
346 string pid, comm, state, ppid, pgrp, session, tty_nr;
347 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
348 string utime, stime, cutime, cstime, priority, nice;
349 string O, itrealvalue, starttime;
351 // the two fields we want
356 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
357 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
358 >> utime >> stime >> cutime >> cstime >> priority >> nice
359 >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
361 long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
362 vm_usage = vsize / 1024.0;
363 resident_set = rss * page_size_kb;
365 mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
369 /* //windows memory usage
370 // Get the list of process identifiers.
371 DWORD aProcesses[1024], cbNeeded, cProcesses;
373 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
375 // Calculate how many process identifiers were returned.
376 cProcesses = cbNeeded / sizeof(DWORD);
378 // Print the memory usage for each process
379 for (int i = 0; i < cProcesses; i++ ) {
380 DWORD processID = aProcesses[i];
382 PROCESS_MEMORY_COUNTERS pmc;
384 HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
386 // Print the process identifier.
387 printf( "\nProcess ID: %u\n", processID);
389 if (NULL != hProcess) {
391 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
392 printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
393 printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
394 printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
395 printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
396 printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
397 printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
398 printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
399 printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
400 printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
402 CloseHandle(hProcess);
412 /***********************************************************************/
413 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
415 fileName = getFullPathName(fileName);
417 fileHandle.open(fileName.c_str(), ios::app);
419 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
426 catch(exception& e) {
427 errorOut(e, "MothurOut", "openOutputFileAppend");
431 /***********************************************************************/
432 void MothurOut::gobble(istream& f){
436 while(isspace(d=f.get())) { ;}
439 catch(exception& e) {
440 errorOut(e, "MothurOut", "gobble");
444 /***********************************************************************/
445 void MothurOut::gobble(istringstream& f){
448 while(isspace(d=f.get())) {;}
451 catch(exception& e) {
452 errorOut(e, "MothurOut", "gobble");
457 /***********************************************************************/
459 string MothurOut::getline(istringstream& fileHandle) {
464 while (!fileHandle.eof()) {
466 char c = fileHandle.get();
468 //are you at the end of the line
469 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
476 catch(exception& e) {
477 errorOut(e, "MothurOut", "getline");
481 /***********************************************************************/
483 string MothurOut::getline(ifstream& fileHandle) {
490 char c = fileHandle.get();
492 //are you at the end of the line
493 if ((c == '\n') || (c == '\r') || (c == '\f') || (c == EOF)){ break; }
500 catch(exception& e) {
501 errorOut(e, "MothurOut", "getline");
505 /***********************************************************************/
507 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
508 #ifdef USE_COMPRESSION
509 inline bool endsWith(string s, const char * suffix){
510 size_t suffixLength = strlen(suffix);
511 return s.size() >= suffixLength && s.substr(s.size() - suffixLength, suffixLength).compare(suffix) == 0;
516 string MothurOut::getRootName(string longName){
519 string rootName = longName;
521 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
522 #ifdef USE_COMPRESSION
523 if (endsWith(rootName, ".gz") || endsWith(rootName, ".bz2")) {
524 int pos = rootName.find_last_of('.');
525 rootName = rootName.substr(0, pos);
526 cerr << "shortening " << longName << " to " << rootName << "\n";
530 if(rootName.find_last_of(".") != rootName.npos){
531 int pos = rootName.find_last_of('.')+1;
532 rootName = rootName.substr(0, pos);
537 catch(exception& e) {
538 errorOut(e, "MothurOut", "getRootName");
542 /***********************************************************************/
544 string MothurOut::getSimpleName(string longName){
546 string simpleName = longName;
549 found=longName.find_last_of("/\\");
551 if(found != longName.npos){
552 simpleName = longName.substr(found+1);
557 catch(exception& e) {
558 errorOut(e, "MothurOut", "getSimpleName");
563 /***********************************************************************/
565 int MothurOut::getRandomIndex(int highest){
568 int random = (int) ((float)(highest+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
572 catch(exception& e) {
573 errorOut(e, "MothurOut", "getRandomIndex");
578 /**********************************************************************/
580 string MothurOut::getPathName(string longName){
582 string rootPathName = longName;
584 if(longName.find_last_of("/\\") != longName.npos){
585 int pos = longName.find_last_of("/\\")+1;
586 rootPathName = longName.substr(0, pos);
591 catch(exception& e) {
592 errorOut(e, "MothurOut", "getPathName");
597 /***********************************************************************/
599 string MothurOut::hasPath(string longName){
604 found=longName.find_last_of("~/\\");
606 if(found != longName.npos){
607 path = longName.substr(0, found+1);
612 catch(exception& e) {
613 errorOut(e, "MothurOut", "hasPath");
618 /***********************************************************************/
620 string MothurOut::getExtension(string longName){
622 string extension = longName;
624 if(longName.find_last_of('.') != longName.npos){
625 int pos = longName.find_last_of('.');
626 extension = longName.substr(pos, longName.length());
631 catch(exception& e) {
632 errorOut(e, "MothurOut", "getExtension");
636 /***********************************************************************/
637 bool MothurOut::isBlank(string fileName){
640 fileName = getFullPathName(fileName);
643 fileHandle.open(fileName.c_str());
645 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
648 //check for blank file
650 if (fileHandle.eof()) { fileHandle.close(); return true; }
655 catch(exception& e) {
656 errorOut(e, "MothurOut", "isBlank");
660 /***********************************************************************/
662 string MothurOut::getFullPathName(string fileName){
665 string path = hasPath(fileName);
669 if (path == "") { return fileName; } //its a simple name
670 else { //we need to complete the pathname
671 // ex. ../../../filename
672 // cwd = /user/work/desktop
675 //get current working directory
676 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
678 if (path.find("~") != -1) { //go to home directory
681 char *homepath = NULL;
682 homepath = getenv ("HOME");
683 if ( homepath != NULL) { homeDir = homepath; }
684 else { homeDir = ""; }
686 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
689 if (path.rfind("./") == string::npos) { return fileName; } //already complete name
690 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
692 //char* cwdpath = new char[1024];
694 //cwdpath=getcwd(cwdpath,size);
697 char *cwdpath = NULL;
698 cwdpath = getcwd(NULL, 0); // or _getcwd
699 if ( cwdpath != NULL) { cwd = cwdpath; }
705 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
707 //break apart the current working directory
709 while (simpleCWD.find_first_of('/') != string::npos) {
710 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
711 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
714 //get last one // ex. ../../../filename = /user/work/desktop/filename
715 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
718 int index = dirs.size()-1;
720 while((pos = path.rfind("./")) != string::npos) { //while you don't have a complete path
721 if (pos == 0) { break; //you are at the end
722 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
723 path = path.substr(0, pos-1);
725 if (index == 0) { break; }
726 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
727 path = path.substr(0, pos);
728 }else if (pos == 1) { break; //you are at the end
729 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
732 for (int i = index; i >= 0; i--) {
733 newFileName = dirs[i] + "/" + newFileName;
736 newFileName = "/" + newFileName;
740 if (path.find("~") != string::npos) { //go to home directory
741 string homeDir = getenv ("HOMEPATH");
742 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
745 if (path.rfind(".\\") == string::npos) { return fileName; } //already complete name
746 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
748 char *cwdpath = NULL;
749 cwdpath = getcwd(NULL, 0); // or _getcwd
750 if ( cwdpath != NULL) { cwd = cwdpath; }
753 //break apart the current working directory
755 while (cwd.find_first_of('\\') != -1) {
756 string dir = cwd.substr(0,cwd.find_first_of('\\'));
757 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
762 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
764 int index = dirs.size()-1;
766 while((pos = path.rfind(".\\")) != string::npos) { //while you don't have a complete path
767 if (pos == 0) { break; //you are at the end
768 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
769 path = path.substr(0, pos-1);
771 if (index == 0) { break; }
772 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
773 path = path.substr(0, pos);
774 }else if (pos == 1) { break; //you are at the end
775 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
778 for (int i = index; i >= 0; i--) {
779 newFileName = dirs[i] + "\\" + newFileName;
788 catch(exception& e) {
789 errorOut(e, "MothurOut", "getFullPathName");
793 /***********************************************************************/
795 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
798 string completeFileName = getFullPathName(fileName);
799 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
800 #ifdef USE_COMPRESSION
801 // check for gzipped or bzipped file
802 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
803 string tempName = string(tmpnam(0));
804 mkfifo(tempName.c_str(), 0666);
805 int fork_result = fork();
806 if (fork_result < 0) {
807 cerr << "Error forking.\n";
809 } else if (fork_result == 0) {
810 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
811 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
812 system(command.c_str());
813 cerr << "Done decompressing " << completeFileName << "\n";
814 mothurRemove(tempName);
817 cerr << "waiting on child process " << fork_result << "\n";
818 completeFileName = tempName;
823 fileHandle.open(completeFileName.c_str());
825 //mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
828 //check for blank file
833 catch(exception& e) {
834 errorOut(e, "MothurOut", "openInputFile - no Error");
838 /***********************************************************************/
840 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
844 string completeFileName = getFullPathName(fileName);
845 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
846 #ifdef USE_COMPRESSION
847 // check for gzipped or bzipped file
848 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
849 string tempName = string(tmpnam(0));
850 mkfifo(tempName.c_str(), 0666);
851 int fork_result = fork();
852 if (fork_result < 0) {
853 cerr << "Error forking.\n";
855 } else if (fork_result == 0) {
856 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
857 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
858 system(command.c_str());
859 cerr << "Done decompressing " << completeFileName << "\n";
860 mothurRemove(tempName);
863 cerr << "waiting on child process " << fork_result << "\n";
864 completeFileName = tempName;
870 fileHandle.open(completeFileName.c_str());
872 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
876 //check for blank file
878 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
883 catch(exception& e) {
884 errorOut(e, "MothurOut", "openInputFile");
888 /***********************************************************************/
890 int MothurOut::renameFile(string oldName, string newName){
893 int exist = openInputFile(newName, inTest, "");
895 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
896 if (exist == 0) { //you could open it so you want to delete it
898 string command = "rm " + newName;
899 system(command.c_str());
902 string command = "mv " + oldName + " " + newName;
903 system(command.c_str());
905 mothurRemove(newName);
906 int renameOk = rename(oldName.c_str(), newName.c_str());
911 catch(exception& e) {
912 errorOut(e, "MothurOut", "renameFile");
917 /***********************************************************************/
919 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
922 string completeFileName = getFullPathName(fileName);
923 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
924 #ifdef USE_COMPRESSION
925 // check for gzipped file
926 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
927 string tempName = string(tmpnam(0));
928 mkfifo(tempName.c_str(), 0666);
929 cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
930 int fork_result = fork();
931 if (fork_result < 0) {
932 cerr << "Error forking.\n";
934 } else if (fork_result == 0) {
935 string command = string(endsWith(completeFileName, ".gz") ? "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
936 system(command.c_str());
939 completeFileName = tempName;
944 fileHandle.open(completeFileName.c_str(), ios::trunc);
946 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
953 catch(exception& e) {
954 errorOut(e, "MothurOut", "openOutputFile");
960 /**************************************************************************************************/
961 int MothurOut::appendFiles(string temp, string filename) {
966 //open output file in append mode
967 openOutputFileAppend(filename, output);
968 int ableToOpen = openInputFile(temp, input, "no error");
969 //int ableToOpen = openInputFile(temp, input);
972 if (ableToOpen == 0) { //you opened it
973 while(char c = input.get()){
974 if(input.eof()) { break; }
975 else { output << c; if (c == '\n') {numLines++;} }
984 catch(exception& e) {
985 errorOut(e, "MothurOut", "appendFiles");
990 /**************************************************************************************************/
991 string MothurOut::sortFile(string distFile, string outputDir){
994 //if (outputDir == "") { outputDir += hasPath(distFile); }
995 string outfile = getRootName(distFile) + "sorted.dist";
998 //if you can, use the unix sort since its been optimized for years
999 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1000 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
1001 system(command.c_str());
1002 #else //you are stuck with my best attempt...
1003 //windows sort does not have a way to specify a column, only a character in the line
1004 //since we cannot assume that the distance will always be at the the same character location on each line
1005 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
1007 //read in file line by file and put distance first
1008 string tempDistFile = distFile + ".temp";
1011 openInputFile(distFile, input);
1012 openOutputFile(tempDistFile, output);
1014 string firstName, secondName;
1017 input >> firstName >> secondName >> dist;
1018 output << dist << '\t' << firstName << '\t' << secondName << endl;
1025 //sort using windows sort
1026 string tempOutfile = outfile + ".temp";
1027 string command = "sort " + tempDistFile + " /O " + tempOutfile;
1028 system(command.c_str());
1030 //read in sorted file and put distance at end again
1032 openInputFile(tempOutfile, input2);
1033 openOutputFile(outfile, output);
1036 input2 >> dist >> firstName >> secondName;
1037 output << firstName << '\t' << secondName << '\t' << dist << endl;
1044 mothurRemove(tempDistFile);
1045 mothurRemove(tempOutfile);
1050 catch(exception& e) {
1051 errorOut(e, "MothurOut", "sortFile");
1055 /**************************************************************************************************/
1056 vector<unsigned long long> MothurOut::setFilePosFasta(string filename, int& num) {
1058 vector<unsigned long long> positions;
1060 //openInputFile(filename, inFASTA);
1061 inFASTA.open(filename.c_str(), ios::binary);
1064 unsigned long long count = 0;
1065 while(!inFASTA.eof()){
1066 //input = getline(inFASTA);
1067 //cout << input << '\t' << inFASTA.tellg() << endl;
1068 //if (input.length() != 0) {
1069 // if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); cout << (pos - input.length() - 1) << endl; }
1071 //gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
1072 char c = inFASTA.get(); count++;
1074 positions.push_back(count-1);
1075 //cout << count << endl;
1080 num = positions.size();
1085 //get num bytes in file
1086 pFile = fopen (filename.c_str(),"rb");
1087 if (pFile==NULL) perror ("Error opening file");
1089 fseek (pFile, 0, SEEK_END);
1094 unsigned long long size = positions[(positions.size()-1)];
1096 openInputFile(filename, in);
1101 if(in.eof()) { break; }
1106 positions.push_back(size);
1111 catch(exception& e) {
1112 errorOut(e, "MothurOut", "setFilePosFasta");
1116 /**************************************************************************************************/
1117 vector<unsigned long long> MothurOut::setFilePosEachLine(string filename, int& num) {
1119 filename = getFullPathName(filename);
1121 vector<unsigned long long> positions;
1123 //openInputFile(filename, in);
1124 in.open(filename.c_str(), ios::binary);
1127 unsigned long long count = 0;
1128 positions.push_back(0);
1131 //unsigned long long lastpos = in.tellg();
1132 //input = getline(in);
1133 //if (input.length() != 0) {
1134 //unsigned long long pos = in.tellg();
1135 //if (pos != -1) { positions.push_back(pos - input.length() - 1); }
1136 //else { positions.push_back(lastpos); }
1138 //gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
1141 //getline counting reads
1142 char d = in.get(); count++;
1143 while ((d != '\n') && (d != '\r') && (d != '\f') && (d != in.eof())) {
1144 //get next character
1150 d=in.get(); count++;
1151 while(isspace(d) && (d != in.eof())) { d=in.get(); count++;}
1153 positions.push_back(count-1);
1154 cout << count-1 << endl;
1158 num = positions.size()-1;
1161 unsigned long long size;
1163 //get num bytes in file
1164 pFile = fopen (filename.c_str(),"rb");
1165 if (pFile==NULL) perror ("Error opening file");
1167 fseek (pFile, 0, SEEK_END);
1172 positions[(positions.size()-1)] = size;
1176 catch(exception& e) {
1177 errorOut(e, "MothurOut", "setFilePosEachLine");
1181 /**************************************************************************************************/
1183 vector<unsigned long long> MothurOut::divideFile(string filename, int& proc) {
1186 vector<unsigned long long> filePos;
1187 filePos.push_back(0);
1190 unsigned long long size;
1192 filename = getFullPathName(filename);
1194 //get num bytes in file
1195 pFile = fopen (filename.c_str(),"rb");
1196 if (pFile==NULL) perror ("Error opening file");
1198 fseek (pFile, 0, SEEK_END);
1203 //estimate file breaks
1204 unsigned long long chunkSize = 0;
1205 chunkSize = size / proc;
1207 //file to small to divide by processors
1208 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1210 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1211 for (int i = 0; i < proc; i++) {
1212 unsigned long long spot = (i+1) * chunkSize;
1215 openInputFile(filename, in);
1219 unsigned long long newSpot = spot;
1222 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1225 //there was not another sequence before the end of the file
1226 unsigned long long sanityPos = in.tellg();
1228 if (sanityPos == -1) { break; }
1229 else { filePos.push_back(newSpot); }
1235 filePos.push_back(size);
1237 //sanity check filePos
1238 for (int i = 0; i < (filePos.size()-1); i++) {
1239 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1242 proc = (filePos.size() - 1);
1246 catch(exception& e) {
1247 errorOut(e, "MothurOut", "divideFile");
1251 /**************************************************************************************************/
1252 int MothurOut::divideFile(string filename, int& proc, vector<string>& files) {
1255 vector<unsigned long long> filePos = divideFile(filename, proc);
1257 for (int i = 0; i < (filePos.size()-1); i++) {
1261 openInputFile(filename, in);
1262 in.seekg(filePos[i]);
1263 unsigned long long size = filePos[(i+1)] - filePos[i];
1264 char* chunk = new char[size];
1265 in.read(chunk, size);
1269 string fileChunkName = filename + "." + toString(i) + ".tmp";
1271 openOutputFile(fileChunkName, out);
1273 out << chunk << endl;
1278 files.push_back(fileChunkName);
1283 catch(exception& e) {
1284 errorOut(e, "MothurOut", "divideFile");
1288 /***********************************************************************/
1290 bool MothurOut::isTrue(string f){
1293 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1295 if ((f == "TRUE") || (f == "T")) { return true; }
1296 else { return false; }
1298 catch(exception& e) {
1299 errorOut(e, "MothurOut", "isTrue");
1304 /***********************************************************************/
1306 float MothurOut::roundDist(float dist, int precision){
1308 return int(dist * precision + 0.5)/float(precision);
1310 catch(exception& e) {
1311 errorOut(e, "MothurOut", "roundDist");
1315 /***********************************************************************/
1317 float MothurOut::ceilDist(float dist, int precision){
1319 return int(ceil(dist * precision))/float(precision);
1321 catch(exception& e) {
1322 errorOut(e, "MothurOut", "ceilDist");
1326 /**********************************************************************************************************************/
1327 int MothurOut::readNames(string namefile, map<string, string>& nameMap) {
1332 openInputFile(namefile, in);
1335 if (control_pressed) { break; }
1337 string firstCol, secondCol;
1338 in >> firstCol >> secondCol; gobble(in);
1340 nameMap[firstCol] = secondCol;
1347 catch(exception& e) {
1348 errorOut(e, "MothurOut", "readNames");
1353 /**********************************************************************************************************************/
1354 map<string, int> MothurOut::readNames(string namefile) {
1357 map<string, int> nameMap;
1361 openInputFile(namefile, in);
1364 if (control_pressed) { break; }
1366 string firstCol, secondCol;
1367 in >> firstCol >> secondCol; gobble(in);
1369 int num = getNumNames(secondCol);
1371 nameMap[firstCol] = num;
1378 catch(exception& e) {
1379 errorOut(e, "MothurOut", "readNames");
1383 /**********************************************************************************************************************/
1384 int MothurOut::readNames(string namefile, vector<seqPriorityNode>& nameVector, map<string, string>& fastamap) {
1390 openInputFile(namefile, in);
1393 if (control_pressed) { break; }
1395 string firstCol, secondCol;
1396 in >> firstCol >> secondCol; gobble(in);
1398 int num = getNumNames(secondCol);
1400 map<string, string>::iterator it = fastamap.find(firstCol);
1401 if (it == fastamap.end()) {
1403 mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
1405 seqPriorityNode temp(num, it->second, firstCol);
1406 nameVector.push_back(temp);
1414 catch(exception& e) {
1415 errorOut(e, "MothurOut", "readNames");
1420 /***********************************************************************/
1422 int MothurOut::getNumNames(string names){
1428 for(int i=0;i<names.size();i++){
1429 if(names[i] == ','){
1437 catch(exception& e) {
1438 errorOut(e, "MothurOut", "getNumNames");
1442 /***********************************************************************/
1444 void MothurOut::mothurRemove(string filename){
1446 filename = getFullPathName(filename);
1447 remove(filename.c_str());
1449 catch(exception& e) {
1450 errorOut(e, "MothurOut", "mothurRemove");
1454 /**************************************************************************************************/
1456 vector<vector<double> > MothurOut::binomial(int maxOrder){
1458 vector<vector<double> > binomial(maxOrder+1);
1460 for(int i=0;i<=maxOrder;i++){
1461 binomial[i].resize(maxOrder+1);
1470 for(int i=2;i<=maxOrder;i++){
1474 for(int i=2;i<=maxOrder;i++){
1475 for(int j=1;j<=maxOrder;j++){
1476 if(i==j){ binomial[i][j]=1; }
1477 if(j>i) { binomial[i][j]=0; }
1478 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
1485 catch(exception& e) {
1486 errorOut(e, "MothurOut", "binomial");
1490 /**************************************************************************************************/
1491 unsigned int MothurOut::fromBase36(string base36){
1493 unsigned int num = 0;
1495 map<char, int> converts;
1560 while (i < base36.length()) {
1562 num = 36 * num + converts[c];
1569 catch(exception& e) {
1570 errorOut(e, "MothurOut", "fromBase36");
1574 /***********************************************************************/
1576 int MothurOut::factorial(int num){
1580 for (int i = 1; i <= num; i++) {
1586 catch(exception& e) {
1587 errorOut(e, "MothurOut", "factorial");
1591 /***********************************************************************/
1593 int MothurOut::getNumSeqs(ifstream& file){
1595 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1599 catch(exception& e) {
1600 errorOut(e, "MothurOut", "getNumSeqs");
1604 /***********************************************************************/
1605 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1610 input = getline(file);
1611 if (input.length() != 0) {
1612 if(input[0] == '>'){ numSeqs++; }
1616 catch(exception& e) {
1617 errorOut(e, "MothurOut", "getNumSeqs");
1621 /***********************************************************************/
1623 //This function parses the estimator options and puts them in a vector
1624 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1626 string individual = "";
1627 int estimLength = estim.size();
1628 for(int i=0;i<estimLength;i++){
1629 if(estim[i] == symbol){
1630 container.push_back(individual);
1634 individual += estim[i];
1637 container.push_back(individual);
1640 catch(exception& e) {
1641 errorOut(e, "MothurOut", "splitAtChar");
1646 /***********************************************************************/
1648 //This function parses the estimator options and puts them in a vector
1649 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1651 string individual = "";
1652 int estimLength = estim.size();
1653 for(int i=0;i<estimLength;i++){
1654 if(estim[i] == '-'){
1655 container.push_back(individual);
1659 individual += estim[i];
1662 container.push_back(individual);
1665 /* string individual;
1667 while (estim.find_first_of('-') != -1) {
1668 individual = estim.substr(0,estim.find_first_of('-'));
1669 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1670 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1671 container.push_back(individual);
1675 container.push_back(estim); */
1677 catch(exception& e) {
1678 errorOut(e, "MothurOut", "splitAtDash");
1683 /***********************************************************************/
1684 //This function parses the label options and puts them in a set
1685 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1687 string individual = "";
1688 int estimLength = estim.size();
1689 for(int i=0;i<estimLength;i++){
1690 if(estim[i] == '-'){
1691 container.insert(individual);
1695 individual += estim[i];
1698 container.insert(individual);
1700 // string individual;
1702 // while (estim.find_first_of('-') != -1) {
1703 // individual = estim.substr(0,estim.find_first_of('-'));
1704 // if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1705 // estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1706 // container.insert(individual);
1710 // container.insert(estim);
1713 catch(exception& e) {
1714 errorOut(e, "MothurOut", "splitAtDash");
1718 /***********************************************************************/
1719 //This function parses the line options and puts them in a set
1720 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1725 while (estim.find_first_of('-') != -1) {
1726 individual = estim.substr(0,estim.find_first_of('-'));
1727 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1728 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1729 convert(individual, lineNum); //convert the string to int
1730 container.insert(lineNum);
1734 convert(estim, lineNum); //convert the string to int
1735 container.insert(lineNum);
1737 catch(exception& e) {
1738 errorOut(e, "MothurOut", "splitAtDash");
1742 /***********************************************************************/
1743 //This function parses the a string and puts peices in a vector
1744 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1746 string individual = "";
1747 int estimLength = estim.size();
1748 for(int i=0;i<estimLength;i++){
1749 if(estim[i] == ','){
1750 container.push_back(individual);
1754 individual += estim[i];
1757 container.push_back(individual);
1762 // string individual;
1764 // while (estim.find_first_of(',') != -1) {
1765 // individual = estim.substr(0,estim.find_first_of(','));
1766 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1767 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1768 // container.push_back(individual);
1772 // container.push_back(estim);
1774 catch(exception& e) {
1775 errorOut(e, "MothurOut", "splitAtComma");
1779 /***********************************************************************/
1781 //This function splits up the various option parameters
1782 void MothurOut::splitAtComma(string& prefix, string& suffix){
1784 prefix = suffix.substr(0,suffix.find_first_of(','));
1785 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
1786 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1788 while(suffix.at(0) == ' ')
1789 suffix = suffix.substr(1, suffix.length());
1793 catch(exception& e) {
1794 errorOut(e, "MothurOut", "splitAtComma");
1798 /***********************************************************************/
1800 //This function separates the key value from the option value i.e. dist=96_...
1801 void MothurOut::splitAtEquals(string& key, string& value){
1803 if(value.find_first_of('=') != -1){
1804 key = value.substr(0,value.find_first_of('='));
1805 if ((value.find_first_of('=')+1) <= value.length()) {
1806 value = value.substr(value.find_first_of('=')+1, value.length());
1813 catch(exception& e) {
1814 errorOut(e, "MothurOut", "splitAtEquals");
1819 /**************************************************************************************************/
1821 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1823 for (int i = 0; i < Groups.size(); i++) {
1824 if (groupname == Groups[i]) { return true; }
1828 catch(exception& e) {
1829 errorOut(e, "MothurOut", "inUsersGroups");
1833 /**************************************************************************************************/
1834 //returns true if any of the strings in first vector are in second vector
1835 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1838 for (int i = 0; i < groupnames.size(); i++) {
1839 if (inUsersGroups(groupnames[i], Groups)) { return true; }
1843 catch(exception& e) {
1844 errorOut(e, "MothurOut", "inUsersGroups");
1848 /***********************************************************************/
1849 //this function determines if the user has given us labels that are smaller than the given label.
1850 //if so then it returns true so that the calling function can run the previous valid distance.
1851 //it's a "smart" distance function. It also checks for invalid labels.
1852 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1855 set<string>::iterator it;
1856 vector<float> orderFloat;
1857 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
1858 map<string, float>::iterator it2;
1860 bool smaller = false;
1862 //unique is the smallest line
1863 if (label == "unique") { return false; }
1865 if (convertTestFloat(label, labelFloat)) {
1866 convert(label, labelFloat);
1867 }else { //cant convert
1872 //go through users set and make them floats
1873 for(it = userLabels.begin(); it != userLabels.end();) {
1876 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1878 orderFloat.push_back(temp);
1879 userMap[*it] = temp;
1881 }else if (*it == "unique") {
1882 orderFloat.push_back(-1.0);
1883 userMap["unique"] = -1.0;
1886 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
1887 userLabels.erase(it++);
1892 sort(orderFloat.begin(), orderFloat.end());
1894 /*************************************************/
1895 //is this label bigger than any of the users labels
1896 /*************************************************/
1898 //loop through order until you find a label greater than label
1899 for (int i = 0; i < orderFloat.size(); i++) {
1900 if (orderFloat[i] < labelFloat) {
1902 if (orderFloat[i] == -1) {
1903 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
1904 userLabels.erase("unique");
1907 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
1909 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
1910 if (it2->second == orderFloat[i]) {
1912 //remove small labels
1913 userLabels.erase(s);
1917 if (errorOff == "") {mothurOut( s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
1919 //since they are sorted once you find a bigger one stop looking
1926 catch(exception& e) {
1927 errorOut(e, "MothurOut", "anyLabelsToProcess");
1932 /**************************************************************************************************/
1933 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1938 string line = getline(file);
1940 //before we added this check
1941 if (line[0] != '#') { good = false; }
1944 line = line.substr(1);
1946 vector<string> versionVector;
1947 splitAtChar(version, versionVector, '.');
1949 //check file version
1950 vector<string> linesVector;
1951 splitAtChar(line, linesVector, '.');
1953 if (versionVector.size() != linesVector.size()) { good = false; }
1955 for (int j = 0; j < versionVector.size(); j++) {
1957 convert(versionVector[j], num1);
1958 convert(linesVector[j], num2);
1960 //if mothurs version is newer than this files version, then we want to remake it
1961 if (num1 > num2) { good = false; break; }
1967 if (!good) { file.close(); }
1968 else { file.seekg(0); }
1972 catch(exception& e) {
1973 errorOut(e, "MothurOut", "checkReleaseVersion");
1977 /**************************************************************************************************/
1978 bool MothurOut::isContainingOnlyDigits(string input) {
1981 //are you a digit in ascii code
1982 for (int i = 0;i < input.length(); i++){
1983 if( input[i]>47 && input[i]<58){}
1984 else { return false; }
1989 catch(exception& e) {
1990 errorOut(e, "MothurOut", "isContainingOnlyDigits");
1994 /**************************************************************************************************/