bpp-phyl3 3.0.0
PGMA.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: The Bio++ Development Group
2//
3// SPDX-License-Identifier: CECILL-2.1
4
5#include "../Tree/NodeTemplate.h"
6#include "../Tree/Tree.h"
7#include "../Tree/TreeTemplate.h"
8#include "../Tree/TreeTemplateTools.h"
9#include "PGMA.h"
10
11using namespace bpp;
12
13// From the STL:
14#include <cmath>
15#include <iostream>
16
17using namespace std;
18
19vector<size_t> PGMA::getBestPair()
20{
21 vector<size_t> bestPair(2);
22 double distMin = -std::log(0.);
23 for (map<size_t, Node*>::iterator i = currentNodes_.begin(); i != currentNodes_.end(); i++)
24 {
25 size_t id = i->first;
26 map<size_t, Node*>::iterator j = i;
27 j++;
28 for ( ; j != currentNodes_.end(); j++)
29 {
30 size_t jd = j->first;
31 double dist = matrix_(id, jd);
32 if (dist < distMin)
33 {
34 distMin = dist;
35 bestPair[0] = id;
36 bestPair[1] = jd;
37 }
38 }
39 }
40
41 if (distMin == -std::log(0.))
42 {
43 throw Exception("Unexpected error: no minimum found in the distance matrix.");
44 }
45
46 return bestPair;
47}
48
49vector<double> PGMA::computeBranchLengthsForPair(const vector<size_t>& pair)
50{
51 vector<double> d(2);
52 double dist = matrix_(pair[0], pair[1]) / 2.;
53 d[0] = dist - dynamic_cast<NodeTemplate<PGMAInfos>*>(currentNodes_[pair[0]])->getInfos().time;
54 d[1] = dist - dynamic_cast<NodeTemplate<PGMAInfos>*>(currentNodes_[pair[1]])->getInfos().time;
55 return d;
56}
57
58double PGMA::computeDistancesFromPair(const vector<size_t>& pair, const vector<double>& branchLengths, size_t pos)
59{
60 double w1, w2;
61 if (weighted_)
62 {
63 w1 = 1;
64 w2 = 1;
65 }
66 else
67 {
68 w1 = static_cast<double>(dynamic_cast<NodeTemplate<PGMAInfos>*>(currentNodes_[pair[0]])->getInfos().numberOfLeaves);
69 w2 = static_cast<double>(dynamic_cast<NodeTemplate<PGMAInfos>*>(currentNodes_[pair[1]])->getInfos().numberOfLeaves);
70 }
71 return (w1 * matrix_(pair[0], pos) + w2 * matrix_(pair[1], pos)) / (w1 + w2);
72}
73
74void PGMA::finalStep(int idRoot)
75{
77 map<size_t, Node*>::iterator it = currentNodes_.begin();
78 size_t i1 = it->first;
79 Node* n1 = it->second;
80 it++;
81 size_t i2 = it->first;
82 Node* n2 = it->second;
83 double d = matrix_(i1, i2) / 2;
84 root->addSon(n1);
85 root->addSon(n2);
86 n1->setDistanceToFather(d - dynamic_cast<NodeTemplate<PGMAInfos>*>(n1)->getInfos().time);
87 n2->setDistanceToFather(d - dynamic_cast<NodeTemplate<PGMAInfos>*>(n2)->getInfos().time);
89}
90
91Node* PGMA::getLeafNode(int id, const string& name)
92{
93 PGMAInfos infos;
94 infos.numberOfLeaves = 1;
95 infos.time = 0.;
97 leaf->setInfos(infos);
98 return leaf;
99}
100
101Node* PGMA::getParentNode(int id, Node* son1, Node* son2)
102{
103 PGMAInfos infos;
104 infos.numberOfLeaves =
105 dynamic_cast<NodeTemplate<PGMAInfos>*>(son1)->getInfos().numberOfLeaves
106 + dynamic_cast<NodeTemplate<PGMAInfos>*>(son2)->getInfos().numberOfLeaves;
107 infos.time = dynamic_cast<NodeTemplate<PGMAInfos>*>(son1)->getInfos().time + son1->getDistanceToFather();
108 Node* parent = new NodeTemplate<PGMAInfos>(id);
109 dynamic_cast<NodeTemplate<PGMAInfos>*>(parent)->setInfos(infos);
110 parent->addSon(son1);
111 parent->addSon(son2);
112 return parent;
113}
The NodeTemplate class.
Definition: NodeTemplate.h:39
virtual void setInfos(const NodeInfos &infos)
Set the information to be associated to this node.
Definition: NodeTemplate.h:156
The phylogenetic node class.
Definition: Node.h:59
virtual void setDistanceToFather(double distance)
Set or update the distance toward the father node.
Definition: Node.h:266
virtual void addSon(size_t pos, Node *node)
Definition: Node.h:386
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
Definition: Node.h:250
std::vector< double > computeBranchLengthsForPair(const std::vector< size_t > &pair)
Compute the branch lengths for two nodes to agglomerate.
Definition: PGMA.cpp:49
virtual Node * getParentNode(int id, Node *son1, Node *son2)
Get an inner node.
Definition: PGMA.cpp:101
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.
Definition: PGMA.cpp:58
virtual Node * getLeafNode(int id, const std::string &name)
Get a leaf node.
Definition: PGMA.cpp:91
bool weighted_
Definition: PGMA.h:36
void finalStep(int idRoot)
Method called when there ar eonly three remaining node to agglomerate, and creates the root node of t...
Definition: PGMA.cpp:74
The phylogenetic tree class.
Definition: TreeTemplate.h:59
Defines the basic types of data flow nodes.
Inner data structure for WPGMA and UPGMA distance methods.
Definition: PGMA.h:19
size_t numberOfLeaves
Definition: PGMA.h:20
double time
Definition: PGMA.h:21