bpp-phyl3  3.0.0
TreeLikelihoodTools.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "TreeLikelihoodTools.h"
6 
7 using namespace std;
8 using namespace bpp;
9 
10 void TreeLikelihoodTools::getAncestralFrequencies(
11  const TreeLikelihoodInterface& tl,
12  size_t site,
13  std::map<int, std::vector<double>>& frequencies,
14  bool alsoForLeaves)
15 {
16  int currentId = tl.tree().getRootId();
17  vector<double> currentFreqs = tl.getRootFrequencies(tl.getSiteIndex(site));
18  getAncestralFrequencies_(tl, tl.getSiteIndex(site), currentId, currentFreqs, frequencies, alsoForLeaves);
19 }
20 
21 void TreeLikelihoodTools::getAncestralFrequencies(
22  const TreeLikelihoodInterface& tl,
23  std::map<int, std::vector<double>>& frequencies,
24  bool alsoForLeaves)
25 {
26  size_t n = tl.likelihoodData().getNumberOfDistinctSites();
27  size_t ns = tl.getNumberOfStates();
28  double sumw = 0, w;
29  map<int, vector<double>> siteFrequencies;
30  for (size_t i = 0; i < n; ++i)
31  {
32  w = tl.likelihoodData().getWeight(i);
33  sumw += w;
34  }
35  for (size_t i = 0; i < n; ++i)
36  {
37  w = tl.likelihoodData().getWeight(i);
38  getAncestralFrequencies(tl, i, siteFrequencies, alsoForLeaves);
39  // Initialization
40  if (i == 0)
41  {
42  frequencies = siteFrequencies; // Initialize all nodes ids.
43  // Now reset to 0:
44  for (map<int, vector<double>>::iterator it = frequencies.begin(); it != frequencies.end(); it++)
45  {
46  VectorTools::fill(it->second, 0.);
47  }
48  }
49  map<int, vector<double>>::iterator it = frequencies.begin();
50  map<int, vector<double>>::iterator itSite = siteFrequencies.begin();
51  for (size_t j = 0; j < frequencies.size(); ++j)
52  {
53  for (size_t k = 0; k < ns; ++k)
54  {
55  it->second[k] += itSite->second[k] * w / sumw;
56  }
57  it++;
58  itSite++;
59  }
60  }
61 }
62 
63 void TreeLikelihoodTools::getAncestralFrequencies_(
64  const TreeLikelihoodInterface& tl,
65  size_t siteIndex,
66  int parentId,
67  const std::vector<double>& ancestralFrequencies,
68  std::map<int, std::vector<double>>& frequencies,
69  bool alsoForLeaves)
70 {
71  if (!tl.tree().isLeaf(parentId) || alsoForLeaves)
72  frequencies[parentId] = ancestralFrequencies;
73  vector<int> sonsId = tl.tree().getSonsId(parentId);
74  for (size_t i = 0; i < sonsId.size(); i++)
75  {
76  vector<double> sonFrequencies(tl.getNumberOfStates());
77  VVdouble pijt = tl.getTransitionProbabilities(sonsId[i], siteIndex);
78  for (size_t j = 0; j < tl.getNumberOfStates(); j++)
79  {
80  double x = 0;
81  for (size_t k = 0; k < tl.getNumberOfStates(); k++)
82  {
83  x += pijt[k][j] * ancestralFrequencies[k];
84  }
85  sonFrequencies[j] = x;
86  }
87  getAncestralFrequencies_(tl, siteIndex, sonsId[i], sonFrequencies, frequencies, alsoForLeaves);
88  }
89 }
virtual size_t getNumberOfDistinctSites() const =0
virtual unsigned int getWeight(size_t pos) const =0
The TreeLikelihood interface.
virtual size_t getNumberOfStates() const =0
virtual TreeLikelihoodData & likelihoodData()=0
virtual const std::vector< double > & getRootFrequencies(size_t siteIndex) const =0
Get the values of the frequencies for each state in the alphabet at the root node.
virtual size_t getSiteIndex(size_t site) const =0
Get the index (used for inner computations) of a given site (original alignment column).
virtual const Tree & tree() const =0
Get the tree (topology and branch lengths).
virtual VVdouble getTransitionProbabilities(int nodeId, size_t siteIndex) const =0
Retrieves all Pij(t) for a particular branch, defined by the upper node and site.
virtual std::vector< int > getSonsId(int parentId) const =0
virtual bool isLeaf(int nodeId) const =0
virtual int getRootId() const =0
Defines the basic types of data flow nodes.
std::vector< Vdouble > VVdouble