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", "outputdir","inputdir"};
29 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
31 OptionParser parser(option);
32 map<string, string> parameters = parser.getParameters();
34 ValidParameters validParameter;
35 map<string, string>::iterator it;
37 //check to make sure all parameters are valid for command
38 for (it = parameters.begin(); it != parameters.end(); it++) {
39 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
42 //if the user changes the output directory command factory will send this info to us in the output parameter
43 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
45 //if the user changes the input directory command factory will send this info to us in the output parameter
46 string inputDir = validParameter.validFile(parameters, "inputdir", false);
47 if (inputDir == "not found"){ inputDir = ""; }
50 it = parameters.find("template");
51 //user has given a template file
52 if(it != parameters.end()){
53 path = hasPath(it->second);
54 //if the user has not given a path then, add inputdir. else leave path alone.
55 if (path == "") { parameters["template"] = inputDir + it->second; }
58 it = parameters.find("taxonomy");
59 //user has given a template file
60 if(it != parameters.end()){
61 path = hasPath(it->second);
62 //if the user has not given a path then, add inputdir. else leave path alone.
63 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
67 //check for required parameters
68 templateFileName = validParameter.validFile(parameters, "template", true);
69 if (templateFileName == "not found") {
70 mothurOut("template is a required parameter for the classify.seqs command.");
74 else if (templateFileName == "not open") { abort = true; }
76 fastaFileName = validParameter.validFile(parameters, "fasta", false);
77 if (fastaFileName == "not found") { mothurOut("fasta is a required parameter for the classify.seqs command."); mothurOutEndLine(); abort = true; }
79 splitAtDash(fastaFileName, fastaFileNames);
81 //go through files and make sure they are good, if not, then disregard them
82 for (int i = 0; i < fastaFileNames.size(); i++) {
84 string path = hasPath(fastaFileNames[i]);
85 //if the user has not given a path then, add inputdir. else leave path alone.
86 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
91 ableToOpen = openInputFile(fastaFileNames[i], in);
92 if (ableToOpen == 1) {
93 mothurOut(fastaFileNames[i] + " will be disregarded."); mothurOutEndLine();
94 //erase from file list
95 fastaFileNames.erase(fastaFileNames.begin()+i);
101 //make sure there is at least one valid file left
102 if (fastaFileNames.size() == 0) { mothurOut("no valid files."); mothurOutEndLine(); abort = true; }
106 taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true);
107 if (taxonomyFileName == "not found") {
108 mothurOut("taxonomy is a required parameter for the classify.seqs command.");
112 else if (taxonomyFileName == "not open") { abort = true; }
115 namefile = validParameter.validFile(parameters, "name", false);
116 if (namefile == "not found") { namefile = ""; }
119 splitAtDash(namefile, namefileNames);
121 //go through files and make sure they are good, if not, then disregard them
122 for (int i = 0; i < namefileNames.size(); i++) {
123 if (inputDir != "") {
124 string path = hasPath(namefileNames[i]);
125 //if the user has not given a path then, add inputdir. else leave path alone.
126 if (path == "") { namefileNames[i] = inputDir + namefileNames[i]; }
131 ableToOpen = openInputFile(namefileNames[i], in);
132 if (ableToOpen == 1) { mothurOut("Unable to match name file with fasta file."); mothurOutEndLine(); abort = true; }
137 if (namefile != "") {
138 if (namefileNames.size() != fastaFileNames.size()) { abort = true; mothurOut("If you provide a name file, you must have one for each fasta file."); mothurOutEndLine(); }
141 //check for optional parameter and set defaults
142 // ...at some point should added some additional type checking...
144 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found"){ temp = "8"; }
145 convert(temp, kmerSize);
147 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
148 convert(temp, processors);
150 search = validParameter.validFile(parameters, "search", false); if (search == "not found"){ search = "kmer"; }
152 method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "bayesian"; }
154 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
155 convert(temp, match);
157 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
158 convert(temp, misMatch);
160 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
161 convert(temp, gapOpen);
163 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
164 convert(temp, gapExtend);
166 temp = validParameter.validFile(parameters, "numwanted", false); if (temp == "not found"){ temp = "10"; }
167 convert(temp, numWanted);
169 temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found"){ temp = "0"; }
170 convert(temp, cutoff);
172 temp = validParameter.validFile(parameters, "probs", false); if (temp == "not found"){ temp = "true"; }
173 probs = isTrue(temp);
175 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "100"; }
176 convert(temp, iters);
180 if ((method == "bayesian") && (search != "kmer")) {
181 mothurOut("The bayesian method requires the kmer search." + search + "will be disregarded." ); mothurOutEndLine();
187 catch(exception& e) {
188 errorOut(e, "ClassifySeqsCommand", "ClassifySeqsCommand");
193 //**********************************************************************************************************************
195 ClassifySeqsCommand::~ClassifySeqsCommand(){
197 if (abort == false) {
198 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
202 //**********************************************************************************************************************
204 void ClassifySeqsCommand::help(){
206 mothurOut("The classify.seqs command reads a fasta file containing sequences and creates a .taxonomy file and a .tax.summary file.\n");
207 mothurOut("The classify.seqs command parameters are template, fasta, search, ksize, method, taxonomy, processors, match, mismatch, gapopen, gapextend, numwanted and probs.\n");
208 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");
209 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");
210 mothurOut("The method parameter allows you to specify classification method to use. Your options are: bayesian and knn. The default is bayesian.\n");
211 mothurOut("The ksize parameter allows you to specify the kmer size for finding most similar template to candidate. The default is 8.\n");
212 mothurOut("The processors parameter allows you to specify the number of processors to use. The default is 1.\n");
213 mothurOut("The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n");
214 mothurOut("The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n");
215 mothurOut("The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -2.0.\n");
216 mothurOut("The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -1.0.\n");
217 mothurOut("The numwanted parameter allows you to specify the number of sequence matches you want with the knn method. The default is 10.\n");
218 mothurOut("The cutoff parameter allows you to specify a bootstrap confidence threshold for your taxonomy. The default is 0.\n");
219 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");
220 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");
221 mothurOut("The classify.seqs command should be in the following format: \n");
222 mothurOut("classify.seqs(template=yourTemplateFile, fasta=yourFastaFile, method=yourClassificationMethod, search=yourSearchmethod, ksize=yourKmerSize, taxonomy=yourTaxonomyFile, processors=yourProcessors) \n");
223 mothurOut("Example classify.seqs(fasta=amazon.fasta, template=core.filtered, method=knn, search=gotoh, ksize=8, processors=2)\n");
224 mothurOut("The .taxonomy file consists of 2 columns: 1 = your sequence name, 2 = the taxonomy for your sequence. \n");
225 mothurOut("The .tax.summary is a summary of the different taxonomies represented in your fasta file. \n");
226 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
228 catch(exception& e) {
229 errorOut(e, "ClassifySeqsCommand", "help");
235 //**********************************************************************************************************************
237 int ClassifySeqsCommand::execute(){
239 if (abort == true) { return 0; }
241 if(method == "bayesian"){ classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters); }
242 else if(method == "knn"){ classify = new Knn(taxonomyFileName, templateFileName, search, kmerSize, gapOpen, gapExtend, match, misMatch, numWanted); }
244 mothurOut(search + " is not a valid method option. I will run the command using bayesian.");
246 classify = new Bayesian(taxonomyFileName, templateFileName, search, kmerSize, cutoff, iters);
250 for (int s = 0; s < fastaFileNames.size(); s++) {
254 nameMap.clear(); //remove old names
257 openInputFile(namefileNames[s], inNames);
259 string firstCol, secondCol;
260 while(!inNames.eof()) {
261 inNames >> firstCol >> secondCol; gobble(inNames);
262 nameMap[firstCol] = getNumNames(secondCol); //ex. seq1 seq1,seq3,seq5 -> seq1 = 3.
267 mothurOut("Classifying sequences from " + fastaFileNames[s] + " ..." ); mothurOutEndLine();
269 if (outputDir == "") { outputDir += hasPath(fastaFileNames[s]); }
270 string newTaxonomyFile = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + getRootName(getSimpleName(taxonomyFileName)) + "taxonomy";
271 string tempTaxonomyFile = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + "taxonomy.temp";
272 string taxSummary = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + getRootName(getSimpleName(taxonomyFileName)) + "tax.summary";
274 int start = time(NULL);
275 int numFastaSeqs = 0;
276 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
278 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
281 openInputFile(fastaFileNames[s], inFASTA);
282 numFastaSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
285 lines.push_back(new linePair(0, numFastaSeqs));
287 driver(lines[0], newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
290 vector<int> positions;
291 processIDS.resize(0);
294 openInputFile(fastaFileNames[s], inFASTA);
297 while(!inFASTA.eof()){
298 input = getline(inFASTA);
299 if (input.length() != 0) {
300 if(input[0] == '>'){ int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
305 numFastaSeqs = positions.size();
307 int numSeqsPerProcessor = numFastaSeqs / processors;
309 for (int i = 0; i < processors; i++) {
310 int startPos = positions[ i * numSeqsPerProcessor ];
311 if(i == processors - 1){
312 numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor;
314 lines.push_back(new linePair(startPos, numSeqsPerProcessor));
316 createProcesses(newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
318 rename((newTaxonomyFile + toString(processIDS[0]) + ".temp").c_str(), newTaxonomyFile.c_str());
319 rename((tempTaxonomyFile + toString(processIDS[0]) + ".temp").c_str(), tempTaxonomyFile.c_str());
321 for(int i=1;i<processors;i++){
322 appendTaxFiles((newTaxonomyFile + toString(processIDS[i]) + ".temp"), newTaxonomyFile);
323 appendTaxFiles((tempTaxonomyFile + toString(processIDS[i]) + ".temp"), tempTaxonomyFile);
324 remove((newTaxonomyFile + toString(processIDS[i]) + ".temp").c_str());
325 remove((tempTaxonomyFile + toString(processIDS[i]) + ".temp").c_str());
331 openInputFile(fastaFileNames[s], inFASTA);
332 numFastaSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
335 lines.push_back(new linePair(0, numFastaSeqs));
337 driver(lines[0], newTaxonomyFile, tempTaxonomyFile, fastaFileNames[s]);
339 //make taxonomy tree from new taxonomy file
340 PhyloTree taxaBrowser;
343 openInputFile(tempTaxonomyFile, in);
345 //read in users taxonomy file and add sequences to tree
348 in >> name >> taxon; gobble(in);
350 if (namefile != "") {
351 itNames = nameMap.find(name);
353 if (itNames == nameMap.end()) {
354 mothurOut(name + " is not in your name file please correct."); mothurOutEndLine(); exit(1);
356 for (int i = 0; i < itNames->second; i++) {
357 taxaBrowser.addSeqToTree(name+toString(i), taxon); //add it as many times as there are identical seqs
360 }else { taxaBrowser.addSeqToTree(name, taxon); } //add it once
364 taxaBrowser.assignHeirarchyIDs(0);
366 taxaBrowser.binUnclassified();
368 remove(tempTaxonomyFile.c_str());
372 openOutputFile(taxSummary, outTaxTree);
373 taxaBrowser.print(outTaxTree);
376 //output taxonomy with the unclassified bins added
378 openInputFile(newTaxonomyFile, inTax);
381 string unclass = newTaxonomyFile + ".unclass.temp";
382 openOutputFile(unclass, outTax);
384 //get maxLevel from phylotree so you know how many 'unclassified's to add
385 int maxLevel = taxaBrowser.getMaxLevel();
387 //read taxfile - this reading and rewriting is done to preserve the confidence sscores.
388 while (!inTax.eof()) {
389 inTax >> name >> taxon; gobble(inTax);
391 string newTax = addUnclassifieds(taxon, maxLevel);
393 outTax << name << '\t' << newTax << endl;
398 remove(newTaxonomyFile.c_str());
399 rename(unclass.c_str(), newTaxonomyFile.c_str());
402 mothurOut("It took " + toString(time(NULL) - start) + " secs to classify " + toString(numFastaSeqs) + " sequences."); mothurOutEndLine(); mothurOutEndLine();
408 catch(exception& e) {
409 errorOut(e, "ClassifySeqsCommand", "execute");
414 /**************************************************************************************************/
415 string ClassifySeqsCommand::addUnclassifieds(string tax, int maxlevel) {
417 string newTax, taxon;
420 //keep what you have counting the levels
421 while (tax.find_first_of(';') != -1) {
423 taxon = tax.substr(0,tax.find_first_of(';'))+';';
424 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
429 //add "unclassified" until you reach maxLevel
430 while (level < maxlevel) {
431 newTax += "unclassified;";
437 catch(exception& e) {
438 errorOut(e, "ClassifySeqsCommand", "addUnclassifieds");
443 /**************************************************************************************************/
445 void ClassifySeqsCommand::createProcesses(string taxFileName, string tempTaxFile, string filename) {
447 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
449 // processIDS.resize(0);
451 //loop through and create all the processes you want
452 while (process != processors) {
456 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
459 driver(lines[process], taxFileName + toString(getpid()) + ".temp", tempTaxFile + toString(getpid()) + ".temp", filename);
461 }else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); }
464 //force parent to wait until all the processes are done
465 for (int i=0;i<processors;i++) {
466 int temp = processIDS[i];
471 catch(exception& e) {
472 errorOut(e, "ClassifySeqsCommand", "createProcesses");
476 /**************************************************************************************************/
478 void ClassifySeqsCommand::appendTaxFiles(string temp, string filename) {
483 openOutputFileAppend(filename, output);
484 openInputFile(temp, input);
486 while(char c = input.get()){
487 if(input.eof()) { break; }
488 else { output << c; }
494 catch(exception& e) {
495 errorOut(e, "ClassifySeqsCommand", "appendTaxFiles");
500 //**********************************************************************************************************************
502 int ClassifySeqsCommand::driver(linePair* line, string taxFName, string tempTFName, string filename){
505 openOutputFile(taxFName, outTax);
507 ofstream outTaxSimple;
508 openOutputFile(tempTFName, outTaxSimple);
511 openInputFile(filename, inFASTA);
513 inFASTA.seekg(line->start);
517 for(int i=0;i<line->numSeqs;i++){
519 Sequence* candidateSeq = new Sequence(inFASTA);
521 if (candidateSeq->getName() != "") {
522 taxonomy = classify->getTaxonomy(candidateSeq);
524 if (taxonomy != "bad seq") {
525 //output confidence scores or not
527 outTax << candidateSeq->getName() << '\t' << taxonomy << endl;
529 outTax << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
530 cout << classify->getSimpleTax() << endl;
533 outTaxSimple << candidateSeq->getName() << '\t' << classify->getSimpleTax() << endl;
538 if((i+1) % 100 == 0){
539 mothurOut("Classifying sequence " + toString(i+1)); mothurOutEndLine();
545 outTaxSimple.close();
549 catch(exception& e) {
550 errorOut(e, "ClassifySeqsCommand", "driver");
555 /**************************************************************************************************/