~ubuntu-branches/ubuntu/precise/libbpp-phyl/precise

« back to all changes in this revision

Viewing changes to src/Bpp/Phyl/Likelihood/DRTreeLikelihoodTools.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Dutheil
  • Date: 2011-06-09 11:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110609110000-yvx78svv6w7xxgph
Tags: upstream-2.0.2
Import upstream version 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
47
VVVdouble DRTreeLikelihoodTools::getPosteriorProbabilitiesForEachStateForEachRate(
 
48
  const DRTreeLikelihood & drl,
 
49
  int nodeId)
 
50
{
 
51
  unsigned int nSites   = drl.getLikelihoodData()->getNumberOfDistinctSites();
 
52
  unsigned int nClasses = drl.getNumberOfClasses();
 
53
  unsigned int 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(unsigned int 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(unsigned int 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(unsigned int 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(unsigned int i = 0; i < nSites; i++)
 
85
    {
 
86
      VVdouble * larray_i = & larray[i];
 
87
      for(unsigned int c = 0; c < nClasses; c++)
 
88
      {
 
89
        Vdouble * larray_i_c = & (* larray_i)[c];
 
90
        for(unsigned int s = 0; s < nStates; s++)
 
91
        {
 
92
          likelihoods[i] += (* larray_i_c)[s];
 
93
        }
 
94
      }
 
95
    }
 
96
    
 
97
    for(unsigned int 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(unsigned int 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(unsigned int 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
 
 
120
Vdouble DRTreeLikelihoodTools::getPosteriorStateFrequencies(
 
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 (unsigned int i = 0; i < probs.size(); i++)
 
128
  {
 
129
    w = drl.getLikelihoodData()->getWeight(i);
 
130
    sumw += w;
 
131
    for (unsigned int j = 0; j < drl.getNumberOfClasses(); j++)
 
132
    {
 
133
      for (unsigned int k = 0; k < drl.getNumberOfStates(); k++)
 
134
      {
 
135
        freqs[k] += probs[i][j][k] * w;
 
136
      }
 
137
    }
 
138
  }
 
139
  for (unsigned int k = 0; k < drl.getNumberOfStates(); k++)
 
140
  {
 
141
    freqs[k] /= sumw;
 
142
  }
 
143
  return freqs;
 
144
}
 
145
 
 
146
//-----------------------------------------------------------------------------------------
 
147