]> git.donarmstrong.com Git - mothur.git/blob - classifyseqscommand.h
added sets to amova and homova commands. added oligos to make.contigs. added metadat...
[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 #include "kmertree.h"
23 #include "aligntree.h"
24
25
26 //KNN and Wang methods modeled from algorithms in
27 //Naı¨ve Bayesian Classifier for Rapid Assignment of rRNA Sequences 
28 //into the New Bacterial Taxonomy􏰎† 
29 //Qiong Wang,1 George M. Garrity,1,2 James M. Tiedje,1,2 and James R. Cole1* 
30 //Center for Microbial Ecology1 and Department of Microbiology and Molecular Genetics,2 Michigan State University, 
31 //East Lansing, Michigan 48824 
32 //Received 10 January 2007/Accepted 18 June 2007 
33
34
35
36 class ClassifySeqsCommand : public Command {
37         
38 public:
39         ClassifySeqsCommand(string);
40         ClassifySeqsCommand();
41         ~ClassifySeqsCommand();
42         
43         vector<string> setParameters();
44         string getCommandName()                 { return "classify.seqs";               }
45         string getCommandCategory()             { return "Phylotype Analysis";  }
46         string getOutputFileNameTag(string, string);
47         string getHelpString(); 
48         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"; }
49         string getDescription()         { return "classify sequences"; }
50         
51         int execute(); 
52         void help() { m->mothurOut(getHelpString()); }  
53         
54         
55         
56 private:
57         struct linePair {
58                 unsigned long long start;
59                 unsigned long long end;
60                 linePair(unsigned long long i, unsigned long long j) : start(i), end(j) {}
61         };
62
63         vector<int> processIDS;   //processid
64         vector<linePair*> lines;
65         vector<string> fastaFileNames;
66         vector<string> namefileNames;
67     vector<string> countfileNames;
68         vector<string> groupfileNames;
69         vector<string> outputNames;
70         map<string, vector<string> > nameMap;
71         map<string,  vector<string> >::iterator itNames;
72         
73         Classify* classify;
74         ReferenceDB* rdb;
75         
76         string fastaFileName, templateFileName, countfile, distanceFileName, namefile, search, method, taxonomyFileName, outputDir, groupfile;
77         int processors, kmerSize, numWanted, cutoff, iters;
78         float match, misMatch, gapOpen, gapExtend;
79         bool abort, probs, save, flip, hasName, hasCount, writeShortcuts;
80         
81         int driver(linePair*, string, string, string, string);
82         int createProcesses(string, string, string, string); 
83         string addUnclassifieds(string, int);
84         
85         int MPIReadNamesFile(string);
86         #ifdef USE_MPI
87         int driverMPI(int, int, MPI_File&, MPI_File&, MPI_File&, MPI_File&, vector<unsigned long long>&);
88         #endif
89 };
90
91 /**************************************************************************************************/
92 //custom data structure for threads to use.
93 // This is passed by void pointer so it can be any data type
94 // that can be passed using a single void pointer (LPVOID).
95 struct classifyData {
96         string taxFName; 
97         string tempTFName; 
98         string filename;
99         string search, taxonomyFileName, templateFileName, method, accnos;
100         unsigned long long start;
101         unsigned long long end;
102         MothurOut* m;
103         float match, misMatch, gapOpen, gapExtend;
104         int count, kmerSize, threadID, cutoff, iters, numWanted;
105         bool probs, flip, writeShortcuts;
106          
107         classifyData(){}
108         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) {
109                 accnos = acc;
110                 taxonomyFileName = tx;
111                 templateFileName = te;
112                 taxFName = a;
113                 tempTFName = r;
114                 filename = f;
115                 search = se;
116                 method = me;
117                 m = mout;
118                 start = st;
119                 end = en;
120                 match = ma; 
121                 misMatch = misMa;
122                 gapOpen = gapO; 
123                 gapExtend = gapE; 
124                 kmerSize = ks;
125                 cutoff = cut;
126                 iters = i;
127                 numWanted = numW;
128                 threadID = tid;
129                 probs = p;
130                 count = 0;
131                 flip = fli;
132         writeShortcuts = wsh;
133         }
134 };
135
136 /**************************************************************************************************/
137 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
138 #else
139 static DWORD WINAPI MyClassThreadFunction(LPVOID lpParam){ 
140         classifyData* pDataArray;
141         pDataArray = (classifyData*)lpParam;
142         
143         try {
144                 ofstream outTax;
145                 pDataArray->m->openOutputFile(pDataArray->taxFName, outTax);
146                 
147                 ofstream outTaxSimple;
148                 pDataArray->m->openOutputFile(pDataArray->tempTFName, outTaxSimple);
149                 
150                 ofstream outAcc;
151                 pDataArray->m->openOutputFile(pDataArray->accnos, outAcc);
152                 
153                 ifstream inFASTA;
154                 pDataArray->m->openInputFile(pDataArray->filename, inFASTA);
155                 
156                 string taxonomy;
157                                 
158                 //print header if you are process 0
159                 if ((pDataArray->start == 0) || (pDataArray->start == 1)) {
160                         inFASTA.seekg(0);
161                 }else { //this accounts for the difference in line endings. 
162                         inFASTA.seekg(pDataArray->start-1); pDataArray->m->gobble(inFASTA); 
163                 }
164                 
165                 pDataArray->count = pDataArray->end;
166                 
167                 //make classify
168                 Classify* myclassify;
169         string outputMethodTag = pDataArray->method + ".";
170                 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);             }
171                 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);                           }
172         else if(pDataArray->method == "zap"){   
173             outputMethodTag = pDataArray->search + "_" + outputMethodTag;
174             if (pDataArray->search == "kmer") {   myclassify = new KmerTree(pDataArray->templateFileName, pDataArray->taxonomyFileName, pDataArray->kmerSize, pDataArray->cutoff); }
175             else {  myclassify = new AlignTree(pDataArray->templateFileName, pDataArray->taxonomyFileName, pDataArray->cutoff);  }
176         }
177                 else {
178                         pDataArray->m->mothurOut(pDataArray->search + " is not a valid method option. I will run the command using bayesian.");
179                         pDataArray->m->mothurOutEndLine();
180                         myclassify = new Bayesian(pDataArray->taxonomyFileName, pDataArray->templateFileName, pDataArray->search, pDataArray->kmerSize, pDataArray->cutoff, pDataArray->iters, pDataArray->threadID, pDataArray->flip, pDataArray->writeShortcuts);     
181                 }
182                 
183                 if (pDataArray->m->control_pressed) { delete myclassify; return 0; }
184                 
185                 int count = 0;
186                 for(int i = 0; i < pDataArray->end; i++){ //end is the number of sequences to process
187                         
188                         if (pDataArray->m->control_pressed) { delete myclassify; return 0; }
189                         
190                         Sequence* candidateSeq = new Sequence(inFASTA); pDataArray->m->gobble(inFASTA);
191                         
192                         if (candidateSeq->getName() != "") {
193                                 
194                                 taxonomy = myclassify->getTaxonomy(candidateSeq);
195                                 
196                                 if (pDataArray->m->control_pressed) { delete candidateSeq; return 0; }
197                                 
198                                 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(); }
199
200                                 //output confidence scores or not
201                                 if (pDataArray->probs) {
202                                         outTax << candidateSeq->getName() << '\t' << taxonomy << endl;
203                                 }else{
204                                         outTax << candidateSeq->getName() << '\t' << myclassify->getSimpleTax() << endl;
205                                 }
206                                         
207                                 outTaxSimple << candidateSeq->getName() << '\t' << myclassify->getSimpleTax() << endl;
208                                         
209                                 if (myclassify->getFlipped()) { outAcc << candidateSeq->getName() << endl; }
210                                 
211                                 count++;
212                         }
213                         delete candidateSeq;
214                         //report progress
215                         if((count) % 100 == 0){ pDataArray->m->mothurOut("Processing sequence: " + toString(count)); pDataArray->m->mothurOutEndLine();         }
216                         
217                 }
218                 //report progress
219                 if((count) % 100 != 0){ pDataArray->m->mothurOut("Processing sequence: " + toString(count)); pDataArray->m->mothurOutEndLine();         }
220                 
221                 delete myclassify;
222                 inFASTA.close();
223                 outTax.close();
224                 outTaxSimple.close();
225                 
226         }
227         catch(exception& e) {
228                 pDataArray->m->errorOut(e, "ClassifySeqsCommand", "MyClassThreadFunction");
229                 exit(1);
230         }
231
232 #endif
233
234
235
236
237 #endif
238