]> git.donarmstrong.com Git - mothur.git/blob - knn.cpp
c11626a082ac9003785fd937e87b922e3bf8025a
[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(tfile, tempFile, method, kmerSize, gapOpen, gapExtend, match, misMatch), num(n)  {}
15 /**************************************************************************************************/
16 string Knn::getTaxonomy(Sequence* seq) {
17         try {
18                 string tax;
19                 
20                 //use database to find closest seq
21
22                 vector<int> closest = database->findClosestSequences(seq, num);
23
24                 vector<string> closestNames;
25                 for (int i = 0; i < closest.size(); i++) {
26                         //find that sequences taxonomy in map
27                         it = taxonomy.find(names[closest[i]]);
28                 
29                         //is this sequence in the taxonomy file
30                         if (it == taxonomy.end()) { //error not in file
31                                 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();
32                         }else{   closestNames.push_back(it->first);     }
33                 }
34                 
35                 if (closestNames.size() == 0) {
36                         m->mothurOut("Error: All the matches for sequence " + seq->getName() + " have been eliminated. " + seq->getName() + " will be disregarded."); m->mothurOutEndLine();
37                         tax = "bad seq";
38                 }else{
39                         tax = findCommonTaxonomy(closestNames);
40                         if (tax == "") { m->mothurOut("There are no common levels for sequence " + seq->getName() + ". " + seq->getName() + " will be disregarded."); m->mothurOutEndLine(); tax = "bad seq"; }
41                 }
42                 
43                 simpleTax = tax;
44                 return tax;     
45         }
46         catch(exception& e) {
47                 m->errorOut(e, "Knn", "getTaxonomy");
48                 exit(1);
49         }
50 }
51 /**************************************************************************************************/
52 string Knn::findCommonTaxonomy(vector<string> closest)  {
53         try {
54                 vector< vector<string> > taxons;  //taxon[0] = vector of taxonomy info for closest[0].
55                                                                                 //so if closest[0] taxonomy is Bacteria;Alphaproteobacteria;Rhizobiales;Azorhizobium_et_rel.;Methylobacterium_et_rel.;Bosea;
56                                                                                 //taxon[0][0] = Bacteria, taxon[0][1] = Alphaproteobacteria....
57                                                                                 
58                 taxons.resize(closest.size());
59                 int smallest = 100;
60                 
61                 for (int i = 0; i < closest.size(); i++) {
62                 
63                         string tax = taxonomy[closest[i]];  //we know its there since we checked in getTaxonomy
64                 
65                         taxons[i] = parseTax(tax);
66                 
67                         //figure out who has the shortest taxonomy info. so you can start comparing there
68                         if (taxons[i].size() < smallest) {
69                                 smallest = taxons[i].size();
70                         }
71                 }
72         
73                 //start at the highest level all the closest seqs have
74                 string common = "";
75                 for (int i = (smallest-1); i >= 0; i--) {
76
77                         string thistax = taxons[0][i];
78                         int num = 0;
79                         for (int j = 1; j < taxons.size(); j++) {
80                                 if (taxons[j][i] != thistax) { break; }
81                                 num = j;
82                         }
83                 
84                         if (num == (taxons.size()-1)) { //they all match at this level
85                                 for (int k = 0; k <= i; k++) {
86                                         common += taxons[0][k] + ';';
87                                 }
88                                 break;
89                         }
90                 }
91         
92                 return common;
93         }
94         catch(exception& e) {
95                 m->errorOut(e, "Knn", "findCommonTaxonomy");
96                 exit(1);
97         }
98 }
99 /**************************************************************************************************/
100