bpp-phyl3  3.0.0
AbstractHomogeneousTreeLikelihood.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_LEGACY_LIKELIHOOD_ABSTRACTHOMOGENEOUSTREELIKELIHOOD_H
6 #define BPP_PHYL_LEGACY_LIKELIHOOD_ABSTRACTHOMOGENEOUSTREELIKELIHOOD_H
7 
8 
11 
12 // From STL:
13 #include <memory>
14 
15 namespace bpp
16 {
23  public virtual HomogeneousTreeLikelihood,
25 {
26 public:
28  public ConstSiteModelIterator
29  {
30 private:
32  size_t index_;
33 
34 public:
35  ConstHomogeneousSiteModelIterator(const Tree& tree, std::shared_ptr<const TransitionModelInterface> model) :
36  siteModelDescription_(model, tree.getBranchesId()),
37  index_(0) {}
38 
39 public:
41  {
42  if (!hasNext())
43  throw Exception("AbstractHomogeneousTreeLikelihood::ConstHomogeneousSiteModelIterator::next(). No more site in the set.");
44  index_++;
45  return &siteModelDescription_;
46  }
47 
48  bool hasNext() const { return index_ == 0; }
49  };
50 
51 protected:
52  std::shared_ptr<TransitionModelInterface> model_;
54 
55  mutable std::map<int, VVVdouble> pxy_;
56 
57  mutable std::map<int, VVVdouble> dpxy_;
58 
59  mutable std::map<int, VVVdouble> d2pxy_;
60 
61  std::vector<double> rootFreqs_;
62 
69  std::vector<Node*> nodes_;
70 
71  // some values we'll need:
72  size_t nbSites_, // the number of sites in the container
73  nbDistinctSites_, // the number of distinct sites
74  nbClasses_, // the number of rate classes
75  nbStates_, // the number of states in the alphabet
76  nbNodes_; // the number of nodes in the tree
77 
78  bool verbose_;
79 
80  double minimumBrLen_;
81  double maximumBrLen_;
82  std::shared_ptr<ConstraintInterface> brLenConstraint_;
83 
84 public:
86  const Tree& tree,
87  std::shared_ptr<TransitionModelInterface> model,
88  std::shared_ptr<DiscreteDistributionInterface> rDist,
89  bool checkRooted = true,
90  bool verbose = true);
91 
98 
105 
107 
108 private:
112  void init_(const Tree& tree,
113  std::shared_ptr<TransitionModelInterface> model,
114  std::shared_ptr<DiscreteDistributionInterface> rDist,
115  bool checkRooted,
116  bool verbose);
117 
118 public:
126  size_t getNumberOfStates() const { return model_->getNumberOfStates(); }
127 
128  const std::vector<int>& getAlphabetStates() const { return model_->getAlphabetStates(); }
129 
130  int getAlphabetStateAsInt(size_t i) const { return model_->getAlphabetStateAsInt(i); }
131 
132  std::string getAlphabetStateAsChar(size_t i) const { return model_->getAlphabetStateAsChar(i); }
133 
134  void initialize();
135 
137 
139 
141  {
143  }
144 
145  const std::vector<double>& getRootFrequencies(size_t siteIndex) const { return model_->getFrequencies(); }
146 
147  VVVdouble getTransitionProbabilitiesPerRateClass(int nodeId, size_t siteIndex) const { return pxy_[nodeId]; }
148 
150  {
152  }
153 
155  {
157  }
158 
168  std::shared_ptr<const TransitionModelInterface> getModel() const { return model_; }
169  std::shared_ptr<const TransitionModelInterface> getModel(int nodeId, size_t siteIndex) const { return model_; }
170 
171  std::shared_ptr<TransitionModelInterface> getModel() { return model_; }
172  std::shared_ptr<TransitionModelInterface> getModel(int nodeId, size_t siteIndex) { return model_; }
173 
174  void setModel(std::shared_ptr<TransitionModelInterface> model);
183  virtual std::shared_ptr<const SubstitutionModelInterface> getSubstitutionModel() const
184  {
185  return std::dynamic_pointer_cast<const SubstitutionModelInterface>(model_);
186  }
187 
197  virtual std::shared_ptr<const SubstitutionModelInterface> getSubstitutionModel(int nodeId, size_t siteIndex) const
198  {
199  return std::dynamic_pointer_cast<const SubstitutionModelInterface>(model_);
200  }
201 
202 public:
203  // Specific methods:
204 
209  virtual void initParameters();
210 
216  virtual void applyParameters();
217 
218  virtual void initBranchLengthsParameters(bool verbose = true);
219 
220  virtual void setMinimumBranchLength(double minimum)
221  {
222  if (minimum > maximumBrLen_)
223  throw Exception("AbstractHomogeneousTreeLikelihood::setMinimumBranchLength. Minimum branch length sould be lower than the maximum one: " + TextTools::toString(maximumBrLen_));
224  minimumBrLen_ = minimum;
225  brLenConstraint_ = std::make_shared<IntervalConstraint>(minimumBrLen_, maximumBrLen_, true, true);
227  }
228 
229  virtual void setMaximumBranchLength(double maximum)
230  {
231  if (maximum < minimumBrLen_)
232  throw Exception("AbstractHomogeneousTreeLikelihood::setMaximumBranchLength. Maximum branch length sould be higher than the minimum one: " + TextTools::toString(minimumBrLen_));
233  maximumBrLen_ = maximum;
234  brLenConstraint_ = std::make_shared<IntervalConstraint>(minimumBrLen_, maximumBrLen_, true, true);
236  }
237 
238  virtual double getMinimumBranchLength() const { return minimumBrLen_; }
239  virtual double getMaximumBranchLength() const { return maximumBrLen_; }
240 
241 protected:
245  virtual void computeAllTransitionProbabilities();
249  virtual void computeTransitionProbabilitiesForNode(const Node* node);
250 };
251 } // end of namespace bpp.
252 #endif // BPP_PHYL_LEGACY_LIKELIHOOD_ABSTRACTHOMOGENEOUSTREELIKELIHOOD_H
Partial implementation of the DiscreteRatesAcrossSitesTreeLikelihood interface.
ParameterList getRateDistributionParameters() const
Get the parameters associated to the rate distribution.
ConstHomogeneousSiteModelIterator(const Tree &tree, std::shared_ptr< const TransitionModelInterface > model)
Partial implementation for homogeneous model of the TreeLikelihood interface.
virtual void initParameters()
This builds the parameters list from all parametrizable objects, i.e. substitution model,...
VVVdouble getTransitionProbabilitiesPerRateClass(int nodeId, size_t siteIndex) const
Retrieves all Pij(t) for a particular branch, defined by the upper node.
virtual std::shared_ptr< const SubstitutionModelInterface > getSubstitutionModel(int nodeId, size_t siteIndex) const
Get a SubstitutionModel pointer toward the model associated to this instance, if possible.
std::vector< Node * > nodes_
Pointer toward all nodes in the tree.
std::shared_ptr< TransitionModelInterface > getModel(int nodeId, size_t siteIndex)
void setModel(std::shared_ptr< TransitionModelInterface > model)
std::shared_ptr< TransitionModelInterface > getModel()
virtual void applyParameters()
All parameters are stored in a parameter list. This function apply these parameters to the substituti...
std::shared_ptr< TransitionModelInterface > model_
const std::vector< double > & getRootFrequencies(size_t siteIndex) const
Get the values of the frequencies for each state in the alphabet at the root node.
ConstBranchModelIterator * getNewBranchModelIterator(int nodeId) const
virtual std::shared_ptr< const SubstitutionModelInterface > getSubstitutionModel() const
Get a SubstitutionModel pointer toward the model associated to this instance, if possible.
const std::vector< int > & getAlphabetStates() const
ParameterList getBranchLengthsParameters() const
Get the branch lengths parameters.
virtual void computeTransitionProbabilitiesForNode(const Node *node)
Fill the pxy_, dpxy_ and d2pxy_ arrays for one node.
ParameterList getRateDistributionParameters() const
Get the parameters associated to the rate distribution.
std::shared_ptr< ConstraintInterface > brLenConstraint_
std::shared_ptr< const TransitionModelInterface > getModel(int nodeId, size_t siteIndex) const
void init_(const Tree &tree, std::shared_ptr< TransitionModelInterface > model, std::shared_ptr< DiscreteDistributionInterface > rDist, bool checkRooted, bool verbose)
Method called by constructor.
ConstSiteModelIterator * getNewSiteModelIterator(size_t siteIndex) const
ParameterList getSubstitutionModelParameters() const
Get the parameters associated to substitution model(s).
AbstractHomogeneousTreeLikelihood(const Tree &tree, std::shared_ptr< TransitionModelInterface > model, std::shared_ptr< DiscreteDistributionInterface > rDist, bool checkRooted=true, bool verbose=true)
std::shared_ptr< const TransitionModelInterface > getModel() const
virtual void computeAllTransitionProbabilities()
Fill the pxy_, dpxy_ and d2pxy_ arrays for all nodes.
AbstractHomogeneousTreeLikelihood & operator=(const AbstractHomogeneousTreeLikelihood &lik)
Assignation operator.
const Tree & tree() const
Get the tree (topology and branch lengths).
std::shared_ptr< TreeTemplate< Node > > tree_
Specialization of the TreeLikelihood interface for the Homogeneous case.
The phylogenetic node class.
Definition: Node.h:59
Iterates through all models used for all sites on a given branch.
A pair of SubstitutionModel / BranchIterator.
Iterates through all models used for all branches on a given site.
Interface for phylogenetic tree objects.
Definition: Tree.h:115
std::string toString(T t)
Defines the basic types of data flow nodes.
std::vector< VVdouble > VVVdouble