2 * classifyseqscommand.cpp
5 * Created by westcott on 11/2/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "classifyseqscommand.h"
11 #include "sequence.hpp"
12 #include "phylotype.h"
14 #include "doTaxonomy.h"
17 //**********************************************************************************************************************
19 ClassifySeqsCommand::ClassifySeqsCommand(string option){
21 // globaldata = GlobalData::getInstance();
24 //allow user to run help
25 if(option == "help") { help(); abort = true; }
29 //valid paramters for this command
30 string AlignArray[] = {"template","fasta","search","ksize","method","processors","taxonomy","match","mismatch","gapopen","gapextend","numwanted"};
31 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
33 OptionParser parser(option);
34 map<string, string> parameters = parser.getParameters();
36 ValidParameters validParameter;
38 //check to make sure all parameters are valid for command
39 for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
40 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
43 //check for required parameters
44 templateFileName = validParameter.validFile(parameters, "template", true);
45 if (templateFileName == "not found") {
46 mothurOut("template is a required parameter for the classify.seqs command.");
50 else if (templateFileName == "not open") { abort = true; }
52 fastaFileName = validParameter.validFile(parameters, "fasta", true);
53 if (fastaFileName == "not found") {
54 mothurOut("fasta is a required parameter for the classify.seqs command.");
58 else if (fastaFileName == "not open") { abort = true; }
60 taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true);
61 if (taxonomyFileName == "not found") {
62 mothurOut("taxonomy is a required parameter for the classify.seqs command.");
66 else if (taxonomyFileName == "not open") { abort = true; }
69 //check for optional parameter and set defaults
70 // ...at some point should added some additional type checking...
72 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found"){ temp = "8"; }
73 convert(temp, kmerSize);
75 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
76 convert(temp, processors);
78 search = validParameter.validFile(parameters, "search", false); if (search == "not found"){ search = "kmer"; }
80 method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "phylotype"; }
82 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
85 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
86 convert(temp, misMatch);
88 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
89 convert(temp, gapOpen);
91 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
92 convert(temp, gapExtend);
94 temp = validParameter.validFile(parameters, "numwanted", false); if (temp == "not found"){ temp = "10"; }
95 convert(temp, numWanted);
100 catch(exception& e) {
101 errorOut(e, "ClassifySeqsCommand", "ClassifySeqsCommand");
106 //**********************************************************************************************************************
108 ClassifySeqsCommand::~ClassifySeqsCommand(){
110 if (abort == false) {
111 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
115 //**********************************************************************************************************************
117 void ClassifySeqsCommand::help(){
119 mothurOut("The classify.seqs command reads a fasta file containing sequences and creates a .taxonomy file and a .tax.summary file.\n");
120 mothurOut("The classify.seqs command parameters are template, fasta, search, ksize, method, taxonomy, processors, match, mismatch, gapopen, gapextend and numwanted.\n");
121 mothurOut("The template, fasta and taxonomy parameters are required.\n");
122 mothurOut("The search parameter allows you to specify the method to find most similar template. Your options are: suffix, kmer and blast. The default is kmer.\n");
123 mothurOut("The method parameter allows you to specify classification method to use. Your options are: phylotype, bayesian and knn. The default is phylotype.\n");
124 mothurOut("The ksize parameter allows you to specify the kmer size for finding most similar template to candidate. The default is 8.\n");
125 mothurOut("The processors parameter allows you to specify the number of processors to use. The default is 1.\n");
126 mothurOut("The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n");
127 mothurOut("The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n");
128 mothurOut("The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -1.0.\n");
129 mothurOut("The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -2.0.\n");
130 mothurOut("The numwanted parameter allows you to specify the number of sequence matches you want with the knn method. The default is 10.\n");
131 mothurOut("The classify.seqs command should be in the following format: \n");
132 mothurOut("classify.seqs(template=yourTemplateFile, fasta=yourFastaFile, method=yourClassificationMethod, search=yourSearchmethod, ksize=yourKmerSize, taxonomy=yourTaxonomyFile, processors=yourProcessors) \n");
133 mothurOut("Example classify.seqs(fasta=amazon.fasta, template=core.filtered, method=phylotype, search=gotoh, ksize=8, processors=2)\n");
134 mothurOut("The .taxonomy file consists of 2 columns: 1 = your sequence name, 2 = the taxonomy for your sequence. \n");
135 mothurOut("The .tax.summary is a summary of the different taxonomies represented in your fasta file. \n");
136 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
138 catch(exception& e) {
139 errorOut(e, "ClassifySeqsCommand", "help");
145 //**********************************************************************************************************************
147 int ClassifySeqsCommand::execute(){
149 if (abort == true) { return 0; }
151 int start = time(NULL);
153 if(method == "phylotype"){ classify = new PhyloType(taxonomyFileName, templateFileName, search, kmerSize, gapOpen, gapExtend, match, misMatch); }
154 //else if(method == "bayesian") { classify = new Bayesian(taxonomyFileName, templateFileName, search); }
155 else if(method == "knn") { classify = new Knn(taxonomyFileName, templateFileName, search, kmerSize, gapOpen, gapExtend, match, misMatch, numWanted); }
157 mothurOut(search + " is not a valid method option. I will run the command using phylotype.");
159 classify = new PhyloType(taxonomyFileName, templateFileName, search, kmerSize, gapOpen, gapExtend, match, misMatch);
162 int numFastaSeqs = 0;
164 string newTaxonomyFile = getRootName(fastaFileName) + "taxonomy";
165 string taxSummary = getRootName(fastaFileName) + "tax.summary";
167 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
170 openInputFile(fastaFileName, inFASTA);
171 numFastaSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
174 lines.push_back(new linePair(0, numFastaSeqs));
176 driver(lines[0], newTaxonomyFile);
180 vector<int> positions;
181 processIDS.resize(0);
184 openInputFile(fastaFileName, inFASTA);
187 while(!inFASTA.eof()){
188 input = getline(inFASTA);
189 if (input.length() != 0) {
190 if(input[0] == '>'){ int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
195 numFastaSeqs = positions.size();
197 int numSeqsPerProcessor = numFastaSeqs / processors;
199 for (int i = 0; i < processors; i++) {
200 int startPos = positions[ i * numSeqsPerProcessor ];
201 if(i == processors - 1){
202 numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor;
204 lines.push_back(new linePair(startPos, numSeqsPerProcessor));
206 createProcesses(newTaxonomyFile);
208 rename((newTaxonomyFile + toString(processIDS[0]) + ".temp").c_str(), newTaxonomyFile.c_str());
210 for(int i=1;i<processors;i++){
211 appendTaxFiles((newTaxonomyFile + toString(processIDS[i]) + ".temp"), newTaxonomyFile);
212 remove((newTaxonomyFile + toString(processIDS[i]) + ".temp").c_str());
218 openInputFile(candidateFileName, inFASTA);
219 numFastaSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
222 lines.push_back(new linePair(0, numFastaSeqs));
224 driver(lines[0], newTaxonomyFile);
229 //make taxonomy tree from new taxonomy file
231 openInputFile(newTaxonomyFile, inTaxonomy);
233 string accession, taxaList;
234 PhyloTree taxaBrowser;
236 //read in users taxonomy file and add sequences to tree
237 while(!inTaxonomy.eof()){
238 inTaxonomy >> accession >> taxaList;
240 taxaBrowser.addSeqToTree(accession, taxaList);
246 taxaBrowser.assignHeirarchyIDs(0);
249 openOutputFile(taxSummary, outTaxTree);
251 taxaBrowser.print(outTaxTree);
254 mothurOut("It took " + toString(time(NULL) - start) + " secs to classify " + toString(numFastaSeqs) + " sequences.");
260 catch(exception& e) {
261 errorOut(e, "ClassifySeqsCommand", "execute");
265 /**************************************************************************************************/
267 void ClassifySeqsCommand::createProcesses(string taxFileName) {
269 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
271 // processIDS.resize(0);
273 //loop through and create all the processes you want
274 while (process != processors) {
278 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
281 driver(lines[process], taxFileName + toString(getpid()) + ".temp");
283 }else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); }
286 //force parent to wait until all the processes are done
287 for (int i=0;i<processors;i++) {
288 int temp = processIDS[i];
293 catch(exception& e) {
294 errorOut(e, "ClassifySeqsCommand", "createProcesses");
298 /**************************************************************************************************/
300 void ClassifySeqsCommand::appendTaxFiles(string temp, string filename) {
305 openOutputFileAppend(filename, output);
306 openInputFile(temp, input);
308 while(char c = input.get()){
309 if(input.eof()) { break; }
310 else { output << c; }
316 catch(exception& e) {
317 errorOut(e, "ClassifySeqsCommand", "appendTaxFiles");
322 //**********************************************************************************************************************
324 int ClassifySeqsCommand::driver(linePair* line, string taxFName){
327 openOutputFile(taxFName, outTax);
330 openInputFile(fastaFileName, inFASTA);
332 inFASTA.seekg(line->start);
336 for(int i=0;i<line->numSeqs;i++){
338 Sequence* candidateSeq = new Sequence(inFASTA);
340 taxonomy = classify->getTaxonomy(candidateSeq);
342 if (taxonomy != "bad seq") {
343 outTax << candidateSeq->getName() << '\t' << taxonomy << endl;
354 catch(exception& e) {
355 errorOut(e, "ClassifySeqsCommand", "driver");
360 /**************************************************************************************************/