bpp-phyl3
3.0.0
LikelihoodCalculation.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_DATAFLOW_LIKELIHOODCALCULATION_H
6
#define BPP_PHYL_LIKELIHOOD_DATAFLOW_LIKELIHOODCALCULATION_H
7
8
#include <
Bpp/Numeric/AbstractParameterAliasable.h
>
9
#include <Eigen/Core>
10
11
#include "
Bpp/Phyl/Likelihood/DataFlow/DataFlowNumeric.h
"
12
#include "
Definitions.h
"
13
14
namespace
bpp
15
{
20
class
LikelihoodCalculation
:
21
public
AbstractParameterAliasable
22
{
23
private
:
24
Context
&
context_
;
25
26
protected
:
27
/******************************************/
30
mutable
ValueRef<DataLik>
likelihood_
;
31
32
public
:
33
LikelihoodCalculation
(
Context
& context) :
34
AbstractParameterAliasable
(
""
),
35
context_
(context)
36
{}
37
38
LikelihoodCalculation
(
Context
& context,
39
ParameterList
& paramList) :
40
AbstractParameterAliasable
(
""
),
41
context_
(context)
42
{
43
shareParameters_
(paramList);
44
}
45
46
47
/*
48
* @brief Copy the likelihood calculation IN THE SAME CONTEXT.
49
*
50
*/
51
52
LikelihoodCalculation
(
const
LikelihoodCalculation
& lik) :
53
AbstractParameterAliasable
(lik),
54
context_
(lik.
context_
),
55
likelihood_
()
56
{}
57
58
LikelihoodCalculation
*
clone
()
const
59
{
60
throw
bpp::Exception
(
"LikelihoodCalculation clone should not happen."
);
61
}
62
63
ValueRef<DataLik>
getLikelihoodNode
()
64
{
65
if
(!
likelihood_
)
66
makeLikelihoods
();
67
68
return
likelihood_
;
69
}
70
71
virtual
bool
isInitialized
()
const
72
{
73
return
likelihood_
!= 0;
74
}
75
76
const
Context
&
getContext
()
const
77
{
78
return
context_
;
79
}
80
81
/*
82
* @brief Return likelihood_ without any computation, used to build likelihood_.
83
*
84
*/
85
void
setLikelihoodNode
(
ValueRef<DataLik>
ll)
86
{
87
likelihood_
= ll;
88
}
89
90
virtual
void
cleanAllLikelihoods
()
91
{
92
if
(
likelihood_
)
93
getContext_
().
erase
(
likelihood_
);
94
likelihood_
.reset();
95
}
96
97
/*
98
* @brief fix Factor such that valRef value becomes normal.
99
*
100
*/
101
102
protected
:
103
/* @brief Build the likelihood DF */
104
105
virtual
void
makeLikelihoods
()
106
{
107
throw
Exception
(
"LikelihoodCalculation:: makeLikelihoods should not be called. Probably likelihood_ is null."
);
108
}
109
110
Context
&
getContext_
()
111
{
112
return
context_
;
113
}
114
115
ValueRef<DataLik>
getLikelihoodNode_
()
116
{
117
return
likelihood_
;
118
}
119
};
120
121
using
SiteLikelihoods
=
Value<RowLik>
;
122
using
SiteLikelihoodsRef
=
ValueRef<RowLik>
;
123
124
class
AlignedLikelihoodCalculation
:
125
public
LikelihoodCalculation
126
{
127
public
:
128
AlignedLikelihoodCalculation
(
Context
& context) :
129
LikelihoodCalculation
(context) {}
130
131
AlignedLikelihoodCalculation
(
const
AlignedLikelihoodCalculation
& lik) :
132
LikelihoodCalculation
(lik)
133
{}
134
135
136
AlignedLikelihoodCalculation
*
clone
()
const
137
{
138
throw
bpp::Exception
(
"AlignedLikelihoodCalculation clone should not happen."
);
139
}
140
141
protected
:
142
/******************************************/
145
/* For Data used for output (ie non shrunk if ever)
146
*
147
*/
148
149
mutable
SiteLikelihoodsRef
siteLikelihoods_
;
150
151
/* For Data used for computation (ie shrunked data if ever)
152
*
153
*/
154
155
mutable
SiteLikelihoodsRef
patternedSiteLikelihoods_
;
156
157
public
:
158
/*
159
* @brief Return the ref to the Sitelikelihoods_ vector on data
160
* (shrunked or not).
161
*
162
* @brief shrunk: bool true if vector on shrunked data (default: false)
163
*
164
*/
165
SiteLikelihoodsRef
getSiteLikelihoods
(
bool
shrunk =
false
)
166
{
167
if
(!(
siteLikelihoods_
||
patternedSiteLikelihoods_
))
168
makeLikelihoods
();
169
170
if
(shrunk &&
patternedSiteLikelihoods_
)
171
return
patternedSiteLikelihoods_
;
172
else
173
return
siteLikelihoods_
;
174
}
175
176
/*
177
* @brief Return the ref to the Sitelikelihoods_ vector on data
178
* (shrunked or not).
179
*
180
* @brief ll: Site Likelihoods
181
* @brief shrunk: given Likelihoods are on shrunked data (default false)
182
*
183
*/
184
void
setSiteLikelihoods
(
SiteLikelihoodsRef
ll,
185
bool
shrunk =
false
)
186
{
187
if
(shrunk)
188
patternedSiteLikelihoods_
= ll;
189
else
190
siteLikelihoods_
= ll;
191
}
192
193
/*
194
* @brief Return Likelihood on a site
195
* @param pos : site position
196
* @param shrunk : on shrunked data (default false)
197
*/
198
DataLik
getLikelihoodForASite
(
size_t
pos,
bool
shrunk =
false
)
199
{
200
if
(!(
siteLikelihoods_
||
patternedSiteLikelihoods_
))
201
makeLikelihoods
();
202
203
if
(shrunk &&
patternedSiteLikelihoods_
)
204
return
patternedSiteLikelihoods_
->targetValue()(Eigen::Index(pos));
205
else
206
return
siteLikelihoods_
->targetValue()(Eigen::Index(pos));
207
}
208
209
DataLik
getLogLikelihoodForASite
(
size_t
pos,
bool
shrunk =
false
)
210
{
211
using namespace
std;
212
return
log
(
getLikelihoodForASite
(pos, shrunk));
213
}
214
221
VDataLik
getLikelihoodPerSite
()
222
{
223
auto
vLik =
getSiteLikelihoods
(
false
)->targetValue();
224
VDataLik
v;
225
copyEigenToBpp
(vLik, v);
226
return
v;
227
}
228
229
/*
230
* @brief Clean all the existing likelihoods (ie remove them from
231
* Context), and all unique dependencies.
232
*
233
*
234
*/
235
void
cleanAllLikelihoods
()
236
{
237
LikelihoodCalculation::cleanAllLikelihoods
();
238
239
if
(
siteLikelihoods_
)
240
{
241
getContext_
().
erase
(
siteLikelihoods_
);
242
siteLikelihoods_
.reset();
243
}
244
if
(
patternedSiteLikelihoods_
)
245
{
246
getContext_
().
erase
(
patternedSiteLikelihoods_
);
247
patternedSiteLikelihoods_
.reset();
248
}
249
}
250
251
protected
:
252
virtual
void
makeLikelihoods
()
253
{
254
throw
Exception
(
"AlignedLikelihoodCalculation:: makeLikelihoods should not be called. Probably both siteLikelihoods_ and patternedSiteLikelihoods_ are null."
);
255
}
256
};
257
}
// namespace bpp
258
#endif
// BPP_PHYL_LIKELIHOOD_DATAFLOW_LIKELIHOODCALCULATION_H
AbstractParameterAliasable.h
DataFlowNumeric.h
Definitions.h
bpp::AbstractParameterAliasable
bpp::AbstractParameterAliasable::shareParameters_
void shareParameters_(const ParameterList ¶meters)
bpp::AlignedLikelihoodCalculation
Definition:
LikelihoodCalculation.h:126
bpp::AlignedLikelihoodCalculation::siteLikelihoods_
SiteLikelihoodsRef siteLikelihoods_
Definition:
LikelihoodCalculation.h:149
bpp::AlignedLikelihoodCalculation::getSiteLikelihoods
SiteLikelihoodsRef getSiteLikelihoods(bool shrunk=false)
Definition:
LikelihoodCalculation.h:165
bpp::AlignedLikelihoodCalculation::AlignedLikelihoodCalculation
AlignedLikelihoodCalculation(Context &context)
Definition:
LikelihoodCalculation.h:128
bpp::AlignedLikelihoodCalculation::makeLikelihoods
virtual void makeLikelihoods()
Definition:
LikelihoodCalculation.h:252
bpp::AlignedLikelihoodCalculation::getLogLikelihoodForASite
DataLik getLogLikelihoodForASite(size_t pos, bool shrunk=false)
Definition:
LikelihoodCalculation.h:209
bpp::AlignedLikelihoodCalculation::cleanAllLikelihoods
void cleanAllLikelihoods()
Definition:
LikelihoodCalculation.h:235
bpp::AlignedLikelihoodCalculation::patternedSiteLikelihoods_
SiteLikelihoodsRef patternedSiteLikelihoods_
Definition:
LikelihoodCalculation.h:155
bpp::AlignedLikelihoodCalculation::getLikelihoodPerSite
VDataLik getLikelihoodPerSite()
Get the likelihood for each site.
Definition:
LikelihoodCalculation.h:221
bpp::AlignedLikelihoodCalculation::setSiteLikelihoods
void setSiteLikelihoods(SiteLikelihoodsRef ll, bool shrunk=false)
Definition:
LikelihoodCalculation.h:184
bpp::AlignedLikelihoodCalculation::AlignedLikelihoodCalculation
AlignedLikelihoodCalculation(const AlignedLikelihoodCalculation &lik)
Definition:
LikelihoodCalculation.h:131
bpp::AlignedLikelihoodCalculation::clone
AlignedLikelihoodCalculation * clone() const
Definition:
LikelihoodCalculation.h:136
bpp::AlignedLikelihoodCalculation::getLikelihoodForASite
DataLik getLikelihoodForASite(size_t pos, bool shrunk=false)
Definition:
LikelihoodCalculation.h:198
bpp::Context
Context for dataflow node construction.
Definition:
DataFlow.h:527
bpp::Context::erase
bool erase(const NodeRef &r)
Remove an element from the map, only if it has no dependent nodes, and likewise down the graph.
Definition:
DataFlow.cpp:363
bpp::Exception
bpp::ExtendedFloat
Definition:
ExtendedFloat.h:38
bpp::LikelihoodCalculation
Definition:
LikelihoodCalculation.h:22
bpp::LikelihoodCalculation::context_
Context & context_
Definition:
LikelihoodCalculation.h:24
bpp::LikelihoodCalculation::LikelihoodCalculation
LikelihoodCalculation(Context &context)
Definition:
LikelihoodCalculation.h:33
bpp::LikelihoodCalculation::isInitialized
virtual bool isInitialized() const
Definition:
LikelihoodCalculation.h:71
bpp::LikelihoodCalculation::getContext
const Context & getContext() const
Definition:
LikelihoodCalculation.h:76
bpp::LikelihoodCalculation::likelihood_
ValueRef< DataLik > likelihood_
Definition:
LikelihoodCalculation.h:30
bpp::LikelihoodCalculation::getContext_
Context & getContext_()
Definition:
LikelihoodCalculation.h:110
bpp::LikelihoodCalculation::cleanAllLikelihoods
virtual void cleanAllLikelihoods()
Definition:
LikelihoodCalculation.h:90
bpp::LikelihoodCalculation::getLikelihoodNode
ValueRef< DataLik > getLikelihoodNode()
Definition:
LikelihoodCalculation.h:63
bpp::LikelihoodCalculation::getLikelihoodNode_
ValueRef< DataLik > getLikelihoodNode_()
Definition:
LikelihoodCalculation.h:115
bpp::LikelihoodCalculation::LikelihoodCalculation
LikelihoodCalculation(const LikelihoodCalculation &lik)
Definition:
LikelihoodCalculation.h:52
bpp::LikelihoodCalculation::LikelihoodCalculation
LikelihoodCalculation(Context &context, ParameterList ¶mList)
Definition:
LikelihoodCalculation.h:38
bpp::LikelihoodCalculation::makeLikelihoods
virtual void makeLikelihoods()
Definition:
LikelihoodCalculation.h:105
bpp::LikelihoodCalculation::clone
LikelihoodCalculation * clone() const
Definition:
LikelihoodCalculation.h:58
bpp::LikelihoodCalculation::setLikelihoodNode
void setLikelihoodNode(ValueRef< DataLik > ll)
Definition:
LikelihoodCalculation.h:85
bpp::ParameterList
bpp::Value< RowLik >
bpp
Defines the basic types of data flow nodes.
bpp::log
double log(const ExtendedFloat &ef)
Definition:
ExtendedFloat.h:443
bpp::VDataLik
std::vector< DataLik > VDataLik
Definition:
Definitions.h:23
bpp::ValueRef
std::shared_ptr< Value< T > > ValueRef
Shared pointer alias for Value<T>.
Definition:
DataFlow.h:84
bpp::copyEigenToBpp
template void copyEigenToBpp(const ExtendedFloatMatrixXd &eigenMatrix, std::vector< std::vector< double >> &bppMatrix)
bpp::SiteLikelihoodsRef
ValueRef< RowLik > SiteLikelihoodsRef
Definition:
LikelihoodCalculation.h:122
Bpp
Phyl
Likelihood
DataFlow
LikelihoodCalculation.h
Generated on Fri Jul 19 2024 11:49:21 for bpp-phyl3 by
1.9.1