X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=taxonomyequalizer.cpp;h=c0507261eaf2c96c9381172a646317c7cb6a1db3;hp=f3ccfe45852ba8065c454d4bf42a0ad83a75d088;hb=d1c97b8c04bb75faca1e76ffad60b37a4d789d3d;hpb=74844a60d80c6dd06e3fb02ee9b928424f9019b0 diff --git a/taxonomyequalizer.cpp b/taxonomyequalizer.cpp index f3ccfe4..c050726 100644 --- a/taxonomyequalizer.cpp +++ b/taxonomyequalizer.cpp @@ -10,51 +10,60 @@ #include "taxonomyequalizer.h" /**************************************************************************************************/ -TaxEqualizer::TaxEqualizer(string tfile, int c) : cutoff(c) { +TaxEqualizer::TaxEqualizer(string tfile, int c, string o) : cutoff(c), outputDir(o) { try { m = MothurOut::getInstance(); containsConfidence = false; ifstream inTax; - openInputFile(tfile, inTax); + m->openInputFile(tfile, inTax); highestLevel = getHighestLevel(inTax); - - //if the user has specified a cutoff and it's smaller than the highest level - if ((cutoff != -1) && (cutoff < highestLevel)) { - highestLevel = cutoff; - }else if (cutoff > highestLevel) { - m->mothurOut("The highest level taxonomy you have is " + toString(highestLevel) + " and your cutoff is " + toString(cutoff) + ". I will set the cutoff to " + toString(highestLevel)); - m->mothurOutEndLine(); - } - - inTax.close(); - ifstream in; - openInputFile(tfile, in); - - ofstream out; - equalizedFile = getRootName(tfile) + "equalized.taxonomy"; - openOutputFile(equalizedFile, out); - string name, tax; - while (in) { - in >> name >> tax; gobble(in); + if (!m->control_pressed) { + + //if the user has specified a cutoff and it's smaller than the highest level + if ((cutoff != -1) && (cutoff < highestLevel)) { + highestLevel = cutoff; + }else if (cutoff > highestLevel) { + m->mothurOut("The highest level taxonomy you have is " + toString(highestLevel) + " and your cutoff is " + toString(cutoff) + ". I will set the cutoff to " + toString(highestLevel)); + m->mothurOutEndLine(); + } - if (containsConfidence) { removeConfidences(tax); } + inTax.close(); + ifstream in; + m->openInputFile(tfile, in); - //is this a taxonomy that needs to be extended? - if (seqLevels[name] < highestLevel) { - extendTaxonomy(name, tax, highestLevel); - }else if (seqLevels[name] > highestLevel) { //this can happen if the user enters a cutoff - truncateTaxonomy(name, tax, highestLevel); + ofstream out; + equalizedFile = outputDir + m->getRootName(m->getSimpleName(tfile)) + "equalized.taxonomy"; + m->openOutputFile(equalizedFile, out); + + + string name, tax; + while (in) { + + if (m->control_pressed) { break; } + + in >> name >> tax; m->gobble(in); + + if (containsConfidence) { m->removeConfidences(tax); } + + //is this a taxonomy that needs to be extended? + if (seqLevels[name] < highestLevel) { + extendTaxonomy(name, tax, highestLevel); + }else if (seqLevels[name] > highestLevel) { //this can happen if the user enters a cutoff + truncateTaxonomy(name, tax, highestLevel); + } + + out << name << '\t' << tax << endl; } - out << name << '\t' << tax << endl; - } + in.close(); + out.close(); + + if (m->control_pressed) { m->mothurRemove(equalizedFile); } + }else { inTax.close(); } - in.close(); - out.close(); - } catch(exception& e) { m->errorOut(e, "TaxEqualizer", "TaxEqualizer"); @@ -69,7 +78,7 @@ int TaxEqualizer::getHighestLevel(ifstream& in) { string name, tax; while (in) { - in >> name >> tax; gobble(in); + in >> name >> tax; m->gobble(in); //count levels in this taxonomy int thisLevel = 0; @@ -79,19 +88,19 @@ int TaxEqualizer::getHighestLevel(ifstream& in) { //save sequences level seqLevels[name] = thisLevel; - + //is this the longest taxonomy? if (thisLevel > level) { level = thisLevel; testTax = tax; //testTax is used to figure out if this file has confidences we need to strip out - } + } } int pos = testTax.find_first_of('('); //if there are '(' then there are confidences we need to take out if (pos != -1) { containsConfidence = true; } - + return level; } @@ -143,29 +152,4 @@ void TaxEqualizer::truncateTaxonomy(string name, string& tax, int desiredLevel) } } /**************************************************************************************************/ -void TaxEqualizer::removeConfidences(string& tax) { - try { - - string taxon; - string newTax = ""; - - while (tax.find_first_of(';') != -1) { - //get taxon - taxon = tax.substr(0,tax.find_first_of(';')); - taxon = taxon.substr(0, taxon.find_first_of('(')); //rip off confidence - taxon += ";"; - - tax = tax.substr(tax.find_first_of(';')+1, tax.length()); - newTax += taxon; - } - - tax = newTax; - } - catch(exception& e) { - m->errorOut(e, "TaxEqualizer", "removeConfidences"); - exit(1); - } -} - -/**************************************************************************************************/