2
// File: DRHomogeneousTreeLikelihood.cpp
3
// Created by: Julien Dutheil
4
// Created on: Fri Oct 17 18:14:51 2003
8
Copyright or Ā© or Copr. CNRS, (November 16, 2004)
10
This software is a computer program whose purpose is to provide classes
11
for phylogenetic data analysis.
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".
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
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.
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.
40
#include "DRHomogeneousTreeLikelihood.h"
41
#include "../PatternTools.h"
44
#include <Bpp/Seq/SiteTools.h>
45
#include <Bpp/Seq/Container/AlignedSequenceContainer.h>
46
#include <Bpp/Seq/Container/SequenceContainerTools.h>
48
#include <Bpp/Text/TextTools.h>
49
#include <Bpp/App/ApplicationTools.h>
58
/******************************************************************************/
60
DRHomogeneousTreeLikelihood::DRHomogeneousTreeLikelihood(
62
SubstitutionModel* model,
63
DiscreteDistribution* rDist,
67
AbstractHomogeneousTreeLikelihood(tree, model, rDist, checkRooted, verbose),
74
/******************************************************************************/
76
DRHomogeneousTreeLikelihood::DRHomogeneousTreeLikelihood(
78
const SiteContainer& data,
79
SubstitutionModel* model,
80
DiscreteDistribution* rDist,
84
AbstractHomogeneousTreeLikelihood(tree, model, rDist, checkRooted, verbose),
92
/******************************************************************************/
94
void DRHomogeneousTreeLikelihood::init_() throw (Exception)
96
likelihoodData_ = new DRASDRTreeLikelihoodData(
98
rateDistribution_->getNumberOfCategories());
101
/******************************************************************************/
103
DRHomogeneousTreeLikelihood::DRHomogeneousTreeLikelihood(const DRHomogeneousTreeLikelihood& lik) :
104
AbstractHomogeneousTreeLikelihood(lik),
108
likelihoodData_ = dynamic_cast<DRASDRTreeLikelihoodData*>(lik.likelihoodData_->clone());
109
likelihoodData_->setTree(tree_);
110
minusLogLik_ = lik.minusLogLik_;
113
/******************************************************************************/
115
DRHomogeneousTreeLikelihood& DRHomogeneousTreeLikelihood::operator=(const DRHomogeneousTreeLikelihood& lik)
117
AbstractHomogeneousTreeLikelihood::operator=(lik);
118
if (likelihoodData_) delete likelihoodData_;
119
likelihoodData_ = dynamic_cast<DRASDRTreeLikelihoodData*>(lik.likelihoodData_->clone());
120
likelihoodData_->setTree(tree_);
121
minusLogLik_ = lik.minusLogLik_;
125
/******************************************************************************/
127
DRHomogeneousTreeLikelihood::~DRHomogeneousTreeLikelihood()
129
delete likelihoodData_;
132
/******************************************************************************/
134
void DRHomogeneousTreeLikelihood::setData(const SiteContainer& sites) throw (Exception)
136
if (data_) delete data_;
137
data_ = PatternTools::getSequenceSubset(sites, *tree_->getRootNode());
138
if (verbose_) ApplicationTools::displayTask("Initializing data structure");
139
likelihoodData_->initLikelihoods(*data_, *model_);
140
if (verbose_) ApplicationTools::displayTaskDone();
142
nbSites_ = likelihoodData_->getNumberOfSites();
143
nbDistinctSites_ = likelihoodData_->getNumberOfDistinctSites();
144
nbStates_ = likelihoodData_->getNumberOfStates();
146
if (verbose_) ApplicationTools::displayResult("Number of distinct sites",
147
TextTools::toString(nbDistinctSites_));
148
initialized_ = false;
151
/******************************************************************************/
153
double DRHomogeneousTreeLikelihood::getLikelihood() const
156
Vdouble* lik = &likelihoodData_->getRootRateSiteLikelihoodArray();
157
const vector<unsigned int> * w = &likelihoodData_->getWeights();
158
for (unsigned int i = 0; i < nbDistinctSites_; i++)
160
l *= std::pow((*lik)[i], (int)(*w)[i]);
165
/******************************************************************************/
167
double DRHomogeneousTreeLikelihood::getLogLikelihood() const
170
Vdouble* lik = &likelihoodData_->getRootRateSiteLikelihoodArray();
171
const vector<unsigned int> * w = &likelihoodData_->getWeights();
172
vector<double> la(nbDistinctSites_);
173
for (unsigned int i = 0; i < nbDistinctSites_; i++)
175
la[i] = (*w)[i] * log((*lik)[i]);
177
sort(la.begin(), la.end());
178
for (unsigned int i = nbDistinctSites_; i > 0; i--)
185
/******************************************************************************/
187
double DRHomogeneousTreeLikelihood::getLikelihoodForASite(unsigned int site) const
189
return likelihoodData_->getRootRateSiteLikelihoodArray()[likelihoodData_->getRootArrayPosition(site)];
192
/******************************************************************************/
194
double DRHomogeneousTreeLikelihood::getLogLikelihoodForASite(unsigned int site) const
196
return log(likelihoodData_->getRootRateSiteLikelihoodArray()[likelihoodData_->getRootArrayPosition(site)]);
199
/******************************************************************************/
200
double DRHomogeneousTreeLikelihood::getLikelihoodForASiteForARateClass(unsigned int site, unsigned int rateClass) const
202
return likelihoodData_->getRootSiteLikelihoodArray()[likelihoodData_->getRootArrayPosition(site)][rateClass];
205
/******************************************************************************/
207
double DRHomogeneousTreeLikelihood::getLogLikelihoodForASiteForARateClass(unsigned int site, unsigned int rateClass) const
209
return log(likelihoodData_->getRootSiteLikelihoodArray()[likelihoodData_->getRootArrayPosition(site)][rateClass]);
212
/******************************************************************************/
214
double DRHomogeneousTreeLikelihood::getLikelihoodForASiteForARateClassForAState(unsigned int site, unsigned int rateClass, int state) const
216
return likelihoodData_->getRootLikelihoodArray()[likelihoodData_->getRootArrayPosition(site)][rateClass][state];
219
/******************************************************************************/
221
double DRHomogeneousTreeLikelihood::getLogLikelihoodForASiteForARateClassForAState(unsigned int site, unsigned int rateClass, int state) const
223
return log(likelihoodData_->getRootLikelihoodArray()[likelihoodData_->getRootArrayPosition(site)][rateClass][state]);
226
/******************************************************************************/
228
void DRHomogeneousTreeLikelihood::setParameters(const ParameterList& parameters)
229
throw (ParameterNotFoundException, ConstraintException)
231
setParametersValues(parameters);
234
/******************************************************************************/
236
void DRHomogeneousTreeLikelihood::fireParameterChanged(const ParameterList& params)
240
if (rateDistribution_->getParameters().getCommonParametersWith(params).size() > 0
241
|| model_->getParameters().getCommonParametersWith(params).size() > 0)
243
//Rate parameter changed, need to recompute all probs:
244
computeAllTransitionProbabilities();
246
else if (params.size() > 0)
248
//We may save some computations:
249
for (unsigned int i = 0; i < params.size(); i++)
251
string s = params[i].getName();
252
if (s.substr(0, 5) == "BrLen")
254
//Branch length parameter:
255
computeTransitionProbabilitiesForNode(nodes_[TextTools::to < unsigned int > (s.substr(5))]);
260
computeTreeLikelihood();
261
if (computeFirstOrderDerivatives_)
263
computeTreeDLikelihoods();
265
if (computeSecondOrderDerivatives_)
267
computeTreeD2Likelihoods();
270
minusLogLik_ = -getLogLikelihood();
273
/******************************************************************************/
275
double DRHomogeneousTreeLikelihood::getValue() const
278
if (!isInitialized()) throw Exception("DRHomogeneousTreeLikelihood::getValue(). Instance is not initialized.");
282
/******************************************************************************
283
* First Order Derivatives *
284
******************************************************************************/
286
void DRHomogeneousTreeLikelihood::computeTreeDLikelihoodAtNode(const Node* node)
288
const Node* father = node->getFather();
289
VVVdouble* _likelihoods_father_node = &likelihoodData_->getLikelihoodArray(father->getId(), node->getId());
290
Vdouble* _dLikelihoods_node = &likelihoodData_->getDLikelihoodArray(node->getId());
291
VVVdouble* pxy__node = &pxy_[node->getId()];
292
VVVdouble* dpxy__node = &dpxy_[node->getId()];
294
computeLikelihoodAtNode_(father, larray);
295
Vdouble* rootLikelihoodsSR = &likelihoodData_->getRootRateSiteLikelihoodArray();
297
double dLi, dLic, dLicx, numerator, denominator;
299
for (unsigned int i = 0; i < nbDistinctSites_; i++)
301
VVdouble* _likelihoods_father_node_i = &(*_likelihoods_father_node)[i];
302
VVdouble* larray_i = &larray[i];
304
for (unsigned int c = 0; c < nbClasses_; c++)
306
Vdouble* _likelihoods_father_node_i_c = &(*_likelihoods_father_node_i)[c];
307
Vdouble* larray_i_c = &(*larray_i)[c];
308
VVdouble* pxy__node_c = &(*pxy__node)[c];
309
VVdouble* dpxy__node_c = &(*dpxy__node)[c];
311
for (unsigned int x = 0; x < nbStates_; x++)
315
Vdouble* pxy__node_c_x = &(*pxy__node_c)[x];
316
Vdouble* dpxy__node_c_x = &(*dpxy__node_c)[x];
318
for (unsigned int y = 0; y < nbStates_; y++)
320
numerator += (*dpxy__node_c_x)[y] * (*_likelihoods_father_node_i_c)[y];
321
denominator += (*pxy__node_c_x)[y] * (*_likelihoods_father_node_i_c)[y];
323
dLicx = denominator == 0. ? 0. : (*larray_i_c)[x] * numerator / denominator;
324
//cout << i << "\t" << c << "\t" << x << "\t" << (*larray_i_c)[x] << "\t" << numerator << "\t" << denominator << endl;
327
dLi += rateDistribution_->getProbability(c) * dLic;
329
(*_dLikelihoods_node)[i] = dLi / (*rootLikelihoodsSR)[i];
330
//cout << dLi << "\t" << (*rootLikelihoodsSR)[i] << endl;
334
/******************************************************************************/
336
void DRHomogeneousTreeLikelihood::computeTreeDLikelihoods()
338
for (unsigned int k = 0; k < nbNodes_; k++)
340
computeTreeDLikelihoodAtNode(nodes_[k]);
344
/******************************************************************************/
346
double DRHomogeneousTreeLikelihood::getFirstOrderDerivative(const std::string& variable) const
349
if (!hasParameter(variable))
350
throw ParameterNotFoundException("DRHomogeneousTreeLikelihood::getFirstOrderDerivative().", variable);
351
if (getRateDistributionParameters().hasParameter(variable))
353
throw Exception("Derivatives respective to rate distribution parameters are not implemented.");
355
if (getSubstitutionModelParameters().hasParameter(variable))
357
throw Exception("Derivatives respective to substitution model parameters are not implemented.");
361
// Computation for branch lengths:
364
// Get the node with the branch whose length must be derivated:
365
unsigned int brI = TextTools::to<unsigned int>(variable.substr(5));
366
const Node* branch = nodes_[brI];
367
Vdouble* _dLikelihoods_branch = &likelihoodData_->getDLikelihoodArray(branch->getId());
369
const vector<unsigned int> * w = &likelihoodData_->getWeights();
370
for (unsigned int i = 0; i < nbDistinctSites_; i++)
372
d += (*w)[i] * (*_dLikelihoods_branch)[i];
377
/******************************************************************************
378
* Second Order Derivatives *
379
******************************************************************************/
381
void DRHomogeneousTreeLikelihood::computeTreeD2LikelihoodAtNode(const Node* node)
383
const Node* father = node->getFather();
384
VVVdouble* _likelihoods_father_node = &likelihoodData_->getLikelihoodArray(father->getId(), node->getId());
385
Vdouble* _d2Likelihoods_node = &likelihoodData_->getD2LikelihoodArray(node->getId());
386
VVVdouble* pxy__node = &pxy_[node->getId()];
387
VVVdouble* d2pxy__node = &d2pxy_[node->getId()];
389
computeLikelihoodAtNode_(father, larray);
390
Vdouble* rootLikelihoodsSR = &likelihoodData_->getRootRateSiteLikelihoodArray();
392
double d2Li, d2Lic, d2Licx, numerator, denominator;
394
for (unsigned int i = 0; i < nbDistinctSites_; i++)
396
VVdouble* _likelihoods_father_node_i = &(*_likelihoods_father_node)[i];
397
VVdouble* larray_i = &larray[i];
399
for (unsigned int c = 0; c < nbClasses_; c++)
401
Vdouble* _likelihoods_father_node_i_c = &(*_likelihoods_father_node_i)[c];
402
Vdouble* larray_i_c = &(*larray_i)[c];
403
VVdouble* pxy__node_c = &(*pxy__node)[c];
404
VVdouble* d2pxy__node_c = &(*d2pxy__node)[c];
406
for (unsigned int x = 0; x < nbStates_; x++)
410
Vdouble* pxy__node_c_x = &(*pxy__node_c)[x];
411
Vdouble* d2pxy__node_c_x = &(*d2pxy__node_c)[x];
413
for (unsigned int y = 0; y < nbStates_; y++)
415
numerator += (*d2pxy__node_c_x)[y] * (*_likelihoods_father_node_i_c)[y];
416
denominator += (*pxy__node_c_x)[y] * (*_likelihoods_father_node_i_c)[y];
418
d2Licx = denominator == 0. ? 0. : (*larray_i_c)[x] * numerator / denominator;
421
d2Li += rateDistribution_->getProbability(c) * d2Lic;
423
(*_d2Likelihoods_node)[i] = d2Li / (*rootLikelihoodsSR)[i];
427
/******************************************************************************/
429
void DRHomogeneousTreeLikelihood::computeTreeD2Likelihoods()
431
for (unsigned int k = 0; k < nbNodes_; k++)
433
computeTreeD2LikelihoodAtNode(nodes_[k]);
437
/******************************************************************************/
439
double DRHomogeneousTreeLikelihood::getSecondOrderDerivative(const std::string& variable) const
442
if (!hasParameter(variable))
443
throw ParameterNotFoundException("DRHomogeneousTreeLikelihood::getSecondOrderDerivative().", variable);
444
if (getRateDistributionParameters().hasParameter(variable))
446
throw Exception("Derivatives respective to rate distribution parameters are not implemented.");
448
if (getSubstitutionModelParameters().hasParameter(variable))
450
throw Exception("Derivatives respective to substitution model parameters are not implemented.");
454
// Computation for branch lengths:
457
// Get the node with the branch whose length must be derivated:
458
unsigned int brI = TextTools::to<unsigned int>(variable.substr(5));
459
const Node* branch = nodes_[brI];
460
Vdouble* _dLikelihoods_branch = &likelihoodData_->getDLikelihoodArray(branch->getId());
461
Vdouble* _d2Likelihoods_branch = &likelihoodData_->getD2LikelihoodArray(branch->getId());
463
const vector<unsigned int> * w = &likelihoodData_->getWeights();
464
for (unsigned int i = 0; i < nbDistinctSites_; i++)
466
d2 += (*w)[i] * ((*_d2Likelihoods_branch)[i] - pow((*_dLikelihoods_branch)[i], 2));
471
/******************************************************************************/
473
void DRHomogeneousTreeLikelihood::resetLikelihoodArrays(const Node* node)
475
for (unsigned int n = 0; n < node->getNumberOfSons(); n++)
477
const Node* subNode = node->getSon(n);
478
resetLikelihoodArray(likelihoodData_->getLikelihoodArray(node->getId(), subNode->getId()));
480
if (node->hasFather())
482
const Node* father = node->getFather();
483
resetLikelihoodArray(likelihoodData_->getLikelihoodArray(node->getId(), father->getId()));
487
/******************************************************************************/
489
void DRHomogeneousTreeLikelihood::computeTreeLikelihood()
491
computeSubtreeLikelihoodPostfix(tree_->getRootNode());
492
computeSubtreeLikelihoodPrefix(tree_->getRootNode());
493
computeRootLikelihood();
496
/******************************************************************************/
498
void DRHomogeneousTreeLikelihood::computeSubtreeLikelihoodPostfix(const Node* node)
500
// if(node->isLeaf()) return;
501
//cout << node->getId() << "\t" << (node->hasName()?node->getName():"") << endl;
502
if (node->getNumberOfSons() == 0) return;
504
// Set all likelihood arrays to 1 for a start:
505
resetLikelihoodArrays(node);
507
map<int, VVVdouble> * _likelihoods_node = &likelihoodData_->getLikelihoodArrays(node->getId());
508
unsigned int nbNodes = node->getNumberOfSons();
509
for (unsigned int l = 0; l < nbNodes; l++)
511
//For each son node...
513
const Node* son = node->getSon(l);
514
VVVdouble* _likelihoods_node_son = &(*_likelihoods_node)[son->getId()];
518
VVdouble* _likelihoods_leaf = &likelihoodData_->getLeafLikelihoods(son->getId());
519
for (unsigned int i = 0; i < nbDistinctSites_; i++)
521
//For each site in the sequence,
522
Vdouble* _likelihoods_leaf_i = &(*_likelihoods_leaf)[i];
523
VVdouble* _likelihoods_node_son_i = &(*_likelihoods_node_son)[i];
524
for (unsigned int c = 0; c < nbClasses_; c++)
526
//For each rate classe,
527
Vdouble* _likelihoods_node_son_i_c = &(*_likelihoods_node_son_i)[c];
528
for (unsigned int x = 0; x < nbStates_; x++)
530
//For each initial state,
531
(*_likelihoods_node_son_i_c)[x] = (*_likelihoods_leaf_i)[x];
538
computeSubtreeLikelihoodPostfix(son); //Recursive method:
539
unsigned int nbSons = son->getNumberOfSons();
540
map<int, VVVdouble> * _likelihoods_son = &likelihoodData_->getLikelihoodArrays(son->getId());
542
vector<const VVVdouble*> iLik(nbSons);
543
vector<const VVVdouble*> tProb(nbSons);
544
for (unsigned int n = 0; n < nbSons; n++)
546
const Node* sonSon = son->getSon(n);
547
tProb[n] = &pxy_[sonSon->getId()];
548
iLik[n] = &(*_likelihoods_son)[sonSon->getId()];
550
computeLikelihoodFromArrays(iLik, tProb, *_likelihoods_node_son, nbSons, nbDistinctSites_, nbClasses_, nbStates_, false);
555
/******************************************************************************/
557
void DRHomogeneousTreeLikelihood::computeSubtreeLikelihoodPrefix(const Node* node)
559
if (!node->hasFather())
561
// 'node' is the root of the tree.
562
// Just call the method on each son node:
563
unsigned int nbSons = node->getNumberOfSons();
564
for (unsigned int n = 0; n < nbSons; n++)
566
computeSubtreeLikelihoodPrefix(node->getSon(n));
572
const Node* father = node->getFather();
573
map<int, VVVdouble> * _likelihoods_node = &likelihoodData_->getLikelihoodArrays(node->getId());
574
map<int, VVVdouble> * _likelihoods_father = &likelihoodData_->getLikelihoodArrays(father->getId());
575
VVVdouble* _likelihoods_node_father = &(*_likelihoods_node)[father->getId()];
578
resetLikelihoodArray(*_likelihoods_node_father);
581
if (father->isLeaf())
583
// If the tree is rooted by a leaf
584
VVdouble* _likelihoods_leaf = &likelihoodData_->getLeafLikelihoods(father->getId());
585
for (unsigned int i = 0; i < nbDistinctSites_; i++)
587
//For each site in the sequence,
588
Vdouble* _likelihoods_leaf_i = &(*_likelihoods_leaf)[i];
589
VVdouble* _likelihoods_node_father_i = &(*_likelihoods_node_father)[i];
590
for (unsigned int c = 0; c < nbClasses_; c++)
592
//For each rate classe,
593
Vdouble* _likelihoods_node_father_i_c = &(*_likelihoods_node_father_i)[c];
594
for (unsigned int x = 0; x < nbStates_; x++)
596
//For each initial state,
597
(*_likelihoods_node_father_i_c)[x] = (*_likelihoods_leaf_i)[x];
604
vector<const Node*> nodes;
606
unsigned int nbFatherSons = father->getNumberOfSons();
607
for (unsigned int n = 0; n < nbFatherSons; n++)
609
const Node* son = father->getSon(n);
610
if (son->getId() != node->getId()) nodes.push_back(son); //This is a real brother, not current node!
612
// Now the real stuff... We've got to compute the likelihoods for the
613
// subtree defined by node 'father'.
614
// This is the same as postfix method, but with different subnodes.
616
unsigned int nbSons = nodes.size(); // In case of a bifurcating tree, this is equal to 1, excepted for the root.
618
vector<const VVVdouble*> iLik(nbSons);
619
vector<const VVVdouble*> tProb(nbSons);
620
for (unsigned int n = 0; n < nbSons; n++)
622
const Node* fatherSon = nodes[n];
623
tProb[n] = &pxy_[fatherSon->getId()];
624
iLik[n] = &(*_likelihoods_father)[fatherSon->getId()];
627
if (father->hasFather())
629
const Node* fatherFather = father->getFather();
630
computeLikelihoodFromArrays(iLik, tProb, &(*_likelihoods_father)[fatherFather->getId()], &pxy_[father->getId()], *_likelihoods_node_father, nbSons, nbDistinctSites_, nbClasses_, nbStates_, false);
634
computeLikelihoodFromArrays(iLik, tProb, *_likelihoods_node_father, nbSons, nbDistinctSites_, nbClasses_, nbStates_, false);
638
if (!father->hasFather())
640
//We have to account for the root frequencies:
641
for (unsigned int i = 0; i < nbDistinctSites_; i++)
643
VVdouble* _likelihoods_node_father_i = &(*_likelihoods_node_father)[i];
644
for (unsigned int c = 0; c < nbClasses_; c++)
646
Vdouble* _likelihoods_node_father_i_c = &(*_likelihoods_node_father_i)[c];
647
for (unsigned int x = 0; x < nbStates_; x++)
649
(*_likelihoods_node_father_i_c)[x] *= rootFreqs_[x];
655
// Call the method on each son node:
656
unsigned int nbNodeSons = node->getNumberOfSons();
657
for (unsigned int i = 0; i < nbNodeSons; i++)
659
computeSubtreeLikelihoodPrefix(node->getSon(i)); //Recursive method.
664
/******************************************************************************/
666
void DRHomogeneousTreeLikelihood::computeRootLikelihood()
668
const Node* root = tree_->getRootNode();
669
VVVdouble* rootLikelihoods = &likelihoodData_->getRootLikelihoodArray();
670
// Set all likelihoods to 1 for a start:
673
VVdouble* leavesLikelihoods_root = &likelihoodData_->getLeafLikelihoods(root->getId());
674
for (unsigned int i = 0; i < nbDistinctSites_; i++)
676
VVdouble* rootLikelihoods_i = &(*rootLikelihoods)[i];
677
Vdouble* leavesLikelihoods_root_i = &(*leavesLikelihoods_root)[i];
678
for (unsigned int c = 0; c < nbClasses_; c++)
680
Vdouble* rootLikelihoods_i_c = &(*rootLikelihoods_i)[c];
681
for (unsigned int x = 0; x < nbStates_; x++)
683
(*rootLikelihoods_i_c)[x] = (*leavesLikelihoods_root_i)[x];
690
resetLikelihoodArray(*rootLikelihoods);
693
map<int, VVVdouble> * likelihoods_root = &likelihoodData_->getLikelihoodArrays(root->getId());
694
unsigned int nbNodes = root->getNumberOfSons();
695
vector<const VVVdouble*> iLik(nbNodes);
696
vector<const VVVdouble*> tProb(nbNodes);
697
for (unsigned int n = 0; n < nbNodes; n++)
699
const Node* son = root->getSon(n);
700
tProb[n] = &pxy_[son->getId()];
701
iLik[n] = &(*likelihoods_root)[son->getId()];
703
computeLikelihoodFromArrays(iLik, tProb, *rootLikelihoods, nbNodes, nbDistinctSites_, nbClasses_, nbStates_, false);
705
Vdouble p = rateDistribution_->getProbabilities();
706
VVdouble* rootLikelihoodsS = &likelihoodData_->getRootSiteLikelihoodArray();
707
Vdouble* rootLikelihoodsSR = &likelihoodData_->getRootRateSiteLikelihoodArray();
708
for (unsigned int i = 0; i < nbDistinctSites_; i++)
710
//For each site in the sequence,
711
VVdouble* rootLikelihoods_i = &(*rootLikelihoods)[i];
712
Vdouble* rootLikelihoodsS_i = &(*rootLikelihoodsS)[i];
713
(*rootLikelihoodsSR)[i] = 0;
714
for (unsigned int c = 0; c < nbClasses_; c++)
716
//For each rate classe,
717
Vdouble* rootLikelihoods_i_c = &(*rootLikelihoods_i)[c];
718
double* rootLikelihoodsS_i_c = &(*rootLikelihoodsS_i)[c];
719
(*rootLikelihoodsS_i_c) = 0;
720
for (unsigned int x = 0; x < nbStates_; x++)
722
//For each initial state,
723
(*rootLikelihoodsS_i_c) += rootFreqs_[x] * (*rootLikelihoods_i_c)[x];
725
(*rootLikelihoodsSR)[i] += p[c] * (*rootLikelihoodsS_i_c);
728
//Final checking (for numerical errors):
729
if ((*rootLikelihoodsSR)[i] < 0) (*rootLikelihoodsSR)[i] = 0.;
733
/******************************************************************************/
735
void DRHomogeneousTreeLikelihood::computeLikelihoodAtNode_(const Node* node, VVVdouble& likelihoodArray) const
737
// const Node * node = tree_->getNode(nodeId);
738
int nodeId = node->getId();
739
likelihoodArray.resize(nbDistinctSites_);
740
map<int, VVVdouble> * likelihoods_node = &likelihoodData_->getLikelihoodArrays(nodeId);
742
//Initialize likelihood array:
745
VVdouble* leavesLikelihoods_node = &likelihoodData_->getLeafLikelihoods(nodeId);
746
for (unsigned int i = 0; i < nbDistinctSites_; i++)
748
VVdouble* likelihoodArray_i = &likelihoodArray[i];
749
Vdouble* leavesLikelihoods_node_i = &(*leavesLikelihoods_node)[i];
750
likelihoodArray_i->resize(nbClasses_);
751
for (unsigned int c = 0; c < nbClasses_; c++)
753
Vdouble* likelihoodArray_i_c = &(*likelihoodArray_i)[c];
754
likelihoodArray_i_c->resize(nbStates_);
755
for (unsigned int x = 0; x < nbStates_; x++)
757
(*likelihoodArray_i_c)[x] = (*leavesLikelihoods_node_i)[x];
765
// Set all likelihoods to 1 for a start:
766
for (unsigned int i = 0; i < nbDistinctSites_; i++)
768
VVdouble* likelihoodArray_i = &likelihoodArray[i];
769
likelihoodArray_i->resize(nbClasses_);
770
for (unsigned int c = 0; c < nbClasses_; c++)
772
Vdouble* likelihoodArray_i_c = &(*likelihoodArray_i)[c];
773
likelihoodArray_i_c->resize(nbStates_);
774
for (unsigned int x = 0; x < nbStates_; x++)
776
(*likelihoodArray_i_c)[x] = 1.;
782
unsigned int nbNodes = node->getNumberOfSons();
784
vector<const VVVdouble*> iLik(nbNodes);
785
vector<const VVVdouble*> tProb(nbNodes);
786
for (unsigned int n = 0; n < nbNodes; n++)
788
const Node* son = node->getSon(n);
789
tProb[n] = &pxy_[son->getId()];
790
iLik[n] = &(*likelihoods_node)[son->getId()];
793
if (node->hasFather())
795
const Node* father = node->getFather();
796
computeLikelihoodFromArrays(iLik, tProb, &(*likelihoods_node)[father->getId()], &pxy_[nodeId], likelihoodArray, nbNodes, nbDistinctSites_, nbClasses_, nbStates_, false);
800
computeLikelihoodFromArrays(iLik, tProb, likelihoodArray, nbNodes, nbDistinctSites_, nbClasses_, nbStates_, false);
802
//We have to account for the equilibrium frequencies:
803
for (unsigned int i = 0; i < nbDistinctSites_; i++)
805
VVdouble* likelihoodArray_i = &likelihoodArray[i];
806
for (unsigned int c = 0; c < nbClasses_; c++)
808
Vdouble* likelihoodArray_i_c = &(*likelihoodArray_i)[c];
809
for (unsigned int x = 0; x < nbStates_; x++)
811
(*likelihoodArray_i_c)[x] *= rootFreqs_[x];
818
/******************************************************************************/
820
void DRHomogeneousTreeLikelihood::computeLikelihoodFromArrays(
821
const vector<const VVVdouble*>& iLik,
822
const vector<const VVVdouble*>& tProb,
824
unsigned int nbNodes,
825
unsigned int nbDistinctSites,
826
unsigned int nbClasses,
827
unsigned int nbStates,
830
if (reset) resetLikelihoodArray(oLik);
832
for (unsigned int n = 0; n < nbNodes; n++)
834
const VVVdouble* pxy_n = tProb[n];
835
const VVVdouble* iLik_n = iLik[n];
837
for (unsigned int i = 0; i < nbDistinctSites; i++)
839
//For each site in the sequence,
840
const VVdouble* iLik_n_i = &(*iLik_n)[i];
841
VVdouble* oLik_i = &(oLik)[i];
843
for (unsigned int c = 0; c < nbClasses; c++)
845
//For each rate classe,
846
const Vdouble* iLik_n_i_c = &(*iLik_n_i)[c];
847
Vdouble* oLik_i_c = &(*oLik_i)[c];
848
const VVdouble* pxy_n_c = &(*pxy_n)[c];
849
for (unsigned int x = 0; x < nbStates; x++)
851
//For each initial state,
852
const Vdouble* pxy_n_c_x = &(*pxy_n_c)[x];
853
double likelihood = 0;
854
for (unsigned int y = 0; y < nbStates; y++)
856
likelihood += (*pxy_n_c_x)[y] * (*iLik_n_i_c)[y];
858
// We store this conditionnal likelihood into the corresponding array:
859
(*oLik_i_c)[x] *= likelihood;
866
/******************************************************************************/
868
void DRHomogeneousTreeLikelihood::computeLikelihoodFromArrays(
869
const vector<const VVVdouble*>& iLik,
870
const vector<const VVVdouble*>& tProb,
871
const VVVdouble* iLikR,
872
const VVVdouble* tProbR,
874
unsigned int nbNodes,
875
unsigned int nbDistinctSites,
876
unsigned int nbClasses,
877
unsigned int nbStates,
880
if (reset) resetLikelihoodArray(oLik);
882
for (unsigned int n = 0; n < nbNodes; n++)
884
const VVVdouble* pxy_n = tProb[n];
885
const VVVdouble* iLik_n = iLik[n];
887
for (unsigned int i = 0; i < nbDistinctSites; i++)
889
//For each site in the sequence,
890
const VVdouble* iLik_n_i = &(*iLik_n)[i];
891
VVdouble* oLik_i = &(oLik)[i];
893
for (unsigned int c = 0; c < nbClasses; c++)
895
//For each rate classe,
896
const Vdouble* iLik_n_i_c = &(*iLik_n_i)[c];
897
Vdouble* oLik_i_c = &(*oLik_i)[c];
898
const VVdouble* pxy_n_c = &(*pxy_n)[c];
899
for (unsigned int x = 0; x < nbStates; x++)
901
//For each initial state,
902
const Vdouble* pxy_n_c_x = &(*pxy_n_c)[x];
903
double likelihood = 0;
904
for (unsigned int y = 0; y < nbStates; y++)
906
//cout << "1:" << (* pxy_n_c_x)[y] << endl;
907
//cout << "2:" << (* iLik_n_i_c)[y] << endl;
908
likelihood += (*pxy_n_c_x)[y] * (*iLik_n_i_c)[y];
909
//cout << i << "\t" << c << "\t" << x << "\t" << y << "\t" << (* pxy__son_c_x)[y] << "\t" << (* likelihoods_root_son_i_c)[y] << endl;
911
// We store this conditionnal likelihood into the corresponding array:
912
(*oLik_i_c)[x] *= likelihood;
918
// Now deal with the subtree containing the root:
919
for (unsigned int i = 0; i < nbDistinctSites; i++)
921
//For each site in the sequence,
922
const VVdouble* iLikR_i = &(*iLikR)[i];
923
VVdouble* oLik_i = &(oLik)[i];
925
for (unsigned int c = 0; c < nbClasses; c++)
927
//For each rate classe,
928
const Vdouble* iLikR_i_c = &(*iLikR_i)[c];
929
Vdouble* oLik_i_c = &(*oLik_i)[c];
930
const VVdouble* pxyR_c = &(*tProbR)[c];
931
for (unsigned int x = 0; x < nbStates; x++)
933
double likelihood = 0;
934
for (unsigned int y = 0; y < nbStates; y++)
936
//For each final state,
937
const Vdouble* pxyR_c_y = &(*pxyR_c)[y];
938
likelihood += (*pxyR_c_y)[x] * (*iLikR_i_c)[y];
940
// We store this conditionnal likelihood into the corresponding array:
941
(*oLik_i_c)[x] *= likelihood;
947
/******************************************************************************/
949
void DRHomogeneousTreeLikelihood::displayLikelihood(const Node* node)
951
cout << "Likelihoods at node " << node->getId() << ": " << endl;
952
for (unsigned int n = 0; n < node->getNumberOfSons(); n++)
954
const Node* subNode = node->getSon(n);
955
cout << "Array for sub-node " << subNode->getId() << endl;
956
displayLikelihoodArray(likelihoodData_->getLikelihoodArray(node->getId(), subNode->getId()));
958
if (node->hasFather())
960
const Node* father = node->getFather();
961
cout << "Array for father node " << father->getId() << endl;
962
displayLikelihoodArray(likelihoodData_->getLikelihoodArray(node->getId(), father->getId()));
964
cout << " ***" << endl;
967
/*******************************************************************************/