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 m = MothurOut::getInstance();
16 containsConfidence = false;
19 openInputFile(tfile, inTax);
21 highestLevel = getHighestLevel(inTax);
23 //if the user has specified a cutoff and it's smaller than the highest level
24 if ((cutoff != -1) && (cutoff < highestLevel)) {
25 highestLevel = cutoff;
26 }else if (cutoff > highestLevel) {
27 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));
28 m->mothurOutEndLine();
33 openInputFile(tfile, in);
36 equalizedFile = getRootName(tfile) + "equalized.taxonomy";
37 openOutputFile(equalizedFile, out);
41 in >> name >> tax; gobble(in);
43 if (containsConfidence) { removeConfidences(tax); }
45 //is this a taxonomy that needs to be extended?
46 if (seqLevels[name] < highestLevel) {
47 extendTaxonomy(name, tax, highestLevel);
48 }else if (seqLevels[name] > highestLevel) { //this can happen if the user enters a cutoff
49 truncateTaxonomy(name, tax, highestLevel);
52 out << name << '\t' << tax << endl;
60 m->errorOut(e, "TaxEqualizer", "TaxEqualizer");
64 /**************************************************************************************************/
65 int TaxEqualizer::getHighestLevel(ifstream& in) {
72 in >> name >> tax; gobble(in);
74 //count levels in this taxonomy
76 for (int i = 0; i < tax.length(); i++) {
77 if (tax[i] == ';') { thisLevel++; }
80 //save sequences level
81 seqLevels[name] = thisLevel;
83 //is this the longest taxonomy?
84 if (thisLevel > level) {
86 testTax = tax; //testTax is used to figure out if this file has confidences we need to strip out
90 int pos = testTax.find_first_of('(');
92 //if there are '(' then there are confidences we need to take out
93 if (pos != -1) { containsConfidence = true; }
99 m->errorOut(e, "TaxEqualizer", "getHighestLevel");
103 /**************************************************************************************************/
104 void TaxEqualizer::extendTaxonomy(string name, string& tax, int desiredLevel) {
108 tax = tax.substr(0, tax.length()-1); //take off final ";"
109 int pos = tax.find_last_of(';');
110 string lastTaxon = tax.substr(pos+1);
111 lastTaxon += ";"; //add back on delimiting char
114 int currentLevel = seqLevels[name];
116 //added last taxon until you get desired level
117 for (int i = currentLevel; i < desiredLevel; i++) {
121 catch(exception& e) {
122 m->errorOut(e, "TaxEqualizer", "extendTaxonomy");
126 /**************************************************************************************************/
127 void TaxEqualizer::truncateTaxonomy(string name, string& tax, int desiredLevel) {
130 int currentLevel = seqLevels[name];
131 tax = tax.substr(0, tax.length()-1); //take off final ";"
133 //remove a taxon until you get to desired level
134 for (int i = currentLevel; i > desiredLevel; i--) {
135 tax = tax.substr(0, tax.find_last_of(';'));
140 catch(exception& e) {
141 m->errorOut(e, "TaxEqualizer", "truncateTaxonomy");
145 /**************************************************************************************************/
146 void TaxEqualizer::removeConfidences(string& tax) {
152 while (tax.find_first_of(';') != -1) {
154 taxon = tax.substr(0,tax.find_first_of(';'));
155 taxon = taxon.substr(0, taxon.find_first_of('(')); //rip off confidence
158 tax = tax.substr(tax.find_first_of(';')+1, tax.length());
164 catch(exception& e) {
165 m->errorOut(e, "TaxEqualizer", "removeConfidences");
170 /**************************************************************************************************/