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
//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);
tree[i].setGroup(tempGroups);
}
}
+
}
catch(exception& e) {
m->errorOut(e, "Tree", "Tree");
//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);
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();
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{
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; }
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);
int nextSpot = numLeaves;
populateNewTree(copy->tree, root, nextSpot);
-
-
}
catch(exception& e) {
m->errorOut(e, "Tree", "getCopy");
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) {
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() {