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 void MothurOut::clearCurrentFiles() {
73 errorOut(e, "MothurOut", "clearCurrentFiles");
77 /*********************************************************************************************/
78 void MothurOut::setFileName(string filename) {
80 logFileName = filename;
84 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
86 if (pid == 0) { //only one process should output to screen
89 openOutputFile(filename, out);
96 errorOut(e, "MothurOut", "setFileName");
100 /*********************************************************************************************/
101 void MothurOut::setDefaultPath(string pathname) {
104 //add / to name if needed
105 string lastChar = pathname.substr(pathname.length()-1);
106 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
107 if (lastChar != "/") { pathname += "/"; }
109 if (lastChar != "\\") { pathname += "\\"; }
112 defaultPath = pathname;
115 catch(exception& e) {
116 errorOut(e, "MothurOut", "setDefaultPath");
120 /*********************************************************************************************/
121 void MothurOut::setOutputDir(string pathname) {
123 outputDir = pathname;
125 catch(exception& e) {
126 errorOut(e, "MothurOut", "setOutputDir");
130 /*********************************************************************************************/
131 void MothurOut::closeLog() {
136 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
138 if (pid == 0) { //only one process should output to screen
147 catch(exception& e) {
148 errorOut(e, "MothurOut", "closeLog");
153 /*********************************************************************************************/
154 MothurOut::~MothurOut() {
159 catch(exception& e) {
160 errorOut(e, "MothurOut", "MothurOut");
164 /*********************************************************************************************/
165 void MothurOut::mothurOut(string output) {
170 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
172 if (pid == 0) { //only one process should output to screen
182 catch(exception& e) {
183 errorOut(e, "MothurOut", "MothurOut");
187 /*********************************************************************************************/
188 void MothurOut::mothurOutEndLine() {
192 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
194 if (pid == 0) { //only one process should output to screen
204 catch(exception& e) {
205 errorOut(e, "MothurOut", "MothurOutEndLine");
209 /*********************************************************************************************/
210 void MothurOut::mothurOutJustToLog(string output) {
214 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
216 if (pid == 0) { //only one process should output to screen
225 catch(exception& e) {
226 errorOut(e, "MothurOut", "MothurOutJustToLog");
230 /*********************************************************************************************/
231 void MothurOut::errorOut(exception& e, string object, string function) {
233 //mem_usage(vm, rss);
235 mothurOut("[ERROR]: ");
236 mothurOut(toString(e.what()));
237 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.");
240 /*********************************************************************************************/
241 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
242 // process_mem_usage(double &, double &) - takes two doubles by reference,
243 // attempts to read the system-dependent data for a process' virtual memory
244 // size and resident set size, and return the results in KB.
246 // On failure, returns 0.0, 0.0
247 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
248 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
253 // 'file' stat seems to give the most reliable results
255 ifstream stat_stream("/proc/self/stat",ios_base::in);
257 // dummy vars for leading entries in stat that we don't care about
259 string pid, comm, state, ppid, pgrp, session, tty_nr;
260 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
261 string utime, stime, cutime, cstime, priority, nice;
262 string O, itrealvalue, starttime;
264 // the two fields we want
269 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
270 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
271 >> utime >> stime >> cutime >> cstime >> priority >> nice
272 >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
274 long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
275 vm_usage = vsize / 1024.0;
276 resident_set = rss * page_size_kb;
278 mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
282 /* //windows memory usage
283 // Get the list of process identifiers.
284 DWORD aProcesses[1024], cbNeeded, cProcesses;
286 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
288 // Calculate how many process identifiers were returned.
289 cProcesses = cbNeeded / sizeof(DWORD);
291 // Print the memory usage for each process
292 for (int i = 0; i < cProcesses; i++ ) {
293 DWORD processID = aProcesses[i];
295 PROCESS_MEMORY_COUNTERS pmc;
297 HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
299 // Print the process identifier.
300 printf( "\nProcess ID: %u\n", processID);
302 if (NULL != hProcess) {
304 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
305 printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
306 printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
307 printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
308 printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
309 printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
310 printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
311 printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
312 printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
313 printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
315 CloseHandle(hProcess);
325 /***********************************************************************/
326 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
328 fileName = getFullPathName(fileName);
330 fileHandle.open(fileName.c_str(), ios::app);
332 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
339 catch(exception& e) {
340 errorOut(e, "MothurOut", "openOutputFileAppend");
344 /***********************************************************************/
345 void MothurOut::gobble(istream& f){
349 while(isspace(d=f.get())) { ;}
352 catch(exception& e) {
353 errorOut(e, "MothurOut", "gobble");
357 /***********************************************************************/
358 void MothurOut::gobble(istringstream& f){
361 while(isspace(d=f.get())) {;}
364 catch(exception& e) {
365 errorOut(e, "MothurOut", "gobble");
370 /***********************************************************************/
372 string MothurOut::getline(istringstream& fileHandle) {
377 while (!fileHandle.eof()) {
379 char c = fileHandle.get();
381 //are you at the end of the line
382 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
389 catch(exception& e) {
390 errorOut(e, "MothurOut", "getline");
394 /***********************************************************************/
396 string MothurOut::getline(ifstream& fileHandle) {
401 while (!fileHandle.eof()) {
403 char c = fileHandle.get();
405 //are you at the end of the line
406 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
413 catch(exception& e) {
414 errorOut(e, "MothurOut", "getline");
418 /***********************************************************************/
420 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
421 #ifdef USE_COMPRESSION
422 inline bool endsWith(string s, const char * suffix){
423 size_t suffixLength = strlen(suffix);
424 return s.size() >= suffixLength && s.substr(s.size() - suffixLength, suffixLength).compare(suffix) == 0;
429 string MothurOut::getRootName(string longName){
432 string rootName = longName;
434 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
435 #ifdef USE_COMPRESSION
436 if (endsWith(rootName, ".gz") || endsWith(rootName, ".bz2")) {
437 int pos = rootName.find_last_of('.');
438 rootName = rootName.substr(0, pos);
439 cerr << "shortening " << longName << " to " << rootName << "\n";
443 if(rootName.find_last_of(".") != rootName.npos){
444 int pos = rootName.find_last_of('.')+1;
445 rootName = rootName.substr(0, pos);
450 catch(exception& e) {
451 errorOut(e, "MothurOut", "getRootName");
455 /***********************************************************************/
457 string MothurOut::getSimpleName(string longName){
459 string simpleName = longName;
462 found=longName.find_last_of("/\\");
464 if(found != longName.npos){
465 simpleName = longName.substr(found+1);
470 catch(exception& e) {
471 errorOut(e, "MothurOut", "getSimpleName");
476 /***********************************************************************/
478 string MothurOut::getPathName(string longName){
480 string rootPathName = longName;
482 if(longName.find_last_of("/\\") != longName.npos){
483 int pos = longName.find_last_of("/\\")+1;
484 rootPathName = longName.substr(0, pos);
489 catch(exception& e) {
490 errorOut(e, "MothurOut", "getPathName");
495 /***********************************************************************/
497 string MothurOut::hasPath(string longName){
502 found=longName.find_last_of("~/\\");
504 if(found != longName.npos){
505 path = longName.substr(0, found+1);
510 catch(exception& e) {
511 errorOut(e, "MothurOut", "hasPath");
516 /***********************************************************************/
518 string MothurOut::getExtension(string longName){
520 string extension = longName;
522 if(longName.find_last_of('.') != longName.npos){
523 int pos = longName.find_last_of('.');
524 extension = longName.substr(pos, longName.length());
529 catch(exception& e) {
530 errorOut(e, "MothurOut", "getExtension");
534 /***********************************************************************/
535 bool MothurOut::isBlank(string fileName){
538 fileName = getFullPathName(fileName);
541 fileHandle.open(fileName.c_str());
543 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
546 //check for blank file
548 if (fileHandle.eof()) { fileHandle.close(); return true; }
553 catch(exception& e) {
554 errorOut(e, "MothurOut", "isBlank");
558 /***********************************************************************/
560 string MothurOut::getFullPathName(string fileName){
563 string path = hasPath(fileName);
567 if (path == "") { return fileName; } //its a simple name
568 else { //we need to complete the pathname
569 // ex. ../../../filename
570 // cwd = /user/work/desktop
573 //get current working directory
574 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
576 if (path.find("~") != -1) { //go to home directory
579 char *homepath = NULL;
580 homepath = getenv ("HOME");
581 if ( homepath != NULL) { homeDir = homepath; }
582 else { homeDir = ""; }
584 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
587 if (path.rfind("./") == string::npos) { return fileName; } //already complete name
588 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
590 //char* cwdpath = new char[1024];
592 //cwdpath=getcwd(cwdpath,size);
595 char *cwdpath = NULL;
596 cwdpath = getcwd(NULL, 0); // or _getcwd
597 if ( cwdpath != NULL) { cwd = cwdpath; }
603 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
605 //break apart the current working directory
607 while (simpleCWD.find_first_of('/') != string::npos) {
608 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
609 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
612 //get last one // ex. ../../../filename = /user/work/desktop/filename
613 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
616 int index = dirs.size()-1;
618 while((pos = path.rfind("./")) != string::npos) { //while you don't have a complete path
619 if (pos == 0) { break; //you are at the end
620 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
621 path = path.substr(0, pos-1);
623 if (index == 0) { break; }
624 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
625 path = path.substr(0, pos);
626 }else if (pos == 1) { break; //you are at the end
627 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
630 for (int i = index; i >= 0; i--) {
631 newFileName = dirs[i] + "/" + newFileName;
634 newFileName = "/" + newFileName;
638 if (path.find("~") != string::npos) { //go to home directory
639 string homeDir = getenv ("HOMEPATH");
640 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
643 if (path.rfind(".\\") == string::npos) { return fileName; } //already complete name
644 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
646 char *cwdpath = NULL;
647 cwdpath = getcwd(NULL, 0); // or _getcwd
648 if ( cwdpath != NULL) { cwd = cwdpath; }
651 //break apart the current working directory
653 while (cwd.find_first_of('\\') != -1) {
654 string dir = cwd.substr(0,cwd.find_first_of('\\'));
655 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
660 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
662 int index = dirs.size()-1;
664 while((pos = path.rfind(".\\")) != string::npos) { //while you don't have a complete path
665 if (pos == 0) { break; //you are at the end
666 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
667 path = path.substr(0, pos-1);
669 if (index == 0) { break; }
670 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
671 path = path.substr(0, pos);
672 }else if (pos == 1) { break; //you are at the end
673 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
676 for (int i = index; i >= 0; i--) {
677 newFileName = dirs[i] + "\\" + newFileName;
686 catch(exception& e) {
687 errorOut(e, "MothurOut", "getFullPathName");
691 /***********************************************************************/
693 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
696 string completeFileName = getFullPathName(fileName);
697 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
698 #ifdef USE_COMPRESSION
699 // check for gzipped or bzipped file
700 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
701 string tempName = string(tmpnam(0));
702 mkfifo(tempName.c_str(), 0666);
703 int fork_result = fork();
704 if (fork_result < 0) {
705 cerr << "Error forking.\n";
707 } else if (fork_result == 0) {
708 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
709 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
710 system(command.c_str());
711 cerr << "Done decompressing " << completeFileName << "\n";
712 remove(tempName.c_str());
715 cerr << "waiting on child process " << fork_result << "\n";
716 completeFileName = tempName;
721 fileHandle.open(completeFileName.c_str());
723 //mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
726 //check for blank file
731 catch(exception& e) {
732 errorOut(e, "MothurOut", "openInputFile - no Error");
736 /***********************************************************************/
738 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
742 string completeFileName = getFullPathName(fileName);
743 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
744 #ifdef USE_COMPRESSION
745 // check for gzipped or bzipped file
746 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
747 string tempName = string(tmpnam(0));
748 mkfifo(tempName.c_str(), 0666);
749 int fork_result = fork();
750 if (fork_result < 0) {
751 cerr << "Error forking.\n";
753 } else if (fork_result == 0) {
754 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
755 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
756 system(command.c_str());
757 cerr << "Done decompressing " << completeFileName << "\n";
758 remove(tempName.c_str());
761 cerr << "waiting on child process " << fork_result << "\n";
762 completeFileName = tempName;
768 fileHandle.open(completeFileName.c_str());
770 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
774 //check for blank file
776 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
781 catch(exception& e) {
782 errorOut(e, "MothurOut", "openInputFile");
786 /***********************************************************************/
788 int MothurOut::renameFile(string oldName, string newName){
791 int exist = openInputFile(newName, inTest, "");
793 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
794 if (exist == 0) { //you could open it so you want to delete it
796 string command = "rm " + newName;
797 system(command.c_str());
800 string command = "mv " + oldName + " " + newName;
801 system(command.c_str());
803 remove(newName.c_str());
804 int renameOk = rename(oldName.c_str(), newName.c_str());
809 catch(exception& e) {
810 errorOut(e, "MothurOut", "renameFile");
815 /***********************************************************************/
817 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
820 string completeFileName = getFullPathName(fileName);
821 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
822 #ifdef USE_COMPRESSION
823 // check for gzipped file
824 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
825 string tempName = string(tmpnam(0));
826 mkfifo(tempName.c_str(), 0666);
827 cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
828 int fork_result = fork();
829 if (fork_result < 0) {
830 cerr << "Error forking.\n";
832 } else if (fork_result == 0) {
833 string command = string(endsWith(completeFileName, ".gz") ? "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
834 system(command.c_str());
837 completeFileName = tempName;
842 fileHandle.open(completeFileName.c_str(), ios::trunc);
844 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
851 catch(exception& e) {
852 errorOut(e, "MothurOut", "openOutputFile");
858 /**************************************************************************************************/
859 void MothurOut::appendFiles(string temp, string filename) {
864 //open output file in append mode
865 openOutputFileAppend(filename, output);
866 int ableToOpen = openInputFile(temp, input, "no error");
867 //int ableToOpen = openInputFile(temp, input);
869 if (ableToOpen == 0) { //you opened it
870 while(char c = input.get()){
871 if(input.eof()) { break; }
872 else { output << c; }
879 catch(exception& e) {
880 errorOut(e, "MothurOut", "appendFiles");
885 /**************************************************************************************************/
886 string MothurOut::sortFile(string distFile, string outputDir){
889 //if (outputDir == "") { outputDir += hasPath(distFile); }
890 string outfile = getRootName(distFile) + "sorted.dist";
893 //if you can, use the unix sort since its been optimized for years
894 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
895 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
896 system(command.c_str());
897 #else //you are stuck with my best attempt...
898 //windows sort does not have a way to specify a column, only a character in the line
899 //since we cannot assume that the distance will always be at the the same character location on each line
900 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
902 //read in file line by file and put distance first
903 string tempDistFile = distFile + ".temp";
906 openInputFile(distFile, input);
907 openOutputFile(tempDistFile, output);
909 string firstName, secondName;
912 input >> firstName >> secondName >> dist;
913 output << dist << '\t' << firstName << '\t' << secondName << endl;
920 //sort using windows sort
921 string tempOutfile = outfile + ".temp";
922 string command = "sort " + tempDistFile + " /O " + tempOutfile;
923 system(command.c_str());
925 //read in sorted file and put distance at end again
927 openInputFile(tempOutfile, input2);
928 openOutputFile(outfile, output);
931 input2 >> dist >> firstName >> secondName;
932 output << firstName << '\t' << secondName << '\t' << dist << endl;
939 remove(tempDistFile.c_str());
940 remove(tempOutfile.c_str());
945 catch(exception& e) {
946 errorOut(e, "MothurOut", "sortFile");
950 /**************************************************************************************************/
951 vector<unsigned long int> MothurOut::setFilePosFasta(string filename, int& num) {
953 vector<unsigned long int> positions;
955 openInputFile(filename, inFASTA);
958 while(!inFASTA.eof()){
959 input = getline(inFASTA);
960 if (input.length() != 0) {
961 if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
963 gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
967 num = positions.size();
972 //get num bytes in file
973 pFile = fopen (filename.c_str(),"rb");
974 if (pFile==NULL) perror ("Error opening file");
976 fseek (pFile, 0, SEEK_END);
981 unsigned long int size = positions[(positions.size()-1)];
983 openInputFile(filename, in);
988 if(in.eof()) { break; }
993 positions.push_back(size);
997 catch(exception& e) {
998 errorOut(e, "MothurOut", "setFilePosFasta");
1002 /**************************************************************************************************/
1003 vector<unsigned long int> MothurOut::setFilePosEachLine(string filename, int& num) {
1005 filename = getFullPathName(filename);
1007 vector<unsigned long int> positions;
1009 openInputFile(filename, in);
1013 unsigned long int lastpos = in.tellg();
1014 input = getline(in);
1015 if (input.length() != 0) {
1016 unsigned long int pos = in.tellg();
1017 if (pos != -1) { positions.push_back(pos - input.length() - 1); }
1018 else { positions.push_back(lastpos); }
1020 gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
1024 num = positions.size();
1027 unsigned long int size;
1029 //get num bytes in file
1030 pFile = fopen (filename.c_str(),"rb");
1031 if (pFile==NULL) perror ("Error opening file");
1033 fseek (pFile, 0, SEEK_END);
1038 positions.push_back(size);
1042 catch(exception& e) {
1043 errorOut(e, "MothurOut", "setFilePosEachLine");
1047 /**************************************************************************************************/
1049 vector<unsigned long int> MothurOut::divideFile(string filename, int& proc) {
1052 vector<unsigned long int> filePos;
1053 filePos.push_back(0);
1056 unsigned long int size;
1058 filename = getFullPathName(filename);
1060 //get num bytes in file
1061 pFile = fopen (filename.c_str(),"rb");
1062 if (pFile==NULL) perror ("Error opening file");
1064 fseek (pFile, 0, SEEK_END);
1069 //estimate file breaks
1070 unsigned long int chunkSize = 0;
1071 chunkSize = size / proc;
1073 //file to small to divide by processors
1074 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1076 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1077 for (int i = 0; i < proc; i++) {
1078 unsigned long int spot = (i+1) * chunkSize;
1081 openInputFile(filename, in);
1085 unsigned long int newSpot = spot;
1088 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1091 //there was not another sequence before the end of the file
1092 unsigned long int sanityPos = in.tellg();
1094 if (sanityPos == -1) { break; }
1095 else { filePos.push_back(newSpot); }
1101 filePos.push_back(size);
1103 //sanity check filePos
1104 for (int i = 0; i < (filePos.size()-1); i++) {
1105 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1108 proc = (filePos.size() - 1);
1112 catch(exception& e) {
1113 errorOut(e, "MothurOut", "divideFile");
1118 /***********************************************************************/
1120 bool MothurOut::isTrue(string f){
1123 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1125 if ((f == "TRUE") || (f == "T")) { return true; }
1126 else { return false; }
1128 catch(exception& e) {
1129 errorOut(e, "MothurOut", "isTrue");
1134 /***********************************************************************/
1136 float MothurOut::roundDist(float dist, int precision){
1138 return int(dist * precision + 0.5)/float(precision);
1140 catch(exception& e) {
1141 errorOut(e, "MothurOut", "roundDist");
1145 /***********************************************************************/
1147 float MothurOut::ceilDist(float dist, int precision){
1149 return int(ceil(dist * precision))/float(precision);
1151 catch(exception& e) {
1152 errorOut(e, "MothurOut", "ceilDist");
1156 /**********************************************************************************************************************/
1157 map<string, int> MothurOut::readNames(string namefile) {
1160 map<string, int> nameMap;
1164 openInputFile(namefile, in);
1167 if (control_pressed) { break; }
1169 string firstCol, secondCol;
1170 in >> firstCol >> secondCol; gobble(in);
1172 int num = getNumNames(secondCol);
1174 nameMap[firstCol] = num;
1181 catch(exception& e) {
1182 errorOut(e, "MothurOut", "readNames");
1187 /***********************************************************************/
1189 int MothurOut::getNumNames(string names){
1195 for(int i=0;i<names.size();i++){
1196 if(names[i] == ','){
1204 catch(exception& e) {
1205 errorOut(e, "MothurOut", "getNumNames");
1210 /**************************************************************************************************/
1212 vector<vector<double> > MothurOut::binomial(int maxOrder){
1214 vector<vector<double> > binomial(maxOrder+1);
1216 for(int i=0;i<=maxOrder;i++){
1217 binomial[i].resize(maxOrder+1);
1226 for(int i=2;i<=maxOrder;i++){
1230 for(int i=2;i<=maxOrder;i++){
1231 for(int j=1;j<=maxOrder;j++){
1232 if(i==j){ binomial[i][j]=1; }
1233 if(j>i) { binomial[i][j]=0; }
1234 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
1241 catch(exception& e) {
1242 errorOut(e, "MothurOut", "binomial");
1246 /**************************************************************************************************/
1247 unsigned int MothurOut::fromBase36(string base36){
1249 unsigned int num = 0;
1251 map<char, int> converts;
1316 while (i < base36.length()) {
1318 num = 36 * num + converts[c];
1325 catch(exception& e) {
1326 errorOut(e, "MothurOut", "fromBase36");
1330 /***********************************************************************/
1332 int MothurOut::factorial(int num){
1336 for (int i = 1; i <= num; i++) {
1342 catch(exception& e) {
1343 errorOut(e, "MothurOut", "factorial");
1347 /***********************************************************************/
1349 int MothurOut::getNumSeqs(ifstream& file){
1351 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1355 catch(exception& e) {
1356 errorOut(e, "MothurOut", "getNumSeqs");
1360 /***********************************************************************/
1361 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1366 input = getline(file);
1367 if (input.length() != 0) {
1368 if(input[0] == '>'){ numSeqs++; }
1372 catch(exception& e) {
1373 errorOut(e, "MothurOut", "getNumSeqs");
1377 /***********************************************************************/
1379 //This function parses the estimator options and puts them in a vector
1380 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1382 string individual = "";
1383 int estimLength = estim.size();
1384 for(int i=0;i<estimLength;i++){
1385 if(estim[i] == symbol){
1386 container.push_back(individual);
1390 individual += estim[i];
1393 container.push_back(individual);
1396 catch(exception& e) {
1397 errorOut(e, "MothurOut", "splitAtChar");
1402 /***********************************************************************/
1404 //This function parses the estimator options and puts them in a vector
1405 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1407 string individual = "";
1408 int estimLength = estim.size();
1409 for(int i=0;i<estimLength;i++){
1410 if(estim[i] == '-'){
1411 container.push_back(individual);
1415 individual += estim[i];
1418 container.push_back(individual);
1421 /* string individual;
1423 while (estim.find_first_of('-') != -1) {
1424 individual = estim.substr(0,estim.find_first_of('-'));
1425 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1426 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1427 container.push_back(individual);
1431 container.push_back(estim); */
1433 catch(exception& e) {
1434 errorOut(e, "MothurOut", "splitAtDash");
1439 /***********************************************************************/
1440 //This function parses the label options and puts them in a set
1441 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1443 string individual = "";
1444 int estimLength = estim.size();
1445 for(int i=0;i<estimLength;i++){
1446 if(estim[i] == '-'){
1447 container.insert(individual);
1451 individual += estim[i];
1454 container.insert(individual);
1456 // string individual;
1458 // while (estim.find_first_of('-') != -1) {
1459 // individual = estim.substr(0,estim.find_first_of('-'));
1460 // if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1461 // estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1462 // container.insert(individual);
1466 // container.insert(estim);
1469 catch(exception& e) {
1470 errorOut(e, "MothurOut", "splitAtDash");
1474 /***********************************************************************/
1475 //This function parses the line options and puts them in a set
1476 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1481 while (estim.find_first_of('-') != -1) {
1482 individual = estim.substr(0,estim.find_first_of('-'));
1483 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1484 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1485 convert(individual, lineNum); //convert the string to int
1486 container.insert(lineNum);
1490 convert(estim, lineNum); //convert the string to int
1491 container.insert(lineNum);
1493 catch(exception& e) {
1494 errorOut(e, "MothurOut", "splitAtDash");
1498 /***********************************************************************/
1499 //This function parses the a string and puts peices in a vector
1500 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1502 string individual = "";
1503 int estimLength = estim.size();
1504 for(int i=0;i<estimLength;i++){
1505 if(estim[i] == ','){
1506 container.push_back(individual);
1510 individual += estim[i];
1513 container.push_back(individual);
1518 // string individual;
1520 // while (estim.find_first_of(',') != -1) {
1521 // individual = estim.substr(0,estim.find_first_of(','));
1522 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1523 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1524 // container.push_back(individual);
1528 // container.push_back(estim);
1530 catch(exception& e) {
1531 errorOut(e, "MothurOut", "splitAtComma");
1535 /***********************************************************************/
1537 //This function splits up the various option parameters
1538 void MothurOut::splitAtComma(string& prefix, string& suffix){
1540 prefix = suffix.substr(0,suffix.find_first_of(','));
1541 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
1542 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1544 while(suffix.at(0) == ' ')
1545 suffix = suffix.substr(1, suffix.length());
1549 catch(exception& e) {
1550 errorOut(e, "MothurOut", "splitAtComma");
1554 /***********************************************************************/
1556 //This function separates the key value from the option value i.e. dist=96_...
1557 void MothurOut::splitAtEquals(string& key, string& value){
1559 if(value.find_first_of('=') != -1){
1560 key = value.substr(0,value.find_first_of('='));
1561 if ((value.find_first_of('=')+1) <= value.length()) {
1562 value = value.substr(value.find_first_of('=')+1, value.length());
1569 catch(exception& e) {
1570 errorOut(e, "MothurOut", "splitAtEquals");
1575 /**************************************************************************************************/
1577 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1579 for (int i = 0; i < Groups.size(); i++) {
1580 if (groupname == Groups[i]) { return true; }
1584 catch(exception& e) {
1585 errorOut(e, "MothurOut", "inUsersGroups");
1589 /**************************************************************************************************/
1590 //returns true if any of the strings in first vector are in second vector
1591 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1594 for (int i = 0; i < groupnames.size(); i++) {
1595 if (inUsersGroups(groupnames[i], Groups)) { return true; }
1599 catch(exception& e) {
1600 errorOut(e, "MothurOut", "inUsersGroups");
1604 /***********************************************************************/
1605 //this function determines if the user has given us labels that are smaller than the given label.
1606 //if so then it returns true so that the calling function can run the previous valid distance.
1607 //it's a "smart" distance function. It also checks for invalid labels.
1608 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1611 set<string>::iterator it;
1612 vector<float> orderFloat;
1613 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
1614 map<string, float>::iterator it2;
1616 bool smaller = false;
1618 //unique is the smallest line
1619 if (label == "unique") { return false; }
1621 if (convertTestFloat(label, labelFloat)) {
1622 convert(label, labelFloat);
1623 }else { //cant convert
1628 //go through users set and make them floats
1629 for(it = userLabels.begin(); it != userLabels.end();) {
1632 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1634 orderFloat.push_back(temp);
1635 userMap[*it] = temp;
1637 }else if (*it == "unique") {
1638 orderFloat.push_back(-1.0);
1639 userMap["unique"] = -1.0;
1642 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
1643 userLabels.erase(it++);
1648 sort(orderFloat.begin(), orderFloat.end());
1650 /*************************************************/
1651 //is this label bigger than any of the users labels
1652 /*************************************************/
1654 //loop through order until you find a label greater than label
1655 for (int i = 0; i < orderFloat.size(); i++) {
1656 if (orderFloat[i] < labelFloat) {
1658 if (orderFloat[i] == -1) {
1659 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
1660 userLabels.erase("unique");
1663 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
1665 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
1666 if (it2->second == orderFloat[i]) {
1668 //remove small labels
1669 userLabels.erase(s);
1673 if (errorOff == "") {mothurOut( s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
1675 //since they are sorted once you find a bigger one stop looking
1682 catch(exception& e) {
1683 errorOut(e, "MothurOut", "anyLabelsToProcess");
1688 /**************************************************************************************************/
1689 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1694 string line = getline(file);
1696 //before we added this check
1697 if (line[0] != '#') { good = false; }
1700 line = line.substr(1);
1702 vector<string> versionVector;
1703 splitAtChar(version, versionVector, '.');
1705 //check file version
1706 vector<string> linesVector;
1707 splitAtChar(line, linesVector, '.');
1709 if (versionVector.size() != linesVector.size()) { good = false; }
1711 for (int j = 0; j < versionVector.size(); j++) {
1713 convert(versionVector[j], num1);
1714 convert(linesVector[j], num2);
1716 //if mothurs version is newer than this files version, then we want to remake it
1717 if (num1 > num2) { good = false; break; }
1723 if (!good) { file.close(); }
1724 else { file.seekg(0); }
1728 catch(exception& e) {
1729 errorOut(e, "MothurOut", "checkReleaseVersion");
1734 /**************************************************************************************************/