bpp-phyl3 3.0.0
LikelihoodCalculationOnABranch.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_ON_A_BRANCH_H
6#define BPP_PHYL_LIKELIHOOD_DATAFLOW_LIKELIHOODCALCULATION_ON_A_BRANCH_H
7
10
14
15namespace bpp
16{
27
34
43
44// Lower Conditional Likelihood under nodes
47
48// Upper Likelihood at top of edges
51
54
56
58
61
62/*
63 * @brief DAG of the conditional likelihoods (product of above and
64 * below likelihoods), with same topology as forward & backward
65 * likelihood DAGs.
66 *
67 */
68
69
72{
73private:
74 /* Considered Edge for each rate category */
75
77 {
78public:
79 /* Vectors are necessary to account for all branches in the DAG matching a tree branch.
80 */
81
82 /* vector of Forward Conditional likelihoods at the bottom of the edge */
83 std::vector<ForwardLikelihoodBelowRef> vBotLik_;
84
85 /* Backward Conditional likelihood at the bottom of the edge */
86
87 std::vector<BackwardLikelihoodAboveRef> vTopLik_;
88
89 /* Conditional likelihoods of the edge */
90
92
93 /************************************/
94 /* Dependencies */
95
96 std::shared_ptr<ConfiguredParameter> brlen_;
97 };
98
99
100 /*
101 * @brief Number of sites
102 *
103 */
104
106
107 /*
108 * @brief Dimension of the shrunked data
109 */
110
112
113 /*
114 *@brief DF Model used on this branch
115 *
116 */
117
118 std::shared_ptr<ConfiguredModel> model_;
119
120
121 /*****************************
122 ****** Patterns
123 *
124 * @brief Links between sites and patterns.
125 *
126 * The size of this vector is equal to the number of sites in the container,
127 * each element corresponds to a site in the container and points to the
128 * corresponding column in the likelihood array of the root node.
129 * If the container contains no repeated site, there will be a strict
130 * equivalence between each site and the likelihood array of the root node.
131 * However, if this is not the case, some pointers may point toward the same
132 * element in the likelihood array.
133 */
134
136
141 std::shared_ptr<SiteWeights> rootWeights_;
142
143 /*
144 * @brief Node for the probabilities of the rate classes
145 *
146 */
147
149
150 /* Likelihood Edges with for all rate categories */
151 std::vector<RateCategoryEdge> vRateCatEdges_;
152
153public:
155
156
157 void setModel(std::shared_ptr<ConfiguredModel> model)
158 {
159 if (model == model_)
160 return;
161 model_ = model;
162 shareParameters_(model_->getParameters());
164 }
165
167
169 {
170 throw bpp::Exception("LikelihoodCalculationOnABranch clone should not happen.");
171 }
172
174 {
175 return model_->targetValue()->stateMap();
176 }
177
178 std::shared_ptr<const StateMapInterface> getStateMap() const
179 {
180 return model_->targetValue()->getStateMap();
181 }
182
183 std::shared_ptr<const Alphabet> getAlphabet() const
184 {
185 return stateMap().getAlphabet();
186 }
187
188
189 /************************************************
190 *** Patterns
191 ****************************/
192
193 /*
194 * @brief the relations between real position and shrunked data
195 * positions.
196 *
197 * @param currentPosition : position in real data
198 *
199 * @return matching position in shrunked data
200 *
201 */
202 size_t getRootArrayPosition(size_t currentPosition) const
203 {
205 {
206 auto pos = Eigen::Index(currentPosition);
207 if (pos>=rootPatternLinks_->targetValue().rows())
208 throw BadSizeException("Forbidden access in getRootArrayPosition.",pos,rootPatternLinks_->targetValue().rows());
209 else
210 return rootPatternLinks_->targetValue()(pos);
211 }
212 else
213 return currentPosition;
214 }
215
216 const PatternType& getRootArrayPositions() const { return rootPatternLinks_->targetValue(); }
217
218 /*
219 * @brief Expands (ie reverse of shrunkage) a vector computed of
220 * shrunked data (ie from one value per distinct site to one
221 * value per site).
222 *
223 */
225 {
227 return vector;
228 else
230 }
231
232 /*
233 * @brief Expands (ie reverse of shrunkage) a matrix computed of
234 * shrunked data (ie from one value per distinct site to one
235 * value per site). Columns are sites.
236 *
237 */
239 {
241 return matrix;
242 else
243 return CWisePattern<MatrixLik>::create(getContext_(), {matrix, rootPatternLinks_}, MatrixDimension (matrix->targetValue().rows(), Eigen::Index (numberOfSites_)));
244 }
245
246 /*
247 * @brief: Get the weight of a position in the shrunked data (ie
248 * the number of sites corresponding to this site)
249 *
250 */
251 unsigned int getWeight(size_t pos) const
252 {
253 return (uint)(rootWeights_->targetValue()(Eigen::Index(pos)));
254 }
255
256 std::shared_ptr<SiteWeights> getRootWeights()
257 {
258 return rootWeights_;
259 }
260
261 /********************************************************
262 * @Likelihoods
263 *
264 *****************************************************/
265
266 /*********************************/
267 /* @brief Methods for external usage (after lik computation) */
268
275 RowLik getSiteLikelihoodsForAClass(size_t nCat, bool shrunk = false);
276
284
286 {
287 return (size_t)likelihoodMatrixDim_.cols;
288 }
289
290 size_t getNumberOfSites() const
291 {
292 return numberOfSites_;
293 }
294
295 std::shared_ptr<ConfiguredModel> getModel()
296 {
297 return model_;
298 }
299
300private:
301 void makeLikelihoods();
302
303 std::shared_ptr<SiteLikelihoods> getSiteLikelihoods_(size_t nCat);
304
305 void cleanAllLikelihoods();
306};
307} // namespace bpp
308
309#endif // BPP_PHYL_LIKELIHOOD_DATAFLOW_LIKELIHOODCALCULATION_ON_A_BRANCH_H
void shareParameters_(const ParameterList &parameters)
static ValueRef< R > create(Context &c, NodeRefVec &&deps, const Dimension< R > &dim)
Build a new CWisePattern node.
Context for dataflow node construction.
Definition: DataFlow.h:527
ValueRef< MatrixLik > expandMatrix(ValueRef< MatrixLik > matrix)
LikelihoodCalculationOnABranch(Context &context, LikelihoodCalculationSingleProcess &likcalsp, uint edgeId)
LikelihoodCalculationOnABranch * clone() const
std::shared_ptr< SiteLikelihoods > getSiteLikelihoods_(size_t nCat)
std::shared_ptr< const StateMapInterface > getStateMap() const
void setModel(std::shared_ptr< ConfiguredModel > model)
size_t getRootArrayPosition(size_t currentPosition) const
std::shared_ptr< ConfiguredModel > model_
std::shared_ptr< const Alphabet > getAlphabet() const
AllRatesSiteLikelihoods getSiteLikelihoodsForAllClasses(bool shrunk=false)
Output array (Classes X Sites) of likelihoods for all sites & classes.
std::shared_ptr< ConfiguredModel > getModel()
std::vector< RateCategoryEdge > vRateCatEdges_
std::shared_ptr< SiteWeights > getRootWeights()
std::shared_ptr< SiteWeights > rootWeights_
The frequency of each site.
ValueRef< RowLik > expandVector(ValueRef< RowLik > vector)
const StateMapInterface & stateMap() const
RowLik getSiteLikelihoodsForAClass(size_t nCat, bool shrunk=false)
Get site likelihoods for a rate category.
r = constant_value.
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:25
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
Defines the basic types of data flow nodes.
std::shared_ptr< Value< T > > ValueRef
Shared pointer alias for Value<T>.
Definition: DataFlow.h:84
ExtendedFloatMatrixXd MatrixLik
Definition: Definitions.h:13
ValueRef< MatrixLik > ForwardLikelihoodBelowRef
Eigen::Matrix< size_t, -1, 1 > PatternType
ValueRef< RowLik > SiteLikelihoodsRef
ValueRef< MatrixLik > BackwardLikelihoodAboveRef
Basic matrix dimension type.