]> git.donarmstrong.com Git - mothur.git/blobdiff - tree.cpp
added namefile to align.check and added seqs from namefile to optimizing piece of...
[mothur.git] / tree.cpp
index 2a22cf3b952ff217c9e57c5ce98bbb01c5cbacb0..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");
@@ -270,7 +272,7 @@ void Tree::getSubTree(Tree* copy, vector<string> Groups) {
                        //initialize leaf nodes
                        if (i <= (numLeaves-1)) {
                                tree[i].setName(Groups[i]);
-                                       
+                               
                                //save group info
                                string group = globaldata->gTreemap->getGroup(Groups[i]);
                                vector<string> tempGroups; tempGroups.push_back(group);
@@ -301,6 +303,7 @@ void Tree::getSubTree(Tree* copy, vector<string> Groups) {
                        int parent = copy->tree[i].getParent();
                        
                        if (parent != -1) {
+                               
                                if (m->inUsersGroups(copy->tree[i].getName(), Groups)) {
                                        //find my siblings name
                                        int parentRC = copy->tree[parent].getRChild();
@@ -327,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{
@@ -354,24 +359,29 @@ 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
                                                //so set our parents sib to our great-grandparent
                                                int parent = copy->tree[i].getParent();
                                                int grandparent = copy->tree[parent].getParent();
-                                               
+                                               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();
                                                        
-                                                       int parentsSibIndex = grandparentLC;
-                                                       if (grandparentRC == parent) { parentsSibIndex = grandparentLC; }
+                                                       parentsSibIndex = grandparentLC;
+                                                       if (grandparentLC == parent) { parentsSibIndex = grandparentRC; }
 
                                                        //whichever of my greatgrandparents children was my grandparent
                                                        if (greatgrandparentLC == grandparent) { greatgrandparentLC = parentsSibIndex; }
@@ -379,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);
@@ -400,8 +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");
@@ -415,17 +425,19 @@ int Tree::populateNewTree(vector<Node>& oldtree, int node, int& index) {
                if (oldtree[node].getLChild() != -1) {
                        int rc = populateNewTree(oldtree, oldtree[node].getLChild(), index);
                        int lc = populateNewTree(oldtree, oldtree[node].getRChild(), index);
-                       
+
                        tree[index].setChildren(lc, rc);
-                       index++;
+                       tree[rc].setParent(index);
+                       tree[lc].setParent(index);
                        
-                       return (index-1);
+                       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());
-                       
-                       tree[indexInNewTree].setParent(index);
                        return indexInNewTree;
-                       
                }
        }
        catch(exception& e) {
@@ -895,7 +907,69 @@ try {
                exit(1);
        }
 }
-                                                                         
+/*****************************************************************/
+void Tree::printBranch(int node, ostream& out, string mode, vector<Node>& theseNodes) {
+       try {
+               
+               // you are not a leaf
+               if (theseNodes[node].getLChild() != -1) {
+                       out << "(";
+                       printBranch(theseNodes[node].getLChild(), out, mode);
+                       out << ",";
+                       printBranch(theseNodes[node].getRChild(), out, mode);
+                       out << ")";
+                       if (mode == "branch") {
+                               //if there is a branch length then print it
+                               if (theseNodes[node].getBranchLength() != -1) {
+                                       out << ":" << theseNodes[node].getBranchLength();
+                               }
+                       }else if (mode == "boot") {
+                               //if there is a label then print it
+                               if (theseNodes[node].getLabel() != -1) {
+                                       out << theseNodes[node].getLabel();
+                               }
+                       }else if (mode == "both") {
+                               if (theseNodes[node].getLabel() != -1) {
+                                       out << theseNodes[node].getLabel();
+                               }
+                               //if there is a branch length then print it
+                               if (theseNodes[node].getBranchLength() != -1) {
+                                       out << ":" << theseNodes[node].getBranchLength();
+                               }
+                       }
+               }else { //you are a leaf
+                       string leafGroup = globaldata->gTreemap->getGroup(theseNodes[node].getName());
+                       
+                       if (mode == "branch") {
+                               out << leafGroup; 
+                               //if there is a branch length then print it
+                               if (theseNodes[node].getBranchLength() != -1) {
+                                       out << ":" << theseNodes[node].getBranchLength();
+                               }
+                       }else if (mode == "boot") {
+                               out << leafGroup; 
+                               //if there is a label then print it
+                               if (theseNodes[node].getLabel() != -1) {
+                                       out << theseNodes[node].getLabel();
+                               }
+                       }else if (mode == "both") {
+                               out << theseNodes[node].getName();
+                               if (theseNodes[node].getLabel() != -1) {
+                                       out << theseNodes[node].getLabel();
+                               }
+                               //if there is a branch length then print it
+                               if (theseNodes[node].getBranchLength() != -1) {
+                                       out << ":" << theseNodes[node].getBranchLength();
+                               }
+                       }
+               }
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "Tree", "printBranch");
+               exit(1);
+       }
+}
 /*****************************************************************/
 
 void Tree::printTree() {