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
6
7using namespace std;
8using namespace bpp;
9
10void TreeLikelihoodTools::getAncestralFrequencies(
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
23 std::map<int, std::vector<double>>& frequencies,
24 bool alsoForLeaves)
25{
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
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 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 const Tree & tree() const =0
Get the tree (topology and branch lengths).
virtual size_t getSiteIndex(size_t site) const =0
Get the index (used for inner computations) of a given site (original alignment column).
virtual TreeLikelihoodData & likelihoodData()=0
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.
static void getAncestralFrequencies(const TreeLikelihoodInterface &tl, size_t site, std::map< int, std::vector< double > > &frequencies, bool alsoForLeaves=false)
Compute the expected ancestral frequencies of all states at all (inner) nodes according to a Markov p...
static void getAncestralFrequencies_(const TreeLikelihoodInterface &tl, size_t siteIndex, int parentId, const std::vector< double > &ancestralFrequencies, std::map< int, std::vector< double > > &frequencies, bool alsoForLeaves)
Recursive method, for internal use only.
virtual std::vector< int > getSonsId(int parentId) const =0
virtual bool isLeaf(int nodeId) const =0
virtual int getRootId() const =0
static void fill(std::vector< T > &v, T value)
Defines the basic types of data flow nodes.
std::vector< Vdouble > VVdouble