*/
#include "phylosummary.h"
-
+#include "referencedb.h"
/**************************************************************************************************/
-PhyloSummary::PhyloSummary(string refTfile, string groupFile){
+PhyloSummary::PhyloSummary(string refTfile, CountTable* c){
try {
m = MothurOut::getInstance();
maxLevel = 0;
ignore = false;
+ numSeqs = 0;
- if (groupFile != "") {
- groupmap = new GroupMap(groupFile);
- groupmap->readMap();
- }else{
- groupmap = NULL;
- }
-
+ ct = c;
+ groupmap = NULL;
+
//check for necessary files
- string taxFileNameTest = refTfile.substr(0,refTfile.find_last_of(".")+1) + "tree.sum";
+ if (refTfile == "saved") { ReferenceDB* rdb = ReferenceDB::getInstance(); refTfile = rdb->getSavedTaxonomy(); }
+ string taxFileNameTest = m->getFullPathName((refTfile.substr(0,refTfile.find_last_of(".")+1) + "tree.sum"));
ifstream FileTest(taxFileNameTest.c_str());
if (!FileTest) {
tree[0].rank = "0";
assignRank(0);
-
+
}
catch(exception& e) {
m->errorOut(e, "PhyloSummary", "PhyloSummary");
/**************************************************************************************************/
-PhyloSummary::PhyloSummary(string groupFile){
+PhyloSummary::PhyloSummary(CountTable* c){
try {
m = MothurOut::getInstance();
maxLevel = 0;
ignore = true;
+ numSeqs = 0;
- if (groupFile != "") {
- groupmap = new GroupMap(groupFile);
- groupmap->readMap();
- }else{
- groupmap = NULL;
- }
+ ct = c;
+ groupmap = NULL;
tree.push_back(rawTaxNode("Root"));
tree[0].rank = "0";
+ }
+ catch(exception& e) {
+ m->errorOut(e, "PhyloSummary", "PhyloSummary");
+ exit(1);
+ }
+}
+/**************************************************************************************************/
+PhyloSummary::PhyloSummary(string refTfile, GroupMap* g){
+ try {
+ m = MothurOut::getInstance();
+ maxLevel = 0;
+ ignore = false;
+ numSeqs = 0;
+ groupmap = g;
+ ct = NULL;
+
+ //check for necessary files
+ if (refTfile == "saved") { ReferenceDB* rdb = ReferenceDB::getInstance(); refTfile = rdb->getSavedTaxonomy(); }
+ string taxFileNameTest = m->getFullPathName((refTfile.substr(0,refTfile.find_last_of(".")+1) + "tree.sum"));
+ ifstream FileTest(taxFileNameTest.c_str());
+
+ if (!FileTest) {
+ m->mothurOut("Error: can't find " + taxFileNameTest + "."); m->mothurOutEndLine(); exit(1);
+ }else{
+ readTreeStruct(FileTest);
+ }
+ tree[0].rank = "0";
+ assignRank(0);
+
}
catch(exception& e) {
m->errorOut(e, "PhyloSummary", "PhyloSummary");
exit(1);
}
}
+
/**************************************************************************************************/
-void PhyloSummary::summarize(string userTfile){
+PhyloSummary::PhyloSummary(GroupMap* g){
try {
+ m = MothurOut::getInstance();
+ maxLevel = 0;
+ ignore = true;
+ numSeqs = 0;
- ifstream in;
- m->openInputFile(userTfile, in);
+ groupmap = g;
+ ct = NULL;
- //read in users taxonomy file and add sequences to tree
- string name, tax;
- while(!in.eof()){
- in >> name >> tax; m->gobble(in);
-
- addSeqToTree(name, tax);
-
- if (m->control_pressed) { break; }
- }
- in.close();
+ tree.push_back(rawTaxNode("Root"));
+ tree[0].rank = "0";
+ }
+ catch(exception& e) {
+ m->errorOut(e, "PhyloSummary", "PhyloSummary");
+ exit(1);
+ }
+}
+/**************************************************************************************************/
+
+int PhyloSummary::summarize(string userTfile){
+ try {
+ map<string, string> temp;
+ m->readTax(userTfile, temp);
+
+ for (map<string, string>::iterator itTemp = temp.begin(); itTemp != temp.end();) {
+ addSeqToTree(itTemp->first, itTemp->second);
+ temp.erase(itTemp++);
+ }
+
+ return numSeqs;
}
catch(exception& e) {
m->errorOut(e, "PhyloSummary", "summarize");
int PhyloSummary::addSeqToTree(string seqName, string seqTaxonomy){
try {
+
numSeqs++;
map<string, int>::iterator childPointer;
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; }
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) {
+ int thisCount = 1;
+
+ if (groupmap != NULL) {
//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<string, int>::iterator itGroup = tree[childPointer->second].groupCount.find(group);
if (itGroup != tree[childPointer->second].groupCount.end()) {
tree[childPointer->second].groupCount[group]++;
}
- }
+ }else if (ct != NULL) {
+ if (ct->hasGroupInfo()) {
+ vector<int> groupCounts = ct->getGroupCounts(seqName);
+ vector<string> groups = ct->getNamesOfGroups();
+ for (int i = 0; i < groups.size(); i++) {
+
+ if (groupCounts[i] != 0) {
+ //do you have a count for this group?
+ map<string, int>::iterator itGroup = tree[childPointer->second].groupCount.find(groups[i]);
+
+ //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[childPointer->second].groupCount.end()) {
+ tree[childPointer->second].groupCount[groups[i]] += groupCounts[i];
+ }
+ }
+ }
+ }
+ thisCount = ct->getNumSeqs(seqName);
+ }
- tree[childPointer->second].total++;
+ tree[childPointer->second].total += thisCount;
currentNode = childPointer->second;
}else{
tree[index].parent = currentNode;
tree[index].level = (level+1);
- tree[index].total = 1;
tree[currentNode].children[taxon] = index;
+ int thisCount = 1;
//initialize groupcounts
if (groupmap != NULL) {
//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<string, int>::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]++;
- }
- }
+ }
+ }else if (ct != NULL) {
+ if (ct->hasGroupInfo()) {
+ vector<string> mGroups = ct->getNamesOfGroups();
+ for (int j = 0; j < mGroups.size(); j++) {
+ tree[index].groupCount[mGroups[j]] = 0;
+ }
+ vector<int> groupCounts = ct->getGroupCounts(seqName);
+ vector<string> groups = ct->getNamesOfGroups();
+
+ for (int i = 0; i < groups.size(); i++) {
+ if (groupCounts[i] != 0) {
+
+ //do you have a count for this group?
+ map<string, int>::iterator itGroup = tree[index].groupCount.find(groups[i]);
+
+ //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[groups[i]]+=groupCounts[i];
+ }
+ }
+ }
+ }
+ thisCount = ct->getNumSeqs(seqName);
+ }
+ tree[index].total = thisCount;
currentNode = index;
}else{ //otherwise, error
}
/**************************************************************************************************/
-int PhyloSummary::addSeqToTree(string seqTaxonomy, vector<string> names){
+int PhyloSummary::addSeqToTree(string seqTaxonomy, map<string, bool> containsGroup){
try {
numSeqs++;
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; }
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<string, bool> containsGroup;
- vector<string> 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(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<string, bool>::iterator itGroup = containsGroup.begin(); itGroup != containsGroup.end(); itGroup++) {
+ if (itGroup->second == true) {
+ tree[childPointer->second].groupCount[itGroup->first]++;
+ }
+ }
- for (map<string, bool>::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;
tree[index].level = (level+1);
tree[index].total = 1;
tree[currentNode].children[taxon] = index;
-
- //initialize groupcounts
- if (groupmap != NULL) {
- map<string, bool> containsGroup;
- vector<string> 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(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<string, bool>::iterator itGroup = containsGroup.begin(); itGroup != containsGroup.end(); itGroup++) {
- if (itGroup->second == true) {
- tree[index].groupCount[itGroup->first]++;
- }
- }
- }
+ for (map<string, bool>::iterator itGroup = containsGroup.begin(); itGroup != containsGroup.end(); itGroup++) {
+ if (itGroup->second == true) {
+ tree[index].groupCount[itGroup->first]++;
+ }
+ }
currentNode = index;
try {
if (ignore) { assignRank(0); }
-
+ vector<string> mGroups;
//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());
- vector<string> mGroups = groupmap->getNamesOfGroups();
+ mGroups = groupmap->getNamesOfGroups();
for (int i = 0; i < mGroups.size(); i++) {
out << mGroups[i] << '\t';
}
- }
+ }else if (ct != NULL) {
+ if (ct->hasGroupInfo()) {
+ mGroups = ct->getNamesOfGroups();
+ for (int i = 0; i < mGroups.size(); i++) {
+ out << mGroups[i] << '\t';
+ }
+ }
+ }
out << endl;
int totalChildrenInTree = 0;
+ map<string, int>::iterator itGroup;
map<string,int>::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) {
+ for (int i = 0; i < mGroups.size(); i++) { tree[0].groupCount[mGroups[i]] += tree[it->second].groupCount[mGroups[i]]; }
+ }else if ( ct != NULL) {
+ if (ct->hasGroupInfo()) { 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<string, int>::iterator itGroup;
+
if (groupmap != NULL) {
- //for (itGroup = tree[0].groupCount.begin(); itGroup != tree[0].groupCount.end(); itGroup++) {
- // out << itGroup->second << '\t';
- //}
- vector<string> mGroups = groupmap->getNamesOfGroups();
- for (int i = 0; i < mGroups.size(); i++) { out << tree[0].groupCount[mGroups[i]] << '\t'; }
- }
+ for (int i = 0; i < mGroups.size(); i++) { out << tree[0].groupCount[mGroups[i]] << '\t'; }
+ }else if ( ct != NULL) {
+ if (ct->hasGroupInfo()) { for (int i = 0; i < mGroups.size(); i++) { out << tree[0].groupCount[mGroups[i]] << '\t'; } }
+ }
out << endl;
//print rest
//}
vector<string> mGroups = groupmap->getNamesOfGroups();
for (int i = 0; i < mGroups.size(); i++) { out << tree[it->second].groupCount[mGroups[i]] << '\t'; }
- }
+ }else if (ct != NULL) {
+ if (ct->hasGroupInfo()) {
+ vector<string> mGroups = ct->getNamesOfGroups();
+ for (int i = 0; i < mGroups.size(); i++) { out << tree[it->second].groupCount[mGroups[i]] << '\t'; }
+ }
+ }
out << endl;
}
for (int j = 0; j < (groupmap->getNamesOfGroups()).size(); j++) {
tree[i].groupCount[(groupmap->getNamesOfGroups())[j]] = 0;
}
- }
+ }else if (ct != NULL) {
+ if (ct->hasGroupInfo()) {
+ for (int j = 0; j < (ct->getNamesOfGroups()).size(); j++) {
+ tree[i].groupCount[(ct->getNamesOfGroups())[j]] = 0;
+ }
+ }
+ }
tree[i].total = 0;
}
catch(exception& e) {
- m->errorOut(e, "PhyloSummary", "print");
+ m->errorOut(e, "PhyloSummary", "readTreeStruct");
exit(1);
}
}
-
/**************************************************************************************************/