]> git.donarmstrong.com Git - mothur.git/blob - mothurout.cpp
added pipeline commands which involved change to command factory and command class...
[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("./") == -1) { 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('/') != -1) {
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("./")) != -1) { //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("~") != -1) { //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(".\\") == -1) { 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(".\\")) != -1) { //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                                 return 1;
662                         }else {
663                                 //check for blank file
664                                 gobble(fileHandle);
665                                 return 0;
666                         }
667         }
668         catch(exception& e) {
669                 errorOut(e, "MothurOut", "openInputFile - no Error");
670                 exit(1);
671         }
672 }
673 /***********************************************************************/
674
675 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
676         try {
677
678                 //get full path name
679                 string completeFileName = getFullPathName(fileName);
680
681 #ifdef USE_COMPRESSION
682   // check for gzipped or bzipped file
683   if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
684     string tempName = string(tmpnam(0));
685     mkfifo(tempName.c_str(), 0666);
686     int fork_result = fork();
687     if (fork_result < 0) {
688       cerr << "Error forking.\n";
689       exit(1);
690     } else if (fork_result == 0) {
691       string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
692       cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
693       system(command.c_str());
694       cerr << "Done decompressing " << completeFileName << "\n";
695       remove(tempName.c_str());
696       exit(EXIT_SUCCESS);
697     } else {
698       cerr << "waiting on child process " << fork_result << "\n";
699       completeFileName = tempName;
700     }
701   }
702 #endif
703
704
705                 fileHandle.open(completeFileName.c_str());
706                 if(!fileHandle) {
707                         mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
708                         return 1;
709                 }
710                 else {
711                         //check for blank file
712                         gobble(fileHandle);
713                         if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine();  }
714                         
715                         return 0;
716                 }
717         }
718         catch(exception& e) {
719                 errorOut(e, "MothurOut", "openInputFile");
720                 exit(1);
721         }       
722 }
723 /***********************************************************************/
724
725 int MothurOut::renameFile(string oldName, string newName){
726         try {
727                 ifstream inTest;
728                 int exist = openInputFile(newName, inTest, "");
729                 
730         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)           
731                 if (exist == 0) { //you could open it so you want to delete it
732                         inTest.close();
733                         string command = "rm " + newName;
734                         system(command.c_str());
735                 }
736                                 
737                 string command = "mv " + oldName + " " + newName;
738                 system(command.c_str());
739         #else
740                 remove(newName.c_str());
741                 int renameOk = rename(oldName.c_str(), newName.c_str());
742         #endif
743                 return 0;
744                 
745         }
746         catch(exception& e) {
747                 errorOut(e, "MothurOut", "renameFile");
748                 exit(1);
749         }       
750 }
751
752 /***********************************************************************/
753
754 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
755         try { 
756         
757                 string completeFileName = getFullPathName(fileName);
758
759 #ifdef USE_COMPRESSION
760     // check for gzipped file
761     if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
762       string tempName = string(tmpnam(0));
763       mkfifo(tempName.c_str(), 0666);
764       cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
765       int fork_result = fork();
766       if (fork_result < 0) {
767         cerr << "Error forking.\n";
768         exit(1);
769       } else if (fork_result == 0) {
770         string command = string(endsWith(completeFileName, ".gz") ?  "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
771         system(command.c_str());
772         exit(0);
773       } else {
774         completeFileName = tempName;
775       }
776     }
777 #endif
778
779                 fileHandle.open(completeFileName.c_str(), ios::trunc);
780                 if(!fileHandle) {
781                         mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
782                         return 1;
783                 }
784                 else {
785                         return 0;
786                 }
787         }
788         catch(exception& e) {
789                 errorOut(e, "MothurOut", "openOutputFile");
790                 exit(1);
791         }       
792
793 }
794
795 /**************************************************************************************************/
796 void MothurOut::appendFiles(string temp, string filename) {
797         try{
798                 ofstream output;
799                 ifstream input;
800         
801                 //open output file in append mode
802                 openOutputFileAppend(filename, output);
803                 int ableToOpen = openInputFile(temp, input, "no error");
804                 
805                 if (ableToOpen == 0) { //you opened it
806                         while(char c = input.get()){
807                                 if(input.eof())         {       break;                  }
808                                 else                            {       output << c;    }
809                         }
810                         input.close();
811                 }
812                 
813                 output.close();
814         }
815         catch(exception& e) {
816                 errorOut(e, "MothurOut", "appendFiles");
817                 exit(1);
818         }       
819 }
820
821 /**************************************************************************************************/
822 string MothurOut::sortFile(string distFile, string outputDir){
823         try {   
824         
825                 //if (outputDir == "") {  outputDir += hasPath(distFile);  }
826                 string outfile = getRootName(distFile) + "sorted.dist";
827
828                 
829                 //if you can, use the unix sort since its been optimized for years
830                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
831                         string command = "sort -n -k +3 " + distFile + " -o " + outfile;
832                         system(command.c_str());
833                 #else //you are stuck with my best attempt...
834                         //windows sort does not have a way to specify a column, only a character in the line
835                         //since we cannot assume that the distance will always be at the the same character location on each line
836                         //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
837                 
838                         //read in file line by file and put distance first
839                         string tempDistFile = distFile + ".temp";
840                         ifstream input;
841                         ofstream output;
842                         openInputFile(distFile, input);
843                         openOutputFile(tempDistFile, output);
844
845                         string firstName, secondName;
846                         float dist;
847                         while (input) {
848                                 input >> firstName >> secondName >> dist;
849                                 output << dist << '\t' << firstName << '\t' << secondName << endl;
850                                 gobble(input);
851                         }
852                         input.close();
853                         output.close();
854                 
855         
856                         //sort using windows sort
857                         string tempOutfile = outfile + ".temp";
858                         string command = "sort " + tempDistFile + " /O " + tempOutfile;
859                         system(command.c_str());
860                 
861                         //read in sorted file and put distance at end again
862                         ifstream input2;
863                         openInputFile(tempOutfile, input2);
864                         openOutputFile(outfile, output);
865                 
866                         while (input2) {
867                                 input2 >> dist >> firstName >> secondName;
868                                 output << firstName << '\t' << secondName << '\t' << dist << endl;
869                                 gobble(input2);
870                         }
871                         input2.close();
872                         output.close();
873                 
874                         //remove temp files
875                         remove(tempDistFile.c_str());
876                         remove(tempOutfile.c_str());
877                 #endif
878                 
879                 return outfile;
880         }
881         catch(exception& e) {
882                 errorOut(e, "MothurOut", "sortFile");
883                 exit(1);
884         }       
885 }
886 /**************************************************************************************************/
887 vector<unsigned long int> MothurOut::setFilePosFasta(string filename, int& num) {
888         try {
889                         vector<unsigned long int> positions;
890                         ifstream inFASTA;
891                         openInputFile(filename, inFASTA);
892                                                 
893                         string input;
894                         while(!inFASTA.eof()){
895                                 input = getline(inFASTA); 
896                                 if (input.length() != 0) {
897                                         if(input[0] == '>'){    unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
898                                 }
899                                 gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
900                         }
901                         inFASTA.close();
902                 
903                         num = positions.size();
904                 
905                         /*FILE * pFile;
906                         long size;
907                 
908                         //get num bytes in file
909                         pFile = fopen (filename.c_str(),"rb");
910                         if (pFile==NULL) perror ("Error opening file");
911                         else{
912                                 fseek (pFile, 0, SEEK_END);
913                                 size=ftell (pFile);
914                                 fclose (pFile);
915                         }*/
916                         
917                         unsigned long int size = positions[(positions.size()-1)];
918                         ifstream in;
919                         openInputFile(filename, in);
920                         
921                         in.seekg(size);
922                 
923                         while(char c = in.get()){
924                                 if(in.eof())            {       break;  }
925                                 else                            {       size++; }
926                         }
927                         in.close();
928                 
929                         positions.push_back(size);
930                 
931                         return positions;
932         }
933         catch(exception& e) {
934                 errorOut(e, "MothurOut", "setFilePosFasta");
935                 exit(1);
936         }
937 }
938 /**************************************************************************************************/
939 vector<unsigned long int> MothurOut::setFilePosEachLine(string filename, int& num) {
940         try {
941                         filename = getFullPathName(filename);
942                         
943                         vector<unsigned long int> positions;
944                         ifstream in;
945                         openInputFile(filename, in);
946                                 
947                         string input;
948                         while(!in.eof()){
949                                 unsigned long int lastpos = in.tellg();
950                                 input = getline(in); 
951                                 if (input.length() != 0) {
952                                         unsigned long int pos = in.tellg(); 
953                                         if (pos != -1) { positions.push_back(pos - input.length() - 1); }
954                                         else {  positions.push_back(lastpos);  }
955                                 }
956                                 gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
957                         }
958                         in.close();
959                 
960                         num = positions.size();
961                 
962                         FILE * pFile;
963                         unsigned long int size;
964                         
965                         //get num bytes in file
966                         pFile = fopen (filename.c_str(),"rb");
967                         if (pFile==NULL) perror ("Error opening file");
968                         else{
969                                 fseek (pFile, 0, SEEK_END);
970                                 size=ftell (pFile);
971                                 fclose (pFile);
972                         }
973                 
974                         positions.push_back(size);
975                 
976                         return positions;
977         }
978         catch(exception& e) {
979                 errorOut(e, "MothurOut", "setFilePosEachLine");
980                 exit(1);
981         }
982 }
983 /**************************************************************************************************/
984
985 vector<unsigned long int> MothurOut::divideFile(string filename, int& proc) {
986         try{
987         
988                 vector<unsigned long int> filePos;
989                 filePos.push_back(0);
990                 
991                 FILE * pFile;
992                 unsigned long int size;
993                 
994                 filename = getFullPathName(filename);
995                 
996                 //get num bytes in file
997                 pFile = fopen (filename.c_str(),"rb");
998                 if (pFile==NULL) perror ("Error opening file");
999                 else{
1000                         fseek (pFile, 0, SEEK_END);
1001                         size=ftell (pFile);
1002                         fclose (pFile);
1003                 }
1004         
1005                 //estimate file breaks
1006                 unsigned long int chunkSize = 0;
1007                 chunkSize = size / proc;
1008
1009                 //file to small to divide by processors
1010                 if (chunkSize == 0)  {  proc = 1;       filePos.push_back(size); return filePos;        }
1011         
1012                 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1013                 for (int i = 0; i < proc; i++) {
1014                         unsigned long int spot = (i+1) * chunkSize;
1015                         
1016                         ifstream in;
1017                         openInputFile(filename, in);
1018                         in.seekg(spot);
1019                         
1020                         //look for next '>'
1021                         unsigned long int newSpot = spot;
1022                         while (!in.eof()) {
1023                            char c = in.get();
1024                            if (c == '>') {   in.putback(c); newSpot = in.tellg(); break;  }
1025                         }
1026                 
1027                         //there was not another sequence before the end of the file
1028                         unsigned long int sanityPos = in.tellg();
1029
1030                         if (sanityPos == -1) {  break;  }
1031                         else {  filePos.push_back(newSpot);  }
1032                         
1033                         in.close();
1034                 }
1035                 
1036                 //save end pos
1037                 filePos.push_back(size);
1038
1039                 //sanity check filePos
1040                 for (int i = 0; i < (filePos.size()-1); i++) {
1041                         if (filePos[(i+1)] <= filePos[i]) {  cout << "erasing " << (i+1) << endl; filePos.erase(filePos.begin()+(i+1)); i--; }
1042                 }
1043
1044                 proc = (filePos.size() - 1);
1045                 
1046                 return filePos;
1047         }
1048         catch(exception& e) {
1049                 errorOut(e, "MothurOut", "divideFile");
1050                 exit(1);
1051         }
1052 }
1053
1054 /***********************************************************************/
1055
1056 bool MothurOut::isTrue(string f){
1057         try {
1058                 
1059                 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1060                 
1061                 if ((f == "TRUE") || (f == "T")) {      return true;    }
1062                 else {  return false;  }
1063         }
1064         catch(exception& e) {
1065                 errorOut(e, "MothurOut", "isTrue");
1066                 exit(1);
1067         }
1068 }
1069
1070 /***********************************************************************/
1071
1072 float MothurOut::roundDist(float dist, int precision){
1073         try {
1074                 return int(dist * precision + 0.5)/float(precision);
1075         }
1076         catch(exception& e) {
1077                 errorOut(e, "MothurOut", "roundDist");
1078                 exit(1);
1079         }
1080 }
1081 /***********************************************************************/
1082
1083 float MothurOut::ceilDist(float dist, int precision){
1084         try {
1085                 return int(ceil(dist * precision))/float(precision);
1086         }
1087         catch(exception& e) {
1088                 errorOut(e, "MothurOut", "ceilDist");
1089                 exit(1);
1090         }
1091 }
1092
1093 /***********************************************************************/
1094
1095 int MothurOut::getNumNames(string names){
1096         try {
1097                 int count = 0;
1098                 
1099                 if(names != ""){
1100                         count = 1;
1101                         for(int i=0;i<names.size();i++){
1102                                 if(names[i] == ','){
1103                                         count++;
1104                                 }
1105                         }
1106                 }
1107                 
1108                 return count;
1109         }
1110         catch(exception& e) {
1111                 errorOut(e, "MothurOut", "getNumNames");
1112                 exit(1);
1113         }
1114 }
1115
1116 /**************************************************************************************************/
1117
1118 vector<vector<double> > MothurOut::binomial(int maxOrder){
1119         try {
1120         vector<vector<double> > binomial(maxOrder+1);
1121         
1122     for(int i=0;i<=maxOrder;i++){
1123                 binomial[i].resize(maxOrder+1);
1124                 binomial[i][0]=1;
1125                 binomial[0][i]=0;
1126     }
1127     binomial[0][0]=1;
1128         
1129     binomial[1][0]=1;
1130     binomial[1][1]=1;
1131         
1132     for(int i=2;i<=maxOrder;i++){
1133                 binomial[1][i]=0;
1134     }
1135         
1136     for(int i=2;i<=maxOrder;i++){
1137                 for(int j=1;j<=maxOrder;j++){
1138                         if(i==j){       binomial[i][j]=1;                                                                       }
1139                         if(j>i) {       binomial[i][j]=0;                                                                       }
1140                         else    {       binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j];     }
1141                 }
1142     }
1143         
1144         return binomial;
1145         
1146         }
1147         catch(exception& e) {
1148                 errorOut(e, "MothurOut", "binomial");
1149                 exit(1);
1150         }
1151 }
1152
1153 /***********************************************************************/
1154
1155 int MothurOut::factorial(int num){
1156         try {
1157                 int total = 1;
1158                 
1159                 for (int i = 1; i <= num; i++) {
1160                         total *= i;
1161                 }
1162                 
1163                 return total;
1164         }
1165         catch(exception& e) {
1166                 errorOut(e, "MothurOut", "factorial");
1167                 exit(1);
1168         }
1169 }
1170 /***********************************************************************/
1171
1172 int MothurOut::getNumSeqs(ifstream& file){
1173         try {
1174                 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1175                 file.seekg(0);
1176                 return numSeqs;
1177         }
1178         catch(exception& e) {
1179                 errorOut(e, "MothurOut", "getNumSeqs");
1180                 exit(1);
1181         }       
1182 }
1183 /***********************************************************************/
1184 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1185         try {
1186                 string input;
1187                 numSeqs = 0;
1188                 while(!file.eof()){
1189                         input = getline(file);
1190                         if (input.length() != 0) {
1191                                 if(input[0] == '>'){ numSeqs++; }
1192                         }
1193                 }
1194         }
1195         catch(exception& e) {
1196                 errorOut(e, "MothurOut", "getNumSeqs");
1197                 exit(1);
1198         }       
1199 }
1200 /***********************************************************************/
1201
1202 //This function parses the estimator options and puts them in a vector
1203 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1204         try {
1205                 string individual = "";
1206                 int estimLength = estim.size();
1207                 for(int i=0;i<estimLength;i++){
1208                         if(estim[i] == symbol){
1209                                 container.push_back(individual);
1210                                 individual = "";                                
1211                         }
1212                         else{
1213                                 individual += estim[i];
1214                         }
1215                 }
1216                 container.push_back(individual);
1217
1218                 /*
1219                 
1220                 while (estim.find_first_of(symbol) != -1) {
1221                         individual = estim.substr(0,estim.find_first_of(symbol));
1222                         if ((estim.find_first_of(symbol)+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1223                                 estim = estim.substr(estim.find_first_of(symbol)+1, estim.length());
1224                                 container.push_back(individual);
1225                         }
1226                 }
1227                 //get last one
1228                 container.push_back(estim); */
1229         }
1230         catch(exception& e) {
1231                 errorOut(e, "MothurOut", "splitAtChar");
1232                 exit(1);
1233         }       
1234 }
1235
1236 /***********************************************************************/
1237
1238 //This function parses the estimator options and puts them in a vector
1239 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1240         try {
1241                 string individual = "";
1242                 int estimLength = estim.size();
1243                 for(int i=0;i<estimLength;i++){
1244                         if(estim[i] == '-'){
1245                                 container.push_back(individual);
1246                                 individual = "";                                
1247                         }
1248                         else{
1249                                 individual += estim[i];
1250                         }
1251                 }
1252                 container.push_back(individual);
1253
1254         
1255         /*      string individual;
1256                 
1257                 while (estim.find_first_of('-') != -1) {
1258                         individual = estim.substr(0,estim.find_first_of('-'));
1259                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1260                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1261                                 container.push_back(individual);
1262                         }
1263                 }
1264                 //get last one
1265                 container.push_back(estim); */
1266         }
1267         catch(exception& e) {
1268                 errorOut(e, "MothurOut", "splitAtDash");
1269                 exit(1);
1270         }       
1271 }
1272
1273 /***********************************************************************/
1274 //This function parses the label options and puts them in a set
1275 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1276         try {
1277                 string individual = "";
1278                 int estimLength = estim.size();
1279                 for(int i=0;i<estimLength;i++){
1280                         if(estim[i] == '-'){
1281                                 container.insert(individual);
1282                                 individual = "";                                
1283                         }
1284                         else{
1285                                 individual += estim[i];
1286                         }
1287                 }
1288                 container.insert(individual);
1289
1290         //      string individual;
1291                 
1292         //      while (estim.find_first_of('-') != -1) {
1293         //              individual = estim.substr(0,estim.find_first_of('-'));
1294         //              if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1295         //                      estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1296         //                      container.insert(individual);
1297         //              }
1298         //      }
1299                 //get last one
1300         //      container.insert(estim);
1301         
1302         }
1303         catch(exception& e) {
1304                 errorOut(e, "MothurOut", "splitAtDash");
1305                 exit(1);
1306         }       
1307 }
1308 /***********************************************************************/
1309 //This function parses the line options and puts them in a set
1310 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1311         try {
1312                 string individual;
1313                 int lineNum;
1314                 
1315                 while (estim.find_first_of('-') != -1) {
1316                         individual = estim.substr(0,estim.find_first_of('-'));
1317                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1318                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1319                                 convert(individual, lineNum); //convert the string to int
1320                                 container.insert(lineNum);
1321                         }
1322                 }
1323                 //get last one
1324                 convert(estim, lineNum); //convert the string to int
1325                 container.insert(lineNum);
1326         }
1327         catch(exception& e) {
1328                 errorOut(e, "MothurOut", "splitAtDash");
1329                 exit(1);
1330         }       
1331 }
1332 /***********************************************************************/
1333 //This function parses the a string and puts peices in a vector
1334 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1335         try {
1336                 string individual = "";
1337                 int estimLength = estim.size();
1338                 for(int i=0;i<estimLength;i++){
1339                         if(estim[i] == ','){
1340                                 container.push_back(individual);
1341                                 individual = "";                                
1342                         }
1343                         else{
1344                                 individual += estim[i];
1345                         }
1346                 }
1347                 container.push_back(individual);
1348                 
1349                 
1350                 
1351                 
1352 //              string individual;
1353 //              
1354 //              while (estim.find_first_of(',') != -1) {
1355 //                      individual = estim.substr(0,estim.find_first_of(','));
1356 //                      if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1357 //                              estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1358 //                              container.push_back(individual);
1359 //                      }
1360 //              }
1361 //              //get last one
1362 //              container.push_back(estim);
1363         }
1364         catch(exception& e) {
1365                 errorOut(e, "MothurOut", "splitAtComma");
1366                 exit(1);
1367         }       
1368 }
1369 /***********************************************************************/
1370
1371 //This function splits up the various option parameters
1372 void MothurOut::splitAtComma(string& prefix, string& suffix){
1373         try {
1374                 prefix = suffix.substr(0,suffix.find_first_of(','));
1375                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
1376                         suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1377                         string space = " ";
1378                         while(suffix.at(0) == ' ')
1379                                 suffix = suffix.substr(1, suffix.length());
1380                 }
1381
1382         }
1383         catch(exception& e) {
1384                 errorOut(e, "MothurOut", "splitAtComma");
1385                 exit(1);
1386         }       
1387 }
1388 /***********************************************************************/
1389
1390 //This function separates the key value from the option value i.e. dist=96_...
1391 void MothurOut::splitAtEquals(string& key, string& value){              
1392         try {
1393                 if(value.find_first_of('=') != -1){
1394                         key = value.substr(0,value.find_first_of('='));
1395                         if ((value.find_first_of('=')+1) <= value.length()) {
1396                                 value = value.substr(value.find_first_of('=')+1, value.length());
1397                         }
1398                 }else{
1399                         key = value;
1400                         value = 1;
1401                 }
1402         }
1403         catch(exception& e) {
1404                 errorOut(e, "MothurOut", "splitAtEquals");
1405                 exit(1);
1406         }       
1407 }
1408
1409 /**************************************************************************************************/
1410
1411 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1412         try {
1413                 for (int i = 0; i < Groups.size(); i++) {
1414                         if (groupname == Groups[i]) { return true; }
1415                 }
1416                 return false;
1417         }
1418         catch(exception& e) {
1419                 errorOut(e, "MothurOut", "inUsersGroups");
1420                 exit(1);
1421         }       
1422 }
1423 /**************************************************************************************************/
1424 //returns true if any of the strings in first vector are in second vector
1425 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1426         try {
1427                 
1428                 for (int i = 0; i < groupnames.size(); i++) {
1429                         if (inUsersGroups(groupnames[i], Groups)) { return true; }
1430                 }
1431                 return false;
1432         }
1433         catch(exception& e) {
1434                 errorOut(e, "MothurOut", "inUsersGroups");
1435                 exit(1);
1436         }       
1437 }
1438 /***********************************************************************/
1439 //this function determines if the user has given us labels that are smaller than the given label.
1440 //if so then it returns true so that the calling function can run the previous valid distance.
1441 //it's a "smart" distance function.  It also checks for invalid labels.
1442 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1443         try {
1444                 
1445                 set<string>::iterator it;
1446                 vector<float> orderFloat;
1447                 map<string, float> userMap;  //the conversion process removes trailing 0's which we need to put back
1448                 map<string, float>::iterator it2;
1449                 float labelFloat;
1450                 bool smaller = false;
1451                 
1452                 //unique is the smallest line
1453                 if (label == "unique") {  return false;  }
1454                 else { 
1455                         if (convertTestFloat(label, labelFloat)) {
1456                                 convert(label, labelFloat); 
1457                         }else { //cant convert 
1458                                 return false;
1459                         }
1460                 }
1461                 
1462                 //go through users set and make them floats
1463                 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
1464                         
1465                         float temp;
1466                         if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1467                                 convert(*it, temp);
1468                                 orderFloat.push_back(temp);
1469                                 userMap[*it] = temp;
1470                         }else if (*it == "unique") { 
1471                                 orderFloat.push_back(-1.0);
1472                                 userMap["unique"] = -1.0;
1473                         }else {
1474                                 if (errorOff == "") {  cout << *it << " is not a valid label." << endl;  }
1475                                 userLabels.erase(*it); 
1476                                 it--;
1477                         }
1478                 }
1479                 
1480                 //sort order
1481                 sort(orderFloat.begin(), orderFloat.end());
1482                 
1483                 /*************************************************/
1484                 //is this label bigger than any of the users labels
1485                 /*************************************************/
1486                                 
1487                 //loop through order until you find a label greater than label
1488                 for (int i = 0; i < orderFloat.size(); i++) {
1489                         if (orderFloat[i] < labelFloat) {
1490                                 smaller = true;
1491                                 if (orderFloat[i] == -1) { 
1492                                         if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
1493                                         userLabels.erase("unique");
1494                                 }
1495                                 else {  
1496                                         if (errorOff == "") { cout << "Your file does not include the label " << endl; }
1497                                         string s = "";
1498                                         for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {  
1499                                                 if (it2->second == orderFloat[i]) {  
1500                                                         s = it2->first;  
1501                                                         //remove small labels
1502                                                         userLabels.erase(s);
1503                                                         break;
1504                                                 }
1505                                         }
1506                                         if (errorOff == "") {cout << s <<  ". I will use the next smallest distance. " << endl; }
1507                                 }
1508                         //since they are sorted once you find a bigger one stop looking
1509                         }else { break; }
1510                 }
1511                 
1512                 return smaller;
1513                                                 
1514         }
1515         catch(exception& e) {
1516                 errorOut(e, "MothurOut", "anyLabelsToProcess");
1517                 exit(1);
1518         }       
1519 }
1520
1521 /**************************************************************************************************/
1522 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1523         try {
1524                 
1525                 bool good = true;
1526                 
1527                 string line = getline(file);  
1528
1529                 //before we added this check
1530                 if (line[0] != '#') {  good = false;  }
1531                 else {
1532                         //rip off #
1533                         line = line.substr(1);
1534                         
1535                         vector<string> versionVector;
1536                         splitAtChar(version, versionVector, '.');
1537                         
1538                         //check file version
1539                         vector<string> linesVector;
1540                         splitAtChar(line, linesVector, '.');
1541                         
1542                         if (versionVector.size() != linesVector.size()) { good = false; }
1543                         else {
1544                                 for (int j = 0; j < versionVector.size(); j++) {
1545                                         int num1, num2;
1546                                         convert(versionVector[j], num1);
1547                                         convert(linesVector[j], num2);
1548                                         
1549                                         //if mothurs version is newer than this files version, then we want to remake it
1550                                         if (num1 > num2) {  good = false; break;  }
1551                                 }
1552                         }
1553                         
1554                 }
1555                 
1556                 if (!good) {  file.close();  }
1557                 else { file.seekg(0);  }
1558                 
1559                 return good;
1560         }
1561         catch(exception& e) {
1562                 errorOut(e, "MothurOut", "checkReleaseVersion");                
1563                 exit(1);
1564         }
1565 }
1566 /**************************************************************************************************/
1567
1568
1569
1570
1571