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