bpp-phyl3 3.0.0
AbstractNonHomogeneousTreeLikelihood.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_ABSTRACTNONHOMOGENEOUSTREELIKELIHOOD_H
6#define BPP_PHYL_LEGACY_LIKELIHOOD_ABSTRACTNONHOMOGENEOUSTREELIKELIHOOD_H
7
8
11
12// From the STL:
13#include <memory>
14
15namespace bpp
16{
23 public virtual NonHomogeneousTreeLikelihood,
25{
26public:
28 public ConstSiteModelIterator
29 {
30private:
31 std::vector<ConstNoPartitionSiteModelDescription> siteModelDescriptions_;
32 size_t index_;
33 size_t nbModels_;
34
35public:
36 ConstNonHomogeneousSiteModelIterator(std::shared_ptr<const SubstitutionModelSet> modelSet) :
37 siteModelDescriptions_(), index_(0), nbModels_(modelSet->getNumberOfModels())
38 {
39 for (size_t i = 0; i < nbModels_; ++i)
40 {
41 siteModelDescriptions_.push_back(ConstNoPartitionSiteModelDescription(modelSet->getModel(i), modelSet->getNodesWithModel(i)));
42 }
43 }
44
45public:
47 {
48 if (!hasNext())
49 throw Exception("AbstractNonHomogeneousTreeLikelihood::ConstHomogeneousSiteModelIterator::next(). No more site in the set.");
51 }
52
53 bool hasNext() const { return index_ < nbModels_; }
54 };
55
56protected:
57 std::shared_ptr<SubstitutionModelSet> modelSet_;
59
60 mutable std::map<int, VVVdouble> pxy_;
61
62 mutable std::map<int, VVVdouble> dpxy_;
63
64 mutable std::map<int, VVVdouble> d2pxy_;
65
66 std::vector<double> rootFreqs_;
67
74 std::vector<Node*> nodes_;
75
79 mutable std::map<int, const Node*> idToNode_;
80
81 // some values we'll need:
82 size_t nbSites_, // the number of sites in the container
83 nbDistinctSites_, // the number of distinct sites
84 nbClasses_, // the number of rate classes
85 nbStates_, // the number of states in the alphabet
86 nbNodes_; // the number of nodes in the tree
87
89
92 std::shared_ptr<ConstraintInterface> brLenConstraint_;
93
96
97public:
99 const Tree& tree,
100 std::shared_ptr<SubstitutionModelSet> modelSet,
101 std::shared_ptr<DiscreteDistributionInterface> rDist,
102 bool verbose = true,
103 bool reparametrizeRoot = true);
104
111
118
120
121private:
125 void init_(
126 const Tree& tree,
127 std::shared_ptr<SubstitutionModelSet> modelSet,
128 std::shared_ptr<DiscreteDistributionInterface> rDist,
129 bool verbose);
130
131public:
139 size_t getNumberOfStates() const override { return modelSet_->getNumberOfStates(); }
140
141 const std::vector<int>& getAlphabetStates() const override { return modelSet_->getAlphabetStates(); }
142
143 int getAlphabetStateAsInt(size_t i) const override { return modelSet_->getAlphabetStateAsInt(i); }
144
145 std::string getAlphabetStateAsChar(size_t i) const override { return modelSet_->getAlphabetStateAsChar(i); }
146
147 void initialize() override;
148
150
152
154 {
156 }
157
158 std::shared_ptr<const TransitionModelInterface> getModelForNode(int nodeId) const override
159 {
160 return modelSet_->getModelForNode(nodeId);
161 }
162
163 std::shared_ptr<TransitionModelInterface> getModelForNode(int nodeId) override
164 {
165 return modelSet_->getModelForNode(nodeId);
166 }
167
168 const std::vector<double>& getRootFrequencies(size_t siteIndex) const override { return rootFreqs_; }
169
170 VVVdouble getTransitionProbabilitiesPerRateClass(int nodeId, size_t siteIndex) const override { return pxy_[nodeId]; }
171
173 {
174 return new ConstNoPartitionBranchModelIterator(modelSet_->getModelForNode(nodeId), nbDistinctSites_);
175 }
176
177 ConstSiteModelIterator* getNewSiteModelIterator(size_t siteIndex) const override
178 {
180 }
181
191 std::shared_ptr<const SubstitutionModelSet> getSubstitutionModelSet() const override { return modelSet_; }
192
193 const SubstitutionModelSet& substitutionModelSet() const override { return *modelSet_; }
194
195 std::shared_ptr<SubstitutionModelSet> getSubstitutionModelSet() override { return modelSet_; }
196
198
199 void setSubstitutionModelSet(std::shared_ptr<SubstitutionModelSet> modelSet) override;
200
202 {
203 return modelSet_->getRootFrequenciesParameters();
204 }
207public:
208 // Specific methods:
209
214 virtual void initParameters();
215
221 virtual void applyParameters();
222
223 virtual void initBranchLengthsParameters(bool verbose = true);
224
225 virtual void setMinimumBranchLength(double minimum)
226 {
227 if (minimum > maximumBrLen_)
228 throw Exception("AbstractNonHomogeneousTreeLikelihood::setMinimumBranchLength. Minimum branch length should be lower than the maximum one: " + TextTools::toString(maximumBrLen_));
229 minimumBrLen_ = minimum;
230 brLenConstraint_ = std::make_shared<IntervalConstraint>(minimumBrLen_, maximumBrLen_, true, true);
232 }
233
234 virtual void setMaximumBranchLength(double maximum)
235 {
236 if (maximum < minimumBrLen_)
237 throw Exception("AbstractNonHomogeneousTreeLikelihood::setMaximumBranchLength. Maximum branch length should be higher than the minimum one: " + TextTools::toString(minimumBrLen_));
238 maximumBrLen_ = maximum;
239 brLenConstraint_ = std::make_shared<IntervalConstraint>(minimumBrLen_, maximumBrLen_, true, true);
241 }
242
243 virtual double getMinimumBranchLength() const { return minimumBrLen_; }
244 virtual double getMaximumBranchLength() const { return maximumBrLen_; }
245
246protected:
254 virtual void computeTransitionProbabilitiesForNode(const Node* node);
255};
256} // end of namespace bpp.
257#endif // BPP_PHYL_LEGACY_LIKELIHOOD_ABSTRACTNONHOMOGENEOUSTREELIKELIHOOD_H
Partial implementation of the DiscreteRatesAcrossSitesTreeLikelihood interface.
ParameterList getRateDistributionParameters() const
Get the parameters associated to the rate distribution.
Partial implementation for branch non-homogeneous models of the TreeLikelihood interface.
virtual void initParameters()
This builds the parameters list from all parametrizable objects, i.e. substitution model,...
ParameterList getBranchLengthsParameters() const override
Get the branch lengths parameters.
const SubstitutionModelSet & substitutionModelSet() const override
AbstractNonHomogeneousTreeLikelihood & operator=(const AbstractNonHomogeneousTreeLikelihood &lik)
Assignation operator.
void initialize() override
Init the likelihood object.
virtual void computeAllTransitionProbabilities()
Fill the pxy_, dpxy_ and d2pxy_ arrays for all nodes.
std::shared_ptr< const TransitionModelInterface > getModelForNode(int nodeId) const override
Get the model associated to a given node.
std::shared_ptr< TransitionModelInterface > getModelForNode(int nodeId) override
Get the substitution model associated to a given node.
ConstSiteModelIterator * getNewSiteModelIterator(size_t siteIndex) const override
void setSubstitutionModelSet(std::shared_ptr< SubstitutionModelSet > modelSet) override
Set the substitution models for this instance.
std::shared_ptr< ConstraintInterface > brLenConstraint_
void init_(const Tree &tree, std::shared_ptr< SubstitutionModelSet > modelSet, std::shared_ptr< DiscreteDistributionInterface > rDist, bool verbose)
Method called by constructor.
ConstBranchModelIterator * getNewBranchModelIterator(int nodeId) const override
const std::vector< double > & getRootFrequencies(size_t siteIndex) const override
Get the values of the frequencies for each state in the alphabet at the root node.
ParameterList getSubstitutionModelParameters() const override
Get the parameters associated to substitution model(s).
virtual void computeTransitionProbabilitiesForNode(const Node *node)
Fill the pxy_, dpxy_ and d2pxy_ arrays for one node.
std::map< int, const Node * > idToNode_
An index linking nodes to their id, for faster access than the getNode() method.
AbstractNonHomogeneousTreeLikelihood(const Tree &tree, std::shared_ptr< SubstitutionModelSet > modelSet, std::shared_ptr< DiscreteDistributionInterface > rDist, bool verbose=true, bool reparametrizeRoot=true)
std::vector< Node * > nodes_
Pointer toward all nodes in the tree.
std::shared_ptr< const SubstitutionModelSet > getSubstitutionModelSet() const override
const std::vector< int > & getAlphabetStates() const override
VVVdouble getTransitionProbabilitiesPerRateClass(int nodeId, size_t siteIndex) const override
Retrieves all Pij(t) for a particular branch, defined by the upper node.
virtual void applyParameters()
All parameters are stored in a parameter list. This function apply these parameters to the substituti...
ParameterList getRateDistributionParameters() const override
Get the parameters associated to the rate distribution.
std::shared_ptr< SubstitutionModelSet > getSubstitutionModelSet() override
const Tree & tree() const
Get the tree (topology and branch lengths).
The phylogenetic node class.
Definition: Node.h:59
Specialization of the TreeLikelihood interface for the branch non-homogeneous and non-stationary mode...
Substitution models manager for non-homogeneous / non-reversible models of evolution.
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