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) : cutoff(c) {
15 containsConfidence = false;
18 openInputFile(tfile, inTax);
20 highestLevel = getHighestLevel(inTax);
22 //if the user has specified a cutoff and it's smaller than the highest level
23 if ((cutoff != -1) && (cutoff < highestLevel)) {
24 highestLevel = cutoff;
25 }else if (cutoff > highestLevel) {
26 mothurOut("The highest level taxonomy you have is " + toString(highestLevel) + " and your cutoff is " + toString(cutoff) + ". I will set the cutoff to " + toString(highestLevel));
32 openInputFile(tfile, in);
35 equalizedFile = getRootName(tfile) + "equalized.taxonomy";
36 openOutputFile(equalizedFile, out);
40 in >> name >> tax; gobble(in);
42 if (containsConfidence) { removeConfidences(tax); }
44 //is this a taxonomy that needs to be extended?
45 if (seqLevels[name] < highestLevel) {
46 extendTaxonomy(name, tax, highestLevel);
47 }else if (seqLevels[name] > highestLevel) { //this can happen if the user enters a cutoff
48 truncateTaxonomy(name, tax, highestLevel);
51 out << name << '\t' << tax << endl;
59 errorOut(e, "TaxEqualizer", "TaxEqualizer");
63 /**************************************************************************************************/
64 int TaxEqualizer::getHighestLevel(ifstream& in) {
71 in >> name >> tax; gobble(in);
73 //count levels in this taxonomy
75 for (int i = 0; i < tax.length(); i++) {
76 if (tax[i] == ';') { thisLevel++; }
79 //save sequences level
80 seqLevels[name] = thisLevel;
82 //is this the longest taxonomy?
83 if (thisLevel > level) {
85 testTax = tax; //testTax is used to figure out if this file has confidences we need to strip out
89 int pos = testTax.find_first_of('(');
91 //if there are '(' then there are confidences we need to take out
92 if (pos != -1) { containsConfidence = true; }
98 errorOut(e, "TaxEqualizer", "getHighestLevel");
102 /**************************************************************************************************/
103 void TaxEqualizer::extendTaxonomy(string name, string& tax, int desiredLevel) {
107 tax = tax.substr(0, tax.length()-1); //take off final ";"
108 int pos = tax.find_last_of(';');
109 string lastTaxon = tax.substr(pos+1);
110 lastTaxon += ";"; //add back on delimiting char
113 int currentLevel = seqLevels[name];
115 //added last taxon until you get desired level
116 for (int i = currentLevel; i < desiredLevel; i++) {
120 catch(exception& e) {
121 errorOut(e, "TaxEqualizer", "extendTaxonomy");
125 /**************************************************************************************************/
126 void TaxEqualizer::truncateTaxonomy(string name, string& tax, int desiredLevel) {
129 int currentLevel = seqLevels[name];
130 tax = tax.substr(0, tax.length()-1); //take off final ";"
132 //remove a taxon until you get to desired level
133 for (int i = currentLevel; i > desiredLevel; i--) {
134 tax = tax.substr(0, tax.find_last_of(';'));
139 catch(exception& e) {
140 errorOut(e, "TaxEqualizer", "truncateTaxonomy");
144 /**************************************************************************************************/
145 void TaxEqualizer::removeConfidences(string& tax) {
151 while (tax.find_first_of(';') != -1) {
153 taxon = tax.substr(0,tax.find_first_of(';'));
154 taxon = taxon.substr(0, taxon.find_first_of('(')); //rip off confidence
157 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
163 catch(exception& e) {
164 errorOut(e, "TaxEqualizer", "removeConfidences");
169 /**************************************************************************************************/