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 
15 namespace bpp
16 {
27 
34 
43 
44 // Lower Conditional Likelihood under nodes
47 
48 // Upper Likelihood at top of edges
51 
54 
56 
58 
59 using ForwardTransition =
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 {
73 private:
74  /* Considered Edge for each rate category */
75 
77  {
78 public:
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 
153 public:
155 
156 
157  void setModel(std::shared_ptr<ConfiguredModel> model)
158  {
159  if (model == model_)
160  return;
161  model_ = model;
162  shareParameters_(model_->getParameters());
163  makeLikelihoods();
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  {
204  return rootPatternLinks_ ? rootPatternLinks_->targetValue()(Eigen::Index(currentPosition)) : currentPosition;
205  }
206 
207  const PatternType& getRootArrayPositions() const { return rootPatternLinks_->targetValue(); }
208 
209  /*
210  * @brief Expands (ie reverse of shrunkage) a vector computed of
211  * shrunked data (ie from one value per distinct site to one
212  * value per site).
213  *
214  */
216  {
217  if (!rootPatternLinks_)
218  return vector;
219  else
221  }
222 
223  /*
224  * @brief Expands (ie reverse of shrunkage) a matrix computed of
225  * shrunked data (ie from one value per distinct site to one
226  * value per site). Columns are sites.
227  *
228  */
230  {
231  if (!rootPatternLinks_)
232  return matrix;
233  else
234  return CWisePattern<MatrixLik>::create(getContext_(), {matrix, rootPatternLinks_}, MatrixDimension (matrix->targetValue().rows(), Eigen::Index (numberOfSites_)));
235  }
236 
237  /*
238  * @brief: Get the weight of a position in the shrunked data (ie
239  * the number of sites corresponding to this site)
240  *
241  */
242  unsigned int getWeight(size_t pos) const
243  {
244  return (uint)(rootWeights_->targetValue()(Eigen::Index(pos)));
245  }
246 
247  std::shared_ptr<SiteWeights> getRootWeights()
248  {
249  return rootWeights_;
250  }
251 
252  /********************************************************
253  * @Likelihoods
254  *
255  *****************************************************/
256 
257  /*********************************/
258  /* @brief Methods for external usage (after lik computation) */
259 
266  RowLik getSiteLikelihoodsForAClass(size_t nCat, bool shrunk = false);
267 
275 
277  {
278  return (size_t)likelihoodMatrixDim_.cols;
279  }
280 
281  size_t getNumberOfSites() const
282  {
283  return numberOfSites_;
284  }
285 
286  std::shared_ptr<ConfiguredModel> getModel()
287  {
288  return model_;
289  }
290 
291 private:
292  void makeLikelihoods();
293 
294  std::shared_ptr<SiteLikelihoods> getSiteLikelihoods_(size_t nCat);
295 
296  void cleanAllLikelihoods();
297 };
298 } // namespace bpp
299 
300 #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
LikelihoodCalculationOnABranch(Context &context, LikelihoodCalculationSingleProcess &likcalsp, uint edgeId)
std::shared_ptr< const StateMapInterface > getStateMap() const
ValueRef< MatrixLik > expandMatrix(ValueRef< MatrixLik > matrix)
ValueRef< RowLik > expandVector(ValueRef< RowLik > vector)
const StateMapInterface & stateMap() const
std::shared_ptr< const Alphabet > getAlphabet() const
LikelihoodCalculationOnABranch * clone() const
void setModel(std::shared_ptr< ConfiguredModel > model)
size_t getRootArrayPosition(size_t currentPosition) const
std::shared_ptr< ConfiguredModel > model_
std::shared_ptr< SiteLikelihoods > getSiteLikelihoods_(size_t nCat)
AllRatesSiteLikelihoods getSiteLikelihoodsForAllClasses(bool shrunk=false)
Output array (Classes X Sites) of likelihoods for all sites & classes.
std::vector< RateCategoryEdge > vRateCatEdges_
std::shared_ptr< SiteWeights > rootWeights_
The frequency of each site.
std::shared_ptr< ConfiguredModel > getModel()
RowLik getSiteLikelihoodsForAClass(size_t nCat, bool shrunk=false)
Get site likelihoods for a rate category.
std::shared_ptr< SiteWeights > getRootWeights()
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.