+/*****************************************************************/
+Tree::Tree(TreeMap* t, vector< vector<double> >& sims) : tmap(t) {
+ try {
+ m = MothurOut::getInstance();
+
+ if (m->runParse == true) { parseTreeFile(); m->runParse = false; }
+ numLeaves = m->Treenames.size();
+ numNodes = 2*numLeaves - 1;
+
+ tree.resize(numNodes);
+
+ //initialize groupNodeInfo
+ for (int i = 0; i < (tmap->getNamesOfGroups()).size(); i++) {
+ groupNodeInfo[(tmap->getNamesOfGroups())[i]].resize(0);
+ }
+
+ //initialize tree with correct number of nodes, name and group info.
+ for (int i = 0; i < numNodes; i++) {
+ //initialize leaf nodes
+ if (i <= (numLeaves-1)) {
+ tree[i].setName(m->Treenames[i]);
+
+ //save group info
+ string group = tmap->getGroup(m->Treenames[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(m->Treenames[i], i);
+
+ //intialize non leaf nodes
+ }else if (i > (numLeaves-1)) {
+ tree[i].setName("");
+ vector<string> tempGroups;
+ tree[i].setGroup(tempGroups);
+ }
+ }
+
+ //build tree from matrix
+ //initialize indexes
+ map<int, int> indexes; //maps row in simMatrix to vector index in the tree
+ for (int g = 0; g < numLeaves; g++) { indexes[g] = g; }
+
+ //do merges and create tree structure by setting parents and children
+ //there are numGroups - 1 merges to do
+ for (int i = 0; i < (numLeaves - 1); i++) {
+ float largest = -1000.0;
+
+ if (m->control_pressed) { break; }
+
+ int row, column;
+ //find largest value in sims matrix by searching lower triangle
+ for (int j = 1; j < sims.size(); j++) {
+ for (int k = 0; k < j; k++) {
+ if (sims[j][k] > largest) { largest = sims[j][k]; row = j; column = k; }
+ }
+ }
+
+ //set non-leaf node info and update leaves to know their parents
+ //non-leaf
+ tree[numLeaves + i].setChildren(indexes[row], indexes[column]);
+
+ //parents
+ tree[indexes[row]].setParent(numLeaves + i);
+ tree[indexes[column]].setParent(numLeaves + i);
+
+ //blength = distance / 2;
+ float blength = ((1.0 - largest) / 2);
+
+ //branchlengths
+ tree[indexes[row]].setBranchLength(blength - tree[indexes[row]].getLengthToLeaves());
+ tree[indexes[column]].setBranchLength(blength - tree[indexes[column]].getLengthToLeaves());
+
+ //set your length to leaves to your childs length plus branchlength
+ tree[numLeaves + i].setLengthToLeaves(tree[indexes[row]].getLengthToLeaves() + tree[indexes[row]].getBranchLength());
+
+
+ //update index
+ indexes[row] = numLeaves+i;
+ indexes[column] = numLeaves+i;
+
+ //remove highest value that caused the merge.
+ sims[row][column] = -1000.0;
+ sims[column][row] = -1000.0;
+
+ //merge values in simsMatrix
+ for (int n = 0; n < sims.size(); n++) {
+ //row becomes merge of 2 groups
+ sims[row][n] = (sims[row][n] + sims[column][n]) / 2;
+ sims[n][row] = sims[row][n];
+ //delete column
+ sims[column][n] = -1000.0;
+ sims[n][column] = -1000.0;
+ }
+ }
+
+ //adjust tree to make sure root to tip length is .5
+ int root = findRoot();
+ tree[root].setBranchLength((0.5 - tree[root].getLengthToLeaves()));
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "Tree", "Tree");
+ exit(1);
+ }
+}