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