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::setFileName(string filename) {
23 logFileName = filename;
27 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
29 if (pid == 0) { //only one process should output to screen
32 openOutputFile(filename, out);
39 errorOut(e, "MothurOut", "setFileName");
43 /*********************************************************************************************/
44 void MothurOut::setDefaultPath(string pathname) {
47 //add / to name if needed
48 string lastChar = pathname.substr(pathname.length()-1);
49 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
50 if (lastChar != "/") { pathname += "/"; }
52 if (lastChar != "\\") { pathname += "\\"; }
55 defaultPath = pathname;
59 errorOut(e, "MothurOut", "setDefaultPath");
63 /*********************************************************************************************/
64 void MothurOut::setOutputDir(string pathname) {
69 errorOut(e, "MothurOut", "setOutputDir");
73 /*********************************************************************************************/
74 void MothurOut::closeLog() {
79 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
81 if (pid == 0) { //only one process should output to screen
91 errorOut(e, "MothurOut", "closeLog");
96 /*********************************************************************************************/
97 MothurOut::~MothurOut() {
102 catch(exception& e) {
103 errorOut(e, "MothurOut", "MothurOut");
107 /*********************************************************************************************/
108 void MothurOut::mothurOut(string output) {
113 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
115 if (pid == 0) { //only one process should output to screen
125 catch(exception& e) {
126 errorOut(e, "MothurOut", "MothurOut");
130 /*********************************************************************************************/
131 void MothurOut::mothurOutEndLine() {
135 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
137 if (pid == 0) { //only one process should output to screen
147 catch(exception& e) {
148 errorOut(e, "MothurOut", "MothurOutEndLine");
152 /*********************************************************************************************/
153 void MothurOut::mothurOutJustToLog(string output) {
157 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
159 if (pid == 0) { //only one process should output to screen
168 catch(exception& e) {
169 errorOut(e, "MothurOut", "MothurOutJustToLog");
173 /*********************************************************************************************/
174 void MothurOut::errorOut(exception& e, string object, string function) {
176 //mem_usage(vm, rss);
178 mothurOut("[ERROR]: ");
179 mothurOut(toString(e.what()));
180 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.");
183 /*********************************************************************************************/
184 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
185 // process_mem_usage(double &, double &) - takes two doubles by reference,
186 // attempts to read the system-dependent data for a process' virtual memory
187 // size and resident set size, and return the results in KB.
189 // On failure, returns 0.0, 0.0
190 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
191 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
196 // 'file' stat seems to give the most reliable results
198 ifstream stat_stream("/proc/self/stat",ios_base::in);
200 // dummy vars for leading entries in stat that we don't care about
202 string pid, comm, state, ppid, pgrp, session, tty_nr;
203 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
204 string utime, stime, cutime, cstime, priority, nice;
205 string O, itrealvalue, starttime;
207 // the two fields we want
212 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
213 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
214 >> utime >> stime >> cutime >> cstime >> priority >> nice
215 >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
217 long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
218 vm_usage = vsize / 1024.0;
219 resident_set = rss * page_size_kb;
221 mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
225 /* //windows memory usage
226 // Get the list of process identifiers.
227 DWORD aProcesses[1024], cbNeeded, cProcesses;
229 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
231 // Calculate how many process identifiers were returned.
232 cProcesses = cbNeeded / sizeof(DWORD);
234 // Print the memory usage for each process
235 for (int i = 0; i < cProcesses; i++ ) {
236 DWORD processID = aProcesses[i];
238 PROCESS_MEMORY_COUNTERS pmc;
240 HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
242 // Print the process identifier.
243 printf( "\nProcess ID: %u\n", processID);
245 if (NULL != hProcess) {
247 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
248 printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
249 printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
250 printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
251 printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
252 printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
253 printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
254 printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
255 printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
256 printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
258 CloseHandle(hProcess);
268 /***********************************************************************/
269 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
271 fileName = getFullPathName(fileName);
273 fileHandle.open(fileName.c_str(), ios::app);
275 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
282 catch(exception& e) {
283 errorOut(e, "MothurOut", "openOutputFileAppend");
287 /***********************************************************************/
288 void MothurOut::gobble(istream& f){
292 while(isspace(d=f.get())) { ;}
295 catch(exception& e) {
296 errorOut(e, "MothurOut", "gobble");
300 /***********************************************************************/
301 void MothurOut::gobble(istringstream& f){
304 while(isspace(d=f.get())) {;}
307 catch(exception& e) {
308 errorOut(e, "MothurOut", "gobble");
313 /***********************************************************************/
315 string MothurOut::getline(istringstream& fileHandle) {
320 while (!fileHandle.eof()) {
322 char c = fileHandle.get();
324 //are you at the end of the line
325 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
332 catch(exception& e) {
333 errorOut(e, "MothurOut", "getline");
337 /***********************************************************************/
339 string MothurOut::getline(ifstream& fileHandle) {
344 while (!fileHandle.eof()) {
346 char c = fileHandle.get();
348 //are you at the end of the line
349 if ((c == '\n') || (c == '\r') || (c == '\f')){ break; }
356 catch(exception& e) {
357 errorOut(e, "MothurOut", "getline");
361 /***********************************************************************/
363 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
364 #ifdef USE_COMPRESSION
365 inline bool endsWith(string s, const char * suffix){
366 size_t suffixLength = strlen(suffix);
367 return s.size() >= suffixLength && s.substr(s.size() - suffixLength, suffixLength).compare(suffix) == 0;
372 string MothurOut::getRootName(string longName){
375 string rootName = longName;
377 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
378 #ifdef USE_COMPRESSION
379 if (endsWith(rootName, ".gz") || endsWith(rootName, ".bz2")) {
380 int pos = rootName.find_last_of('.');
381 rootName = rootName.substr(0, pos);
382 cerr << "shortening " << longName << " to " << rootName << "\n";
386 if(rootName.find_last_of(".") != rootName.npos){
387 int pos = rootName.find_last_of('.')+1;
388 rootName = rootName.substr(0, pos);
393 catch(exception& e) {
394 errorOut(e, "MothurOut", "getRootName");
398 /***********************************************************************/
400 string MothurOut::getSimpleName(string longName){
402 string simpleName = longName;
405 found=longName.find_last_of("/\\");
407 if(found != longName.npos){
408 simpleName = longName.substr(found+1);
413 catch(exception& e) {
414 errorOut(e, "MothurOut", "getSimpleName");
419 /***********************************************************************/
421 string MothurOut::getPathName(string longName){
423 string rootPathName = longName;
425 if(longName.find_last_of("/\\") != longName.npos){
426 int pos = longName.find_last_of("/\\")+1;
427 rootPathName = longName.substr(0, pos);
432 catch(exception& e) {
433 errorOut(e, "MothurOut", "getPathName");
438 /***********************************************************************/
440 string MothurOut::hasPath(string longName){
445 found=longName.find_last_of("~/\\");
447 if(found != longName.npos){
448 path = longName.substr(0, found+1);
453 catch(exception& e) {
454 errorOut(e, "MothurOut", "hasPath");
459 /***********************************************************************/
461 string MothurOut::getExtension(string longName){
463 string extension = longName;
465 if(longName.find_last_of('.') != longName.npos){
466 int pos = longName.find_last_of('.');
467 extension = longName.substr(pos, longName.length());
472 catch(exception& e) {
473 errorOut(e, "MothurOut", "getExtension");
477 /***********************************************************************/
478 bool MothurOut::isBlank(string fileName){
481 fileName = getFullPathName(fileName);
484 fileHandle.open(fileName.c_str());
486 mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
489 //check for blank file
491 if (fileHandle.eof()) { fileHandle.close(); return true; }
496 catch(exception& e) {
497 errorOut(e, "MothurOut", "isBlank");
501 /***********************************************************************/
503 string MothurOut::getFullPathName(string fileName){
506 string path = hasPath(fileName);
510 if (path == "") { return fileName; } //its a simple name
511 else { //we need to complete the pathname
512 // ex. ../../../filename
513 // cwd = /user/work/desktop
516 //get current working directory
517 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
519 if (path.find("~") != -1) { //go to home directory
522 char *homepath = NULL;
523 homepath = getenv ("HOME");
524 if ( homepath != NULL) { homeDir = homepath; }
525 else { homeDir = ""; }
527 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
530 if (path.rfind("./") == string::npos) { return fileName; } //already complete name
531 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
533 //char* cwdpath = new char[1024];
535 //cwdpath=getcwd(cwdpath,size);
538 char *cwdpath = NULL;
539 cwdpath = getcwd(NULL, 0); // or _getcwd
540 if ( cwdpath != NULL) { cwd = cwdpath; }
546 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
548 //break apart the current working directory
550 while (simpleCWD.find_first_of('/') != string::npos) {
551 string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
552 simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
555 //get last one // ex. ../../../filename = /user/work/desktop/filename
556 dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
559 int index = dirs.size()-1;
561 while((pos = path.rfind("./")) != string::npos) { //while you don't have a complete path
562 if (pos == 0) { break; //you are at the end
563 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
564 path = path.substr(0, pos-1);
566 if (index == 0) { break; }
567 }else if (path[(pos-1)] == '/') { //you want the current working dir ./
568 path = path.substr(0, pos);
569 }else if (pos == 1) { break; //you are at the end
570 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
573 for (int i = index; i >= 0; i--) {
574 newFileName = dirs[i] + "/" + newFileName;
577 newFileName = "/" + newFileName;
581 if (path.find("~") != string::npos) { //go to home directory
582 string homeDir = getenv ("HOMEPATH");
583 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
586 if (path.rfind(".\\") == string::npos) { return fileName; } //already complete name
587 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
589 char *cwdpath = NULL;
590 cwdpath = getcwd(NULL, 0); // or _getcwd
591 if ( cwdpath != NULL) { cwd = cwdpath; }
594 //break apart the current working directory
596 while (cwd.find_first_of('\\') != -1) {
597 string dir = cwd.substr(0,cwd.find_first_of('\\'));
598 cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
603 dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
605 int index = dirs.size()-1;
607 while((pos = path.rfind(".\\")) != string::npos) { //while you don't have a complete path
608 if (pos == 0) { break; //you are at the end
609 }else if (path[(pos-1)] == '.') { //you want your parent directory ../
610 path = path.substr(0, pos-1);
612 if (index == 0) { break; }
613 }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
614 path = path.substr(0, pos);
615 }else if (pos == 1) { break; //you are at the end
616 }else { cout << "cannot resolve path for " << fileName << endl; return fileName; }
619 for (int i = index; i >= 0; i--) {
620 newFileName = dirs[i] + "\\" + newFileName;
629 catch(exception& e) {
630 errorOut(e, "MothurOut", "getFullPathName");
634 /***********************************************************************/
636 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
639 string completeFileName = getFullPathName(fileName);
640 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
641 #ifdef USE_COMPRESSION
642 // check for gzipped or bzipped file
643 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
644 string tempName = string(tmpnam(0));
645 mkfifo(tempName.c_str(), 0666);
646 int fork_result = fork();
647 if (fork_result < 0) {
648 cerr << "Error forking.\n";
650 } else if (fork_result == 0) {
651 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
652 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
653 system(command.c_str());
654 cerr << "Done decompressing " << completeFileName << "\n";
655 remove(tempName.c_str());
658 cerr << "waiting on child process " << fork_result << "\n";
659 completeFileName = tempName;
664 fileHandle.open(completeFileName.c_str());
666 //mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
669 //check for blank file
674 catch(exception& e) {
675 errorOut(e, "MothurOut", "openInputFile - no Error");
679 /***********************************************************************/
681 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
685 string completeFileName = getFullPathName(fileName);
686 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
687 #ifdef USE_COMPRESSION
688 // check for gzipped or bzipped file
689 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
690 string tempName = string(tmpnam(0));
691 mkfifo(tempName.c_str(), 0666);
692 int fork_result = fork();
693 if (fork_result < 0) {
694 cerr << "Error forking.\n";
696 } else if (fork_result == 0) {
697 string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
698 cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
699 system(command.c_str());
700 cerr << "Done decompressing " << completeFileName << "\n";
701 remove(tempName.c_str());
704 cerr << "waiting on child process " << fork_result << "\n";
705 completeFileName = tempName;
711 fileHandle.open(completeFileName.c_str());
713 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
717 //check for blank file
719 if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine(); }
724 catch(exception& e) {
725 errorOut(e, "MothurOut", "openInputFile");
729 /***********************************************************************/
731 int MothurOut::renameFile(string oldName, string newName){
734 int exist = openInputFile(newName, inTest, "");
736 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
737 if (exist == 0) { //you could open it so you want to delete it
739 string command = "rm " + newName;
740 system(command.c_str());
743 string command = "mv " + oldName + " " + newName;
744 system(command.c_str());
746 remove(newName.c_str());
747 int renameOk = rename(oldName.c_str(), newName.c_str());
752 catch(exception& e) {
753 errorOut(e, "MothurOut", "renameFile");
758 /***********************************************************************/
760 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
763 string completeFileName = getFullPathName(fileName);
764 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
765 #ifdef USE_COMPRESSION
766 // check for gzipped file
767 if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
768 string tempName = string(tmpnam(0));
769 mkfifo(tempName.c_str(), 0666);
770 cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
771 int fork_result = fork();
772 if (fork_result < 0) {
773 cerr << "Error forking.\n";
775 } else if (fork_result == 0) {
776 string command = string(endsWith(completeFileName, ".gz") ? "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
777 system(command.c_str());
780 completeFileName = tempName;
785 fileHandle.open(completeFileName.c_str(), ios::trunc);
787 mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
794 catch(exception& e) {
795 errorOut(e, "MothurOut", "openOutputFile");
801 /**************************************************************************************************/
802 void MothurOut::appendFiles(string temp, string filename) {
807 //open output file in append mode
808 openOutputFileAppend(filename, output);
809 int ableToOpen = openInputFile(temp, input, "no error");
810 //int ableToOpen = openInputFile(temp, input);
812 if (ableToOpen == 0) { //you opened it
813 while(char c = input.get()){
814 if(input.eof()) { break; }
815 else { output << c; }
822 catch(exception& e) {
823 errorOut(e, "MothurOut", "appendFiles");
828 /**************************************************************************************************/
829 string MothurOut::sortFile(string distFile, string outputDir){
832 //if (outputDir == "") { outputDir += hasPath(distFile); }
833 string outfile = getRootName(distFile) + "sorted.dist";
836 //if you can, use the unix sort since its been optimized for years
837 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
838 string command = "sort -n -k +3 " + distFile + " -o " + outfile;
839 system(command.c_str());
840 #else //you are stuck with my best attempt...
841 //windows sort does not have a way to specify a column, only a character in the line
842 //since we cannot assume that the distance will always be at the the same character location on each line
843 //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
845 //read in file line by file and put distance first
846 string tempDistFile = distFile + ".temp";
849 openInputFile(distFile, input);
850 openOutputFile(tempDistFile, output);
852 string firstName, secondName;
855 input >> firstName >> secondName >> dist;
856 output << dist << '\t' << firstName << '\t' << secondName << endl;
863 //sort using windows sort
864 string tempOutfile = outfile + ".temp";
865 string command = "sort " + tempDistFile + " /O " + tempOutfile;
866 system(command.c_str());
868 //read in sorted file and put distance at end again
870 openInputFile(tempOutfile, input2);
871 openOutputFile(outfile, output);
874 input2 >> dist >> firstName >> secondName;
875 output << firstName << '\t' << secondName << '\t' << dist << endl;
882 remove(tempDistFile.c_str());
883 remove(tempOutfile.c_str());
888 catch(exception& e) {
889 errorOut(e, "MothurOut", "sortFile");
893 /**************************************************************************************************/
894 vector<unsigned long int> MothurOut::setFilePosFasta(string filename, int& num) {
896 vector<unsigned long int> positions;
898 openInputFile(filename, inFASTA);
901 while(!inFASTA.eof()){
902 input = getline(inFASTA);
903 if (input.length() != 0) {
904 if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
906 gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
910 num = positions.size();
915 //get num bytes in file
916 pFile = fopen (filename.c_str(),"rb");
917 if (pFile==NULL) perror ("Error opening file");
919 fseek (pFile, 0, SEEK_END);
924 unsigned long int size = positions[(positions.size()-1)];
926 openInputFile(filename, in);
931 if(in.eof()) { break; }
936 positions.push_back(size);
940 catch(exception& e) {
941 errorOut(e, "MothurOut", "setFilePosFasta");
945 /**************************************************************************************************/
946 vector<unsigned long int> MothurOut::setFilePosEachLine(string filename, int& num) {
948 filename = getFullPathName(filename);
950 vector<unsigned long int> positions;
952 openInputFile(filename, in);
956 unsigned long int lastpos = in.tellg();
958 if (input.length() != 0) {
959 unsigned long int pos = in.tellg();
960 if (pos != -1) { positions.push_back(pos - input.length() - 1); }
961 else { positions.push_back(lastpos); }
963 gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
967 num = positions.size();
970 unsigned long int 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 positions.push_back(size);
985 catch(exception& e) {
986 errorOut(e, "MothurOut", "setFilePosEachLine");
990 /**************************************************************************************************/
992 vector<unsigned long int> MothurOut::divideFile(string filename, int& proc) {
995 vector<unsigned long int> filePos;
996 filePos.push_back(0);
999 unsigned long int size;
1001 filename = getFullPathName(filename);
1003 //get num bytes in file
1004 pFile = fopen (filename.c_str(),"rb");
1005 if (pFile==NULL) perror ("Error opening file");
1007 fseek (pFile, 0, SEEK_END);
1012 //estimate file breaks
1013 unsigned long int chunkSize = 0;
1014 chunkSize = size / proc;
1016 //file to small to divide by processors
1017 if (chunkSize == 0) { proc = 1; filePos.push_back(size); return filePos; }
1019 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1020 for (int i = 0; i < proc; i++) {
1021 unsigned long int spot = (i+1) * chunkSize;
1024 openInputFile(filename, in);
1028 unsigned long int newSpot = spot;
1031 if (c == '>') { in.putback(c); newSpot = in.tellg(); break; }
1034 //there was not another sequence before the end of the file
1035 unsigned long int sanityPos = in.tellg();
1037 if (sanityPos == -1) { break; }
1038 else { filePos.push_back(newSpot); }
1044 filePos.push_back(size);
1046 //sanity check filePos
1047 for (int i = 0; i < (filePos.size()-1); i++) {
1048 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1051 proc = (filePos.size() - 1);
1055 catch(exception& e) {
1056 errorOut(e, "MothurOut", "divideFile");
1061 /***********************************************************************/
1063 bool MothurOut::isTrue(string f){
1066 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1068 if ((f == "TRUE") || (f == "T")) { return true; }
1069 else { return false; }
1071 catch(exception& e) {
1072 errorOut(e, "MothurOut", "isTrue");
1077 /***********************************************************************/
1079 float MothurOut::roundDist(float dist, int precision){
1081 return int(dist * precision + 0.5)/float(precision);
1083 catch(exception& e) {
1084 errorOut(e, "MothurOut", "roundDist");
1088 /***********************************************************************/
1090 float MothurOut::ceilDist(float dist, int precision){
1092 return int(ceil(dist * precision))/float(precision);
1094 catch(exception& e) {
1095 errorOut(e, "MothurOut", "ceilDist");
1100 /***********************************************************************/
1102 int MothurOut::getNumNames(string names){
1108 for(int i=0;i<names.size();i++){
1109 if(names[i] == ','){
1117 catch(exception& e) {
1118 errorOut(e, "MothurOut", "getNumNames");
1123 /**************************************************************************************************/
1125 vector<vector<double> > MothurOut::binomial(int maxOrder){
1127 vector<vector<double> > binomial(maxOrder+1);
1129 for(int i=0;i<=maxOrder;i++){
1130 binomial[i].resize(maxOrder+1);
1139 for(int i=2;i<=maxOrder;i++){
1143 for(int i=2;i<=maxOrder;i++){
1144 for(int j=1;j<=maxOrder;j++){
1145 if(i==j){ binomial[i][j]=1; }
1146 if(j>i) { binomial[i][j]=0; }
1147 else { binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j]; }
1154 catch(exception& e) {
1155 errorOut(e, "MothurOut", "binomial");
1159 /**************************************************************************************************/
1160 unsigned int MothurOut::fromBase36(string base36){
1162 unsigned int num = 0;
1164 map<char, int> converts;
1229 while (i < base36.length()) {
1231 num = 36 * num + converts[c];
1238 catch(exception& e) {
1239 errorOut(e, "MothurOut", "fromBase36");
1243 /***********************************************************************/
1245 int MothurOut::factorial(int num){
1249 for (int i = 1; i <= num; i++) {
1255 catch(exception& e) {
1256 errorOut(e, "MothurOut", "factorial");
1260 /***********************************************************************/
1262 int MothurOut::getNumSeqs(ifstream& file){
1264 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1268 catch(exception& e) {
1269 errorOut(e, "MothurOut", "getNumSeqs");
1273 /***********************************************************************/
1274 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1279 input = getline(file);
1280 if (input.length() != 0) {
1281 if(input[0] == '>'){ numSeqs++; }
1285 catch(exception& e) {
1286 errorOut(e, "MothurOut", "getNumSeqs");
1290 /***********************************************************************/
1292 //This function parses the estimator options and puts them in a vector
1293 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1295 string individual = "";
1296 int estimLength = estim.size();
1297 for(int i=0;i<estimLength;i++){
1298 if(estim[i] == symbol){
1299 container.push_back(individual);
1303 individual += estim[i];
1306 container.push_back(individual);
1309 catch(exception& e) {
1310 errorOut(e, "MothurOut", "splitAtChar");
1315 /***********************************************************************/
1317 //This function parses the estimator options and puts them in a vector
1318 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1320 string individual = "";
1321 int estimLength = estim.size();
1322 for(int i=0;i<estimLength;i++){
1323 if(estim[i] == '-'){
1324 container.push_back(individual);
1328 individual += estim[i];
1331 container.push_back(individual);
1334 /* string individual;
1336 while (estim.find_first_of('-') != -1) {
1337 individual = estim.substr(0,estim.find_first_of('-'));
1338 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1339 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1340 container.push_back(individual);
1344 container.push_back(estim); */
1346 catch(exception& e) {
1347 errorOut(e, "MothurOut", "splitAtDash");
1352 /***********************************************************************/
1353 //This function parses the label options and puts them in a set
1354 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1356 string individual = "";
1357 int estimLength = estim.size();
1358 for(int i=0;i<estimLength;i++){
1359 if(estim[i] == '-'){
1360 container.insert(individual);
1364 individual += estim[i];
1367 container.insert(individual);
1369 // string individual;
1371 // while (estim.find_first_of('-') != -1) {
1372 // individual = estim.substr(0,estim.find_first_of('-'));
1373 // if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1374 // estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1375 // container.insert(individual);
1379 // container.insert(estim);
1382 catch(exception& e) {
1383 errorOut(e, "MothurOut", "splitAtDash");
1387 /***********************************************************************/
1388 //This function parses the line options and puts them in a set
1389 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1394 while (estim.find_first_of('-') != -1) {
1395 individual = estim.substr(0,estim.find_first_of('-'));
1396 if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1397 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1398 convert(individual, lineNum); //convert the string to int
1399 container.insert(lineNum);
1403 convert(estim, lineNum); //convert the string to int
1404 container.insert(lineNum);
1406 catch(exception& e) {
1407 errorOut(e, "MothurOut", "splitAtDash");
1411 /***********************************************************************/
1412 //This function parses the a string and puts peices in a vector
1413 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1415 string individual = "";
1416 int estimLength = estim.size();
1417 for(int i=0;i<estimLength;i++){
1418 if(estim[i] == ','){
1419 container.push_back(individual);
1423 individual += estim[i];
1426 container.push_back(individual);
1431 // string individual;
1433 // while (estim.find_first_of(',') != -1) {
1434 // individual = estim.substr(0,estim.find_first_of(','));
1435 // if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1436 // estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1437 // container.push_back(individual);
1441 // container.push_back(estim);
1443 catch(exception& e) {
1444 errorOut(e, "MothurOut", "splitAtComma");
1448 /***********************************************************************/
1450 //This function splits up the various option parameters
1451 void MothurOut::splitAtComma(string& prefix, string& suffix){
1453 prefix = suffix.substr(0,suffix.find_first_of(','));
1454 if ((suffix.find_first_of(',')+2) <= suffix.length()) { //checks to make sure you don't have comma at end of string
1455 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1457 while(suffix.at(0) == ' ')
1458 suffix = suffix.substr(1, suffix.length());
1462 catch(exception& e) {
1463 errorOut(e, "MothurOut", "splitAtComma");
1467 /***********************************************************************/
1469 //This function separates the key value from the option value i.e. dist=96_...
1470 void MothurOut::splitAtEquals(string& key, string& value){
1472 if(value.find_first_of('=') != -1){
1473 key = value.substr(0,value.find_first_of('='));
1474 if ((value.find_first_of('=')+1) <= value.length()) {
1475 value = value.substr(value.find_first_of('=')+1, value.length());
1482 catch(exception& e) {
1483 errorOut(e, "MothurOut", "splitAtEquals");
1488 /**************************************************************************************************/
1490 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1492 for (int i = 0; i < Groups.size(); i++) {
1493 if (groupname == Groups[i]) { return true; }
1497 catch(exception& e) {
1498 errorOut(e, "MothurOut", "inUsersGroups");
1502 /**************************************************************************************************/
1503 //returns true if any of the strings in first vector are in second vector
1504 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1507 for (int i = 0; i < groupnames.size(); i++) {
1508 if (inUsersGroups(groupnames[i], Groups)) { return true; }
1512 catch(exception& e) {
1513 errorOut(e, "MothurOut", "inUsersGroups");
1517 /***********************************************************************/
1518 //this function determines if the user has given us labels that are smaller than the given label.
1519 //if so then it returns true so that the calling function can run the previous valid distance.
1520 //it's a "smart" distance function. It also checks for invalid labels.
1521 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1524 set<string>::iterator it;
1525 vector<float> orderFloat;
1526 map<string, float> userMap; //the conversion process removes trailing 0's which we need to put back
1527 map<string, float>::iterator it2;
1529 bool smaller = false;
1531 //unique is the smallest line
1532 if (label == "unique") { return false; }
1534 if (convertTestFloat(label, labelFloat)) {
1535 convert(label, labelFloat);
1536 }else { //cant convert
1541 //go through users set and make them floats
1542 for(it = userLabels.begin(); it != userLabels.end();) {
1545 if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1547 orderFloat.push_back(temp);
1548 userMap[*it] = temp;
1550 }else if (*it == "unique") {
1551 orderFloat.push_back(-1.0);
1552 userMap["unique"] = -1.0;
1555 if (errorOff == "") { mothurOut(*it + " is not a valid label."); mothurOutEndLine(); }
1556 userLabels.erase(it++);
1561 sort(orderFloat.begin(), orderFloat.end());
1563 /*************************************************/
1564 //is this label bigger than any of the users labels
1565 /*************************************************/
1567 //loop through order until you find a label greater than label
1568 for (int i = 0; i < orderFloat.size(); i++) {
1569 if (orderFloat[i] < labelFloat) {
1571 if (orderFloat[i] == -1) {
1572 if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
1573 userLabels.erase("unique");
1576 if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
1578 for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {
1579 if (it2->second == orderFloat[i]) {
1581 //remove small labels
1582 userLabels.erase(s);
1586 if (errorOff == "") {mothurOut( s + ". I will use the next smallest distance. "); mothurOutEndLine(); }
1588 //since they are sorted once you find a bigger one stop looking
1595 catch(exception& e) {
1596 errorOut(e, "MothurOut", "anyLabelsToProcess");
1601 /**************************************************************************************************/
1602 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1607 string line = getline(file);
1609 //before we added this check
1610 if (line[0] != '#') { good = false; }
1613 line = line.substr(1);
1615 vector<string> versionVector;
1616 splitAtChar(version, versionVector, '.');
1618 //check file version
1619 vector<string> linesVector;
1620 splitAtChar(line, linesVector, '.');
1622 if (versionVector.size() != linesVector.size()) { good = false; }
1624 for (int j = 0; j < versionVector.size(); j++) {
1626 convert(versionVector[j], num1);
1627 convert(linesVector[j], num2);
1629 //if mothurs version is newer than this files version, then we want to remake it
1630 if (num1 > num2) { good = false; break; }
1636 if (!good) { file.close(); }
1637 else { file.seekg(0); }
1641 catch(exception& e) {
1642 errorOut(e, "MothurOut", "checkReleaseVersion");
1647 /**************************************************************************************************/