7 #include "../Tree/Tree.h"
20 return positiveLengths_ ?
21 std::max(lambda_ * (matrix_(pair[0], pos) - branchLengths[0]) + (1 - lambda_) * (matrix_(pair[1], pos) - branchLengths[1]), 0.)
22 : lambda_ * (matrix_(pair[0], pos) - branchLengths[0]) + (1 - lambda_) * (matrix_(pair[1], pos) - branchLengths[1]);
28 for (
size_t i = 0; i < matrix_.size(); i++)
30 currentNodes_[i] = getLeafNode(
static_cast<int>(i), matrix_.getName(i));
32 int idNextNode =
static_cast<int>(matrix_.size());
33 vector<double> newDist(matrix_.size());
34 vector<double> newVar(matrix_.size());
37 while (currentNodes_.size() > (rootTree_ ? 2 : 3))
41 vector<size_t> bestPair = getBestPair();
42 vector<double> distances = computeBranchLengthsForPair(bestPair);
43 Node* best1 = currentNodes_[bestPair[0]];
44 Node* best2 = currentNodes_[bestPair[1]];
48 Node* parent = getParentNode(idNextNode++, best1, best2);
51 if (variance_(bestPair[0], bestPair[1]) == 0)
55 for (map<size_t, Node*>::iterator i = currentNodes_.begin(); i != currentNodes_.end(); i++)
58 if (
id != bestPair[0] &&
id != bestPair[1])
59 lambda_ += (variance_(bestPair[1],
id) - variance_(bestPair[0],
id));
61 double div = 2 *
static_cast<double>(currentNodes_.size() - 2) * variance_(bestPair[0], bestPair[1]);
70 for (map<size_t, Node*>::iterator i = currentNodes_.begin(); i != currentNodes_.end(); i++)
73 if (
id != bestPair[0] &&
id != bestPair[1])
75 newDist[id] = computeDistancesFromPair(bestPair, distances,
id);
76 newVar[id] = lambda_ * variance_(bestPair[0],
id) + (1 - lambda_) * variance_(bestPair[1],
id) - lambda_ * (1 - lambda_) * variance_(bestPair[0], bestPair[1]);
84 currentNodes_[bestPair[0]] = parent;
85 currentNodes_.erase(bestPair[1]);
86 for (map<size_t, Node*>::iterator i = currentNodes_.begin(); i != currentNodes_.end(); i++)
89 matrix_( bestPair[0],
id) = matrix_(
id, bestPair[0]) = newDist[id];
90 variance_(bestPair[0],
id) = variance_(
id, bestPair[0]) = newVar[id];
93 finalStep(idNextNode);
double computeDistancesFromPair(const std::vector< size_t > &pair, const std::vector< double > &branchLengths, size_t pos)
Actualizes the distance matrix according to a given pair and the corresponding branch lengths.
void computeTree()
Compute the tree corresponding to the distance matrix.
The phylogenetic node class.
virtual void setDistanceToFather(double distance)
Set or update the distance toward the father node.
Defines the basic types of data flow nodes.