bpp-phyl3 3.0.0
OnABranchPhyloLikelihood.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_LIKELIHOOD_PHYLOLIKELIHOODS_ONABRANCHPHYLOLIKELIHOOD_H
6#define BPP_PHYL_LIKELIHOOD_PHYLOLIKELIHOODS_ONABRANCHPHYLOLIKELIHOOD_H
7
8#include <Bpp/Exceptions.h>
12#include <unordered_map>
13
14#include "../DataFlow/DataFlowNumeric.h"
15#include "../DataFlow/LikelihoodCalculationOnABranch.h"
16#include "../DataFlow/Parameter.h"
18
19/* This file contains wrappers.
20 * They are used to bridge the gap between bpp::dataflow stuff and the rest of bpp.
21 *
22 */
23
24namespace bpp
25{
32{
33protected:
34 // Store nodes
35 mutable std::shared_ptr<LikelihoodCalculationOnABranch> likCal_;
36
40 mutable std::unordered_map<std::string, ValueRef<RowLik>> firstOrderDerivativeVectors_;
41
42 mutable std::unordered_map<std::pair<std::string, std::string>, ValueRef<RowLik>,
45
46public:
48 std::shared_ptr<LikelihoodCalculationSingleProcess> likCal,
49 uint edgeId,
50 const ParameterList& variableNodes) :
54 likCal_(std::make_shared<LikelihoodCalculationOnABranch>(context, *likCal, edgeId))
55 {
56 shareParameters_(variableNodes);
57 }
58
60 std::shared_ptr<LikelihoodCalculationSingleProcess> likCal,
61 uint edgeId) :
65 likCal_(std::make_shared<LikelihoodCalculationOnABranch>(context, *likCal, edgeId))
66 {}
67
68
70 std::shared_ptr<LikelihoodCalculationOnABranch> likCal,
71 const ParameterList& variableNodes) :
75 likCal_(likCal)
76 {
77 shareParameters_(variableNodes);
78 }
79
84 std::shared_ptr<LikelihoodCalculationOnABranch> likCal) :
88 likCal_(likCal)
89 {
90#ifdef DEBUG
91 std::cerr << "OnABranchPhyloLikelihood(context, OnABranchPhyloLikelihood)" << std::endl;
92#endif
93 shareParameters_(likCal_->getIndependentParameters());
94#ifdef DEBUG
95 std::cerr << "OnABranchPhyloLikelihood(context, OnABranchPhyloLikelihood)" << std::endl;
96#endif
97 }
98
99 // Legacy boilerplate
101 {
102 throw Exception("OnABranchPhyloLikelihood::clone should not be called.");
103 return new OnABranchPhyloLikelihood (*this);
104 }
105
106
107 size_t getNumberOfSites() const override
108 {
110 }
111
113 {
115 }
116
121 // size_t getNumberOfClasses() const { return substitutionProcess().getNumberOfClasses(); }
122
123
124 // std::shared_ptr<const Alphabet> getAlphabet() const override
125 // {
126 // return likelihoodCalculationOnABranch.getAlphabet();
127 // }
128
132 bool isInitialized() const override
133 {
135 }
136
137 void setModel(std::shared_ptr<ConfiguredModel> model)
138 {
139 likCal_->setModel(model);
140 }
141
148 {
149 return ParameterList();
150 }
151
153 {
154 return ParameterList();
155 }
156
163 {
164 return ParameterList();
165 }
166
173 {
174 return ParameterList();
175 }
176
183 {
184 return ParameterList();
185 }
186
194 {
195 return ParameterList();
196 }
197
203 {
204 return *likCal_;
205 }
206
207 std::shared_ptr<LikelihoodCalculation> getLikelihoodCalculation() const override
208 {
209 return likCal_;
210 }
211
213 {
214 return *likCal_;
215 }
216
217 std::shared_ptr<AlignedLikelihoodCalculation> getAlignedLikelihoodCalculation() const override
218 {
219 return likCal_;
220 }
221
223 {
224 return *likCal_;
225 }
226
227 std::shared_ptr<LikelihoodCalculationOnABranch> getLikelihoodCalculationOnABranch() const
228 {
229 return likCal_;
230 }
231
232 // Get nodes of derivatives directly
233
234 ValueRef<RowLik> getFirstOrderDerivativeVector (const std::string& variable) const
235 {
236 return firstOrderDerivativeVector(variable);
237 }
238
239 ValueRef<RowLik> firstOrderDerivativeVector (const std::string& variable) const
240 {
241 const auto it = firstOrderDerivativeVectors_.find (variable);
242 if (it != firstOrderDerivativeVectors_.end ())
243 {
244 return it->second;
245 }
246 else
247 {
248 auto vector = getLikelihoodCalculationOnABranch()->getSiteLikelihoods(true)->deriveAsValue (context_, accessVariableNode (variable));
249 firstOrderDerivativeVectors_.emplace (variable, vector);
250 return vector;
251 }
252 }
253
254 ValueRef<RowLik> getSecondOrderDerivativeVector (const std::string& variable) const
255 {
256 return getSecondOrderDerivativeVector (variable, variable);
257 }
258
260 const std::string& variable2) const
261 {
262 return secondOrderDerivativeVector (variable1, variable2);
263 }
264
265 ValueRef<RowLik> secondOrderDerivativeVector (const std::string& variable1,
266 const std::string& variable2) const
267 {
268 const auto key = std::make_pair (variable1, variable2);
269 const auto it = secondOrderDerivativeVectors_.find (key);
270 if (it != secondOrderDerivativeVectors_.end ())
271 {
272 return it->second;
273 }
274 else
275 {
276 // Reuse firstOrderDerivative() to generate the first derivative with caching
277 auto vector =
278 firstOrderDerivativeVector (variable1)->deriveAsValue (context_, accessVariableNode (variable2));
279 secondOrderDerivativeVectors_.emplace (key, vector);
280 return vector;
281 }
282 }
283
292 // VVdouble getPosteriorProbabilitiesPerSitePerClass() const;
293
294 // Vdouble getPosteriorProbabilitiesForSitePerClass(size_t pos) const;
295
296 /*
297 *@brief return the likelihood of rate classes on each site.
298 *
299 *@return 2D-vector sites x classes
300 */
301
303
304 std::vector<size_t> getClassWithMaxPostProbPerSite() const;
305
306 // Vdouble getPosteriorRatePerSite() const;
307
308 // Vdouble getPosteriorStateFrequencies(uint nodeId);
309};
310} // namespace bpp
311
312#endif // BPP_PHYL_LIKELIHOOD_PHYLOLIKELIHOODS_ONABRANCHPHYLOLIKELIHOOD_H
virtual void shareParameters_(const ParameterList &parameters)
static Node_DF & accessVariableNode(const Parameter &param)
const Context & context() const override
Context for dataflow node construction.
Definition: DataFlow.h:527
virtual bool isInitialized() const
Wraps a dataflow graph as a function: resultNode = f(variableNodes).
bool isInitialized() const override
Get the number of model classes.
std::shared_ptr< LikelihoodCalculation > getLikelihoodCalculation() const override
ValueRef< RowLik > getSecondOrderDerivativeVector(const std::string &variable) const
ValueRef< RowLik > secondOrderDerivativeVector(const std::string &variable1, const std::string &variable2) const
OnABranchPhyloLikelihood * clone() const override
ValueRef< RowLik > getFirstOrderDerivativeVector(const std::string &variable) const
std::unordered_map< std::pair< std::string, std::string >, ValueRef< RowLik >, StringPairHash > secondOrderDerivativeVectors_
AlignedLikelihoodCalculation & alignedLikelihoodCalculation() const override
OnABranchPhyloLikelihood(Context &context, std::shared_ptr< LikelihoodCalculationSingleProcess > likCal, uint edgeId, const ParameterList &variableNodes)
std::unordered_map< std::string, ValueRef< RowLik > > firstOrderDerivativeVectors_
For Dataflow computing.
VVDataLik getLikelihoodPerSitePerClass() const
Get the posterior probabilities of each class, for each site.
std::vector< size_t > getClassWithMaxPostProbPerSite() const
ParameterList getRootFrequenciesParameters() const override
Get the independent parameters associated to the root frequencies(s).
ParameterList getBranchLengthParameters() const override
Get the independent branch lengths parameters.
OnABranchPhyloLikelihood(Context &context, std::shared_ptr< LikelihoodCalculationOnABranch > likCal, const ParameterList &variableNodes)
OnABranchPhyloLikelihood(Context &context, std::shared_ptr< LikelihoodCalculationSingleProcess > likCal, uint edgeId)
void setModel(std::shared_ptr< ConfiguredModel > model)
LikelihoodCalculationOnABranch & likelihoodCalculationOnABranch() const
ParameterList getSubstitutionModelParameters() const override
Get the independent parameters associated to substitution model(s).
ValueRef< RowLik > getSecondOrderDerivativeVector(const std::string &variable1, const std::string &variable2) const
std::shared_ptr< LikelihoodCalculationOnABranch > getLikelihoodCalculationOnABranch() const
std::shared_ptr< LikelihoodCalculationOnABranch > likCal_
LikelihoodCalculation & likelihoodCalculation() const override
ValueRef< RowLik > firstOrderDerivativeVector(const std::string &variable) const
ParameterList getRateDistributionParameters() const override
Get the independent parameters associated to the rate distribution(s).
ParameterList getNonDerivableParameters() const override
std::shared_ptr< AlignedLikelihoodCalculation > getAlignedLikelihoodCalculation() const override
size_t getNumberOfSites() const override
Get the number of sites in the dataset.
OnABranchPhyloLikelihood(Context &context, std::shared_ptr< LikelihoodCalculationOnABranch > likCal)
: the parameters the independent parameters of the LikelihoodCalculation
ParameterList getDerivableParameters() const override
Defines the basic types of data flow nodes.
std::shared_ptr< Value< T > > ValueRef
Shared pointer alias for Value<T>.
Definition: DataFlow.h:84
std::vector< VDataLik > VVDataLik
Definition: Definitions.h:24