+map<string, int> PhyloDiversityCommand::getRootForGroups(Tree* t){
+ try {
+ map<string, int> roots; //maps group to root for group, may not be root of tree
+ map<string, bool> done;
+
+ //initialize root for all groups to -1
+ for (int k = 0; k < (t->getTreeMap())->getNamesOfGroups().size(); k++) { done[(t->getTreeMap())->getNamesOfGroups()[k]] = false; }
+
+ for (int i = 0; i < t->getNumLeaves(); i++) {
+
+ vector<string> groups = t->tree[i].getGroup();
+
+ int index = t->tree[i].getParent();
+
+ for (int j = 0; j < groups.size(); j++) {
+
+ if (done[groups[j]] == false) { //we haven't found the root for this group yet
+
+ done[groups[j]] = true;
+ roots[groups[j]] = i; //set root to self to start
+
+ //while you aren't at root
+ while(t->tree[index].getParent() != -1){
+
+ if (m->control_pressed) { return roots; }
+
+ //do both your chidren have have descendants from the users groups?
+ int lc = t->tree[index].getLChild();
+ int rc = t->tree[index].getRChild();
+
+ int LpcountSize = 0;
+ map<string, int>:: iterator itGroup = t->tree[lc].pcount.find(groups[j]);
+ if (itGroup != t->tree[lc].pcount.end()) { LpcountSize++; }
+
+ int RpcountSize = 0;
+ itGroup = t->tree[rc].pcount.find(groups[j]);
+ if (itGroup != t->tree[rc].pcount.end()) { RpcountSize++; }
+
+ if ((LpcountSize != 0) && (RpcountSize != 0)) { //possible root
+ roots[groups[j]] = index;
+ }else { ;}
+
+ index = t->tree[index].getParent();
+ }
+ }
+ }
+ }
+
+ return roots;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "PhyloDiversityCommand", "getRootForGroups");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************