5#include "../Tree/NodeTemplate.h"
11const string HierarchicalClustering::COMPLETE =
"Complete";
12const string HierarchicalClustering::SINGLE =
"Single";
13const string HierarchicalClustering::AVERAGE =
"Average";
14const string HierarchicalClustering::MEDIAN =
"Median";
15const string HierarchicalClustering::WARD =
"Ward";
16const string HierarchicalClustering::CENTROID =
"Centroid";
18vector<size_t> HierarchicalClustering::getBestPair()
20 vector<size_t> bestPair(2);
25 map<size_t, Node*>::iterator j = i;
41 cout <<
"---------------------------------------------------------------------------------" << endl;
45 map<size_t, Node*>::iterator j = i;
55 cout <<
"---------------------------------------------------------------------------------" << endl;
57 throw Exception(
"Unexpected error: no minimum found in the distance matrix.");
65 double dist =
matrix_(pair[0], pair[1]) / 2.;
73 double w1, w2, w3, w4;
109 w1 = (n1 + n3) / (n1 + n2 + n3);
110 w2 = (n2 + n3) / (n1 + n2 + n3);
111 w3 = -n3 / (n1 + n2 + n3);
114 else if (
method_ ==
"Centroid")
120 w3 = -n1 * n2 /
pow(n1 + n2, 2.);
124 throw Exception(
"HierarchicalClustering::computeBranchLengthsForPair. unknown method '" +
method_ +
"'.");
125 double d1 =
matrix_(pair[0], pos);
126 double d2 =
matrix_(pair[1], pos);
127 double d3 =
matrix_(pair[0], pair[1]);
128 return w1 * d1 + w2 * d2 + w3 * d3 + w4 *
std::abs(d1 - d2);
135 size_t i1 = it->first;
136 Node* n1 = it->second;
138 size_t i2 = it->first;
139 Node* n2 = it->second;
140 double d =
matrix_(i1, i2) / 2;
std::unique_ptr< Tree > tree_
std::map< size_t, Node * > currentNodes_
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.
std::vector< double > computeBranchLengthsForPair(const std::vector< size_t > &pair)
Compute the branch lengths for two nodes to agglomerate.
virtual Node * getLeafNode(int id, const std::string &name)
Get a leaf node.
virtual Node * getParentNode(int id, Node *son1, Node *son2)
Get an inner node.
void finalStep(int idRoot)
Method called when there ar eonly three remaining node to agglomerate, and creates the root node of t...
virtual void setInfos(const NodeInfos &infos)
Set the information to be associated to this node.
The phylogenetic node class.
virtual void setDistanceToFather(double distance)
Set or update the distance toward the father node.
virtual void addSon(size_t pos, Node *node)
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
The phylogenetic tree class.
Defines the basic types of data flow nodes.
ExtendedFloat pow(const ExtendedFloat &ef, double exp)
ExtendedFloat abs(const ExtendedFloat &ef)