bpp-phyl3  3.0.0
DRTreeLikelihoodTools.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 
8 
9 using namespace bpp;
10 
11 // -----------------------------------------------------------------------------------------
12 
14  const DRTreeLikelihoodInterface& drl,
15  int nodeId)
16 {
17  size_t nSites = drl.likelihoodData().getNumberOfDistinctSites();
18  size_t nClasses = drl.getNumberOfClasses();
19  size_t nStates = drl.getNumberOfStates();
20  VVVdouble postProb(nSites);
21 
22  auto rDist = drl.getRateDistribution();
23  Vdouble rcProbs = rDist->getProbabilities();
24  if (drl.tree().isLeaf(nodeId))
25  {
26  VVdouble larray = drl.likelihoodData().getLeafLikelihoods(nodeId);
27  for (size_t i = 0; i < nSites; ++i)
28  {
29  VVdouble* postProb_i = &postProb[i];
30  postProb_i->resize(nClasses);
31  Vdouble* larray_i = &larray[i];
32  // In case of generic character:
33  double sumprobs = VectorTools::sum(*larray_i);
34  for (size_t c = 0; c < nClasses; c++)
35  {
36  Vdouble* postProb_i_c = &(*postProb_i)[c];
37  postProb_i_c->resize(nStates);
38  double* rcProb = &rcProbs[c];
39  for (size_t x = 0; x < nStates; x++)
40  {
41  (*postProb_i_c)[x] = (*larray_i)[x] * (*rcProb) / sumprobs;
42  }
43  }
44  }
45  }
46  else
47  {
48  VVVdouble larray;
49  drl.computeLikelihoodAtNode(nodeId, larray);
50 
51  Vdouble likelihoods(nSites, 0);
52  for (size_t i = 0; i < nSites; i++)
53  {
54  VVdouble* larray_i = &larray[i];
55  for (size_t c = 0; c < nClasses; c++)
56  {
57  Vdouble* larray_i_c = &(*larray_i)[c];
58  for (size_t s = 0; s < nStates; s++)
59  {
60  likelihoods[i] += (*larray_i_c)[s];
61  }
62  }
63  }
64 
65  for (size_t i = 0; i < nSites; i++)
66  {
67  VVdouble* postProb_i = &postProb[i];
68  postProb_i->resize(nClasses);
69  VVdouble* larray_i = &larray[i];
70  double likelihood = likelihoods[i];
71  for (size_t c = 0; c < nClasses; c++)
72  {
73  Vdouble* postProb_i_c = &(*postProb_i)[c];
74  postProb_i_c->resize(nStates);
75  Vdouble* larray_i_c = &(*larray_i)[c];
76  for (size_t x = 0; x < nStates; x++)
77  {
78  (*postProb_i_c)[x] = (*larray_i_c)[x] / likelihood;
79  }
80  }
81  }
82  }
83  return postProb;
84 }
85 
86 // -----------------------------------------------------------------------------------------
87 
89  const DRTreeLikelihoodInterface& drl,
90  int nodeId)
91 {
93  Vdouble freqs(drl.getNumberOfStates());
94  double sumw = 0, w;
95  for (size_t i = 0; i < probs.size(); i++)
96  {
97  w = drl.likelihoodData().getWeight(i);
98  sumw += w;
99  for (size_t j = 0; j < drl.getNumberOfClasses(); j++)
100  {
101  for (size_t k = 0; k < drl.getNumberOfStates(); k++)
102  {
103  freqs[k] += probs[i][j][k] * w;
104  }
105  }
106  }
107  for (size_t k = 0; k < drl.getNumberOfStates(); k++)
108  {
109  freqs[k] /= sumw;
110  }
111  return freqs;
112 }
113 
114 // -----------------------------------------------------------------------------------------
unsigned int getWeight(size_t pos) const
VVdouble & getLeafLikelihoods(int nodeId)
Interface for double-recursive (DR) implementation of the likelihood computation.
virtual void computeLikelihoodAtNode(int nodeId, VVVdouble &likelihoodArray) const =0
Compute the likelihood array at a given node.
virtual DRASDRTreeLikelihoodData & likelihoodData() override=0
static VVVdouble getPosteriorProbabilitiesPerStatePerRate(const DRTreeLikelihoodInterface &drl, int nodeId)
Compute the posterior probabilities for each state and each rate of each distinct site.
static Vdouble getPosteriorStateFrequencies(const DRTreeLikelihoodInterface &drl, int nodeId)
Compute the posterior probabilities for each state for a given node.
virtual size_t getNumberOfClasses() const =0
Get the number of classes.
virtual std::shared_ptr< const DiscreteDistributionInterface > getRateDistribution() const =0
Get the rate distribution used for the computation.
virtual size_t getNumberOfStates() const =0
virtual const Tree & tree() const =0
Get the tree (topology and branch lengths).
virtual bool isLeaf(int nodeId) const =0
static T sum(const std::vector< T > &v1)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
std::vector< VVdouble > VVVdouble
std::vector< Vdouble > VVdouble