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