bpp-phyl3  3.0.0
AbstractAgglomerativeDistanceMethod.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_PHYL_DISTANCE_ABSTRACTAGGLOMERATIVEDISTANCEMETHOD_H
6 #define BPP_PHYL_DISTANCE_ABSTRACTAGGLOMERATIVEDISTANCEMETHOD_H
7 
8 
9 #include "../Tree/Node.h"
10 #include "../Tree/TreeTemplate.h"
11 #include "DistanceMethod.h"
12 
13 // From the STL:
14 #include <map>
15 
16 namespace bpp
17 {
28 {
29 protected:
31  std::unique_ptr<Tree> tree_;
32 
33  std::map<size_t, Node*> currentNodes_;
34  bool verbose_;
35  bool rootTree_;
36 
37 public:
39  bool verbose = true,
40  bool rootTree = false) :
41  matrix_(0),
42  tree_(nullptr),
43  currentNodes_(),
44  verbose_(verbose),
45  rootTree_(rootTree) {}
46 
48  const DistanceMatrix& matrix,
49  bool verbose = true,
50  bool rootTree = false) :
51  matrix_(0),
52  tree_(nullptr),
53  currentNodes_(),
54  verbose_(verbose),
55  rootTree_(rootTree)
56  {
57  setDistanceMatrix(matrix);
58  }
59 
61 
64  {
65  // Hard copy of inner tree:
66  if (a.tree_)
67  tree_.reset(new TreeTemplate<Node>(*a.tree_));
68  }
69 
71  {
72  matrix_ = a.matrix_;
73  // Hard copy of inner tree:
74  if (a.tree_)
75  tree_.reset(new TreeTemplate<Node>(*a.tree_));
76  else tree_ = 0;
77  currentNodes_.clear();
78  verbose_ = a.verbose_;
79  rootTree_ = a.rootTree_;
80  return *this;
81  }
82 
83 public:
84  virtual void setDistanceMatrix(const DistanceMatrix& matrix) override;
85 
86  bool hasTree() const override
87  {
88  return tree_ != nullptr;
89  }
90 
91  const Tree& tree() const override
92  {
93  if (tree_)
94  return *tree_;
95 
96  throw NullPointerException("AbstractAgglomerativeDistanceMethod::tree(). No tree was computed.");
97  }
98 
111  void computeTree() override;
112 
113  void setVerbose(bool yn) override { verbose_ = yn; }
114  bool isVerbose() const override { return verbose_; }
115 
116 protected:
132  virtual std::vector<size_t> getBestPair() = 0;
133 
147  virtual std::vector<double> computeBranchLengthsForPair(const std::vector<size_t>& pair) = 0;
148 
157  virtual double computeDistancesFromPair(const std::vector<size_t>& pair, const std::vector<double>& branchLengths, size_t pos) = 0;
158 
164  virtual void finalStep(int idRoot) = 0;
165 
175  virtual Node* getLeafNode(int id, const std::string& name);
176 
187  virtual Node* getParentNode(int id, Node* son1, Node* son2);
189 };
190 } // end of namespace bpp.
191 #endif // BPP_PHYL_DISTANCE_ABSTRACTAGGLOMERATIVEDISTANCEMETHOD_H
Partial implementation of the AgglomerativeDistanceMethod interface.
virtual Node * getLeafNode(int id, const std::string &name)
Get a leaf node.
AbstractAgglomerativeDistanceMethod(const AbstractAgglomerativeDistanceMethod &a)
AbstractAgglomerativeDistanceMethod & operator=(const AbstractAgglomerativeDistanceMethod &a)
virtual double computeDistancesFromPair(const std::vector< size_t > &pair, const std::vector< double > &branchLengths, size_t pos)=0
Actualizes the distance matrix according to a given pair and the corresponding branch lengths.
virtual std::vector< double > computeBranchLengthsForPair(const std::vector< size_t > &pair)=0
Compute the branch lengths for two nodes to agglomerate.
virtual void setDistanceMatrix(const DistanceMatrix &matrix) override
Set the distance matrix to use.
virtual Node * getParentNode(int id, Node *son1, Node *son2)
Get an inner node.
virtual std::vector< size_t > getBestPair()=0
Get the best pair of nodes to agglomerate.
AbstractAgglomerativeDistanceMethod(const DistanceMatrix &matrix, bool verbose=true, bool rootTree=false)
virtual void finalStep(int idRoot)=0
Method called when there ar eonly three remaining node to agglomerate, and creates the root node of t...
AbstractAgglomerativeDistanceMethod(bool verbose=true, bool rootTree=false)
void computeTree() override
Compute the tree corresponding to the distance matrix.
Interface for agglomerative distance methods.
The phylogenetic node class.
Definition: Node.h:59
The phylogenetic tree class.
Definition: TreeTemplate.h:59
Interface for phylogenetic tree objects.
Definition: Tree.h:115
Defines the basic types of data flow nodes.