5 #include "../Tree/NodeTemplate.h"
20 vector<size_t> bestPair(2);
22 for (map<size_t, Node*>::iterator i = currentNodes_.begin(); i != currentNodes_.end(); i++)
25 map<size_t, Node*>::iterator j = i;
27 for ( ; j != currentNodes_.end(); j++)
30 double dist = matrix_(
id, jd);
41 cout <<
"---------------------------------------------------------------------------------" << endl;
42 for (map<size_t, Node*>::iterator i = currentNodes_.begin(); i != currentNodes_.end(); i++)
45 map<size_t, Node*>::iterator j = i;
47 for ( ; j != currentNodes_.end(); j++)
50 double dist = matrix_(
id, jd);
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;
74 if (method_ ==
"Single")
81 else if (method_ ==
"Complete")
88 else if (method_ ==
"Median")
95 else if (method_ ==
"Average")
104 else if (method_ ==
"Ward")
106 double n1 =
static_cast<double>(
dynamic_cast<NodeTemplate<ClusterInfos>*
>(currentNodes_[pair[0]])->getInfos().numberOfLeaves);
107 double n2 =
static_cast<double>(
dynamic_cast<NodeTemplate<ClusterInfos>*
>(currentNodes_[pair[1]])->getInfos().numberOfLeaves);
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")
116 double n1 =
static_cast<double>(
dynamic_cast<NodeTemplate<ClusterInfos>*
>(currentNodes_[pair[0]])->getInfos().numberOfLeaves);
117 double n2 =
static_cast<double>(
dynamic_cast<NodeTemplate<ClusterInfos>*
>(currentNodes_[pair[1]])->getInfos().numberOfLeaves);
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);
134 map<size_t, Node*>::iterator it = currentNodes_.begin();
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;
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.
static const std::string AVERAGE
static const std::string CENTROID
virtual Node * getLeafNode(int id, const std::string &name)
Get a leaf node.
std::vector< size_t > getBestPair()
Get the best pair of nodes to agglomerate.
static const std::string COMPLETE
static const std::string MEDIAN
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...
static const std::string SINGLE
static const std::string WARD
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)