bpp-phyl3 3.0.0
AbstractTreeLikelihood.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_ABSTRACTTREELIKELIHOOD_H
6#define BPP_PHYL_LEGACY_LIKELIHOOD_ABSTRACTTREELIKELIHOOD_H
7
9
10#include "TreeLikelihood.h"
11#include "../../Model/SubstitutionModel.h"
12
13// From bpp-seq:
15
16// From the STL:
17#include <memory>
18
19namespace bpp
20{
36 public virtual TreeLikelihoodInterface,
38{
39public:
46 public BranchIterator
47 {
48private:
49 std::vector<int> nodesId_;
50 size_t index_;
51
52public:
53 SimpleBranchIterator(const std::vector<int>& nodesId) :
54 nodesId_(nodesId), index_(0) {}
55
56public:
57 int next()
58 {
59 if (!hasNext())
60 throw Exception("AbstractTreeLikelihood::SimpleBranchIterator::next(). No more branch in the set.");
61 return nodesId_[index_++];
62 }
63
64 bool hasNext() const { return index_ < nodesId_.size(); }
65 };
66
75 public SiteIterator
76 {
77private:
78 size_t maxIndex_;
79 size_t index_;
80 size_t offset_;
81
82public:
83 SimpleSiteIterator(size_t nbSites, size_t offset = 0) :
84 maxIndex_(nbSites), index_(0), offset_(offset) {}
85
86public:
87 size_t next()
88 {
89 if (!hasNext())
90 throw Exception("AbstractTreeLikelihood::SimpleSiteIterator::next(). No more site in the set.");
91 return offset_ + index_++;
92 }
93
94 bool hasNext() const { return index_ < maxIndex_; }
95 };
96
104 {
105private:
106 std::shared_ptr<const TransitionModelInterface> model_;
107 size_t nbSites_;
108
109public:
110 ConstNoPartitionBranchModelDescription(std::shared_ptr<const TransitionModelInterface> model, size_t nbSites) :
111 model_(model), nbSites_(nbSites) {}
112
114 model_(bmd.model_),
115 nbSites_(bmd.nbSites_)
116 {}
117
119 {
120 model_ = bmd.model_;
121 nbSites_ = bmd.nbSites_;
122 return *this;
123 }
124
125public:
126 std::shared_ptr<const TransitionModelInterface> getModel() const override { return model_; }
127 const TransitionModelInterface& model() const override { return *model_; }
128
129 std::shared_ptr<const SubstitutionModelInterface> getSubstitutionModel() const override
130 {
131 return std::dynamic_pointer_cast<const SubstitutionModelInterface>(model_);
132 }
133
135 {
136 return dynamic_cast<const SubstitutionModelInterface&>(*model_);
137 }
138
140 };
141
144 {
145private:
147 size_t index_;
148
149public:
150 ConstNoPartitionBranchModelIterator(std::shared_ptr<const TransitionModelInterface> model, size_t nbSites) :
151 branchModelDescription_(model, nbSites), index_(0) {}
152
153public:
155 {
156 if (!hasNext())
157 throw Exception("AbstractHomogeneousTreeLikelihood::ConstHomogeneousBranchModelIterator::next(). No more branch in the set.");
158 index_++;
160 }
161
162 bool hasNext() const { return index_ == 0; }
163 };
164
167 {
168private:
169 std::shared_ptr<const TransitionModelInterface> model_;
170 std::vector<int> nodesId_;
171
172public:
173 ConstNoPartitionSiteModelDescription(std::shared_ptr<const TransitionModelInterface> model, const std::vector<int> nodesId) :
174 model_(model), nodesId_(nodesId) {}
175
177 model_(smd.model_),
178 nodesId_(smd.nodesId_)
179 {}
180
182 {
183 model_ = smd.model_;
184 nodesId_ = smd.nodesId_;
185 return *this;
186 }
187
188public:
189 std::shared_ptr<const TransitionModelInterface> getModel() const override { return model_; }
190 const TransitionModelInterface& model() const override { return *model_; }
191
192 std::shared_ptr<const SubstitutionModelInterface> getSubstitutionModel() const override
193 {
194 return std::dynamic_pointer_cast<const SubstitutionModelInterface>(model_);
195 }
196
198 {
199 return dynamic_cast<const SubstitutionModelInterface&>(*model_);
200 }
201
203 };
204
207protected:
208 std::unique_ptr<const AlignmentDataInterface> data_;
209 mutable std::shared_ptr< TreeTemplate<Node>> tree_;
213
214public:
217 data_(),
218 tree_(),
221 initialized_(false) {}
222
225 data_(),
226 tree_(),
230 {
231 if (lik.data_) data_ = std::unique_ptr<AlignmentDataInterface>(lik.data_->clone());
232 if (lik.tree_) tree_ = std::unique_ptr< TreeTemplate<Node>>(lik.tree_->clone());
233 }
234
236 {
237 AbstractParametrizable::operator=(lik);
238 if (lik.data_) data_ = std::unique_ptr<AlignmentDataInterface>(lik.data_->clone());
239 else data_ = 0;
240 if (lik.tree_) tree_ = std::unique_ptr< TreeTemplate<Node>>(lik.tree_->clone());
241 else tree_ = 0;
245 return *this;
246 }
247
254
255public:
261 bool hasLikelihoodData() const { return data_ != nullptr; }
262 const AlignmentDataInterface& data() const { return *data_; }
263 std::shared_ptr<const Alphabet> getAlphabet() const { return data_->getAlphabet(); }
268 size_t getNumberOfSites() const { return data_->getNumberOfSites(); }
269 const Tree& tree() const { return *tree_; }
275 bool isInitialized() const { return initialized_; }
276 void initialize() { initialized_ = true; }
278};
279} // end of namespace bpp.
280#endif // BPP_PHYL_LEGACY_LIKELIHOOD_ABSTRACTTREELIKELIHOOD_H
const SubstitutionModelInterface & substitutionModel() const override
ConstNoPartitionBranchModelDescription(std::shared_ptr< const TransitionModelInterface > model, size_t nbSites)
ConstNoPartitionBranchModelDescription(const ConstNoPartitionBranchModelDescription &bmd)
std::shared_ptr< const TransitionModelInterface > getModel() const override
ConstNoPartitionBranchModelDescription & operator=(const ConstNoPartitionBranchModelDescription &bmd)
std::shared_ptr< const SubstitutionModelInterface > getSubstitutionModel() const override
ConstNoPartitionBranchModelIterator(std::shared_ptr< const TransitionModelInterface > model, size_t nbSites)
std::shared_ptr< const TransitionModelInterface > model_
ConstNoPartitionSiteModelDescription(std::shared_ptr< const TransitionModelInterface > model, const std::vector< int > nodesId)
ConstNoPartitionSiteModelDescription & operator=(const ConstNoPartitionSiteModelDescription &smd)
const TransitionModelInterface & model() const override
const SubstitutionModelInterface & substitutionModel() const override
ConstNoPartitionSiteModelDescription(const ConstNoPartitionSiteModelDescription &smd)
std::shared_ptr< const TransitionModelInterface > getModel() const override
std::shared_ptr< const SubstitutionModelInterface > getSubstitutionModel() const override
SimpleBranchIterator(const std::vector< int > &nodesId)
SimpleSiteIterator(size_t nbSites, size_t offset=0)
Partial implementation of the TreeLikelihood interface.
const AlignmentDataInterface & data() const
Get the dataset for which the likelihood must be evaluated.
void initialize()
Init the likelihood object.
void enableDerivatives(bool yn)
Tell if derivatives must be computed.
const Tree & tree() const
Get the tree (topology and branch lengths).
std::shared_ptr< const Alphabet > getAlphabet() const
Get the alphabet associated to the dataset.
std::unique_ptr< const AlignmentDataInterface > data_
std::shared_ptr< TreeTemplate< Node > > tree_
AbstractTreeLikelihood(const AbstractTreeLikelihood &lik)
Vdouble getLikelihoodPerSite() const
Get the likelihood for each site.
VVdouble getLikelihoodPerSitePerState() const
Get the likelihood for each site and for each state.
Vdouble getLogLikelihoodPerSite() const
Get the logarithm of the likelihood for each site.
VVdouble getLogLikelihoodPerSitePerState() const
Get the logarithm of the likelihood for each site and for each state.
AbstractTreeLikelihood & operator=(const AbstractTreeLikelihood &lik)
size_t getNumberOfSites() const
Get the number of sites in the dataset.
virtual ~AbstractTreeLikelihood()
Abstract class destructor.
Interface for all substitution models.
Interface for all transition models.
An iterator over a set of branches, specified by their node ids.
A pair of SubstitutionModel / SiteIterator.
Iterates through all models used for all sites on a given branch.
A pair of SubstitutionModel / BranchIterator.
An iterator over a set of sites, specified by their position.
The TreeLikelihood interface.
Interface for phylogenetic tree objects.
Definition: Tree.h:115
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
std::vector< Vdouble > VVdouble