]> git.donarmstrong.com Git - mothur.git/blob - mothurout.cpp
working on megastats
[mothur.git] / mothurout.cpp
1 /*
2  *  mothurOut.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 2/25/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "mothurout.h"
11
12 /******************************************************/
13 MothurOut* MothurOut::getInstance() {
14         if( _uniqueInstance == 0) {
15                 _uniqueInstance = new MothurOut();
16         }
17         return _uniqueInstance;
18 }
19 /*********************************************************************************************/
20 void MothurOut::setFileName(string filename)  {
21         try {
22                 logFileName = filename;
23                 
24                 #ifdef USE_MPI
25                         int pid;
26                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
27                                         
28                         if (pid == 0) { //only one process should output to screen
29                 #endif
30                 
31                 openOutputFile(filename, out);
32                 
33                 #ifdef USE_MPI
34                         }
35                 #endif
36         }
37         catch(exception& e) {
38                 errorOut(e, "MothurOut", "setFileName");
39                 exit(1);
40         }
41 }
42 /*********************************************************************************************/
43 void MothurOut::setDefaultPath(string pathname)  {
44         try {
45         
46                 //add / to name if needed
47                 string lastChar = pathname.substr(pathname.length()-1);
48                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
49                         if (lastChar != "/") { pathname += "/"; }
50                 #else
51                         if (lastChar != "\\") { pathname += "\\"; }     
52                 #endif
53                 
54                 defaultPath = pathname;
55                 
56         }
57         catch(exception& e) {
58                 errorOut(e, "MothurOut", "setDefaultPath");
59                 exit(1);
60         }
61 }
62 /*********************************************************************************************/
63 void MothurOut::closeLog()  {
64         try {
65                 
66                 #ifdef USE_MPI
67                         int pid;
68                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
69                                         
70                         if (pid == 0) { //only one process should output to screen
71                 #endif
72                 
73                 out.close();
74                 
75                 #ifdef USE_MPI
76                         }
77                 #endif
78         }
79         catch(exception& e) {
80                 errorOut(e, "MothurOut", "closeLog");
81                 exit(1);
82         }
83 }
84
85 /*********************************************************************************************/
86 MothurOut::~MothurOut() {
87         try {
88                 _uniqueInstance = 0;
89                 
90         }
91         catch(exception& e) {
92                 errorOut(e, "MothurOut", "MothurOut");
93                 exit(1);
94         }
95 }
96 /*********************************************************************************************/
97 void MothurOut::mothurOut(string output) {
98         try {
99                 
100                 #ifdef USE_MPI
101                         int pid;
102                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
103                                         
104                         if (pid == 0) { //only one process should output to screen
105                 #endif
106                 
107                 cout << output;
108                 out << output;
109                 
110                 #ifdef USE_MPI
111                         }
112                 #endif
113         }
114         catch(exception& e) {
115                 errorOut(e, "MothurOut", "MothurOut");
116                 exit(1);
117         }
118 }
119 /*********************************************************************************************/
120 void MothurOut::mothurOutEndLine() {
121         try {
122                 #ifdef USE_MPI
123                         int pid;
124                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
125                                         
126                         if (pid == 0) { //only one process should output to screen
127                 #endif
128                 
129                 cout << endl;
130                 out << endl;
131                 
132                 #ifdef USE_MPI
133                         }
134                 #endif
135         }
136         catch(exception& e) {
137                 errorOut(e, "MothurOut", "MothurOutEndLine");
138                 exit(1);
139         }
140 }
141 /*********************************************************************************************/
142 void MothurOut::mothurOutJustToLog(string output) {
143         try {
144                 #ifdef USE_MPI
145                         int pid;
146                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
147                                         
148                         if (pid == 0) { //only one process should output to screen
149                 #endif
150                 
151                 out << output;
152                 
153                 #ifdef USE_MPI
154                         }
155                 #endif
156         }
157         catch(exception& e) {
158                 errorOut(e, "MothurOut", "MothurOutJustToLog");
159                 exit(1);
160         }
161 }
162 /*********************************************************************************************/
163 void MothurOut::errorOut(exception& e, string object, string function) {
164         //double vm, rss;
165         //mem_usage(vm, rss);
166         
167         mothurOut("[ERROR]: ");
168         mothurOut(toString(e.what()));
169         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.");
170         mothurOutEndLine();
171 }
172 /*********************************************************************************************/
173 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c 
174 // process_mem_usage(double &, double &) - takes two doubles by reference,
175 // attempts to read the system-dependent data for a process' virtual memory
176 // size and resident set size, and return the results in KB.
177 //
178 // On failure, returns 0.0, 0.0
179 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
180   #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
181   
182            vm_usage     = 0.0;
183            resident_set = 0.0;
184
185            // 'file' stat seems to give the most reliable results
186            //
187            ifstream stat_stream("/proc/self/stat",ios_base::in);
188
189            // dummy vars for leading entries in stat that we don't care about
190            //
191            string pid, comm, state, ppid, pgrp, session, tty_nr;
192            string tpgid, flags, minflt, cminflt, majflt, cmajflt;
193            string utime, stime, cutime, cstime, priority, nice;
194            string O, itrealvalue, starttime;
195
196            // the two fields we want
197            //
198            unsigned long vsize;
199            long rss;
200
201            stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
202                                    >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
203                                    >> utime >> stime >> cutime >> cstime >> priority >> nice
204                                    >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
205
206            long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
207            vm_usage     = vsize / 1024.0;
208            resident_set = rss * page_size_kb;
209            
210            mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
211                 return 0;
212
213         #else
214 /*              //windows memory usage
215                 // Get the list of process identifiers.
216                 DWORD aProcesses[1024], cbNeeded, cProcesses;
217                 
218                 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
219
220                 // Calculate how many process identifiers were returned.
221                 cProcesses = cbNeeded / sizeof(DWORD);
222
223                 // Print the memory usage for each process
224                 for (int i = 0; i < cProcesses; i++ ) {
225                         DWORD processID = aProcesses[i];
226                         
227                         PROCESS_MEMORY_COUNTERS pmc;
228
229                         HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
230
231                         // Print the process identifier.
232                         printf( "\nProcess ID: %u\n", processID);
233                         
234                         if (NULL != hProcess) {
235
236                                 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
237                                         printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
238                                         printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
239                                         printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
240                                         printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
241                                         printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
242                                         printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
243                                         printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
244                                         printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage ); 
245                                         printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
246                                 }
247                                 CloseHandle(hProcess);
248                         }
249                 }
250 */
251                         return 0;
252
253         #endif
254 }
255
256
257 /***********************************************************************/
258 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
259         try {
260                 fileName = getFullPathName(fileName);
261                 
262                 fileHandle.open(fileName.c_str(), ios::app);
263                 if(!fileHandle) {
264                         mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
265                         return 1;
266                 }
267                 else {
268                         return 0;
269                 }
270         }
271         catch(exception& e) {
272                 errorOut(e, "MothurOut", "openOutputFileAppend");
273                 exit(1);
274         }
275 }
276 /***********************************************************************/
277 void MothurOut::gobble(istream& f){
278         try {
279                 char d;
280                 while(isspace(d=f.get()))               {;}
281                 f.putback(d);
282         }
283         catch(exception& e) {
284                 errorOut(e, "MothurOut", "gobble");
285                 exit(1);
286         }
287 }
288 /***********************************************************************/
289 void MothurOut::gobble(istringstream& f){
290         try {
291                 char d;
292                 while(isspace(d=f.get()))               {;}
293                 f.putback(d);
294         }
295         catch(exception& e) {
296                 errorOut(e, "MothurOut", "gobble");
297                 exit(1);
298         }
299 }
300
301 /***********************************************************************/
302
303 string MothurOut::getline(istringstream& fileHandle) {
304         try {
305         
306                 string line = "";
307                 
308                 while (!fileHandle.eof())       {
309                         //get next character
310                         char c = fileHandle.get(); 
311                         
312                         //are you at the end of the line
313                         if ((c == '\n') || (c == '\r') || (c == '\f')){  break; }       
314                         else {          line += c;              }
315                 }
316                 
317                 return line;
318                 
319         }
320         catch(exception& e) {
321                 errorOut(e, "MothurOut", "getline");
322                 exit(1);
323         }
324 }
325 /***********************************************************************/
326
327 string MothurOut::getline(ifstream& fileHandle) {
328         try {
329         
330                 string line = "";
331                 
332                 while (!fileHandle.eof())       {
333                         //get next character
334                         char c = fileHandle.get(); 
335                         
336                         //are you at the end of the line
337                         if ((c == '\n') || (c == '\r') || (c == '\f')){  break; }       
338                         else {          line += c;              }
339                 }
340                 
341                 return line;
342                 
343         }
344         catch(exception& e) {
345                 errorOut(e, "MothurOut", "getline");
346                 exit(1);
347         }
348 }
349 /***********************************************************************/
350
351 #ifdef USE_COMPRESSION
352 inline bool endsWith(string s, const char * suffix){
353   size_t suffixLength = strlen(suffix);
354   return s.size() >= suffixLength && s.substr(s.size() - suffixLength, suffixLength).compare(suffix) == 0;
355 }
356 #endif
357
358 string MothurOut::getRootName(string longName){
359         try {
360         
361                 string rootName = longName;
362
363 #ifdef USE_COMPRESSION
364     if (endsWith(rootName, ".gz") || endsWith(rootName, ".bz2")) {
365       int pos = rootName.find_last_of('.');
366       rootName = rootName.substr(0, pos);
367       cerr << "shortening " << longName << " to " << rootName << "\n";
368     }
369 #endif
370
371                 if(rootName.find_last_of(".") != rootName.npos){
372                         int pos = rootName.find_last_of('.')+1;
373                         rootName = rootName.substr(0, pos);
374                 }
375
376                 return rootName;
377         }
378         catch(exception& e) {
379                 errorOut(e, "MothurOut", "getRootName");
380                 exit(1);
381         }
382 }
383 /***********************************************************************/
384
385 string MothurOut::getSimpleName(string longName){
386         try {
387                 string simpleName = longName;
388                 
389                 size_t found;
390                 found=longName.find_last_of("/\\");
391
392                 if(found != longName.npos){
393                         simpleName = longName.substr(found+1);
394                 }
395                 
396                 return simpleName;
397         }
398         catch(exception& e) {
399                 errorOut(e, "MothurOut", "getSimpleName");
400                 exit(1);
401         }
402 }
403
404 /***********************************************************************/
405
406 string MothurOut::getPathName(string longName){
407         try {
408                 string rootPathName = longName;
409                 
410                 if(longName.find_last_of("/\\") != longName.npos){
411                         int pos = longName.find_last_of("/\\")+1;
412                         rootPathName = longName.substr(0, pos);
413                 }
414                 
415                 return rootPathName;
416         }
417         catch(exception& e) {
418                 errorOut(e, "MothurOut", "getPathName");
419                 exit(1);
420         }       
421
422 }
423 /***********************************************************************/
424
425 string MothurOut::hasPath(string longName){
426         try {
427                 string path = "";
428                 
429                 size_t found;
430                 found=longName.find_last_of("~/\\");
431
432                 if(found != longName.npos){
433                         path = longName.substr(0, found+1);
434                 }
435                 
436                 return path;
437         }
438         catch(exception& e) {
439                 errorOut(e, "MothurOut", "hasPath");
440                 exit(1);
441         }       
442 }
443
444 /***********************************************************************/
445
446 string MothurOut::getExtension(string longName){
447         try {
448                 string extension = longName;
449                 
450                 if(longName.find_last_of('.') != longName.npos){
451                         int pos = longName.find_last_of('.');
452                         extension = longName.substr(pos, longName.length());
453                 }
454                 
455                 return extension;
456         }
457         catch(exception& e) {
458                 errorOut(e, "MothurOut", "getExtension");
459                 exit(1);
460         }       
461 }
462 /***********************************************************************/
463 bool MothurOut::isBlank(string fileName){
464         try {
465                 
466                 fileName = getFullPathName(fileName);
467                 
468                 ifstream fileHandle;
469                 fileHandle.open(fileName.c_str());
470                 if(!fileHandle) {
471                         mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
472                         return false;
473                 }else {
474                         //check for blank file
475                         gobble(fileHandle);
476                         if (fileHandle.eof()) { fileHandle.close(); return true;  }
477                 }
478                 return false;
479         }
480         catch(exception& e) {
481                 errorOut(e, "MothurOut", "isBlank");
482                 exit(1);
483         }       
484 }
485 /***********************************************************************/
486
487 string MothurOut::getFullPathName(string fileName){
488         try{
489         
490         string path = hasPath(fileName);
491         string newFileName;
492         int pos;
493         
494         if (path == "") { return fileName; } //its a simple name
495         else { //we need to complete the pathname
496                 // ex. ../../../filename 
497                 // cwd = /user/work/desktop
498                                 
499                 string cwd;
500                 //get current working directory 
501                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)   
502                         
503                         if (path.find("~") != -1) { //go to home directory
504                                 string homeDir = getenv ("HOME");
505                                 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
506                                 return newFileName;
507                         }else { //find path
508                                 if (path.rfind("./") == -1) { return fileName; } //already complete name
509                                 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
510                                 
511                                 char* cwdpath = new char[1024];
512
513                                 size_t size;
514                                 cwdpath=getcwd(cwdpath,size);
515                         
516                                 cwd = cwdpath;
517                                 
518                                 //rip off first '/'
519                                 string simpleCWD;
520                                 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
521                                 
522                                 //break apart the current working directory
523                                 vector<string> dirs;
524                                 while (simpleCWD.find_first_of('/') != -1) {
525                                         string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
526                                         simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
527                                         dirs.push_back(dir);
528                                 }
529                                 //get last one              // ex. ../../../filename = /user/work/desktop/filename
530                                 dirs.push_back(simpleCWD);  //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
531                                 
532                         
533                                 int index = dirs.size()-1;
534                 
535                                 while((pos = path.rfind("./")) != -1) { //while you don't have a complete path
536                                         if (pos == 0) { break;  //you are at the end
537                                         }else if (path[(pos-1)] == '.') { //you want your parent directory ../
538                                                 path = path.substr(0, pos-1);
539                                                 index--;
540                                                 if (index == 0) {  break; }
541                                         }else if (path[(pos-1)] == '/') { //you want the current working dir ./
542                                                 path = path.substr(0, pos);
543                                         }else if (pos == 1) { break;  //you are at the end
544                                         }else { cout << "cannot resolve path for " <<  fileName << endl; return fileName; }
545                                 }
546                         
547                                 for (int i = index; i >= 0; i--) {
548                                         newFileName = dirs[i] +  "/" + newFileName;             
549                                 }
550                                 
551                                 newFileName =  "/" +  newFileName;
552                                 return newFileName;
553                         }       
554                 #else
555                         if (path.find("~") != -1) { //go to home directory
556                                 string homeDir = getenv ("HOMEPATH");
557                                 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
558                                 return newFileName;
559                         }else { //find path
560                                 if (path.rfind(".\\") == -1) { return fileName; } //already complete name
561                                 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
562                                                         
563                                 char *cwdpath = NULL;
564                                 cwdpath = getcwd(NULL, 0); // or _getcwd
565                                 if ( cwdpath != NULL) { cwd = cwdpath; }
566                                 else { cwd = "";  }
567                                 
568                                 //break apart the current working directory
569                                 vector<string> dirs;
570                                 while (cwd.find_first_of('\\') != -1) {
571                                         string dir = cwd.substr(0,cwd.find_first_of('\\'));
572                                         cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
573                                         dirs.push_back(dir);
574                 
575                                 }
576                                 //get last one
577                                 dirs.push_back(cwd);  //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
578                                         
579                                 int index = dirs.size()-1;
580                                         
581                                 while((pos = path.rfind(".\\")) != -1) { //while you don't have a complete path
582                                         if (pos == 0) { break;  //you are at the end
583                                         }else if (path[(pos-1)] == '.') { //you want your parent directory ../
584                                                 path = path.substr(0, pos-1);
585                                                 index--;
586                                                 if (index == 0) {  break; }
587                                         }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
588                                                 path = path.substr(0, pos);
589                                         }else if (pos == 1) { break;  //you are at the end
590                                         }else { cout << "cannot resolve path for " <<  fileName << endl; return fileName; }
591                                 }
592                         
593                                 for (int i = index; i >= 0; i--) {
594                                         newFileName = dirs[i] +  "\\" + newFileName;            
595                                 }
596                                 
597                                 return newFileName;
598                         }
599                         
600                 #endif
601         }
602         }
603         catch(exception& e) {
604                 errorOut(e, "MothurOut", "getFullPathName");
605                 exit(1);
606         }       
607 }
608 /***********************************************************************/
609
610 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
611         try {
612                         //get full path name
613                         string completeFileName = getFullPathName(fileName);
614
615 #ifdef USE_COMPRESSION
616       // check for gzipped or bzipped file
617       if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
618         string tempName = string(tmpnam(0));
619         mkfifo(tempName.c_str(), 0666);
620         int fork_result = fork();
621         if (fork_result < 0) {
622           cerr << "Error forking.\n";
623           exit(1);
624         } else if (fork_result == 0) {
625           string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
626           cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
627           system(command.c_str());
628           cerr << "Done decompressing " << completeFileName << "\n";
629           remove(tempName.c_str());
630           exit(EXIT_SUCCESS);
631         } else {
632           cerr << "waiting on child process " << fork_result << "\n";
633           completeFileName = tempName;
634         }
635       }
636 #endif
637
638                         fileHandle.open(completeFileName.c_str());
639                         if(!fileHandle) {
640                                 return 1;
641                         }else {
642                                 //check for blank file
643                                 gobble(fileHandle);
644                                 return 0;
645                         }
646         }
647         catch(exception& e) {
648                 errorOut(e, "MothurOut", "openInputFile - no Error");
649                 exit(1);
650         }
651 }
652 /***********************************************************************/
653
654 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
655         try {
656
657                 //get full path name
658                 string completeFileName = getFullPathName(fileName);
659
660 #ifdef USE_COMPRESSION
661   // check for gzipped or bzipped file
662   if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
663     string tempName = string(tmpnam(0));
664     mkfifo(tempName.c_str(), 0666);
665     int fork_result = fork();
666     if (fork_result < 0) {
667       cerr << "Error forking.\n";
668       exit(1);
669     } else if (fork_result == 0) {
670       string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
671       cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
672       system(command.c_str());
673       cerr << "Done decompressing " << completeFileName << "\n";
674       remove(tempName.c_str());
675       exit(EXIT_SUCCESS);
676     } else {
677       cerr << "waiting on child process " << fork_result << "\n";
678       completeFileName = tempName;
679     }
680   }
681 #endif
682
683
684                 fileHandle.open(completeFileName.c_str());
685                 if(!fileHandle) {
686                         mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
687                         return 1;
688                 }
689                 else {
690                         //check for blank file
691                         gobble(fileHandle);
692                         if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine();  }
693                         
694                         return 0;
695                 }
696         }
697         catch(exception& e) {
698                 errorOut(e, "MothurOut", "openInputFile");
699                 exit(1);
700         }       
701 }
702 /***********************************************************************/
703
704 int MothurOut::renameFile(string oldName, string newName){
705         try {
706                 ifstream inTest;
707                 int exist = openInputFile(newName, inTest, "");
708                 
709         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)           
710                 if (exist == 0) { //you could open it so you want to delete it
711                         inTest.close();
712                         string command = "rm " + newName;
713                         system(command.c_str());
714                 }
715                                 
716                 string command = "mv " + oldName + " " + newName;
717                 system(command.c_str());
718         #else
719                 remove(newName.c_str());
720                 int renameOk = rename(oldName.c_str(), newName.c_str());
721         #endif
722                 return 0;
723                 
724         }
725         catch(exception& e) {
726                 errorOut(e, "MothurOut", "renameFile");
727                 exit(1);
728         }       
729 }
730
731 /***********************************************************************/
732
733 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
734         try { 
735         
736                 string completeFileName = getFullPathName(fileName);
737
738 #ifdef USE_COMPRESSION
739     // check for gzipped file
740     if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
741       string tempName = string(tmpnam(0));
742       mkfifo(tempName.c_str(), 0666);
743       cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
744       int fork_result = fork();
745       if (fork_result < 0) {
746         cerr << "Error forking.\n";
747         exit(1);
748       } else if (fork_result == 0) {
749         string command = string(endsWith(completeFileName, ".gz") ?  "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
750         system(command.c_str());
751         exit(0);
752       } else {
753         completeFileName = tempName;
754       }
755     }
756 #endif
757
758                 fileHandle.open(completeFileName.c_str(), ios::trunc);
759                 if(!fileHandle) {
760                         mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
761                         return 1;
762                 }
763                 else {
764                         return 0;
765                 }
766         }
767         catch(exception& e) {
768                 errorOut(e, "MothurOut", "openOutputFile");
769                 exit(1);
770         }       
771
772 }
773
774 /**************************************************************************************************/
775 void MothurOut::appendFiles(string temp, string filename) {
776         try{
777                 ofstream output;
778                 ifstream input;
779         
780                 //open output file in append mode
781                 openOutputFileAppend(filename, output);
782                 int ableToOpen = openInputFile(temp, input, "no error");
783                 
784                 if (ableToOpen == 0) { //you opened it
785                         while(char c = input.get()){
786                                 if(input.eof())         {       break;                  }
787                                 else                            {       output << c;    }
788                         }
789                         input.close();
790                 }
791                 
792                 output.close();
793         }
794         catch(exception& e) {
795                 errorOut(e, "MothurOut", "appendFiles");
796                 exit(1);
797         }       
798 }
799
800 /**************************************************************************************************/
801 string MothurOut::sortFile(string distFile, string outputDir){
802         try {   
803         
804                 //if (outputDir == "") {  outputDir += hasPath(distFile);  }
805                 string outfile = getRootName(distFile) + "sorted.dist";
806
807                 
808                 //if you can, use the unix sort since its been optimized for years
809                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
810                         string command = "sort -n -k +3 " + distFile + " -o " + outfile;
811                         system(command.c_str());
812                 #else //you are stuck with my best attempt...
813                         //windows sort does not have a way to specify a column, only a character in the line
814                         //since we cannot assume that the distance will always be at the the same character location on each line
815                         //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
816                 
817                         //read in file line by file and put distance first
818                         string tempDistFile = distFile + ".temp";
819                         ifstream input;
820                         ofstream output;
821                         openInputFile(distFile, input);
822                         openOutputFile(tempDistFile, output);
823
824                         string firstName, secondName;
825                         float dist;
826                         while (input) {
827                                 input >> firstName >> secondName >> dist;
828                                 output << dist << '\t' << firstName << '\t' << secondName << endl;
829                                 gobble(input);
830                         }
831                         input.close();
832                         output.close();
833                 
834         
835                         //sort using windows sort
836                         string tempOutfile = outfile + ".temp";
837                         string command = "sort " + tempDistFile + " /O " + tempOutfile;
838                         system(command.c_str());
839                 
840                         //read in sorted file and put distance at end again
841                         ifstream input2;
842                         openInputFile(tempOutfile, input2);
843                         openOutputFile(outfile, output);
844                 
845                         while (input2) {
846                                 input2 >> dist >> firstName >> secondName;
847                                 output << firstName << '\t' << secondName << '\t' << dist << endl;
848                                 gobble(input2);
849                         }
850                         input2.close();
851                         output.close();
852                 
853                         //remove temp files
854                         remove(tempDistFile.c_str());
855                         remove(tempOutfile.c_str());
856                 #endif
857                 
858                 return outfile;
859         }
860         catch(exception& e) {
861                 errorOut(e, "MothurOut", "sortFile");
862                 exit(1);
863         }       
864 }
865 /**************************************************************************************************/
866 vector<unsigned long int> MothurOut::setFilePosFasta(string filename, int& num) {
867         try {
868                         vector<unsigned long int> positions;
869                         ifstream inFASTA;
870                         openInputFile(filename, inFASTA);
871                                                 
872                         string input;
873                         while(!inFASTA.eof()){
874                                 input = getline(inFASTA); 
875                                 if (input.length() != 0) {
876                                         if(input[0] == '>'){    unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
877                                 }
878                                 gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
879                         }
880                         inFASTA.close();
881                 
882                         num = positions.size();
883                 
884                         /*FILE * pFile;
885                         long size;
886                 
887                         //get num bytes in file
888                         pFile = fopen (filename.c_str(),"rb");
889                         if (pFile==NULL) perror ("Error opening file");
890                         else{
891                                 fseek (pFile, 0, SEEK_END);
892                                 size=ftell (pFile);
893                                 fclose (pFile);
894                         }*/
895                         
896                         unsigned long int size = positions[(positions.size()-1)];
897                         ifstream in;
898                         openInputFile(filename, in);
899                         
900                         in.seekg(size);
901                 
902                         while(char c = in.get()){
903                                 if(in.eof())            {       break;  }
904                                 else                            {       size++; }
905                         }
906                         in.close();
907                 
908                         positions.push_back(size);
909                 
910                         return positions;
911         }
912         catch(exception& e) {
913                 errorOut(e, "MothurOut", "setFilePosFasta");
914                 exit(1);
915         }
916 }
917 /**************************************************************************************************/
918 vector<unsigned long int> MothurOut::setFilePosEachLine(string filename, int& num) {
919         try {
920                         filename = getFullPathName(filename);
921                         
922                         vector<unsigned long int> positions;
923                         ifstream in;
924                         openInputFile(filename, in);
925                                 
926                         string input;
927                         while(!in.eof()){
928                                 unsigned long int lastpos = in.tellg();
929                                 input = getline(in); 
930                                 if (input.length() != 0) {
931                                         unsigned long int pos = in.tellg(); 
932                                         if (pos != -1) { positions.push_back(pos - input.length() - 1); }
933                                         else {  positions.push_back(lastpos);  }
934                                 }
935                                 gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
936                         }
937                         in.close();
938                 
939                         num = positions.size();
940                 
941                         FILE * pFile;
942                         unsigned long int size;
943                         
944                         //get num bytes in file
945                         pFile = fopen (filename.c_str(),"rb");
946                         if (pFile==NULL) perror ("Error opening file");
947                         else{
948                                 fseek (pFile, 0, SEEK_END);
949                                 size=ftell (pFile);
950                                 fclose (pFile);
951                         }
952                 
953                         positions.push_back(size);
954                 
955                         return positions;
956         }
957         catch(exception& e) {
958                 errorOut(e, "MothurOut", "setFilePosEachLine");
959                 exit(1);
960         }
961 }
962 /**************************************************************************************************/
963
964 vector<unsigned long int> MothurOut::divideFile(string filename, int& proc) {
965         try{
966         
967                 vector<unsigned long int> filePos;
968                 filePos.push_back(0);
969                 
970                 FILE * pFile;
971                 unsigned long int size;
972                 
973                 filename = getFullPathName(filename);
974                 
975                 //get num bytes in file
976                 pFile = fopen (filename.c_str(),"rb");
977                 if (pFile==NULL) perror ("Error opening file");
978                 else{
979                         fseek (pFile, 0, SEEK_END);
980                         size=ftell (pFile);
981                         fclose (pFile);
982                 }
983         
984                 //estimate file breaks
985                 unsigned long int chunkSize = 0;
986                 chunkSize = size / proc;
987         
988                 //file to small to divide by processors
989                 if (chunkSize == 0)  {  proc = 1;       filePos.push_back(size); return filePos;        }
990         
991                 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
992                 for (int i = 0; i < proc; i++) {
993                         unsigned long int spot = (i+1) * chunkSize;
994                         
995                         ifstream in;
996                         openInputFile(filename, in);
997                         in.seekg(spot);
998                         
999                         //look for next '>'
1000                         unsigned long int newSpot = spot;
1001                         while (!in.eof()) {
1002                            char c = in.get();
1003                            if (c == '>') {   in.putback(c); newSpot = in.tellg(); break;  }
1004                         }
1005                         
1006                         //there was not another sequence before the end of the file
1007                         unsigned long int sanityPos = in.tellg();
1008
1009                         if (sanityPos == -1) {  break;  }
1010                         else {  filePos.push_back(newSpot);  }
1011                         
1012                         in.close();
1013                 }
1014                 
1015                 //save end pos
1016                 filePos.push_back(size);
1017
1018                 //sanity check filePos
1019                 for (int i = 0; i < (filePos.size()-1); i++) {
1020                         if (filePos[(i+1)] <= filePos[i]) {  filePos.erase(filePos.begin()+(i+1)); i--; }
1021                 }
1022
1023                 proc = (filePos.size() - 1);
1024                 
1025                 return filePos;
1026         }
1027         catch(exception& e) {
1028                 errorOut(e, "MothurOut", "divideFile");
1029                 exit(1);
1030         }
1031 }
1032
1033 /***********************************************************************/
1034
1035 bool MothurOut::isTrue(string f){
1036         try {
1037                 
1038                 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1039                 
1040                 if ((f == "TRUE") || (f == "T")) {      return true;    }
1041                 else {  return false;  }
1042         }
1043         catch(exception& e) {
1044                 errorOut(e, "MothurOut", "isTrue");
1045                 exit(1);
1046         }
1047 }
1048
1049 /***********************************************************************/
1050
1051 float MothurOut::roundDist(float dist, int precision){
1052         try {
1053                 return int(dist * precision + 0.5)/float(precision);
1054         }
1055         catch(exception& e) {
1056                 errorOut(e, "MothurOut", "roundDist");
1057                 exit(1);
1058         }
1059 }
1060 /***********************************************************************/
1061
1062 float MothurOut::ceilDist(float dist, int precision){
1063         try {
1064                 return int(ceil(dist * precision))/float(precision);
1065         }
1066         catch(exception& e) {
1067                 errorOut(e, "MothurOut", "ceilDist");
1068                 exit(1);
1069         }
1070 }
1071
1072 /***********************************************************************/
1073
1074 int MothurOut::getNumNames(string names){
1075         try {
1076                 int count = 0;
1077                 
1078                 if(names != ""){
1079                         count = 1;
1080                         for(int i=0;i<names.size();i++){
1081                                 if(names[i] == ','){
1082                                         count++;
1083                                 }
1084                         }
1085                 }
1086                 
1087                 return count;
1088         }
1089         catch(exception& e) {
1090                 errorOut(e, "MothurOut", "getNumNames");
1091                 exit(1);
1092         }
1093 }
1094
1095 /**************************************************************************************************/
1096
1097 vector<vector<double> > MothurOut::binomial(int maxOrder){
1098         try {
1099         vector<vector<double> > binomial(maxOrder+1);
1100         
1101     for(int i=0;i<=maxOrder;i++){
1102                 binomial[i].resize(maxOrder+1);
1103                 binomial[i][0]=1;
1104                 binomial[0][i]=0;
1105     }
1106     binomial[0][0]=1;
1107         
1108     binomial[1][0]=1;
1109     binomial[1][1]=1;
1110         
1111     for(int i=2;i<=maxOrder;i++){
1112                 binomial[1][i]=0;
1113     }
1114         
1115     for(int i=2;i<=maxOrder;i++){
1116                 for(int j=1;j<=maxOrder;j++){
1117                         if(i==j){       binomial[i][j]=1;                                                                       }
1118                         if(j>i) {       binomial[i][j]=0;                                                                       }
1119                         else    {       binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j];     }
1120                 }
1121     }
1122         
1123         return binomial;
1124         
1125         }
1126         catch(exception& e) {
1127                 errorOut(e, "MothurOut", "binomial");
1128                 exit(1);
1129         }
1130 }
1131
1132 /***********************************************************************/
1133
1134 int MothurOut::factorial(int num){
1135         try {
1136                 int total = 1;
1137                 
1138                 for (int i = 1; i <= num; i++) {
1139                         total *= i;
1140                 }
1141                 
1142                 return total;
1143         }
1144         catch(exception& e) {
1145                 errorOut(e, "MothurOut", "factorial");
1146                 exit(1);
1147         }
1148 }
1149 /***********************************************************************/
1150
1151 int MothurOut::getNumSeqs(ifstream& file){
1152         try {
1153                 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1154                 file.seekg(0);
1155                 return numSeqs;
1156         }
1157         catch(exception& e) {
1158                 errorOut(e, "MothurOut", "getNumSeqs");
1159                 exit(1);
1160         }       
1161 }
1162 /***********************************************************************/
1163 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1164         try {
1165                 string input;
1166                 numSeqs = 0;
1167                 while(!file.eof()){
1168                         input = getline(file);
1169                         if (input.length() != 0) {
1170                                 if(input[0] == '>'){ numSeqs++; }
1171                         }
1172                 }
1173         }
1174         catch(exception& e) {
1175                 errorOut(e, "MothurOut", "getNumSeqs");
1176                 exit(1);
1177         }       
1178 }
1179 /***********************************************************************/
1180
1181 //This function parses the estimator options and puts them in a vector
1182 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1183         try {
1184                 string individual;
1185                 
1186                 while (estim.find_first_of(symbol) != -1) {
1187                         individual = estim.substr(0,estim.find_first_of(symbol));
1188                         if ((estim.find_first_of(symbol)+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1189                                 estim = estim.substr(estim.find_first_of(symbol)+1, estim.length());
1190                                 container.push_back(individual);
1191                         }
1192                 }
1193                 //get last one
1194                 container.push_back(estim);
1195         }
1196         catch(exception& e) {
1197                 errorOut(e, "MothurOut", "splitAtChar");
1198                 exit(1);
1199         }       
1200 }
1201
1202 /***********************************************************************/
1203
1204 //This function parses the estimator options and puts them in a vector
1205 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1206         try {
1207                 string individual = "";
1208                 int estimLength = estim.size();
1209                 for(int i=0;i<estimLength;i++){
1210                         if(estim[i] == '-'){
1211                                 container.push_back(individual);
1212                                 individual = "";                                
1213                         }
1214                         else{
1215                                 individual += estim[i];
1216                         }
1217                 }
1218                 container.push_back(individual);
1219
1220         
1221         /*      string individual;
1222                 
1223                 while (estim.find_first_of('-') != -1) {
1224                         individual = estim.substr(0,estim.find_first_of('-'));
1225                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1226                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1227                                 container.push_back(individual);
1228                         }
1229                 }
1230                 //get last one
1231                 container.push_back(estim); */
1232         }
1233         catch(exception& e) {
1234                 errorOut(e, "MothurOut", "splitAtDash");
1235                 exit(1);
1236         }       
1237 }
1238
1239 /***********************************************************************/
1240 //This function parses the label options and puts them in a set
1241 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1242         try {
1243                 string individual = "";
1244                 int estimLength = estim.size();
1245                 for(int i=0;i<estimLength;i++){
1246                         if(estim[i] == '-'){
1247                                 container.insert(individual);
1248                                 individual = "";                                
1249                         }
1250                         else{
1251                                 individual += estim[i];
1252                         }
1253                 }
1254                 container.insert(individual);
1255
1256         //      string individual;
1257                 
1258         //      while (estim.find_first_of('-') != -1) {
1259         //              individual = estim.substr(0,estim.find_first_of('-'));
1260         //              if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1261         //                      estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1262         //                      container.insert(individual);
1263         //              }
1264         //      }
1265                 //get last one
1266         //      container.insert(estim);
1267         
1268         }
1269         catch(exception& e) {
1270                 errorOut(e, "MothurOut", "splitAtDash");
1271                 exit(1);
1272         }       
1273 }
1274 /***********************************************************************/
1275 //This function parses the line options and puts them in a set
1276 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1277         try {
1278                 string individual;
1279                 int lineNum;
1280                 
1281                 while (estim.find_first_of('-') != -1) {
1282                         individual = estim.substr(0,estim.find_first_of('-'));
1283                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1284                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1285                                 convert(individual, lineNum); //convert the string to int
1286                                 container.insert(lineNum);
1287                         }
1288                 }
1289                 //get last one
1290                 convert(estim, lineNum); //convert the string to int
1291                 container.insert(lineNum);
1292         }
1293         catch(exception& e) {
1294                 errorOut(e, "MothurOut", "splitAtDash");
1295                 exit(1);
1296         }       
1297 }
1298 /***********************************************************************/
1299 //This function parses the a string and puts peices in a vector
1300 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1301         try {
1302                 string individual = "";
1303                 int estimLength = estim.size();
1304                 for(int i=0;i<estimLength;i++){
1305                         if(estim[i] == ','){
1306                                 container.push_back(individual);
1307                                 individual = "";                                
1308                         }
1309                         else{
1310                                 individual += estim[i];
1311                         }
1312                 }
1313                 container.push_back(individual);
1314                 
1315                 
1316                 
1317                 
1318 //              string individual;
1319 //              
1320 //              while (estim.find_first_of(',') != -1) {
1321 //                      individual = estim.substr(0,estim.find_first_of(','));
1322 //                      if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1323 //                              estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1324 //                              container.push_back(individual);
1325 //                      }
1326 //              }
1327 //              //get last one
1328 //              container.push_back(estim);
1329         }
1330         catch(exception& e) {
1331                 errorOut(e, "MothurOut", "splitAtComma");
1332                 exit(1);
1333         }       
1334 }
1335 /***********************************************************************/
1336
1337 //This function splits up the various option parameters
1338 void MothurOut::splitAtComma(string& prefix, string& suffix){
1339         try {
1340                 prefix = suffix.substr(0,suffix.find_first_of(','));
1341                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
1342                         suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1343                         string space = " ";
1344                         while(suffix.at(0) == ' ')
1345                                 suffix = suffix.substr(1, suffix.length());
1346                 }
1347
1348         }
1349         catch(exception& e) {
1350                 errorOut(e, "MothurOut", "splitAtComma");
1351                 exit(1);
1352         }       
1353 }
1354 /***********************************************************************/
1355
1356 //This function separates the key value from the option value i.e. dist=96_...
1357 void MothurOut::splitAtEquals(string& key, string& value){              
1358         try {
1359                 if(value.find_first_of('=') != -1){
1360                         key = value.substr(0,value.find_first_of('='));
1361                         if ((value.find_first_of('=')+1) <= value.length()) {
1362                                 value = value.substr(value.find_first_of('=')+1, value.length());
1363                         }
1364                 }else{
1365                         key = value;
1366                         value = 1;
1367                 }
1368         }
1369         catch(exception& e) {
1370                 errorOut(e, "MothurOut", "splitAtEquals");
1371                 exit(1);
1372         }       
1373 }
1374
1375 /**************************************************************************************************/
1376
1377 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1378         try {
1379                 for (int i = 0; i < Groups.size(); i++) {
1380                         if (groupname == Groups[i]) { return true; }
1381                 }
1382                 return false;
1383         }
1384         catch(exception& e) {
1385                 errorOut(e, "MothurOut", "inUsersGroups");
1386                 exit(1);
1387         }       
1388 }
1389 /**************************************************************************************************/
1390 //returns true if any of the strings in first vector are in second vector
1391 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1392         try {
1393                 
1394                 for (int i = 0; i < groupnames.size(); i++) {
1395                         if (inUsersGroups(groupnames[i], Groups)) { return true; }
1396                 }
1397                 return false;
1398         }
1399         catch(exception& e) {
1400                 errorOut(e, "MothurOut", "inUsersGroups");
1401                 exit(1);
1402         }       
1403 }
1404 /***********************************************************************/
1405 //this function determines if the user has given us labels that are smaller than the given label.
1406 //if so then it returns true so that the calling function can run the previous valid distance.
1407 //it's a "smart" distance function.  It also checks for invalid labels.
1408 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1409         try {
1410                 
1411                 set<string>::iterator it;
1412                 vector<float> orderFloat;
1413                 map<string, float> userMap;  //the conversion process removes trailing 0's which we need to put back
1414                 map<string, float>::iterator it2;
1415                 float labelFloat;
1416                 bool smaller = false;
1417                 
1418                 //unique is the smallest line
1419                 if (label == "unique") {  return false;  }
1420                 else { 
1421                         if (convertTestFloat(label, labelFloat)) {
1422                                 convert(label, labelFloat); 
1423                         }else { //cant convert 
1424                                 return false;
1425                         }
1426                 }
1427                 
1428                 //go through users set and make them floats
1429                 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
1430                         
1431                         float temp;
1432                         if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1433                                 convert(*it, temp);
1434                                 orderFloat.push_back(temp);
1435                                 userMap[*it] = temp;
1436                         }else if (*it == "unique") { 
1437                                 orderFloat.push_back(-1.0);
1438                                 userMap["unique"] = -1.0;
1439                         }else {
1440                                 if (errorOff == "") {  cout << *it << " is not a valid label." << endl;  }
1441                                 userLabels.erase(*it); 
1442                                 it--;
1443                         }
1444                 }
1445                 
1446                 //sort order
1447                 sort(orderFloat.begin(), orderFloat.end());
1448                 
1449                 /*************************************************/
1450                 //is this label bigger than any of the users labels
1451                 /*************************************************/
1452                                 
1453                 //loop through order until you find a label greater than label
1454                 for (int i = 0; i < orderFloat.size(); i++) {
1455                         if (orderFloat[i] < labelFloat) {
1456                                 smaller = true;
1457                                 if (orderFloat[i] == -1) { 
1458                                         if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
1459                                         userLabels.erase("unique");
1460                                 }
1461                                 else {  
1462                                         if (errorOff == "") { cout << "Your file does not include the label " << endl; }
1463                                         string s = "";
1464                                         for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {  
1465                                                 if (it2->second == orderFloat[i]) {  
1466                                                         s = it2->first;  
1467                                                         //remove small labels
1468                                                         userLabels.erase(s);
1469                                                         break;
1470                                                 }
1471                                         }
1472                                         if (errorOff == "") {cout << s <<  ". I will use the next smallest distance. " << endl; }
1473                                 }
1474                         //since they are sorted once you find a bigger one stop looking
1475                         }else { break; }
1476                 }
1477                 
1478                 return smaller;
1479                                                 
1480         }
1481         catch(exception& e) {
1482                 errorOut(e, "MothurOut", "anyLabelsToProcess");
1483                 exit(1);
1484         }       
1485 }
1486
1487 /**************************************************************************************************/
1488 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1489         try {
1490                 
1491                 bool good = true;
1492                 
1493                 string line = getline(file);  
1494
1495                 //before we added this check
1496                 if (line[0] != '#') {  good = false;  }
1497                 else {
1498                         //rip off #
1499                         line = line.substr(1);
1500                         
1501                         vector<string> versionVector;
1502                         splitAtChar(version, versionVector, '.');
1503                         
1504                         //check file version
1505                         vector<string> linesVector;
1506                         splitAtChar(line, linesVector, '.');
1507                         
1508                         if (versionVector.size() != linesVector.size()) { good = false; }
1509                         else {
1510                                 for (int j = 0; j < versionVector.size(); j++) {
1511                                         int num1, num2;
1512                                         convert(versionVector[j], num1);
1513                                         convert(linesVector[j], num2);
1514                                         
1515                                         //if mothurs version is newer than this files version, then we want to remake it
1516                                         if (num1 > num2) {  good = false; break;  }
1517                                 }
1518                         }
1519                         
1520                 }
1521                 
1522                 if (!good) {  file.close();  }
1523                 else { file.seekg(0);  }
1524                 
1525                 return good;
1526         }
1527         catch(exception& e) {
1528                 errorOut(e, "MothurOut", "checkReleaseVersion");                
1529                 exit(1);
1530         }
1531 }
1532 /**************************************************************************************************/
1533
1534
1535
1536
1537