X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=phylotree.cpp;h=b9bab4ec2934a4121305f87111cf2d238f5ec4d5;hp=5438147ea3dd186989dafd144cf2d819c8845ef3;hb=a8e2df1b96a57f5f29576b08361b86a96a8eff4f;hpb=4c5e7a20a03ddc6feb49ff9d21fcb4c79bc5508d diff --git a/phylotree.cpp b/phylotree.cpp index 5438147..b9bab4e 100644 --- a/phylotree.cpp +++ b/phylotree.cpp @@ -20,6 +20,7 @@ PhyloTree::PhyloTree(){ tree[0].heirarchyID = "0"; maxLevel = 0; calcTotals = true; + addSeqToTree("unknown", "unknown;"); } catch(exception& e) { m->errorOut(e, "PhyloTree", "PhyloTree"); @@ -74,7 +75,7 @@ PhyloTree::PhyloTree(ifstream& in, string filename){ for (int i = 0; i < numGenus; i++) { iss >> gnode >> gsize; m->gobble(iss); - uniqueTaxonomies[gnode] = gnode; + uniqueTaxonomies.insert(gnode); totals.push_back(gsize); } @@ -101,7 +102,7 @@ PhyloTree::PhyloTree(ifstream& in, string filename){ for (int i = 0; i < numGenus; i++) { in >> gnode >> gsize; m->gobble(in); - uniqueTaxonomies[gnode] = gnode; + uniqueTaxonomies.insert(gnode); totals.push_back(gsize); } @@ -127,7 +128,6 @@ PhyloTree::PhyloTree(string tfile){ maxLevel = 0; calcTotals = true; string name, tax; - #ifdef USE_MPI int pid, num, processors; @@ -178,20 +178,26 @@ PhyloTree::PhyloTree(string tfile){ MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case #else - ifstream in; - m->openInputFile(tfile, in); - - //read in users taxonomy file and add sequences to tree - while(!in.eof()){ - in >> name >> tax; m->gobble(in); - - addSeqToTree(name, tax); - } - in.close(); + map temp; + m->readTax(tfile, temp); + + for (map::iterator itTemp = temp.begin(); itTemp != temp.end();) { + addSeqToTree(itTemp->first, itTemp->second); + temp.erase(itTemp++); + } #endif assignHeirarchyIDs(0); - + + + string unknownTax = "unknown;"; + //added last taxon until you get desired level + for (int i = 1; i < maxLevel; i++) { + unknownTax += "unclassfied;"; + } + + addSeqToTree("unknown", unknownTax); + //create file for summary if needed setUp(tfile); } @@ -232,7 +238,6 @@ string PhyloTree::getNextTaxon(string& heirarchy, string seqname){ int PhyloTree::addSeqToTree(string seqName, string seqTaxonomy){ try { - numSeqs++; map::iterator childPointer; @@ -241,6 +246,8 @@ int PhyloTree::addSeqToTree(string seqName, string seqTaxonomy){ int level = 1; tree[0].accessions.push_back(seqName); + m->removeConfidences(seqTaxonomy); + string taxon;// = getNextTaxon(seqTaxonomy); while(seqTaxonomy != ""){ @@ -252,8 +259,10 @@ int PhyloTree::addSeqToTree(string seqName, string seqTaxonomy){ //somehow the parent is getting one too many accnos //use print to reassign the taxa id taxon = getNextTaxon(seqTaxonomy, seqName); + + if (m->debug) { m->mothurOut(seqName +'\t' + taxon +'\n'); } - if (taxon == "") { m->mothurOut(seqName + " has an error in the taxonomy. This may be due to a ;;"); m->mothurOutEndLine(); if (currentNode != 0) { uniqueTaxonomies[currentNode] = currentNode; } break; } + if (taxon == "") { m->mothurOut(seqName + " has an error in the taxonomy. This may be due to a ;;"); m->mothurOutEndLine(); if (currentNode != 0) { uniqueTaxonomies.insert(currentNode); } break; } childPointer = tree[currentNode].children.find(taxon); @@ -273,7 +282,7 @@ int PhyloTree::addSeqToTree(string seqName, string seqTaxonomy){ name2Taxonomy[seqName] = currentNode; } - if (seqTaxonomy == "") { uniqueTaxonomies[currentNode] = currentNode; } + if (seqTaxonomy == "") { uniqueTaxonomies.insert(currentNode); } } return 0; @@ -288,9 +297,16 @@ vector PhyloTree::getGenusNodes() { try { genusIndex.clear(); //generate genusIndexes - map::iterator it2; - for (it2=uniqueTaxonomies.begin(); it2!=uniqueTaxonomies.end(); it2++) { genusIndex.push_back(it2->first); } - + set::iterator it2; + map temp; + for (it2=uniqueTaxonomies.begin(); it2!=uniqueTaxonomies.end(); it2++) { genusIndex.push_back(*it2); temp[*it2] = genusIndex.size()-1; } + + for (map::iterator itName = name2Taxonomy.begin(); itName != name2Taxonomy.end(); itName++) { + map::iterator itTemp = temp.find(itName->second); + if (itTemp != temp.end()) { name2GenusNodeIndex[itName->first] = itTemp->second; } + else { m->mothurOut("[ERROR]: trouble making name2GenusNodeIndex, aborting.\n"); m->control_pressed = true; } + } + return genusIndex; } catch(exception& e) { @@ -327,6 +343,9 @@ void PhyloTree::assignHeirarchyIDs(int index){ int counter = 1; for(it=tree[index].children.begin();it!=tree[index].children.end();it++){ + + if (m->debug) { m->mothurOut(toString(index) +'\t' + tree[it->second].name +'\n'); } + tree[it->second].heirarchyID = tree[index].heirarchyID + '.' + toString(counter); counter++; tree[it->second].level = tree[index].level + 1; @@ -373,7 +392,7 @@ void PhyloTree::binUnclassified(string file){ map::iterator childPointer; vector copy = tree; - + //fill out tree fillOutTree(0, copy); @@ -385,6 +404,8 @@ void PhyloTree::binUnclassified(string file){ } } + if (m->debug) { m->mothurOut("maxLevel = " + toString(maxLevel) +'\n'); } + int copyNodes = copy.size(); //go through the seqs and if a sequence finest taxon is not the same level as the most finely defined taxon then classify it as unclassified where necessary @@ -395,11 +416,14 @@ void PhyloTree::binUnclassified(string file){ int level = copy[itLeaf->second].level; int currentNode = itLeaf->second; + + if (m->debug) { m->mothurOut(copy[currentNode].name +'\n'); } //this sequence is unclassified at some levels while(level < maxLevel){ level++; + if (m->debug) { m->mothurOut("level = " + toString(level) +'\n'); } string taxon = "unclassified"; @@ -482,16 +506,16 @@ string PhyloTree::getFullTaxonomy(string seqName) { void PhyloTree::print(ofstream& out, vector& copy){ try { - + //output mothur version out << "#" << m->getVersion() << endl; out << copy.size() << endl; out << maxLevel << endl; - + for (int i = 0; i < copy.size(); i++) { - + out << copy[i].level << '\t'<< copy[i].name << '\t' << copy[i].children.size() << '\t'; map::iterator it; @@ -534,8 +558,8 @@ void PhyloTree::printTreeNodes(string treefilename) { //print genus nodes outTree << endl << uniqueTaxonomies.size() << endl; - map::iterator it2; - for (it2=uniqueTaxonomies.begin(); it2!=uniqueTaxonomies.end(); it2++) { outTree << it2->first << '\t' << tree[it2->first].accessions.size() << endl; } + set::iterator it2; + for (it2=uniqueTaxonomies.begin(); it2!=uniqueTaxonomies.end(); it2++) { outTree << *it2 << '\t' << tree[*it2].accessions.size() << endl; } outTree << endl; outTree.close(); @@ -587,12 +611,12 @@ string PhyloTree::getName(int i ){ } } /**************************************************************************************************/ -int PhyloTree::getIndex(string seqName){ +int PhyloTree::getGenusIndex(string seqName){ try { - map::iterator itFind = name2Taxonomy.find(seqName); + map::iterator itFind = name2GenusNodeIndex.find(seqName); - if (itFind != name2Taxonomy.end()) { return name2Taxonomy[seqName]; } - else { m->mothurOut("Cannot find " + seqName + ". Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1);} + if (itFind != name2GenusNodeIndex.end()) { return itFind->second; } + else { m->mothurOut("Cannot find " + seqName + ". Could be a mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1);} } catch(exception& e) { m->errorOut(e, "PhyloTree", "get"); @@ -604,17 +628,20 @@ bool PhyloTree::ErrorCheck(vector templateFileNames){ try { bool okay = true; + templateFileNames.push_back("unknown"); map::iterator itFind; map taxonomyFileNames = name2Taxonomy; + if (m->debug) { m->mothurOut("[DEBUG]: in error check. Numseqs in template = " + toString(templateFileNames.size()) + ". Numseqs in taxonomy = " + toString(taxonomyFileNames.size()) + ".\n"); } + for (int i = 0; i < templateFileNames.size(); i++) { itFind = taxonomyFileNames.find(templateFileNames[i]); if (itFind != taxonomyFileNames.end()) { //found it so erase it taxonomyFileNames.erase(itFind); }else { - m->mothurOut(templateFileNames[i] + " is in your template file and is not in your taxonomy file. Please correct."); m->mothurOutEndLine(); + m->mothurOut("'" +templateFileNames[i] + "' is in your template file and is not in your taxonomy file. Please correct."); m->mothurOutEndLine(); okay = false; }