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