bpp-phyl3  3.0.0
RHomogeneousMixedTreeLikelihood.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
6 
7 // From the STL:
8 #include <iostream>
9 
10 #include <cmath>
11 #include "../../PatternTools.h"
12 
15 
16 using namespace bpp;
17 using namespace std;
18 
20  const Tree& tree,
21  std::shared_ptr<TransitionModelInterface> model,
22  std::shared_ptr<DiscreteDistributionInterface> rDist,
23  bool checkRooted,
24  bool verbose,
25  bool usePatterns) :
26  RHomogeneousTreeLikelihood(tree, model, rDist, checkRooted, verbose, usePatterns),
27  treeLikelihoodsContainer_(),
28  probas_()
29 {
30  shared_ptr<MixedTransitionModelInterface> mixedmodel;
31  if ((mixedmodel = dynamic_pointer_cast<MixedTransitionModelInterface>(model_)) == nullptr)
32  throw Exception("Bad model: RHomogeneousMixedTreeLikelihood needs a MixedTransitionModel.");
33  size_t s = mixedmodel->getNumberOfModels();
34  for (size_t i = 0; i < s; ++i)
35  {
36  treeLikelihoodsContainer_.push_back(
37  make_shared<RHomogeneousTreeLikelihood>(
38  tree,
39  unique_ptr<TransitionModelInterface>(mixedmodel->nModel(i).clone()),
40  rDist,
41  checkRooted,
42  false,
43  usePatterns));
44  probas_.push_back(mixedmodel->getNProbability(i));
45  }
46 }
47 
49  const Tree& tree,
50  const AlignmentDataInterface& data,
51  std::shared_ptr<TransitionModelInterface> model,
52  std::shared_ptr<DiscreteDistributionInterface> rDist,
53  bool checkRooted,
54  bool verbose,
55  bool usePatterns) :
56  RHomogeneousTreeLikelihood(tree, model, rDist, checkRooted, verbose, usePatterns),
57  treeLikelihoodsContainer_(),
58  probas_()
59 {
60  shared_ptr<MixedTransitionModelInterface> mixedmodel;
61 
62  if ((mixedmodel = dynamic_pointer_cast<MixedTransitionModelInterface>(model_)) == nullptr)
63  throw Exception("Bad model: RHomogeneousMixedTreeLikelihood needs a MixedTransitionModel.");
64 
65  size_t s = mixedmodel->getNumberOfModels();
66  for (size_t i = 0; i < s; i++)
67  {
68  treeLikelihoodsContainer_.push_back(
69  make_shared<RHomogeneousTreeLikelihood>(
70  tree,
71  unique_ptr<TransitionModelInterface>(mixedmodel->nModel(i).clone()),
72  rDist,
73  checkRooted,
74  false,
75  usePatterns));
76  probas_.push_back(mixedmodel->getNProbability(i));
77  }
78  setData(data);
79 }
80 
82 {
84 
86  probas_.clear();
87 
88  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); i++)
89  {
90  treeLikelihoodsContainer_.push_back(shared_ptr<RHomogeneousTreeLikelihood>(lik.treeLikelihoodsContainer_[i]->clone()));
91  probas_.push_back(lik.probas_[i]);
92  }
93 
94  return *this;
95 }
96 
97 
100  treeLikelihoodsContainer_(lik.treeLikelihoodsContainer_.size()),
101  probas_(lik.probas_.size())
102 {
103  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); ++i)
104  {
105  treeLikelihoodsContainer_[i] = shared_ptr<RHomogeneousTreeLikelihood>(lik.treeLikelihoodsContainer_[i]->clone());
106  probas_.push_back(lik.probas_[i]);
107  }
108 }
109 
111 
112 
114 {
115  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); ++i)
116  {
117  treeLikelihoodsContainer_[i]->initialize();
118  }
119 
121 }
122 
124 {
126 
127  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); ++i)
128  {
129  treeLikelihoodsContainer_[i]->setData(sites);
130  }
131 }
132 
133 
135 {
136  // checks in the model will change
137  bool modelC = model_->getParameters().testParametersValues(params);
138 
139  applyParameters();
140  auto mixedmodel = dynamic_pointer_cast<MixedTransitionModelInterface>(model_);
141  size_t s = mixedmodel->getNumberOfModels();
142 
143  for (size_t i = 0; i < s; ++i)
144  {
145  ParameterList pl;
146  pl.addParameters(mixedmodel->nModel(i).getParameters());
148 
149  if (modelC)
150  {
151  treeLikelihoodsContainer_[i]->setParameters(pl);
152  }
153  else
154  treeLikelihoodsContainer_[i]->matchParametersValues(pl);
155  }
156 
157  probas_ = mixedmodel->getProbabilities();
159 }
160 
162 {
163  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); i++)
164  {
165  treeLikelihoodsContainer_[i]->computeTreeLikelihood();
166  }
167 }
168 
169 /******************************************************************************
170 * Likelihoods *
171 ******************************************************************************/
173 {
174  double res = 0;
175 
176  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); i++)
177  {
178  res += treeLikelihoodsContainer_[i]->getLikelihoodForASiteForARateClass(site, rateClass) * probas_[i];
179  }
180 
181  return res;
182 }
183 
185 {
186  double x = getLikelihoodForASiteForARateClass(site, rateClass);
187  if (x < 0)
188  x = 0;
189  return log(x);
190 }
191 
192 double RHomogeneousMixedTreeLikelihood::getLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const
193 {
194  double res = 0;
195 
196  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); i++)
197  {
198  res += treeLikelihoodsContainer_[i]->getLikelihoodForASiteForARateClassForAState(site, rateClass, state) * probas_[i];
199  }
200 
201  return res;
202 }
203 
204 double RHomogeneousMixedTreeLikelihood::getLogLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const
205 {
206  double x = getLikelihoodForASiteForARateClassForAState(site, rateClass, state);
207  if (x < 0)
208  x = 0;
209  return log(x);
210 }
211 
212 
213 /******************************************************************************
214 * First Order Derivatives *
215 ******************************************************************************/
217 {
218  double res = 0;
219 
220  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); i++)
221  {
222  res += treeLikelihoodsContainer_[i]->getDLikelihoodForASiteForARateClass(site, rateClass) * probas_[i];
223  }
224 
225  return res;
226 }
227 
229 {
230  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); i++)
231  {
232  treeLikelihoodsContainer_[i]->computeTreeDLikelihood(variable);
233  }
234 }
235 
236 /******************************************************************************
237 * Second Order Derivatives *
238 ******************************************************************************/
240 {
241  double res = 0;
242 
243  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); i++)
244  {
245  res += treeLikelihoodsContainer_[i]->getD2LikelihoodForASiteForARateClass(site, rateClass) * probas_[i];
246  }
247 
248  return res;
249 }
250 
251 
253 {
254  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); i++)
255  {
256  treeLikelihoodsContainer_[i]->computeTreeD2Likelihood(variable);
257  }
258 }
259 
261 {
262  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); i++)
263  {
264  treeLikelihoodsContainer_[i]->computeSubtreeLikelihood(node);
265  }
266 }
267 
269 {
270  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); i++)
271  {
272  treeLikelihoodsContainer_[i]->computeDownSubtreeDLikelihood(node);
273  }
274 }
275 
277 {
278  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); i++)
279  {
280  treeLikelihoodsContainer_[i]->computeDownSubtreeD2Likelihood(node);
281  }
282 }
283 
284 
286 {
287  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); i++)
288  {
289  treeLikelihoodsContainer_[i]->computeAllTransitionProbabilities();
290  }
291 }
292 
293 
295 {
296  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); i++)
297  {
298  treeLikelihoodsContainer_[i]->computeTransitionProbabilitiesForNode(node);
299  }
300 }
301 
302 
304 {
305  for (size_t i = 0; i < treeLikelihoodsContainer_.size(); i++)
306  {
307  treeLikelihoodsContainer_[i]->displayLikelihood(node);
308  }
309 }
virtual void applyParameters()
All parameters are stored in a parameter list. This function apply these parameters to the substituti...
std::shared_ptr< TransitionModelInterface > model_
const ParameterList & getParameters() const override
const AlignmentDataInterface & data() const
Get the dataset for which the likelihood must be evaluated.
const Tree & tree() const
Get the tree (topology and branch lengths).
The phylogenetic node class.
Definition: Node.h:59
virtual void addParameters(const ParameterList &params)
virtual void includeParameters(const ParameterList &params)
virtual double getDLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
double getLogLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const
Get the logarithm of the likelihood for a site knowing its rate class and its ancestral state.
void computeTransitionProbabilitiesForNode(const Node *node)
This method is used by fireParameterChanged method.
virtual void computeTreeD2Likelihood(const std::string &variable)
virtual void computeSubtreeLikelihood(const Node *node)
Compute the likelihood for a subtree defined by the Tree::Node node.
double getLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const
Get the likelihood for a site knowing its rate class and its ancestral state.
void computeAllTransitionProbabilities()
This method is used by fireParameterChanged method.
virtual double getD2LikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
RHomogeneousMixedTreeLikelihood & operator=(const RHomogeneousMixedTreeLikelihood &lik)
void fireParameterChanged(const ParameterList &params)
std::vector< std::shared_ptr< RHomogeneousTreeLikelihood > > treeLikelihoodsContainer_
void setData(const AlignmentDataInterface &sites)
Set the dataset for which the likelihood must be evaluated.
double getLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
Get the likelihood for a site knowing its rate class.
virtual void displayLikelihood(const Node *node)
This method is mainly for debugging purpose.
virtual void computeTreeDLikelihood(const std::string &variable)
double getLogLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
Get the logarithm of the likelihood for a site knowing its rate class.
This class implement the 'traditional' way of computing likelihood for a tree.
RHomogeneousTreeLikelihood & operator=(const RHomogeneousTreeLikelihood &lik)
void setData(const AlignmentDataInterface &sites)
Set the dataset for which the likelihood must be evaluated.
double getLogLikelihood() const
Get the logarithm of the likelihood for the whole dataset.
Interface for phylogenetic tree objects.
Definition: Tree.h:115
Defines the basic types of data flow nodes.
double log(const ExtendedFloat &ef)