]> git.donarmstrong.com Git - mothur.git/blob - mothurout.cpp
initial add of metastats
[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 MothurOut* MothurOut::getInstance() {
14         if( _uniqueInstance == 0) {
15                 _uniqueInstance = new MothurOut();
16         }
17         return _uniqueInstance;
18 }
19 /*********************************************************************************************/
20 void MothurOut::setFileName(string filename)  {
21         try {
22                 logFileName = filename;
23                 
24                 #ifdef USE_MPI
25                         int pid;
26                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
27                                         
28                         if (pid == 0) { //only one process should output to screen
29                 #endif
30                 
31                 openOutputFile(filename, out);
32                 
33                 #ifdef USE_MPI
34                         }
35                 #endif
36         }
37         catch(exception& e) {
38                 errorOut(e, "MothurOut", "setFileName");
39                 exit(1);
40         }
41 }
42 /*********************************************************************************************/
43 void MothurOut::setDefaultPath(string pathname)  {
44         try {
45         
46                 //add / to name if needed
47                 string lastChar = pathname.substr(pathname.length()-1);
48                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
49                         if (lastChar != "/") { pathname += "/"; }
50                 #else
51                         if (lastChar != "\\") { pathname += "\\"; }     
52                 #endif
53                 
54                 defaultPath = pathname;
55                 
56         }
57         catch(exception& e) {
58                 errorOut(e, "MothurOut", "setDefaultPath");
59                 exit(1);
60         }
61 }
62 /*********************************************************************************************/
63 void MothurOut::closeLog()  {
64         try {
65                 
66                 #ifdef USE_MPI
67                         int pid;
68                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
69                                         
70                         if (pid == 0) { //only one process should output to screen
71                 #endif
72                 
73                 out.close();
74                 
75                 #ifdef USE_MPI
76                         }
77                 #endif
78         }
79         catch(exception& e) {
80                 errorOut(e, "MothurOut", "closeLog");
81                 exit(1);
82         }
83 }
84
85 /*********************************************************************************************/
86 MothurOut::~MothurOut() {
87         try {
88                 _uniqueInstance = 0;
89                 
90         }
91         catch(exception& e) {
92                 errorOut(e, "MothurOut", "MothurOut");
93                 exit(1);
94         }
95 }
96 /*********************************************************************************************/
97 void MothurOut::mothurOut(string output) {
98         try {
99                 
100                 #ifdef USE_MPI
101                         int pid;
102                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
103                                         
104                         if (pid == 0) { //only one process should output to screen
105                 #endif
106                 
107                 cout << output;
108                 out << output;
109                 
110                 #ifdef USE_MPI
111                         }
112                 #endif
113         }
114         catch(exception& e) {
115                 errorOut(e, "MothurOut", "MothurOut");
116                 exit(1);
117         }
118 }
119 /*********************************************************************************************/
120 void MothurOut::mothurOutEndLine() {
121         try {
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                 cout << endl;
130                 out << endl;
131                 
132                 #ifdef USE_MPI
133                         }
134                 #endif
135         }
136         catch(exception& e) {
137                 errorOut(e, "MothurOut", "MothurOutEndLine");
138                 exit(1);
139         }
140 }
141 /*********************************************************************************************/
142 void MothurOut::mothurOutJustToLog(string output) {
143         try {
144                 #ifdef USE_MPI
145                         int pid;
146                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
147                                         
148                         if (pid == 0) { //only one process should output to screen
149                 #endif
150                 
151                 out << output;
152                 
153                 #ifdef USE_MPI
154                         }
155                 #endif
156         }
157         catch(exception& e) {
158                 errorOut(e, "MothurOut", "MothurOutJustToLog");
159                 exit(1);
160         }
161 }
162 /*********************************************************************************************/
163 void MothurOut::errorOut(exception& e, string object, string function) {
164         //double vm, rss;
165         //mem_usage(vm, rss);
166         
167         mothurOut("[ERROR]: ");
168         mothurOut(toString(e.what()));
169         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.");
170         mothurOutEndLine();
171 }
172 /*********************************************************************************************/
173 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c 
174 // process_mem_usage(double &, double &) - takes two doubles by reference,
175 // attempts to read the system-dependent data for a process' virtual memory
176 // size and resident set size, and return the results in KB.
177 //
178 // On failure, returns 0.0, 0.0
179 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
180   #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
181   
182            vm_usage     = 0.0;
183            resident_set = 0.0;
184
185            // 'file' stat seems to give the most reliable results
186            //
187            ifstream stat_stream("/proc/self/stat",ios_base::in);
188
189            // dummy vars for leading entries in stat that we don't care about
190            //
191            string pid, comm, state, ppid, pgrp, session, tty_nr;
192            string tpgid, flags, minflt, cminflt, majflt, cmajflt;
193            string utime, stime, cutime, cstime, priority, nice;
194            string O, itrealvalue, starttime;
195
196            // the two fields we want
197            //
198            unsigned long vsize;
199            long rss;
200
201            stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
202                                    >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
203                                    >> utime >> stime >> cutime >> cstime >> priority >> nice
204                                    >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
205
206            long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
207            vm_usage     = vsize / 1024.0;
208            resident_set = rss * page_size_kb;
209            
210            mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
211                 return 0;
212
213         #else
214 /*              //windows memory usage
215                 // Get the list of process identifiers.
216                 DWORD aProcesses[1024], cbNeeded, cProcesses;
217                 
218                 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
219
220                 // Calculate how many process identifiers were returned.
221                 cProcesses = cbNeeded / sizeof(DWORD);
222
223                 // Print the memory usage for each process
224                 for (int i = 0; i < cProcesses; i++ ) {
225                         DWORD processID = aProcesses[i];
226                         
227                         PROCESS_MEMORY_COUNTERS pmc;
228
229                         HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
230
231                         // Print the process identifier.
232                         printf( "\nProcess ID: %u\n", processID);
233                         
234                         if (NULL != hProcess) {
235
236                                 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
237                                         printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
238                                         printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
239                                         printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
240                                         printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
241                                         printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
242                                         printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
243                                         printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
244                                         printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage ); 
245                                         printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
246                                 }
247                                 CloseHandle(hProcess);
248                         }
249                 }
250 */
251                         return 0;
252
253         #endif
254 }
255
256
257 /***********************************************************************/
258 int MothurOut::openOutputFileAppend(string fileName, ofstream& fileHandle){
259         try {
260                 fileName = getFullPathName(fileName);
261                 
262                 fileHandle.open(fileName.c_str(), ios::app);
263                 if(!fileHandle) {
264                         mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
265                         return 1;
266                 }
267                 else {
268                         return 0;
269                 }
270         }
271         catch(exception& e) {
272                 errorOut(e, "MothurOut", "openOutputFileAppend");
273                 exit(1);
274         }
275 }
276 /***********************************************************************/
277 void MothurOut::gobble(istream& f){
278         try {
279                 char d;
280                 while(isspace(d=f.get()))               {;}
281                 f.putback(d);
282         }
283         catch(exception& e) {
284                 errorOut(e, "MothurOut", "gobble");
285                 exit(1);
286         }
287 }
288 /***********************************************************************/
289 void MothurOut::gobble(istringstream& f){
290         try {
291                 char d;
292                 while(isspace(d=f.get()))               {;}
293                 f.putback(d);
294         }
295         catch(exception& e) {
296                 errorOut(e, "MothurOut", "gobble");
297                 exit(1);
298         }
299 }
300
301 /***********************************************************************/
302
303 string MothurOut::getline(istringstream& fileHandle) {
304         try {
305         
306                 string line = "";
307                 
308                 while (!fileHandle.eof())       {
309                         //get next character
310                         char c = fileHandle.get(); 
311                         
312                         //are you at the end of the line
313                         if ((c == '\n') || (c == '\r') || (c == '\f')){  break; }       
314                         else {          line += c;              }
315                 }
316                 
317                 return line;
318                 
319         }
320         catch(exception& e) {
321                 errorOut(e, "MothurOut", "getline");
322                 exit(1);
323         }
324 }
325 /***********************************************************************/
326
327 string MothurOut::getline(ifstream& fileHandle) {
328         try {
329         
330                 string line = "";
331                 
332                 while (!fileHandle.eof())       {
333                         //get next character
334                         char c = fileHandle.get(); 
335                         
336                         //are you at the end of the line
337                         if ((c == '\n') || (c == '\r') || (c == '\f')){  break; }       
338                         else {          line += c;              }
339                 }
340                 
341                 return line;
342                 
343         }
344         catch(exception& e) {
345                 errorOut(e, "MothurOut", "getline");
346                 exit(1);
347         }
348 }
349 /***********************************************************************/
350
351 string MothurOut::getRootName(string longName){
352         try {
353         
354                 string rootName = longName;
355                 
356                 if(longName.find_last_of(".") != longName.npos){
357                         int pos = longName.find_last_of('.')+1;
358                         rootName = longName.substr(0, pos);
359                 }
360
361                 return rootName;
362         }
363         catch(exception& e) {
364                 errorOut(e, "MothurOut", "getRootName");
365                 exit(1);
366         }
367 }
368 /***********************************************************************/
369
370 string MothurOut::getSimpleName(string longName){
371         try {
372                 string simpleName = longName;
373                 
374                 size_t found;
375                 found=longName.find_last_of("/\\");
376
377                 if(found != longName.npos){
378                         simpleName = longName.substr(found+1);
379                 }
380                 
381                 return simpleName;
382         }
383         catch(exception& e) {
384                 errorOut(e, "MothurOut", "getSimpleName");
385                 exit(1);
386         }
387 }
388
389 /***********************************************************************/
390
391 string MothurOut::getPathName(string longName){
392         try {
393                 string rootPathName = longName;
394                 
395                 if(longName.find_last_of("/\\") != longName.npos){
396                         int pos = longName.find_last_of("/\\")+1;
397                         rootPathName = longName.substr(0, pos);
398                 }
399                 
400                 return rootPathName;
401         }
402         catch(exception& e) {
403                 errorOut(e, "MothurOut", "getPathName");
404                 exit(1);
405         }       
406
407 }
408 /***********************************************************************/
409
410 string MothurOut::hasPath(string longName){
411         try {
412                 string path = "";
413                 
414                 size_t found;
415                 found=longName.find_last_of("~/\\");
416
417                 if(found != longName.npos){
418                         path = longName.substr(0, found+1);
419                 }
420                 
421                 return path;
422         }
423         catch(exception& e) {
424                 errorOut(e, "MothurOut", "hasPath");
425                 exit(1);
426         }       
427 }
428
429 /***********************************************************************/
430
431 string MothurOut::getExtension(string longName){
432         try {
433                 string extension = longName;
434                 
435                 if(longName.find_last_of('.') != longName.npos){
436                         int pos = longName.find_last_of('.');
437                         extension = longName.substr(pos, longName.length());
438                 }
439                 
440                 return extension;
441         }
442         catch(exception& e) {
443                 errorOut(e, "MothurOut", "getExtension");
444                 exit(1);
445         }       
446 }
447 /***********************************************************************/
448 bool MothurOut::isBlank(string fileName){
449         try {
450                 
451                 fileName = getFullPathName(fileName);
452                 
453                 ifstream fileHandle;
454                 fileHandle.open(fileName.c_str());
455                 if(!fileHandle) {
456                         mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
457                         return false;
458                 }else {
459                         //check for blank file
460                         gobble(fileHandle);
461                         if (fileHandle.eof()) { fileHandle.close(); return true;  }
462                 }
463                 return false;
464         }
465         catch(exception& e) {
466                 errorOut(e, "MothurOut", "isBlank");
467                 exit(1);
468         }       
469 }
470 /***********************************************************************/
471
472 string MothurOut::getFullPathName(string fileName){
473         try{
474         
475         string path = hasPath(fileName);
476         string newFileName;
477         int pos;
478         
479         if (path == "") { return fileName; } //its a simple name
480         else { //we need to complete the pathname
481                 // ex. ../../../filename 
482                 // cwd = /user/work/desktop
483                                 
484                 string cwd;
485                 //get current working directory 
486                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)   
487                         
488                         if (path.find("~") != -1) { //go to home directory
489                                 string homeDir = getenv ("HOME");
490                                 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
491                                 return newFileName;
492                         }else { //find path
493                                 if (path.rfind("./") == -1) { return fileName; } //already complete name
494                                 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
495                                 
496                                 //char* cwdpath = new char[1024];
497
498                                 //size_t size;
499                                 //cwdpath=getcwd(cwdpath,size);
500                                 //cwd = cwdpath;
501                                 
502                                 char *cwdpath = NULL;
503                                 cwdpath = getcwd(NULL, 0); // or _getcwd
504                                 if ( cwdpath != NULL) { cwd = cwdpath; }
505                                 else { cwd = "";  }
506                                 
507                                 //rip off first '/'
508                                 string simpleCWD;
509                                 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
510                                 
511                                 //break apart the current working directory
512                                 vector<string> dirs;
513                                 while (simpleCWD.find_first_of('/') != -1) {
514                                         string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
515                                         simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
516                                         dirs.push_back(dir);
517                                 }
518                                 //get last one              // ex. ../../../filename = /user/work/desktop/filename
519                                 dirs.push_back(simpleCWD);  //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
520                                 
521                         
522                                 int index = dirs.size()-1;
523                 
524                                 while((pos = path.rfind("./")) != -1) { //while you don't have a complete path
525                                         if (pos == 0) { break;  //you are at the end
526                                         }else if (path[(pos-1)] == '.') { //you want your parent directory ../
527                                                 path = path.substr(0, pos-1);
528                                                 index--;
529                                                 if (index == 0) {  break; }
530                                         }else if (path[(pos-1)] == '/') { //you want the current working dir ./
531                                                 path = path.substr(0, pos);
532                                         }else if (pos == 1) { break;  //you are at the end
533                                         }else { cout << "cannot resolve path for " <<  fileName << endl; return fileName; }
534                                 }
535                         
536                                 for (int i = index; i >= 0; i--) {
537                                         newFileName = dirs[i] +  "/" + newFileName;             
538                                 }
539                                 
540                                 newFileName =  "/" +  newFileName;
541                                 return newFileName;
542                 
543                         }       
544                 #else
545                         if (path.find("~") != -1) { //go to home directory
546                                 string homeDir = getenv ("HOMEPATH");
547                                 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
548                                 return newFileName;
549                         }else { //find path
550                                 if (path.rfind(".\\") == -1) { return fileName; } //already complete name
551                                 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
552                                                         
553                                 char *cwdpath = NULL;
554                                 cwdpath = getcwd(NULL, 0); // or _getcwd
555                                 if ( cwdpath != NULL) { cwd = cwdpath; }
556                                 else { cwd = "";  }
557                                 
558                                 //break apart the current working directory
559                                 vector<string> dirs;
560                                 while (cwd.find_first_of('\\') != -1) {
561                                         string dir = cwd.substr(0,cwd.find_first_of('\\'));
562                                         cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
563                                         dirs.push_back(dir);
564                 
565                                 }
566                                 //get last one
567                                 dirs.push_back(cwd);  //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
568                                         
569                                 int index = dirs.size()-1;
570                                         
571                                 while((pos = path.rfind(".\\")) != -1) { //while you don't have a complete path
572                                         if (pos == 0) { break;  //you are at the end
573                                         }else if (path[(pos-1)] == '.') { //you want your parent directory ../
574                                                 path = path.substr(0, pos-1);
575                                                 index--;
576                                                 if (index == 0) {  break; }
577                                         }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
578                                                 path = path.substr(0, pos);
579                                         }else if (pos == 1) { break;  //you are at the end
580                                         }else { cout << "cannot resolve path for " <<  fileName << endl; return fileName; }
581                                 }
582                         
583                                 for (int i = index; i >= 0; i--) {
584                                         newFileName = dirs[i] +  "\\" + newFileName;            
585                                 }
586                                 
587                                 return newFileName;
588                         }
589                         
590                 #endif
591         }
592         }
593         catch(exception& e) {
594                 errorOut(e, "MothurOut", "getFullPathName");
595                 exit(1);
596         }       
597 }
598 /***********************************************************************/
599 //no error open
600 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
601         try {
602                         //get full path name
603                         string completeFileName = getFullPathName(fileName);
604
605                         fileHandle.open(completeFileName.c_str());
606                         if(!fileHandle) {
607                                 return 1;
608                         }else {
609                                 //check for blank file
610                                 gobble(fileHandle);
611                                 return 0;
612                         }
613         }
614         catch(exception& e) {
615                 errorOut(e, "MothurOut", "openInputFile - no Error");
616                 exit(1);
617         }
618 }
619 /***********************************************************************/
620
621 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
622         try {
623                 //get full path name
624                 string completeFileName = getFullPathName(fileName);
625
626                 fileHandle.open(completeFileName.c_str());
627                 if(!fileHandle) {
628                         mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
629                         return 1;
630                 }
631                 else {
632                         //check for blank file
633                         gobble(fileHandle);
634                         if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine();  }
635                         
636                         return 0;
637                 }
638         }
639         catch(exception& e) {
640                 errorOut(e, "MothurOut", "openInputFile");
641                 exit(1);
642         }       
643 }
644 /***********************************************************************/
645
646 int MothurOut::renameFile(string oldName, string newName){
647         try {
648                 ifstream inTest;
649                 int exist = openInputFile(newName, inTest, "");
650                 
651         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)           
652                 if (exist == 0) { //you could open it so you want to delete it
653                         inTest.close();
654                         string command = "rm " + newName;
655                         system(command.c_str());
656                 }
657                                 
658                 string command = "mv " + oldName + " " + newName;
659                 system(command.c_str());
660         #else
661                 remove(newName.c_str());
662                 int renameOk = rename(oldName.c_str(), newName.c_str());
663         #endif
664                 return 0;
665                 
666         }
667         catch(exception& e) {
668                 errorOut(e, "MothurOut", "renameFile");
669                 exit(1);
670         }       
671 }
672
673 /***********************************************************************/
674
675 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
676         try { 
677         
678                 string completeFileName = getFullPathName(fileName);
679                 
680                 fileHandle.open(completeFileName.c_str(), ios::trunc);
681                 if(!fileHandle) {
682                         mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
683                         return 1;
684                 }
685                 else {
686                         return 0;
687                 }
688         }
689         catch(exception& e) {
690                 errorOut(e, "MothurOut", "openOutputFile");
691                 exit(1);
692         }       
693
694 }
695
696 /**************************************************************************************************/
697 void MothurOut::appendFiles(string temp, string filename) {
698         try{
699                 ofstream output;
700                 ifstream input;
701         
702                 //open output file in append mode
703                 openOutputFileAppend(filename, output);
704                 int ableToOpen = openInputFile(temp, input, "no error");
705                 
706                 if (ableToOpen == 0) { //you opened it
707                         while(char c = input.get()){
708                                 if(input.eof())         {       break;                  }
709                                 else                            {       output << c;    }
710                         }
711                         input.close();
712                 }
713                 
714                 output.close();
715         }
716         catch(exception& e) {
717                 errorOut(e, "MothurOut", "appendFiles");
718                 exit(1);
719         }       
720 }
721
722 /**************************************************************************************************/
723 string MothurOut::sortFile(string distFile, string outputDir){
724         try {   
725         
726                 //if (outputDir == "") {  outputDir += hasPath(distFile);  }
727                 string outfile = getRootName(distFile) + "sorted.dist";
728
729                 
730                 //if you can, use the unix sort since its been optimized for years
731                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
732                         string command = "sort -n -k +3 " + distFile + " -o " + outfile;
733                         system(command.c_str());
734                 #else //you are stuck with my best attempt...
735                         //windows sort does not have a way to specify a column, only a character in the line
736                         //since we cannot assume that the distance will always be at the the same character location on each line
737                         //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
738                 
739                         //read in file line by file and put distance first
740                         string tempDistFile = distFile + ".temp";
741                         ifstream input;
742                         ofstream output;
743                         openInputFile(distFile, input);
744                         openOutputFile(tempDistFile, output);
745
746                         string firstName, secondName;
747                         float dist;
748                         while (input) {
749                                 input >> firstName >> secondName >> dist;
750                                 output << dist << '\t' << firstName << '\t' << secondName << endl;
751                                 gobble(input);
752                         }
753                         input.close();
754                         output.close();
755                 
756         
757                         //sort using windows sort
758                         string tempOutfile = outfile + ".temp";
759                         string command = "sort " + tempDistFile + " /O " + tempOutfile;
760                         system(command.c_str());
761                 
762                         //read in sorted file and put distance at end again
763                         ifstream input2;
764                         openInputFile(tempOutfile, input2);
765                         openOutputFile(outfile, output);
766                 
767                         while (input2) {
768                                 input2 >> dist >> firstName >> secondName;
769                                 output << firstName << '\t' << secondName << '\t' << dist << endl;
770                                 gobble(input2);
771                         }
772                         input2.close();
773                         output.close();
774                 
775                         //remove temp files
776                         remove(tempDistFile.c_str());
777                         remove(tempOutfile.c_str());
778                 #endif
779                 
780                 return outfile;
781         }
782         catch(exception& e) {
783                 errorOut(e, "MothurOut", "sortFile");
784                 exit(1);
785         }       
786 }
787 /**************************************************************************************************/
788 vector<unsigned long int> MothurOut::setFilePosFasta(string filename, int& num) {
789         try {
790                         vector<unsigned long int> positions;
791                         ifstream inFASTA;
792                         openInputFile(filename, inFASTA);
793                                                 
794                         string input;
795                         while(!inFASTA.eof()){
796                                 input = getline(inFASTA); 
797                                 if (input.length() != 0) {
798                                         if(input[0] == '>'){    unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
799                                 }
800                                 gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
801                         }
802                         inFASTA.close();
803                 
804                         num = positions.size();
805                 
806                         /*FILE * pFile;
807                         long size;
808                 
809                         //get num bytes in file
810                         pFile = fopen (filename.c_str(),"rb");
811                         if (pFile==NULL) perror ("Error opening file");
812                         else{
813                                 fseek (pFile, 0, SEEK_END);
814                                 size=ftell (pFile);
815                                 fclose (pFile);
816                         }*/
817                         
818                         unsigned long int size = positions[(positions.size()-1)];
819                         ifstream in;
820                         openInputFile(filename, in);
821                         
822                         in.seekg(size);
823                 
824                         while(char c = in.get()){
825                                 if(in.eof())            {       break;  }
826                                 else                            {       size++; }
827                         }
828                         in.close();
829                 
830                         positions.push_back(size);
831                 
832                         return positions;
833         }
834         catch(exception& e) {
835                 errorOut(e, "MothurOut", "setFilePosFasta");
836                 exit(1);
837         }
838 }
839 /**************************************************************************************************/
840 vector<unsigned long int> MothurOut::setFilePosEachLine(string filename, int& num) {
841         try {
842                         filename = getFullPathName(filename);
843                         
844                         vector<unsigned long int> positions;
845                         ifstream in;
846                         openInputFile(filename, in);
847                                 
848                         string input;
849                         while(!in.eof()){
850                                 unsigned long int lastpos = in.tellg();
851                                 input = getline(in); 
852                                 if (input.length() != 0) {
853                                         unsigned long int pos = in.tellg(); 
854                                         if (pos != -1) { positions.push_back(pos - input.length() - 1); }
855                                         else {  positions.push_back(lastpos);  }
856                                 }
857                                 gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
858                         }
859                         in.close();
860                 
861                         num = positions.size();
862                 
863                         FILE * pFile;
864                         unsigned long int size;
865                         
866                         //get num bytes in file
867                         pFile = fopen (filename.c_str(),"rb");
868                         if (pFile==NULL) perror ("Error opening file");
869                         else{
870                                 fseek (pFile, 0, SEEK_END);
871                                 size=ftell (pFile);
872                                 fclose (pFile);
873                         }
874                 
875                         positions.push_back(size);
876                 
877                         return positions;
878         }
879         catch(exception& e) {
880                 errorOut(e, "MothurOut", "setFilePosEachLine");
881                 exit(1);
882         }
883 }
884 /**************************************************************************************************/
885
886 vector<unsigned long int> MothurOut::divideFile(string filename, int& proc) {
887         try{
888         
889                 vector<unsigned long int> filePos;
890                 filePos.push_back(0);
891                 
892                 FILE * pFile;
893                 unsigned long int size;
894                 
895                 filename = getFullPathName(filename);
896                 
897                 //get num bytes in file
898                 pFile = fopen (filename.c_str(),"rb");
899                 if (pFile==NULL) perror ("Error opening file");
900                 else{
901                         fseek (pFile, 0, SEEK_END);
902                         size=ftell (pFile);
903                         fclose (pFile);
904                 }
905         
906                 //estimate file breaks
907                 unsigned long int chunkSize = 0;
908                 chunkSize = size / proc;
909         
910                 //file to small to divide by processors
911                 if (chunkSize == 0)  {  proc = 1;       filePos.push_back(size); return filePos;        }
912         
913                 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
914                 for (int i = 0; i < proc; i++) {
915                         unsigned long int spot = (i+1) * chunkSize;
916                         
917                         ifstream in;
918                         openInputFile(filename, in);
919                         in.seekg(spot);
920                         
921                         //look for next '>'
922                         unsigned long int newSpot = spot;
923                         while (!in.eof()) {
924                            char c = in.get();
925                            if (c == '>') {   in.putback(c); newSpot = in.tellg(); break;  }
926                         }
927                         
928                         //there was not another sequence before the end of the file
929                         unsigned long int sanityPos = in.tellg();
930
931                         if (sanityPos == -1) {  break;  }
932                         else {  filePos.push_back(newSpot);  }
933                         
934                         in.close();
935                 }
936                 
937                 //save end pos
938                 filePos.push_back(size);
939
940                 //sanity check filePos
941                 for (int i = 0; i < (filePos.size()-1); i++) {
942                         if (filePos[(i+1)] <= filePos[i]) {  filePos.erase(filePos.begin()+(i+1)); i--; }
943                 }
944
945                 proc = (filePos.size() - 1);
946                 
947                 return filePos;
948         }
949         catch(exception& e) {
950                 errorOut(e, "MothurOut", "divideFile");
951                 exit(1);
952         }
953 }
954
955 /***********************************************************************/
956
957 bool MothurOut::isTrue(string f){
958         try {
959                 
960                 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
961                 
962                 if ((f == "TRUE") || (f == "T")) {      return true;    }
963                 else {  return false;  }
964         }
965         catch(exception& e) {
966                 errorOut(e, "MothurOut", "isTrue");
967                 exit(1);
968         }
969 }
970
971 /***********************************************************************/
972
973 float MothurOut::roundDist(float dist, int precision){
974         try {
975                 return int(dist * precision + 0.5)/float(precision);
976         }
977         catch(exception& e) {
978                 errorOut(e, "MothurOut", "roundDist");
979                 exit(1);
980         }
981 }
982 /***********************************************************************/
983
984 float MothurOut::ceilDist(float dist, int precision){
985         try {
986                 return int(ceil(dist * precision))/float(precision);
987         }
988         catch(exception& e) {
989                 errorOut(e, "MothurOut", "ceilDist");
990                 exit(1);
991         }
992 }
993
994 /***********************************************************************/
995
996 int MothurOut::getNumNames(string names){
997         try {
998                 int count = 0;
999                 
1000                 if(names != ""){
1001                         count = 1;
1002                         for(int i=0;i<names.size();i++){
1003                                 if(names[i] == ','){
1004                                         count++;
1005                                 }
1006                         }
1007                 }
1008                 
1009                 return count;
1010         }
1011         catch(exception& e) {
1012                 errorOut(e, "MothurOut", "getNumNames");
1013                 exit(1);
1014         }
1015 }
1016
1017 /**************************************************************************************************/
1018
1019 vector<vector<double> > MothurOut::binomial(int maxOrder){
1020         try {
1021         vector<vector<double> > binomial(maxOrder+1);
1022         
1023     for(int i=0;i<=maxOrder;i++){
1024                 binomial[i].resize(maxOrder+1);
1025                 binomial[i][0]=1;
1026                 binomial[0][i]=0;
1027     }
1028     binomial[0][0]=1;
1029         
1030     binomial[1][0]=1;
1031     binomial[1][1]=1;
1032         
1033     for(int i=2;i<=maxOrder;i++){
1034                 binomial[1][i]=0;
1035     }
1036         
1037     for(int i=2;i<=maxOrder;i++){
1038                 for(int j=1;j<=maxOrder;j++){
1039                         if(i==j){       binomial[i][j]=1;                                                                       }
1040                         if(j>i) {       binomial[i][j]=0;                                                                       }
1041                         else    {       binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j];     }
1042                 }
1043     }
1044         
1045         return binomial;
1046         
1047         }
1048         catch(exception& e) {
1049                 errorOut(e, "MothurOut", "binomial");
1050                 exit(1);
1051         }
1052 }
1053
1054 /***********************************************************************/
1055
1056 int MothurOut::factorial(int num){
1057         try {
1058                 int total = 1;
1059                 
1060                 for (int i = 1; i <= num; i++) {
1061                         total *= i;
1062                 }
1063                 
1064                 return total;
1065         }
1066         catch(exception& e) {
1067                 errorOut(e, "MothurOut", "factorial");
1068                 exit(1);
1069         }
1070 }
1071 /***********************************************************************/
1072
1073 int MothurOut::getNumSeqs(ifstream& file){
1074         try {
1075                 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1076                 file.seekg(0);
1077                 return numSeqs;
1078         }
1079         catch(exception& e) {
1080                 errorOut(e, "MothurOut", "getNumSeqs");
1081                 exit(1);
1082         }       
1083 }
1084 /***********************************************************************/
1085 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1086         try {
1087                 string input;
1088                 numSeqs = 0;
1089                 while(!file.eof()){
1090                         input = getline(file);
1091                         if (input.length() != 0) {
1092                                 if(input[0] == '>'){ numSeqs++; }
1093                         }
1094                 }
1095         }
1096         catch(exception& e) {
1097                 errorOut(e, "MothurOut", "getNumSeqs");
1098                 exit(1);
1099         }       
1100 }
1101 /***********************************************************************/
1102
1103 //This function parses the estimator options and puts them in a vector
1104 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1105         try {
1106                 string individual;
1107                 
1108                 while (estim.find_first_of(symbol) != -1) {
1109                         individual = estim.substr(0,estim.find_first_of(symbol));
1110                         if ((estim.find_first_of(symbol)+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1111                                 estim = estim.substr(estim.find_first_of(symbol)+1, estim.length());
1112                                 container.push_back(individual);
1113                         }
1114                 }
1115                 //get last one
1116                 container.push_back(estim);
1117         }
1118         catch(exception& e) {
1119                 errorOut(e, "MothurOut", "splitAtChar");
1120                 exit(1);
1121         }       
1122 }
1123
1124 /***********************************************************************/
1125
1126 //This function parses the estimator options and puts them in a vector
1127 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1128         try {
1129                 string individual = "";
1130                 int estimLength = estim.size();
1131                 for(int i=0;i<estimLength;i++){
1132                         if(estim[i] == '-'){
1133                                 container.push_back(individual);
1134                                 individual = "";                                
1135                         }
1136                         else{
1137                                 individual += estim[i];
1138                         }
1139                 }
1140                 container.push_back(individual);
1141
1142         
1143         /*      string individual;
1144                 
1145                 while (estim.find_first_of('-') != -1) {
1146                         individual = estim.substr(0,estim.find_first_of('-'));
1147                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1148                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1149                                 container.push_back(individual);
1150                         }
1151                 }
1152                 //get last one
1153                 container.push_back(estim); */
1154         }
1155         catch(exception& e) {
1156                 errorOut(e, "MothurOut", "splitAtDash");
1157                 exit(1);
1158         }       
1159 }
1160
1161 /***********************************************************************/
1162 //This function parses the label options and puts them in a set
1163 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1164         try {
1165                 string individual = "";
1166                 int estimLength = estim.size();
1167                 for(int i=0;i<estimLength;i++){
1168                         if(estim[i] == '-'){
1169                                 container.insert(individual);
1170                                 individual = "";                                
1171                         }
1172                         else{
1173                                 individual += estim[i];
1174                         }
1175                 }
1176                 container.insert(individual);
1177
1178         //      string individual;
1179                 
1180         //      while (estim.find_first_of('-') != -1) {
1181         //              individual = estim.substr(0,estim.find_first_of('-'));
1182         //              if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1183         //                      estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1184         //                      container.insert(individual);
1185         //              }
1186         //      }
1187                 //get last one
1188         //      container.insert(estim);
1189         
1190         }
1191         catch(exception& e) {
1192                 errorOut(e, "MothurOut", "splitAtDash");
1193                 exit(1);
1194         }       
1195 }
1196 /***********************************************************************/
1197 //This function parses the line options and puts them in a set
1198 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1199         try {
1200                 string individual;
1201                 int lineNum;
1202                 
1203                 while (estim.find_first_of('-') != -1) {
1204                         individual = estim.substr(0,estim.find_first_of('-'));
1205                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1206                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1207                                 convert(individual, lineNum); //convert the string to int
1208                                 container.insert(lineNum);
1209                         }
1210                 }
1211                 //get last one
1212                 convert(estim, lineNum); //convert the string to int
1213                 container.insert(lineNum);
1214         }
1215         catch(exception& e) {
1216                 errorOut(e, "MothurOut", "splitAtDash");
1217                 exit(1);
1218         }       
1219 }
1220 /***********************************************************************/
1221 //This function parses the a string and puts peices in a vector
1222 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1223         try {
1224                 string individual = "";
1225                 int estimLength = estim.size();
1226                 for(int i=0;i<estimLength;i++){
1227                         if(estim[i] == ','){
1228                                 container.push_back(individual);
1229                                 individual = "";                                
1230                         }
1231                         else{
1232                                 individual += estim[i];
1233                         }
1234                 }
1235                 container.push_back(individual);
1236                 
1237                 
1238                 
1239                 
1240 //              string individual;
1241 //              
1242 //              while (estim.find_first_of(',') != -1) {
1243 //                      individual = estim.substr(0,estim.find_first_of(','));
1244 //                      if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1245 //                              estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1246 //                              container.push_back(individual);
1247 //                      }
1248 //              }
1249 //              //get last one
1250 //              container.push_back(estim);
1251         }
1252         catch(exception& e) {
1253                 errorOut(e, "MothurOut", "splitAtComma");
1254                 exit(1);
1255         }       
1256 }
1257 /***********************************************************************/
1258
1259 //This function splits up the various option parameters
1260 void MothurOut::splitAtComma(string& prefix, string& suffix){
1261         try {
1262                 prefix = suffix.substr(0,suffix.find_first_of(','));
1263                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
1264                         suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1265                         string space = " ";
1266                         while(suffix.at(0) == ' ')
1267                                 suffix = suffix.substr(1, suffix.length());
1268                 }
1269
1270         }
1271         catch(exception& e) {
1272                 errorOut(e, "MothurOut", "splitAtComma");
1273                 exit(1);
1274         }       
1275 }
1276 /***********************************************************************/
1277
1278 //This function separates the key value from the option value i.e. dist=96_...
1279 void MothurOut::splitAtEquals(string& key, string& value){              
1280         try {
1281                 if(value.find_first_of('=') != -1){
1282                         key = value.substr(0,value.find_first_of('='));
1283                         if ((value.find_first_of('=')+1) <= value.length()) {
1284                                 value = value.substr(value.find_first_of('=')+1, value.length());
1285                         }
1286                 }else{
1287                         key = value;
1288                         value = 1;
1289                 }
1290         }
1291         catch(exception& e) {
1292                 errorOut(e, "MothurOut", "splitAtEquals");
1293                 exit(1);
1294         }       
1295 }
1296
1297 /**************************************************************************************************/
1298
1299 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1300         try {
1301                 for (int i = 0; i < Groups.size(); i++) {
1302                         if (groupname == Groups[i]) { return true; }
1303                 }
1304                 return false;
1305         }
1306         catch(exception& e) {
1307                 errorOut(e, "MothurOut", "inUsersGroups");
1308                 exit(1);
1309         }       
1310 }
1311 /**************************************************************************************************/
1312 //returns true if any of the strings in first vector are in second vector
1313 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1314         try {
1315                 
1316                 for (int i = 0; i < groupnames.size(); i++) {
1317                         if (inUsersGroups(groupnames[i], Groups)) { return true; }
1318                 }
1319                 return false;
1320         }
1321         catch(exception& e) {
1322                 errorOut(e, "MothurOut", "inUsersGroups");
1323                 exit(1);
1324         }       
1325 }
1326 /***********************************************************************/
1327 //this function determines if the user has given us labels that are smaller than the given label.
1328 //if so then it returns true so that the calling function can run the previous valid distance.
1329 //it's a "smart" distance function.  It also checks for invalid labels.
1330 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1331         try {
1332                 
1333                 set<string>::iterator it;
1334                 vector<float> orderFloat;
1335                 map<string, float> userMap;  //the conversion process removes trailing 0's which we need to put back
1336                 map<string, float>::iterator it2;
1337                 float labelFloat;
1338                 bool smaller = false;
1339                 
1340                 //unique is the smallest line
1341                 if (label == "unique") {  return false;  }
1342                 else { 
1343                         if (convertTestFloat(label, labelFloat)) {
1344                                 convert(label, labelFloat); 
1345                         }else { //cant convert 
1346                                 return false;
1347                         }
1348                 }
1349                 
1350                 //go through users set and make them floats
1351                 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
1352                         
1353                         float temp;
1354                         if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1355                                 convert(*it, temp);
1356                                 orderFloat.push_back(temp);
1357                                 userMap[*it] = temp;
1358                         }else if (*it == "unique") { 
1359                                 orderFloat.push_back(-1.0);
1360                                 userMap["unique"] = -1.0;
1361                         }else {
1362                                 if (errorOff == "") {  cout << *it << " is not a valid label." << endl;  }
1363                                 userLabels.erase(*it); 
1364                                 it--;
1365                         }
1366                 }
1367                 
1368                 //sort order
1369                 sort(orderFloat.begin(), orderFloat.end());
1370                 
1371                 /*************************************************/
1372                 //is this label bigger than any of the users labels
1373                 /*************************************************/
1374                                 
1375                 //loop through order until you find a label greater than label
1376                 for (int i = 0; i < orderFloat.size(); i++) {
1377                         if (orderFloat[i] < labelFloat) {
1378                                 smaller = true;
1379                                 if (orderFloat[i] == -1) { 
1380                                         if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
1381                                         userLabels.erase("unique");
1382                                 }
1383                                 else {  
1384                                         if (errorOff == "") { cout << "Your file does not include the label " << endl; }
1385                                         string s = "";
1386                                         for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {  
1387                                                 if (it2->second == orderFloat[i]) {  
1388                                                         s = it2->first;  
1389                                                         //remove small labels
1390                                                         userLabels.erase(s);
1391                                                         break;
1392                                                 }
1393                                         }
1394                                         if (errorOff == "") {cout << s <<  ". I will use the next smallest distance. " << endl; }
1395                                 }
1396                         //since they are sorted once you find a bigger one stop looking
1397                         }else { break; }
1398                 }
1399                 
1400                 return smaller;
1401                                                 
1402         }
1403         catch(exception& e) {
1404                 errorOut(e, "MothurOut", "anyLabelsToProcess");
1405                 exit(1);
1406         }       
1407 }
1408
1409 /**************************************************************************************************/
1410 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1411         try {
1412                 
1413                 bool good = true;
1414                 
1415                 string line = getline(file);  
1416
1417                 //before we added this check
1418                 if (line[0] != '#') {  good = false;  }
1419                 else {
1420                         //rip off #
1421                         line = line.substr(1);
1422                         
1423                         vector<string> versionVector;
1424                         splitAtChar(version, versionVector, '.');
1425                         
1426                         //check file version
1427                         vector<string> linesVector;
1428                         splitAtChar(line, linesVector, '.');
1429                         
1430                         if (versionVector.size() != linesVector.size()) { good = false; }
1431                         else {
1432                                 for (int j = 0; j < versionVector.size(); j++) {
1433                                         int num1, num2;
1434                                         convert(versionVector[j], num1);
1435                                         convert(linesVector[j], num2);
1436                                         
1437                                         //if mothurs version is newer than this files version, then we want to remake it
1438                                         if (num1 > num2) {  good = false; break;  }
1439                                 }
1440                         }
1441                         
1442                 }
1443                 
1444                 if (!good) {  file.close();  }
1445                 else { file.seekg(0);  }
1446                 
1447                 return good;
1448         }
1449         catch(exception& e) {
1450                 errorOut(e, "MothurOut", "checkReleaseVersion");                
1451                 exit(1);
1452         }
1453 }
1454 /**************************************************************************************************/
1455
1456
1457
1458
1459