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