]> git.donarmstrong.com Git - mothur.git/blob - mothurout.cpp
moved utilities out of mothur.h and into mothurOut class.
[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                 fileHandle.open(fileName.c_str(), ios::app);
261                 if(!fileHandle) {
262                         mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
263                         return 1;
264                 }
265                 else {
266                         return 0;
267                 }
268         }
269         catch(exception& e) {
270                 errorOut(e, "MothurOut", "openOutputFileAppend");
271                 exit(1);
272         }
273 }
274 /***********************************************************************/
275 void MothurOut::gobble(istream& f){
276         try {
277                 char d;
278                 while(isspace(d=f.get()))               {;}
279                 f.putback(d);
280         }
281         catch(exception& e) {
282                 errorOut(e, "MothurOut", "gobble");
283                 exit(1);
284         }
285 }
286 /***********************************************************************/
287 void MothurOut::gobble(istringstream& f){
288         try {
289                 char d;
290                 while(isspace(d=f.get()))               {;}
291                 f.putback(d);
292         }
293         catch(exception& e) {
294                 errorOut(e, "MothurOut", "gobble");
295                 exit(1);
296         }
297 }
298
299 /***********************************************************************/
300
301 string MothurOut::getline(istringstream& fileHandle) {
302         try {
303         
304                 string line = "";
305                 
306                 while (!fileHandle.eof())       {
307                         //get next character
308                         char c = fileHandle.get(); 
309                         
310                         //are you at the end of the line
311                         if ((c == '\n') || (c == '\r') || (c == '\f')){  break; }       
312                         else {          line += c;              }
313                 }
314                 
315                 return line;
316                 
317         }
318         catch(exception& e) {
319                 errorOut(e, "MothurOut", "getline");
320                 exit(1);
321         }
322 }
323 /***********************************************************************/
324
325 string MothurOut::getline(ifstream& fileHandle) {
326         try {
327         
328                 string line = "";
329                 
330                 while (!fileHandle.eof())       {
331                         //get next character
332                         char c = fileHandle.get(); 
333                         
334                         //are you at the end of the line
335                         if ((c == '\n') || (c == '\r') || (c == '\f')){  break; }       
336                         else {          line += c;              }
337                 }
338                 
339                 return line;
340                 
341         }
342         catch(exception& e) {
343                 errorOut(e, "MothurOut", "getline");
344                 exit(1);
345         }
346 }
347 /***********************************************************************/
348
349 string MothurOut::getRootName(string longName){
350         try {
351         
352                 string rootName = longName;
353                 
354                 if(longName.find_last_of(".") != longName.npos){
355                         int pos = longName.find_last_of('.')+1;
356                         rootName = longName.substr(0, pos);
357                 }
358
359                 return rootName;
360         }
361         catch(exception& e) {
362                 errorOut(e, "MothurOut", "getRootName");
363                 exit(1);
364         }
365 }
366 /***********************************************************************/
367
368 string MothurOut::getSimpleName(string longName){
369         try {
370                 string simpleName = longName;
371                 
372                 size_t found;
373                 found=longName.find_last_of("/\\");
374
375                 if(found != longName.npos){
376                         simpleName = longName.substr(found+1);
377                 }
378                 
379                 return simpleName;
380         }
381         catch(exception& e) {
382                 errorOut(e, "MothurOut", "getSimpleName");
383                 exit(1);
384         }
385 }
386
387 /***********************************************************************/
388
389 string MothurOut::getPathName(string longName){
390         try {
391                 string rootPathName = longName;
392                 
393                 if(longName.find_last_of("/\\") != longName.npos){
394                         int pos = longName.find_last_of("/\\")+1;
395                         rootPathName = longName.substr(0, pos);
396                 }
397                 
398                 return rootPathName;
399         }
400         catch(exception& e) {
401                 errorOut(e, "MothurOut", "getPathName");
402                 exit(1);
403         }       
404
405 }
406 /***********************************************************************/
407
408 string MothurOut::hasPath(string longName){
409         try {
410                 string path = "";
411                 
412                 size_t found;
413                 found=longName.find_last_of("~/\\");
414
415                 if(found != longName.npos){
416                         path = longName.substr(0, found+1);
417                 }
418                 
419                 return path;
420         }
421         catch(exception& e) {
422                 errorOut(e, "MothurOut", "hasPath");
423                 exit(1);
424         }       
425 }
426
427 /***********************************************************************/
428
429 string MothurOut::getExtension(string longName){
430         try {
431                 string extension = longName;
432                 
433                 if(longName.find_last_of('.') != longName.npos){
434                         int pos = longName.find_last_of('.');
435                         extension = longName.substr(pos, longName.length());
436                 }
437                 
438                 return extension;
439         }
440         catch(exception& e) {
441                 errorOut(e, "MothurOut", "getExtension");
442                 exit(1);
443         }       
444 }
445 /***********************************************************************/
446 bool MothurOut::isBlank(string fileName){
447         try {
448                 ifstream fileHandle;
449                 fileHandle.open(fileName.c_str());
450                 if(!fileHandle) {
451                         mothurOut("[ERROR]: Could not open " + fileName); mothurOutEndLine();
452                         return false;
453                 }else {
454                         //check for blank file
455                         gobble(fileHandle);
456                         if (fileHandle.eof()) { fileHandle.close(); return true;  }
457                 }
458                 return false;
459         }
460         catch(exception& e) {
461                 errorOut(e, "MothurOut", "isBlank");
462                 exit(1);
463         }       
464 }
465 /***********************************************************************/
466
467 string MothurOut::getFullPathName(string fileName){
468         try{
469         
470         string path = hasPath(fileName);
471         string newFileName;
472         int pos;
473         
474         if (path == "") { return fileName; } //its a simple name
475         else { //we need to complete the pathname
476                 // ex. ../../../filename 
477                 // cwd = /user/work/desktop
478                                 
479                 string cwd;
480                 //get current working directory 
481                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)   
482                         
483                         if (path.find("~") != -1) { //go to home directory
484                                 string homeDir = getenv ("HOME");
485                                 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
486                                 return newFileName;
487                         }else { //find path
488                                 if (path.rfind("./") == -1) { return fileName; } //already complete name
489                                 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
490                                 
491                                 char* cwdpath = new char[1024];
492
493                                 size_t size;
494                                 cwdpath=getcwd(cwdpath,size);
495                         
496                                 cwd = cwdpath;
497                                 
498                                 //rip off first '/'
499                                 string simpleCWD;
500                                 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
501                                 
502                                 //break apart the current working directory
503                                 vector<string> dirs;
504                                 while (simpleCWD.find_first_of('/') != -1) {
505                                         string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
506                                         simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
507                                         dirs.push_back(dir);
508                                 }
509                                 //get last one              // ex. ../../../filename = /user/work/desktop/filename
510                                 dirs.push_back(simpleCWD);  //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
511                                 
512                         
513                                 int index = dirs.size()-1;
514                 
515                                 while((pos = path.rfind("./")) != -1) { //while you don't have a complete path
516                                         if (pos == 0) { break;  //you are at the end
517                                         }else if (path[(pos-1)] == '.') { //you want your parent directory ../
518                                                 path = path.substr(0, pos-1);
519                                                 index--;
520                                                 if (index == 0) {  break; }
521                                         }else if (path[(pos-1)] == '/') { //you want the current working dir ./
522                                                 path = path.substr(0, pos);
523                                         }else if (pos == 1) { break;  //you are at the end
524                                         }else { cout << "cannot resolve path for " <<  fileName << endl; return fileName; }
525                                 }
526                         
527                                 for (int i = index; i >= 0; i--) {
528                                         newFileName = dirs[i] +  "/" + newFileName;             
529                                 }
530                                 
531                                 newFileName =  "/" +  newFileName;
532                                 return newFileName;
533                         }       
534                 #else
535                         if (path.find("~") != -1) { //go to home directory
536                                 string homeDir = getenv ("HOMEPATH");
537                                 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
538                                 return newFileName;
539                         }else { //find path
540                                 if (path.rfind(".\\") == -1) { return fileName; } //already complete name
541                                 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
542                                                         
543                                 char *cwdpath = NULL;
544                                 cwdpath = getcwd(NULL, 0); // or _getcwd
545                                 if ( cwdpath != NULL) { cwd = cwdpath; }
546                                 else { cwd = "";  }
547                                 
548                                 //break apart the current working directory
549                                 vector<string> dirs;
550                                 while (cwd.find_first_of('\\') != -1) {
551                                         string dir = cwd.substr(0,cwd.find_first_of('\\'));
552                                         cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
553                                         dirs.push_back(dir);
554                 
555                                 }
556                                 //get last one
557                                 dirs.push_back(cwd);  //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
558                                         
559                                 int index = dirs.size()-1;
560                                         
561                                 while((pos = path.rfind(".\\")) != -1) { //while you don't have a complete path
562                                         if (pos == 0) { break;  //you are at the end
563                                         }else if (path[(pos-1)] == '.') { //you want your parent directory ../
564                                                 path = path.substr(0, pos-1);
565                                                 index--;
566                                                 if (index == 0) {  break; }
567                                         }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
568                                                 path = path.substr(0, pos);
569                                         }else if (pos == 1) { break;  //you are at the end
570                                         }else { cout << "cannot resolve path for " <<  fileName << endl; return fileName; }
571                                 }
572                         
573                                 for (int i = index; i >= 0; i--) {
574                                         newFileName = dirs[i] +  "\\" + newFileName;            
575                                 }
576                                 
577                                 return newFileName;
578                         }
579                         
580                 #endif
581         }
582         }
583         catch(exception& e) {
584                 errorOut(e, "MothurOut", "getFullPathName");
585                 exit(1);
586         }       
587 }
588 /***********************************************************************/
589
590 int MothurOut::openInputFile(string fileName, ifstream& fileHandle, string m){
591         try {
592                         //get full path name
593                         string completeFileName = getFullPathName(fileName);
594
595                         fileHandle.open(completeFileName.c_str());
596                         if(!fileHandle) {
597                                 return 1;
598                         }else {
599                                 //check for blank file
600                                 gobble(fileHandle);
601                                 return 0;
602                         }
603         }
604         catch(exception& e) {
605                 errorOut(e, "MothurOut", "openInputFile - no Error");
606                 exit(1);
607         }
608 }
609 /***********************************************************************/
610
611 int MothurOut::openInputFile(string fileName, ifstream& fileHandle){
612         try {
613                 //get full path name
614                 string completeFileName = getFullPathName(fileName);
615
616                 fileHandle.open(completeFileName.c_str());
617                 if(!fileHandle) {
618                         mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
619                         return 1;
620                 }
621                 else {
622                         //check for blank file
623                         gobble(fileHandle);
624                         if (fileHandle.eof()) { mothurOut("[ERROR]: " + completeFileName + " is blank. Please correct."); mothurOutEndLine();  }
625                         
626                         return 0;
627                 }
628         }
629         catch(exception& e) {
630                 errorOut(e, "MothurOut", "openInputFile");
631                 exit(1);
632         }       
633 }
634 /***********************************************************************/
635
636 int MothurOut::renameFile(string oldName, string newName){
637         try {
638                 ifstream inTest;
639                 int exist = openInputFile(newName, inTest, "");
640                 
641         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)           
642                 if (exist == 0) { //you could open it so you want to delete it
643                         inTest.close();
644                         string command = "rm " + newName;
645                         system(command.c_str());
646                 }
647                                 
648                 string command = "mv " + oldName + " " + newName;
649                 system(command.c_str());
650         #else
651                 remove(newName.c_str());
652                 int renameOk = rename(oldName.c_str(), newName.c_str());
653         #endif
654                 return 0;
655                 
656         }
657         catch(exception& e) {
658                 errorOut(e, "MothurOut", "renameFile");
659                 exit(1);
660         }       
661 }
662
663 /***********************************************************************/
664
665 int MothurOut::openOutputFile(string fileName, ofstream& fileHandle){
666         try { 
667         
668                 string completeFileName = getFullPathName(fileName);
669                 
670                 fileHandle.open(completeFileName.c_str(), ios::trunc);
671                 if(!fileHandle) {
672                         mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
673                         return 1;
674                 }
675                 else {
676                         return 0;
677                 }
678         }
679         catch(exception& e) {
680                 errorOut(e, "MothurOut", "openOutputFile");
681                 exit(1);
682         }       
683
684 }
685
686 /**************************************************************************************************/
687 void MothurOut::appendFiles(string temp, string filename) {
688         try{
689                 ofstream output;
690                 ifstream input;
691         
692                 //open output file in append mode
693                 openOutputFileAppend(filename, output);
694                 int ableToOpen = openInputFile(temp, input, "no error");
695                 
696                 if (ableToOpen == 0) { //you opened it
697                         while(char c = input.get()){
698                                 if(input.eof())         {       break;                  }
699                                 else                            {       output << c;    }
700                         }
701                         input.close();
702                 }
703                 
704                 output.close();
705         }
706         catch(exception& e) {
707                 errorOut(e, "MothurOut", "appendFiles");
708                 exit(1);
709         }       
710 }
711
712 /**************************************************************************************************/
713 string MothurOut::sortFile(string distFile, string outputDir){
714         try {   
715         
716                 //if (outputDir == "") {  outputDir += hasPath(distFile);  }
717                 string outfile = getRootName(distFile) + "sorted.dist";
718
719                 
720                 //if you can, use the unix sort since its been optimized for years
721                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
722                         string command = "sort -n -k +3 " + distFile + " -o " + outfile;
723                         system(command.c_str());
724                 #else //you are stuck with my best attempt...
725                         //windows sort does not have a way to specify a column, only a character in the line
726                         //since we cannot assume that the distance will always be at the the same character location on each line
727                         //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
728                 
729                         //read in file line by file and put distance first
730                         string tempDistFile = distFile + ".temp";
731                         ifstream input;
732                         ofstream output;
733                         openInputFile(distFile, input);
734                         openOutputFile(tempDistFile, output);
735
736                         string firstName, secondName;
737                         float dist;
738                         while (input) {
739                                 input >> firstName >> secondName >> dist;
740                                 output << dist << '\t' << firstName << '\t' << secondName << endl;
741                                 gobble(input);
742                         }
743                         input.close();
744                         output.close();
745                 
746         
747                         //sort using windows sort
748                         string tempOutfile = outfile + ".temp";
749                         string command = "sort " + tempDistFile + " /O " + tempOutfile;
750                         system(command.c_str());
751                 
752                         //read in sorted file and put distance at end again
753                         ifstream input2;
754                         openInputFile(tempOutfile, input2);
755                         openOutputFile(outfile, output);
756                 
757                         while (input2) {
758                                 input2 >> dist >> firstName >> secondName;
759                                 output << firstName << '\t' << secondName << '\t' << dist << endl;
760                                 gobble(input2);
761                         }
762                         input2.close();
763                         output.close();
764                 
765                         //remove temp files
766                         remove(tempDistFile.c_str());
767                         remove(tempOutfile.c_str());
768                 #endif
769                 
770                 return outfile;
771         }
772         catch(exception& e) {
773                 errorOut(e, "MothurOut", "sortFile");
774                 exit(1);
775         }       
776 }
777 /**************************************************************************************************/
778 vector<unsigned long int> MothurOut::setFilePosFasta(string filename, int& num) {
779         try {
780                         vector<unsigned long int> positions;
781                         ifstream inFASTA;
782                         openInputFile(filename, inFASTA);
783                                                 
784                         string input;
785                         while(!inFASTA.eof()){
786                                 input = getline(inFASTA); 
787                                 if (input.length() != 0) {
788                                         if(input[0] == '>'){    unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
789                                 }
790                                 gobble(inFASTA); //has to be here since windows line endings are 2 characters and mess up the positions
791                         }
792                         inFASTA.close();
793                 
794                         num = positions.size();
795                 
796                         /*FILE * pFile;
797                         long size;
798                 
799                         //get num bytes in file
800                         pFile = fopen (filename.c_str(),"rb");
801                         if (pFile==NULL) perror ("Error opening file");
802                         else{
803                                 fseek (pFile, 0, SEEK_END);
804                                 size=ftell (pFile);
805                                 fclose (pFile);
806                         }*/
807                         
808                         unsigned long int size = positions[(positions.size()-1)];
809                         ifstream in;
810                         openInputFile(filename, in);
811                         
812                         in.seekg(size);
813                 
814                         while(char c = in.get()){
815                                 if(in.eof())            {       break;  }
816                                 else                            {       size++; }
817                         }
818                         in.close();
819                 
820                         positions.push_back(size);
821                 
822                         return positions;
823         }
824         catch(exception& e) {
825                 errorOut(e, "MothurOut", "setFilePosFasta");
826                 exit(1);
827         }
828 }
829 /**************************************************************************************************/
830 vector<unsigned long int> MothurOut::setFilePosEachLine(string filename, int& num) {
831         try {
832
833                         vector<unsigned long int> positions;
834                         ifstream in;
835                         openInputFile(filename, in);
836                                 
837                         string input;
838                         while(!in.eof()){
839                                 unsigned long int lastpos = in.tellg();
840                                 input = getline(in); 
841                                 if (input.length() != 0) {
842                                         unsigned long int pos = in.tellg(); 
843                                         if (pos != -1) { positions.push_back(pos - input.length() - 1); }
844                                         else {  positions.push_back(lastpos);  }
845                                 }
846                                 gobble(in); //has to be here since windows line endings are 2 characters and mess up the positions
847                         }
848                         in.close();
849                 
850                         num = positions.size();
851                 
852                         FILE * pFile;
853                         unsigned long int size;
854                 
855                         //get num bytes in file
856                         pFile = fopen (filename.c_str(),"rb");
857                         if (pFile==NULL) perror ("Error opening file");
858                         else{
859                                 fseek (pFile, 0, SEEK_END);
860                                 size=ftell (pFile);
861                                 fclose (pFile);
862                         }
863                 
864                         positions.push_back(size);
865                 
866                         return positions;
867         }
868         catch(exception& e) {
869                 errorOut(e, "MothurOut", "setFilePosEachLine");
870                 exit(1);
871         }
872 }
873 /**************************************************************************************************/
874
875 vector<unsigned long int> MothurOut::divideFile(string filename, int& proc) {
876         try{
877         
878                 vector<unsigned long int> filePos;
879                 filePos.push_back(0);
880                 
881                 FILE * pFile;
882                 unsigned long int size;
883                 
884                 //get num bytes in file
885                 pFile = fopen (filename.c_str(),"rb");
886                 if (pFile==NULL) perror ("Error opening file");
887                 else{
888                         fseek (pFile, 0, SEEK_END);
889                         size=ftell (pFile);
890                         fclose (pFile);
891                 }
892         
893                 //estimate file breaks
894                 unsigned long int chunkSize = 0;
895                 chunkSize = size / proc;
896                 
897                 //file to small to divide by processors
898                 if (chunkSize == 0)  {  proc = 1;       filePos.push_back(size); return filePos;        }
899         
900                 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
901                 for (int i = 0; i < proc; i++) {
902                         unsigned long int spot = (i+1) * chunkSize;
903                         
904                         ifstream in;
905                         openInputFile(filename, in);
906                         in.seekg(spot);
907                         
908                         //look for next '>'
909                         unsigned long int newSpot = spot;
910                         while (!in.eof()) {
911                            char c = in.get();
912                            if (c == '>') {   in.putback(c); newSpot = in.tellg(); break;  }
913                         }
914                         
915                         //there was not another sequence before the end of the file
916                         unsigned long int sanityPos = in.tellg();
917                         if (sanityPos = -1) {   break;  }
918                         else {   filePos.push_back(newSpot);  }
919                         
920                         in.close();
921                 }
922                 
923                 //save end pos
924                 filePos.push_back(size);
925                 
926                 //sanity check filePos
927                 for (int i = 0; i < (filePos.size()-1); i++) {
928                         if (filePos[(i+1)] <= filePos[i]) {  filePos.erase(filePos.begin()+(i+1)); i--; }
929                 }
930
931                 proc = (filePos.size() - 1);
932                 
933                 return filePos;
934         }
935         catch(exception& e) {
936                 errorOut(e, "MothurOut", "divideFile");
937                 exit(1);
938         }
939 }
940
941 /***********************************************************************/
942
943 bool MothurOut::isTrue(string f){
944         try {
945                 
946                 for (int i = 0; i < f.length(); i++) { f[i] = toupper(f[i]); }
947                 
948                 if ((f == "TRUE") || (f == "T")) {      return true;    }
949                 else {  return false;  }
950         }
951         catch(exception& e) {
952                 errorOut(e, "MothurOut", "isTrue");
953                 exit(1);
954         }
955 }
956
957 /***********************************************************************/
958
959 float MothurOut::roundDist(float dist, int precision){
960         try {
961                 return int(dist * precision + 0.5)/float(precision);
962         }
963         catch(exception& e) {
964                 errorOut(e, "MothurOut", "roundDist");
965                 exit(1);
966         }
967 }
968 /***********************************************************************/
969
970 float MothurOut::ceilDist(float dist, int precision){
971         try {
972                 return int(ceil(dist * precision))/float(precision);
973         }
974         catch(exception& e) {
975                 errorOut(e, "MothurOut", "ceilDist");
976                 exit(1);
977         }
978 }
979
980 /***********************************************************************/
981
982 int MothurOut::getNumNames(string names){
983         try {
984                 int count = 0;
985                 
986                 if(names != ""){
987                         count = 1;
988                         for(int i=0;i<names.size();i++){
989                                 if(names[i] == ','){
990                                         count++;
991                                 }
992                         }
993                 }
994                 
995                 return count;
996         }
997         catch(exception& e) {
998                 errorOut(e, "MothurOut", "getNumNames");
999                 exit(1);
1000         }
1001 }
1002
1003 /**************************************************************************************************/
1004
1005 vector<vector<double> > MothurOut::binomial(int maxOrder){
1006         try {
1007         vector<vector<double> > binomial(maxOrder+1);
1008         
1009     for(int i=0;i<=maxOrder;i++){
1010                 binomial[i].resize(maxOrder+1);
1011                 binomial[i][0]=1;
1012                 binomial[0][i]=0;
1013     }
1014     binomial[0][0]=1;
1015         
1016     binomial[1][0]=1;
1017     binomial[1][1]=1;
1018         
1019     for(int i=2;i<=maxOrder;i++){
1020                 binomial[1][i]=0;
1021     }
1022         
1023     for(int i=2;i<=maxOrder;i++){
1024                 for(int j=1;j<=maxOrder;j++){
1025                         if(i==j){       binomial[i][j]=1;                                                                       }
1026                         if(j>i) {       binomial[i][j]=0;                                                                       }
1027                         else    {       binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j];     }
1028                 }
1029     }
1030         
1031         return binomial;
1032         
1033         }
1034         catch(exception& e) {
1035                 errorOut(e, "MothurOut", "binomial");
1036                 exit(1);
1037         }
1038 }
1039
1040 /***********************************************************************/
1041
1042 int MothurOut::factorial(int num){
1043         try {
1044                 int total = 1;
1045                 
1046                 for (int i = 1; i <= num; i++) {
1047                         total *= i;
1048                 }
1049                 
1050                 return total;
1051         }
1052         catch(exception& e) {
1053                 errorOut(e, "MothurOut", "factorial");
1054                 exit(1);
1055         }
1056 }
1057 /***********************************************************************/
1058
1059 int MothurOut::getNumSeqs(ifstream& file){
1060         try {
1061                 int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
1062                 file.seekg(0);
1063                 return numSeqs;
1064         }
1065         catch(exception& e) {
1066                 errorOut(e, "MothurOut", "getNumSeqs");
1067                 exit(1);
1068         }       
1069 }
1070 /***********************************************************************/
1071 void MothurOut::getNumSeqs(ifstream& file, int& numSeqs){
1072         try {
1073                 string input;
1074                 numSeqs = 0;
1075                 while(!file.eof()){
1076                         input = getline(file);
1077                         if (input.length() != 0) {
1078                                 if(input[0] == '>'){ numSeqs++; }
1079                         }
1080                 }
1081         }
1082         catch(exception& e) {
1083                 errorOut(e, "MothurOut", "getNumSeqs");
1084                 exit(1);
1085         }       
1086 }
1087 /***********************************************************************/
1088
1089 //This function parses the estimator options and puts them in a vector
1090 void MothurOut::splitAtChar(string& estim, vector<string>& container, char symbol) {
1091         try {
1092                 string individual;
1093                 
1094                 while (estim.find_first_of(symbol) != -1) {
1095                         individual = estim.substr(0,estim.find_first_of(symbol));
1096                         if ((estim.find_first_of(symbol)+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1097                                 estim = estim.substr(estim.find_first_of(symbol)+1, estim.length());
1098                                 container.push_back(individual);
1099                         }
1100                 }
1101                 //get last one
1102                 container.push_back(estim);
1103         }
1104         catch(exception& e) {
1105                 errorOut(e, "MothurOut", "splitAtChar");
1106                 exit(1);
1107         }       
1108 }
1109
1110 /***********************************************************************/
1111
1112 //This function parses the estimator options and puts them in a vector
1113 void MothurOut::splitAtDash(string& estim, vector<string>& container) {
1114         try {
1115                 string individual;
1116                 
1117                 while (estim.find_first_of('-') != -1) {
1118                         individual = estim.substr(0,estim.find_first_of('-'));
1119                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1120                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1121                                 container.push_back(individual);
1122                         }
1123                 }
1124                 //get last one
1125                 container.push_back(estim);
1126         }
1127         catch(exception& e) {
1128                 errorOut(e, "MothurOut", "splitAtDash");
1129                 exit(1);
1130         }       
1131 }
1132
1133 /***********************************************************************/
1134 //This function parses the label options and puts them in a set
1135 void MothurOut::splitAtDash(string& estim, set<string>& container) {
1136         try {
1137                 string individual;
1138                 
1139                 while (estim.find_first_of('-') != -1) {
1140                         individual = estim.substr(0,estim.find_first_of('-'));
1141                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1142                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1143                                 container.insert(individual);
1144                         }
1145                 }
1146                 //get last one
1147                 container.insert(estim);
1148         }
1149         catch(exception& e) {
1150                 errorOut(e, "MothurOut", "splitAtDash");
1151                 exit(1);
1152         }       
1153 }
1154 /***********************************************************************/
1155 //This function parses the line options and puts them in a set
1156 void MothurOut::splitAtDash(string& estim, set<int>& container) {
1157         try {
1158                 string individual;
1159                 int lineNum;
1160                 
1161                 while (estim.find_first_of('-') != -1) {
1162                         individual = estim.substr(0,estim.find_first_of('-'));
1163                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
1164                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
1165                                 convert(individual, lineNum); //convert the string to int
1166                                 container.insert(lineNum);
1167                         }
1168                 }
1169                 //get last one
1170                 convert(estim, lineNum); //convert the string to int
1171                 container.insert(lineNum);
1172         }
1173         catch(exception& e) {
1174                 errorOut(e, "MothurOut", "splitAtDash");
1175                 exit(1);
1176         }       
1177 }
1178 /***********************************************************************/
1179 //This function parses the a string and puts peices in a vector
1180 void MothurOut::splitAtComma(string& estim, vector<string>& container) {
1181         try {
1182                 string individual;
1183                 
1184                 while (estim.find_first_of(',') != -1) {
1185                         individual = estim.substr(0,estim.find_first_of(','));
1186                         if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
1187                                 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
1188                                 container.push_back(individual);
1189                         }
1190                 }
1191                 //get last one
1192                 container.push_back(estim);
1193         }
1194         catch(exception& e) {
1195                 errorOut(e, "MothurOut", "splitAtComma");
1196                 exit(1);
1197         }       
1198 }
1199 /***********************************************************************/
1200
1201 //This function splits up the various option parameters
1202 void MothurOut::splitAtComma(string& prefix, string& suffix){
1203         try {
1204                 prefix = suffix.substr(0,suffix.find_first_of(','));
1205                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
1206                         suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
1207                         string space = " ";
1208                         while(suffix.at(0) == ' ')
1209                                 suffix = suffix.substr(1, suffix.length());
1210                 }
1211
1212         }
1213         catch(exception& e) {
1214                 errorOut(e, "MothurOut", "splitAtComma");
1215                 exit(1);
1216         }       
1217 }
1218 /***********************************************************************/
1219
1220 //This function separates the key value from the option value i.e. dist=96_...
1221 void MothurOut::splitAtEquals(string& key, string& value){              
1222         try {
1223                 if(value.find_first_of('=') != -1){
1224                         key = value.substr(0,value.find_first_of('='));
1225                         if ((value.find_first_of('=')+1) <= value.length()) {
1226                                 value = value.substr(value.find_first_of('=')+1, value.length());
1227                         }
1228                 }else{
1229                         key = value;
1230                         value = 1;
1231                 }
1232         }
1233         catch(exception& e) {
1234                 errorOut(e, "MothurOut", "splitAtEquals");
1235                 exit(1);
1236         }       
1237 }
1238
1239 /**************************************************************************************************/
1240
1241 bool MothurOut::inUsersGroups(string groupname, vector<string> Groups) {
1242         try {
1243                 for (int i = 0; i < Groups.size(); i++) {
1244                         if (groupname == Groups[i]) { return true; }
1245                 }
1246                 return false;
1247         }
1248         catch(exception& e) {
1249                 errorOut(e, "MothurOut", "inUsersGroups");
1250                 exit(1);
1251         }       
1252 }
1253 /**************************************************************************************************/
1254 //returns true if any of the strings in first vector are in second vector
1255 bool MothurOut::inUsersGroups(vector<string> groupnames, vector<string> Groups) {
1256         try {
1257                 
1258                 for (int i = 0; i < groupnames.size(); i++) {
1259                         if (inUsersGroups(groupnames[i], Groups)) { return true; }
1260                 }
1261                 return false;
1262         }
1263         catch(exception& e) {
1264                 errorOut(e, "MothurOut", "inUsersGroups");
1265                 exit(1);
1266         }       
1267 }
1268 /***********************************************************************/
1269 //this function determines if the user has given us labels that are smaller than the given label.
1270 //if so then it returns true so that the calling function can run the previous valid distance.
1271 //it's a "smart" distance function.  It also checks for invalid labels.
1272 bool MothurOut::anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
1273         try {
1274                 
1275                 set<string>::iterator it;
1276                 vector<float> orderFloat;
1277                 map<string, float> userMap;  //the conversion process removes trailing 0's which we need to put back
1278                 map<string, float>::iterator it2;
1279                 float labelFloat;
1280                 bool smaller = false;
1281                 
1282                 //unique is the smallest line
1283                 if (label == "unique") {  return false;  }
1284                 else { 
1285                         if (convertTestFloat(label, labelFloat)) {
1286                                 convert(label, labelFloat); 
1287                         }else { //cant convert 
1288                                 return false;
1289                         }
1290                 }
1291                 
1292                 //go through users set and make them floats
1293                 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
1294                         
1295                         float temp;
1296                         if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
1297                                 convert(*it, temp);
1298                                 orderFloat.push_back(temp);
1299                                 userMap[*it] = temp;
1300                         }else if (*it == "unique") { 
1301                                 orderFloat.push_back(-1.0);
1302                                 userMap["unique"] = -1.0;
1303                         }else {
1304                                 if (errorOff == "") {  cout << *it << " is not a valid label." << endl;  }
1305                                 userLabels.erase(*it); 
1306                                 it--;
1307                         }
1308                 }
1309                 
1310                 //sort order
1311                 sort(orderFloat.begin(), orderFloat.end());
1312                 
1313                 /*************************************************/
1314                 //is this label bigger than any of the users labels
1315                 /*************************************************/
1316                                 
1317                 //loop through order until you find a label greater than label
1318                 for (int i = 0; i < orderFloat.size(); i++) {
1319                         if (orderFloat[i] < labelFloat) {
1320                                 smaller = true;
1321                                 if (orderFloat[i] == -1) { 
1322                                         if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
1323                                         userLabels.erase("unique");
1324                                 }
1325                                 else {  
1326                                         if (errorOff == "") { cout << "Your file does not include the label " << endl; }
1327                                         string s = "";
1328                                         for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {  
1329                                                 if (it2->second == orderFloat[i]) {  
1330                                                         s = it2->first;  
1331                                                         //remove small labels
1332                                                         userLabels.erase(s);
1333                                                         break;
1334                                                 }
1335                                         }
1336                                         if (errorOff == "") {cout << s <<  ". I will use the next smallest distance. " << endl; }
1337                                 }
1338                         //since they are sorted once you find a bigger one stop looking
1339                         }else { break; }
1340                 }
1341                 
1342                 return smaller;
1343                                                 
1344         }
1345         catch(exception& e) {
1346                 errorOut(e, "MothurOut", "anyLabelsToProcess");
1347                 exit(1);
1348         }       
1349 }
1350
1351 /**************************************************************************************************/
1352 bool MothurOut::checkReleaseVersion(ifstream& file, string version) {
1353         try {
1354                 
1355                 bool good = true;
1356                 
1357                 string line = getline(file);  
1358
1359                 //before we added this check
1360                 if (line[0] != '#') {  good = false;  }
1361                 else {
1362                         //rip off #
1363                         line = line.substr(1);
1364                         
1365                         vector<string> versionVector;
1366                         splitAtChar(version, versionVector, '.');
1367                         
1368                         //check file version
1369                         vector<string> linesVector;
1370                         splitAtChar(line, linesVector, '.');
1371                         
1372                         if (versionVector.size() != linesVector.size()) { good = false; }
1373                         else {
1374                                 for (int j = 0; j < versionVector.size(); j++) {
1375                                         int num1, num2;
1376                                         convert(versionVector[j], num1);
1377                                         convert(linesVector[j], num2);
1378                                         
1379                                         //if mothurs version is newer than this files version, then we want to remake it
1380                                         if (num1 > num2) {  good = false; break;  }
1381                                 }
1382                         }
1383                         
1384                 }
1385                 
1386                 if (!good) {  file.close();  }
1387                 else { file.seekg(0);  }
1388                 
1389                 return good;
1390         }
1391         catch(exception& e) {
1392                 errorOut(e, "MothurOut", "checkReleaseVersion");                
1393                 exit(1);
1394         }
1395 }
1396 /**************************************************************************************************/
1397
1398
1399
1400
1401