]> git.donarmstrong.com Git - mothur.git/blobdiff - phylosummary.cpp
added relabund parameter to classify.seqs and summary.tax commands
[mothur.git] / phylosummary.cpp
index ab6bb831dfae73bb56c75eb8b144963701dc02be..89099fff3fcc312b81c033b23fe87af81242d58e 100644 (file)
@@ -8,19 +8,22 @@
  */
 
 #include "phylosummary.h"
+#include "referencedb.h"
 /**************************************************************************************************/
 
-PhyloSummary::PhyloSummary(string refTfile, CountTable* c){
+PhyloSummary::PhyloSummary(string refTfile, CountTable* c, bool r){
        try {
                m = MothurOut::getInstance();
                maxLevel = 0;
                ignore = false;
         numSeqs = 0;
+        relabund = r;
                
                ct = c;
         groupmap = 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());
                
@@ -42,12 +45,13 @@ PhyloSummary::PhyloSummary(string refTfile, CountTable* c){
 
 /**************************************************************************************************/
 
-PhyloSummary::PhyloSummary(CountTable* c){
+PhyloSummary::PhyloSummary(CountTable* c, bool r){
        try {
                m = MothurOut::getInstance();
                maxLevel = 0;
                ignore = true;
         numSeqs = 0;
+        relabund = r;
                
                ct = c;
         groupmap = NULL;
@@ -61,17 +65,19 @@ PhyloSummary::PhyloSummary(CountTable* c){
        }
 }
 /**************************************************************************************************/
-PhyloSummary::PhyloSummary(string refTfile, GroupMap* g){
+PhyloSummary::PhyloSummary(string refTfile, GroupMap* g, bool r){
        try {
                m = MothurOut::getInstance();
                maxLevel = 0;
                ignore = false;
         numSeqs = 0;
+        relabund = r;
                
                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());
                
@@ -93,12 +99,13 @@ PhyloSummary::PhyloSummary(string refTfile, GroupMap* g){
 
 /**************************************************************************************************/
 
-PhyloSummary::PhyloSummary(GroupMap* g){
+PhyloSummary::PhyloSummary(GroupMap* g, bool r){
        try {
                m = MothurOut::getInstance();
                maxLevel = 0;
                ignore = true;
         numSeqs = 0;
+        relabund = r;
                
                groupmap = g;
         ct = NULL;
@@ -374,11 +381,10 @@ void PhyloSummary::assignRank(int index){
        try {
                map<string,int>::iterator it;
                int counter = 1;
-               
+        
                for(it=tree[index].children.begin();it!=tree[index].children.end();it++){
                        tree[it->second].rank = tree[index].rank + '.' + toString(counter);
                        counter++;
-                                                                       
                        assignRank(it->second);
                }
        }
@@ -392,7 +398,7 @@ void PhyloSummary::assignRank(int index){
 void PhyloSummary::print(ofstream& out){
        try {
                
-               if (ignore) { assignRank(0); }
+               if (ignore)     {  assignRank(0); }
         vector<string> mGroups;
                //print labels
                out << "taxlevel\t rankID\t taxon\t daughterlevels\t total\t";
@@ -411,12 +417,10 @@ void PhyloSummary::print(ofstream& out){
                 }
             }
         }
-               
                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)  {   
@@ -432,18 +436,93 @@ void PhyloSummary::print(ofstream& out){
                }
                
                //print root
-               out << tree[0].level << "\t" << tree[0].rank << "\t" << tree[0].name << "\t" << totalChildrenInTree << "\t" << tree[0].total << "\t";
+        if (relabund) {
+            out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
+            out << tree[0].level << "\t" << tree[0].rank << "\t" << tree[0].name << "\t" << totalChildrenInTree << "\t" << (tree[0].total/(double) tree[0].total) << '\t';
+            
+            if (groupmap != NULL) {
+                for (int i = 0; i < mGroups.size(); i++) {
+                    double thisNum = tree[0].groupCount[mGroups[i]];
+                    thisNum /= (double) groupmap->getNumSeqs(mGroups[i]);
+                    out << thisNum << '\t';
+                }
+            }else if ( ct != NULL) {
+                if (ct->hasGroupInfo()) {
+                    for (int i = 0; i < mGroups.size(); i++) {
+                        double thisNum = tree[0].groupCount[mGroups[i]];
+                        thisNum /= (double) ct->getGroupCount(mGroups[i]);
+                        out << thisNum << '\t';
+                    }
+                }
+            }
+            out << endl;
+        }else {
+            out << tree[0].level << "\t" << tree[0].rank << "\t" << tree[0].name << "\t" << totalChildrenInTree << "\t" << tree[0].total << "\t";
+            if (groupmap != NULL) {
+                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
+               print(0, out);
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "PhyloSummary", "print");
+               exit(1);
+       }
+}
+/**************************************************************************************************/
+
+void PhyloSummary::print(ofstream& out, bool relabund){
+       try {
                
+               if (ignore) { assignRank(0); }
+       
+               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++;
+                               tree[0].total += tree[it->second].total;
+                               
+                               if (groupmap != NULL) {
+                    vector<string> mGroups = groupmap->getNamesOfGroups();
+                                       for (int i = 0; i < mGroups.size(); i++) { tree[0].groupCount[mGroups[i]] += tree[it->second].groupCount[mGroups[i]]; }
+                               }else if ( ct != NULL) {
+                    vector<string> mGroups = ct->getNamesOfGroups();
+                    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].name << "\t" << "1.0000" << "\t"; //root relative abundance is 1, everyone classifies to root
+               
+               /*
                if (groupmap != NULL) {
-                       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'; } }
+        }*/
+        
+        if (groupmap != NULL) {
+            vector<string> mGroups = groupmap->getNamesOfGroups();
+                       for (int i = 0; i < mGroups.size(); i++) {  out << "1.0000" << '\t'; }
+        }else if ( ct != NULL) {
+            vector<string> mGroups = ct->getNamesOfGroups();
+            if (ct->hasGroupInfo()) { for (int i = 0; i < mGroups.size(); i++) {  out << "1.0000" << '\t'; } }
         }
+        
                out << endl;
                
                //print rest
-               print(0, out);
+               print(0, out, relabund);
                
        }
        catch(exception& e) {
@@ -451,10 +530,68 @@ void PhyloSummary::print(ofstream& out){
                exit(1);
        }
 }
-
 /**************************************************************************************************/
 
 void PhyloSummary::print(int i, ofstream& out){
+       try {
+               map<string,int>::iterator it;
+               for(it=tree[i].children.begin();it!=tree[i].children.end();it++){
+                       
+                       if (tree[it->second].total != 0)  {
+                
+                               int totalChildrenInTree = 0;
+                
+                               map<string,int>::iterator it2;
+                               for(it2=tree[it->second].children.begin();it2!=tree[it->second].children.end();it2++){
+                                       if (tree[it2->second].total != 0)  {   totalChildrenInTree++; }
+                               }
+                
+                if (relabund) {
+                    out << tree[it->second].level << "\t" << tree[it->second].rank << "\t" << tree[it->second].name << "\t" << totalChildrenInTree << "\t" << (tree[it->second].total/(double) tree[0].total) << "\t";
+                }else {
+                    out << tree[it->second].level << "\t" << tree[it->second].rank << "\t" << tree[it->second].name << "\t" << totalChildrenInTree << "\t" << tree[it->second].total << "\t";
+                               }
+                
+                if (relabund) {
+                    map<string, int>::iterator itGroup;
+                    if (groupmap != NULL) {
+                        vector<string> mGroups = groupmap->getNamesOfGroups();
+                        for (int i = 0; i < mGroups.size(); i++) {  out << (tree[it->second].groupCount[mGroups[i]]/(double)groupmap->getNumSeqs(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]]/(double)ct->getGroupCount(mGroups[i])) << '\t'; }
+                        }
+                    }
+                }else {
+                    map<string, int>::iterator itGroup;
+                    if (groupmap != NULL) {
+                        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;
+                               
+                       }
+                       
+                       print(it->second, out);
+               }
+       }
+       catch(exception& e) {
+               m->errorOut(e, "PhyloSummary", "print");
+               exit(1);
+       }
+}
+
+/**************************************************************************************************/
+
+void PhyloSummary::print(int i, ofstream& out, bool relabund){
        try {
                map<string,int>::iterator it;
                for(it=tree[i].children.begin();it!=tree[i].children.end();it++){
@@ -467,27 +604,41 @@ void PhyloSummary::print(int i, ofstream& out){
                                for(it2=tree[it->second].children.begin();it2!=tree[it->second].children.end();it2++){   
                                        if (tree[it2->second].total != 0)  {   totalChildrenInTree++; }
                                }
-                       
-                               out << tree[it->second].level << "\t" << tree[it->second].rank << "\t" << tree[it->second].name << "\t" << totalChildrenInTree << "\t" << tree[it->second].total << "\t";
+                
+                string nodeName = "";
+                int thisNode = it->second;
+                while (tree[thisNode].rank != "0") { //while you are not at top
+                    if (m->control_pressed) { break; }
+                    nodeName = tree[thisNode].name + "|" + nodeName;
+                    thisNode = tree[thisNode].parent;
+                }
+                if (nodeName != "") { nodeName = nodeName.substr(0, nodeName.length()-1); }
+                
+                               out << nodeName << "\t" << (tree[it->second].total / (float)tree[i].total) << "\t";
                                
                                map<string, int>::iterator itGroup;
                                if (groupmap != NULL) {
-                                       //for (itGroup = tree[it->second].groupCount.begin(); itGroup != tree[it->second].groupCount.end(); itGroup++) {
-                                       //      out << itGroup->second << '\t';
-                                       //}
                                        vector<string> mGroups = groupmap->getNamesOfGroups();
-                                       for (int i = 0; i < mGroups.size(); i++) {  out << tree[it->second].groupCount[mGroups[i]] << '\t'; } 
+                                       for (int j = 0; j < mGroups.size(); j++) {
+                        if (tree[i].groupCount[mGroups[j]] == 0) {
+                            out << 0 << '\t';
+                        }else { out << (tree[it->second].groupCount[mGroups[j]] / (float)tree[i].groupCount[mGroups[j]]) << '\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'; } 
+                        for (int j = 0; j < mGroups.size(); j++) {
+                            if (tree[i].groupCount[mGroups[j]] == 0) {
+                                out << 0 << '\t';
+                            }else { out << (tree[it->second].groupCount[mGroups[j]] / (float)tree[i].groupCount[mGroups[j]]) << '\t'; }
+                        }
                     }
                 }
                                out << endl;
                                
                        }
                        
-                       print(it->second, out);
+                       print(it->second, out, relabund);
                }
        }
        catch(exception& e) {