]> git.donarmstrong.com Git - mothur.git/blob - knn.cpp
made classifier faster
[mothur.git] / knn.cpp
1 /*
2  *  knn.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 11/4/09.
6  *  Copyright 2009 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "knn.h"
11
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)  {
15         try {
16                 //create search database and names vector
17                 generateDatabaseAndNames(tfile, tempFile, method, kmerSize, gapOpen, gapExtend, match, misMatch);
18         }
19         catch(exception& e) {
20                 m->errorOut(e, "Knn", "Knn");
21                 exit(1);
22         }
23 }
24 /**************************************************************************************************/
25 string Knn::getTaxonomy(Sequence* seq) {
26         try {
27                 string tax;
28                 
29                 //use database to find closest seq
30                 vector<int> closest = database->findClosestSequences(seq, num);
31                 
32                 if (m->control_pressed) { return tax; }
33
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]]);
38                 
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);     }
43                 }
44                 
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();
47                         tax = "bad seq";
48                 }else{
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"; }
51                 }
52                 
53                 simpleTax = tax;
54                 return tax;     
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "Knn", "getTaxonomy");
58                 exit(1);
59         }
60 }
61 /**************************************************************************************************/
62 string Knn::findCommonTaxonomy(vector<string> closest)  {
63         try {
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....
67                                                                                 
68                 taxons.resize(closest.size());
69                 int smallest = 100;
70                 
71                 for (int i = 0; i < closest.size(); i++) {
72                         if (m->control_pressed) { return "control"; }
73                 
74                         string tax = taxonomy[closest[i]];  //we know its there since we checked in getTaxonomy
75                 
76                         taxons[i] = parseTax(tax);
77                 
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();
81                         }
82                 }
83         
84                 //start at the highest level all the closest seqs have
85                 string common = "";
86                 for (int i = (smallest-1); i >= 0; i--) {
87                         if (m->control_pressed) { return "control"; }
88
89                         string thistax = taxons[0][i];
90                         int num = 0;
91                         for (int j = 1; j < taxons.size(); j++) {
92                                 if (taxons[j][i] != thistax) { break; }
93                                 num = j;
94                         }
95                 
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] + ';';
99                                 }
100                                 break;
101                         }
102                 }
103         
104                 return common;
105         }
106         catch(exception& e) {
107                 m->errorOut(e, "Knn", "findCommonTaxonomy");
108                 exit(1);
109         }
110 }
111 /**************************************************************************************************/
112