X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=phylosummary.cpp;h=2f565150aabeec35cc8a3c03a459d8b85090550b;hb=8338fe821bc5e3f3cf15a5bf165eabe103b1ebce;hp=4e94605e47caf6e09d099a4df32dfdd7677be676;hpb=2e77272b6f5987bd2bde592bb15262a8c69ea9e0;p=mothur.git diff --git a/phylosummary.cpp b/phylosummary.cpp index 4e94605..2f56515 100644 --- a/phylosummary.cpp +++ b/phylosummary.cpp @@ -15,6 +15,7 @@ PhyloSummary::PhyloSummary(string refTfile, string groupFile){ try { m = MothurOut::getInstance(); maxLevel = 0; + ignore = false; if (groupFile != "") { groupmap = new GroupMap(groupFile); @@ -24,7 +25,7 @@ PhyloSummary::PhyloSummary(string refTfile, string groupFile){ } //check for necessary files - string taxFileNameTest = refTfile.substr(0,refTfile.find_last_of(".")+1) + "tree.sum"; + string taxFileNameTest = m->getFullPathName((refTfile.substr(0,refTfile.find_last_of(".")+1) + "tree.sum")); ifstream FileTest(taxFileNameTest.c_str()); if (!FileTest) { @@ -42,9 +43,35 @@ PhyloSummary::PhyloSummary(string refTfile, string groupFile){ exit(1); } } + +/**************************************************************************************************/ + +PhyloSummary::PhyloSummary(string groupFile){ + try { + m = MothurOut::getInstance(); + maxLevel = 0; + ignore = true; + + if (groupFile != "") { + groupmap = new GroupMap(groupFile); + groupmap->readMap(); + }else{ + groupmap = NULL; + } + + tree.push_back(rawTaxNode("Root")); + tree[0].rank = "0"; + + + } + catch(exception& e) { + m->errorOut(e, "PhyloSummary", "PhyloSummary"); + exit(1); + } +} /**************************************************************************************************/ -void PhyloSummary::summarize(string userTfile){ +int PhyloSummary::summarize(string userTfile){ try { ifstream in; @@ -52,14 +79,18 @@ void PhyloSummary::summarize(string userTfile){ //read in users taxonomy file and add sequences to tree string name, tax; + int numSeqs = 0; while(!in.eof()){ in >> name >> tax; m->gobble(in); addSeqToTree(name, tax); + numSeqs++; if (m->control_pressed) { break; } } in.close(); + + return numSeqs; } catch(exception& e) { m->errorOut(e, "PhyloSummary", "summarize"); @@ -90,6 +121,7 @@ string PhyloSummary::getNextTaxon(string& heirarchy){ int PhyloSummary::addSeqToTree(string seqName, string seqTaxonomy){ try { + numSeqs++; map::iterator childPointer; @@ -99,6 +131,9 @@ int PhyloSummary::addSeqToTree(string seqName, string seqTaxonomy){ int level = 0; + //are there confidence scores, if so remove them + if (seqTaxonomy.find_first_of('(') != -1) { m->removeConfidences(seqTaxonomy); } + while (seqTaxonomy != "") { if (m->control_pressed) { return 0; } @@ -114,7 +149,7 @@ int PhyloSummary::addSeqToTree(string seqName, string seqTaxonomy){ //find out the sequences group string group = groupmap->getGroup(seqName); - if (group == "not found") { m->mothurOut(seqName + " is not in your groupfile, and will be included in the overall total, but not any group total."); m->mothurOutEndLine(); } + if (group == "not found") { m->mothurOut("[WARNING]: " + seqName + " is not in your groupfile, and will be included in the overall total, but not any group total."); m->mothurOutEndLine(); } //do you have a count for this group? map::iterator itGroup = tree[childPointer->second].groupCount.find(group); @@ -128,9 +163,44 @@ int PhyloSummary::addSeqToTree(string seqName, string seqTaxonomy){ tree[childPointer->second].total++; currentNode = childPointer->second; - }else{ //otherwise, error - m->mothurOut("Warning: cannot find taxon " + taxon + " in reference taxonomy tree at level " + toString(tree[currentNode].level) + " for " + seqName + ". This may cause totals of daughter levels not to add up in summary file."); m->mothurOutEndLine(); - break; + }else{ + if (ignore) { + + tree.push_back(rawTaxNode(taxon)); + int index = tree.size() - 1; + + tree[index].parent = currentNode; + tree[index].level = (level+1); + tree[index].total = 1; + tree[currentNode].children[taxon] = index; + + //initialize groupcounts + if (groupmap != NULL) { + vector mGroups = groupmap->getNamesOfGroups(); + for (int j = 0; j < mGroups.size(); j++) { + tree[index].groupCount[mGroups[j]] = 0; + } + + //find out the sequences group + string group = groupmap->getGroup(seqName); + + if (group == "not found") { m->mothurOut("[WARNING]: " + seqName + " is not in your groupfile, and will be included in the overall total, but not any group total."); m->mothurOutEndLine(); } + + //do you have a count for this group? + map::iterator itGroup = tree[index].groupCount.find(group); + + //if yes, increment it - there should not be a case where we can't find it since we load group in read + if (itGroup != tree[index].groupCount.end()) { + tree[index].groupCount[group]++; + } + } + + currentNode = index; + + }else{ //otherwise, error + m->mothurOut("Warning: cannot find taxon " + taxon + " in reference taxonomy tree at level " + toString(tree[currentNode].level) + " for " + seqName + ". This may cause totals of daughter levels not to add up in summary file."); m->mothurOutEndLine(); + break; + } } level++; @@ -139,13 +209,129 @@ int PhyloSummary::addSeqToTree(string seqName, string seqTaxonomy){ for (int k = level; k < maxLevel; k++) { seqTaxonomy += "unclassified;"; } } } + return 0; + } + catch(exception& e) { + m->errorOut(e, "PhyloSummary", "addSeqToTree"); + exit(1); + } +} +/**************************************************************************************************/ +int PhyloSummary::addSeqToTree(string seqTaxonomy, vector names){ + try { + numSeqs++; + + map::iterator childPointer; + + int currentNode = 0; + string taxon; + + int level = 0; + + //are there confidence scores, if so remove them + if (seqTaxonomy.find_first_of('(') != -1) { m->removeConfidences(seqTaxonomy); } + + while (seqTaxonomy != "") { + + if (m->control_pressed) { return 0; } + + //somehow the parent is getting one too many accnos + //use print to reassign the taxa id + taxon = getNextTaxon(seqTaxonomy); + + childPointer = tree[currentNode].children.find(taxon); + + if(childPointer != tree[currentNode].children.end()){ //if the node already exists, update count and move on + if (groupmap != NULL) { + + map containsGroup; + vector mGroups = groupmap->getNamesOfGroups(); + for (int j = 0; j < mGroups.size(); j++) { + containsGroup[mGroups[j]] = false; + } + + for (int k = 0; k < names.size(); k++) { + //find out the sequences group + string group = groupmap->getGroup(names[k]); + + if (group == "not found") { m->mothurOut("[WARNING]: " + names[k] + " is not in your groupfile, and will be included in the overall total, but not any group total."); m->mothurOutEndLine(); } + else { + containsGroup[group] = true; + } + } + + for (map::iterator itGroup = containsGroup.begin(); itGroup != containsGroup.end(); itGroup++) { + if (itGroup->second == true) { + tree[childPointer->second].groupCount[itGroup->first]++; + } + } + + } + + tree[childPointer->second].total++; + + currentNode = childPointer->second; + }else{ + if (ignore) { + + tree.push_back(rawTaxNode(taxon)); + int index = tree.size() - 1; + + tree[index].parent = currentNode; + tree[index].level = (level+1); + tree[index].total = 1; + tree[currentNode].children[taxon] = index; + + //initialize groupcounts + if (groupmap != NULL) { + map containsGroup; + vector mGroups = groupmap->getNamesOfGroups(); + for (int j = 0; j < mGroups.size(); j++) { + tree[index].groupCount[mGroups[j]] = 0; + containsGroup[mGroups[j]] = false; + } + + + for (int k = 0; k < names.size(); k++) { + //find out the sequences group + string group = groupmap->getGroup(names[k]); + + if (group == "not found") { m->mothurOut("[WARNING]: " + names[k] + " is not in your groupfile, and will be included in the overall total, but not any group total."); m->mothurOutEndLine(); } + else { + containsGroup[group] = true; + } + } + + for (map::iterator itGroup = containsGroup.begin(); itGroup != containsGroup.end(); itGroup++) { + if (itGroup->second == true) { + tree[index].groupCount[itGroup->first]++; + } + } + } + + currentNode = index; + + }else{ //otherwise, error + m->mothurOut("Warning: cannot find taxon " + taxon + " in reference taxonomy tree at level " + toString(tree[currentNode].level) + ". This may cause totals of daughter levels not to add up in summary file."); m->mothurOutEndLine(); + break; + } + } + + level++; + + if ((seqTaxonomy == "") && (level < maxLevel)) { //if you think you are done and you are not. + for (int k = level; k < maxLevel; k++) { seqTaxonomy += "unclassified;"; } + } + } + return 0; } catch(exception& e) { m->errorOut(e, "PhyloSummary", "addSeqToTree"); exit(1); } } + /**************************************************************************************************/ void PhyloSummary::assignRank(int index){ @@ -169,35 +355,48 @@ void PhyloSummary::assignRank(int index){ void PhyloSummary::print(ofstream& out){ try { + + if (ignore) { assignRank(0); } + //print labels out << "taxlevel\t rankID\t taxon\t daughterlevels\t total\t"; if (groupmap != NULL) { //so the labels match the counts below, since the map sorts them automatically... //sort(groupmap->namesOfGroups.begin(), groupmap->namesOfGroups.end()); - - for (int i = 0; i < groupmap->namesOfGroups.size(); i++) { - out << groupmap->namesOfGroups[i] << '\t'; + vector mGroups = groupmap->getNamesOfGroups(); + for (int i = 0; i < mGroups.size(); i++) { + out << mGroups[i] << '\t'; } } out << endl; int totalChildrenInTree = 0; + map::iterator itGroup; map::iterator it; for(it=tree[0].children.begin();it!=tree[0].children.end();it++){ - if (tree[it->second].total != 0) { totalChildrenInTree++; } + if (tree[it->second].total != 0) { + totalChildrenInTree++; + tree[0].total += tree[it->second].total; + + if (groupmap != NULL) { + vector mGroups = groupmap->getNamesOfGroups(); + for (int i = 0; i < mGroups.size(); i++) { tree[0].groupCount[mGroups[i]] += tree[it->second].groupCount[mGroups[i]]; } + } + } } //print root out << tree[0].level << "\t" << tree[0].rank << "\t" << tree[0].name << "\t" << totalChildrenInTree << "\t" << tree[0].total << "\t"; - map::iterator itGroup; + if (groupmap != NULL) { //for (itGroup = tree[0].groupCount.begin(); itGroup != tree[0].groupCount.end(); itGroup++) { // out << itGroup->second << '\t'; //} - for (int i = 0; i < groupmap->namesOfGroups.size(); i++) { out << tree[0].groupCount[groupmap->namesOfGroups[i]] << '\t'; } + vector mGroups = groupmap->getNamesOfGroups(); + for (int i = 0; i < mGroups.size(); i++) { out << tree[0].groupCount[mGroups[i]] << '\t'; } } out << endl; @@ -234,9 +433,11 @@ void PhyloSummary::print(int i, ofstream& out){ //for (itGroup = tree[it->second].groupCount.begin(); itGroup != tree[it->second].groupCount.end(); itGroup++) { // out << itGroup->second << '\t'; //} - for (int i = 0; i < groupmap->namesOfGroups.size(); i++) { out << tree[it->second].groupCount[groupmap->namesOfGroups[i]] << '\t'; } + vector mGroups = groupmap->getNamesOfGroups(); + for (int i = 0; i < mGroups.size(); i++) { out << tree[it->second].groupCount[mGroups[i]] << '\t'; } } out << endl; + } print(it->second, out); @@ -277,8 +478,8 @@ void PhyloSummary::readTreeStruct(ifstream& in){ //initialize groupcounts if (groupmap != NULL) { - for (int j = 0; j < groupmap->namesOfGroups.size(); j++) { - tree[i].groupCount[groupmap->namesOfGroups[j]] = 0; + for (int j = 0; j < (groupmap->getNamesOfGroups()).size(); j++) { + tree[i].groupCount[(groupmap->getNamesOfGroups())[j]] = 0; } } @@ -291,11 +492,10 @@ void PhyloSummary::readTreeStruct(ifstream& in){ } catch(exception& e) { - m->errorOut(e, "PhyloSummary", "print"); + m->errorOut(e, "PhyloSummary", "readTreeStruct"); exit(1); } } - /**************************************************************************************************/