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