]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
trim.seqs couts
[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(ifstream& 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
250 inline bool isTrue(string f){
251         
252         if ((f == "TRUE") || (f == "T") || (f == "true") || (f == "t")) {       return true;    }
253         else {  return false;  }
254 }
255
256 /***********************************************************************/
257
258 inline float roundDist(float dist, int precision){
259         
260         return int(dist * precision + 0.5)/float(precision);
261         
262 }
263 /***********************************************************************/
264
265 inline float ceilDist(float dist, int precision){
266         
267         return int(ceil(dist * precision))/float(precision);
268         
269 }
270
271 /***********************************************************************/
272
273 inline int getNumNames(string names){
274         
275         int count = 0;
276         
277         if(names != ""){
278                 count = 1;
279                 for(int i=0;i<names.size();i++){
280                         if(names[i] == ','){
281                                 count++;
282                         }
283                 }
284         }
285         
286         return count;
287         
288 }
289
290 /**************************************************************************************************/
291
292 inline vector<vector<double> > binomial(int maxOrder){
293         
294         vector<vector<double> > binomial(maxOrder+1);
295         
296     for(int i=0;i<=maxOrder;i++){
297                 binomial[i].resize(maxOrder+1);
298                 binomial[i][0]=1;
299                 binomial[0][i]=0;
300     }
301     binomial[0][0]=1;
302         
303     binomial[1][0]=1;
304     binomial[1][1]=1;
305         
306     for(int i=2;i<=maxOrder;i++){
307                 binomial[1][i]=0;
308     }
309         
310     for(int i=2;i<=maxOrder;i++){
311                 for(int j=1;j<=maxOrder;j++){
312                         if(i==j){       binomial[i][j]=1;                                                                       }
313                         if(j>i) {       binomial[i][j]=0;                                                                       }
314                         else    {       binomial[i][j]=binomial[i-1][j-1]+binomial[i-1][j];     }
315                 }
316     }
317         
318         return binomial;
319 }
320
321 /***********************************************************************/
322
323 inline string getRootName(string longName){
324  
325         string rootName = longName;
326         
327         if(longName.find_last_of(".") != longName.npos){
328                 int pos = longName.find_last_of('.')+1;
329                 rootName = longName.substr(0, pos);
330         }
331
332         return rootName;
333 }
334 /***********************************************************************/
335
336 inline string getSimpleName(string longName){
337  
338         string simpleName = longName;
339         
340         size_t found;
341         found=longName.find_last_of("/\\");
342
343         if(found != longName.npos){
344                 simpleName = longName.substr(found+1);
345         }
346         
347                 //if(longName.find_last_of("/") != longName.npos){
348                 //      int pos = longName.find_last_of('/')+1;
349                 //      simpleName = longName.substr(pos, longName.length());
350                 //}
351         
352         return simpleName;
353 }
354
355 /***********************************************************************/
356
357 inline int factorial(int num){
358         int total = 1;
359         
360         for (int i = 1; i <= num; i++) {
361                 total *= i;
362         }
363         
364         return total;
365 }
366 /**************************************************************************************************
367
368 double min(double x, double y)
369 {
370     if(x<y){    return x;    }
371     else   {    return y;    }
372 }
373
374 /***********************************************************************/
375
376 inline string getPathName(string longName){
377  
378         string rootPathName = longName;
379         
380         if(longName.find_last_of("/\\") != longName.npos){
381                 int pos = longName.find_last_of("/\\")+1;
382                 rootPathName = longName.substr(0, pos);
383         }
384         
385         return rootPathName;
386 }
387 /***********************************************************************/
388
389 inline string hasPath(string longName){
390         
391         string path = "";
392         
393         size_t found;
394         found=longName.find_last_of("~/\\");
395
396         if(found != longName.npos){
397                 path = longName.substr(0, found+1);
398         }
399         
400         return path;
401 }
402
403 /***********************************************************************/
404
405 inline string getExtension(string longName){
406         
407         string extension = longName;
408         
409         if(longName.find_last_of('.') != longName.npos){
410                 int pos = longName.find_last_of('.');
411                 extension = longName.substr(pos, longName.length());
412         }
413         
414         return extension;
415 }
416 /***********************************************************************/
417 inline bool isBlank(string fileName){
418         
419         ifstream fileHandle;
420         fileHandle.open(fileName.c_str());
421         if(!fileHandle) {
422                 cout << "Error: Could not open " << fileName << endl;
423                 return false;
424         }else {
425                 //check for blank file
426                 gobble(fileHandle);
427                 if (fileHandle.eof()) { fileHandle.close(); return true;  }
428         }
429         return false;
430 }
431 /***********************************************************************/
432
433 inline string getFullPathName(string fileName){
434         try{
435         
436         string path = hasPath(fileName);
437         string newFileName;
438         int pos;
439         
440         if (path == "") { return fileName; } //its a simple name
441         else { //we need to complete the pathname
442                 // ex. ../../../filename 
443                 // cwd = /user/work/desktop
444                                 
445                 string cwd;
446                 //get current working directory 
447                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)   
448                         
449                         if (path.find("~") != -1) { //go to home directory
450                                 string homeDir = getenv ("HOME");
451                                 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
452                                 return newFileName;
453                         }else { //find path
454                                 if (path.rfind("./") == -1) { return fileName; } //already complete name
455                                 else { newFileName = fileName.substr(fileName.rfind("./")+2); } //save the complete part of the name
456                                 
457                                 char* cwdpath = new char[1024];
458
459                                 size_t size;
460                                 cwdpath=getcwd(cwdpath,size);
461                         
462                                 cwd = cwdpath;
463                                 
464                                 //rip off first '/'
465                                 string simpleCWD;
466                                 if (cwd.length() > 0) { simpleCWD = cwd.substr(1); }
467                                 
468                                 //break apart the current working directory
469                                 vector<string> dirs;
470                                 while (simpleCWD.find_first_of('/') != -1) {
471                                         string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/'));
472                                         simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length());
473                                         dirs.push_back(dir);
474                                 }
475                                 //get last one              // ex. ../../../filename = /user/work/desktop/filename
476                                 dirs.push_back(simpleCWD);  //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
477                                 
478                         
479                                 int index = dirs.size()-1;
480                 
481                                 while((pos = path.rfind("./")) != -1) { //while you don't have a complete path
482                                         if (pos == 0) { break;  //you are at the end
483                                         }else if (path[(pos-1)] == '.') { //you want your parent directory ../
484                                                 path = path.substr(0, pos-1);
485                                                 index--;
486                                                 if (index == 0) {  break; }
487                                         }else if (path[(pos-1)] == '/') { //you want the current working dir ./
488                                                 path = path.substr(0, pos);
489                                         }else if (pos == 1) { break;  //you are at the end
490                                         }else { cout << "cannot resolve path for " <<  fileName << endl; return fileName; }
491                                 }
492                         
493                                 for (int i = index; i >= 0; i--) {
494                                         newFileName = dirs[i] +  "/" + newFileName;             
495                                 }
496                                 
497                                 newFileName =  "/" +  newFileName;
498                                 return newFileName;
499                         }       
500                 #else
501                         if (path.find("~") != -1) { //go to home directory
502                                 string homeDir = getenv ("HOMEPATH");
503                                 newFileName = homeDir + fileName.substr(fileName.find("~")+1);
504                                 return newFileName;
505                         }else { //find path
506                                 if (path.rfind(".\\") == -1) { return fileName; } //already complete name
507                                 else { newFileName = fileName.substr(fileName.rfind(".\\")+2); } //save the complete part of the name
508                                                         
509                                 char *cwdpath = NULL;
510                                 cwdpath = getcwd(NULL, 0); // or _getcwd
511                                 if ( cwdpath != NULL) { cwd = cwdpath; }
512                                 else { cwd = "";  }
513                                 
514                                 //break apart the current working directory
515                                 vector<string> dirs;
516                                 while (cwd.find_first_of('\\') != -1) {
517                                         string dir = cwd.substr(0,cwd.find_first_of('\\'));
518                                         cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length());
519                                         dirs.push_back(dir);
520                 
521                                 }
522                                 //get last one
523                                 dirs.push_back(cwd);  //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop
524                                         
525                                 int index = dirs.size()-1;
526                                         
527                                 while((pos = path.rfind(".\\")) != -1) { //while you don't have a complete path
528                                         if (pos == 0) { break;  //you are at the end
529                                         }else if (path[(pos-1)] == '.') { //you want your parent directory ../
530                                                 path = path.substr(0, pos-1);
531                                                 index--;
532                                                 if (index == 0) {  break; }
533                                         }else if (path[(pos-1)] == '\\') { //you want the current working dir ./
534                                                 path = path.substr(0, pos);
535                                         }else if (pos == 1) { break;  //you are at the end
536                                         }else { cout << "cannot resolve path for " <<  fileName << endl; return fileName; }
537                                 }
538                         
539                                 for (int i = index; i >= 0; i--) {
540                                         newFileName = dirs[i] +  "\\" + newFileName;            
541                                 }
542                                 
543                                 return newFileName;
544                         }
545                         
546                 #endif
547         }
548         }
549         catch(exception& e) {
550                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function getFullPathName. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
551                 exit(1);
552         }       
553 }
554 /***********************************************************************/
555
556 inline int openInputFile(string fileName, ifstream& fileHandle, string m){
557         
558         //get full path name
559         string completeFileName = getFullPathName(fileName);
560
561         fileHandle.open(completeFileName.c_str());
562         if(!fileHandle) {
563                 return 1;
564         }else {
565                 //check for blank file
566                 gobble(fileHandle);
567                 return 0;
568         }       
569 }
570 /***********************************************************************/
571
572 inline int openInputFile(string fileName, ifstream& fileHandle){
573         
574         //get full path name
575         string completeFileName = getFullPathName(fileName);
576
577         fileHandle.open(completeFileName.c_str());
578         if(!fileHandle) {
579                 cout << "Error: Could not open " << completeFileName << endl;
580                 return 1;
581         }
582         else {
583                 //check for blank file
584                 gobble(fileHandle);
585                 if (fileHandle.eof()) { cout << completeFileName << " is blank. Please correct." << endl;   }
586                 
587                 return 0;
588         }
589         
590 }
591 /***********************************************************************/
592
593 inline int renameFile(string oldName, string newName){
594         
595         ifstream inTest;
596         int exist = openInputFile(newName, inTest, "");
597         
598 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)           
599         if (exist == 0) { //you could open it so you want to delete it
600                 inTest.close();
601                 string command = "rm " + newName;
602                 system(command.c_str());
603         }
604                         
605         string command = "mv " + oldName + " " + newName;
606         system(command.c_str());
607 #else
608         remove(newName.c_str());
609         int renameOk = rename(oldName.c_str(), newName.c_str());
610 #endif
611         return 0;
612 }
613
614 /***********************************************************************/
615
616 inline int openOutputFile(string fileName, ofstream& fileHandle){
617         
618         string completeFileName = getFullPathName(fileName);
619         
620         fileHandle.open(completeFileName.c_str(), ios::trunc);
621         if(!fileHandle) {
622                 cout << "Error: Could not open " << completeFileName << endl;
623                 return 1;
624         }
625         else {
626                 return 0;
627         }
628
629 }
630
631 /***********************************************************************/
632
633 inline int getNumSeqs(ifstream& file){
634         
635         int numSeqs = count(istreambuf_iterator<char>(file),istreambuf_iterator<char>(), '>');
636         file.seekg(0);
637         return numSeqs;
638
639 }
640 /***********************************************************************/
641 inline void getNumSeqs(ifstream& file, int& numSeqs){
642         
643         string input;
644         numSeqs = 0;
645         while(!file.eof()){
646                 input = getline(file);
647                 if (input.length() != 0) {
648                         if(input[0] == '>'){ numSeqs++; }
649                 }
650         }
651 }
652
653 /***********************************************************************/
654
655 inline bool inVector(string member, vector<string> group){
656         
657         for (int i = 0; i < group.size(); i++) {
658                 if (group[i] == member) {  return true;         }
659         }
660         
661         return false;
662 }
663 /***********************************************************************/
664
665 //This function parses the estimator options and puts them in a vector
666 inline void splitAtDash(string& estim, vector<string>& container) {
667         try {
668                 string individual;
669                 
670                 while (estim.find_first_of('-') != -1) {
671                         individual = estim.substr(0,estim.find_first_of('-'));
672                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
673                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
674                                 container.push_back(individual);
675                         }
676                 }
677                 //get last one
678                 container.push_back(estim);
679         }
680         catch(exception& e) {
681                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
682                 exit(1);
683         }       
684 }
685
686 /***********************************************************************/
687 //This function parses the label options and puts them in a set
688 inline void splitAtDash(string& estim, set<string>& container) {
689         try {
690                 string individual;
691                 
692                 while (estim.find_first_of('-') != -1) {
693                         individual = estim.substr(0,estim.find_first_of('-'));
694                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
695                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
696                                 container.insert(individual);
697                         }
698                 }
699                 //get last one
700                 container.insert(estim);
701         }
702         catch(exception& e) {
703                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
704                 exit(1);
705         }       
706 }
707 /***********************************************************************/
708 //This function parses the line options and puts them in a set
709 inline void splitAtDash(string& estim, set<int>& container) {
710         try {
711                 string individual;
712                 int lineNum;
713                 
714                 while (estim.find_first_of('-') != -1) {
715                         individual = estim.substr(0,estim.find_first_of('-'));
716                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
717                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
718                                 convert(individual, lineNum); //convert the string to int
719                                 container.insert(lineNum);
720                         }
721                 }
722                 //get last one
723                 convert(estim, lineNum); //convert the string to int
724                 container.insert(lineNum);
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 //This function parses the a string and puts peices in a vector
733 inline void splitAtComma(string& estim, vector<string>& container) {
734         try {
735                 string individual;
736                 
737                 while (estim.find_first_of(',') != -1) {
738                         individual = estim.substr(0,estim.find_first_of(','));
739                         if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
740                                 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
741                                 container.push_back(individual);
742                         }
743                 }
744                 //get last one
745                 container.push_back(estim);
746         }
747         catch(exception& e) {
748                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtComma. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
749                 exit(1);
750         }       
751 }
752 /***********************************************************************/
753
754 //This function splits up the various option parameters
755 inline void splitAtComma(string& prefix, string& suffix){
756         try {
757                 prefix = suffix.substr(0,suffix.find_first_of(','));
758                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
759                         suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
760                         string space = " ";
761                         while(suffix.at(0) == ' ')
762                                 suffix = suffix.substr(1, suffix.length());
763                 }
764
765         }
766         catch(exception& e) {
767                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtComma. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
768                 exit(1);
769         }       
770 }
771 /***********************************************************************/
772
773 //This function separates the key value from the option value i.e. dist=96_...
774 inline void splitAtEquals(string& key, string& value){          
775         try {
776                 if(value.find_first_of('=') != -1){
777                         key = value.substr(0,value.find_first_of('='));
778                         if ((value.find_first_of('=')+1) <= value.length()) {
779                                 value = value.substr(value.find_first_of('=')+1, value.length());
780                         }
781                 }else{
782                         key = value;
783                         value = 1;
784                 }
785         }
786         catch(exception& e) {
787                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtEquals. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
788                 exit(1);
789         }       
790 }
791 /**************************************************************************************************/
792
793 inline bool inUsersGroups(string groupname, vector<string> Groups) {
794         try {
795                 for (int i = 0; i < Groups.size(); i++) {
796                         if (groupname == Groups[i]) { return true; }
797                 }
798                 return false;
799         }
800         catch(exception& e) {
801                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function inUsersGroups. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
802                 exit(1);
803         }       
804 }
805 /**************************************************************************************************/
806 //returns true if any of the strings in first vector are in second vector
807 inline bool inUsersGroups(vector<string> groupnames, vector<string> Groups) {
808         try {
809                 
810                 for (int i = 0; i < groupnames.size(); i++) {
811                         if (inUsersGroups(groupnames[i], Groups)) { return true; }
812                 }
813                 return false;
814         }
815         catch(exception& e) {
816                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function inUsersGroups. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
817                 exit(1);
818         }       
819 }
820 /***********************************************************************/
821 //this function determines if the user has given us labels that are smaller than the given label.
822 //if so then it returns true so that the calling function can run the previous valid distance.
823 //it's a "smart" distance function.  It also checks for invalid labels.
824 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
825         try {
826                 
827                 set<string>::iterator it;
828                 vector<float> orderFloat;
829                 map<string, float> userMap;  //the conversion process removes trailing 0's which we need to put back
830                 map<string, float>::iterator it2;
831                 float labelFloat;
832                 bool smaller = false;
833                 
834                 //unique is the smallest line
835                 if (label == "unique") {  return false;  }
836                 else { 
837                         if (convertTestFloat(label, labelFloat)) {
838                                 convert(label, labelFloat); 
839                         }else { //cant convert 
840                                 return false;
841                         }
842                 }
843                 
844                 //go through users set and make them floats
845                 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
846                         
847                         float temp;
848                         if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
849                                 convert(*it, temp);
850                                 orderFloat.push_back(temp);
851                                 userMap[*it] = temp;
852                         }else if (*it == "unique") { 
853                                 orderFloat.push_back(-1.0);
854                                 userMap["unique"] = -1.0;
855                         }else {
856                                 if (errorOff == "") {  cout << *it << " is not a valid label." << endl;  }
857                                 userLabels.erase(*it); 
858                                 it--;
859                         }
860                 }
861                 
862                 //sort order
863                 sort(orderFloat.begin(), orderFloat.end());
864                 
865                 /*************************************************/
866                 //is this label bigger than any of the users labels
867                 /*************************************************/
868                                 
869                 //loop through order until you find a label greater than label
870                 for (int i = 0; i < orderFloat.size(); i++) {
871                         if (orderFloat[i] < labelFloat) {
872                                 smaller = true;
873                                 if (orderFloat[i] == -1) { 
874                                         if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
875                                         userLabels.erase("unique");
876                                 }
877                                 else {  
878                                         if (errorOff == "") { cout << "Your file does not include the label " << endl; }
879                                         string s = "";
880                                         for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {  
881                                                 if (it2->second == orderFloat[i]) {  
882                                                         s = it2->first;  
883                                                         //remove small labels
884                                                         userLabels.erase(s);
885                                                         break;
886                                                 }
887                                         }
888                                         if (errorOff == "") {cout << s <<  ". I will use the next smallest distance. " << endl; }
889                                 }
890                         //since they are sorted once you find a bigger one stop looking
891                         }else { break; }
892                 }
893                 
894                 return smaller;
895                                                 
896         }
897         catch(exception& e) {
898                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function anyLabelsToProcess. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
899                 exit(1);
900         }       
901 }
902
903 /**************************************************************************************************/
904 inline void appendFiles(string temp, string filename) {
905         try{
906                 ofstream output;
907                 ifstream input;
908         
909                 //open output file in append mode
910                 openOutputFileAppend(filename, output);
911                 int ableToOpen = openInputFile(temp, input, "no error");
912                 
913                 if (ableToOpen == 0) { //you opened it
914                         while(char c = input.get()){
915                                 if(input.eof())         {       break;                  }
916                                 else                            {       output << c;    }
917                         }
918                         input.close();
919                 }
920                 
921                 output.close();
922         }
923         catch(exception& e) {
924                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function appendFiles. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
925                 exit(1);
926         }       
927 }
928
929 /**************************************************************************************************/
930 inline string sortFile(string distFile, string outputDir){
931         try {   
932         
933                 //if (outputDir == "") {  outputDir += hasPath(distFile);  }
934                 string outfile = getRootName(distFile) + "sorted.dist";
935
936                 
937                 //if you can, use the unix sort since its been optimized for years
938                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
939                         string command = "sort -n -k +3 " + distFile + " -o " + outfile;
940                         system(command.c_str());
941                 #else //you are stuck with my best attempt...
942                         //windows sort does not have a way to specify a column, only a character in the line
943                         //since we cannot assume that the distance will always be at the the same character location on each line
944                         //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
945                 
946                         //read in file line by file and put distance first
947                         string tempDistFile = distFile + ".temp";
948                         ifstream input;
949                         ofstream output;
950                         openInputFile(distFile, input);
951                         openOutputFile(tempDistFile, output);
952
953                         string firstName, secondName;
954                         float dist;
955                         while (input) {
956                                 input >> firstName >> secondName >> dist;
957                                 output << dist << '\t' << firstName << '\t' << secondName << endl;
958                                 gobble(input);
959                         }
960                         input.close();
961                         output.close();
962                 
963         
964                         //sort using windows sort
965                         string tempOutfile = outfile + ".temp";
966                         string command = "sort " + tempDistFile + " /O " + tempOutfile;
967                         system(command.c_str());
968                 
969                         //read in sorted file and put distance at end again
970                         ifstream input2;
971                         openInputFile(tempOutfile, input2);
972                         openOutputFile(outfile, output);
973                 
974                         while (input2) {
975                                 input2 >> dist >> firstName >> secondName;
976                                 output << firstName << '\t' << secondName << '\t' << dist << endl;
977                                 gobble(input2);
978                         }
979                         input2.close();
980                         output.close();
981                 
982                         //remove temp files
983                         remove(tempDistFile.c_str());
984                         remove(tempOutfile.c_str());
985                 #endif
986                 
987                 return outfile;
988         }
989         catch(exception& e) {
990                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function sortfile. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
991                 exit(1);
992         }       
993 }
994 /**************************************************************************************************/
995 inline vector<long> setFilePosFasta(string filename, int& num) {
996
997                         vector<long> positions;
998                         ifstream inFASTA;
999                         openInputFile(filename, inFASTA);
1000                                 
1001                         string input;
1002                         while(!inFASTA.eof()){
1003                                 input = getline(inFASTA); gobble(inFASTA);
1004                                 if (input.length() != 0) {
1005                                         if(input[0] == '>'){    long pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1);      }
1006                                 }
1007                         }
1008                         inFASTA.close();
1009                 
1010                         num = positions.size();
1011                 
1012                         /*FILE * pFile;
1013                         long size;
1014                 
1015                         //get num bytes in file
1016                         pFile = fopen (filename.c_str(),"rb");
1017                         if (pFile==NULL) perror ("Error opening file");
1018                         else{
1019                                 fseek (pFile, 0, SEEK_END);
1020                                 size=ftell (pFile);
1021                                 fclose (pFile);
1022                         }*/
1023                         
1024                         long size = positions[(positions.size()-1)];
1025                         ifstream in;
1026                         openInputFile(filename, in);
1027                         
1028                         in.seekg(size);
1029                 
1030                         while(char c = in.get()){
1031                                 if(in.eof())            {       break;  }
1032                                 else                            {       size++; }
1033                         }
1034                         in.close();
1035                 
1036                         positions.push_back(size);
1037                 
1038                         return positions;
1039 }
1040 /**************************************************************************************************/
1041 inline vector<long> setFilePosEachLine(string filename, int& num) {
1042
1043                         vector<long> positions;
1044                         ifstream in;
1045                         openInputFile(filename, in);
1046                                 
1047                         string input;
1048                         while(!in.eof()){
1049                                 long lastpos = in.tellg();
1050                                 input = getline(in); gobble(in);
1051                                 if (input.length() != 0) {
1052                                         long pos = in.tellg(); 
1053                                         if (pos != -1) { positions.push_back(pos - input.length() - 1); }
1054                                         else {  positions.push_back(lastpos);  }
1055                                 }
1056                         }
1057                         in.close();
1058                 
1059                         num = positions.size();
1060                 
1061                         FILE * pFile;
1062                         long size;
1063                 
1064                         //get num bytes in file
1065                         pFile = fopen (filename.c_str(),"rb");
1066                         if (pFile==NULL) perror ("Error opening file");
1067                         else{
1068                                 fseek (pFile, 0, SEEK_END);
1069                                 size=ftell (pFile);
1070                                 fclose (pFile);
1071                         }
1072                 
1073                         positions.push_back(size);
1074                 
1075                         return positions;
1076 }
1077
1078 /**************************************************************************************************/
1079 #endif
1080