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