]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
added versioning info to all shortcut files mothur makes.
[mothur.git] / mothur.h
1 #ifndef MOTHUR_H
2 #define MOTHUR_H
3
4
5
6 /*
7  *  mothur.h
8  *  Mothur
9  *
10  *  Created by Sarah Westcott on 2/19/09.
11  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
12  *
13  */
14
15 /* This file contains all the standard incudes we use in the project as well as some common utilities. */
16
17 //#include <cstddef>
18
19 //io libraries
20 #include <iostream>
21 #include <iomanip>
22 #include <fstream>
23 #include <sstream>
24 #include <signal.h>
25
26
27 //exception
28 #include <stdexcept>
29 #include <exception>
30 #include <cstdlib> 
31
32
33 //containers
34 #include <vector>
35 #include <set>
36 #include <map>
37 #include <string>
38 #include <list>
39
40 //math
41 #include <cmath>
42 #include <math.h>
43 #include <algorithm>
44
45 //misc
46 #include <cerrno>
47 #include <ctime>
48 #include <limits>
49
50 #ifdef USE_MPI
51         #include "mpi.h"
52 #endif
53 /***********************************************************************/
54
55 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
56         #include <sys/wait.h>
57         #include <sys/time.h>
58         #include <sys/resource.h>
59         #include <unistd.h>
60         
61         #ifdef USE_READLINE
62                 #include <readline/readline.h>
63                 #include <readline/history.h>
64         #endif
65
66 #else
67         #include <conio.h> //allows unbuffered screen capture from stdin
68         #include <direct.h> //get cwd
69         #include <windows.h>
70         #include <psapi.h>
71
72 #endif
73
74 using namespace std;
75
76 #define exp(x) (exp((double) x))
77 #define sqrt(x) (sqrt((double) x))
78 #define log10(x) (log10((double) x))
79 #define log2(x) (log10(x)/log10(2))
80 #define isnan(x) ((x) != (x))
81 #define isinf(x) (fabs(x) == std::numeric_limits<double>::infinity())
82
83 typedef unsigned long ull;
84
85 struct IntNode {
86         int lvalue;
87         int rvalue;
88         int lcoef;
89         int rcoef;
90         IntNode* left;
91         IntNode* right;
92         
93         IntNode(int lv, int rv, IntNode* l, IntNode* r) : lvalue(lv), rvalue(rv), left(l), right(r) {};
94         IntNode() {};
95 };
96
97 struct ThreadNode {
98         int* pid;
99         IntNode* left;
100         IntNode* right;
101 };
102
103 /************************************************************/
104 struct clusterNode {
105         int numSeq;
106         int parent;
107         int smallChild; //used to make linkTable work with list and rabund. represents bin number of this cluster node
108         clusterNode(int num, int par, int kid) : numSeq(num), parent(par), smallChild(kid) {};
109 };
110 /************************************************************/
111 struct seqDist {
112         int seq1;
113         int seq2;
114         float dist;
115         seqDist() {}
116         seqDist(int s1, int s2, float d) : seq1(s1), seq2(s2), dist(d) {}
117         ~seqDist() {}
118 };
119 //********************************************************************************************************************
120 //sorts lowest to highest
121 inline bool compareSequenceDistance(seqDist left, seqDist right){
122         return (left.dist < right.dist);        
123
124 /***********************************************************************/
125
126 // snagged from http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
127 // works for now, but there should be a way to do it without killing the whole program
128
129 class BadConversion : public runtime_error {
130 public:
131         BadConversion(const string& s) : runtime_error(s){ }
132 };
133
134 //**********************************************************************************************************************
135
136 template<typename T>
137 inline void convert(const string& s, T& x, bool failIfLeftoverChars = true){
138         istringstream i(s);
139         char c;
140         if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
141                 throw BadConversion(s);
142 }
143
144 //**********************************************************************************************************************
145
146 template<typename T>
147 inline bool convertTestFloat(const string& s, T& x, bool failIfLeftoverChars = true){
148         istringstream i(s);
149         char c;
150         if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
151         {
152                 return false;
153         } 
154         return true;
155 }
156
157 //**********************************************************************************************************************
158
159 template<typename T>
160 inline bool convertTest(const string& s, T& x, bool failIfLeftoverChars = true){
161         istringstream i(s);
162         char c;
163         if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
164         {
165                 cout << "unable to be converted into an integer.\n" << endl;
166                 return false;
167         } 
168         return true;
169 }
170
171 //**********************************************************************************************************************
172
173 template<typename T>
174 string toString(const T&x){
175     stringstream output;
176     output << x;
177     return output.str();
178 }
179
180 //**********************************************************************************************************************
181
182 template<typename T>
183 string toHex(const T&x){
184         stringstream output;
185         
186         output << hex << x;
187
188     return output.str();
189 }
190 //**********************************************************************************************************************
191
192 template<typename T>
193 string toString(const T&x, int i){
194         stringstream output;
195         
196         output.precision(i);
197     output << fixed << x;
198         
199     return output.str();
200 }
201 /***********************************************************************/
202 inline int openOutputFileAppend(string fileName, ofstream& fileHandle){
203         
204         fileHandle.open(fileName.c_str(), ios::app);
205         if(!fileHandle) {
206                 cout << "Error: Could not open " << fileName << endl;
207                 return 1;
208         }
209         else {
210                 return 0;
211         }
212
213 }
214 /***********************************************************************/
215
216 inline void gobble(istream& f){
217         
218         char d;
219     while(isspace(d=f.get()))           {;}
220         f.putback(d);
221         
222 }
223 /***********************************************************************/
224
225 inline string getline(istringstream& fileHandle) {
226         try {
227         
228                 string line = "";
229                 
230                 while (!fileHandle.eof())       {
231                         //get next character
232                         char c = fileHandle.get(); 
233                         
234                         //are you at the end of the line
235                         if ((c == '\n') || (c == '\r') || (c == '\f')){  break; }       
236                         else {          line += c;              }
237                 }
238                 
239                 return line;
240                 
241         }
242         catch(exception& e) {
243                 cout << "Error in mothur function getline" << endl;
244                 exit(1);
245         }
246 }
247 /***********************************************************************/
248
249 inline string getline(ifstream& fileHandle) {
250         try {
251         
252                 string line = "";
253                 
254                 while (!fileHandle.eof())       {
255                         //get next character
256                         char c = fileHandle.get(); 
257                         
258                         //are you at the end of the line
259                         if ((c == '\n') || (c == '\r') || (c == '\f')){  break; }       
260                         else {          line += c;              }
261                 }
262                 
263                 return line;
264                 
265         }
266         catch(exception& e) {
267                 cout << "Error in mothur function getline" << endl;
268                 exit(1);
269         }
270 }
271 /***********************************************************************/
272
273 inline bool isTrue(string f){
274         
275         if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) {       return true;    }
276         else {  return false;  }
277 }
278
279 /***********************************************************************/
280
281 inline float roundDist(float dist, int precision){
282         
283         return int(dist * precision + 0.5)/float(precision);
284         
285 }
286 /***********************************************************************/
287
288 inline float ceilDist(float dist, int precision){
289         
290         return int(ceil(dist * precision))/float(precision);
291         
292 }
293
294 /***********************************************************************/
295
296 inline int getNumNames(string names){
297         
298         int count = 0;
299         
300         if(names != ""){
301                 count = 1;
302                 for(int i=0;i<names.size();i++){
303                         if(names[i] == ','){
304                                 count++;
305                         }
306                 }
307         }
308         
309         return count;
310         
311 }
312
313 /**************************************************************************************************/
314
315 inline vector<vector<double> > binomial(int maxOrder){
316         
317         vector<vector<double> > binomial(maxOrder+1);
318         
319     for(int i=0;i<=maxOrder;i++){
320                 binomial[i].resize(maxOrder+1);
321                 binomial[i][0]=1;
322                 binomial[0][i]=0;
323     }
324     binomial[0][0]=1;
325         
326     binomial[1][0]=1;
327     binomial[1][1]=1;
328         
329     for(int i=2;i<=maxOrder;i++){
330                 binomial[1][i]=0;
331     }
332         
333     for(int i=2;i<=maxOrder;i++){
334                 for(int j=1;j<=maxOrder;j++){
335                         if(i==j){       binomial[i][j]=1;                                                                       }
336                         if(j>i) {       binomial[i][j]=0;                                                                       }
337                         else    {       binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j];     }
338                 }
339     }
340         
341         return binomial;
342 }
343
344 /***********************************************************************/
345
346 inline string getRootName(string longName){
347  
348         string rootName = longName;
349         
350         if(longName.find_last_of(".") != longName.npos){
351                 int pos = longName.find_last_of('.')+1;
352                 rootName = longName.substr(0, pos);
353         }
354
355         return rootName;
356 }
357 /***********************************************************************/
358
359 inline string getSimpleName(string longName){
360  
361         string simpleName = longName;
362         
363         size_t found;
364         found=longName.find_last_of("/\\");
365
366         if(found != longName.npos){
367                 simpleName = longName.substr(found+1);
368         }
369         
370                 //if(longName.find_last_of("/") != longName.npos){
371                 //      int pos = longName.find_last_of('/')+1;
372                 //      simpleName = longName.substr(pos, longName.length());
373                 //}
374         
375         return simpleName;
376 }
377
378 /***********************************************************************/
379
380 inline int factorial(int num){
381         int total = 1;
382         
383         for (int i = 1; i <= num; i++) {
384                 total *= i;
385         }
386         
387         return total;
388 }
389 /**************************************************************************************************
390
391 double min(double x, double y)
392 {
393     if(x<y){    return x;    }
394     else   {    return y;    }
395 }
396
397 /***********************************************************************/
398
399 inline string getPathName(string longName){
400  
401         string rootPathName = longName;
402         
403         if(longName.find_last_of("/\\") != longName.npos){
404                 int pos = longName.find_last_of("/\\")+1;
405                 rootPathName = longName.substr(0, pos);
406         }
407         
408         return rootPathName;
409 }
410 /***********************************************************************/
411
412 inline string hasPath(string longName){
413         
414         string path = "";
415         
416         size_t found;
417         found=longName.find_last_of("~/\\");
418
419         if(found != longName.npos){
420                 path = longName.substr(0, found+1);
421         }
422         
423         return path;
424 }
425
426 /***********************************************************************/
427
428 inline string getExtension(string longName){
429         
430         string extension = longName;
431         
432         if(longName.find_last_of('.') != longName.npos){
433                 int pos = longName.find_last_of('.');
434                 extension = longName.substr(pos, longName.length());
435         }
436         
437         return extension;
438 }
439 /***********************************************************************/
440 inline bool isBlank(string fileName){
441         
442         ifstream fileHandle;
443         fileHandle.open(fileName.c_str());
444         if(!fileHandle) {
445                 cout << "Error: Could not open " << fileName << endl;
446                 return false;
447         }else {
448                 //check for blank file
449                 gobble(fileHandle);
450                 if (fileHandle.eof()) { fileHandle.close(); return true;  }
451         }
452         return false;
453 }
454 /***********************************************************************/
455
456 inline string getFullPathName(string fileName){
457         try{
458         
459         string path = hasPath(fileName);
460         string newFileName;
461         int pos;
462         
463         if (path == "") { return fileName; } //its a simple name
464         else { //we need to complete the pathname
465                 // ex. ../../../filename 
466                 // cwd = /user/work/desktop
467                                 
468                 string cwd;
469                 //get current working directory 
470                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)   
471                         
472                         if (path.find("~") != -1) { //go to home directory
473                                 string homeDir = getenv ("HOME");
474                                 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
475                                 return newFileName;
476                         }else { //find path
477                                 if (path.rfind("./") == -1) { return fileName; } //already complete name
478                                 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
479                                 
480                                 char* cwdpath = new char[1024];
481
482                                 size_t size;
483                                 cwdpath=getcwd(cwdpath,size);
484                         
485                                 cwd = cwdpath;
486                                 
487                                 //rip off first '/'
488                                 string simpleCWD;
489                                 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
490                                 
491                                 //break apart the current working directory
492                                 vector<string> dirs;
493                                 while (simpleCWD.find_first_of('/') != -1) {
494                                         string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
495                                         simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
496                                         dirs.push_back(dir);
497                                 }
498                                 //get last one              // ex. ../../../filename = /user/work/desktop/filename
499                                 dirs.push_back(simpleCWD);  //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
500                                 
501                         
502                                 int index = dirs.size()-1;
503                 
504                                 while((pos = path.rfind("./")) != -1) { //while you don't have a complete path
505                                         if (pos == 0) { break;  //you are at the end
506                                         }else if (path[(pos-1)] == '.') { //you want your parent directory ../
507                                                 path = path.substr(0, pos-1);
508                                                 index--;
509                                                 if (index == 0) {  break; }
510                                         }else if (path[(pos-1)] == '/') { //you want the current working dir ./
511                                                 path = path.substr(0, pos);
512                                         }else if (pos == 1) { break;  //you are at the end
513                                         }else { cout << "cannot resolve path for " <<  fileName << endl; return fileName; }
514                                 }
515                         
516                                 for (int i = index; i >= 0; i--) {
517                                         newFileName = dirs[i] +  "/" + newFileName;             
518                                 }
519                                 
520                                 newFileName =  "/" +  newFileName;
521                                 return newFileName;
522                         }       
523                 #else
524                         if (path.find("~") != -1) { //go to home directory
525                                 string homeDir = getenv ("HOMEPATH");
526                                 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
527                                 return newFileName;
528                         }else { //find path
529                                 if (path.rfind(".\\") == -1) { return fileName; } //already complete name
530                                 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
531                                                         
532                                 char *cwdpath = NULL;
533                                 cwdpath = getcwd(NULL, 0); // or _getcwd
534                                 if ( cwdpath != NULL) { cwd = cwdpath; }
535                                 else { cwd = "";  }
536                                 
537                                 //break apart the current working directory
538                                 vector<string> dirs;
539                                 while (cwd.find_first_of('\\') != -1) {
540                                         string dir = cwd.substr(0,cwd.find_first_of('\\'));
541                                         cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
542                                         dirs.push_back(dir);
543                 
544                                 }
545                                 //get last one
546                                 dirs.push_back(cwd);  //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
547                                         
548                                 int index = dirs.size()-1;
549                                         
550                                 while((pos = path.rfind(".\\")) != -1) { //while you don't have a complete path
551                                         if (pos == 0) { break;  //you are at the end
552                                         }else if (path[(pos-1)] == '.') { //you want your parent directory ../
553                                                 path = path.substr(0, pos-1);
554                                                 index--;
555                                                 if (index == 0) {  break; }
556                                         }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
557                                                 path = path.substr(0, pos);
558                                         }else if (pos == 1) { break;  //you are at the end
559                                         }else { cout << "cannot resolve path for " <<  fileName << endl; return fileName; }
560                                 }
561                         
562                                 for (int i = index; i >= 0; i--) {
563                                         newFileName = dirs[i] +  "\\" + newFileName;            
564                                 }
565                                 
566                                 return newFileName;
567                         }
568                         
569                 #endif
570         }
571         }
572         catch(exception& e) {
573                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function getFullPathName. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
574                 exit(1);
575         }       
576 }
577 /***********************************************************************/
578
579 inline int openInputFile(string fileName, ifstream& fileHandle, string m){
580         
581         //get full path name
582         string completeFileName = getFullPathName(fileName);
583
584         fileHandle.open(completeFileName.c_str());
585         if(!fileHandle) {
586                 return 1;
587         }else {
588                 //check for blank file
589                 gobble(fileHandle);
590                 return 0;
591         }       
592 }
593 /***********************************************************************/
594
595 inline int openInputFile(string fileName, ifstream& fileHandle){
596         
597         //get full path name
598         string completeFileName = getFullPathName(fileName);
599
600         fileHandle.open(completeFileName.c_str());
601         if(!fileHandle) {
602                 cout << "Error: Could not open " << completeFileName << endl;
603                 return 1;
604         }
605         else {
606                 //check for blank file
607                 gobble(fileHandle);
608                 if (fileHandle.eof()) { cout << completeFileName << " is blank. Please correct." << endl;   }
609                 
610                 return 0;
611         }
612         
613 }
614 /***********************************************************************/
615
616 inline int renameFile(string oldName, string newName){
617         
618         ifstream inTest;
619         int exist = openInputFile(newName, inTest, "");
620         
621 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)           
622         if (exist == 0) { //you could open it so you want to delete it
623                 inTest.close();
624                 string command = "rm " + newName;
625                 system(command.c_str());
626         }
627                         
628         string command = "mv " + oldName + " " + newName;
629         system(command.c_str());
630 #else
631         remove(newName.c_str());
632         int renameOk = rename(oldName.c_str(), newName.c_str());
633 #endif
634         return 0;
635 }
636
637 /***********************************************************************/
638
639 inline int openOutputFile(string fileName, ofstream& fileHandle){
640         
641         string completeFileName = getFullPathName(fileName);
642         
643         fileHandle.open(completeFileName.c_str(), ios::trunc);
644         if(!fileHandle) {
645                 cout << "Error: Could not open " << completeFileName << endl;
646                 return 1;
647         }
648         else {
649                 return 0;
650         }
651
652 }
653
654 /***********************************************************************/
655
656 inline int getNumSeqs(ifstream& file){
657         
658         int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
659         file.seekg(0);
660         return numSeqs;
661
662 }
663 /***********************************************************************/
664 inline void getNumSeqs(ifstream& file, int& numSeqs){
665         
666         string input;
667         numSeqs = 0;
668         while(!file.eof()){
669                 input = getline(file);
670                 if (input.length() != 0) {
671                         if(input[0] == '>'){ numSeqs++; }
672                 }
673         }
674 }
675
676 /***********************************************************************/
677
678 inline bool inVector(string member, vector<string> group){
679         
680         for (int i = 0; i < group.size(); i++) {
681                 if (group[i] == member) {  return true;         }
682         }
683         
684         return false;
685 }
686 /***********************************************************************/
687
688 //This function parses the estimator options and puts them in a vector
689 inline void splitAtChar(string& estim, vector<string>& container, char symbol) {
690         try {
691                 string individual;
692                 
693                 while (estim.find_first_of(symbol) != -1) {
694                         individual = estim.substr(0,estim.find_first_of(symbol));
695                         if ((estim.find_first_of(symbol)+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
696                                 estim = estim.substr(estim.find_first_of(symbol)+1, estim.length());
697                                 container.push_back(individual);
698                         }
699                 }
700                 //get last one
701                 container.push_back(estim);
702         }
703         catch(exception& e) {
704                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
705                 exit(1);
706         }       
707 }
708
709 /***********************************************************************/
710
711 //This function parses the estimator options and puts them in a vector
712 inline void splitAtDash(string& estim, vector<string>& container) {
713         try {
714                 string individual;
715                 
716                 while (estim.find_first_of('-') != -1) {
717                         individual = estim.substr(0,estim.find_first_of('-'));
718                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
719                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
720                                 container.push_back(individual);
721                         }
722                 }
723                 //get last one
724                 container.push_back(estim);
725         }
726         catch(exception& e) {
727                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
728                 exit(1);
729         }       
730 }
731
732 /***********************************************************************/
733 //This function parses the label options and puts them in a set
734 inline void splitAtDash(string& estim, set<string>& container) {
735         try {
736                 string individual;
737                 
738                 while (estim.find_first_of('-') != -1) {
739                         individual = estim.substr(0,estim.find_first_of('-'));
740                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
741                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
742                                 container.insert(individual);
743                         }
744                 }
745                 //get last one
746                 container.insert(estim);
747         }
748         catch(exception& e) {
749                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
750                 exit(1);
751         }       
752 }
753 /***********************************************************************/
754 //This function parses the line options and puts them in a set
755 inline void splitAtDash(string& estim, set<int>& container) {
756         try {
757                 string individual;
758                 int lineNum;
759                 
760                 while (estim.find_first_of('-') != -1) {
761                         individual = estim.substr(0,estim.find_first_of('-'));
762                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
763                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
764                                 convert(individual, lineNum); //convert the string to int
765                                 container.insert(lineNum);
766                         }
767                 }
768                 //get last one
769                 convert(estim, lineNum); //convert the string to int
770                 container.insert(lineNum);
771         }
772         catch(exception& e) {
773                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
774                 exit(1);
775         }       
776 }
777 /***********************************************************************/
778 //This function parses the a string and puts peices in a vector
779 inline void splitAtComma(string& estim, vector<string>& container) {
780         try {
781                 string individual;
782                 
783                 while (estim.find_first_of(',') != -1) {
784                         individual = estim.substr(0,estim.find_first_of(','));
785                         if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
786                                 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
787                                 container.push_back(individual);
788                         }
789                 }
790                 //get last one
791                 container.push_back(estim);
792         }
793         catch(exception& e) {
794                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtComma. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
795                 exit(1);
796         }       
797 }
798 /***********************************************************************/
799
800 //This function splits up the various option parameters
801 inline void splitAtComma(string& prefix, string& suffix){
802         try {
803                 prefix = suffix.substr(0,suffix.find_first_of(','));
804                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
805                         suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
806                         string space = " ";
807                         while(suffix.at(0) == ' ')
808                                 suffix = suffix.substr(1, suffix.length());
809                 }
810
811         }
812         catch(exception& e) {
813                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtComma. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
814                 exit(1);
815         }       
816 }
817 /***********************************************************************/
818
819 //This function separates the key value from the option value i.e. dist=96_...
820 inline void splitAtEquals(string& key, string& value){          
821         try {
822                 if(value.find_first_of('=') != -1){
823                         key = value.substr(0,value.find_first_of('='));
824                         if ((value.find_first_of('=')+1) <= value.length()) {
825                                 value = value.substr(value.find_first_of('=')+1, value.length());
826                         }
827                 }else{
828                         key = value;
829                         value = 1;
830                 }
831         }
832         catch(exception& e) {
833                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtEquals. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
834                 exit(1);
835         }       
836 }
837
838 /**************************************************************************************************/
839
840 inline bool inUsersGroups(string groupname, vector<string> Groups) {
841         try {
842                 for (int i = 0; i < Groups.size(); i++) {
843                         if (groupname == Groups[i]) { return true; }
844                 }
845                 return false;
846         }
847         catch(exception& e) {
848                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function inUsersGroups. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
849                 exit(1);
850         }       
851 }
852 /**************************************************************************************************/
853 //returns true if any of the strings in first vector are in second vector
854 inline bool inUsersGroups(vector<string> groupnames, vector<string> Groups) {
855         try {
856                 
857                 for (int i = 0; i < groupnames.size(); i++) {
858                         if (inUsersGroups(groupnames[i], Groups)) { return true; }
859                 }
860                 return false;
861         }
862         catch(exception& e) {
863                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function inUsersGroups. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
864                 exit(1);
865         }       
866 }
867 /***********************************************************************/
868 //this function determines if the user has given us labels that are smaller than the given label.
869 //if so then it returns true so that the calling function can run the previous valid distance.
870 //it's a "smart" distance function.  It also checks for invalid labels.
871 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
872         try {
873                 
874                 set<string>::iterator it;
875                 vector<float> orderFloat;
876                 map<string, float> userMap;  //the conversion process removes trailing 0's which we need to put back
877                 map<string, float>::iterator it2;
878                 float labelFloat;
879                 bool smaller = false;
880                 
881                 //unique is the smallest line
882                 if (label == "unique") {  return false;  }
883                 else { 
884                         if (convertTestFloat(label, labelFloat)) {
885                                 convert(label, labelFloat); 
886                         }else { //cant convert 
887                                 return false;
888                         }
889                 }
890                 
891                 //go through users set and make them floats
892                 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
893                         
894                         float temp;
895                         if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
896                                 convert(*it, temp);
897                                 orderFloat.push_back(temp);
898                                 userMap[*it] = temp;
899                         }else if (*it == "unique") { 
900                                 orderFloat.push_back(-1.0);
901                                 userMap["unique"] = -1.0;
902                         }else {
903                                 if (errorOff == "") {  cout << *it << " is not a valid label." << endl;  }
904                                 userLabels.erase(*it); 
905                                 it--;
906                         }
907                 }
908                 
909                 //sort order
910                 sort(orderFloat.begin(), orderFloat.end());
911                 
912                 /*************************************************/
913                 //is this label bigger than any of the users labels
914                 /*************************************************/
915                                 
916                 //loop through order until you find a label greater than label
917                 for (int i = 0; i < orderFloat.size(); i++) {
918                         if (orderFloat[i] < labelFloat) {
919                                 smaller = true;
920                                 if (orderFloat[i] == -1) { 
921                                         if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
922                                         userLabels.erase("unique");
923                                 }
924                                 else {  
925                                         if (errorOff == "") { cout << "Your file does not include the label " << endl; }
926                                         string s = "";
927                                         for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {  
928                                                 if (it2->second == orderFloat[i]) {  
929                                                         s = it2->first;  
930                                                         //remove small labels
931                                                         userLabels.erase(s);
932                                                         break;
933                                                 }
934                                         }
935                                         if (errorOff == "") {cout << s <<  ". I will use the next smallest distance. " << endl; }
936                                 }
937                         //since they are sorted once you find a bigger one stop looking
938                         }else { break; }
939                 }
940                 
941                 return smaller;
942                                                 
943         }
944         catch(exception& e) {
945                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function anyLabelsToProcess. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
946                 exit(1);
947         }       
948 }
949
950 /**************************************************************************************************/
951 inline void appendFiles(string temp, string filename) {
952         try{
953                 ofstream output;
954                 ifstream input;
955         
956                 //open output file in append mode
957                 openOutputFileAppend(filename, output);
958                 int ableToOpen = openInputFile(temp, input, "no error");
959                 
960                 if (ableToOpen == 0) { //you opened it
961                         while(char c = input.get()){
962                                 if(input.eof())         {       break;                  }
963                                 else                            {       output << c;    }
964                         }
965                         input.close();
966                 }
967                 
968                 output.close();
969         }
970         catch(exception& e) {
971                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function appendFiles. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
972                 exit(1);
973         }       
974 }
975
976 /**************************************************************************************************/
977 inline string sortFile(string distFile, string outputDir){
978         try {   
979         
980                 //if (outputDir == "") {  outputDir += hasPath(distFile);  }
981                 string outfile = getRootName(distFile) + "sorted.dist";
982
983                 
984                 //if you can, use the unix sort since its been optimized for years
985                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
986                         string command = "sort -n -k +3 " + distFile + " -o " + outfile;
987                         system(command.c_str());
988                 #else //you are stuck with my best attempt...
989                         //windows sort does not have a way to specify a column, only a character in the line
990                         //since we cannot assume that the distance will always be at the the same character location on each line
991                         //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
992                 
993                         //read in file line by file and put distance first
994                         string tempDistFile = distFile + ".temp";
995                         ifstream input;
996                         ofstream output;
997                         openInputFile(distFile, input);
998                         openOutputFile(tempDistFile, output);
999
1000                         string firstName, secondName;
1001                         float dist;
1002                         while (input) {
1003                                 input >> firstName >> secondName >> dist;
1004                                 output << dist << '\t' << firstName << '\t' << secondName << endl;
1005                                 gobble(input);
1006                         }
1007                         input.close();
1008                         output.close();
1009                 
1010         
1011                         //sort using windows sort
1012                         string tempOutfile = outfile + ".temp";
1013                         string command = "sort " + tempDistFile + " /O " + tempOutfile;
1014                         system(command.c_str());
1015                 
1016                         //read in sorted file and put distance at end again
1017                         ifstream input2;
1018                         openInputFile(tempOutfile, input2);
1019                         openOutputFile(outfile, output);
1020                 
1021                         while (input2) {
1022                                 input2 >> dist >> firstName >> secondName;
1023                                 output << firstName << '\t' << secondName << '\t' << dist << endl;
1024                                 gobble(input2);
1025                         }
1026                         input2.close();
1027                         output.close();
1028                 
1029                         //remove temp files
1030                         remove(tempDistFile.c_str());
1031                         remove(tempOutfile.c_str());
1032                 #endif
1033                 
1034                 return outfile;
1035         }
1036         catch(exception& e) {
1037                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function sortfile. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
1038                 exit(1);
1039         }       
1040 }
1041 /**************************************************************************************************/
1042 inline vector<unsigned long int> setFilePosFasta(string filename, int& num) {
1043
1044                         vector<unsigned long int> positions;
1045                         ifstream inFASTA;
1046                         openInputFile(filename, inFASTA);
1047                                 
1048                         string input;
1049                         while(!inFASTA.eof()){
1050                                 input = getline(inFASTA); gobble(inFASTA);
1051                                 if (input.length() != 0) {
1052                                         if(input[0] == '>'){    unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
1053                                 }
1054                         }
1055                         inFASTA.close();
1056                 
1057                         num = positions.size();
1058                 
1059                         /*FILE * pFile;
1060                         long size;
1061                 
1062                         //get num bytes in file
1063                         pFile = fopen (filename.c_str(),"rb");
1064                         if (pFile==NULL) perror ("Error opening file");
1065                         else{
1066                                 fseek (pFile, 0, SEEK_END);
1067                                 size=ftell (pFile);
1068                                 fclose (pFile);
1069                         }*/
1070                         
1071                         unsigned long int size = positions[(positions.size()-1)];
1072                         ifstream in;
1073                         openInputFile(filename, in);
1074                         
1075                         in.seekg(size);
1076                 
1077                         while(char c = in.get()){
1078                                 if(in.eof())            {       break;  }
1079                                 else                            {       size++; }
1080                         }
1081                         in.close();
1082                 
1083                         positions.push_back(size);
1084                 
1085                         return positions;
1086 }
1087 /**************************************************************************************************/
1088 inline vector<unsigned long int> setFilePosEachLine(string filename, int& num) {
1089
1090                         vector<unsigned long int> positions;
1091                         ifstream in;
1092                         openInputFile(filename, in);
1093                                 
1094                         string input;
1095                         while(!in.eof()){
1096                                 unsigned long int lastpos = in.tellg();
1097                                 input = getline(in); gobble(in);
1098                                 if (input.length() != 0) {
1099                                         unsigned long int pos = in.tellg(); 
1100                                         if (pos != -1) { positions.push_back(pos - input.length() - 1); }
1101                                         else {  positions.push_back(lastpos);  }
1102                                 }
1103                         }
1104                         in.close();
1105                 
1106                         num = positions.size();
1107                 
1108                         FILE * pFile;
1109                         unsigned long int size;
1110                 
1111                         //get num bytes in file
1112                         pFile = fopen (filename.c_str(),"rb");
1113                         if (pFile==NULL) perror ("Error opening file");
1114                         else{
1115                                 fseek (pFile, 0, SEEK_END);
1116                                 size=ftell (pFile);
1117                                 fclose (pFile);
1118                         }
1119                 
1120                         positions.push_back(size);
1121                 
1122                         return positions;
1123 }
1124 /**************************************************************************************************/
1125 inline bool checkReleaseVersion(ifstream& file, string version) {
1126         try {
1127                 
1128                 bool good = true;
1129                 
1130                 string line = getline(file);  
1131
1132                 //before we added this check
1133                 if (line[0] != '#') {  good = false;  }
1134                 else {
1135                         //rip off #
1136                         line = line.substr(1);
1137                         
1138                         vector<string> versionVector;
1139                         splitAtChar(version, versionVector, '.');
1140                         
1141                         //check file version
1142                         vector<string> linesVector;
1143                         splitAtChar(line, linesVector, '.');
1144                         
1145                         if (versionVector.size() != linesVector.size()) { good = false; }
1146                         else {
1147                                 for (int j = 0; j < versionVector.size(); j++) {
1148                                         int num1, num2;
1149                                         convert(versionVector[j], num1);
1150                                         convert(linesVector[j], num2);
1151                                         
1152                                         //if mothurs version is newer than this files version, then we want to remake it
1153                                         if (num1 > num2) {  good = false; break;  }
1154                                 }
1155                         }
1156                         
1157                 }
1158                 
1159                 if (!good) {  file.close();  }
1160                 else { file.seekg(0);  }
1161                 
1162                 return good;
1163         }
1164         catch(exception& e) {
1165                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function checkReleaseVersion. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
1166                 exit(1);
1167         }
1168 }
1169 /**************************************************************************************************/
1170 #endif
1171