]> git.donarmstrong.com Git - mothur.git/blob - mothurout.cpp
changes while testing 1.27
[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("count");
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("count=" + 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         
1056         if (oldName == newName) { return 0; }
1057         
1058                 ifstream inTest;
1059                 int exist = openInputFile(newName, inTest, "");
1060                 inTest.close();
1061                 
1062         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)          
1063                 if (exist == 0) { //you could open it so you want to delete it
1064                         string command = "rm " + newName;
1065                         system(command.c_str());
1066                 }
1067                                 
1068                 string command = "mv " + oldName + " " + newName;
1069                 system(command.c_str());
1070         #else
1071                 mothurRemove(newName);
1072                 int renameOk = rename(oldName.c_str(), newName.c_str());
1073         #endif
1074                 return 0;
1075                 
1076         }
1077         catch(exception& e) {
1078                 errorOut(e, "MothurOut", "renameFile");
1079                 exit(1);
1080         }       
1081 }
1082
1083 /***********************************************************************/
1084
1085 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
1086         try { 
1087         
1088                 string completeFileName = getFullPathName(fileName);
1089 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1090 #ifdef USE_COMPRESSION
1091     // check for gzipped file
1092     if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
1093       string tempName = string(tmpnam(0));
1094       mkfifo(tempName.c_str(), 0666);
1095       cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
1096       int fork_result = fork();
1097       if (fork_result < 0) {
1098         cerr << "Error forking.\n";
1099         exit(1);
1100       } else if (fork_result == 0) {
1101         string command = string(endsWith(completeFileName, ".gz") ?  "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
1102         system(command.c_str());
1103         exit(0);
1104       } else {
1105         completeFileName = tempName;
1106       }
1107     }
1108 #endif
1109 #endif
1110                 fileHandle.open(completeFileName.c_str(), ios::trunc);
1111                 if(!fileHandle) {
1112                         mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
1113                         return 1;
1114                 }
1115                 else {
1116                         return 0;
1117                 }
1118         }
1119         catch(exception& e) {
1120                 errorOut(e, "MothurOut", "openOutputFile");
1121                 exit(1);
1122         }       
1123
1124 }
1125
1126 /**************************************************************************************************/
1127 int MothurOut::appendFiles(string temp, string filename) {
1128         try{
1129                 ofstream output;
1130                 ifstream input;
1131         
1132                 //open output file in append mode
1133                 openOutputFileAppend(filename, output);
1134                 int ableToOpen = openInputFile(temp, input, "no error");
1135                 //int ableToOpen = openInputFile(temp, input);
1136                 
1137                 int numLines = 0;
1138                 if (ableToOpen == 0) { //you opened it
1139             
1140             char buffer[4096];        
1141             while (!input.eof()) {
1142                 input.read(buffer, 4096);
1143                 output.write(buffer, input.gcount());
1144                 //count number of lines
1145                 for (int i = 0; i < input.gcount(); i++) {  if (buffer[i] == '\n') {numLines++;} }
1146             }
1147                         input.close();
1148                 }
1149                 
1150                 output.close();
1151                 
1152                 return numLines;
1153         }
1154         catch(exception& e) {
1155                 errorOut(e, "MothurOut", "appendFiles");
1156                 exit(1);
1157         }       
1158 }
1159
1160 /**************************************************************************************************/
1161 string MothurOut::sortFile(string distFile, string outputDir){
1162         try {   
1163         
1164                 //if (outputDir == "") {  outputDir += hasPath(distFile);  }
1165                 string outfile = getRootName(distFile) + "sorted.dist";
1166
1167                 
1168                 //if you can, use the unix sort since its been optimized for years
1169                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1170                         string command = "sort -n -k +3 " + distFile + " -o " + outfile;
1171                         system(command.c_str());
1172                 #else //you are stuck with my best attempt...
1173                         //windows sort does not have a way to specify a column, only a character in the line
1174                         //since we cannot assume that the distance will always be at the the same character location on each line
1175                         //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
1176                 
1177                         //read in file line by file and put distance first
1178                         string tempDistFile = distFile + ".temp";
1179                         ifstream input;
1180                         ofstream output;
1181                         openInputFile(distFile, input);
1182                         openOutputFile(tempDistFile, output);
1183
1184                         string firstName, secondName;
1185                         float dist;
1186                         while (input) {
1187                                 input >> firstName >> secondName >> dist;
1188                                 output << dist << '\t' << firstName << '\t' << secondName << endl;
1189                                 gobble(input);
1190                         }
1191                         input.close();
1192                         output.close();
1193                 
1194         
1195                         //sort using windows sort
1196                         string tempOutfile = outfile + ".temp";
1197                         string command = "sort " + tempDistFile + " /O " + tempOutfile;
1198                         system(command.c_str());
1199                 
1200                         //read in sorted file and put distance at end again
1201                         ifstream input2;
1202                         openInputFile(tempOutfile, input2);
1203                         openOutputFile(outfile, output);
1204                 
1205                         while (input2) {
1206                                 input2 >> dist >> firstName >> secondName;
1207                                 output << firstName << '\t' << secondName << '\t' << dist << endl;
1208                                 gobble(input2);
1209                         }
1210                         input2.close();
1211                         output.close();
1212                 
1213                         //remove temp files
1214                         mothurRemove(tempDistFile);
1215                         mothurRemove(tempOutfile);
1216                 #endif
1217                 
1218                 return outfile;
1219         }
1220         catch(exception& e) {
1221                 errorOut(e, "MothurOut", "sortFile");
1222                 exit(1);
1223         }       
1224 }
1225 /**************************************************************************************************/
1226 vector<unsigned long long> MothurOut::setFilePosFasta(string filename, int& num) {
1227         try {
1228                         vector<unsigned long long> positions;
1229                         ifstream inFASTA;
1230                         //openInputFile(filename, inFASTA);
1231                         inFASTA.open(filename.c_str(), ios::binary);
1232                                                 
1233                         string input;
1234                         unsigned long long count = 0;
1235                         while(!inFASTA.eof()){
1236                                 //input = getline(inFASTA); 
1237                                 //cout << input << '\t' << inFASTA.tellg() << endl;
1238                                 //if (input.length() != 0) {
1239                                 //      if(input[0] == '>'){    unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1);  cout << (pos - input.length() - 1) << endl; }
1240                                 //}
1241                                 //gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
1242                                 char c = inFASTA.get(); count++;
1243                                 if (c == '>') {
1244                                         positions.push_back(count-1);
1245                                         //cout << count << endl;
1246                                 }
1247                         }
1248                         inFASTA.close();
1249                 
1250                         num = positions.size();
1251                 
1252                         /*FILE * pFile;
1253                         long size;
1254                 
1255                         //get num bytes in file
1256                         pFile = fopen (filename.c_str(),"rb");
1257                         if (pFile==NULL) perror ("Error opening file");
1258                         else{
1259                                 fseek (pFile, 0, SEEK_END);
1260                                 size=ftell (pFile);
1261                                 fclose (pFile);
1262                         }*/
1263                         
1264                         unsigned long long size = positions[(positions.size()-1)];
1265                         ifstream in;
1266                         openInputFile(filename, in);
1267                         
1268                         in.seekg(size);
1269                 
1270                         while(in.get()){
1271                                 if(in.eof())            {       break;  }
1272                                 else                            {       size++; }
1273                         }
1274                         in.close();
1275                 
1276                         positions.push_back(size);
1277                         positions[0] = 0;
1278                 
1279                         return positions;
1280         }
1281         catch(exception& e) {
1282                 errorOut(e, "MothurOut", "setFilePosFasta");
1283                 exit(1);
1284         }
1285 }
1286 /**************************************************************************************************/
1287 vector<unsigned long long> MothurOut::setFilePosEachLine(string filename, int& num) {
1288         try {
1289                         filename = getFullPathName(filename);
1290                         
1291                         vector<unsigned long long> positions;
1292                         ifstream in;
1293                         //openInputFile(filename, in);
1294                         in.open(filename.c_str(), ios::binary);
1295                 
1296                         string input;
1297                         unsigned long long count = 0;
1298                         positions.push_back(0);
1299                 
1300                         while(!in.eof()){
1301                                 //getline counting reads
1302                                 char d = in.get(); count++;
1303                                 while ((d != '\n') && (d != '\r') && (d != '\f') && (d != in.eof()))    {
1304                                         //get next character
1305                                         d = in.get(); 
1306                                         count++;
1307                                 }
1308                                 
1309                                 if (!in.eof()) {
1310                                         d=in.get(); count++;
1311                                         while(isspace(d) && (d != in.eof()))            { d=in.get(); count++;}
1312                                 }
1313                                 positions.push_back(count-1);
1314                                 //cout << count-1 << endl;
1315                         }
1316                         in.close();
1317                 
1318                         num = positions.size()-1;
1319                 
1320                         FILE * pFile;
1321                         unsigned long long size;
1322                         
1323                         //get num bytes in file
1324                         pFile = fopen (filename.c_str(),"rb");
1325                         if (pFile==NULL) perror ("Error opening file");
1326                         else{
1327                                 fseek (pFile, 0, SEEK_END);
1328                                 size=ftell (pFile);
1329                                 fclose (pFile);
1330                         }
1331                 
1332                         positions[(positions.size()-1)] = size;
1333                 
1334                         return positions;
1335         }
1336         catch(exception& e) {
1337                 errorOut(e, "MothurOut", "setFilePosEachLine");
1338                 exit(1);
1339         }
1340 }
1341 /**************************************************************************************************/
1342
1343 vector<unsigned long long> MothurOut::divideFile(string filename, int& proc) {
1344         try{
1345                 vector<unsigned long long> filePos;
1346                 filePos.push_back(0);
1347                 
1348                 FILE * pFile;
1349                 unsigned long long size;
1350                 
1351                 filename = getFullPathName(filename);
1352         
1353                 //get num bytes in file
1354                 pFile = fopen (filename.c_str(),"rb");
1355                 if (pFile==NULL) perror ("Error opening file");
1356                 else{
1357                         fseek (pFile, 0, SEEK_END);
1358                         size=ftell (pFile);
1359                         fclose (pFile);
1360                 }
1361                 
1362         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
1363                                 
1364                 //estimate file breaks
1365                 unsigned long long chunkSize = 0;
1366                 chunkSize = size / proc;
1367
1368                 //file to small to divide by processors
1369                 if (chunkSize == 0)  {  proc = 1;       filePos.push_back(size); return filePos;        }
1370         
1371                 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
1372                 for (int i = 0; i < proc; i++) {
1373                         unsigned long long spot = (i+1) * chunkSize;
1374                         
1375                         ifstream in;
1376                         openInputFile(filename, in);
1377                         in.seekg(spot);
1378                         
1379                         //look for next '>'
1380                         unsigned long long newSpot = spot;
1381                         while (!in.eof()) {
1382                            char c = in.get();
1383                                 
1384                            if (c == '>') {   in.putback(c); newSpot = in.tellg(); break;  }
1385                            else if (int(c) == -1) { break; }
1386                                 
1387                         }
1388                 
1389                         //there was not another sequence before the end of the file
1390                         unsigned long long sanityPos = in.tellg();
1391
1392                         if (sanityPos == -1) {  break;  }
1393                         else {  filePos.push_back(newSpot);  }
1394                         
1395                         in.close();
1396                 }
1397                 
1398                 //save end pos
1399                 filePos.push_back(size);
1400                 
1401                 //sanity check filePos
1402                 for (int i = 0; i < (filePos.size()-1); i++) {
1403                         if (filePos[(i+1)] <= filePos[i]) {  filePos.erase(filePos.begin()+(i+1)); i--; }
1404                 }
1405
1406                 proc = (filePos.size() - 1);
1407 #else
1408                 mothurOut("[ERROR]: Windows version should not be calling the divideFile function."); mothurOutEndLine();
1409                 proc=1;
1410                 filePos.push_back(size);
1411 #endif
1412                 return filePos;
1413         }
1414         catch(exception& e) {
1415                 errorOut(e, "MothurOut", "divideFile");
1416                 exit(1);
1417         }
1418 }
1419 /**************************************************************************************************/
1420 int MothurOut::divideFile(string filename, int& proc, vector<string>& files) {
1421         try{
1422                 
1423                 vector<unsigned long long> filePos = divideFile(filename, proc);
1424                 
1425                 for (int i = 0; i < (filePos.size()-1); i++) {
1426                         
1427                         //read file chunk
1428                         ifstream in;
1429                         openInputFile(filename, in);
1430                         in.seekg(filePos[i]);
1431                         unsigned long long size = filePos[(i+1)] - filePos[i];
1432                         char* chunk = new char[size];
1433                         in.read(chunk, size);
1434                         in.close();
1435                         
1436                         //open new file
1437                         string fileChunkName = filename + "." + toString(i) + ".tmp";
1438                         ofstream out; 
1439                         openOutputFile(fileChunkName, out);
1440                         
1441                         out << chunk << endl;
1442                         out.close();
1443                         delete[] chunk;
1444                         
1445                         //save name
1446                         files.push_back(fileChunkName);
1447                 }
1448                                 
1449                 return 0;
1450         }
1451         catch(exception& e) {
1452                 errorOut(e, "MothurOut", "divideFile");
1453                 exit(1);
1454         }
1455 }
1456 /***********************************************************************/
1457
1458 bool MothurOut::isTrue(string f){
1459         try {
1460                 
1461                 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
1462                 
1463                 if ((f == "TRUE") || (f == "T")) {      return true;    }
1464                 else {  return false;  }
1465         }
1466         catch(exception& e) {
1467                 errorOut(e, "MothurOut", "isTrue");
1468                 exit(1);
1469         }
1470 }
1471
1472 /***********************************************************************/
1473
1474 float MothurOut::roundDist(float dist, int precision){
1475         try {
1476                 return int(dist * precision + 0.5)/float(precision);
1477         }
1478         catch(exception& e) {
1479                 errorOut(e, "MothurOut", "roundDist");
1480                 exit(1);
1481         }
1482 }
1483 /***********************************************************************/
1484
1485 float MothurOut::ceilDist(float dist, int precision){
1486         try {
1487                 return int(ceil(dist * precision))/float(precision);
1488         }
1489         catch(exception& e) {
1490                 errorOut(e, "MothurOut", "ceilDist");
1491                 exit(1);
1492         }
1493 }
1494 /***********************************************************************/
1495
1496 vector<string> MothurOut::splitWhiteSpace(string& rest, char buffer[], int size){
1497         try {
1498         vector<string> pieces;
1499         
1500         for (int i = 0; i < size; i++) {
1501             if (!isspace(buffer[i]))  { rest += buffer[i];  }
1502             else {
1503                 if (rest != "") { pieces.push_back(rest);  rest = ""; }
1504                 while (i < size) {  //gobble white space
1505                     if (isspace(buffer[i])) { i++; }
1506                     else { rest = buffer[i];  break; } //cout << "next piece buffer = " << nextPiece << endl;
1507                 } 
1508             }
1509         }
1510         
1511         return pieces;
1512         }
1513         catch(exception& e) {
1514                 errorOut(e, "MothurOut", "splitWhiteSpace");
1515                 exit(1);
1516         }
1517 }
1518 /***********************************************************************/
1519 vector<string> MothurOut::splitWhiteSpace(string input){
1520         try {
1521         vector<string> pieces;
1522         string rest = "";
1523         
1524         for (int i = 0; i < input.length(); i++) {
1525             if (!isspace(input[i]))  { rest += input[i];  }
1526             else {
1527                 if (rest != "") { pieces.push_back(rest);  rest = ""; }
1528                 while (i < input.length()) {  //gobble white space
1529                     if (isspace(input[i])) { i++; }
1530                     else { rest = input[i];  break; } //cout << "next piece buffer = " << nextPiece << endl;
1531                 } 
1532             }
1533         }
1534         
1535         if (rest != "") { pieces.push_back(rest); }
1536         
1537         return pieces;
1538         }
1539         catch(exception& e) {
1540                 errorOut(e, "MothurOut", "splitWhiteSpace");
1541                 exit(1);
1542         }
1543 }
1544 //**********************************************************************************************************************
1545 int MothurOut::readTax(string namefile, map<string, string>& taxMap) {
1546         try {
1547         //open input file
1548                 ifstream in;
1549                 openInputFile(namefile, in);
1550         
1551         string rest = "";
1552         char buffer[4096];
1553         bool pairDone = false;
1554         bool columnOne = true;
1555         string firstCol, secondCol;
1556         
1557                 while (!in.eof()) {
1558                         if (control_pressed) { break; }
1559                         
1560             in.read(buffer, 4096);
1561             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1562             
1563             for (int i = 0; i < pieces.size(); i++) {
1564                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1565                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1566                 
1567                 if (pairDone) { 
1568                     //are there confidence scores, if so remove them
1569                     if (secondCol.find_first_of('(') != -1) {  removeConfidences(secondCol);    }
1570                     taxMap[firstCol] = secondCol;
1571                     if (debug) {  mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n");  }
1572                     pairDone = false; 
1573                 }
1574             }
1575                 }
1576                 in.close();
1577         
1578         if (rest != "") {
1579             vector<string> pieces = splitWhiteSpace(rest);
1580             
1581             for (int i = 0; i < pieces.size(); i++) {
1582                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1583                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1584                 
1585                 if (pairDone) { 
1586                     //are there confidence scores, if so remove them
1587                     if (secondCol.find_first_of('(') != -1) {  removeConfidences(secondCol);    }
1588                     taxMap[firstCol] = secondCol;
1589                     if (debug) {  mothurOut("[DEBUG]: name = '" + firstCol + "' tax = '" + secondCol + "'\n");  }
1590                     pairDone = false; 
1591                 }
1592             } 
1593         }
1594                 
1595                 return taxMap.size();
1596
1597         }
1598         catch(exception& e) {
1599                 errorOut(e, "MothurOut", "readTax");
1600                 exit(1);
1601         }
1602 }
1603 /**********************************************************************************************************************/
1604 int MothurOut::readNames(string namefile, map<string, string>& nameMap, bool redund) { 
1605         try {
1606                 //open input file
1607                 ifstream in;
1608                 openInputFile(namefile, in);
1609         
1610         string rest = "";
1611         char buffer[4096];
1612         bool pairDone = false;
1613         bool columnOne = true;
1614         string firstCol, secondCol;
1615         
1616                 while (!in.eof()) {
1617                         if (control_pressed) { break; }
1618                         
1619             in.read(buffer, 4096);
1620             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1621             
1622             for (int i = 0; i < pieces.size(); i++) {
1623                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1624                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1625                 
1626                 if (pairDone) { 
1627                     //parse names into vector
1628                     vector<string> theseNames;
1629                     splitAtComma(secondCol, theseNames);
1630                     for (int i = 0; i < theseNames.size(); i++) {  nameMap[theseNames[i]] = firstCol;  }
1631                     pairDone = false; 
1632                 }
1633             }
1634                 }
1635                 in.close();
1636         
1637         if (rest != "") {
1638             vector<string> pieces = splitWhiteSpace(rest);
1639             
1640             for (int i = 0; i < pieces.size(); i++) {
1641                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1642                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1643                 
1644                 if (pairDone) { 
1645                     //parse names into vector
1646                     vector<string> theseNames;
1647                     splitAtComma(secondCol, theseNames);
1648                     for (int i = 0; i < theseNames.size(); i++) {  nameMap[theseNames[i]] = firstCol;  }
1649                     pairDone = false; 
1650                 }
1651             }  
1652         }
1653                 
1654                 return nameMap.size();
1655                 
1656         }
1657         catch(exception& e) {
1658                 errorOut(e, "MothurOut", "readNames");
1659                 exit(1);
1660         }
1661 }
1662 /**********************************************************************************************************************/
1663 int MothurOut::readNames(string namefile, map<string, string>& nameMap, int flip) { 
1664         try {
1665                 //open input file
1666                 ifstream in;
1667                 openInputFile(namefile, in);
1668         
1669         string rest = "";
1670         char buffer[4096];
1671         bool pairDone = false;
1672         bool columnOne = true;
1673         string firstCol, secondCol;
1674         
1675                 while (!in.eof()) {
1676                         if (control_pressed) { break; }
1677                         
1678             in.read(buffer, 4096);
1679             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1680             
1681             for (int i = 0; i < pieces.size(); i++) {
1682                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1683                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1684                 
1685                 if (pairDone) { 
1686                     nameMap[secondCol] = firstCol;
1687                     pairDone = false; 
1688                 }
1689             }
1690                 }
1691                 in.close();
1692         
1693         if (rest != "") {
1694             vector<string> pieces = splitWhiteSpace(rest);
1695             
1696             for (int i = 0; i < pieces.size(); i++) {
1697                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1698                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1699                 
1700                 if (pairDone) { 
1701                     nameMap[secondCol] = firstCol;
1702                     pairDone = false; 
1703                 }
1704             } 
1705         }
1706                 
1707                 return nameMap.size();
1708                 
1709         }
1710         catch(exception& e) {
1711                 errorOut(e, "MothurOut", "readNames");
1712                 exit(1);
1713         }
1714 }
1715 /**********************************************************************************************************************/
1716 int MothurOut::readNames(string namefile, map<string, string>& nameMap, map<string, int>& nameCount) { 
1717         try {
1718                 nameMap.clear(); nameCount.clear();
1719                 //open input file
1720                 ifstream in;
1721                 openInputFile(namefile, in);
1722         
1723         string rest = "";
1724         char buffer[4096];
1725         bool pairDone = false;
1726         bool columnOne = true;
1727         string firstCol, secondCol;
1728         
1729                 while (!in.eof()) {
1730                         if (control_pressed) { break; }
1731                         
1732             in.read(buffer, 4096);
1733             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1734             
1735             for (int i = 0; i < pieces.size(); i++) {
1736                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1737                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1738                 
1739                 if (pairDone) { 
1740                     //parse names into vector
1741                     vector<string> theseNames;
1742                     splitAtComma(secondCol, theseNames);
1743                     for (int i = 0; i < theseNames.size(); i++) {  nameMap[theseNames[i]] = firstCol;  }
1744                     nameCount[firstCol] = theseNames.size();
1745                     pairDone = false; 
1746                 }
1747             }
1748                 }
1749                 in.close();
1750                 
1751         if (rest != "") {
1752             vector<string> pieces = splitWhiteSpace(rest);
1753             
1754             for (int i = 0; i < pieces.size(); i++) {
1755                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1756                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1757                 
1758                 if (pairDone) { 
1759                     //parse names into vector
1760                     vector<string> theseNames;
1761                     splitAtComma(secondCol, theseNames);
1762                     for (int i = 0; i < theseNames.size(); i++) {  nameMap[theseNames[i]] = firstCol;  }
1763                     nameCount[firstCol] = theseNames.size();
1764                     pairDone = false; 
1765                 }
1766             }
1767
1768         }
1769                 return nameMap.size();
1770                 
1771         }
1772         catch(exception& e) {
1773                 errorOut(e, "MothurOut", "readNames");
1774                 exit(1);
1775         }
1776 }
1777 /**********************************************************************************************************************/
1778 int MothurOut::readNames(string namefile, map<string, string>& nameMap) { 
1779         try {
1780                 //open input file
1781                 ifstream in;
1782                 openInputFile(namefile, in);
1783
1784         string rest = "";
1785         char buffer[4096];
1786         bool pairDone = false;
1787         bool columnOne = true;
1788         string firstCol, secondCol;
1789         
1790                 while (!in.eof()) {
1791                         if (control_pressed) { break; }
1792                         
1793             in.read(buffer, 4096);
1794             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1795              
1796             for (int i = 0; i < pieces.size(); i++) {
1797                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1798                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1799                 
1800                 if (pairDone) { nameMap[firstCol] = secondCol; pairDone = false; }
1801             }
1802                 }
1803                 in.close();
1804         
1805         if (rest != "") {
1806             vector<string> pieces = splitWhiteSpace(rest);
1807             
1808             for (int i = 0; i < pieces.size(); i++) {
1809                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1810                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1811                 
1812                 if (pairDone) { nameMap[firstCol] = secondCol; pairDone = false; }
1813             }
1814         }
1815                 
1816                 return nameMap.size();
1817                 
1818         }
1819         catch(exception& e) {
1820                 errorOut(e, "MothurOut", "readNames");
1821                 exit(1);
1822         }
1823 }
1824 /**********************************************************************************************************************/
1825 int MothurOut::readNames(string namefile, map<string, vector<string> >& nameMap) { 
1826         try {        
1827                 //open input file
1828                 ifstream in;
1829                 openInputFile(namefile, in);
1830                 
1831         string rest = "";
1832         char buffer[4096];
1833         bool pairDone = false;
1834         bool columnOne = true;
1835         string firstCol, secondCol;
1836         
1837                 while (!in.eof()) {
1838                         if (control_pressed) { break; }
1839                         
1840             in.read(buffer, 4096);
1841             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1842             
1843             for (int i = 0; i < pieces.size(); i++) {
1844                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1845                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1846                 
1847                 if (pairDone) { 
1848                     vector<string> temp;
1849                     splitAtComma(secondCol, temp);
1850                     nameMap[firstCol] = temp;
1851                     pairDone = false;  
1852                 } 
1853             }
1854                 }
1855                 in.close();
1856         
1857         if (rest != "") {
1858             vector<string> pieces = splitWhiteSpace(rest);
1859             
1860             for (int i = 0; i < pieces.size(); i++) {
1861                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1862                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1863                 
1864                 if (pairDone) { 
1865                     vector<string> temp;
1866                     splitAtComma(secondCol, temp);
1867                     nameMap[firstCol] = temp;
1868                     pairDone = false;  
1869                 } 
1870             }
1871         }
1872         
1873                 return nameMap.size();
1874         }
1875         catch(exception& e) {
1876                 errorOut(e, "MothurOut", "readNames");
1877                 exit(1);
1878         }
1879 }
1880 /**********************************************************************************************************************/
1881 map<string, int> MothurOut::readNames(string namefile) { 
1882         try {
1883                 map<string, int> nameMap;
1884                 
1885                 //open input file
1886                 ifstream in;
1887                 openInputFile(namefile, in);
1888                 
1889         string rest = "";
1890         char buffer[4096];
1891         bool pairDone = false;
1892         bool columnOne = true;
1893         string firstCol, secondCol;
1894         
1895                 while (!in.eof()) {
1896                         if (control_pressed) { break; }
1897                         
1898             in.read(buffer, 4096);
1899             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1900             
1901             for (int i = 0; i < pieces.size(); i++) {
1902                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1903                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1904                 
1905                 if (pairDone) { 
1906                     int num = getNumNames(secondCol);
1907                     nameMap[firstCol] = num;
1908                     pairDone = false;  
1909                 } 
1910             }
1911                 }
1912         in.close();
1913         
1914         if (rest != "") {
1915             vector<string> pieces = splitWhiteSpace(rest);
1916             for (int i = 0; i < pieces.size(); i++) {
1917                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1918                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1919                 
1920                 if (pairDone) { 
1921                     int num = getNumNames(secondCol);
1922                     nameMap[firstCol] = num;
1923                     pairDone = false;  
1924                 } 
1925             }
1926         }
1927                 
1928                 return nameMap;
1929                 
1930         }
1931         catch(exception& e) {
1932                 errorOut(e, "MothurOut", "readNames");
1933                 exit(1);
1934         }
1935 }
1936 /**********************************************************************************************************************/
1937 int MothurOut::readNames(string namefile, vector<seqPriorityNode>& nameVector, map<string, string>& fastamap) { 
1938         try {
1939                 int error = 0;
1940                 
1941                 //open input file
1942                 ifstream in;
1943                 openInputFile(namefile, in);
1944                 
1945         string rest = "";
1946         char buffer[4096];
1947         bool pairDone = false;
1948         bool columnOne = true;
1949         string firstCol, secondCol;
1950         
1951                 while (!in.eof()) {
1952                         if (control_pressed) { break; }
1953                         
1954             in.read(buffer, 4096);
1955             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
1956             
1957             for (int i = 0; i < pieces.size(); i++) {
1958                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1959                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1960                 
1961                 if (pairDone) { 
1962                     int num = getNumNames(secondCol);
1963                     
1964                     map<string, string>::iterator it = fastamap.find(firstCol);
1965                     if (it == fastamap.end()) {
1966                         error = 1;
1967                         mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
1968                     }else {
1969                         seqPriorityNode temp(num, it->second, firstCol);
1970                         nameVector.push_back(temp);
1971                     }
1972                     
1973                     pairDone = false;  
1974                 } 
1975             }
1976                 }
1977         in.close();
1978         
1979         if (rest != "") {
1980             vector<string> pieces = splitWhiteSpace(rest);
1981             
1982             for (int i = 0; i < pieces.size(); i++) {
1983                 if (columnOne) {  firstCol = pieces[i]; columnOne=false; }
1984                 else  { secondCol = pieces[i]; pairDone = true; columnOne=true; }
1985                 
1986                 if (pairDone) { 
1987                     int num = getNumNames(secondCol);
1988                     
1989                     map<string, string>::iterator it = fastamap.find(firstCol);
1990                     if (it == fastamap.end()) {
1991                         error = 1;
1992                         mothurOut("[ERROR]: " + firstCol + " is not in your fastafile, but is in your namesfile, please correct."); mothurOutEndLine();
1993                     }else {
1994                         seqPriorityNode temp(num, it->second, firstCol);
1995                         nameVector.push_back(temp);
1996                     }
1997                     
1998                     pairDone = false;  
1999                 } 
2000             }
2001         }
2002                 return error;
2003         }
2004         catch(exception& e) {
2005                 errorOut(e, "MothurOut", "readNames");
2006                 exit(1);
2007         }
2008 }
2009 //**********************************************************************************************************************
2010 set<string> MothurOut::readAccnos(string accnosfile){
2011         try {
2012                 set<string> names;
2013                 ifstream in;
2014                 openInputFile(accnosfile, in);
2015                 string name;
2016                 
2017         string rest = "";
2018         char buffer[4096];
2019         
2020                 while (!in.eof()) {
2021                         if (control_pressed) { break; }
2022                         
2023             in.read(buffer, 4096);
2024             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2025             
2026             for (int i = 0; i < pieces.size(); i++) {  names.insert(pieces[i]);  }
2027         }
2028                 in.close();     
2029                 
2030         if (rest != "") {
2031             vector<string> pieces = splitWhiteSpace(rest);
2032             for (int i = 0; i < pieces.size(); i++) {  names.insert(pieces[i]);  } 
2033         }
2034                 return names;
2035         }
2036         catch(exception& e) {
2037                 errorOut(e, "MothurOut", "readAccnos");
2038                 exit(1);
2039         }
2040 }
2041 //**********************************************************************************************************************
2042 int MothurOut::readAccnos(string accnosfile, vector<string>& names){
2043         try {
2044         names.clear();
2045                 ifstream in;
2046                 openInputFile(accnosfile, in);
2047                 string name;
2048                 
2049         string rest = "";
2050         char buffer[4096];
2051         
2052                 while (!in.eof()) {
2053                         if (control_pressed) { break; }
2054                         
2055             in.read(buffer, 4096);
2056             vector<string> pieces = splitWhiteSpace(rest, buffer, in.gcount());
2057             
2058             for (int i = 0; i < pieces.size(); i++) {  names.push_back(pieces[i]);  }
2059         }
2060                 in.close();     
2061         
2062         if (rest != "") {
2063             vector<string> pieces = splitWhiteSpace(rest);
2064             for (int i = 0; i < pieces.size(); i++) {  names.push_back(pieces[i]);  }
2065         }
2066                 
2067                 return 0;
2068         }
2069         catch(exception& e) {
2070                 errorOut(e, "MothurOut", "readAccnos");
2071                 exit(1);
2072         }
2073 }
2074 /***********************************************************************/
2075
2076 int MothurOut::getNumNames(string names){
2077         try {
2078                 int count = 0;
2079                 
2080                 if(names != ""){
2081                         count = 1;
2082                         for(int i=0;i<names.size();i++){
2083                                 if(names[i] == ','){
2084                                         count++;
2085                                 }
2086                         }
2087                 }
2088                 
2089                 return count;
2090         }
2091         catch(exception& e) {
2092                 errorOut(e, "MothurOut", "getNumNames");
2093                 exit(1);
2094         }
2095 }
2096 /***********************************************************************/
2097
2098 int MothurOut::getNumChar(string line, char c){
2099         try {
2100                 int count = 0;
2101                 
2102                 if(line != ""){
2103                         for(int i=0;i<line.size();i++){
2104                                 if(line[i] == c){
2105                                         count++;
2106                                 }
2107                         }
2108                 }
2109                 
2110                 return count;
2111         }
2112         catch(exception& e) {
2113                 errorOut(e, "MothurOut", "getNumChar");
2114                 exit(1);
2115         }
2116 }
2117 /***********************************************************************/
2118 int MothurOut::mothurRemove(string filename){
2119         try {
2120                 filename = getFullPathName(filename);
2121                 int error = remove(filename.c_str());
2122                 //if (error != 0) { 
2123                 //      if (errno != ENOENT) { //ENOENT == file does not exist
2124                 //              string message = "Error deleting file " + filename;
2125                 //              perror(message.c_str()); 
2126                 //      }
2127                 //}
2128                 return error;
2129         }
2130         catch(exception& e) {
2131                 errorOut(e, "MothurOut", "mothurRemove");
2132                 exit(1);
2133         }
2134 }
2135 /***********************************************************************/
2136 bool MothurOut::mothurConvert(string item, int& num){
2137         try {
2138                 bool error = false;
2139                 
2140                 if (isNumeric1(item)) {
2141                         convert(item, num);
2142                 }else {
2143                         num = 0;
2144                         error = true;
2145                         mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2146                         commandInputsConvertError = true;
2147                 }
2148                 
2149                 return error;
2150         }
2151         catch(exception& e) {
2152                 errorOut(e, "MothurOut", "mothurConvert");
2153                 exit(1);
2154         }
2155 }
2156 /***********************************************************************/
2157 bool MothurOut::mothurConvert(string item, intDist& num){
2158         try {
2159                 bool error = false;
2160                 
2161                 if (isNumeric1(item)) {
2162                         convert(item, num);
2163                 }else {
2164                         num = 0;
2165                         error = true;
2166                         mothurOut("[ERROR]: cannot convert " + item + " to an integer."); mothurOutEndLine();
2167                         commandInputsConvertError = true;
2168                 }
2169                 
2170                 return error;
2171         }
2172         catch(exception& e) {
2173                 errorOut(e, "MothurOut", "mothurConvert");
2174                 exit(1);
2175         }
2176 }
2177
2178 /***********************************************************************/
2179 bool MothurOut::isNumeric1(string stringToCheck){
2180         try {
2181                 bool numeric = false;
2182                 
2183                 if(stringToCheck.find_first_not_of("0123456789.-") == string::npos) { numeric = true; }
2184                         
2185                 return numeric;
2186         }
2187         catch(exception& e) {
2188                 errorOut(e, "MothurOut", "isNumeric1");
2189                 exit(1);
2190         }
2191         
2192 }
2193 /***********************************************************************/
2194 bool MothurOut::mothurConvert(string item, float& num){
2195         try {
2196                 bool error = false;
2197                 
2198                 if (isNumeric1(item)) {
2199                         convert(item, num);
2200                 }else {
2201                         num = 0;
2202                         error = true;
2203                         mothurOut("[ERROR]: cannot convert " + item + " to a float."); mothurOutEndLine();
2204                         commandInputsConvertError = true;
2205                 }
2206                 
2207                 return error;
2208         }
2209         catch(exception& e) {
2210                 errorOut(e, "MothurOut", "mothurConvert");
2211                 exit(1);
2212         }
2213 }
2214 /***********************************************************************/
2215 bool MothurOut::mothurConvert(string item, double& num){
2216         try {
2217                 bool error = false;
2218                 
2219                 if (isNumeric1(item)) {
2220                         convert(item, num);
2221                 }else {
2222                         num = 0;
2223                         error = true;
2224                         mothurOut("[ERROR]: cannot convert " + item + " to a double."); mothurOutEndLine();
2225                         commandInputsConvertError = true;
2226                 }
2227                 
2228                 return error;
2229         }
2230         catch(exception& e) {
2231                 errorOut(e, "MothurOut", "mothurConvert");
2232                 exit(1);
2233         }
2234 }
2235 /**************************************************************************************************/
2236
2237 vector<vector<double> > MothurOut::binomial(int maxOrder){
2238         try {
2239         vector<vector<double> > binomial(maxOrder+1);
2240         
2241     for(int i=0;i<=maxOrder;i++){
2242                 binomial[i].resize(maxOrder+1);
2243                 binomial[i][0]=1;
2244                 binomial[0][i]=0;
2245     }
2246     binomial[0][0]=1;
2247         
2248     binomial[1][0]=1;
2249     binomial[1][1]=1;
2250         
2251     for(int i=2;i<=maxOrder;i++){
2252                 binomial[1][i]=0;
2253     }
2254         
2255     for(int i=2;i<=maxOrder;i++){
2256                 for(int j=1;j<=maxOrder;j++){
2257                         if(i==j){       binomial[i][j]=1;                                                                       }
2258                         if(j>i) {       binomial[i][j]=0;                                                                       }
2259                         else    {       binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j];     }
2260                 }
2261     }
2262         
2263         return binomial;
2264         
2265         }
2266         catch(exception& e) {
2267                 errorOut(e, "MothurOut", "binomial");
2268                 exit(1);
2269         }
2270 }
2271 /**************************************************************************************************/
2272 unsigned int MothurOut::fromBase36(string base36){
2273         try {
2274                 unsigned int num = 0;
2275                 
2276                 map<char, int> converts;
2277                 converts['A'] = 0;
2278                 converts['a'] = 0;
2279                 converts['B'] = 1;
2280                 converts['b'] = 1;
2281                 converts['C'] = 2;
2282                 converts['c'] = 2;
2283                 converts['D'] = 3;
2284                 converts['d'] = 3;
2285                 converts['E'] = 4;
2286                 converts['e'] = 4;
2287                 converts['F'] = 5;
2288                 converts['f'] = 5;
2289                 converts['G'] = 6;
2290                 converts['g'] = 6;
2291                 converts['H'] = 7;
2292                 converts['h'] = 7;
2293                 converts['I'] = 8;
2294                 converts['i'] = 8;
2295                 converts['J'] = 9;
2296                 converts['j'] = 9;
2297                 converts['K'] = 10;
2298                 converts['k'] = 10;
2299                 converts['L'] = 11;
2300                 converts['l'] = 11;
2301                 converts['M'] = 12;
2302                 converts['m'] = 12;
2303                 converts['N'] = 13;
2304                 converts['n'] = 13;
2305                 converts['O'] = 14;
2306                 converts['o'] = 14;
2307                 converts['P'] = 15;
2308                 converts['p'] = 15;
2309                 converts['Q'] = 16;
2310                 converts['q'] = 16;
2311                 converts['R'] = 17;
2312                 converts['r'] = 17;
2313                 converts['S'] = 18;
2314                 converts['s'] = 18;
2315                 converts['T'] = 19;
2316                 converts['t'] = 19;
2317                 converts['U'] = 20;
2318                 converts['u'] = 20;
2319                 converts['V'] = 21;
2320                 converts['v'] = 21;
2321                 converts['W'] = 22;
2322                 converts['w'] = 22;
2323                 converts['X'] = 23;
2324                 converts['x'] = 23;
2325                 converts['Y'] = 24;
2326                 converts['y'] = 24;
2327                 converts['Z'] = 25;
2328                 converts['z'] = 25;
2329                 converts['0'] = 26;
2330                 converts['1'] = 27;
2331                 converts['2'] = 28;
2332                 converts['3'] = 29;
2333                 converts['4'] = 30;
2334                 converts['5'] = 31;
2335                 converts['6'] = 32;
2336                 converts['7'] = 33;
2337                 converts['8'] = 34;
2338                 converts['9'] = 35;             
2339                 
2340                 int i = 0;
2341                 while (i < base36.length()) {
2342                         char c = base36[i];
2343                         num = 36 * num + converts[c];
2344                         i++;
2345                 }
2346                 
2347                 return num;
2348                 
2349         }
2350         catch(exception& e) {
2351                 errorOut(e, "MothurOut", "fromBase36");
2352                 exit(1);
2353         }
2354 }
2355 /***********************************************************************/
2356
2357 int MothurOut::factorial(int num){
2358         try {
2359                 int total = 1;
2360                 
2361                 for (int i = 1; i <= num; i++) {
2362                         total *= i;
2363                 }
2364                 
2365                 return total;
2366         }
2367         catch(exception& e) {
2368                 errorOut(e, "MothurOut", "factorial");
2369                 exit(1);
2370         }
2371 }
2372 /***********************************************************************/
2373
2374 int MothurOut::getNumSeqs(ifstream& file){
2375         try {
2376                 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
2377                 file.seekg(0);
2378                 return numSeqs;
2379         }
2380         catch(exception& e) {
2381                 errorOut(e, "MothurOut", "getNumSeqs");
2382                 exit(1);
2383         }       
2384 }
2385 /***********************************************************************/
2386 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
2387         try {
2388                 string input;
2389                 numSeqs = 0;
2390                 while(!file.eof()){
2391                         input = getline(file);
2392                         if (input.length() != 0) {
2393                                 if(input[0] == '>'){ numSeqs++; }
2394                         }
2395                 }
2396         }
2397         catch(exception& e) {
2398                 errorOut(e, "MothurOut", "getNumSeqs");
2399                 exit(1);
2400         }       
2401 }
2402 /***********************************************************************/
2403
2404 //This function parses the estimator options and puts them in a vector
2405 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
2406         try {
2407                 string individual = "";
2408                 int estimLength = estim.size();
2409                 for(int i=0;i<estimLength;i++){
2410                         if(estim[i] == symbol){
2411                                 container.push_back(individual);
2412                                 individual = "";                                
2413                         }
2414                         else{
2415                                 individual += estim[i];
2416                         }
2417                 }
2418                 container.push_back(individual);
2419
2420         }
2421         catch(exception& e) {
2422                 errorOut(e, "MothurOut", "splitAtChar");
2423                 exit(1);
2424         }       
2425 }
2426
2427 /***********************************************************************/
2428
2429 //This function parses the estimator options and puts them in a vector
2430 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
2431         try {
2432                 string individual = "";
2433                 int estimLength = estim.size();
2434                 for(int i=0;i<estimLength;i++){
2435                         if(estim[i] == '-'){
2436                                 container.push_back(individual);
2437                                 individual = "";                                
2438                         }
2439                         else{
2440                                 individual += estim[i];
2441                         }
2442                 }
2443                 container.push_back(individual);
2444
2445         
2446         /*      string individual;
2447                 
2448                 while (estim.find_first_of('-') != -1) {
2449                         individual = estim.substr(0,estim.find_first_of('-'));
2450                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
2451                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
2452                                 container.push_back(individual);
2453                         }
2454                 }
2455                 //get last one
2456                 container.push_back(estim); */
2457         }
2458         catch(exception& e) {
2459                 errorOut(e, "MothurOut", "splitAtDash");
2460                 exit(1);
2461         }       
2462 }
2463
2464 /***********************************************************************/
2465 //This function parses the label options and puts them in a set
2466 void MothurOut::splitAtDash(string& estim, set<string>& container) {
2467         try {
2468                 string individual = "";
2469                 int estimLength = estim.size();
2470                 for(int i=0;i<estimLength;i++){
2471                         if(estim[i] == '-'){
2472                                 container.insert(individual);
2473                                 individual = "";                                
2474                         }
2475                         else{
2476                                 individual += estim[i];
2477                         }
2478                 }
2479                 container.insert(individual);
2480
2481         //      string individual;
2482                 
2483         //      while (estim.find_first_of('-') != -1) {
2484         //              individual = estim.substr(0,estim.find_first_of('-'));
2485         //              if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
2486         //                      estim = estim.substr(estim.find_first_of('-')+1, estim.length());
2487         //                      container.insert(individual);
2488         //              }
2489         //      }
2490                 //get last one
2491         //      container.insert(estim);
2492         
2493         }
2494         catch(exception& e) {
2495                 errorOut(e, "MothurOut", "splitAtDash");
2496                 exit(1);
2497         }       
2498 }
2499 /***********************************************************************/
2500 //This function parses the line options and puts them in a set
2501 void MothurOut::splitAtDash(string& estim, set<int>& container) {
2502         try {
2503                 string individual;
2504                 int lineNum;
2505                 
2506                 while (estim.find_first_of('-') != -1) {
2507                         individual = estim.substr(0,estim.find_first_of('-'));
2508                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
2509                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
2510                                 convert(individual, lineNum); //convert the string to int
2511                                 container.insert(lineNum);
2512                         }
2513                 }
2514                 //get last one
2515                 convert(estim, lineNum); //convert the string to int
2516                 container.insert(lineNum);
2517         }
2518         catch(exception& e) {
2519                 errorOut(e, "MothurOut", "splitAtDash");
2520                 exit(1);
2521         }       
2522 }
2523 /***********************************************************************/
2524 string MothurOut::makeList(vector<string>& names) {
2525         try {
2526                 string list = "";
2527         
2528         if (names.size() == 0) { return list; }
2529                 
2530         for (int i = 0; i < names.size()-1; i++) { list += names[i] + ",";  }
2531         
2532         //get last name
2533         list += names[names.size()-1];
2534         
2535         return list;
2536     }
2537         catch(exception& e) {
2538                 errorOut(e, "MothurOut", "makeList");
2539                 exit(1);
2540         }       
2541 }
2542
2543 /***********************************************************************/
2544 //This function parses the a string and puts peices in a vector
2545 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
2546         try {
2547                 string individual = "";
2548                 int estimLength = estim.size();
2549                 for(int i=0;i<estimLength;i++){
2550                         if(estim[i] == ','){
2551                                 container.push_back(individual);
2552                                 individual = "";                                
2553                         }
2554                         else{
2555                                 individual += estim[i];
2556                         }
2557                 }
2558                 container.push_back(individual);
2559                 
2560                 
2561                 
2562                 
2563 //              string individual;
2564 //              
2565 //              while (estim.find_first_of(',') != -1) {
2566 //                      individual = estim.substr(0,estim.find_first_of(','));
2567 //                      if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
2568 //                              estim = estim.substr(estim.find_first_of(',')+1, estim.length());
2569 //                              container.push_back(individual);
2570 //                      }
2571 //              }
2572 //              //get last one
2573 //              container.push_back(estim);
2574         }
2575         catch(exception& e) {
2576                 errorOut(e, "MothurOut", "splitAtComma");
2577                 exit(1);
2578         }       
2579 }
2580 /***********************************************************************/
2581 //This function splits up the various option parameters
2582 void MothurOut::splitAtChar(string& prefix, string& suffix, char c){
2583         try {
2584                 prefix = suffix.substr(0,suffix.find_first_of(c));
2585                 if ((suffix.find_first_of(c)+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
2586                         suffix = suffix.substr(suffix.find_first_of(c)+1, suffix.length());
2587                         string space = " ";
2588                         while(suffix.at(0) == ' ')
2589                                 suffix = suffix.substr(1, suffix.length());
2590                 }
2591         
2592         }
2593         catch(exception& e) {
2594                 errorOut(e, "MothurOut", "splitAtComma");
2595                 exit(1);
2596         }       
2597 }
2598
2599 /***********************************************************************/
2600
2601 //This function splits up the various option parameters
2602 void MothurOut::splitAtComma(string& prefix, string& suffix){
2603         try {
2604                 prefix = suffix.substr(0,suffix.find_first_of(','));
2605                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
2606                         suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
2607                         string space = " ";
2608                         while(suffix.at(0) == ' ')
2609                                 suffix = suffix.substr(1, suffix.length());
2610                 }
2611
2612         }
2613         catch(exception& e) {
2614                 errorOut(e, "MothurOut", "splitAtComma");
2615                 exit(1);
2616         }       
2617 }
2618 /***********************************************************************/
2619
2620 //This function separates the key value from the option value i.e. dist=96_...
2621 void MothurOut::splitAtEquals(string& key, string& value){              
2622         try {
2623                 if(value.find_first_of('=') != -1){
2624                         key = value.substr(0,value.find_first_of('='));
2625                         if ((value.find_first_of('=')+1) <= value.length()) {
2626                                 value = value.substr(value.find_first_of('=')+1, value.length());
2627                         }
2628                 }else{
2629                         key = value;
2630                         value = 1;
2631                 }
2632         }
2633         catch(exception& e) {
2634                 errorOut(e, "MothurOut", "splitAtEquals");
2635                 exit(1);
2636         }       
2637 }
2638
2639 /**************************************************************************************************/
2640
2641 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
2642         try {
2643                 for (int i = 0; i < Groups.size(); i++) {
2644                         if (groupname == Groups[i]) { return true; }
2645                 }
2646                 return false;
2647         }
2648         catch(exception& e) {
2649                 errorOut(e, "MothurOut", "inUsersGroups");
2650                 exit(1);
2651         }       
2652 }
2653 /**************************************************************************************************/
2654 //returns true if any of the strings in first vector are in second vector
2655 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
2656         try {
2657                 
2658                 for (int i = 0; i < groupnames.size(); i++) {
2659                         if (inUsersGroups(groupnames[i], Groups)) { return true; }
2660                 }
2661                 return false;
2662         }
2663         catch(exception& e) {
2664                 errorOut(e, "MothurOut", "inUsersGroups");
2665                 exit(1);
2666         }       
2667 }
2668 /***********************************************************************/
2669 //this function determines if the user has given us labels that are smaller than the given label.
2670 //if so then it returns true so that the calling function can run the previous valid distance.
2671 //it's a "smart" distance function.  It also checks for invalid labels.
2672 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
2673         try {
2674                 
2675                 set<string>::iterator it;
2676                 vector<float> orderFloat;
2677                 map<string, float> userMap;  //the conversion process removes trailing 0's which we need to put back
2678                 map<string, float>::iterator it2;
2679                 float labelFloat;
2680                 bool smaller = false;
2681                 
2682                 //unique is the smallest line
2683                 if (label == "unique") {  return false;  }
2684                 else { 
2685                         if (convertTestFloat(label, labelFloat)) {
2686                                 convert(label, labelFloat); 
2687                         }else { //cant convert 
2688                                 return false;
2689                         }
2690                 }
2691                 
2692                 //go through users set and make them floats
2693                 for(it = userLabels.begin(); it != userLabels.end();) {
2694                         
2695                         float temp;
2696                         if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
2697                                 convert(*it, temp);
2698                                 orderFloat.push_back(temp);
2699                                 userMap[*it] = temp;
2700                                 it++;
2701                         }else if (*it == "unique") { 
2702                                 orderFloat.push_back(-1.0);
2703                                 userMap["unique"] = -1.0;
2704                                 it++;
2705                         }else {
2706                                 if (errorOff == "") {  mothurOut(*it + " is not a valid label."); mothurOutEndLine();  }
2707                                 userLabels.erase(it++); 
2708                         }
2709                 }
2710                 
2711                 //sort order
2712                 sort(orderFloat.begin(), orderFloat.end());
2713                 
2714                 /*************************************************/
2715                 //is this label bigger than any of the users labels
2716                 /*************************************************/
2717                                 
2718                 //loop through order until you find a label greater than label
2719                 for (int i = 0; i < orderFloat.size(); i++) {
2720                         if (orderFloat[i] < labelFloat) {
2721                                 smaller = true;
2722                                 if (orderFloat[i] == -1) { 
2723                                         if (errorOff == "") { mothurOut("Your file does not include the label unique."); mothurOutEndLine(); }
2724                                         userLabels.erase("unique");
2725                                 }
2726                                 else {  
2727                                         if (errorOff == "") { mothurOut("Your file does not include the label "); mothurOutEndLine(); }
2728                                         string s = "";
2729                                         for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {  
2730                                                 if (it2->second == orderFloat[i]) {  
2731                                                         s = it2->first;  
2732                                                         //remove small labels
2733                                                         userLabels.erase(s);
2734                                                         break;
2735                                                 }
2736                                         }
2737                                         if (errorOff == "") {mothurOut( s +  ". I will use the next smallest distance. "); mothurOutEndLine(); }
2738                                 }
2739                         //since they are sorted once you find a bigger one stop looking
2740                         }else { break; }
2741                 }
2742                 
2743                 return smaller;
2744                                                 
2745         }
2746         catch(exception& e) {
2747                 errorOut(e, "MothurOut", "anyLabelsToProcess");
2748                 exit(1);
2749         }       
2750 }
2751
2752 /**************************************************************************************************/
2753 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
2754         try {
2755                 
2756                 bool good = true;
2757                 
2758                 string line = getline(file);  
2759
2760                 //before we added this check
2761                 if (line[0] != '#') {  good = false;  }
2762                 else {
2763                         //rip off #
2764                         line = line.substr(1);
2765                         
2766                         vector<string> versionVector;
2767                         splitAtChar(version, versionVector, '.');
2768                         
2769                         //check file version
2770                         vector<string> linesVector;
2771                         splitAtChar(line, linesVector, '.');
2772                         
2773                         if (versionVector.size() != linesVector.size()) { good = false; }
2774                         else {
2775                                 for (int j = 0; j < versionVector.size(); j++) {
2776                                         int num1, num2;
2777                                         convert(versionVector[j], num1);
2778                                         convert(linesVector[j], num2);
2779                                         
2780                                         //if mothurs version is newer than this files version, then we want to remake it
2781                                         if (num1 > num2) {  good = false; break;  }
2782                                 }
2783                         }
2784                         
2785                 }
2786                 
2787                 if (!good) {  file.close();  }
2788                 else { file.seekg(0);  }
2789                 
2790                 return good;
2791         }
2792         catch(exception& e) {
2793                 errorOut(e, "MothurOut", "checkReleaseVersion");                
2794                 exit(1);
2795         }
2796 }
2797 /**************************************************************************************************/
2798 bool MothurOut::isContainingOnlyDigits(string input) {
2799         try{
2800                 
2801                 //are you a digit in ascii code
2802                 for (int i = 0;i < input.length(); i++){
2803                         if( input[i]>47 && input[i]<58){}
2804                         else { return false; }
2805                 }
2806                 
2807                 return true;
2808         }
2809         catch(exception& e) {
2810                 errorOut(e, "MothurOut", "isContainingOnlyDigits");             
2811                 exit(1);
2812         }
2813 }
2814 /**************************************************************************************************/
2815 int MothurOut::removeConfidences(string& tax) {
2816         try {
2817                 
2818                 string taxon;
2819                 string newTax = "";
2820                 
2821                 while (tax.find_first_of(';') != -1) {
2822                         
2823                         if (control_pressed) { return 0; }
2824                         
2825                         //get taxon
2826                         taxon = tax.substr(0,tax.find_first_of(';'));
2827         
2828                         int pos = taxon.find_last_of('(');
2829                         if (pos != -1) {
2830                                 //is it a number?
2831                                 int pos2 = taxon.find_last_of(')');
2832                                 if (pos2 != -1) {
2833                                         string confidenceScore = taxon.substr(pos+1, (pos2-(pos+1)));
2834                                         if (isNumeric1(confidenceScore)) {
2835                                                 taxon = taxon.substr(0, pos); //rip off confidence 
2836                                         }
2837                                 }
2838                         }
2839                         taxon += ";";
2840                         
2841                         tax = tax.substr(tax.find_first_of(';')+1, tax.length());
2842                         newTax += taxon;
2843                 }
2844                 
2845                 tax = newTax;
2846                 
2847                 return 0;
2848         }
2849         catch(exception& e) {
2850                 errorOut(e, "MothurOut", "removeConfidences");
2851                 exit(1);
2852         }
2853 }
2854 /**************************************************************************************************/
2855
2856
2857
2858
2859