]> git.donarmstrong.com Git - mothur.git/blob - mothurout.cpp
added sparseDistanceMatrix class. Modified cluster commands to use the new sparse...
[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 set<string> MothurOut::getCurrentTypes()  {
22         try {
23         
24         set<string> types;
25         types.insert("fasta");
26         types.insert("accnos");
27         types.insert("column");
28         types.insert("design");
29         types.insert("group");
30         types.insert("list");
31         types.insert("name");
32         types.insert("oligos");
33         types.insert("order");
34         types.insert("ordergroup");
35         types.insert("phylip");
36         types.insert("qfile");
37         types.insert("relabund");
38         types.insert("sabund");
39         types.insert("rabund");
40         types.insert("sff");
41         types.insert("shared");
42         types.insert("taxonomy");
43         types.insert("tree");
44         types.insert("flow");
45         types.insert("biom");
46         types.insert("counttable");
47         types.insert("processors");
48
49                 return types;
50         }
51         catch(exception& e) {
52                 errorOut(e, "MothurOut", "getCurrentTypes");
53                 exit(1);
54         }
55 }
56 /*********************************************************************************************/
57 void MothurOut::printCurrentFiles()  {
58         try {
59         
60         
61                 if (accnosfile != "")           {  mothurOut("accnos=" + accnosfile); mothurOutEndLine();                       }
62                 if (columnfile != "")           {  mothurOut("column=" + columnfile); mothurOutEndLine();                       }
63                 if (designfile != "")           {  mothurOut("design=" + designfile); mothurOutEndLine();                       }
64                 if (fastafile != "")            {  mothurOut("fasta=" + fastafile); mothurOutEndLine();                         }
65                 if (groupfile != "")            {  mothurOut("group=" + groupfile); mothurOutEndLine();                         }
66                 if (listfile != "")                     {  mothurOut("list=" + listfile); mothurOutEndLine();                           }
67                 if (namefile != "")                     {  mothurOut("name=" + namefile); mothurOutEndLine();                           }
68                 if (oligosfile != "")           {  mothurOut("oligos=" + oligosfile); mothurOutEndLine();                       }
69                 if (orderfile != "")            {  mothurOut("order=" + orderfile); mothurOutEndLine();                         }
70                 if (ordergroupfile != "")       {  mothurOut("ordergroup=" + ordergroupfile); mothurOutEndLine();       }
71                 if (phylipfile != "")           {  mothurOut("phylip=" + phylipfile); mothurOutEndLine();                       }
72                 if (qualfile != "")                     {  mothurOut("qfile=" + qualfile); mothurOutEndLine();                          }
73                 if (rabundfile != "")           {  mothurOut("rabund=" + rabundfile); mothurOutEndLine();                       }
74                 if (relabundfile != "")         {  mothurOut("relabund=" + relabundfile); mothurOutEndLine();           }
75                 if (sabundfile != "")           {  mothurOut("sabund=" + sabundfile); mothurOutEndLine();                       }
76                 if (sfffile != "")                      {  mothurOut("sff=" + sfffile); mothurOutEndLine();                                     }
77                 if (sharedfile != "")           {  mothurOut("shared=" + sharedfile); mothurOutEndLine();                       }
78                 if (taxonomyfile != "")         {  mothurOut("taxonomy=" + taxonomyfile); mothurOutEndLine();           }
79                 if (treefile != "")                     {  mothurOut("tree=" + treefile); mothurOutEndLine();                           }
80                 if (flowfile != "")                     {  mothurOut("flow=" + flowfile); mothurOutEndLine();                           }
81         if (biomfile != "")                     {  mothurOut("biom=" + biomfile); mothurOutEndLine();                           }
82         if (counttablefile != "")       {  mothurOut("counttable=" + counttablefile); mothurOutEndLine();       }
83                 if (processors != "1")          {  mothurOut("processors=" + processors); mothurOutEndLine();           }
84                 
85         }
86         catch(exception& e) {
87                 errorOut(e, "MothurOut", "printCurrentFiles");
88                 exit(1);
89         }
90 }
91 /*********************************************************************************************/
92 bool MothurOut::hasCurrentFiles()  {
93         try {
94                 bool hasCurrent = false;
95                 
96                 if (accnosfile != "")           {  return true;                 }
97                 if (columnfile != "")           {  return true;                 }
98                 if (designfile != "")           {  return true;                 }
99                 if (fastafile != "")            {  return true;                 }
100                 if (groupfile != "")            {  return true;                 }
101                 if (listfile != "")                     {  return true;                 }
102                 if (namefile != "")                     {  return true;                 }
103                 if (oligosfile != "")           {  return true;                 }
104                 if (orderfile != "")            {  return true;                 }
105                 if (ordergroupfile != "")       {  return true;                 }
106                 if (phylipfile != "")           {  return true;                 }
107                 if (qualfile != "")                     {  return true;                 }
108                 if (rabundfile != "")           {  return true;                 }
109                 if (relabundfile != "")         {  return true;                 }
110                 if (sabundfile != "")           {  return true;                 }
111                 if (sfffile != "")                      {  return true;                 }
112                 if (sharedfile != "")           {  return true;                 }
113                 if (taxonomyfile != "")         {  return true;                 }
114                 if (treefile != "")                     {  return true;                 }
115                 if (flowfile != "")                     {  return true;                 }
116         if (biomfile != "")                     {  return true;                 }
117         if (counttablefile != "")       {  return true;                 }
118                 if (processors != "1")          {  return true;                 }
119                 
120                 return hasCurrent;
121                 
122         }
123         catch(exception& e) {
124                 errorOut(e, "MothurOut", "hasCurrentFiles");
125                 exit(1);
126         }
127 }
128
129 /*********************************************************************************************/
130 void MothurOut::clearCurrentFiles()  {
131         try {
132                 phylipfile = "";
133                 columnfile = "";
134                 listfile = "";
135                 rabundfile = "";
136                 sabundfile = "";
137                 namefile = "";
138                 groupfile = "";
139                 designfile = "";
140                 orderfile = "";
141                 treefile = "";
142                 sharedfile = "";
143                 ordergroupfile = "";
144                 relabundfile = "";
145                 fastafile = "";
146                 qualfile = "";
147                 sfffile = "";
148                 oligosfile = "";
149                 accnosfile = "";
150                 taxonomyfile = "";      
151                 flowfile = "";
152         biomfile = "";
153         counttablefile = "";
154                 processors = "1";
155         }
156         catch(exception& e) {
157                 errorOut(e, "MothurOut", "clearCurrentFiles");
158                 exit(1);
159         }
160 }
161 /***********************************************************************/
162 string MothurOut::findProgramPath(string programName){
163         try { 
164                 
165                 string envPath = getenv("PATH");
166                 string pPath = "";
167                 
168                 //delimiting path char
169                 char delim;
170 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
171         delim = ':';
172 #else
173         delim = ';';
174 #endif
175                 
176                 //break apart path variable by ':'
177                 vector<string> dirs;
178                 splitAtChar(envPath, dirs, delim);
179                 
180         if (debug) { mothurOut("[DEBUG]: dir's in path: \n"); }
181         
182                 //get path related to mothur
183                 for (int i = 0; i < dirs.size(); i++) {
184             
185             if (debug) { mothurOut("[DEBUG]: " + dirs[i] + "\n"); }
186             
187                         //to lower so we can find it
188                         string tempLower = "";
189                         for (int j = 0; j < dirs[i].length(); j++) {  tempLower += tolower(dirs[i][j]);  }
190                         
191                         //is this mothurs path?
192                         if (tempLower.find(programName) != -1) {  pPath = dirs[i]; break;  }
193                 }
194         
195                 if (debug) { mothurOut("[DEBUG]: programPath = " + pPath + "\n"); }
196         
197                 if (pPath != "") {
198                         //add programName so it looks like what argv would look like
199 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
200             pPath += "/" + programName;
201 #else
202             pPath += "\\" + programName;
203 #endif
204                 }else {
205                         //okay programName is not in the path, so the folder programName is in must be in the path
206                         //lets find out which one
207                         
208                         //get path related to the program
209                         for (int i = 0; i < dirs.size(); i++) {
210                 
211                 if (debug) { mothurOut("[DEBUG]: looking in " + dirs[i] + " for " + programName + " \n"); }
212                 
213                                 //is this the programs path?
214                                 ifstream in;
215                                 string tempIn = dirs[i];
216 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
217                 tempIn += "/" + programName;
218 #else
219                 tempIn += "\\" + programName;
220 #endif
221                                 openInputFile(tempIn, in, "");
222                                 
223                                 //if this file exists
224                                 if (in) { in.close(); pPath = tempIn; if (debug) { mothurOut("[DEBUG]: found it, programPath = " + pPath + "\n"); } break;   }
225                         }
226                 }
227                 
228                 return pPath;
229                 
230         }
231         catch(exception& e) {
232                 errorOut(e, "MothurOut", "findProgramPath");
233                 exit(1);
234         }
235 }
236 /*********************************************************************************************/
237 void MothurOut::setFileName(string filename)  {
238         try {
239                 logFileName = filename;
240                 
241                 #ifdef USE_MPI
242                         int pid;
243                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
244                                         
245                         if (pid == 0) { //only one process should output to screen
246                 #endif
247                 
248                 openOutputFile(filename, out);
249                 
250                 #ifdef USE_MPI
251                         }
252                 #endif
253         }
254         catch(exception& e) {
255                 errorOut(e, "MothurOut", "setFileName");
256                 exit(1);
257         }
258 }
259 /*********************************************************************************************/
260 void MothurOut::setDefaultPath(string pathname)  {
261         try {
262         
263                 //add / to name if needed
264                 string lastChar = pathname.substr(pathname.length()-1);
265                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
266                         if (lastChar != "/") { pathname += "/"; }
267                 #else
268                         if (lastChar != "\\") { pathname += "\\"; }     
269                 #endif
270                 
271                 defaultPath = pathname;
272                 
273         }
274         catch(exception& e) {
275                 errorOut(e, "MothurOut", "setDefaultPath");
276                 exit(1);
277         }
278 }
279 /*********************************************************************************************/
280 void MothurOut::setOutputDir(string pathname)  {
281         try {
282                 outputDir = pathname;
283         }
284         catch(exception& e) {
285                 errorOut(e, "MothurOut", "setOutputDir");
286                 exit(1);
287         }
288 }
289 /*********************************************************************************************/
290 void MothurOut::closeLog()  {
291         try {
292                 
293                 #ifdef USE_MPI
294                         int pid;
295                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
296                                         
297                         if (pid == 0) { //only one process should output to screen
298                 #endif
299                 
300                 out.close();
301                 
302                 #ifdef USE_MPI
303                         }
304                 #endif
305         }
306         catch(exception& e) {
307                 errorOut(e, "MothurOut", "closeLog");
308                 exit(1);
309         }
310 }
311
312 /*********************************************************************************************/
313 MothurOut::~MothurOut() {
314         try {
315                 _uniqueInstance = 0;
316                 
317         }
318         catch(exception& e) {
319                 errorOut(e, "MothurOut", "MothurOut");
320                 exit(1);
321         }
322 }
323 /*********************************************************************************************/
324 void MothurOut::mothurOut(string output) {
325         try {
326                 
327                 #ifdef USE_MPI
328                         int pid;
329                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
330                                         
331                         if (pid == 0) { //only one process should output to screen
332                 #endif
333                 
334                 out << output;
335         logger() << output;
336                 
337                 #ifdef USE_MPI
338                         }
339                 #endif
340         }
341         catch(exception& e) {
342                 errorOut(e, "MothurOut", "MothurOut");
343                 exit(1);
344         }
345 }
346 /*********************************************************************************************/
347 void MothurOut::mothurOutEndLine() {
348         try {
349                 #ifdef USE_MPI
350                         int pid;
351                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
352                                         
353                         if (pid == 0) { //only one process should output to screen
354                 #endif
355                 
356                 out << endl;
357         logger() << endl;
358                 
359                 #ifdef USE_MPI
360                         }
361                 #endif
362         }
363         catch(exception& e) {
364                 errorOut(e, "MothurOut", "MothurOutEndLine");
365                 exit(1);
366         }
367 }
368 /*********************************************************************************************/
369 void MothurOut::mothurOut(string output, ofstream& outputFile) {
370         try {
371                 
372 #ifdef USE_MPI
373                 int pid;
374                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
375                 
376                 if (pid == 0) { //only one process should output to screen
377 #endif
378                         
379                         
380                         out << output;
381                         outputFile << output;
382             logger() << output;
383                         
384 #ifdef USE_MPI
385                 }
386 #endif
387         
388         }
389         catch(exception& e) {
390                 errorOut(e, "MothurOut", "MothurOut");
391                 exit(1);
392         }
393 }
394 /*********************************************************************************************/
395 void MothurOut::mothurOutEndLine(ofstream& outputFile) {
396         try {
397 #ifdef USE_MPI
398                 int pid;
399                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
400                 
401                 if (pid == 0) { //only one process should output to screen
402 #endif
403                         
404                         out << endl;
405                         outputFile << endl;
406             logger() << endl;
407                         
408 #ifdef USE_MPI
409                 }
410 #endif
411         }
412         catch(exception& e) {
413                 errorOut(e, "MothurOut", "MothurOutEndLine");
414                 exit(1);
415         }
416 }
417 /*********************************************************************************************/
418 void MothurOut::mothurOutJustToLog(string output) {
419         try {
420                 #ifdef USE_MPI
421                         int pid;
422                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
423                                         
424                         if (pid == 0) { //only one process should output to screen
425                 #endif
426                 
427                 out << output;
428                 
429                 #ifdef USE_MPI
430                         }
431                 #endif
432         }
433         catch(exception& e) {
434                 errorOut(e, "MothurOut", "MothurOutJustToLog");
435                 exit(1);
436         }
437 }
438 /*********************************************************************************************/
439 void MothurOut::errorOut(exception& e, string object, string function) {
440         //double vm, rss;
441         //mem_usage(vm, rss);
442         
443         mothurOut("[ERROR]: ");
444         mothurOut(toString(e.what()));
445         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.");
446         mothurOutEndLine();
447 }
448 /*********************************************************************************************/
449 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c 
450 // process_mem_usage(double &, double &) - takes two doubles by reference,
451 // attempts to read the system-dependent data for a process' virtual memory
452 // size and resident set size, and return the results in KB.
453 //
454 // On failure, returns 0.0, 0.0
455 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
456   #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
457   
458            vm_usage     = 0.0;
459            resident_set = 0.0;
460
461            // 'file' stat seems to give the most reliable results
462            //
463            ifstream stat_stream("/proc/self/stat",ios_base::in);
464
465            // dummy vars for leading entries in stat that we don't care about
466            //
467            string pid, comm, state, ppid, pgrp, session, tty_nr;
468            string tpgid, flags, minflt, cminflt, majflt, cmajflt;
469            string utime, stime, cutime, cstime, priority, nice;
470            string O, itrealvalue, starttime;
471
472            // the two fields we want
473            //
474            unsigned long vsize;
475            long rss;
476
477            stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
478                                    >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
479                                    >> utime >> stime >> cutime >> cstime >> priority >> nice
480                                    >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
481
482            long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
483            vm_usage     = vsize / 1024.0;
484            resident_set = rss * page_size_kb;
485            
486            mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
487                 return 0;
488
489         #else
490 /*              //windows memory usage
491                 // Get the list of process identifiers.
492                 DWORD aProcesses[1024], cbNeeded, cProcesses;
493                 
494                 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
495
496                 // Calculate how many process identifiers were returned.
497                 cProcesses = cbNeeded / sizeof(DWORD);
498
499                 // Print the memory usage for each process
500                 for (int i = 0; i < cProcesses; i++ ) {
501                         DWORD processID = aProcesses[i];
502                         
503                         PROCESS_MEMORY_COUNTERS pmc;
504
505                         HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
506
507                         // Print the process identifier.
508                         printf( "\nProcess ID: %u\n", processID);
509                         
510                         if (NULL != hProcess) {
511
512                                 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
513                                         printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
514                                         printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
515                                         printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
516                                         printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
517                                         printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
518                                         printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
519                                         printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
520                                         printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage ); 
521                                         printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
522                                 }
523                                 CloseHandle(hProcess);
524                         }
525                 }
526 */
527                         return 0;
528
529         #endif
530 }
531
532
533 /***********************************************************************/
534 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
535         try {
536                 fileName = getFullPathName(fileName);
537                 
538                 fileHandle.open(fileName.c_str(), ios::app);
539                 if(!fileHandle) {
540                         mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
541                         return 1;
542                 }
543                 else {
544                         return 0;
545                 }
546         }
547         catch(exception& e) {
548                 errorOut(e, "MothurOut", "openOutputFileAppend");
549                 exit(1);
550         }
551 }
552 /***********************************************************************/
553 void MothurOut::gobble(istream& f){
554         try {
555                 
556                 char d;
557                 while(isspace(d=f.get()))               { ;}
558                 if(!f.eof()) { f.putback(d); }
559         }
560         catch(exception& e) {
561                 errorOut(e, "MothurOut", "gobble");
562                 exit(1);
563         }
564 }
565 /***********************************************************************/
566 void MothurOut::gobble(istringstream& f){
567         try {
568                 char d;
569                 while(isspace(d=f.get()))               {;}
570                 if(!f.eof()) { f.putback(d); }
571         }
572         catch(exception& e) {
573                 errorOut(e, "MothurOut", "gobble");
574                 exit(1);
575         }
576 }
577
578 /***********************************************************************/
579
580 string MothurOut::getline(istringstream& fileHandle) {
581         try {
582         
583                 string line = "";
584                 
585                 while (!fileHandle.eof())       {
586                         //get next character
587                         char c = fileHandle.get(); 
588                         
589                         //are you at the end of the line
590                         if ((c == '\n') || (c == '\r') || (c == '\f')){  break; }       
591                         else {          line += c;              }
592                 }
593                 
594                 return line;
595                 
596         }
597         catch(exception& e) {
598                 errorOut(e, "MothurOut", "getline");
599                 exit(1);
600         }
601 }
602 /***********************************************************************/
603
604 string MothurOut::getline(ifstream& fileHandle) {
605         try {
606         
607                 string line = "";
608                 
609                 while (fileHandle)      {
610                         //get next character
611                         char c = fileHandle.get(); 
612                         
613                         //are you at the end of the line
614                         if ((c == '\n') || (c == '\r') || (c == '\f') || (c == EOF)){  break;   }       
615                         else {          line += c;              }
616                 }
617                 
618                 return line;
619                 
620         }
621         catch(exception& e) {
622                 errorOut(e, "MothurOut", "getline");
623                 exit(1);
624         }
625 }
626 /***********************************************************************/
627
628 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
629 #ifdef USE_COMPRESSION
630 inline bool endsWith(string s, const char * suffix){
631   size_t suffixLength = strlen(suffix);
632   return s.size() >= suffixLength && s.substr(s.size() - suffixLength, suffixLength).compare(suffix) == 0;
633 }
634 #endif
635 #endif
636
637 string MothurOut::getRootName(string longName){
638         try {
639         
640                 string rootName = longName;
641
642 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
643 #ifdef USE_COMPRESSION
644     if (endsWith(rootName, ".gz") || endsWith(rootName, ".bz2")) {
645       int pos = rootName.find_last_of('.');
646       rootName = rootName.substr(0, pos);
647       cerr << "shortening " << longName << " to " << rootName << "\n";
648     }
649 #endif
650 #endif
651                 if(rootName.find_last_of(".") != rootName.npos){
652                         int pos = rootName.find_last_of('.')+1;
653                         rootName = rootName.substr(0, pos);
654                 }
655
656                 return rootName;
657         }
658         catch(exception& e) {
659                 errorOut(e, "MothurOut", "getRootName");
660                 exit(1);
661         }
662 }
663 /***********************************************************************/
664
665 string MothurOut::getSimpleName(string longName){
666         try {
667                 string simpleName = longName;
668                 
669                 size_t found;
670                 found=longName.find_last_of("/\\");
671
672                 if(found != longName.npos){
673                         simpleName = longName.substr(found+1);
674                 }
675                 
676                 return simpleName;
677         }
678         catch(exception& e) {
679                 errorOut(e, "MothurOut", "getSimpleName");
680                 exit(1);
681         }
682 }
683
684 /***********************************************************************/
685
686 int MothurOut::getRandomIndex(int highest){
687         try {
688                 
689                 int random = (int) ((float)(highest+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
690                 
691                 return random;
692         }
693         catch(exception& e) {
694                 errorOut(e, "MothurOut", "getRandomIndex");
695                 exit(1);
696         }       
697         
698 }
699 /**********************************************************************/
700
701 string MothurOut::getPathName(string longName){
702         try {
703                 string rootPathName = longName;
704                 
705                 if(longName.find_last_of("/\\") != longName.npos){
706                         int pos = longName.find_last_of("/\\")+1;
707                         rootPathName = longName.substr(0, pos);
708                 }
709                 
710                 return rootPathName;
711         }
712         catch(exception& e) {
713                 errorOut(e, "MothurOut", "getPathName");
714                 exit(1);
715         }       
716
717 }
718 /***********************************************************************/
719
720 bool MothurOut::dirCheck(string& dirName){
721         try {
722         
723         string tag = "";
724         #ifdef USE_MPI
725             int pid; 
726             MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
727                 
728             tag = toString(pid);
729         #endif
730
731         //add / to name if needed
732         string lastChar = dirName.substr(dirName.length()-1);
733         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
734         if (lastChar != "/") { dirName += "/"; }
735         #else
736         if (lastChar != "\\") { dirName += "\\"; }      
737         #endif
738
739         //test to make sure directory exists
740         dirName = getFullPathName(dirName);
741         string outTemp = dirName + tag + "temp";
742         ofstream out;
743         out.open(outTemp.c_str(), ios::trunc);
744         if(!out) {
745             mothurOut(dirName + " directory does not exist or is not writable."); mothurOutEndLine(); 
746         }else{
747             out.close();
748             mothurRemove(outTemp);
749             return true;
750         }
751         
752         return false;
753     }
754         catch(exception& e) {
755                 errorOut(e, "MothurOut", "dirCheck");
756                 exit(1);
757         }       
758     
759 }
760 /***********************************************************************/
761
762 string MothurOut::hasPath(string longName){
763         try {
764                 string path = "";
765                 
766                 size_t found;
767                 found=longName.find_last_of("~/\\");
768
769                 if(found != longName.npos){
770                         path = longName.substr(0, found+1);
771                 }
772                 
773                 return path;
774         }
775         catch(exception& e) {
776                 errorOut(e, "MothurOut", "hasPath");
777                 exit(1);
778         }       
779 }
780
781 /***********************************************************************/
782
783 string MothurOut::getExtension(string longName){
784         try {
785                 string extension = "";
786                 
787                 if(longName.find_last_of('.') != longName.npos){
788                         int pos = longName.find_last_of('.');
789                         extension = longName.substr(pos, longName.length());
790                 }
791                 
792                 return extension;
793         }
794         catch(exception& e) {
795                 errorOut(e, "MothurOut", "getExtension");
796                 exit(1);
797         }       
798 }
799 /***********************************************************************/
800 bool MothurOut::isBlank(string fileName){
801         try {
802                 
803                 fileName = getFullPathName(fileName);
804                 
805                 ifstream fileHandle;
806                 fileHandle.open(fileName.c_str());
807                 if(!fileHandle) {
808                         mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
809                         return false;
810                 }else {
811                         //check for blank file
812                         gobble(fileHandle);
813                         if (fileHandle.eof()) { fileHandle.close(); return true;  }
814                         fileHandle.close();
815                 }
816                 return false;
817         }
818         catch(exception& e) {
819                 errorOut(e, "MothurOut", "isBlank");
820                 exit(1);
821         }       
822 }
823 /***********************************************************************/
824
825 string MothurOut::getFullPathName(string fileName){
826         try{
827         
828         string path = hasPath(fileName);
829         string newFileName;
830         int pos;
831         
832         if (path == "") { return fileName; } //its a simple name
833         else { //we need to complete the pathname
834                 // ex. ../../../filename 
835                 // cwd = /user/work/desktop
836                                 
837                 string cwd;
838                 //get current working directory 
839                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)  
840                         
841                         if (path.find("~") != -1) { //go to home directory
842                                 string homeDir;
843                         
844                                 char *homepath = NULL;
845                                 homepath = getenv ("HOME");
846                                 if ( homepath != NULL) { homeDir = homepath; }
847                                 else { homeDir = "";  }
848
849                                 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
850                                 return newFileName;
851                         }else { //find path
852                                 if (path.rfind("./") == string::npos) { return fileName; } //already complete name
853                                 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
854                                 
855                                 //char* cwdpath = new char[1024];
856                                 //size_t size;
857                                 //cwdpath=getcwd(cwdpath,size);
858                                 //cwd = cwdpath;
859                                 
860                                 char *cwdpath = NULL;
861                                 cwdpath = getcwd(NULL, 0); // or _getcwd
862                                 if ( cwdpath != NULL) { cwd = cwdpath; }
863                                 else { cwd = "";  }
864
865                                 
866                                 //rip off first '/'
867                                 string simpleCWD;
868                                 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
869                                 
870                                 //break apart the current working directory
871                                 vector<string> dirs;
872                                 while (simpleCWD.find_first_of('/') != string::npos) {
873                                         string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
874                                         simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
875                                         dirs.push_back(dir);
876                                 }
877                                 //get last one              // ex. ../../../filename = /user/work/desktop/filename
878                                 dirs.push_back(simpleCWD);  //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
879                                 
880                         
881                                 int index = dirs.size()-1;
882                 
883                                 while((pos = path.rfind("./")) != string::npos) { //while you don't have a complete path
884                                         if (pos == 0) { break;  //you are at the end
885                                         }else if (path[(pos-1)] == '.') { //you want your parent directory ../
886                                                 path = path.substr(0, pos-1);
887                                                 index--;
888                                                 if (index == 0) {  break; }
889                                         }else if (path[(pos-1)] == '/') { //you want the current working dir ./
890                                                 path = path.substr(0, pos);
891                                         }else if (pos == 1) { break;  //you are at the end
892                                         }else { mothurOut("cannot resolve path for " +  fileName + "\n"); return fileName; }
893                                 }
894                         
895                                 for (int i = index; i >= 0; i--) {
896                                         newFileName = dirs[i] +  "/" + newFileName;             
897                                 }
898                                 
899                                 newFileName =  "/" +  newFileName;
900                                 return newFileName;
901                         }       
902                 #else
903                         if (path.find("~") != string::npos) { //go to home directory
904                                 string homeDir = getenv ("HOMEPATH");
905                                 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
906                                 return newFileName;
907                         }else { //find path
908                                 if (path.rfind(".\\") == string::npos) { return fileName; } //already complete name
909                                 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
910                                                         
911                                 char *cwdpath = NULL;
912                                 cwdpath = getcwd(NULL, 0); // or _getcwd
913                                 if ( cwdpath != NULL) { cwd = cwdpath; }
914                                 else { cwd = "";  }
915                                 
916                                 //break apart the current working directory
917                                 vector<string> dirs;
918                                 while (cwd.find_first_of('\\') != -1) {
919                                         string dir = cwd.substr(0,cwd.find_first_of('\\'));
920                                         cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
921                                         dirs.push_back(dir);
922                 
923                                 }
924                                 //get last one
925                                 dirs.push_back(cwd);  //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
926                                         
927                                 int index = dirs.size()-1;
928                                         
929                                 while((pos = path.rfind(".\\")) != string::npos) { //while you don't have a complete path
930                                         if (pos == 0) { break;  //you are at the end
931                                         }else if (path[(pos-1)] == '.') { //you want your parent directory ../
932                                                 path = path.substr(0, pos-1);
933                                                 index--;
934                                                 if (index == 0) {  break; }
935                                         }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
936                                                 path = path.substr(0, pos);
937                                         }else if (pos == 1) { break;  //you are at the end
938                                         }else { mothurOut("cannot resolve path for " +  fileName + "\n"); return fileName; }
939                                 }
940                         
941                                 for (int i = index; i >= 0; i--) {
942                                         newFileName = dirs[i] +  "\\" + newFileName;            
943                                 }
944                                 
945                                 return newFileName;
946                         }
947                         
948                 #endif
949         }
950         }
951         catch(exception& e) {
952                 errorOut(e, "MothurOut", "getFullPathName");
953                 exit(1);
954         }       
955 }
956 /***********************************************************************/
957
958 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
959         try {
960                         //get full path name
961                         string completeFileName = getFullPathName(fileName);
962 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
963 #ifdef USE_COMPRESSION
964       // check for gzipped or bzipped file
965       if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
966         string tempName = string(tmpnam(0));
967         mkfifo(tempName.c_str(), 0666);
968         int fork_result = fork();
969         if (fork_result < 0) {
970           cerr << "Error forking.\n";
971           exit(1);
972         } else if (fork_result == 0) {
973           string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
974           cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
975           system(command.c_str());
976           cerr << "Done decompressing " << completeFileName << "\n";
977           mothurRemove(tempName);
978           exit(EXIT_SUCCESS);
979         } else {
980           cerr << "waiting on child process " << fork_result << "\n";
981           completeFileName = tempName;
982         }
983       }
984 #endif
985 #endif
986                         fileHandle.open(completeFileName.c_str());
987                         if(!fileHandle) {
988                                 //mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
989                                 return 1;
990                         }else {
991                                 //check for blank file
992                                 gobble(fileHandle);
993                                 return 0;
994                         }
995         }
996         catch(exception& e) {
997                 errorOut(e, "MothurOut", "openInputFile - no Error");
998                 exit(1);
999         }
1000 }
1001 /***********************************************************************/
1002
1003 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
1004         try {
1005
1006                 //get full path name
1007                 string completeFileName = getFullPathName(fileName);
1008 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1009 #ifdef USE_COMPRESSION
1010   // check for gzipped or bzipped file
1011   if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
1012     string tempName = string(tmpnam(0));
1013     mkfifo(tempName.c_str(), 0666);
1014     int fork_result = fork();
1015     if (fork_result < 0) {
1016       cerr << "Error forking.\n";
1017       exit(1);
1018     } else if (fork_result == 0) {
1019       string command = (endsWith(completeFileName, ".gz") ? "zcat " : "bzcat ") + completeFileName + string(" > ") + tempName;
1020       cerr << "Decompressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
1021       system(command.c_str());
1022       cerr << "Done decompressing " << completeFileName << "\n";
1023       mothurRemove(tempName);
1024       exit(EXIT_SUCCESS);
1025     } else {
1026       cerr << "waiting on child process " << fork_result << "\n";
1027       completeFileName = tempName;
1028     }
1029   }
1030 #endif
1031 #endif
1032
1033                 fileHandle.open(completeFileName.c_str());
1034                 if(!fileHandle) {
1035                         mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1036                         return 1;
1037                 }
1038                 else {
1039                         //check for blank file
1040                         gobble(fileHandle);
1041                         if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine();  }
1042                         
1043                         return 0;
1044                 }
1045         }
1046         catch(exception& e) {
1047                 errorOut(e, "MothurOut", "openInputFile");
1048                 exit(1);
1049         }       
1050 }
1051 /***********************************************************************/
1052
1053 int MothurOut::renameFile(string oldName, string newName){
1054         try {
1055                 ifstream inTest;
1056                 int exist = openInputFile(newName, inTest, "");
1057                 inTest.close();
1058                 
1059         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)          
1060                 if (exist == 0) { //you could open it so you want to delete it
1061                         string command = "rm " + newName;
1062                         system(command.c_str());
1063                 }
1064                                 
1065                 string command = "mv " + oldName + " " + newName;
1066                 system(command.c_str());
1067         #else
1068                 mothurRemove(newName);
1069                 int renameOk = rename(oldName.c_str(), newName.c_str());
1070         #endif
1071                 return 0;
1072                 
1073         }
1074         catch(exception& e) {
1075                 errorOut(e, "MothurOut", "renameFile");
1076                 exit(1);
1077         }       
1078 }
1079
1080 /***********************************************************************/
1081
1082 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
1083         try { 
1084         
1085                 string completeFileName = getFullPathName(fileName);
1086 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1087 #ifdef USE_COMPRESSION
1088     // check for gzipped file
1089     if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
1090       string tempName = string(tmpnam(0));
1091       mkfifo(tempName.c_str(), 0666);
1092       cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
1093       int fork_result = fork();
1094       if (fork_result < 0) {
1095         cerr << "Error forking.\n";
1096         exit(1);
1097       } else if (fork_result == 0) {
1098         string command = string(endsWith(completeFileName, ".gz") ?  "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
1099         system(command.c_str());
1100         exit(0);
1101       } else {
1102         completeFileName = tempName;
1103       }
1104     }
1105 #endif
1106 #endif
1107                 fileHandle.open(completeFileName.c_str(), ios::trunc);
1108                 if(!fileHandle) {
1109                         mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1110                         return 1;
1111                 }
1112                 else {
1113                         return 0;
1114                 }
1115         }
1116         catch(exception& e) {
1117                 errorOut(e, "MothurOut", "openOutputFile");
1118                 exit(1);
1119         }       
1120
1121 }
1122
1123 /**************************************************************************************************/
1124 int MothurOut::appendFiles(string temp, string filename) {
1125         try{
1126                 ofstream output;
1127                 ifstream input;
1128         
1129                 //open output file in append mode
1130                 openOutputFileAppend(filename, output);
1131                 int ableToOpen = openInputFile(temp, input, "no error");
1132                 //int ableToOpen = openInputFile(temp, input);
1133                 
1134                 int numLines = 0;
1135                 if (ableToOpen == 0) { //you opened it
1136             
1137             char buffer[4096];        
1138             while (!input.eof()) {
1139                 input.read(buffer, 4096);
1140                 output.write(buffer, input.gcount());
1141                 //count number of lines
1142                 for (int i = 0; i < input.gcount(); i++) {  if (buffer[i] == '\n') {numLines++;} }
1143             }
1144                         input.close();
1145                 }
1146                 
1147                 output.close();
1148                 
1149                 return numLines;
1150         }
1151         catch(exception& e) {
1152                 errorOut(e, "MothurOut", "appendFiles");
1153                 exit(1);
1154         }       
1155 }
1156
1157 /**************************************************************************************************/
1158 string MothurOut::sortFile(string distFile, string outputDir){
1159         try {   
1160         
1161                 //if (outputDir == "") {  outputDir += hasPath(distFile);  }
1162                 string outfile = getRootName(distFile) + "sorted.dist";
1163
1164                 
1165                 //if you can, use the unix sort since its been optimized for years
1166                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1167                         string command = "sort -n -k +3 " + distFile + " -o " + outfile;
1168                         system(command.c_str());
1169                 #else //you are stuck with my best attempt...
1170                         //windows sort does not have a way to specify a column, only a character in the line
1171                         //since we cannot assume that the distance will always be at the the same character location on each line
1172                         //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
1173                 
1174                         //read in file line by file and put distance first
1175                         string tempDistFile = distFile + ".temp";
1176                         ifstream input;
1177                         ofstream output;
1178                         openInputFile(distFile, input);
1179                         openOutputFile(tempDistFile, output);
1180
1181                         string firstName, secondName;
1182                         float dist;
1183                         while (input) {
1184                                 input >> firstName >> secondName >> dist;
1185                                 output << dist << '\t' << firstName << '\t' << secondName << endl;
1186                                 gobble(input);
1187                         }
1188                         input.close();
1189                         output.close();
1190                 
1191         
1192                         //sort using windows sort
1193                         string tempOutfile = outfile + ".temp";
1194                         string command = "sort " + tempDistFile + " /O " + tempOutfile;
1195                         system(command.c_str());
1196                 
1197                         //read in sorted file and put distance at end again
1198                         ifstream input2;
1199                         openInputFile(tempOutfile, input2);
1200                         openOutputFile(outfile, output);
1201                 
1202                         while (input2) {
1203                                 input2 >> dist >> firstName >> secondName;
1204                                 output << firstName << '\t' << secondName << '\t' << dist << endl;
1205                                 gobble(input2);
1206                         }
1207                         input2.close();
1208                         output.close();
1209                 
1210                         //remove temp files
1211                         mothurRemove(tempDistFile);
1212                         mothurRemove(tempOutfile);
1213                 #endif
1214                 
1215                 return outfile;
1216         }
1217         catch(exception& e) {
1218                 errorOut(e, "MothurOut", "sortFile");
1219                 exit(1);
1220         }       
1221 }
1222 /**************************************************************************************************/
1223 vector<unsigned long long> MothurOut::setFilePosFasta(string filename, int& num) {
1224         try {
1225                         vector<unsigned long long> positions;
1226                         ifstream inFASTA;
1227                         //openInputFile(filename, inFASTA);
1228                         inFASTA.open(filename.c_str(), ios::binary);
1229                                                 
1230                         string input;
1231                         unsigned long long count = 0;
1232                         while(!inFASTA.eof()){
1233                                 //input = getline(inFASTA); 
1234                                 //cout << input << '\t' << inFASTA.tellg() << endl;
1235                                 //if (input.length() != 0) {
1236                                 //      if(input[0] == '>'){    unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1);  cout << (pos - input.length() - 1) << endl; }
1237                                 //}
1238                                 //gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
1239                                 char c = inFASTA.get(); count++;
1240                                 if (c == '>') {
1241                                         positions.push_back(count-1);
1242                                         //cout << count << endl;
1243                                 }
1244                         }
1245                         inFASTA.close();
1246                 
1247                         num = positions.size();
1248                 
1249                         /*FILE * pFile;
1250                         long size;
1251                 
1252                         //get num bytes in file
1253                         pFile = fopen (filename.c_str(),"rb");
1254                         if (pFile==NULL) perror ("Error opening file");
1255                         else{
1256                                 fseek (pFile, 0, SEEK_END);
1257                                 size=ftell (pFile);
1258                                 fclose (pFile);
1259                         }*/
1260                         
1261                         unsigned long long size = positions[(positions.size()-1)];
1262                         ifstream in;
1263                         openInputFile(filename, in);
1264                         
1265                         in.seekg(size);
1266                 
1267                         while(in.get()){
1268                                 if(in.eof())            {       break;  }
1269                                 else                            {       size++; }
1270                         }
1271                         in.close();
1272                 
1273                         positions.push_back(size);
1274                         positions[0] = 0;
1275                 
1276                         return positions;
1277         }
1278         catch(exception& e) {
1279                 errorOut(e, "MothurOut", "setFilePosFasta");
1280                 exit(1);
1281         }
1282 }
1283 /**************************************************************************************************/
1284 vector<unsigned long long> MothurOut::setFilePosEachLine(string filename, int& num) {
1285         try {
1286                         filename = getFullPathName(filename);
1287                         
1288                         vector<unsigned long long> positions;
1289                         ifstream in;
1290                         //openInputFile(filename, in);
1291                         in.open(filename.c_str(), ios::binary);
1292                 
1293                         string input;
1294                         unsigned long long count = 0;
1295                         positions.push_back(0);
1296                 
1297                         while(!in.eof()){
1298                                 //getline counting reads
1299                                 char d = in.get(); count++;
1300                                 while ((d != '\n') && (d != '\r') && (d != '\f') && (d != in.eof()))    {
1301                                         //get next character
1302                                         d = in.get(); 
1303                                         count++;
1304                                 }
1305                                 
1306                                 if (!in.eof()) {
1307                                         d=in.get(); count++;
1308                                         while(isspace(d) && (d != in.eof()))            { d=in.get(); count++;}
1309                                 }
1310                                 positions.push_back(count-1);
1311                                 //cout << count-1 << endl;
1312                         }
1313                         in.close();
1314                 
1315                         num = positions.size()-1;
1316                 
1317                         FILE * pFile;
1318                         unsigned long long size;
1319                         
1320                         //get num bytes in file
1321                         pFile = fopen (filename.c_str(),"rb");
1322                         if (pFile==NULL) perror ("Error opening file");
1323                         else{
1324                                 fseek (pFile, 0, SEEK_END);
1325                                 size=ftell (pFile);
1326                                 fclose (pFile);
1327                         }
1328                 
1329                         positions[(positions.size()-1)] = size;
1330                 
1331                         return positions;
1332         }
1333         catch(exception& e) {
1334                 errorOut(e, "MothurOut", "setFilePosEachLine");
1335                 exit(1);
1336         }
1337 }
1338 /**************************************************************************************************/
1339
1340 vector<unsigned long long> MothurOut::divideFile(string filename, int& proc) {
1341         try{
1342                 vector<unsigned long long> filePos;
1343                 filePos.push_back(0);
1344                 
1345                 FILE * pFile;
1346                 unsigned long long size;
1347                 
1348                 filename = getFullPathName(filename);
1349         
1350                 //get num bytes in file
1351                 pFile = fopen (filename.c_str(),"rb");
1352                 if (pFile==NULL) perror ("Error opening file");
1353                 else{
1354                         fseek (pFile, 0, SEEK_END);
1355                         size=ftell (pFile);
1356                         fclose (pFile);
1357                 }
1358                 
1359         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1360                                 
1361                 //estimate file breaks
1362                 unsigned long long chunkSize = 0;
1363                 chunkSize = size / proc;
1364
1365                 //file to small to divide by processors
1366                 if (chunkSize == 0)  {  proc = 1;       filePos.push_back(size); return filePos;        }
1367         
1368                 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1369                 for (int i = 0; i < proc; i++) {
1370                         unsigned long long spot = (i+1) * chunkSize;
1371                         
1372                         ifstream in;
1373                         openInputFile(filename, in);
1374                         in.seekg(spot);
1375                         
1376                         //look for next '>'
1377                         unsigned long long newSpot = spot;
1378                         while (!in.eof()) {
1379                            char c = in.get();
1380                                 
1381                            if (c == '>') {   in.putback(c); newSpot = in.tellg(); break;  }
1382                            else if (int(c) == -1) { break; }
1383                                 
1384                         }
1385                 
1386                         //there was not another sequence before the end of the file
1387                         unsigned long long sanityPos = in.tellg();
1388
1389                         if (sanityPos == -1) {  break;  }
1390                         else {  filePos.push_back(newSpot);  }
1391                         
1392                         in.close();
1393                 }
1394                 
1395                 //save end pos
1396                 filePos.push_back(size);
1397                 
1398                 //sanity check filePos
1399                 for (int i = 0; i < (filePos.size()-1); i++) {
1400                         if (filePos[(i+1)] <= filePos[i]) {  filePos.erase(filePos.begin()+(i+1)); i--; }
1401                 }
1402
1403                 proc = (filePos.size() - 1);
1404 #else
1405                 mothurOut("[ERROR]: Windows version should not be calling the divideFile function."); mothurOutEndLine();
1406                 proc=1;
1407                 filePos.push_back(size);
1408 #endif
1409                 return filePos;
1410         }
1411         catch(exception& e) {
1412                 errorOut(e, "MothurOut", "divideFile");
1413                 exit(1);
1414         }
1415 }
1416 /**************************************************************************************************/
1417 int MothurOut::divideFile(string filename, int& proc, vector<string>& files) {
1418         try{
1419                 
1420                 vector<unsigned long long> filePos = divideFile(filename, proc);
1421                 
1422                 for (int i = 0; i < (filePos.size()-1); i++) {
1423                         
1424                         //read file chunk
1425                         ifstream in;
1426                         openInputFile(filename, in);
1427                         in.seekg(filePos[i]);
1428                         unsigned long long size = filePos[(i+1)] - filePos[i];
1429                         char* chunk = new char[size];
1430                         in.read(chunk, size);
1431                         in.close();
1432                         
1433                         //open new file
1434                         string fileChunkName = filename + "." + toString(i) + ".tmp";
1435                         ofstream out; 
1436                         openOutputFile(fileChunkName, out);
1437                         
1438                         out << chunk << endl;
1439                         out.close();
1440                         delete[] chunk;
1441                         
1442                         //save name
1443                         files.push_back(fileChunkName);
1444                 }
1445                                 
1446                 return 0;
1447         }
1448         catch(exception& e) {
1449                 errorOut(e, "MothurOut", "divideFile");
1450                 exit(1);
1451         }
1452 }
1453 /***********************************************************************/
1454
1455 bool MothurOut::isTrue(string f){
1456         try {
1457                 
1458                 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1459                 
1460                 if ((f == "TRUE") || (f == "T")) {      return true;    }
1461                 else {  return false;  }
1462         }
1463         catch(exception& e) {
1464                 errorOut(e, "MothurOut", "isTrue");
1465                 exit(1);
1466         }
1467 }
1468
1469 /***********************************************************************/
1470
1471 float MothurOut::roundDist(float dist, int precision){
1472         try {
1473                 return int(dist * precision + 0.5)/float(precision);
1474         }
1475         catch(exception& e) {
1476                 errorOut(e, "MothurOut", "roundDist");
1477                 exit(1);
1478         }
1479 }
1480 /***********************************************************************/
1481
1482 float MothurOut::ceilDist(float dist, int precision){
1483         try {
1484                 return int(ceil(dist * precision))/float(precision);
1485         }
1486         catch(exception& e) {
1487                 errorOut(e, "MothurOut", "ceilDist");
1488                 exit(1);
1489         }
1490 }
1491 /***********************************************************************/
1492
1493 vector<string> MothurOut::splitWhiteSpace(string& rest, char buffer[], int size){
1494         try {
1495         vector<string> pieces;
1496         
1497         for (int i = 0; i < size; i++) {
1498             if (!isspace(buffer[i]))  { rest += buffer[i];  }
1499             else {
1500                 if (rest != "") { pieces.push_back(rest);  rest = ""; }
1501                 while (i < size) {  //gobble white space
1502                     if (isspace(buffer[i])) { i++; }
1503                     else { rest = buffer[i];  break; } //cout << "next piece buffer = " << nextPiece << endl;
1504                 } 
1505             }
1506         }
1507         
1508         return pieces;
1509         }
1510         catch(exception& e) {
1511                 errorOut(e, "MothurOut", "splitWhiteSpace");
1512                 exit(1);
1513         }
1514 }
1515 /***********************************************************************/
1516 vector<string> MothurOut::splitWhiteSpace(string input){
1517         try {
1518         vector<string> pieces;
1519         string rest = "";
1520         
1521         for (int i = 0; i < input.length(); i++) {
1522             if (!isspace(input[i]))  { rest += input[i];  }
1523             else {
1524                 if (rest != "") { pieces.push_back(rest);  rest = ""; }
1525                 while (i < input.length()) {  //gobble white space
1526                     if (isspace(input[i])) { i++; }
1527                     else { rest = input[i];  break; } //cout << "next piece buffer = " << nextPiece << endl;
1528                 } 
1529             }
1530         }
1531         
1532         if (rest != "") { pieces.push_back(rest); }
1533         
1534         return pieces;
1535         }
1536         catch(exception& e) {
1537                 errorOut(e, "MothurOut", "splitWhiteSpace");
1538                 exit(1);
1539         }
1540 }
1541 //**********************************************************************************************************************
1542 int MothurOut::readTax(string namefile, map<string, string>& taxMap) {
1543         try {
1544                 
1545         //open input file
1546                 ifstream in;
1547                 openInputFile(namefile, in);
1548         
1549         string rest = "";
1550         char buffer[4096];
1551         bool pairDone = false;
1552         bool columnOne = true;
1553         string firstCol, secondCol;
1554         
1555                 while (!in.eof()) {
1556                         if (control_pressed) { break; }
1557                         
1558             in.read(buffer, 4096);
1559             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1560             
1561             for (int i = 0; i < pieces.size(); i++) {
1562                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1563                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1564                 
1565                 if (pairDone) { 
1566                     //are there confidence scores, if so remove them
1567                     if (secondCol.find_first_of('(') != -1) {  removeConfidences(secondCol);    }
1568                     taxMap[firstCol] = secondCol;
1569                     if (debug) {  mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n");  }
1570                     pairDone = false; 
1571                 }
1572             }
1573                 }
1574                 in.close();
1575                 
1576                 return taxMap.size();
1577
1578         }
1579         catch(exception& e) {
1580                 errorOut(e, "MothurOut", "readTax");
1581                 exit(1);
1582         }
1583 }
1584 /**********************************************************************************************************************/
1585 int MothurOut::readNames(string namefile, map<string, string>& nameMap, bool redund) { 
1586         try {
1587                 
1588                 //open input file
1589                 ifstream in;
1590                 openInputFile(namefile, in);
1591         
1592         string rest = "";
1593         char buffer[4096];
1594         bool pairDone = false;
1595         bool columnOne = true;
1596         string firstCol, secondCol;
1597         
1598                 while (!in.eof()) {
1599                         if (control_pressed) { break; }
1600                         
1601             in.read(buffer, 4096);
1602             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1603             
1604             for (int i = 0; i < pieces.size(); i++) {
1605                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1606                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1607                 
1608                 if (pairDone) { 
1609                     //parse names into vector
1610                     vector<string> theseNames;
1611                     splitAtComma(secondCol, theseNames);
1612                     for (int i = 0; i < theseNames.size(); i++) {  nameMap[theseNames[i]] = firstCol;  }
1613                     pairDone = false; 
1614                 }
1615             }
1616                 }
1617                 in.close();
1618                 
1619                 return nameMap.size();
1620                 
1621         }
1622         catch(exception& e) {
1623                 errorOut(e, "MothurOut", "readNames");
1624                 exit(1);
1625         }
1626 }
1627 /**********************************************************************************************************************/
1628 int MothurOut::readNames(string namefile, map<string, string>& nameMap, int flip) { 
1629         try {
1630                 
1631                 //open input file
1632                 ifstream in;
1633                 openInputFile(namefile, in);
1634         
1635         string rest = "";
1636         char buffer[4096];
1637         bool pairDone = false;
1638         bool columnOne = true;
1639         string firstCol, secondCol;
1640         
1641                 while (!in.eof()) {
1642                         if (control_pressed) { break; }
1643                         
1644             in.read(buffer, 4096);
1645             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1646             
1647             for (int i = 0; i < pieces.size(); i++) {
1648                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1649                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1650                 
1651                 if (pairDone) { 
1652                     nameMap[secondCol] = firstCol;
1653                     pairDone = false; 
1654                 }
1655             }
1656                 }
1657                 in.close();
1658                 
1659                 return nameMap.size();
1660                 
1661         }
1662         catch(exception& e) {
1663                 errorOut(e, "MothurOut", "readNames");
1664                 exit(1);
1665         }
1666 }
1667 /**********************************************************************************************************************/
1668 int MothurOut::readNames(string namefile, map<string, string>& nameMap, map<string, int>& nameCount) { 
1669         try {
1670                 nameMap.clear(); nameCount.clear();
1671                 //open input file
1672                 ifstream in;
1673                 openInputFile(namefile, in);
1674         
1675         string rest = "";
1676         char buffer[4096];
1677         bool pairDone = false;
1678         bool columnOne = true;
1679         string firstCol, secondCol;
1680         
1681                 while (!in.eof()) {
1682                         if (control_pressed) { break; }
1683                         
1684             in.read(buffer, 4096);
1685             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1686             
1687             for (int i = 0; i < pieces.size(); i++) {
1688                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1689                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1690                 
1691                 if (pairDone) { 
1692                     //parse names into vector
1693                     vector<string> theseNames;
1694                     splitAtComma(secondCol, theseNames);
1695                     for (int i = 0; i < theseNames.size(); i++) {  nameMap[theseNames[i]] = firstCol;  }
1696                     nameCount[firstCol] = theseNames.size();
1697                     pairDone = false; 
1698                 }
1699             }
1700                 }
1701                 in.close();
1702                 
1703                 return nameMap.size();
1704                 
1705         }
1706         catch(exception& e) {
1707                 errorOut(e, "MothurOut", "readNames");
1708                 exit(1);
1709         }
1710 }
1711 /**********************************************************************************************************************/
1712 int MothurOut::readNames(string namefile, map<string, string>& nameMap) { 
1713         try {
1714                 
1715                 //open input file
1716                 ifstream in;
1717                 openInputFile(namefile, in);
1718
1719         string rest = "";
1720         char buffer[4096];
1721         bool pairDone = false;
1722         bool columnOne = true;
1723         string firstCol, secondCol;
1724         
1725                 while (!in.eof()) {
1726                         if (control_pressed) { break; }
1727                         
1728             in.read(buffer, 4096);
1729             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1730              
1731             for (int i = 0; i < pieces.size(); i++) {
1732                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1733                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1734                 
1735                 if (pairDone) { nameMap[firstCol] = secondCol; pairDone = false; }
1736             }
1737                 }
1738                 in.close();
1739                 
1740                 return nameMap.size();
1741                 
1742         }
1743         catch(exception& e) {
1744                 errorOut(e, "MothurOut", "readNames");
1745                 exit(1);
1746         }
1747 }
1748 /**********************************************************************************************************************/
1749 int MothurOut::readNames(string namefile, map<string, vector<string> >& nameMap) { 
1750         try {
1751                 
1752                 //open input file
1753                 ifstream in;
1754                 openInputFile(namefile, in);
1755                 
1756         string rest = "";
1757         char buffer[4096];
1758         bool pairDone = false;
1759         bool columnOne = true;
1760         string firstCol, secondCol;
1761         
1762                 while (!in.eof()) {
1763                         if (control_pressed) { break; }
1764                         
1765             in.read(buffer, 4096);
1766             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1767             
1768             for (int i = 0; i < pieces.size(); i++) {
1769                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1770                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1771                 
1772                 if (pairDone) { 
1773                     vector<string> temp;
1774                     splitAtComma(secondCol, temp);
1775                     nameMap[firstCol] = temp;
1776                     pairDone = false;  
1777                 } 
1778             }
1779                 }
1780                 in.close();
1781         
1782                 return nameMap.size();
1783         }
1784         catch(exception& e) {
1785                 errorOut(e, "MothurOut", "readNames");
1786                 exit(1);
1787         }
1788 }
1789 /**********************************************************************************************************************/
1790 map<string, int> MothurOut::readNames(string namefile) { 
1791         try {
1792                 
1793                 map<string, int> nameMap;
1794                 
1795                 //open input file
1796                 ifstream in;
1797                 openInputFile(namefile, in);
1798                 
1799         string rest = "";
1800         char buffer[4096];
1801         bool pairDone = false;
1802         bool columnOne = true;
1803         string firstCol, secondCol;
1804         
1805                 while (!in.eof()) {
1806                         if (control_pressed) { break; }
1807                         
1808             in.read(buffer, 4096);
1809             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1810             
1811             for (int i = 0; i < pieces.size(); i++) {
1812                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1813                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1814                 
1815                 if (pairDone) { 
1816                     int num = getNumNames(secondCol);
1817                     nameMap[firstCol] = num;
1818                     pairDone = false;  
1819                 } 
1820             }
1821                 }
1822         in.close();
1823                 
1824                 return nameMap;
1825                 
1826         }
1827         catch(exception& e) {
1828                 errorOut(e, "MothurOut", "readNames");
1829                 exit(1);
1830         }
1831 }
1832 /**********************************************************************************************************************/
1833 int MothurOut::readNames(string namefile, vector<seqPriorityNode>& nameVector, map<string, string>& fastamap) { 
1834         try {
1835                 int error = 0;
1836                 
1837                 //open input file
1838                 ifstream in;
1839                 openInputFile(namefile, in);
1840                 
1841         string rest = "";
1842         char buffer[4096];
1843         bool pairDone = false;
1844         bool columnOne = true;
1845         string firstCol, secondCol;
1846         
1847                 while (!in.eof()) {
1848                         if (control_pressed) { break; }
1849                         
1850             in.read(buffer, 4096);
1851             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1852             
1853             for (int i = 0; i < pieces.size(); i++) {
1854                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1855                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1856                 
1857                 if (pairDone) { 
1858                     int num = getNumNames(secondCol);
1859                     
1860                     map<string, string>::iterator it = fastamap.find(firstCol);
1861                     if (it == fastamap.end()) {
1862                         error = 1;
1863                         mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
1864                     }else {
1865                         seqPriorityNode temp(num, it->second, firstCol);
1866                         nameVector.push_back(temp);
1867                     }
1868                     
1869                     pairDone = false;  
1870                 } 
1871             }
1872                 }
1873         in.close();
1874         
1875                 return error;
1876         }
1877         catch(exception& e) {
1878                 errorOut(e, "MothurOut", "readNames");
1879                 exit(1);
1880         }
1881 }
1882 //**********************************************************************************************************************
1883 set<string> MothurOut::readAccnos(string accnosfile){
1884         try {
1885                 set<string> names;
1886                 ifstream in;
1887                 openInputFile(accnosfile, in);
1888                 string name;
1889                 
1890         string rest = "";
1891         char buffer[4096];
1892         
1893                 while (!in.eof()) {
1894                         if (control_pressed) { break; }
1895                         
1896             in.read(buffer, 4096);
1897             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1898             
1899             for (int i = 0; i < pieces.size(); i++) {  names.insert(pieces[i]);  }
1900         }
1901                 in.close();     
1902                 
1903                 return names;
1904         }
1905         catch(exception& e) {
1906                 errorOut(e, "MothurOut", "readAccnos");
1907                 exit(1);
1908         }
1909 }
1910 //**********************************************************************************************************************
1911 int MothurOut::readAccnos(string accnosfile, vector<string>& names){
1912         try {
1913         names.clear();
1914                 ifstream in;
1915                 openInputFile(accnosfile, in);
1916                 string name;
1917                 
1918         string rest = "";
1919         char buffer[4096];
1920         
1921                 while (!in.eof()) {
1922                         if (control_pressed) { break; }
1923                         
1924             in.read(buffer, 4096);
1925             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1926             
1927             for (int i = 0; i < pieces.size(); i++) {  names.push_back(pieces[i]);  }
1928         }
1929                 in.close();     
1930                 
1931                 return 0;
1932         }
1933         catch(exception& e) {
1934                 errorOut(e, "MothurOut", "readAccnos");
1935                 exit(1);
1936         }
1937 }
1938 /***********************************************************************/
1939
1940 int MothurOut::getNumNames(string names){
1941         try {
1942                 int count = 0;
1943                 
1944                 if(names != ""){
1945                         count = 1;
1946                         for(int i=0;i<names.size();i++){
1947                                 if(names[i] == ','){
1948                                         count++;
1949                                 }
1950                         }
1951                 }
1952                 
1953                 return count;
1954         }
1955         catch(exception& e) {
1956                 errorOut(e, "MothurOut", "getNumNames");
1957                 exit(1);
1958         }
1959 }
1960 /***********************************************************************/
1961
1962 int MothurOut::getNumChar(string line, char c){
1963         try {
1964                 int count = 0;
1965                 
1966                 if(line != ""){
1967                         for(int i=0;i<line.size();i++){
1968                                 if(line[i] == c){
1969                                         count++;
1970                                 }
1971                         }
1972                 }
1973                 
1974                 return count;
1975         }
1976         catch(exception& e) {
1977                 errorOut(e, "MothurOut", "getNumChar");
1978                 exit(1);
1979         }
1980 }
1981 /***********************************************************************/
1982 int MothurOut::mothurRemove(string filename){
1983         try {
1984                 filename = getFullPathName(filename);
1985                 int error = remove(filename.c_str());
1986                 //if (error != 0) { 
1987                 //      if (errno != ENOENT) { //ENOENT == file does not exist
1988                 //              string message = "Error deleting file " + filename;
1989                 //              perror(message.c_str()); 
1990                 //      }
1991                 //}
1992                 return error;
1993         }
1994         catch(exception& e) {
1995                 errorOut(e, "MothurOut", "mothurRemove");
1996                 exit(1);
1997         }
1998 }
1999 /***********************************************************************/
2000 bool MothurOut::mothurConvert(string item, int& num){
2001         try {
2002                 bool error = false;
2003                 
2004                 if (isNumeric1(item)) {
2005                         convert(item, num);
2006                 }else {
2007                         num = 0;
2008                         error = true;
2009                         mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2010                         commandInputsConvertError = true;
2011                 }
2012                 
2013                 return error;
2014         }
2015         catch(exception& e) {
2016                 errorOut(e, "MothurOut", "mothurConvert");
2017                 exit(1);
2018         }
2019 }
2020 /***********************************************************************/
2021 bool MothurOut::mothurConvert(string item, intDist& num){
2022         try {
2023                 bool error = false;
2024                 
2025                 if (isNumeric1(item)) {
2026                         convert(item, num);
2027                 }else {
2028                         num = 0;
2029                         error = true;
2030                         mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2031                         commandInputsConvertError = true;
2032                 }
2033                 
2034                 return error;
2035         }
2036         catch(exception& e) {
2037                 errorOut(e, "MothurOut", "mothurConvert");
2038                 exit(1);
2039         }
2040 }
2041
2042 /***********************************************************************/
2043 bool MothurOut::isNumeric1(string stringToCheck){
2044         try {
2045                 bool numeric = false;
2046                 
2047                 if(stringToCheck.find_first_not_of("0123456789.-") == string::npos) { numeric = true; }
2048                         
2049                 return numeric;
2050         }
2051         catch(exception& e) {
2052                 errorOut(e, "MothurOut", "isNumeric1");
2053                 exit(1);
2054         }
2055         
2056 }
2057 /***********************************************************************/
2058 bool MothurOut::mothurConvert(string item, float& num){
2059         try {
2060                 bool error = false;
2061                 
2062                 if (isNumeric1(item)) {
2063                         convert(item, num);
2064                 }else {
2065                         num = 0;
2066                         error = true;
2067                         mothurOut("[ERROR]: cannot convert " + item + " to a float."); mothurOutEndLine();
2068                         commandInputsConvertError = true;
2069                 }
2070                 
2071                 return error;
2072         }
2073         catch(exception& e) {
2074                 errorOut(e, "MothurOut", "mothurConvert");
2075                 exit(1);
2076         }
2077 }
2078 /***********************************************************************/
2079 bool MothurOut::mothurConvert(string item, double& num){
2080         try {
2081                 bool error = false;
2082                 
2083                 if (isNumeric1(item)) {
2084                         convert(item, num);
2085                 }else {
2086                         num = 0;
2087                         error = true;
2088                         mothurOut("[ERROR]: cannot convert " + item + " to a double."); mothurOutEndLine();
2089                         commandInputsConvertError = true;
2090                 }
2091                 
2092                 return error;
2093         }
2094         catch(exception& e) {
2095                 errorOut(e, "MothurOut", "mothurConvert");
2096                 exit(1);
2097         }
2098 }
2099 /**************************************************************************************************/
2100
2101 vector<vector<double> > MothurOut::binomial(int maxOrder){
2102         try {
2103         vector<vector<double> > binomial(maxOrder+1);
2104         
2105     for(int i=0;i<=maxOrder;i++){
2106                 binomial[i].resize(maxOrder+1);
2107                 binomial[i][0]=1;
2108                 binomial[0][i]=0;
2109     }
2110     binomial[0][0]=1;
2111         
2112     binomial[1][0]=1;
2113     binomial[1][1]=1;
2114         
2115     for(int i=2;i<=maxOrder;i++){
2116                 binomial[1][i]=0;
2117     }
2118         
2119     for(int i=2;i<=maxOrder;i++){
2120                 for(int j=1;j<=maxOrder;j++){
2121                         if(i==j){       binomial[i][j]=1;                                                                       }
2122                         if(j>i) {       binomial[i][j]=0;                                                                       }
2123                         else    {       binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j];     }
2124                 }
2125     }
2126         
2127         return binomial;
2128         
2129         }
2130         catch(exception& e) {
2131                 errorOut(e, "MothurOut", "binomial");
2132                 exit(1);
2133         }
2134 }
2135 /**************************************************************************************************/
2136 unsigned int MothurOut::fromBase36(string base36){
2137         try {
2138                 unsigned int num = 0;
2139                 
2140                 map<char, int> converts;
2141                 converts['A'] = 0;
2142                 converts['a'] = 0;
2143                 converts['B'] = 1;
2144                 converts['b'] = 1;
2145                 converts['C'] = 2;
2146                 converts['c'] = 2;
2147                 converts['D'] = 3;
2148                 converts['d'] = 3;
2149                 converts['E'] = 4;
2150                 converts['e'] = 4;
2151                 converts['F'] = 5;
2152                 converts['f'] = 5;
2153                 converts['G'] = 6;
2154                 converts['g'] = 6;
2155                 converts['H'] = 7;
2156                 converts['h'] = 7;
2157                 converts['I'] = 8;
2158                 converts['i'] = 8;
2159                 converts['J'] = 9;
2160                 converts['j'] = 9;
2161                 converts['K'] = 10;
2162                 converts['k'] = 10;
2163                 converts['L'] = 11;
2164                 converts['l'] = 11;
2165                 converts['M'] = 12;
2166                 converts['m'] = 12;
2167                 converts['N'] = 13;
2168                 converts['n'] = 13;
2169                 converts['O'] = 14;
2170                 converts['o'] = 14;
2171                 converts['P'] = 15;
2172                 converts['p'] = 15;
2173                 converts['Q'] = 16;
2174                 converts['q'] = 16;
2175                 converts['R'] = 17;
2176                 converts['r'] = 17;
2177                 converts['S'] = 18;
2178                 converts['s'] = 18;
2179                 converts['T'] = 19;
2180                 converts['t'] = 19;
2181                 converts['U'] = 20;
2182                 converts['u'] = 20;
2183                 converts['V'] = 21;
2184                 converts['v'] = 21;
2185                 converts['W'] = 22;
2186                 converts['w'] = 22;
2187                 converts['X'] = 23;
2188                 converts['x'] = 23;
2189                 converts['Y'] = 24;
2190                 converts['y'] = 24;
2191                 converts['Z'] = 25;
2192                 converts['z'] = 25;
2193                 converts['0'] = 26;
2194                 converts['1'] = 27;
2195                 converts['2'] = 28;
2196                 converts['3'] = 29;
2197                 converts['4'] = 30;
2198                 converts['5'] = 31;
2199                 converts['6'] = 32;
2200                 converts['7'] = 33;
2201                 converts['8'] = 34;
2202                 converts['9'] = 35;             
2203                 
2204                 int i = 0;
2205                 while (i < base36.length()) {
2206                         char c = base36[i];
2207                         num = 36 * num + converts[c];
2208                         i++;
2209                 }
2210                 
2211                 return num;
2212                 
2213         }
2214         catch(exception& e) {
2215                 errorOut(e, "MothurOut", "fromBase36");
2216                 exit(1);
2217         }
2218 }
2219 /***********************************************************************/
2220
2221 int MothurOut::factorial(int num){
2222         try {
2223                 int total = 1;
2224                 
2225                 for (int i = 1; i <= num; i++) {
2226                         total *= i;
2227                 }
2228                 
2229                 return total;
2230         }
2231         catch(exception& e) {
2232                 errorOut(e, "MothurOut", "factorial");
2233                 exit(1);
2234         }
2235 }
2236 /***********************************************************************/
2237
2238 int MothurOut::getNumSeqs(ifstream& file){
2239         try {
2240                 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
2241                 file.seekg(0);
2242                 return numSeqs;
2243         }
2244         catch(exception& e) {
2245                 errorOut(e, "MothurOut", "getNumSeqs");
2246                 exit(1);
2247         }       
2248 }
2249 /***********************************************************************/
2250 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
2251         try {
2252                 string input;
2253                 numSeqs = 0;
2254                 while(!file.eof()){
2255                         input = getline(file);
2256                         if (input.length() != 0) {
2257                                 if(input[0] == '>'){ numSeqs++; }
2258                         }
2259                 }
2260         }
2261         catch(exception& e) {
2262                 errorOut(e, "MothurOut", "getNumSeqs");
2263                 exit(1);
2264         }       
2265 }
2266 /***********************************************************************/
2267
2268 //This function parses the estimator options and puts them in a vector
2269 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
2270         try {
2271                 string individual = "";
2272                 int estimLength = estim.size();
2273                 for(int i=0;i<estimLength;i++){
2274                         if(estim[i] == symbol){
2275                                 container.push_back(individual);
2276                                 individual = "";                                
2277                         }
2278                         else{
2279                                 individual += estim[i];
2280                         }
2281                 }
2282                 container.push_back(individual);
2283
2284         }
2285         catch(exception& e) {
2286                 errorOut(e, "MothurOut", "splitAtChar");
2287                 exit(1);
2288         }       
2289 }
2290
2291 /***********************************************************************/
2292
2293 //This function parses the estimator options and puts them in a vector
2294 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
2295         try {
2296                 string individual = "";
2297                 int estimLength = estim.size();
2298                 for(int i=0;i<estimLength;i++){
2299                         if(estim[i] == '-'){
2300                                 container.push_back(individual);
2301                                 individual = "";                                
2302                         }
2303                         else{
2304                                 individual += estim[i];
2305                         }
2306                 }
2307                 container.push_back(individual);
2308
2309         
2310         /*      string individual;
2311                 
2312                 while (estim.find_first_of('-') != -1) {
2313                         individual = estim.substr(0,estim.find_first_of('-'));
2314                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
2315                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
2316                                 container.push_back(individual);
2317                         }
2318                 }
2319                 //get last one
2320                 container.push_back(estim); */
2321         }
2322         catch(exception& e) {
2323                 errorOut(e, "MothurOut", "splitAtDash");
2324                 exit(1);
2325         }       
2326 }
2327
2328 /***********************************************************************/
2329 //This function parses the label options and puts them in a set
2330 void MothurOut::splitAtDash(string& estim, set<string>& container) {
2331         try {
2332                 string individual = "";
2333                 int estimLength = estim.size();
2334                 for(int i=0;i<estimLength;i++){
2335                         if(estim[i] == '-'){
2336                                 container.insert(individual);
2337                                 individual = "";                                
2338                         }
2339                         else{
2340                                 individual += estim[i];
2341                         }
2342                 }
2343                 container.insert(individual);
2344
2345         //      string individual;
2346                 
2347         //      while (estim.find_first_of('-') != -1) {
2348         //              individual = estim.substr(0,estim.find_first_of('-'));
2349         //              if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
2350         //                      estim = estim.substr(estim.find_first_of('-')+1, estim.length());
2351         //                      container.insert(individual);
2352         //              }
2353         //      }
2354                 //get last one
2355         //      container.insert(estim);
2356         
2357         }
2358         catch(exception& e) {
2359                 errorOut(e, "MothurOut", "splitAtDash");
2360                 exit(1);
2361         }       
2362 }
2363 /***********************************************************************/
2364 //This function parses the line options and puts them in a set
2365 void MothurOut::splitAtDash(string& estim, set<int>& container) {
2366         try {
2367                 string individual;
2368                 int lineNum;
2369                 
2370                 while (estim.find_first_of('-') != -1) {
2371                         individual = estim.substr(0,estim.find_first_of('-'));
2372                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
2373                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
2374                                 convert(individual, lineNum); //convert the string to int
2375                                 container.insert(lineNum);
2376                         }
2377                 }
2378                 //get last one
2379                 convert(estim, lineNum); //convert the string to int
2380                 container.insert(lineNum);
2381         }
2382         catch(exception& e) {
2383                 errorOut(e, "MothurOut", "splitAtDash");
2384                 exit(1);
2385         }       
2386 }
2387 /***********************************************************************/
2388 string MothurOut::makeList(vector<string>& names) {
2389         try {
2390                 string list = "";
2391         
2392         if (names.size() == 0) { return list; }
2393                 
2394         for (int i = 0; i < names.size()-1; i++) { list += names[i] + ",";  }
2395         
2396         //get last name
2397         list += names[names.size()-1];
2398         
2399         return list;
2400     }
2401         catch(exception& e) {
2402                 errorOut(e, "MothurOut", "makeList");
2403                 exit(1);
2404         }       
2405 }
2406
2407 /***********************************************************************/
2408 //This function parses the a string and puts peices in a vector
2409 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
2410         try {
2411                 string individual = "";
2412                 int estimLength = estim.size();
2413                 for(int i=0;i<estimLength;i++){
2414                         if(estim[i] == ','){
2415                                 container.push_back(individual);
2416                                 individual = "";                                
2417                         }
2418                         else{
2419                                 individual += estim[i];
2420                         }
2421                 }
2422                 container.push_back(individual);
2423                 
2424                 
2425                 
2426                 
2427 //              string individual;
2428 //              
2429 //              while (estim.find_first_of(',') != -1) {
2430 //                      individual = estim.substr(0,estim.find_first_of(','));
2431 //                      if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
2432 //                              estim = estim.substr(estim.find_first_of(',')+1, estim.length());
2433 //                              container.push_back(individual);
2434 //                      }
2435 //              }
2436 //              //get last one
2437 //              container.push_back(estim);
2438         }
2439         catch(exception& e) {
2440                 errorOut(e, "MothurOut", "splitAtComma");
2441                 exit(1);
2442         }       
2443 }
2444 /***********************************************************************/
2445 //This function splits up the various option parameters
2446 void MothurOut::splitAtChar(string& prefix, string& suffix, char c){
2447         try {
2448                 prefix = suffix.substr(0,suffix.find_first_of(c));
2449                 if ((suffix.find_first_of(c)+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
2450                         suffix = suffix.substr(suffix.find_first_of(c)+1, suffix.length());
2451                         string space = " ";
2452                         while(suffix.at(0) == ' ')
2453                                 suffix = suffix.substr(1, suffix.length());
2454                 }
2455         
2456         }
2457         catch(exception& e) {
2458                 errorOut(e, "MothurOut", "splitAtComma");
2459                 exit(1);
2460         }       
2461 }
2462
2463 /***********************************************************************/
2464
2465 //This function splits up the various option parameters
2466 void MothurOut::splitAtComma(string& prefix, string& suffix){
2467         try {
2468                 prefix = suffix.substr(0,suffix.find_first_of(','));
2469                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
2470                         suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
2471                         string space = " ";
2472                         while(suffix.at(0) == ' ')
2473                                 suffix = suffix.substr(1, suffix.length());
2474                 }
2475
2476         }
2477         catch(exception& e) {
2478                 errorOut(e, "MothurOut", "splitAtComma");
2479                 exit(1);
2480         }       
2481 }
2482 /***********************************************************************/
2483
2484 //This function separates the key value from the option value i.e. dist=96_...
2485 void MothurOut::splitAtEquals(string& key, string& value){              
2486         try {
2487                 if(value.find_first_of('=') != -1){
2488                         key = value.substr(0,value.find_first_of('='));
2489                         if ((value.find_first_of('=')+1) <= value.length()) {
2490                                 value = value.substr(value.find_first_of('=')+1, value.length());
2491                         }
2492                 }else{
2493                         key = value;
2494                         value = 1;
2495                 }
2496         }
2497         catch(exception& e) {
2498                 errorOut(e, "MothurOut", "splitAtEquals");
2499                 exit(1);
2500         }       
2501 }
2502
2503 /**************************************************************************************************/
2504
2505 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
2506         try {
2507                 for (int i = 0; i < Groups.size(); i++) {
2508                         if (groupname == Groups[i]) { return true; }
2509                 }
2510                 return false;
2511         }
2512         catch(exception& e) {
2513                 errorOut(e, "MothurOut", "inUsersGroups");
2514                 exit(1);
2515         }       
2516 }
2517 /**************************************************************************************************/
2518 //returns true if any of the strings in first vector are in second vector
2519 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
2520         try {
2521                 
2522                 for (int i = 0; i < groupnames.size(); i++) {
2523                         if (inUsersGroups(groupnames[i], Groups)) { return true; }
2524                 }
2525                 return false;
2526         }
2527         catch(exception& e) {
2528                 errorOut(e, "MothurOut", "inUsersGroups");
2529                 exit(1);
2530         }       
2531 }
2532 /***********************************************************************/
2533 //this function determines if the user has given us labels that are smaller than the given label.
2534 //if so then it returns true so that the calling function can run the previous valid distance.
2535 //it's a "smart" distance function.  It also checks for invalid labels.
2536 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
2537         try {
2538                 
2539                 set<string>::iterator it;
2540                 vector<float> orderFloat;
2541                 map<string, float> userMap;  //the conversion process removes trailing 0's which we need to put back
2542                 map<string, float>::iterator it2;
2543                 float labelFloat;
2544                 bool smaller = false;
2545                 
2546                 //unique is the smallest line
2547                 if (label == "unique") {  return false;  }
2548                 else { 
2549                         if (convertTestFloat(label, labelFloat)) {
2550                                 convert(label, labelFloat); 
2551                         }else { //cant convert 
2552                                 return false;
2553                         }
2554                 }
2555                 
2556                 //go through users set and make them floats
2557                 for(it = userLabels.begin(); it != userLabels.end();) {
2558                         
2559                         float temp;
2560                         if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
2561                                 convert(*it, temp);
2562                                 orderFloat.push_back(temp);
2563                                 userMap[*it] = temp;
2564                                 it++;
2565                         }else if (*it == "unique") { 
2566                                 orderFloat.push_back(-1.0);
2567                                 userMap["unique"] = -1.0;
2568                                 it++;
2569                         }else {
2570                                 if (errorOff == "") {  mothurOut(*it + " is not a valid label."); mothurOutEndLine();  }
2571                                 userLabels.erase(it++); 
2572                         }
2573                 }
2574                 
2575                 //sort order
2576                 sort(orderFloat.begin(), orderFloat.end());
2577                 
2578                 /*************************************************/
2579                 //is this label bigger than any of the users labels
2580                 /*************************************************/
2581                                 
2582                 //loop through order until you find a label greater than label
2583                 for (int i = 0; i < orderFloat.size(); i++) {
2584                         if (orderFloat[i] < labelFloat) {
2585                                 smaller = true;
2586                                 if (orderFloat[i] == -1) { 
2587                                         if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
2588                                         userLabels.erase("unique");
2589                                 }
2590                                 else {  
2591                                         if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
2592                                         string s = "";
2593                                         for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {  
2594                                                 if (it2->second == orderFloat[i]) {  
2595                                                         s = it2->first;  
2596                                                         //remove small labels
2597                                                         userLabels.erase(s);
2598                                                         break;
2599                                                 }
2600                                         }
2601                                         if (errorOff == "") {mothurOut( s +  ". I will use the next smallest distance. "); mothurOutEndLine(); }
2602                                 }
2603                         //since they are sorted once you find a bigger one stop looking
2604                         }else { break; }
2605                 }
2606                 
2607                 return smaller;
2608                                                 
2609         }
2610         catch(exception& e) {
2611                 errorOut(e, "MothurOut", "anyLabelsToProcess");
2612                 exit(1);
2613         }       
2614 }
2615
2616 /**************************************************************************************************/
2617 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
2618         try {
2619                 
2620                 bool good = true;
2621                 
2622                 string line = getline(file);  
2623
2624                 //before we added this check
2625                 if (line[0] != '#') {  good = false;  }
2626                 else {
2627                         //rip off #
2628                         line = line.substr(1);
2629                         
2630                         vector<string> versionVector;
2631                         splitAtChar(version, versionVector, '.');
2632                         
2633                         //check file version
2634                         vector<string> linesVector;
2635                         splitAtChar(line, linesVector, '.');
2636                         
2637                         if (versionVector.size() != linesVector.size()) { good = false; }
2638                         else {
2639                                 for (int j = 0; j < versionVector.size(); j++) {
2640                                         int num1, num2;
2641                                         convert(versionVector[j], num1);
2642                                         convert(linesVector[j], num2);
2643                                         
2644                                         //if mothurs version is newer than this files version, then we want to remake it
2645                                         if (num1 > num2) {  good = false; break;  }
2646                                 }
2647                         }
2648                         
2649                 }
2650                 
2651                 if (!good) {  file.close();  }
2652                 else { file.seekg(0);  }
2653                 
2654                 return good;
2655         }
2656         catch(exception& e) {
2657                 errorOut(e, "MothurOut", "checkReleaseVersion");                
2658                 exit(1);
2659         }
2660 }
2661 /**************************************************************************************************/
2662 bool MothurOut::isContainingOnlyDigits(string input) {
2663         try{
2664                 
2665                 //are you a digit in ascii code
2666                 for (int i = 0;i < input.length(); i++){
2667                         if( input[i]>47 && input[i]<58){}
2668                         else { return false; }
2669                 }
2670                 
2671                 return true;
2672         }
2673         catch(exception& e) {
2674                 errorOut(e, "MothurOut", "isContainingOnlyDigits");             
2675                 exit(1);
2676         }
2677 }
2678 /**************************************************************************************************/
2679 int MothurOut::removeConfidences(string& tax) {
2680         try {
2681                 
2682                 string taxon;
2683                 string newTax = "";
2684                 
2685                 while (tax.find_first_of(';') != -1) {
2686                         
2687                         if (control_pressed) { return 0; }
2688                         
2689                         //get taxon
2690                         taxon = tax.substr(0,tax.find_first_of(';'));
2691         
2692                         int pos = taxon.find_last_of('(');
2693                         if (pos != -1) {
2694                                 //is it a number?
2695                                 int pos2 = taxon.find_last_of(')');
2696                                 if (pos2 != -1) {
2697                                         string confidenceScore = taxon.substr(pos+1, (pos2-(pos+1)));
2698                                         if (isNumeric1(confidenceScore)) {
2699                                                 taxon = taxon.substr(0, pos); //rip off confidence 
2700                                         }
2701                                 }
2702                         }
2703                         taxon += ";";
2704                         
2705                         tax = tax.substr(tax.find_first_of(';')+1, tax.length());
2706                         newTax += taxon;
2707                 }
2708                 
2709                 tax = newTax;
2710                 
2711                 return 0;
2712         }
2713         catch(exception& e) {
2714                 errorOut(e, "MothurOut", "removeConfidences");
2715                 exit(1);
2716         }
2717 }
2718 /**************************************************************************************************/
2719
2720
2721
2722
2723