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"
13 #include "phylotree.h"
16 //**********************************************************************************************************************
18 ClassifySeqsCommand::ClassifySeqsCommand(string option){
22 //allow user to run help
23 if(option == "help") { help(); abort = true; }
27 //valid paramters for this command
28 string AlignArray[] = {"template","fasta","name","search","ksize","method","processors","taxonomy","match","mismatch","gapopen","gapextend","numwanted","cutoff","probs","iters"};
29 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
31 OptionParser parser(option);
32 map<string, string> parameters = parser.getParameters();
34 ValidParameters validParameter;
36 //check to make sure all parameters are valid for command
37 for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
38 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
41 //check for required parameters
42 templateFileName = validParameter.validFile(parameters, "template", true);
43 if (templateFileName == "not found") {
44 mothurOut("template is a required parameter for the classify.seqs command.");
48 else if (templateFileName == "not open") { abort = true; }
50 fastaFileName = validParameter.validFile(parameters, "fasta", false);
51 if (fastaFileName == "not found") { mothurOut("fasta is a required parameter for the classify.seqs command."); mothurOutEndLine(); abort = true; }
53 splitAtDash(fastaFileName, fastaFileNames);
55 //go through files and make sure they are good, if not, then disregard them
56 for (int i = 0; i < fastaFileNames.size(); i++) {
59 ableToOpen = openInputFile(fastaFileNames[i], in);
60 if (ableToOpen == 1) {
61 mothurOut(fastaFileNames[i] + " will be disregarded."); mothurOutEndLine();
62 //erase from file list
63 fastaFileNames.erase(fastaFileNames.begin()+i);
69 //make sure there is at least one valid file left
70 if (fastaFileNames.size() == 0) { mothurOut("no valid files."); mothurOutEndLine(); abort = true; }
74 taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true);
75 if (taxonomyFileName == "not found") {
76 mothurOut("taxonomy is a required parameter for the classify.seqs command.");
80 else if (taxonomyFileName == "not open") { abort = true; }
83 namefile = validParameter.validFile(parameters, "name", false);
84 if (namefile == "not found") { namefile = ""; }
87 splitAtDash(namefile, namefileNames);
89 //go through files and make sure they are good, if not, then disregard them
90 for (int i = 0; i < namefileNames.size(); i++) {
93 ableToOpen = openInputFile(namefileNames[i], in);
94 if (ableToOpen == 1) { mothurOut("Unable to match name file with fasta file."); mothurOutEndLine(); abort = true; }
100 if (namefileNames.size() != fastaFileNames.size()) { abort = true; mothurOut("If you provide a name file, you must have one for each fasta file."); mothurOutEndLine(); }
103 //check for optional parameter and set defaults
104 // ...at some point should added some additional type checking...
106 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found"){ temp = "8"; }
107 convert(temp, kmerSize);
109 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
110 convert(temp, processors);
112 search = validParameter.validFile(parameters, "search", false); if (search == "not found"){ search = "kmer"; }
114 method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "bayesian"; }
116 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
117 convert(temp, match);
119 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
120 convert(temp, misMatch);
122 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
123 convert(temp, gapOpen);
125 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
126 convert(temp, gapExtend);
128 temp = validParameter.validFile(parameters, "numwanted", false); if (temp == "not found"){ temp = "10"; }
129 convert(temp, numWanted);
131 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found"){ temp = "0"; }
132 convert(temp, cutoff);
134 temp = validParameter.validFile(parameters, "probs", false); if (temp == "not found"){ temp = "true"; }
135 probs = isTrue(temp);
137 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "100"; }
138 convert(temp, iters);
142 if ((method == "bayesian") && (search != "kmer")) {
143 mothurOut("The bayesian method requires the kmer search." + search + "will be disregarded." ); mothurOutEndLine();
149 catch(exception& e) {
150 errorOut(e, "ClassifySeqsCommand", "ClassifySeqsCommand");
155 //**********************************************************************************************************************
157 ClassifySeqsCommand::~ClassifySeqsCommand(){
159 if (abort == false) {
160 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
164 //**********************************************************************************************************************
166 void ClassifySeqsCommand::help(){
168 mothurOut("The classify.seqs command reads a fasta file containing sequences and creates a .taxonomy file and a .tax.summary file.\n");
169 mothurOut("The classify.seqs command parameters are template, fasta, search, ksize, method, taxonomy, processors, match, mismatch, gapopen, gapextend, numwanted and probs.\n");
170 mothurOut("The template, fasta and taxonomy parameters are required. You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
171 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");
172 mothurOut("The method parameter allows you to specify classification method to use. Your options are: bayesian and knn. The default is bayesian.\n");
173 mothurOut("The ksize parameter allows you to specify the kmer size for finding most similar template to candidate. The default is 8.\n");
174 mothurOut("The processors parameter allows you to specify the number of processors to use. The default is 1.\n");
175 mothurOut("The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n");
176 mothurOut("The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n");
177 mothurOut("The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -2.0.\n");
178 mothurOut("The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -1.0.\n");
179 mothurOut("The numwanted parameter allows you to specify the number of sequence matches you want with the knn method. The default is 10.\n");
180 mothurOut("The cutoff parameter allows you to specify a bootstrap confidence threshold for your taxonomy. The default is 0.\n");
181 mothurOut("The probs parameter shut off the bootstrapping results for the bayesian method. The default is true, meaning you want the bootstrapping to be run.\n");
182 mothurOut("The iters parameter allows you to specify how many iterations to do when calculating the bootstrap confidence score for your taxonomy with the bayesian method. The default is 100.\n");
183 mothurOut("The classify.seqs command should be in the following format: \n");
184 mothurOut("classify.seqs(template=yourTemplateFile, fasta=yourFastaFile, method=yourClassificationMethod, search=yourSearchmethod, ksize=yourKmerSize, taxonomy=yourTaxonomyFile, processors=yourProcessors) \n");
185 mothurOut("Example classify.seqs(fasta=amazon.fasta, template=core.filtered, method=knn, search=gotoh, ksize=8, processors=2)\n");
186 mothurOut("The .taxonomy file consists of 2 columns: 1 = your sequence name, 2 = the taxonomy for your sequence. \n");
187 mothurOut("The .tax.summary is a summary of the different taxonomies represented in your fasta file. \n");
188 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
190 catch(exception& e) {
191 errorOut(e, "ClassifySeqsCommand", "help");
197 //**********************************************************************************************************************
199 int ClassifySeqsCommand::execute(){
201 if (abort == true) { return 0; }
203 if(method == "bayesian"){ classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters); }
204 else if(method == "knn"){ classify = new Knn(taxonomyFileName, templateFileName, search, kmerSize, gapOpen, gapExtend, match, misMatch, numWanted); }
206 mothurOut(search + " is not a valid method option. I will run the command using bayesian.");
208 classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters);
212 for (int s = 0; s < fastaFileNames.size(); s++) {
216 nameMap.clear(); //remove old names
219 openInputFile(namefileNames[s], inNames);
221 string firstCol, secondCol;
222 while(!inNames.eof()) {
223 inNames >> firstCol >> secondCol; gobble(inNames);
224 nameMap[firstCol] = getNumNames(secondCol); //ex. seq1 seq1,seq3,seq5 -> seq1 = 3.
229 mothurOut("Classifying sequences from " + fastaFileNames[s] + " ..." ); mothurOutEndLine();
230 string newTaxonomyFile = getRootName(fastaFileNames[s]) + getRootName(taxonomyFileName) + "taxonomy";
231 string tempTaxonomyFile = getRootName(fastaFileNames[s]) + "taxonomy.temp";
232 string taxSummary = getRootName(fastaFileNames[s]) + getRootName(taxonomyFileName) + "tax.summary";
234 int start = time(NULL);
235 int numFastaSeqs = 0;
236 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
238 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
241 openInputFile(fastaFileNames[s], inFASTA);
242 numFastaSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
245 lines.push_back(new linePair(0, numFastaSeqs));
247 driver(lines[0], newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
250 vector<int> positions;
251 processIDS.resize(0);
254 openInputFile(fastaFileNames[s], inFASTA);
257 while(!inFASTA.eof()){
258 input = getline(inFASTA);
259 if (input.length() != 0) {
260 if(input[0] == '>'){ int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
265 numFastaSeqs = positions.size();
267 int numSeqsPerProcessor = numFastaSeqs / processors;
269 for (int i = 0; i < processors; i++) {
270 int startPos = positions[ i * numSeqsPerProcessor ];
271 if(i == processors - 1){
272 numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor;
274 lines.push_back(new linePair(startPos, numSeqsPerProcessor));
276 createProcesses(newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
278 rename((newTaxonomyFile + toString(processIDS[0]) + ".temp").c_str(), newTaxonomyFile.c_str());
279 rename((tempTaxonomyFile + toString(processIDS[0]) + ".temp").c_str(), tempTaxonomyFile.c_str());
281 for(int i=1;i<processors;i++){
282 appendTaxFiles((newTaxonomyFile + toString(processIDS[i]) + ".temp"), newTaxonomyFile);
283 appendTaxFiles((tempTaxonomyFile + toString(processIDS[i]) + ".temp"), tempTaxonomyFile);
284 remove((newTaxonomyFile + toString(processIDS[i]) + ".temp").c_str());
285 remove((tempTaxonomyFile + toString(processIDS[i]) + ".temp").c_str());
291 openInputFile(fastaFileNames[s], inFASTA);
292 numFastaSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
295 lines.push_back(new linePair(0, numFastaSeqs));
297 driver(lines[0], newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
299 //make taxonomy tree from new taxonomy file
300 PhyloTree taxaBrowser;
303 openInputFile(tempTaxonomyFile, in);
305 //read in users taxonomy file and add sequences to tree
308 in >> name >> taxon; gobble(in);
310 if (namefile != "") {
311 itNames = nameMap.find(name);
313 if (itNames == nameMap.end()) {
314 mothurOut(name + " is not in your name file please correct."); mothurOutEndLine(); exit(1);
316 for (int i = 0; i < itNames->second; i++) {
317 taxaBrowser.addSeqToTree(name+toString(i), taxon); //add it as many times as there are identical seqs
320 }else { taxaBrowser.addSeqToTree(name, taxon); } //add it once
324 taxaBrowser.assignHeirarchyIDs(0);
326 taxaBrowser.binUnclassified();
328 remove(tempTaxonomyFile.c_str());
332 openOutputFile(taxSummary, outTaxTree);
333 taxaBrowser.print(outTaxTree);
336 //output taxonomy with the unclassified bins added
338 openInputFile(newTaxonomyFile, inTax);
341 string unclass = newTaxonomyFile + ".unclass.temp";
342 openOutputFile(unclass, outTax);
344 //get maxLevel from phylotree so you know how many 'unclassified's to add
345 int maxLevel = taxaBrowser.getMaxLevel();
347 //read taxfile - this reading and rewriting is done to preserve the confidence sscores.
348 while (!inTax.eof()) {
349 inTax >> name >> taxon; gobble(inTax);
351 string newTax = addUnclassifieds(taxon, maxLevel);
353 outTax << name << '\t' << newTax << endl;
358 remove(newTaxonomyFile.c_str());
359 rename(unclass.c_str(), newTaxonomyFile.c_str());
362 mothurOut("It took " + toString(time(NULL) - start) + " secs to classify " + toString(numFastaSeqs) + " sequences."); mothurOutEndLine(); mothurOutEndLine();
368 catch(exception& e) {
369 errorOut(e, "ClassifySeqsCommand", "execute");
374 /**************************************************************************************************/
375 string ClassifySeqsCommand::addUnclassifieds(string tax, int maxlevel) {
377 string newTax, taxon;
380 //keep what you have counting the levels
381 while (tax.find_first_of(';') != -1) {
383 taxon = tax.substr(0,tax.find_first_of(';'))+';';
384 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
389 //add "unclassified" until you reach maxLevel
390 while (level < maxlevel) {
391 newTax += "unclassified;";
397 catch(exception& e) {
398 errorOut(e, "ClassifySeqsCommand", "addUnclassifieds");
403 /**************************************************************************************************/
405 void ClassifySeqsCommand::createProcesses(string taxFileName, string tempTaxFile, string filename) {
407 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
409 // processIDS.resize(0);
411 //loop through and create all the processes you want
412 while (process != processors) {
416 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
419 driver(lines[process], taxFileName + toString(getpid()) + ".temp", tempTaxFile + toString(getpid()) + ".temp", filename);
421 }else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); }
424 //force parent to wait until all the processes are done
425 for (int i=0;i<processors;i++) {
426 int temp = processIDS[i];
431 catch(exception& e) {
432 errorOut(e, "ClassifySeqsCommand", "createProcesses");
436 /**************************************************************************************************/
438 void ClassifySeqsCommand::appendTaxFiles(string temp, string filename) {
443 openOutputFileAppend(filename, output);
444 openInputFile(temp, input);
446 while(char c = input.get()){
447 if(input.eof()) { break; }
448 else { output << c; }
454 catch(exception& e) {
455 errorOut(e, "ClassifySeqsCommand", "appendTaxFiles");
460 //**********************************************************************************************************************
462 int ClassifySeqsCommand::driver(linePair* line, string taxFName, string tempTFName, string filename){
465 openOutputFile(taxFName, outTax);
467 ofstream outTaxSimple;
468 openOutputFile(tempTFName, outTaxSimple);
471 openInputFile(filename, inFASTA);
473 inFASTA.seekg(line->start);
477 for(int i=0;i<line->numSeqs;i++){
479 Sequence* candidateSeq = new Sequence(inFASTA);
481 if (candidateSeq->getName() != "") {
482 taxonomy = classify->getTaxonomy(candidateSeq);
484 if (taxonomy != "bad seq") {
485 //output confidence scores or not
487 outTax << candidateSeq->getName() << '\t' << taxonomy << endl;
489 outTax << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
490 cout << classify->getSimpleTax() << endl;
493 outTaxSimple << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
498 if((i+1) % 100 == 0){
499 mothurOut("Classifying sequence " + toString(i+1)); mothurOutEndLine();
505 outTaxSimple.close();
509 catch(exception& e) {
510 errorOut(e, "ClassifySeqsCommand", "driver");
515 /**************************************************************************************************/