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