]> git.donarmstrong.com Git - mothur.git/blob - classifyseqscommand.h
added count file to get.groups and remove.groups. added shortcut parameter to classif...
[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, writeShortcuts;
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, writeShortcuts;
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, bool wsh) {
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         writeShortcuts = wsh;
131         }
132 };
133
134 /**************************************************************************************************/
135 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
136 #else
137 static DWORD WINAPI MyClassThreadFunction(LPVOID lpParam){ 
138         classifyData* pDataArray;
139         pDataArray = (classifyData*)lpParam;
140         
141         try {
142                 ofstream outTax;
143                 pDataArray->m->openOutputFile(pDataArray->taxFName, outTax);
144                 
145                 ofstream outTaxSimple;
146                 pDataArray->m->openOutputFile(pDataArray->tempTFName, outTaxSimple);
147                 
148                 ofstream outAcc;
149                 pDataArray->m->openOutputFile(pDataArray->accnos, outAcc);
150                 
151                 ifstream inFASTA;
152                 pDataArray->m->openInputFile(pDataArray->filename, inFASTA);
153                 
154                 string taxonomy;
155                                 
156                 //print header if you are process 0
157                 if ((pDataArray->start == 0) || (pDataArray->start == 1)) {
158                         inFASTA.seekg(0);
159                 }else { //this accounts for the difference in line endings. 
160                         inFASTA.seekg(pDataArray->start-1); pDataArray->m->gobble(inFASTA); 
161                 }
162                 
163                 pDataArray->count = pDataArray->end;
164                 
165                 //make classify
166                 Classify* myclassify;
167                 if(pDataArray->method == "bayesian"){   myclassify = new Bayesian(pDataArray->taxonomyFileName, pDataArray->templateFileName, pDataArray->search, pDataArray->kmerSize, pDataArray->cutoff, pDataArray->iters, pDataArray->threadID, pDataArray->flip, pDataArray->writeShortcuts);             }
168                 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);                           }
169                 else {
170                         pDataArray->m->mothurOut(pDataArray->search + " is not a valid method option. I will run the command using bayesian.");
171                         pDataArray->m->mothurOutEndLine();
172                         myclassify = new Bayesian(pDataArray->taxonomyFileName, pDataArray->templateFileName, pDataArray->search, pDataArray->kmerSize, pDataArray->cutoff, pDataArray->iters, pDataArray->threadID, pDataArray->flip, pDataArray->writeShortcuts);     
173                 }
174                 
175                 if (pDataArray->m->control_pressed) { delete myclassify; return 0; }
176                 
177                 int count = 0;
178                 for(int i = 0; i < pDataArray->end; i++){ //end is the number of sequences to process
179                         
180                         if (pDataArray->m->control_pressed) { delete myclassify; return 0; }
181                         
182                         Sequence* candidateSeq = new Sequence(inFASTA); pDataArray->m->gobble(inFASTA);
183                         
184                         if (candidateSeq->getName() != "") {
185                                 
186                                 taxonomy = myclassify->getTaxonomy(candidateSeq);
187                                 
188                                 if (pDataArray->m->control_pressed) { delete candidateSeq; return 0; }
189                                 
190                                 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(); }
191
192                                 //output confidence scores or not
193                                 if (pDataArray->probs) {
194                                         outTax << candidateSeq->getName() << '\t' << taxonomy << endl;
195                                 }else{
196                                         outTax << candidateSeq->getName() << '\t' << myclassify->getSimpleTax() << endl;
197                                 }
198                                         
199                                 outTaxSimple << candidateSeq->getName() << '\t' << myclassify->getSimpleTax() << endl;
200                                         
201                                 if (myclassify->getFlipped()) { outAcc << candidateSeq->getName() << endl; }
202                                 
203                                 count++;
204                         }
205                         delete candidateSeq;
206                         //report progress
207                         if((count) % 100 == 0){ pDataArray->m->mothurOut("Processing sequence: " + toString(count)); pDataArray->m->mothurOutEndLine();         }
208                         
209                 }
210                 //report progress
211                 if((count) % 100 != 0){ pDataArray->m->mothurOut("Processing sequence: " + toString(count)); pDataArray->m->mothurOutEndLine();         }
212                 
213                 delete myclassify;
214                 inFASTA.close();
215                 outTax.close();
216                 outTaxSimple.close();
217                 
218         }
219         catch(exception& e) {
220                 pDataArray->m->errorOut(e, "ClassifySeqsCommand", "MyClassThreadFunction");
221                 exit(1);
222         }
223
224 #endif
225
226
227
228
229 #endif
230