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