42 #include "../NodeTemplate.h" 62 vector<size_t> bestPair(2);
63 double distMin = -std::log(0.);
64 for (map<size_t, Node*>::iterator i = currentNodes_.begin(); i != currentNodes_.end(); i++)
67 map<size_t, Node*>::iterator j = i;
69 for ( ; j != currentNodes_.end(); j++)
72 double dist = matrix_(
id, jd);
81 if (distMin == -std::log(0.))
83 cout <<
"---------------------------------------------------------------------------------" << endl;
84 for (map<size_t, Node*>::iterator i = currentNodes_.begin(); i != currentNodes_.end(); i++)
87 map<size_t, Node*>::iterator j = i;
89 for ( ; j != currentNodes_.end(); j++)
92 double dist = matrix_(
id, jd);
97 cout <<
"---------------------------------------------------------------------------------" << endl;
99 throw Exception(
"Unexpected error: no minimum found in the distance matrix.");
107 double dist = matrix_(pair[0], pair[1]) / 2.;
115 double w1, w2, w3, w4;
116 if (method_ ==
"Single")
123 else if (method_ ==
"Complete")
130 else if (method_ ==
"Median")
137 else if (method_ ==
"Average")
139 double n1 =
static_cast<double>(
dynamic_cast<NodeTemplate<ClusterInfos>*
>(currentNodes_[pair[0]])->getInfos().numberOfLeaves);
140 double n2 =
static_cast<double>(
dynamic_cast<NodeTemplate<ClusterInfos>*
>(currentNodes_[pair[1]])->getInfos().numberOfLeaves);
146 else if (method_ ==
"Ward")
148 double n1 =
static_cast<double>(
dynamic_cast<NodeTemplate<ClusterInfos>*
>(currentNodes_[pair[0]])->getInfos().numberOfLeaves);
149 double n2 =
static_cast<double>(
dynamic_cast<NodeTemplate<ClusterInfos>*
>(currentNodes_[pair[1]])->getInfos().numberOfLeaves);
151 w1 = (n1 + n3) / (n1 + n2 + n3);
152 w2 = (n2 + n3) / (n1 + n2 + n3);
153 w3 = -n3 / (n1 + n2 + n3);
156 else if (method_ ==
"Centroid")
158 double n1 =
static_cast<double>(
dynamic_cast<NodeTemplate<ClusterInfos>*
>(currentNodes_[pair[0]])->getInfos().numberOfLeaves);
159 double n2 =
static_cast<double>(
dynamic_cast<NodeTemplate<ClusterInfos>*
>(currentNodes_[pair[1]])->getInfos().numberOfLeaves);
162 w3 = -n1 * n2 / pow(n1 + n2, 2.);
166 throw Exception(
"HierarchicalClustering::computeBranchLengthsForPair. unknown method '" + method_ +
"'.");
167 double d1 = matrix_(pair[0], pos);
168 double d2 = matrix_(pair[1], pos);
169 double d3 = matrix_(pair[0], pair[1]);
170 return w1 * d1 + w2 * d2 + w3 * d3 + w4* std::abs(d1 - d2);
176 map<size_t, Node*>::iterator it = currentNodes_.begin();
177 size_t i1 = it->first;
178 Node* n1 = it->second;
180 size_t i2 = it->first;
181 Node* n2 = it->second;
182 double d = matrix_(i1, i2) / 2;
virtual void addSon(size_t pos, Node *node)
virtual Node * getLeafNode(int id, const std::string &name)
Get a leaf node.
virtual void setInfos(const NodeInfos &infos)
Set the information to be associated to this node.
std::vector< double > computeBranchLengthsForPair(const std::vector< size_t > &pair)
Compute the branch lengths for two nodes to agglomerate.
static const std::string SINGLE
The phylogenetic tree class.
static const std::string COMPLETE
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...
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
static const std::string CENTROID
std::vector< size_t > getBestPair()
Get the best pair of nodes to agglomerate.
The phylogenetic node class.
TreeTemplate< Node > * getTree() const
Get the computed tree, if there is one.
virtual void setDistanceToFather(double distance)
Set or update the distance toward the father 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 WARD
virtual Node * getParentNode(int id, Node *son1, Node *son2)
Get an inner node.
static const std::string MEDIAN
static const std::string AVERAGE