2 * taxonomyequalizer.cpp
5 * Created by westcott on 11/20/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "taxonomyequalizer.h"
12 /**************************************************************************************************/
13 TaxEqualizer::TaxEqualizer(string tfile, int c, string o) : cutoff(c), outputDir(o) {
15 m = MothurOut::getInstance();
16 containsConfidence = false;
19 openInputFile(tfile, inTax);
21 highestLevel = getHighestLevel(inTax);
23 if (!m->control_pressed) {
25 //if the user has specified a cutoff and it's smaller than the highest level
26 if ((cutoff != -1) && (cutoff < highestLevel)) {
27 highestLevel = cutoff;
28 }else if (cutoff > highestLevel) {
29 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));
30 m->mothurOutEndLine();
35 openInputFile(tfile, in);
38 equalizedFile = outputDir + getRootName(getSimpleName(tfile)) + "equalized.taxonomy";
39 openOutputFile(equalizedFile, out);
45 if (m->control_pressed) { break; }
47 in >> name >> tax; gobble(in);
49 if (containsConfidence) { removeConfidences(tax); }
51 //is this a taxonomy that needs to be extended?
52 if (seqLevels[name] < highestLevel) {
53 extendTaxonomy(name, tax, highestLevel);
54 }else if (seqLevels[name] > highestLevel) { //this can happen if the user enters a cutoff
55 truncateTaxonomy(name, tax, highestLevel);
58 out << name << '\t' << tax << endl;
64 if (m->control_pressed) { remove(equalizedFile.c_str()); }
65 }else { inTax.close(); }
69 m->errorOut(e, "TaxEqualizer", "TaxEqualizer");
73 /**************************************************************************************************/
74 int TaxEqualizer::getHighestLevel(ifstream& in) {
81 in >> name >> tax; gobble(in);
83 //count levels in this taxonomy
85 for (int i = 0; i < tax.length(); i++) {
86 if (tax[i] == ';') { thisLevel++; }
89 //save sequences level
90 seqLevels[name] = thisLevel;
92 //is this the longest taxonomy?
93 if (thisLevel > level) {
95 testTax = tax; //testTax is used to figure out if this file has confidences we need to strip out
99 int pos = testTax.find_first_of('(');
101 //if there are '(' then there are confidences we need to take out
102 if (pos != -1) { containsConfidence = true; }
107 catch(exception& e) {
108 m->errorOut(e, "TaxEqualizer", "getHighestLevel");
112 /**************************************************************************************************/
113 void TaxEqualizer::extendTaxonomy(string name, string& tax, int desiredLevel) {
117 tax = tax.substr(0, tax.length()-1); //take off final ";"
118 int pos = tax.find_last_of(';');
119 string lastTaxon = tax.substr(pos+1);
120 lastTaxon += ";"; //add back on delimiting char
123 int currentLevel = seqLevels[name];
125 //added last taxon until you get desired level
126 for (int i = currentLevel; i < desiredLevel; i++) {
130 catch(exception& e) {
131 m->errorOut(e, "TaxEqualizer", "extendTaxonomy");
135 /**************************************************************************************************/
136 void TaxEqualizer::truncateTaxonomy(string name, string& tax, int desiredLevel) {
139 int currentLevel = seqLevels[name];
140 tax = tax.substr(0, tax.length()-1); //take off final ";"
142 //remove a taxon until you get to desired level
143 for (int i = currentLevel; i > desiredLevel; i--) {
144 tax = tax.substr(0, tax.find_last_of(';'));
149 catch(exception& e) {
150 m->errorOut(e, "TaxEqualizer", "truncateTaxonomy");
154 /**************************************************************************************************/
155 void TaxEqualizer::removeConfidences(string& tax) {
161 while (tax.find_first_of(';') != -1) {
163 taxon = tax.substr(0,tax.find_first_of(';'));
164 taxon = taxon.substr(0, taxon.find_first_of('(')); //rip off confidence
167 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
173 catch(exception& e) {
174 m->errorOut(e, "TaxEqualizer", "removeConfidences");
179 /**************************************************************************************************/