+ tmap->setIndex(searchName, index);
+ }
+ catch(exception& e) {
+ m->errorOut(e, "Tree", "setIndex");
+ exit(1);
+ }
+}
+/*****************************************************************/
+int Tree::assembleTree(map<string, string> nameMap) {
+ try {
+ //save for later
+ names = nameMap;
+
+ //if user has given a names file we want to include that info in the pgroups and pcount info.
+ if(nameMap.size() != 0) { addNamesToCounts(nameMap); }
+
+ //build the pGroups in non leaf nodes to be used in the parsimony calcs.
+ for (int i = numLeaves; i < numNodes; i++) {
+ if (m->control_pressed) { return 1; }
+
+ tree[i].pGroups = (mergeGroups(i));
+ tree[i].pcount = (mergeGcounts(i));
+ }
+
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "Tree", "assembleTree");
+ exit(1);
+ }
+}
+/*****************************************************************
+int Tree::assembleTree(string n) {
+ try {
+
+ //build the pGroups in non leaf nodes to be used in the parsimony calcs.
+ for (int i = numLeaves; i < numNodes; i++) {
+ if (m->control_pressed) { return 1; }
+
+ tree[i].pGroups = (mergeGroups(i));
+ tree[i].pcount = (mergeGcounts(i));
+ }
+ //float B = clock();
+ //cout << "assembleTree\t" << (B-A) / CLOCKS_PER_SEC << endl;
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "Tree", "assembleTree");
+ exit(1);
+ }
+}
+/*****************************************************************/
+//assumes leaf node names are in groups and no names file - used by indicator command
+void Tree::getSubTree(Tree* Ctree, vector<string> Groups) {
+ try {
+
+ //copy Tree since we are going to destroy it
+ Tree* copy = new Tree(tmap);
+ copy->getCopy(Ctree);
+ map<string, string> empty;
+ copy->assembleTree(empty);
+
+ //we want to select some of the leaf nodes to create the output tree
+ //go through the input Tree starting at parents of leaves
+ for (int i = 0; i < numNodes; i++) {
+
+ //initialize leaf nodes
+ if (i <= (numLeaves-1)) {
+ tree[i].setName(Groups[i]);
+
+ //save group info
+ string group = tmap->getGroup(Groups[i]);
+ vector<string> tempGroups; tempGroups.push_back(group);
+ tree[i].setGroup(tempGroups);
+ groupNodeInfo[group].push_back(i);
+
+ //set pcount and pGroup for groupname to 1.
+ tree[i].pcount[group] = 1;
+ tree[i].pGroups[group] = 1;
+
+ //Treemap knows name, group and index to speed up search
+ tmap->setIndex(Groups[i], i);
+
+ //intialize non leaf nodes
+ }else if (i > (numLeaves-1)) {
+ tree[i].setName("");
+ vector<string> tempGroups;
+ tree[i].setGroup(tempGroups);
+ }
+ }
+
+ set<int> removedLeaves;
+ for (int i = 0; i < copy->getNumLeaves(); i++) {
+
+ if (removedLeaves.count(i) == 0) {
+
+ //am I in the 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();
+ int parentLC = copy->tree[parent].getLChild();
+
+ //if I am the right child, then my sib is the left child
+ int sibIndex = parentRC;
+ if (parentRC == i) { sibIndex = parentLC; }
+
+ string sibsName = copy->tree[sibIndex].getName();
+
+ //if yes, is my sibling
+ if ((m->inUsersGroups(sibsName, Groups)) || (sibsName == "")) {
+ //we both are okay no trimming required
+ }else{
+ //i am, my sib is not, so remove sib by setting my parent to my grandparent
+ int grandparent = copy->tree[parent].getParent();
+ int grandparentLC = copy->tree[grandparent].getLChild();
+ int grandparentRC = copy->tree[grandparent].getRChild();
+
+ //whichever of my granparents children was my parent now equals me
+ if (grandparentLC == parent) { grandparentLC = i; }
+ else { grandparentRC = i; }
+
+ copy->tree[i].setParent(grandparent);
+ copy->tree[i].setBranchLength((copy->tree[i].getBranchLength()+copy->tree[parent].getBranchLength()));
+ if (grandparent != -1) {
+ copy->tree[grandparent].setChildren(grandparentLC, grandparentRC);
+ }
+ removedLeaves.insert(sibIndex);
+ }
+ }else{
+ //find my siblings name
+ int parentRC = copy->tree[parent].getRChild();
+ int parentLC = copy->tree[parent].getLChild();
+
+ //if I am the right child, then my sib is the left child
+ int sibIndex = parentRC;
+ if (parentRC == i) { sibIndex = parentLC; }
+
+ string sibsName = copy->tree[sibIndex].getName();
+
+ //if no is my sibling
+ if ((m->inUsersGroups(sibsName, Groups)) || (sibsName == "")) {
+ //i am not, but my sib is
+ int grandparent = copy->tree[parent].getParent();
+ int grandparentLC = copy->tree[grandparent].getLChild();
+ int grandparentRC = copy->tree[grandparent].getRChild();
+
+ //whichever of my granparents children was my parent now equals my sib
+ if (grandparentLC == parent) { grandparentLC = sibIndex; }
+ else { grandparentRC = sibIndex; }
+
+ copy->tree[sibIndex].setParent(grandparent);
+ copy->tree[sibIndex].setBranchLength((copy->tree[sibIndex].getBranchLength()+copy->tree[parent].getBranchLength()));
+ 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, 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();
+
+ parentsSibIndex = grandparentLC;
+ if (grandparentLC == parent) { parentsSibIndex = grandparentRC; }
+
+ //whichever of my greatgrandparents children was my grandparent
+ if (greatgrandparentLC == grandparent) { greatgrandparentLC = parentsSibIndex; }
+ else { greatgrandparentRC = parentsSibIndex; }
+
+ copy->tree[parentsSibIndex].setParent(greatgrandparent);
+ copy->tree[parentsSibIndex].setBranchLength((copy->tree[parentsSibIndex].getBranchLength()+copy->tree[grandparent].getBranchLength()));
+ if (greatgrandparent != -1) {
+ copy->tree[greatgrandparent].setChildren(greatgrandparentLC, greatgrandparentRC);
+ }
+ }else{
+ copy->tree[parent].setParent(-1);
+ //cout << "issues with making subtree" << endl;
+ }
+ removedLeaves.insert(sibIndex);
+ removedLeaves.insert(i);
+ }
+ }
+ }
+ }
+ }
+
+ int root = 0;
+ for (int i = 0; i < copy->getNumNodes(); i++) {
+ //you found the root
+ if (copy->tree[i].getParent() == -1) { root = i; break; }
+ }
+
+ int nextSpot = numLeaves;
+ populateNewTree(copy->tree, root, nextSpot);
+
+ delete copy;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "Tree", "getSubTree");
+ exit(1);
+ }
+}
+/*****************************************************************/
+//assumes nameMap contains unique names as key or is empty.
+//assumes numLeaves defined in tree constructor equals size of seqsToInclude and seqsToInclude only contains unique seqs.
+int Tree::getSubTree(Tree* copy, vector<string> seqsToInclude, map<string, string> nameMap) {
+ try {
+
+ if (numLeaves != seqsToInclude.size()) { m->mothurOut("[ERROR]: numLeaves does not equal numUniques, cannot create subtree.\n"); m->control_pressed = true; return 0; }
+
+ getSubTree(copy, seqsToInclude);
+ if (nameMap.size() != 0) { addNamesToCounts(nameMap); }
+
+ //build the pGroups in non leaf nodes to be used in the parsimony calcs.
+ for (int i = numLeaves; i < numNodes; i++) {
+ if (m->control_pressed) { return 1; }
+
+ tree[i].pGroups = (mergeGroups(i));
+ tree[i].pcount = (mergeGcounts(i));
+ }
+
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "Tree", "getSubTree");
+ exit(1);
+ }
+}
+/*****************************************************************/
+int Tree::populateNewTree(vector<Node>& oldtree, int node, int& index) {
+ try {
+
+ 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);
+ 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 = tmap->getIndex(oldtree[node].getName());
+ return indexInNewTree;
+ }
+ }
+ catch(exception& e) {
+ m->errorOut(e, "Tree", "populateNewTree");
+ exit(1);
+ }
+}
+/*****************************************************************/
+void Tree::getCopy(Tree* copy, map<string, string> nameMap) {
+ try {
+
+ //for each node in the tree copy its info
+ for (int i = 0; i < numNodes; i++) {
+ //copy branch length
+ tree[i].setBranchLength(copy->tree[i].getBranchLength());
+
+ //copy parent
+ tree[i].setParent(copy->tree[i].getParent());
+
+ //copy children
+ tree[i].setChildren(copy->tree[i].getLChild(), copy->tree[i].getRChild());
+ }
+
+ if (nameMap.size() != 0) { addNamesToCounts(nameMap); }
+
+ //build the pGroups in non leaf nodes to be used in the parsimony calcs.
+ for (int i = numLeaves; i < numNodes; i++) {
+ if (m->control_pressed) { break; }
+
+ tree[i].pGroups = (mergeGroups(i));
+ tree[i].pcount = (mergeGcounts(i));
+ }
+ }
+ catch(exception& e) {
+ m->errorOut(e, "Tree", "getCopy");
+ exit(1);
+ }
+}
+/*****************************************************************/
+void Tree::getCopy(Tree* copy) {
+ try {
+
+ //for each node in the tree copy its info
+ for (int i = 0; i < numNodes; i++) {
+ //copy name
+ tree[i].setName(copy->tree[i].getName());
+
+ //copy group
+ tree[i].setGroup(copy->tree[i].getGroup());
+
+ //copy branch length
+ tree[i].setBranchLength(copy->tree[i].getBranchLength());
+
+ //copy parent
+ tree[i].setParent(copy->tree[i].getParent());
+
+ //copy children
+ tree[i].setChildren(copy->tree[i].getLChild(), copy->tree[i].getRChild());
+
+ //copy index in node and tmap
+ tree[i].setIndex(copy->tree[i].getIndex());
+ setIndex(copy->tree[i].getName(), getIndex(copy->tree[i].getName()));
+
+ //copy pGroups
+ tree[i].pGroups = copy->tree[i].pGroups;
+
+ //copy pcount
+ tree[i].pcount = copy->tree[i].pcount;
+ }
+
+ groupNodeInfo = copy->groupNodeInfo;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "Tree", "getCopy");
+ exit(1);
+ }
+}
+/*****************************************************************/
+//returns a map with a groupname and the number of times that group was seen in the children
+//for instance if your children are white and black then it would return a map with 2 entries
+// p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
+//and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
+
+map<string, int> Tree::mergeGroups(int i) {
+ try {
+ int lc = tree[i].getLChild();
+ int rc = tree[i].getRChild();
+
+ //set parsimony groups to left child
+ map<string,int> parsimony = tree[lc].pGroups;
+
+ int maxPars = 1;
+
+ //look at right child groups and update maxPars if right child has something higher for that group.
+ for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
+ it2 = parsimony.find(it->first);
+ if (it2 != parsimony.end()) {
+ parsimony[it->first]++;
+ }else {
+ parsimony[it->first] = 1;
+ }
+
+ if(parsimony[it->first] > maxPars){
+ maxPars = parsimony[it->first];
+ }
+ }
+
+ // this is true if right child had a greater parsimony for a certain group
+ if(maxPars > 1){
+ //erase all the groups that are only 1 because you found something with 2.
+ for(it=parsimony.begin();it!=parsimony.end();){
+ if(it->second == 1){
+ parsimony.erase(it++);
+ }else { it++; }
+ }
+ //set one remaining groups to 1
+ //so with our above example p[white] = 2 would be left and it would become p[white] = 1
+ for(it=parsimony.begin();it!=parsimony.end();it++){
+ parsimony[it->first] = 1;
+ }
+
+ }
+
+ return parsimony;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "Tree", "mergeGroups");
+ exit(1);
+ }
+}
+/*****************************************************************/
+//returns a map with a groupname and the number of times that group was seen in the children
+//for instance if your children are white and black then it would return a map with 2 entries
+// p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
+//and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
+
+map<string, int> Tree::mergeUserGroups(int i, vector<string> g) {
+ try {
+
+ int lc = tree[i].getLChild();
+ int rc = tree[i].getRChild();
+
+ //loop through nodes groups removing the ones the user doesn't want
+ for(it=tree[lc].pGroups.begin();it!=tree[lc].pGroups.end();){
+ if (m->inUsersGroups(it->first, g) != true) {
+ tree[lc].pGroups.erase(it++);
+ }else { it++; }
+ }
+
+ //loop through nodes groups removing the ones the user doesn't want
+ for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();){
+ if (m->inUsersGroups(it->first, g) != true) {
+ tree[rc].pGroups.erase(it++);
+ }else { it++; }
+ }
+
+ //set parsimony groups to left child
+ map<string,int> parsimony = tree[lc].pGroups;
+
+ int maxPars = 1;
+
+ //look at right child groups and update maxPars if right child has something higher for that group.
+ for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
+ it2 = parsimony.find(it->first);
+ if (it2 != parsimony.end()) {
+ parsimony[it->first]++;
+ }else {
+ parsimony[it->first] = 1;
+ }
+
+ if(parsimony[it->first] > maxPars){
+ maxPars = parsimony[it->first];
+ }
+ }
+
+ // this is true if right child had a greater parsimony for a certain group
+ if(maxPars > 1){
+ //erase all the groups that are only 1 because you found something with 2.
+ for(it=parsimony.begin();it!=parsimony.end();){
+ if(it->second == 1){
+ parsimony.erase(it++);
+ }else { it++; }
+ }
+
+ for(it=parsimony.begin();it!=parsimony.end();it++){
+ parsimony[it->first] = 1;
+ }
+ }
+
+ return parsimony;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "Tree", "mergeUserGroups");
+ exit(1);
+ }
+}
+
+
+/**************************************************************************************************/
+
+map<string,int> Tree::mergeGcounts(int position) {
+ try{
+ map<string,int>::iterator pos;
+
+ int lc = tree[position].getLChild();
+ int rc = tree[position].getRChild();
+
+ map<string,int> sum = tree[lc].pcount;
+
+ for(it=tree[rc].pcount.begin();it!=tree[rc].pcount.end();it++){
+ sum[it->first] += it->second;
+ }
+ return sum;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "Tree", "mergeGcounts");
+ exit(1);
+ }
+}
+/**************************************************************************************************/
+void Tree::randomLabels(vector<string> g) {
+ try {
+
+ //initialize groupNodeInfo
+ for (int i = 0; i < (tmap->getNamesOfGroups()).size(); i++) {
+ groupNodeInfo[(tmap->getNamesOfGroups())[i]].resize(0);
+ }
+
+ for(int i = 0; i < numLeaves; i++){
+ int z;
+ //get random index to switch with
+ z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
+
+ //you only want to randomize the nodes that are from a group the user wants analyzed, so
+ //if either of the leaf nodes you are about to switch are not in the users groups then you don't want to switch them.
+ bool treez, treei;
+
+ treez = m->inUsersGroups(tree[z].getGroup(), g);
+ treei = m->inUsersGroups(tree[i].getGroup(), g);
+
+ if ((treez == true) && (treei == true)) {
+ //switches node i and node z's info.
+ map<string,int> lib_hold = tree[z].pGroups;
+ tree[z].pGroups = (tree[i].pGroups);
+ tree[i].pGroups = (lib_hold);
+
+ vector<string> zgroup = tree[z].getGroup();
+ tree[z].setGroup(tree[i].getGroup());
+ tree[i].setGroup(zgroup);
+
+ string zname = tree[z].getName();
+ tree[z].setName(tree[i].getName());
+ tree[i].setName(zname);
+
+ map<string,int> gcount_hold = tree[z].pcount;
+ tree[z].pcount = (tree[i].pcount);
+ tree[i].pcount = (gcount_hold);
+ }
+
+ for (int k = 0; k < (tree[i].getGroup()).size(); k++) { groupNodeInfo[(tree[i].getGroup())[k]].push_back(i); }
+ for (int k = 0; k < (tree[z].getGroup()).size(); k++) { groupNodeInfo[(tree[z].getGroup())[k]].push_back(z); }
+ }