]> git.donarmstrong.com Git - mothur.git/blobdiff - tree.cpp
added root parameter to the unifrac commands so you can choose to include the entire...
[mothur.git] / tree.cpp
index 114b4de0d6e2c408479ae8c03b65d2b1300bb9b8..02f400407cfaa5f8c9283fa4f96d8bbc2fb0c3d8 100644 (file)
--- a/tree.cpp
+++ b/tree.cpp
@@ -50,12 +50,12 @@ Tree::Tree() {
                numNodes = 2*numLeaves - 1;
                
                tree.resize(numNodes);
-               
+                       
                //initialize groupNodeInfo
                for (int i = 0; i < globaldata->gTreemap->namesOfGroups.size(); i++) {
                        groupNodeInfo[globaldata->gTreemap->namesOfGroups[i]].resize(0);
                }
-
+               
                //initialize tree with correct number of nodes, name and group info.
                for (int i = 0; i < numNodes; i++) {
                        //initialize leaf nodes
@@ -64,6 +64,7 @@ Tree::Tree() {
                                
                                //save group info
                                string group = globaldata->gTreemap->getGroup(globaldata->Treenames[i]);
+                               
                                vector<string> tempGroups; tempGroups.push_back(group);
                                tree[i].setGroup(tempGroups);
                                groupNodeInfo[group].push_back(i); 
@@ -82,6 +83,7 @@ Tree::Tree() {
                                tree[i].setGroup(tempGroups);
                        }
                }
+               
        }
        catch(exception& e) {
                m->errorOut(e, "Tree", "Tree");
@@ -328,7 +330,9 @@ void Tree::getSubTree(Tree* copy, vector<string> Groups) {
                                                
                                                copy->tree[i].setParent(grandparent);
                                                copy->tree[i].setBranchLength((copy->tree[i].getBranchLength()+copy->tree[parent].getBranchLength()));
-                                               copy->tree[grandparent].setChildren(grandparentLC, grandparentRC);
+                                               if (grandparent != -1) {
+                                                       copy->tree[grandparent].setChildren(grandparentLC, grandparentRC);
+                                               }
                                                removedLeaves.insert(sibIndex);
                                        }
                                }else{
@@ -355,7 +359,9 @@ void Tree::getSubTree(Tree* copy, vector<string> Groups) {
                                                
                                                copy->tree[sibIndex].setParent(grandparent);
                                                copy->tree[sibIndex].setBranchLength((copy->tree[sibIndex].getBranchLength()+copy->tree[parent].getBranchLength()));
-                                               copy->tree[grandparent].setChildren(grandparentLC, grandparentRC);
+                                               if (grandparent != -1) {
+                                                       copy->tree[grandparent].setChildren(grandparentLC, grandparentRC);
+                                               }
                                                removedLeaves.insert(i);
                                        }else{
                                                //neither of us are, so we want to eliminate ourselves and our parent
@@ -365,8 +371,11 @@ void Tree::getSubTree(Tree* copy, vector<string> Groups) {
                                                int parentsSibIndex;
                                                if (grandparent != -1) {
                                                        int greatgrandparent = copy->tree[grandparent].getParent();
-                                                       int greatgrandparentLC = copy->tree[greatgrandparent].getLChild();
-                                                       int greatgrandparentRC = copy->tree[greatgrandparent].getRChild();
+                                                       int greatgrandparentLC, greatgrandparentRC;
+                                                       if (greatgrandparent != -1) {
+                                                               greatgrandparentLC = copy->tree[greatgrandparent].getLChild();
+                                                               greatgrandparentRC = copy->tree[greatgrandparent].getRChild();
+                                                       }
                                                        
                                                        int grandparentLC = copy->tree[grandparent].getLChild();
                                                        int grandparentRC = copy->tree[grandparent].getRChild();
@@ -380,10 +389,12 @@ void Tree::getSubTree(Tree* copy, vector<string> Groups) {
                                                        
                                                        copy->tree[parentsSibIndex].setParent(greatgrandparent);
                                                        copy->tree[parentsSibIndex].setBranchLength((copy->tree[parentsSibIndex].getBranchLength()+copy->tree[grandparent].getBranchLength()));
-                                                       copy->tree[greatgrandparent].setChildren(greatgrandparentLC, greatgrandparentRC);
+                                                       if (greatgrandparent != -1) {
+                                                               copy->tree[greatgrandparent].setChildren(greatgrandparentLC, greatgrandparentRC);
+                                                       }
                                                }else{
-                                                       copy->tree[parent].setChildren(-1, -1);
-                                                       cout << "issues with making subtree" << endl;
+                                                       copy->tree[parent].setParent(-1);
+                                                       //cout << "issues with making subtree" << endl;
                                                }
                                                removedLeaves.insert(sibIndex);
                                                removedLeaves.insert(i);
@@ -401,7 +412,6 @@ void Tree::getSubTree(Tree* copy, vector<string> Groups) {
                
                int nextSpot = numLeaves;
                populateNewTree(copy->tree, root, nextSpot);
-               
        }
        catch(exception& e) {
                m->errorOut(e, "Tree", "getCopy");
@@ -420,6 +430,10 @@ int Tree::populateNewTree(vector<Node>& oldtree, int node, int& index) {
                        tree[rc].setParent(index);
                        tree[lc].setParent(index);
                        
+                       tree[index].setBranchLength(oldtree[node].getBranchLength());
+                       tree[rc].setBranchLength(oldtree[oldtree[node].getLChild()].getBranchLength());
+                       tree[lc].setBranchLength(oldtree[oldtree[node].getRChild()].getBranchLength());
+                       
                        return (index++);
                }else { //you are a leaf
                        int indexInNewTree = globaldata->gTreemap->getIndex(oldtree[node].getName());