]> git.donarmstrong.com Git - mothur.git/blob - mothur.h
modified the engine to extract the path of mothur executable if argv does not contain...
[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 splitAtChar(string& estim, vector<string>& container, char symbol) {
667         try {
668                 string individual;
669                 
670                 while (estim.find_first_of(symbol) != -1) {
671                         individual = estim.substr(0,estim.find_first_of(symbol));
672                         if ((estim.find_first_of(symbol)+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
673                                 estim = estim.substr(estim.find_first_of(symbol)+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
688 //This function parses the estimator options and puts them in a vector
689 inline void splitAtDash(string& estim, vector<string>& container) {
690         try {
691                 string individual;
692                 
693                 while (estim.find_first_of('-') != -1) {
694                         individual = estim.substr(0,estim.find_first_of('-'));
695                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
696                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
697                                 container.push_back(individual);
698                         }
699                 }
700                 //get last one
701                 container.push_back(estim);
702         }
703         catch(exception& e) {
704                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
705                 exit(1);
706         }       
707 }
708
709 /***********************************************************************/
710 //This function parses the label options and puts them in a set
711 inline void splitAtDash(string& estim, set<string>& container) {
712         try {
713                 string individual;
714                 
715                 while (estim.find_first_of('-') != -1) {
716                         individual = estim.substr(0,estim.find_first_of('-'));
717                         if ((estim.find_first_of('-')+1) <= estim.length()) { //checks to make sure you don't have dash at end of string
718                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
719                                 container.insert(individual);
720                         }
721                 }
722                 //get last one
723                 container.insert(estim);
724         }
725         catch(exception& e) {
726                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
727                 exit(1);
728         }       
729 }
730 /***********************************************************************/
731 //This function parses the line options and puts them in a set
732 inline void splitAtDash(string& estim, set<int>& container) {
733         try {
734                 string individual;
735                 int lineNum;
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 dash at end of string
740                                 estim = estim.substr(estim.find_first_of('-')+1, estim.length());
741                                 convert(individual, lineNum); //convert the string to int
742                                 container.insert(lineNum);
743                         }
744                 }
745                 //get last one
746                 convert(estim, lineNum); //convert the string to int
747                 container.insert(lineNum);
748         }
749         catch(exception& e) {
750                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtDash. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
751                 exit(1);
752         }       
753 }
754 /***********************************************************************/
755 //This function parses the a string and puts peices in a vector
756 inline void splitAtComma(string& estim, vector<string>& container) {
757         try {
758                 string individual;
759                 
760                 while (estim.find_first_of(',') != -1) {
761                         individual = estim.substr(0,estim.find_first_of(','));
762                         if ((estim.find_first_of(',')+1) <= estim.length()) { //checks to make sure you don't have comma at end of string
763                                 estim = estim.substr(estim.find_first_of(',')+1, estim.length());
764                                 container.push_back(individual);
765                         }
766                 }
767                 //get last one
768                 container.push_back(estim);
769         }
770         catch(exception& e) {
771                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtComma. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
772                 exit(1);
773         }       
774 }
775 /***********************************************************************/
776
777 //This function splits up the various option parameters
778 inline void splitAtComma(string& prefix, string& suffix){
779         try {
780                 prefix = suffix.substr(0,suffix.find_first_of(','));
781                 if ((suffix.find_first_of(',')+2) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
782                         suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
783                         string space = " ";
784                         while(suffix.at(0) == ' ')
785                                 suffix = suffix.substr(1, suffix.length());
786                 }
787
788         }
789         catch(exception& e) {
790                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtComma. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
791                 exit(1);
792         }       
793 }
794 /***********************************************************************/
795
796 //This function separates the key value from the option value i.e. dist=96_...
797 inline void splitAtEquals(string& key, string& value){          
798         try {
799                 if(value.find_first_of('=') != -1){
800                         key = value.substr(0,value.find_first_of('='));
801                         if ((value.find_first_of('=')+1) <= value.length()) {
802                                 value = value.substr(value.find_first_of('=')+1, value.length());
803                         }
804                 }else{
805                         key = value;
806                         value = 1;
807                 }
808         }
809         catch(exception& e) {
810                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function splitAtEquals. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
811                 exit(1);
812         }       
813 }
814
815 /**************************************************************************************************/
816
817 inline bool inUsersGroups(string groupname, vector<string> Groups) {
818         try {
819                 for (int i = 0; i < Groups.size(); i++) {
820                         if (groupname == Groups[i]) { return true; }
821                 }
822                 return false;
823         }
824         catch(exception& e) {
825                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function inUsersGroups. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
826                 exit(1);
827         }       
828 }
829 /**************************************************************************************************/
830 //returns true if any of the strings in first vector are in second vector
831 inline bool inUsersGroups(vector<string> groupnames, vector<string> Groups) {
832         try {
833                 
834                 for (int i = 0; i < groupnames.size(); i++) {
835                         if (inUsersGroups(groupnames[i], Groups)) { return true; }
836                 }
837                 return false;
838         }
839         catch(exception& e) {
840                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function inUsersGroups. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
841                 exit(1);
842         }       
843 }
844 /***********************************************************************/
845 //this function determines if the user has given us labels that are smaller than the given label.
846 //if so then it returns true so that the calling function can run the previous valid distance.
847 //it's a "smart" distance function.  It also checks for invalid labels.
848 inline bool anyLabelsToProcess(string label, set<string>& userLabels, string errorOff) {
849         try {
850                 
851                 set<string>::iterator it;
852                 vector<float> orderFloat;
853                 map<string, float> userMap;  //the conversion process removes trailing 0's which we need to put back
854                 map<string, float>::iterator it2;
855                 float labelFloat;
856                 bool smaller = false;
857                 
858                 //unique is the smallest line
859                 if (label == "unique") {  return false;  }
860                 else { 
861                         if (convertTestFloat(label, labelFloat)) {
862                                 convert(label, labelFloat); 
863                         }else { //cant convert 
864                                 return false;
865                         }
866                 }
867                 
868                 //go through users set and make them floats
869                 for(it = userLabels.begin(); it != userLabels.end(); ++it) {
870                         
871                         float temp;
872                         if ((*it != "unique") && (convertTestFloat(*it, temp) == true)){
873                                 convert(*it, temp);
874                                 orderFloat.push_back(temp);
875                                 userMap[*it] = temp;
876                         }else if (*it == "unique") { 
877                                 orderFloat.push_back(-1.0);
878                                 userMap["unique"] = -1.0;
879                         }else {
880                                 if (errorOff == "") {  cout << *it << " is not a valid label." << endl;  }
881                                 userLabels.erase(*it); 
882                                 it--;
883                         }
884                 }
885                 
886                 //sort order
887                 sort(orderFloat.begin(), orderFloat.end());
888                 
889                 /*************************************************/
890                 //is this label bigger than any of the users labels
891                 /*************************************************/
892                                 
893                 //loop through order until you find a label greater than label
894                 for (int i = 0; i < orderFloat.size(); i++) {
895                         if (orderFloat[i] < labelFloat) {
896                                 smaller = true;
897                                 if (orderFloat[i] == -1) { 
898                                         if (errorOff == "") { cout << "Your file does not include the label unique." << endl; }
899                                         userLabels.erase("unique");
900                                 }
901                                 else {  
902                                         if (errorOff == "") { cout << "Your file does not include the label " << endl; }
903                                         string s = "";
904                                         for (it2 = userMap.begin(); it2!= userMap.end(); it2++) {  
905                                                 if (it2->second == orderFloat[i]) {  
906                                                         s = it2->first;  
907                                                         //remove small labels
908                                                         userLabels.erase(s);
909                                                         break;
910                                                 }
911                                         }
912                                         if (errorOff == "") {cout << s <<  ". I will use the next smallest distance. " << endl; }
913                                 }
914                         //since they are sorted once you find a bigger one stop looking
915                         }else { break; }
916                 }
917                 
918                 return smaller;
919                                                 
920         }
921         catch(exception& e) {
922                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function anyLabelsToProcess. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
923                 exit(1);
924         }       
925 }
926
927 /**************************************************************************************************/
928 inline void appendFiles(string temp, string filename) {
929         try{
930                 ofstream output;
931                 ifstream input;
932         
933                 //open output file in append mode
934                 openOutputFileAppend(filename, output);
935                 int ableToOpen = openInputFile(temp, input, "no error");
936                 
937                 if (ableToOpen == 0) { //you opened it
938                         while(char c = input.get()){
939                                 if(input.eof())         {       break;                  }
940                                 else                            {       output << c;    }
941                         }
942                         input.close();
943                 }
944                 
945                 output.close();
946         }
947         catch(exception& e) {
948                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function appendFiles. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
949                 exit(1);
950         }       
951 }
952
953 /**************************************************************************************************/
954 inline string sortFile(string distFile, string outputDir){
955         try {   
956         
957                 //if (outputDir == "") {  outputDir += hasPath(distFile);  }
958                 string outfile = getRootName(distFile) + "sorted.dist";
959
960                 
961                 //if you can, use the unix sort since its been optimized for years
962                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
963                         string command = "sort -n -k +3 " + distFile + " -o " + outfile;
964                         system(command.c_str());
965                 #else //you are stuck with my best attempt...
966                         //windows sort does not have a way to specify a column, only a character in the line
967                         //since we cannot assume that the distance will always be at the the same character location on each line
968                         //due to variable sequence name lengths, I chose to force the distance into first position, then sort and then put it back.
969                 
970                         //read in file line by file and put distance first
971                         string tempDistFile = distFile + ".temp";
972                         ifstream input;
973                         ofstream output;
974                         openInputFile(distFile, input);
975                         openOutputFile(tempDistFile, output);
976
977                         string firstName, secondName;
978                         float dist;
979                         while (input) {
980                                 input >> firstName >> secondName >> dist;
981                                 output << dist << '\t' << firstName << '\t' << secondName << endl;
982                                 gobble(input);
983                         }
984                         input.close();
985                         output.close();
986                 
987         
988                         //sort using windows sort
989                         string tempOutfile = outfile + ".temp";
990                         string command = "sort " + tempDistFile + " /O " + tempOutfile;
991                         system(command.c_str());
992                 
993                         //read in sorted file and put distance at end again
994                         ifstream input2;
995                         openInputFile(tempOutfile, input2);
996                         openOutputFile(outfile, output);
997                 
998                         while (input2) {
999                                 input2 >> dist >> firstName >> secondName;
1000                                 output << firstName << '\t' << secondName << '\t' << dist << endl;
1001                                 gobble(input2);
1002                         }
1003                         input2.close();
1004                         output.close();
1005                 
1006                         //remove temp files
1007                         remove(tempDistFile.c_str());
1008                         remove(tempOutfile.c_str());
1009                 #endif
1010                 
1011                 return outfile;
1012         }
1013         catch(exception& e) {
1014                 cout << "Standard Error: " << e.what() << " has occurred in the mothur.h function sortfile. Please contact Pat Schloss at mothur.bugs@gmail.com." << "\n";
1015                 exit(1);
1016         }       
1017 }
1018 /**************************************************************************************************/
1019 inline vector<unsigned long int> setFilePosFasta(string filename, int& num) {
1020
1021                         vector<unsigned long int> positions;
1022                         ifstream inFASTA;
1023                         openInputFile(filename, inFASTA);
1024                                 
1025                         string input;
1026                         while(!inFASTA.eof()){
1027                                 input = getline(inFASTA); gobble(inFASTA);
1028                                 if (input.length() != 0) {
1029                                         if(input[0] == '>'){    unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
1030                                 }
1031                         }
1032                         inFASTA.close();
1033                 
1034                         num = positions.size();
1035                 
1036                         /*FILE * pFile;
1037                         long size;
1038                 
1039                         //get num bytes in file
1040                         pFile = fopen (filename.c_str(),"rb");
1041                         if (pFile==NULL) perror ("Error opening file");
1042                         else{
1043                                 fseek (pFile, 0, SEEK_END);
1044                                 size=ftell (pFile);
1045                                 fclose (pFile);
1046                         }*/
1047                         
1048                         unsigned long int size = positions[(positions.size()-1)];
1049                         ifstream in;
1050                         openInputFile(filename, in);
1051                         
1052                         in.seekg(size);
1053                 
1054                         while(char c = in.get()){
1055                                 if(in.eof())            {       break;  }
1056                                 else                            {       size++; }
1057                         }
1058                         in.close();
1059                 
1060                         positions.push_back(size);
1061                 
1062                         return positions;
1063 }
1064 /**************************************************************************************************/
1065 inline vector<unsigned long int> setFilePosEachLine(string filename, int& num) {
1066
1067                         vector<unsigned long int> positions;
1068                         ifstream in;
1069                         openInputFile(filename, in);
1070                                 
1071                         string input;
1072                         while(!in.eof()){
1073                                 unsigned long int lastpos = in.tellg();
1074                                 input = getline(in); gobble(in);
1075                                 if (input.length() != 0) {
1076                                         unsigned long int pos = in.tellg(); 
1077                                         if (pos != -1) { positions.push_back(pos - input.length() - 1); }
1078                                         else {  positions.push_back(lastpos);  }
1079                                 }
1080                         }
1081                         in.close();
1082                 
1083                         num = positions.size();
1084                 
1085                         FILE * pFile;
1086                         unsigned long int size;
1087                 
1088                         //get num bytes in file
1089                         pFile = fopen (filename.c_str(),"rb");
1090                         if (pFile==NULL) perror ("Error opening file");
1091                         else{
1092                                 fseek (pFile, 0, SEEK_END);
1093                                 size=ftell (pFile);
1094                                 fclose (pFile);
1095                         }
1096                 
1097                         positions.push_back(size);
1098                 
1099                         return positions;
1100 }
1101
1102 /**************************************************************************************************/
1103 #endif
1104