]> git.donarmstrong.com Git - mothur.git/blob - mothurout.cpp
modified trim.seqs to speed up the append with multiple processors
[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 unsigned int MothurOut::fromBase36(string base36){
1160         try {
1161                 unsigned int num = 0;
1162                 
1163                 map<char, int> converts;
1164                 converts['A'] = 0;
1165                 converts['a'] = 0;
1166                 converts['B'] = 1;
1167                 converts['b'] = 1;
1168                 converts['C'] = 2;
1169                 converts['c'] = 2;
1170                 converts['D'] = 3;
1171                 converts['d'] = 3;
1172                 converts['E'] = 4;
1173                 converts['e'] = 4;
1174                 converts['F'] = 5;
1175                 converts['f'] = 5;
1176                 converts['G'] = 6;
1177                 converts['g'] = 6;
1178                 converts['H'] = 7;
1179                 converts['h'] = 7;
1180                 converts['I'] = 8;
1181                 converts['i'] = 8;
1182                 converts['J'] = 9;
1183                 converts['j'] = 9;
1184                 converts['K'] = 10;
1185                 converts['k'] = 10;
1186                 converts['L'] = 11;
1187                 converts['l'] = 11;
1188                 converts['M'] = 12;
1189                 converts['m'] = 12;
1190                 converts['N'] = 13;
1191                 converts['n'] = 13;
1192                 converts['O'] = 14;
1193                 converts['o'] = 14;
1194                 converts['P'] = 15;
1195                 converts['p'] = 15;
1196                 converts['Q'] = 16;
1197                 converts['q'] = 16;
1198                 converts['R'] = 17;
1199                 converts['r'] = 17;
1200                 converts['S'] = 18;
1201                 converts['s'] = 18;
1202                 converts['T'] = 19;
1203                 converts['t'] = 19;
1204                 converts['U'] = 20;
1205                 converts['u'] = 20;
1206                 converts['V'] = 21;
1207                 converts['v'] = 21;
1208                 converts['W'] = 22;
1209                 converts['w'] = 22;
1210                 converts['X'] = 23;
1211                 converts['x'] = 23;
1212                 converts['Y'] = 24;
1213                 converts['y'] = 24;
1214                 converts['Z'] = 25;
1215                 converts['z'] = 25;
1216                 converts['0'] = 26;
1217                 converts['1'] = 27;
1218                 converts['2'] = 28;
1219                 converts['3'] = 29;
1220                 converts['4'] = 30;
1221                 converts['5'] = 31;
1222                 converts['6'] = 32;
1223                 converts['7'] = 33;
1224                 converts['8'] = 34;
1225                 converts['9'] = 35;             
1226                 
1227                 int i = 0;
1228                 while (i < base36.length()) {
1229                         char c = base36[i];
1230                         num = 36 * num + converts[c];
1231                         i++;
1232                 }
1233                 
1234                 return num;
1235                 
1236         }
1237         catch(exception& e) {
1238                 errorOut(e, "MothurOut", "fromBase36");
1239                 exit(1);
1240         }
1241 }
1242 /***********************************************************************/
1243
1244 int MothurOut::factorial(int num){
1245         try {
1246                 int total = 1;
1247                 
1248                 for (int i = 1; i <= num; i++) {
1249                         total *= i;
1250                 }
1251                 
1252                 return total;
1253         }
1254         catch(exception& e) {
1255                 errorOut(e, "MothurOut", "factorial");
1256                 exit(1);
1257         }
1258 }
1259 /***********************************************************************/
1260
1261 int MothurOut::getNumSeqs(ifstream& file){
1262         try {
1263                 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1264                 file.seekg(0);
1265                 return numSeqs;
1266         }
1267         catch(exception& e) {
1268                 errorOut(e, "MothurOut", "getNumSeqs");
1269                 exit(1);
1270         }       
1271 }
1272 /***********************************************************************/
1273 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1274         try {
1275                 string input;
1276                 numSeqs = 0;
1277                 while(!file.eof()){
1278                         input = getline(file);
1279                         if (input.length() != 0) {
1280                                 if(input[0] == '>'){ numSeqs++; }
1281                         }
1282                 }
1283         }
1284         catch(exception& e) {
1285                 errorOut(e, "MothurOut", "getNumSeqs");
1286                 exit(1);
1287         }       
1288 }
1289 /***********************************************************************/
1290
1291 //This function parses the estimator options and puts them in a vector
1292 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1293         try {
1294                 string individual = "";
1295                 int estimLength = estim.size();
1296                 for(int i=0;i<estimLength;i++){
1297                         if(estim[i] == symbol){
1298                                 container.push_back(individual);
1299                                 individual = "";                                
1300                         }
1301                         else{
1302                                 individual += estim[i];
1303                         }
1304                 }
1305                 container.push_back(individual);
1306
1307         }
1308         catch(exception& e) {
1309                 errorOut(e, "MothurOut", "splitAtChar");
1310                 exit(1);
1311         }       
1312 }
1313
1314 /***********************************************************************/
1315
1316 //This function parses the estimator options and puts them in a vector
1317 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1318         try {
1319                 string individual = "";
1320                 int estimLength = estim.size();
1321                 for(int i=0;i<estimLength;i++){
1322                         if(estim[i] == '-'){
1323                                 container.push_back(individual);
1324                                 individual = "";                                
1325                         }
1326                         else{
1327                                 individual += estim[i];
1328                         }
1329                 }
1330                 container.push_back(individual);
1331
1332         
1333         /*      string individual;
1334                 
1335                 while (estim.find_first_of('-') != -1) {
1336                         individual = estim.substr(0,estim.find_first_of('-'));
1337                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1338                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1339                                 container.push_back(individual);
1340                         }
1341                 }
1342                 //get last one
1343                 container.push_back(estim); */
1344         }
1345         catch(exception& e) {
1346                 errorOut(e, "MothurOut", "splitAtDash");
1347                 exit(1);
1348         }       
1349 }
1350
1351 /***********************************************************************/
1352 //This function parses the label options and puts them in a set
1353 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1354         try {
1355                 string individual = "";
1356                 int estimLength = estim.size();
1357                 for(int i=0;i<estimLength;i++){
1358                         if(estim[i] == '-'){
1359                                 container.insert(individual);
1360                                 individual = "";                                
1361                         }
1362                         else{
1363                                 individual += estim[i];
1364                         }
1365                 }
1366                 container.insert(individual);
1367
1368         //      string individual;
1369                 
1370         //      while (estim.find_first_of('-') != -1) {
1371         //              individual = estim.substr(0,estim.find_first_of('-'));
1372         //              if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1373         //                      estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1374         //                      container.insert(individual);
1375         //              }
1376         //      }
1377                 //get last one
1378         //      container.insert(estim);
1379         
1380         }
1381         catch(exception& e) {
1382                 errorOut(e, "MothurOut", "splitAtDash");
1383                 exit(1);
1384         }       
1385 }
1386 /***********************************************************************/
1387 //This function parses the line options and puts them in a set
1388 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1389         try {
1390                 string individual;
1391                 int lineNum;
1392                 
1393                 while (estim.find_first_of('-') != -1) {
1394                         individual = estim.substr(0,estim.find_first_of('-'));
1395                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1396                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1397                                 convert(individual, lineNum); //convert the string to int
1398                                 container.insert(lineNum);
1399                         }
1400                 }
1401                 //get last one
1402                 convert(estim, lineNum); //convert the string to int
1403                 container.insert(lineNum);
1404         }
1405         catch(exception& e) {
1406                 errorOut(e, "MothurOut", "splitAtDash");
1407                 exit(1);
1408         }       
1409 }
1410 /***********************************************************************/
1411 //This function parses the a string and puts peices in a vector
1412 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1413         try {
1414                 string individual = "";
1415                 int estimLength = estim.size();
1416                 for(int i=0;i<estimLength;i++){
1417                         if(estim[i] == ','){
1418                                 container.push_back(individual);
1419                                 individual = "";                                
1420                         }
1421                         else{
1422                                 individual += estim[i];
1423                         }
1424                 }
1425                 container.push_back(individual);
1426                 
1427                 
1428                 
1429                 
1430 //              string individual;
1431 //              
1432 //              while (estim.find_first_of(',') != -1) {
1433 //                      individual = estim.substr(0,estim.find_first_of(','));
1434 //                      if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1435 //                              estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1436 //                              container.push_back(individual);
1437 //                      }
1438 //              }
1439 //              //get last one
1440 //              container.push_back(estim);
1441         }
1442         catch(exception& e) {
1443                 errorOut(e, "MothurOut", "splitAtComma");
1444                 exit(1);
1445         }       
1446 }
1447 /***********************************************************************/
1448
1449 //This function splits up the various option parameters
1450 void MothurOut::splitAtComma(string& prefix, string& suffix){
1451         try {
1452                 prefix = suffix.substr(0,suffix.find_first_of(','));
1453                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
1454                         suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1455                         string space = " ";
1456                         while(suffix.at(0) == ' ')
1457                                 suffix = suffix.substr(1, suffix.length());
1458                 }
1459
1460         }
1461         catch(exception& e) {
1462                 errorOut(e, "MothurOut", "splitAtComma");
1463                 exit(1);
1464         }       
1465 }
1466 /***********************************************************************/
1467
1468 //This function separates the key value from the option value i.e. dist=96_...
1469 void MothurOut::splitAtEquals(string& key, string& value){              
1470         try {
1471                 if(value.find_first_of('=') != -1){
1472                         key = value.substr(0,value.find_first_of('='));
1473                         if ((value.find_first_of('=')+1) <= value.length()) {
1474                                 value = value.substr(value.find_first_of('=')+1, value.length());
1475                         }
1476                 }else{
1477                         key = value;
1478                         value = 1;
1479                 }
1480         }
1481         catch(exception& e) {
1482                 errorOut(e, "MothurOut", "splitAtEquals");
1483                 exit(1);
1484         }       
1485 }
1486
1487 /**************************************************************************************************/
1488
1489 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1490         try {
1491                 for (int i = 0; i < Groups.size(); i++) {
1492                         if (groupname == Groups[i]) { return true; }
1493                 }
1494                 return false;
1495         }
1496         catch(exception& e) {
1497                 errorOut(e, "MothurOut", "inUsersGroups");
1498                 exit(1);
1499         }       
1500 }
1501 /**************************************************************************************************/
1502 //returns true if any of the strings in first vector are in second vector
1503 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1504         try {
1505                 
1506                 for (int i = 0; i < groupnames.size(); i++) {
1507                         if (inUsersGroups(groupnames[i], Groups)) { return true; }
1508                 }
1509                 return false;
1510         }
1511         catch(exception& e) {
1512                 errorOut(e, "MothurOut", "inUsersGroups");
1513                 exit(1);
1514         }       
1515 }
1516 /***********************************************************************/
1517 //this function determines if the user has given us labels that are smaller than the given label.
1518 //if so then it returns true so that the calling function can run the previous valid distance.
1519 //it's a "smart" distance function.  It also checks for invalid labels.
1520 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1521         try {
1522                 
1523                 set<string>::iterator it;
1524                 vector<float> orderFloat;
1525                 map<string, float> userMap;  //the conversion process removes trailing 0's which we need to put back
1526                 map<string, float>::iterator it2;
1527                 float labelFloat;
1528                 bool smaller = false;
1529                 
1530                 //unique is the smallest line
1531                 if (label == "unique") {  return false;  }
1532                 else { 
1533                         if (convertTestFloat(label, labelFloat)) {
1534                                 convert(label, labelFloat); 
1535                         }else { //cant convert 
1536                                 return false;
1537                         }
1538                 }
1539                 
1540                 //go through users set and make them floats
1541                 for(it = userLabels.begin(); it != userLabels.end();) {
1542                         
1543                         float temp;
1544                         if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1545                                 convert(*it, temp);
1546                                 orderFloat.push_back(temp);
1547                                 userMap[*it] = temp;
1548                                 it++;
1549                         }else if (*it == "unique") { 
1550                                 orderFloat.push_back(-1.0);
1551                                 userMap["unique"] = -1.0;
1552                                 it++;
1553                         }else {
1554                                 if (errorOff == "") {  mothurOut(*it + " is not a valid label."); mothurOutEndLine();  }
1555                                 userLabels.erase(it++); 
1556                         }
1557                 }
1558                 
1559                 //sort order
1560                 sort(orderFloat.begin(), orderFloat.end());
1561                 
1562                 /*************************************************/
1563                 //is this label bigger than any of the users labels
1564                 /*************************************************/
1565                                 
1566                 //loop through order until you find a label greater than label
1567                 for (int i = 0; i < orderFloat.size(); i++) {
1568                         if (orderFloat[i] < labelFloat) {
1569                                 smaller = true;
1570                                 if (orderFloat[i] == -1) { 
1571                                         if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
1572                                         userLabels.erase("unique");
1573                                 }
1574                                 else {  
1575                                         if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
1576                                         string s = "";
1577                                         for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {  
1578                                                 if (it2->second == orderFloat[i]) {  
1579                                                         s = it2->first;  
1580                                                         //remove small labels
1581                                                         userLabels.erase(s);
1582                                                         break;
1583                                                 }
1584                                         }
1585                                         if (errorOff == "") {mothurOut( s +  ". I will use the next smallest distance. "); mothurOutEndLine(); }
1586                                 }
1587                         //since they are sorted once you find a bigger one stop looking
1588                         }else { break; }
1589                 }
1590                 
1591                 return smaller;
1592                                                 
1593         }
1594         catch(exception& e) {
1595                 errorOut(e, "MothurOut", "anyLabelsToProcess");
1596                 exit(1);
1597         }       
1598 }
1599
1600 /**************************************************************************************************/
1601 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1602         try {
1603                 
1604                 bool good = true;
1605                 
1606                 string line = getline(file);  
1607
1608                 //before we added this check
1609                 if (line[0] != '#') {  good = false;  }
1610                 else {
1611                         //rip off #
1612                         line = line.substr(1);
1613                         
1614                         vector<string> versionVector;
1615                         splitAtChar(version, versionVector, '.');
1616                         
1617                         //check file version
1618                         vector<string> linesVector;
1619                         splitAtChar(line, linesVector, '.');
1620                         
1621                         if (versionVector.size() != linesVector.size()) { good = false; }
1622                         else {
1623                                 for (int j = 0; j < versionVector.size(); j++) {
1624                                         int num1, num2;
1625                                         convert(versionVector[j], num1);
1626                                         convert(linesVector[j], num2);
1627                                         
1628                                         //if mothurs version is newer than this files version, then we want to remake it
1629                                         if (num1 > num2) {  good = false; break;  }
1630                                 }
1631                         }
1632                         
1633                 }
1634                 
1635                 if (!good) {  file.close();  }
1636                 else { file.seekg(0);  }
1637                 
1638                 return good;
1639         }
1640         catch(exception& e) {
1641                 errorOut(e, "MothurOut", "checkReleaseVersion");                
1642                 exit(1);
1643         }
1644 }
1645
1646 /**************************************************************************************************/
1647
1648
1649
1650
1651