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