]> git.donarmstrong.com Git - mothur.git/blob - classifyseqscommand.h
major change to the tree class to use the count table class instead of tree map....
[mothur.git] / classifyseqscommand.h
1 #ifndef CLASSIFYSEQSCOMMAND_H
2 #define CLASSIFYSEQSCOMMAND_H
3
4 /*
5  *  classifyseqscommand.h
6  *  Mothur
7  *
8  *  Created by westcott on 11/2/09.
9  *  Copyright 2009 Schloss Lab. All rights reserved.
10  *
11  */
12
13
14 #include "command.hpp"
15 #include "classify.h"
16 #include "referencedb.h"
17 #include "sequence.hpp"
18 #include "bayesian.h"
19 #include "phylotree.h"
20 #include "phylosummary.h"
21 #include "knn.h"
22
23
24 //KNN and Bayesian methods modeled from algorithms in
25 //Naı¨ve Bayesian Classifier for Rapid Assignment of rRNA Sequences 
26 //into the New Bacterial Taxonomy􏰎† 
27 //Qiong Wang,1 George M. Garrity,1,2 James M. Tiedje,1,2 and James R. Cole1* 
28 //Center for Microbial Ecology1 and Department of Microbiology and Molecular Genetics,2 Michigan State University, 
29 //East Lansing, Michigan 48824 
30 //Received 10 January 2007/Accepted 18 June 2007 
31
32
33
34 class ClassifySeqsCommand : public Command {
35         
36 public:
37         ClassifySeqsCommand(string);
38         ClassifySeqsCommand();
39         ~ClassifySeqsCommand();
40         
41         vector<string> setParameters();
42         string getCommandName()                 { return "classify.seqs";               }
43         string getCommandCategory()             { return "Phylotype Analysis";  }
44         string getOutputFileNameTag(string, string);
45         string getHelpString(); 
46         string getCitation() { return "Wang Q, Garrity GM, Tiedje JM, Cole JR (2007). Naive Bayesian classifier for rapid assignment of rRNA sequences into the new bacterial taxonomy. Appl Environ Microbiol 73: 5261-7. [ for Bayesian classifier ] \nAltschul SF, Madden TL, Schaffer AA, Zhang J, Zhang Z, Miller W, Lipman DJ (1997). Gapped BLAST and PSI-BLAST: a new generation of protein database search programs. Nucleic Acids Res 25: 3389-402. [ for BLAST ] \nDeSantis TZ, Hugenholtz P, Larsen N, Rojas M, Brodie EL, Keller K, Huber T, Dalevi D, Hu P, Andersen GL (2006). Greengenes, a chimera-checked 16S rRNA gene database and workbench compatible with ARB. Appl Environ Microbiol 72: 5069-72. [ for kmer ] \nhttp://www.mothur.org/wiki/Classify.seqs"; }
47         string getDescription()         { return "classify sequences"; }
48         
49         int execute(); 
50         void help() { m->mothurOut(getHelpString()); }  
51         
52         
53         
54 private:
55         struct linePair {
56                 unsigned long long start;
57                 unsigned long long end;
58                 linePair(unsigned long long i, unsigned long long j) : start(i), end(j) {}
59         };
60
61         vector<int> processIDS;   //processid
62         vector<linePair*> lines;
63         vector<string> fastaFileNames;
64         vector<string> namefileNames;
65     vector<string> countfileNames;
66         vector<string> groupfileNames;
67         vector<string> outputNames;
68         map<string, vector<string> > nameMap;
69         map<string,  vector<string> >::iterator itNames;
70         
71         Classify* classify;
72         ReferenceDB* rdb;
73         
74         string fastaFileName, templateFileName, countfile, distanceFileName, namefile, search, method, taxonomyFileName, outputDir, groupfile;
75         int processors, kmerSize, numWanted, cutoff, iters;
76         float match, misMatch, gapOpen, gapExtend;
77         bool abort, probs, save, flip, hasName, hasCount;
78         
79         int driver(linePair*, string, string, string, string);
80         int createProcesses(string, string, string, string); 
81         string addUnclassifieds(string, int);
82         
83         int MPIReadNamesFile(string);
84         #ifdef USE_MPI
85         int driverMPI(int, int, MPI_File&, MPI_File&, MPI_File&, MPI_File&, vector<unsigned long long>&);
86         #endif
87 };
88
89 /**************************************************************************************************/
90 //custom data structure for threads to use.
91 // This is passed by void pointer so it can be any data type
92 // that can be passed using a single void pointer (LPVOID).
93 struct classifyData {
94         string taxFName; 
95         string tempTFName; 
96         string filename;
97         string search, taxonomyFileName, templateFileName, method, accnos;
98         unsigned long long start;
99         unsigned long long end;
100         MothurOut* m;
101         float match, misMatch, gapOpen, gapExtend;
102         int count, kmerSize, threadID, cutoff, iters, numWanted;
103         bool probs, flip;
104          
105         classifyData(){}
106         classifyData(string acc, bool p, string me, string te, string tx, string a, string r, string f, string se, int ks, int i, int numW, MothurOut* mout, unsigned long long st, unsigned long long en, float ma, float misMa, float gapO, float gapE, int cut, int tid, bool fli) {
107                 accnos = acc;
108                 taxonomyFileName = tx;
109                 templateFileName = te;
110                 taxFName = a;
111                 tempTFName = r;
112                 filename = f;
113                 search = se;
114                 method = me;
115                 m = mout;
116                 start = st;
117                 end = en;
118                 match = ma; 
119                 misMatch = misMa;
120                 gapOpen = gapO; 
121                 gapExtend = gapE; 
122                 kmerSize = ks;
123                 cutoff = cut;
124                 iters = i;
125                 numWanted = numW;
126                 threadID = tid;
127                 probs = p;
128                 count = 0;
129                 flip = fli;
130         }
131 };
132
133 /**************************************************************************************************/
134 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
135 #else
136 static DWORD WINAPI MyClassThreadFunction(LPVOID lpParam){ 
137         classifyData* pDataArray;
138         pDataArray = (classifyData*)lpParam;
139         
140         try {
141                 ofstream outTax;
142                 pDataArray->m->openOutputFile(pDataArray->taxFName, outTax);
143                 
144                 ofstream outTaxSimple;
145                 pDataArray->m->openOutputFile(pDataArray->tempTFName, outTaxSimple);
146                 
147                 ofstream outAcc;
148                 pDataArray->m->openOutputFile(pDataArray->accnos, outAcc);
149                 
150                 ifstream inFASTA;
151                 pDataArray->m->openInputFile(pDataArray->filename, inFASTA);
152                 
153                 string taxonomy;
154                                 
155                 //print header if you are process 0
156                 if ((pDataArray->start == 0) || (pDataArray->start == 1)) {
157                         inFASTA.seekg(0);
158                 }else { //this accounts for the difference in line endings. 
159                         inFASTA.seekg(pDataArray->start-1); pDataArray->m->gobble(inFASTA); 
160                 }
161                 
162                 pDataArray->count = pDataArray->end;
163                 
164                 //make classify
165                 Classify* myclassify;
166                 if(pDataArray->method == "bayesian"){   myclassify = new Bayesian(pDataArray->taxonomyFileName, pDataArray->templateFileName, pDataArray->search, pDataArray->kmerSize, pDataArray->cutoff, pDataArray->iters, pDataArray->threadID, pDataArray->flip);         }
167                 else if(pDataArray->method == "knn"){   myclassify = new Knn(pDataArray->taxonomyFileName, pDataArray->templateFileName, pDataArray->search, pDataArray->kmerSize, pDataArray->gapOpen, pDataArray->gapExtend, pDataArray->match, pDataArray->misMatch, pDataArray->numWanted, pDataArray->threadID);                           }
168                 else {
169                         pDataArray->m->mothurOut(pDataArray->search + " is not a valid method option. I will run the command using bayesian.");
170                         pDataArray->m->mothurOutEndLine();
171                         myclassify = new Bayesian(pDataArray->taxonomyFileName, pDataArray->templateFileName, pDataArray->search, pDataArray->kmerSize, pDataArray->cutoff, pDataArray->iters, pDataArray->threadID, pDataArray->flip); 
172                 }
173                 
174                 if (pDataArray->m->control_pressed) { delete myclassify; return 0; }
175                 
176                 int count = 0;
177                 for(int i = 0; i < pDataArray->end; i++){ //end is the number of sequences to process
178                         
179                         if (pDataArray->m->control_pressed) { delete myclassify; return 0; }
180                         
181                         Sequence* candidateSeq = new Sequence(inFASTA); pDataArray->m->gobble(inFASTA);
182                         
183                         if (candidateSeq->getName() != "") {
184                                 
185                                 taxonomy = myclassify->getTaxonomy(candidateSeq);
186                                 
187                                 if (pDataArray->m->control_pressed) { delete candidateSeq; return 0; }
188                                 
189                                 if (taxonomy == "unknown;") { pDataArray->m->mothurOut("[WARNING]: " + candidateSeq->getName() + " could not be classified. You can use the remove.lineage command with taxon=unknown; to remove such sequences."); pDataArray->m->mothurOutEndLine(); }
190
191                                 //output confidence scores or not
192                                 if (pDataArray->probs) {
193                                         outTax << candidateSeq->getName() << '\t' << taxonomy << endl;
194                                 }else{
195                                         outTax << candidateSeq->getName() << '\t' << myclassify->getSimpleTax() << endl;
196                                 }
197                                         
198                                 outTaxSimple << candidateSeq->getName() << '\t' << myclassify->getSimpleTax() << endl;
199                                         
200                                 if (myclassify->getFlipped()) { outAcc << candidateSeq->getName() << endl; }
201                                 
202                                 count++;
203                         }
204                         delete candidateSeq;
205                         //report progress
206                         if((count) % 100 == 0){ pDataArray->m->mothurOut("Processing sequence: " + toString(count)); pDataArray->m->mothurOutEndLine();         }
207                         
208                 }
209                 //report progress
210                 if((count) % 100 != 0){ pDataArray->m->mothurOut("Processing sequence: " + toString(count)); pDataArray->m->mothurOutEndLine();         }
211                 
212                 delete myclassify;
213                 inFASTA.close();
214                 outTax.close();
215                 outTaxSimple.close();
216                 
217         }
218         catch(exception& e) {
219                 pDataArray->m->errorOut(e, "ClassifySeqsCommand", "MyClassThreadFunction");
220                 exit(1);
221         }
222
223 #endif
224
225
226
227
228 #endif
229