bpp-phyl  2.4.0
TreeLikelihoodTools.cpp
Go to the documentation of this file.
1 //
2 // File: TreeLikelihoodTools.cpp
3 // Created by: Julien Dutheil
4 // Created on: Tue Jun 30 12:25 2009
5 //
6 
7 /*
8 Copyright or © or Copr. CNRS, (November 16, 2004)
9 
10 This software is a computer program whose purpose is to provide classes
11 for phylogenetic data analysis.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #include "TreeLikelihoodTools.h"
41 
42 using namespace std;
43 using namespace bpp;
44 
45 void TreeLikelihoodTools::getAncestralFrequencies(
46  const TreeLikelihood& tl,
47  size_t site,
48  std::map<int, std::vector<double> >& frequencies,
49  bool alsoForLeaves)
50 {
51  int currentId = tl.getTree().getRootId();
52  vector<double> currentFreqs = tl.getRootFrequencies(tl.getSiteIndex(site));
53  getAncestralFrequencies_(tl, tl.getSiteIndex(site), currentId, currentFreqs, frequencies, alsoForLeaves);
54 }
55 
56 void TreeLikelihoodTools::getAncestralFrequencies(
57  const TreeLikelihood& tl,
58  std::map<int, std::vector<double> >& frequencies,
59  bool alsoForLeaves)
60 {
62  size_t ns = tl.getNumberOfStates();
63  double sumw = 0, w;
64  map<int, vector<double> > siteFrequencies;
65  for (size_t i = 0; i < n; ++i)
66  {
67  w = tl.getLikelihoodData()->getWeight(i);
68  sumw += w;
69  }
70  for (size_t i = 0; i < n; ++i)
71  {
72  w = tl.getLikelihoodData()->getWeight(i);
73  getAncestralFrequencies(tl, i, siteFrequencies, alsoForLeaves);
74  //Initialization
75  if (i == 0)
76  {
77  frequencies = siteFrequencies; //Initialize all nodes ids.
78  //Now reset to 0:
79  for (map<int, vector<double> >::iterator it = frequencies.begin(); it != frequencies.end(); it++)
80  VectorTools::fill(it->second, 0.);
81  }
82  map<int, vector<double> >::iterator it = frequencies.begin();
83  map<int, vector<double> >::iterator itSite = siteFrequencies.begin();
84  for (size_t j = 0; j < frequencies.size(); ++j)
85  {
86  for (size_t k = 0; k < ns; ++k)
87  it->second[k] += itSite->second[k] * w / sumw;
88  it++;
89  itSite++;
90  }
91  }
92 }
93 
94 void TreeLikelihoodTools::getAncestralFrequencies_(
95  const TreeLikelihood& tl,
96  size_t siteIndex,
97  int parentId,
98  const std::vector<double>& ancestralFrequencies,
99  std::map<int,std::vector<double> >& frequencies,
100  bool alsoForLeaves)
101 {
102  if (!tl.getTree().isLeaf(parentId) || alsoForLeaves)
103  frequencies[parentId] = ancestralFrequencies;
104  vector<int> sonsId = tl.getTree().getSonsId(parentId);
105  for (size_t i = 0; i < sonsId.size(); i++)
106  {
107  vector<double> sonFrequencies(tl.getNumberOfStates());
108  VVdouble pijt = tl.getTransitionProbabilities(sonsId[i], siteIndex);
109  for (size_t j = 0; j < tl.getNumberOfStates(); j++)
110  {
111  double x = 0;
112  for (size_t k = 0; k < tl.getNumberOfStates(); k++)
113  {
114  x += pijt[k][j] * ancestralFrequencies[k];
115  }
116  sonFrequencies[j] = x;
117  }
118  getAncestralFrequencies_(tl, siteIndex, sonsId[i], sonFrequencies, frequencies, alsoForLeaves);
119  }
120 }
121 
virtual int getRootId() const =0
The TreeLikelihood interface.
STL namespace.
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 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 unsigned int getWeight(size_t pos) const =0
virtual bool isLeaf(int nodeId) const =0
virtual TreeLikelihoodData * getLikelihoodData()=0
virtual const Tree & getTree() const =0
Get the tree (topology and branch lengths).
virtual size_t getNumberOfDistinctSites() const =0
virtual size_t getSiteIndex(size_t site) const =0
Get the index (used for inner computations) of a given site (original alignment column).
virtual std::vector< int > getSonsId(int parentId) const =0
std::vector< Vdouble > VVdouble