X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=knn.cpp;h=1835c48f802b861f29a6a6c2360a47788d13bc46;hb=0bcfddf7bc721a334bdae42d86a580019303537d;hp=0a62e743b0dd8d4f5435d504127a70724f9f27a8;hpb=7b3c9ca940891c1b20b3b7ec13e05d7e7b316b63;p=mothur.git diff --git a/knn.cpp b/knn.cpp index 0a62e74..1835c48 100644 --- a/knn.cpp +++ b/knn.cpp @@ -10,8 +10,42 @@ #include "knn.h" /**************************************************************************************************/ -Knn::Knn(string tfile, string tempFile, string method, int kmerSize, int gapOpen, int gapExtend, int match, int misMatch, int n) -: Classify(tfile, tempFile, method, kmerSize, gapOpen, gapExtend, match, misMatch), num(n) {} +Knn::Knn(string tfile, string tempFile, string method, int kmerSize, float gapOpen, float gapExtend, float match, float misMatch, int n) +: Classify(), num(n), search(method) { + try { + //create search database and names vector + generateDatabaseAndNames(tfile, tempFile, method, kmerSize, gapOpen, gapExtend, match, misMatch); + } + catch(exception& e) { + m->errorOut(e, "Knn", "Knn"); + exit(1); + } +} +/**************************************************************************************************/ +void Knn::setDistName(string s) { + try { + outDistName = s; + ofstream outDistance; + m->openOutputFile(outDistName, outDistance); + outDistance << "Name\tBestMatch\tDistance" << endl; + outDistance.close(); + } + catch(exception& e) { + m->errorOut(e, "Knn", "setDistName"); + exit(1); + } +} +/**************************************************************************************************/ +Knn::~Knn() { + try { + delete phyloTree; + if (database != NULL) { delete database; } + } + catch(exception& e) { + m->errorOut(e, "Knn", "~Knn"); + exit(1); + } +} /**************************************************************************************************/ string Knn::getTaxonomy(Sequence* seq) { try { @@ -19,7 +53,11 @@ string Knn::getTaxonomy(Sequence* seq) { //use database to find closest seq vector closest = database->findClosestSequences(seq, num); - + + if (search == "distance") { ofstream outDistance; m->openOutputFileAppend(outDistName, outDistance); outDistance << seq->getName() << '\t' << database->getName(closest[0]) << '\t' << database->getSearchScore() << endl; outDistance.close(); } + + if (m->control_pressed) { return tax; } + vector closestNames; for (int i = 0; i < closest.size(); i++) { //find that sequences taxonomy in map @@ -27,22 +65,23 @@ string Knn::getTaxonomy(Sequence* seq) { //is this sequence in the taxonomy file if (it == taxonomy.end()) { //error not in file - mothurOut("Error: sequence " + names[closest[i]] + " is not in the taxonomy file. It will be eliminated as a match to sequence " + seq->getName() + "."); mothurOutEndLine(); + 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(); }else{ closestNames.push_back(it->first); } } if (closestNames.size() == 0) { - mothurOut("Error: All the matches for sequence " + seq->getName() + " have been eliminated. " + seq->getName() + " will be disregarded."); mothurOutEndLine(); + m->mothurOut("Error: All the matches for sequence " + seq->getName() + " have been eliminated. " + seq->getName() + " will be disregarded."); m->mothurOutEndLine(); tax = "bad seq"; }else{ tax = findCommonTaxonomy(closestNames); - if (tax == "") { mothurOut("There are no common levels for sequence " + seq->getName() + ". " + seq->getName() + " will be disregarded."); mothurOutEndLine(); tax = "bad seq"; } + if (tax == "") { m->mothurOut("There are no common levels for sequence " + seq->getName() + ". " + seq->getName() + " will be disregarded."); m->mothurOutEndLine(); tax = "bad seq"; } } + simpleTax = tax; return tax; } catch(exception& e) { - errorOut(e, "Knn", "getTaxonomy"); + m->errorOut(e, "Knn", "getTaxonomy"); exit(1); } } @@ -57,6 +96,7 @@ string Knn::findCommonTaxonomy(vector closest) { int smallest = 100; for (int i = 0; i < closest.size(); i++) { + if (m->control_pressed) { return "control"; } string tax = taxonomy[closest[i]]; //we know its there since we checked in getTaxonomy @@ -71,6 +111,7 @@ string Knn::findCommonTaxonomy(vector closest) { //start at the highest level all the closest seqs have string common = ""; for (int i = (smallest-1); i >= 0; i--) { + if (m->control_pressed) { return "control"; } string thistax = taxons[0][i]; int num = 0; @@ -90,7 +131,7 @@ string Knn::findCommonTaxonomy(vector closest) { return common; } catch(exception& e) { - errorOut(e, "Knn", "findCommonTaxonomy"); + m->errorOut(e, "Knn", "findCommonTaxonomy"); exit(1); } }