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)
14 : Classify(), num(n) {
16 //create search database and names vector
17 generateDatabaseAndNames(tfile, tempFile, method, kmerSize, gapOpen, gapExtend, match, misMatch);
20 m->errorOut(e, "Knn", "Knn");
24 /**************************************************************************************************/
25 string Knn::getTaxonomy(Sequence* seq) {
29 //use database to find closest seq
30 vector<int> closest = database->findClosestSequences(seq, num);
32 if (m->control_pressed) { return tax; }
34 vector<string> closestNames;
35 for (int i = 0; i < closest.size(); i++) {
36 //find that sequences taxonomy in map
37 it = taxonomy.find(names[closest[i]]);
39 //is this sequence in the taxonomy file
40 if (it == taxonomy.end()) { //error not in file
41 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();
42 }else{ closestNames.push_back(it->first); }
45 if (closestNames.size() == 0) {
46 m->mothurOut("Error: All the matches for sequence " + seq->getName() + " have been eliminated. " + seq->getName() + " will be disregarded."); m->mothurOutEndLine();
49 tax = findCommonTaxonomy(closestNames);
50 if (tax == "") { m->mothurOut("There are no common levels for sequence " + seq->getName() + ". " + seq->getName() + " will be disregarded."); m->mothurOutEndLine(); tax = "bad seq"; }
57 m->errorOut(e, "Knn", "getTaxonomy");
61 /**************************************************************************************************/
62 string Knn::findCommonTaxonomy(vector<string> closest) {
64 vector< vector<string> > taxons; //taxon[0] = vector of taxonomy info for closest[0].
65 //so if closest[0] taxonomy is Bacteria;Alphaproteobacteria;Rhizobiales;Azorhizobium_et_rel.;Methylobacterium_et_rel.;Bosea;
66 //taxon[0][0] = Bacteria, taxon[0][1] = Alphaproteobacteria....
68 taxons.resize(closest.size());
71 for (int i = 0; i < closest.size(); i++) {
72 if (m->control_pressed) { return "control"; }
74 string tax = taxonomy[closest[i]]; //we know its there since we checked in getTaxonomy
76 taxons[i] = parseTax(tax);
78 //figure out who has the shortest taxonomy info. so you can start comparing there
79 if (taxons[i].size() < smallest) {
80 smallest = taxons[i].size();
84 //start at the highest level all the closest seqs have
86 for (int i = (smallest-1); i >= 0; i--) {
87 if (m->control_pressed) { return "control"; }
89 string thistax = taxons[0][i];
91 for (int j = 1; j < taxons.size(); j++) {
92 if (taxons[j][i] != thistax) { break; }
96 if (num == (taxons.size()-1)) { //they all match at this level
97 for (int k = 0; k <= i; k++) {
98 common += taxons[0][k] + ';';
106 catch(exception& e) {
107 m->errorOut(e, "Knn", "findCommonTaxonomy");
111 /**************************************************************************************************/