bpp-phyl  2.2.0
DRTreeLikelihoodTools.cpp
Go to the documentation of this file.
1 //
2 // File: DRTreeLikelihoodTools.cpp
3 // Created by: Julien Dutheil
4 // Created on: Mon Janv 17 09:56 2005
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 "DRTreeLikelihoodTools.h"
41 #include <Bpp/Numeric/VectorTools.h>
42 
43 using namespace bpp;
44 
45 //-----------------------------------------------------------------------------------------
46 
48  const DRTreeLikelihood & drl,
49  int nodeId)
50 {
51  size_t nSites = drl.getLikelihoodData()->getNumberOfDistinctSites();
52  size_t nClasses = drl.getNumberOfClasses();
53  size_t nStates = drl.getNumberOfStates();
54  VVVdouble postProb(nSites);
55 
56  const DiscreteDistribution* rDist = drl.getRateDistribution();
57  Vdouble rcProbs = rDist->getProbabilities();
58  if(drl.getTree().isLeaf(nodeId))
59  {
60  VVdouble larray = drl.getLikelihoodData()->getLeafLikelihoods(nodeId);
61  for(size_t i = 0; i < nSites; i++)
62  {
63  VVdouble * postProb_i = & postProb[i];
64  postProb_i->resize(nClasses);
65  Vdouble * larray_i = & larray[i];
66  for(size_t c = 0; c < nClasses; c++)
67  {
68  Vdouble * postProb_i_c = & (* postProb_i)[c];
69  postProb_i_c->resize(nStates);
70  double * rcProb = & rcProbs[c];
71  for(size_t x = 0; x < nStates; x++)
72  {
73  (* postProb_i_c)[x] = (* larray_i)[x] * (* rcProb);
74  }
75  }
76  }
77  }
78  else
79  {
80  VVVdouble larray;
81  drl.computeLikelihoodAtNode(nodeId, larray);
82 
83  Vdouble likelihoods(nSites, 0);
84  for(size_t i = 0; i < nSites; i++)
85  {
86  VVdouble * larray_i = & larray[i];
87  for(size_t c = 0; c < nClasses; c++)
88  {
89  Vdouble * larray_i_c = & (* larray_i)[c];
90  for(size_t s = 0; s < nStates; s++)
91  {
92  likelihoods[i] += (* larray_i_c)[s];
93  }
94  }
95  }
96 
97  for(size_t i = 0; i < nSites; i++)
98  {
99  VVdouble * postProb_i = & postProb[i];
100  postProb_i->resize(nClasses);
101  VVdouble * larray_i = & larray[i];
102  double likelihood = likelihoods[i];
103  for(size_t c = 0; c < nClasses; c++)
104  {
105  Vdouble * postProb_i_c = & (* postProb_i)[c];
106  postProb_i_c->resize(nStates);
107  Vdouble * larray_i_c = & (* larray_i)[c];
108  for(size_t x = 0; x < nStates; x++)
109  {
110  (* postProb_i_c)[x] = (* larray_i_c)[x] / likelihood;
111  }
112  }
113  }
114  }
115  return postProb;
116 }
117 
118 //-----------------------------------------------------------------------------------------
119 
121  const DRTreeLikelihood& drl,
122  int nodeId)
123 {
124  VVVdouble probs = getPosteriorProbabilitiesForEachStateForEachRate(drl, nodeId);
125  Vdouble freqs(drl.getNumberOfStates());
126  double sumw = 0, w;
127  for (size_t i = 0; i < probs.size(); i++)
128  {
129  w = drl.getLikelihoodData()->getWeight(i);
130  sumw += w;
131  for (size_t j = 0; j < drl.getNumberOfClasses(); j++)
132  {
133  for (size_t k = 0; k < drl.getNumberOfStates(); k++)
134  {
135  freqs[k] += probs[i][j][k] * w;
136  }
137  }
138  }
139  for (size_t k = 0; k < drl.getNumberOfStates(); k++)
140  {
141  freqs[k] /= sumw;
142  }
143  return freqs;
144 }
145 
146 //-----------------------------------------------------------------------------------------
147 
unsigned int getWeight(size_t pos) const
virtual size_t getNumberOfStates() const =0
virtual size_t getNumberOfClasses() const =0
Get the number of classes.
VVdouble & getLeafLikelihoods(int nodeId)
virtual bool isLeaf(int nodeId) const =0
virtual const Tree & getTree() const =0
Get the tree (topology and branch lengths).
virtual DRASDRTreeLikelihoodData * getLikelihoodData()=0
virtual const DiscreteDistribution * getRateDistribution() const =0
Get the rate distribution used for the computation.
virtual void computeLikelihoodAtNode(int nodeId, VVVdouble &likelihoodArray) const =0
Compute the likelihood array at a given node.
Interface for double-recursive (DR) implementation of the likelihood computation. ...
static Vdouble getPosteriorStateFrequencies(const DRTreeLikelihood &drl, int nodeId)
Compute the posterior probabilities for each state for a given node.
static VVVdouble getPosteriorProbabilitiesForEachStateForEachRate(const DRTreeLikelihood &drl, int nodeId)
Compute the posterior probabilities for each state and each rate of each distinct site...