bpp-phyl  2.4.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"
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  //In case of generic character:
67  double sumprobs = VectorTools::sum(*larray_i);
68  for (size_t c = 0; c < nClasses; c++)
69  {
70  Vdouble * postProb_i_c = & (* postProb_i)[c];
71  postProb_i_c->resize(nStates);
72  double * rcProb = & rcProbs[c];
73  for (size_t x = 0; x < nStates; x++)
74  {
75  (* postProb_i_c)[x] = (* larray_i)[x] * (* rcProb) / sumprobs;
76  }
77  }
78  }
79  }
80  else
81  {
82  VVVdouble larray;
83  drl.computeLikelihoodAtNode(nodeId, larray);
84 
85  Vdouble likelihoods(nSites, 0);
86  for(size_t i = 0; i < nSites; i++)
87  {
88  VVdouble * larray_i = & larray[i];
89  for(size_t c = 0; c < nClasses; c++)
90  {
91  Vdouble * larray_i_c = & (* larray_i)[c];
92  for(size_t s = 0; s < nStates; s++)
93  {
94  likelihoods[i] += (* larray_i_c)[s];
95  }
96  }
97  }
98 
99  for(size_t i = 0; i < nSites; i++)
100  {
101  VVdouble * postProb_i = & postProb[i];
102  postProb_i->resize(nClasses);
103  VVdouble * larray_i = & larray[i];
104  double likelihood = likelihoods[i];
105  for(size_t c = 0; c < nClasses; c++)
106  {
107  Vdouble * postProb_i_c = & (* postProb_i)[c];
108  postProb_i_c->resize(nStates);
109  Vdouble * larray_i_c = & (* larray_i)[c];
110  for(size_t x = 0; x < nStates; x++)
111  {
112  (* postProb_i_c)[x] = (* larray_i_c)[x] / likelihood;
113  }
114  }
115  }
116  }
117  return postProb;
118 }
119 
120 //-----------------------------------------------------------------------------------------
121 
123  const DRTreeLikelihood& drl,
124  int nodeId)
125 {
127  Vdouble freqs(drl.getNumberOfStates());
128  double sumw = 0, w;
129  for (size_t i = 0; i < probs.size(); i++)
130  {
131  w = drl.getLikelihoodData()->getWeight(i);
132  sumw += w;
133  for (size_t j = 0; j < drl.getNumberOfClasses(); j++)
134  {
135  for (size_t k = 0; k < drl.getNumberOfStates(); k++)
136  {
137  freqs[k] += probs[i][j][k] * w;
138  }
139  }
140  }
141  for (size_t k = 0; k < drl.getNumberOfStates(); k++)
142  {
143  freqs[k] /= sumw;
144  }
145  return freqs;
146 }
147 
148 //-----------------------------------------------------------------------------------------
149 
static T sum(const std::vector< T > &v1)
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).
std::vector< double > Vdouble
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. ...
virtual Vdouble getProbabilities() const =0
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...
std::vector< VVdouble > VVVdouble
std::vector< Vdouble > VVdouble
unsigned int getWeight(size_t pos) const