]> git.donarmstrong.com Git - mothur.git/blob - classifyseqscommand.h
changed reading of name file to use buffered reads. note the splitAtWhiteSpace functi...
[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 getHelpString(); 
45         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"; }
46         string getDescription()         { return "classify sequences"; }
47         
48         int execute(); 
49         void help() { m->mothurOut(getHelpString()); }  
50         
51         
52         
53 private:
54         struct linePair {
55                 unsigned long long start;
56                 unsigned long long end;
57                 linePair(unsigned long long i, unsigned long long j) : start(i), end(j) {}
58         };
59
60         vector<int> processIDS;   //processid
61         vector<linePair*> lines;
62         vector<string> fastaFileNames;
63         vector<string> namefileNames;
64         vector<string> groupfileNames;
65         vector<string> outputNames;
66         map<string, vector<string> > nameMap;
67         map<string,  vector<string> >::iterator itNames;
68         
69         Classify* classify;
70         ReferenceDB* rdb;
71         
72         string fastaFileName, templateFileName, distanceFileName, namefile, search, method, taxonomyFileName, outputDir, groupfile;
73         int processors, kmerSize, numWanted, cutoff, iters;
74         float match, misMatch, gapOpen, gapExtend;
75         bool abort, probs, save, flip;
76         
77         int driver(linePair*, string, string, string, string);
78         int createProcesses(string, string, string, string); 
79         string addUnclassifieds(string, int);
80         
81         int MPIReadNamesFile(string);
82         #ifdef USE_MPI
83         int driverMPI(int, int, MPI_File&, MPI_File&, MPI_File&, MPI_File&, vector<unsigned long long>&);
84         #endif
85 };
86
87 /**************************************************************************************************/
88 //custom data structure for threads to use.
89 // This is passed by void pointer so it can be any data type
90 // that can be passed using a single void pointer (LPVOID).
91 struct classifyData {
92         string taxFName; 
93         string tempTFName; 
94         string filename;
95         string search, taxonomyFileName, templateFileName, method, accnos;
96         unsigned long long start;
97         unsigned long long end;
98         MothurOut* m;
99         float match, misMatch, gapOpen, gapExtend;
100         int count, kmerSize, threadID, cutoff, iters, numWanted;
101         bool probs, flip;
102          
103         classifyData(){}
104         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) {
105                 accnos = acc;
106                 taxonomyFileName = tx;
107                 templateFileName = te;
108                 taxFName = a;
109                 tempTFName = r;
110                 filename = f;
111                 search = se;
112                 method = me;
113                 m = mout;
114                 start = st;
115                 end = en;
116                 match = ma; 
117                 misMatch = misMa;
118                 gapOpen = gapO; 
119                 gapExtend = gapE; 
120                 kmerSize = ks;
121                 cutoff = cut;
122                 iters = i;
123                 numWanted = numW;
124                 threadID = tid;
125                 probs = p;
126                 count = 0;
127                 flip = fli;
128         }
129 };
130
131 /**************************************************************************************************/
132 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
133 #else
134 static DWORD WINAPI MyClassThreadFunction(LPVOID lpParam){ 
135         classifyData* pDataArray;
136         pDataArray = (classifyData*)lpParam;
137         
138         try {
139                 ofstream outTax;
140                 pDataArray->m->openOutputFile(pDataArray->taxFName, outTax);
141                 
142                 ofstream outTaxSimple;
143                 pDataArray->m->openOutputFile(pDataArray->tempTFName, outTaxSimple);
144                 
145                 ofstream outAcc;
146                 pDataArray->m->openOutputFile(pDataArray->accnos, outAcc);
147                 
148                 ifstream inFASTA;
149                 pDataArray->m->openInputFile(pDataArray->filename, inFASTA);
150                 
151                 string taxonomy;
152                                 
153                 //print header if you are process 0
154                 if ((pDataArray->start == 0) || (pDataArray->start == 1)) {
155                         inFASTA.seekg(0);
156                 }else { //this accounts for the difference in line endings. 
157                         inFASTA.seekg(pDataArray->start-1); pDataArray->m->gobble(inFASTA); 
158                 }
159                 
160                 pDataArray->count = pDataArray->end;
161                 
162                 //make classify
163                 Classify* myclassify;
164                 if(pDataArray->method == "bayesian"){   myclassify = new Bayesian(pDataArray->taxonomyFileName, pDataArray->templateFileName, pDataArray->search, pDataArray->kmerSize, pDataArray->cutoff, pDataArray->iters, pDataArray->threadID, pDataArray->flip);         }
165                 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);                           }
166                 else {
167                         pDataArray->m->mothurOut(pDataArray->search + " is not a valid method option. I will run the command using bayesian.");
168                         pDataArray->m->mothurOutEndLine();
169                         myclassify = new Bayesian(pDataArray->taxonomyFileName, pDataArray->templateFileName, pDataArray->search, pDataArray->kmerSize, pDataArray->cutoff, pDataArray->iters, pDataArray->threadID, pDataArray->flip); 
170                 }
171                 
172                 if (pDataArray->m->control_pressed) { delete myclassify; return 0; }
173                 
174                 int count = 0;
175                 for(int i = 0; i < pDataArray->end; i++){ //end is the number of sequences to process
176                         
177                         if (pDataArray->m->control_pressed) { delete myclassify; return 0; }
178                         
179                         Sequence* candidateSeq = new Sequence(inFASTA); pDataArray->m->gobble(inFASTA);
180                         
181                         if (candidateSeq->getName() != "") {
182                                 
183                                 taxonomy = myclassify->getTaxonomy(candidateSeq);
184                                 
185                                 if (pDataArray->m->control_pressed) { delete candidateSeq; return 0; }
186                                 
187                                 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(); }
188
189                                 //output confidence scores or not
190                                 if (pDataArray->probs) {
191                                         outTax << candidateSeq->getName() << '\t' << taxonomy << endl;
192                                 }else{
193                                         outTax << candidateSeq->getName() << '\t' << myclassify->getSimpleTax() << endl;
194                                 }
195                                         
196                                 outTaxSimple << candidateSeq->getName() << '\t' << myclassify->getSimpleTax() << endl;
197                                         
198                                 if (myclassify->getFlipped()) { outAcc << candidateSeq->getName() << endl; }
199                                 
200                                 count++;
201                         }
202                         delete candidateSeq;
203                         //report progress
204                         if((count) % 100 == 0){ pDataArray->m->mothurOut("Processing sequence: " + toString(count)); pDataArray->m->mothurOutEndLine();         }
205                         
206                 }
207                 //report progress
208                 if((count) % 100 != 0){ pDataArray->m->mothurOut("Processing sequence: " + toString(count)); pDataArray->m->mothurOutEndLine();         }
209                 
210                 delete myclassify;
211                 inFASTA.close();
212                 outTax.close();
213                 outTaxSimple.close();
214                 
215         }
216         catch(exception& e) {
217                 pDataArray->m->errorOut(e, "ClassifySeqsCommand", "MyClassThreadFunction");
218                 exit(1);
219         }
220
221 #endif
222
223
224
225
226 #endif
227