5 * Created by westcott on 11/4/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
12 /**************************************************************************************************/
13 Knn::Knn(string tfile, string tempFile, string method, int kmerSize, float gapOpen, float gapExtend, float match, float misMatch, int n, int tid)
14 : Classify(), num(n), search(method) {
18 //create search database and names vector
19 generateDatabaseAndNames(tfile, tempFile, method, kmerSize, gapOpen, gapExtend, match, misMatch);
22 m->errorOut(e, "Knn", "Knn");
26 /**************************************************************************************************/
27 void Knn::setDistName(string s) {
31 m->openOutputFile(outDistName, outDistance);
32 outDistance << "Name\tBestMatch\tDistance" << endl;
36 m->errorOut(e, "Knn", "setDistName");
40 /**************************************************************************************************/
44 if (database != NULL) { delete database; }
47 m->errorOut(e, "Knn", "~Knn");
51 /**************************************************************************************************/
52 string Knn::getTaxonomy(Sequence* seq) {
56 //use database to find closest seq
57 vector<int> closest = database->findClosestSequences(seq, num);
59 if (search == "distance") { ofstream outDistance; m->openOutputFileAppend(outDistName, outDistance); outDistance << seq->getName() << '\t' << database->getName(closest[0]) << '\t' << database->getSearchScore() << endl; outDistance.close(); }
61 if (m->control_pressed) { return tax; }
63 vector<string> closestNames;
64 for (int i = 0; i < closest.size(); i++) {
65 //find that sequences taxonomy in map
66 it = taxonomy.find(names[closest[i]]);
68 //is this sequence in the taxonomy file
69 if (it == taxonomy.end()) { //error not in file
70 m->mothurOut("Error: sequence " + names[closest[i]] + " is not in the taxonomy file. It will be eliminated as a match to sequence " + seq->getName() + "."); m->mothurOutEndLine();
71 }else{ closestNames.push_back(it->first); }
74 if (closestNames.size() == 0) {
75 m->mothurOut("Error: All the matches for sequence " + seq->getName() + " have been eliminated. " + seq->getName() + " will be disregarded."); m->mothurOutEndLine();
78 tax = findCommonTaxonomy(closestNames);
79 if (tax == "") { m->mothurOut("There are no common levels for sequence " + seq->getName() + ". " + seq->getName() + " will be disregarded."); m->mothurOutEndLine(); tax = "bad seq"; }
86 m->errorOut(e, "Knn", "getTaxonomy");
90 /**************************************************************************************************/
91 string Knn::findCommonTaxonomy(vector<string> closest) {
93 vector< vector<string> > taxons; //taxon[0] = vector of taxonomy info for closest[0].
94 //so if closest[0] taxonomy is Bacteria;Alphaproteobacteria;Rhizobiales;Azorhizobium_et_rel.;Methylobacterium_et_rel.;Bosea;
95 //taxon[0][0] = Bacteria, taxon[0][1] = Alphaproteobacteria....
97 taxons.resize(closest.size());
100 for (int i = 0; i < closest.size(); i++) {
101 if (m->control_pressed) { return "control"; }
103 string tax = taxonomy[closest[i]]; //we know its there since we checked in getTaxonomy
105 taxons[i] = parseTax(tax);
107 //figure out who has the shortest taxonomy info. so you can start comparing there
108 if (taxons[i].size() < smallest) {
109 smallest = taxons[i].size();
113 //start at the highest level all the closest seqs have
115 for (int i = (smallest-1); i >= 0; i--) {
116 if (m->control_pressed) { return "control"; }
118 string thistax = taxons[0][i];
120 for (int j = 1; j < taxons.size(); j++) {
121 if (taxons[j][i] != thistax) { break; }
125 if (num == (taxons.size()-1)) { //they all match at this level
126 for (int k = 0; k <= i; k++) {
127 common += taxons[0][k] + ';';
135 catch(exception& e) {
136 m->errorOut(e, "Knn", "findCommonTaxonomy");
140 /**************************************************************************************************/