bpp-phyl3  3.0.0
DRNonHomogeneousTreeLikelihood.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
6 #include <Bpp/Text/TextTools.h>
7 
8 #include "../../PatternTools.h"
10 
11 // From bpp-seq:
12 #include <Bpp/Seq/SiteTools.h>
15 
16 using namespace bpp;
17 
18 // From the STL:
19 #include <iostream>
20 
21 using namespace std;
22 
23 /******************************************************************************/
24 
26  const Tree& tree,
27  std::shared_ptr<SubstitutionModelSet> modelSet,
28  std::shared_ptr<DiscreteDistributionInterface> rDist,
29  bool verbose,
30  bool reparametrizeRoot) :
31  AbstractNonHomogeneousTreeLikelihood(tree, modelSet, rDist, verbose, reparametrizeRoot),
32  likelihoodData_(),
33  minusLogLik_(-1.)
34 {
35  if (!modelSet->isFullySetUpFor(tree))
36  throw Exception("DRNonHomogeneousTreeLikelihood(constructor). Model set is not fully specified.");
37  init_();
38 }
39 
40 /******************************************************************************/
41 
43  const Tree& tree,
44  const AlignmentDataInterface& data,
45  std::shared_ptr<SubstitutionModelSet> modelSet,
46  std::shared_ptr<DiscreteDistributionInterface> rDist,
47  bool verbose,
48  bool reparametrizeRoot) :
49  AbstractNonHomogeneousTreeLikelihood(tree, modelSet, rDist, verbose, reparametrizeRoot),
50  likelihoodData_(),
51  minusLogLik_(-1.)
52 {
53  if (!modelSet->isFullySetUpFor(tree))
54  throw Exception("DRNonHomogeneousTreeLikelihood(constructor). Model set is not fully specified.");
55  init_();
56  setData(data);
57 }
58 
59 /******************************************************************************/
60 
62 {
63  likelihoodData_ = make_unique<DRASDRTreeLikelihoodData>(
64  tree_,
65  rateDistribution_->getNumberOfCategories());
66 }
67 
68 /******************************************************************************/
69 
72  likelihoodData_(),
73  minusLogLik_(lik.minusLogLik_)
74 {
75  likelihoodData_ = unique_ptr<DRASDRTreeLikelihoodData>(lik.likelihoodData_->clone());
76  likelihoodData_->setTree(tree_);
77 }
78 
79 /******************************************************************************/
80 
82 {
84  likelihoodData_ = unique_ptr<DRASDRTreeLikelihoodData>(lik.likelihoodData_->clone());
85  likelihoodData_->setTree(tree_);
87  return *this;
88 }
89 
90 /******************************************************************************/
91 
93 {
94  data_ = PatternTools::getSequenceSubset(sites, *tree_->getRootNode());
95  if (verbose_)
96  ApplicationTools::displayTask("Initializing data structure");
97  likelihoodData_->initLikelihoods(*data_, *modelSet_->getModel(0)); // We assume here that all models have the same number of states, and that they have the same 'init' method,
98  // Which is a reasonable assumption as long as they share the same alphabet.
99  if (verbose_)
101 
102  nbSites_ = likelihoodData_->getNumberOfSites();
103  nbDistinctSites_ = likelihoodData_->getNumberOfDistinctSites();
104  nbStates_ = likelihoodData_->getNumberOfStates();
105 
106  if (verbose_)
107  ApplicationTools::displayResult("Number of distinct sites",
109  initialized_ = false;
110 }
111 
112 /******************************************************************************/
113 
115 {
116  double l = 1.;
117  Vdouble* lik = &likelihoodData_->getRootRateSiteLikelihoodArray();
118  const vector<unsigned int>* w = &likelihoodData_->getWeights();
119  for (size_t i = 0; i < nbDistinctSites_; i++)
120  {
121  l *= std::pow((*lik)[i], (int)(*w)[i]);
122  }
123  return l;
124 }
125 
126 /******************************************************************************/
127 
129 {
130  double ll = 0;
131  Vdouble* lik = &likelihoodData_->getRootRateSiteLikelihoodArray();
132  const vector<unsigned int>* w = &likelihoodData_->getWeights();
133  vector<double> la(nbDistinctSites_);
134  for (size_t i = 0; i < nbDistinctSites_; i++)
135  {
136  la[i] = (*w)[i] * log((*lik)[i]);
137  }
138  sort(la.begin(), la.end());
139  for (size_t i = nbDistinctSites_; i > 0; i--)
140  {
141  ll += la[i - 1];
142  }
143  return ll;
144 }
145 
146 /******************************************************************************/
147 
149 {
150  return likelihoodData_->getRootRateSiteLikelihoodArray()[likelihoodData_->getRootArrayPosition(site)];
151 }
152 
153 /******************************************************************************/
154 
156 {
157  return log(likelihoodData_->getRootRateSiteLikelihoodArray()[likelihoodData_->getRootArrayPosition(site)]);
158 }
159 
160 /******************************************************************************/
162 {
163  return likelihoodData_->getRootSiteLikelihoodArray()[likelihoodData_->getRootArrayPosition(site)][rateClass];
164 }
165 
166 /******************************************************************************/
167 
169 {
170  return log(likelihoodData_->getRootSiteLikelihoodArray()[likelihoodData_->getRootArrayPosition(site)][rateClass]);
171 }
172 
173 /******************************************************************************/
174 
175 double DRNonHomogeneousTreeLikelihood::getLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const
176 {
177  return likelihoodData_->getRootLikelihoodArray()[likelihoodData_->getRootArrayPosition(site)][rateClass][static_cast<size_t>(state)];
178 }
179 
180 /******************************************************************************/
181 
182 double DRNonHomogeneousTreeLikelihood::getLogLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const
183 {
184  return log(likelihoodData_->getRootLikelihoodArray()[likelihoodData_->getRootArrayPosition(site)][rateClass][static_cast<size_t>(state)]);
185 }
186 
187 /******************************************************************************/
188 
190 {
191  setParametersValues(parameters);
192 }
193 
194 /******************************************************************************/
195 
197 {
198  applyParameters();
199 
200  if (params.getCommonParametersWith(rateDistribution_->getIndependentParameters()).size() > 0)
201  {
203  }
204  else
205  {
206  vector<int> ids;
207  vector<string> tmp = params.getCommonParametersWith(modelSet_->getNodeParameters()).getParameterNames();
208  for (size_t i = 0; i < tmp.size(); i++)
209  {
210  vector<int> tmpv = modelSet_->getNodesWithParameter(tmp[i]);
211  ids = VectorTools::vectorUnion(ids, tmpv);
212  }
214  vector<const Node*> nodes;
215  for (size_t i = 0; i < ids.size(); i++)
216  {
217  nodes.push_back(idToNode_[ids[i]]);
218  }
219  vector<const Node*> tmpv;
220  bool test = false;
221  for (size_t i = 0; i < tmp.size(); i++)
222  {
223  if (tmp[i] == "BrLenRoot" || tmp[i] == "RootPosition")
224  {
225  if (!test)
226  {
227  tmpv.push_back(tree_->getRootNode()->getSon(0));
228  tmpv.push_back(tree_->getRootNode()->getSon(1));
229  test = true; // Add only once.
230  }
231  }
232  else
233  tmpv.push_back(nodes_[TextTools::to< size_t >(tmp[i].substr(5))]);
234  }
235  nodes = VectorTools::vectorUnion(nodes, tmpv);
236 
237  for (size_t i = 0; i < nodes.size(); i++)
238  {
240  }
241  rootFreqs_ = modelSet_->getRootFrequencies();
242  }
245  {
247  }
249  {
251  }
252 }
253 
254 /******************************************************************************/
255 
257 {
258  if (!isInitialized())
259  throw Exception("DRNonHomogeneousTreeLikelihood::getValue(). Instance is not initialized.");
260  return -getLogLikelihood();
261 }
262 
263 /******************************************************************************
264 * First Order Derivatives *
265 ******************************************************************************/
267 {
268  const Node* father = node->getFather();
269  VVVdouble* _likelihoods_father_node = &likelihoodData_->getLikelihoodArray(father->getId(), node->getId());
270  Vdouble* _dLikelihoods_node = &likelihoodData_->getDLikelihoodArray(node->getId());
271  VVVdouble* pxy__node = &pxy_[node->getId()];
272  VVVdouble* dpxy__node = &dpxy_[node->getId()];
273  VVVdouble larray;
274  computeLikelihoodAtNode_(father, larray);
275  Vdouble* rootLikelihoodsSR = &likelihoodData_->getRootRateSiteLikelihoodArray();
276 
277  double dLi, dLic, dLicx, numerator, denominator;
278  for (size_t i = 0; i < nbDistinctSites_; i++)
279  {
280  VVdouble* _likelihoods_father_node_i = &(*_likelihoods_father_node)[i];
281  VVdouble* larray_i = &larray[i];
282  dLi = 0;
283  for (size_t c = 0; c < nbClasses_; c++)
284  {
285  Vdouble* _likelihoods_father_node_i_c = &(*_likelihoods_father_node_i)[c];
286  Vdouble* larray_i_c = &(*larray_i)[c];
287  VVdouble* pxy__node_c = &(*pxy__node)[c];
288  VVdouble* dpxy__node_c = &(*dpxy__node)[c];
289  dLic = 0;
290  for (size_t x = 0; x < nbStates_; x++)
291  {
292  numerator = 0;
293  denominator = 0;
294  Vdouble* pxy__node_c_x = &(*pxy__node_c)[x];
295  Vdouble* dpxy__node_c_x = &(*dpxy__node_c)[x];
296  dLicx = 0;
297  for (size_t y = 0; y < nbStates_; y++)
298  {
299  numerator += (*dpxy__node_c_x)[y] * (*_likelihoods_father_node_i_c)[y];
300  denominator += (*pxy__node_c_x)[y] * (*_likelihoods_father_node_i_c)[y];
301  }
302  dLicx = denominator == 0. ? 0. : (*larray_i_c)[x] * numerator / denominator;
303  dLic += dLicx;
304  }
305  dLi += rateDistribution_->getProbability(c) * dLic;
306  }
307  (*_dLikelihoods_node)[i] = dLi / (*rootLikelihoodsSR)[i];
308  }
309 }
310 
311 /******************************************************************************/
312 
314 {
315  for (size_t k = 0; k < nbNodes_; k++)
316  {
318  }
319 }
320 
321 /******************************************************************************/
322 
324 {
325  if (!hasParameter(variable))
326  throw ParameterNotFoundException("DRNonHomogeneousTreeLikelihood::getFirstOrderDerivative().", variable);
328  {
329  throw Exception("Derivatives respective to rate distribution parameters are not implemented.");
330  }
332  {
333  throw Exception("Derivatives respective to substitution model parameters are not implemented.");
334  }
335 
336  //
337  // Computation for branch lengths:
338  //
339  const vector<unsigned int>* w = &likelihoodData_->getWeights();
340  Vdouble* _dLikelihoods_branch;
341  if (variable == "BrLenRoot")
342  {
343  _dLikelihoods_branch = &likelihoodData_->getDLikelihoodArray(root1_);
344  double d1 = 0;
345  for (size_t i = 0; i < nbDistinctSites_; i++)
346  {
347  d1 -= (*w)[i] * (*_dLikelihoods_branch)[i];
348  }
349  _dLikelihoods_branch = &likelihoodData_->getDLikelihoodArray(root2_);
350  double d2 = 0;
351  for (size_t i = 0; i < nbDistinctSites_; i++)
352  {
353  d2 -= (*w)[i] * (*_dLikelihoods_branch)[i];
354  }
355  double pos = getParameterValue("RootPosition");
356  return pos * d1 + (1. - pos) * d2;
357  }
358  else if (variable == "RootPosition")
359  {
360  _dLikelihoods_branch = &likelihoodData_->getDLikelihoodArray(root1_);
361  double d1 = 0;
362  for (size_t i = 0; i < nbDistinctSites_; i++)
363  {
364  d1 -= (*w)[i] * (*_dLikelihoods_branch)[i];
365  }
366  _dLikelihoods_branch = &likelihoodData_->getDLikelihoodArray(root2_);
367  double d2 = 0;
368  for (size_t i = 0; i < nbDistinctSites_; i++)
369  {
370  d2 -= (*w)[i] * (*_dLikelihoods_branch)[i];
371  }
372  double len = getParameterValue("BrLenRoot");
373  return len * (d1 - d2);
374  }
375  else
376  {
377  // Get the node with the branch whose length must be derivated:
378  size_t brI = TextTools::to<size_t>(variable.substr(5));
379  const Node* branch = nodes_[brI];
380  _dLikelihoods_branch = &likelihoodData_->getDLikelihoodArray(branch->getId());
381  double d = 0;
382  for (size_t i = 0; i < nbDistinctSites_; i++)
383  {
384  d += (*w)[i] * (*_dLikelihoods_branch)[i];
385  }
386  return -d;
387  }
388 }
389 
390 /******************************************************************************
391 * Second Order Derivatives *
392 ******************************************************************************/
394 {
395  const Node* father = node->getFather();
396  VVVdouble* _likelihoods_father_node = &likelihoodData_->getLikelihoodArray(father->getId(), node->getId());
397  Vdouble* _d2Likelihoods_node = &likelihoodData_->getD2LikelihoodArray(node->getId());
398  VVVdouble* pxy__node = &pxy_[node->getId()];
399  VVVdouble* d2pxy__node = &d2pxy_[node->getId()];
400  VVVdouble larray;
401  computeLikelihoodAtNode_(father, larray);
402  Vdouble* rootLikelihoodsSR = &likelihoodData_->getRootRateSiteLikelihoodArray();
403 
404  double d2Li, d2Lic, d2Licx, numerator, denominator;
405 
406  for (size_t i = 0; i < nbDistinctSites_; i++)
407  {
408  VVdouble* _likelihoods_father_node_i = &(*_likelihoods_father_node)[i];
409  VVdouble* larray_i = &larray[i];
410  d2Li = 0;
411  for (size_t c = 0; c < nbClasses_; c++)
412  {
413  Vdouble* _likelihoods_father_node_i_c = &(*_likelihoods_father_node_i)[c];
414  Vdouble* larray_i_c = &(*larray_i)[c];
415  VVdouble* pxy__node_c = &(*pxy__node)[c];
416  VVdouble* d2pxy__node_c = &(*d2pxy__node)[c];
417  d2Lic = 0;
418  for (size_t x = 0; x < nbStates_; x++)
419  {
420  numerator = 0;
421  denominator = 0;
422  Vdouble* pxy__node_c_x = &(*pxy__node_c)[x];
423  Vdouble* d2pxy__node_c_x = &(*d2pxy__node_c)[x];
424  d2Licx = 0;
425  for (size_t y = 0; y < nbStates_; y++)
426  {
427  numerator += (*d2pxy__node_c_x)[y] * (*_likelihoods_father_node_i_c)[y];
428  denominator += (*pxy__node_c_x)[y] * (*_likelihoods_father_node_i_c)[y];
429  }
430  d2Licx = denominator == 0. ? 0. : (*larray_i_c)[x] * numerator / denominator;
431  d2Lic += d2Licx;
432  }
433  d2Li += rateDistribution_->getProbability(c) * d2Lic;
434  }
435  (*_d2Likelihoods_node)[i] = d2Li / (*rootLikelihoodsSR)[i];
436  }
437 }
438 
439 /******************************************************************************/
440 
442 {
443  for (size_t k = 0; k < nbNodes_; k++)
444  {
446  }
447 }
448 
449 /******************************************************************************/
450 
452 {
453  if (!hasParameter(variable))
454  throw ParameterNotFoundException("DRNonHomogeneousTreeLikelihood::getSecondOrderDerivative().", variable);
456  {
457  throw Exception("Derivatives respective to rate distribution parameters are not implemented.");
458  }
460  {
461  throw Exception("Derivatives respective to substitution model parameters are not implemented.");
462  }
463 
464  //
465  // Computation for branch lengths:
466  //
467 
468  const vector<unsigned int>* w = &likelihoodData_->getWeights();
469  // We can't deduce second order derivatives regarding BrLenRoot and RootPosition from the
470  // branch length derivatives. We need a bit more calculations...
471  // NB: we could save a few calculations here...
472  if (variable == "BrLenRoot")
473  {
474  const Node* father = tree_->getRootNode();
475 
476  // Compute dLikelihoods array for the father node.
477  // Fist initialize to 1:
478  VVVdouble dLikelihoods_father(nbDistinctSites_);
479  VVVdouble d2Likelihoods_father(nbDistinctSites_);
480  for (size_t i = 0; i < nbDistinctSites_; i++)
481  {
482  VVdouble* dLikelihoods_father_i = &dLikelihoods_father[i];
483  VVdouble* d2Likelihoods_father_i = &d2Likelihoods_father[i];
484  dLikelihoods_father_i->resize(nbClasses_);
485  d2Likelihoods_father_i->resize(nbClasses_);
486  for (size_t c = 0; c < nbClasses_; c++)
487  {
488  Vdouble* dLikelihoods_father_i_c = &(*dLikelihoods_father_i)[c];
489  Vdouble* d2Likelihoods_father_i_c = &(*d2Likelihoods_father_i)[c];
490  dLikelihoods_father_i_c->resize(nbStates_);
491  d2Likelihoods_father_i_c->resize(nbStates_);
492  for (size_t s = 0; s < nbStates_; s++)
493  {
494  (*dLikelihoods_father_i_c)[s] = 1.;
495  (*d2Likelihoods_father_i_c)[s] = 1.;
496  }
497  }
498  }
499 
500  size_t nbNodes = father->getNumberOfSons();
501  for (size_t l = 0; l < nbNodes; l++)
502  {
503  const Node* son = father->getSon(l);
504 
505  if (son->getId() == root1_)
506  {
507  VVVdouble* _likelihoodsroot1_ = &likelihoodData_->getLikelihoodArray(father->getId(), root1_);
508  VVVdouble* _likelihoodsroot2_ = &likelihoodData_->getLikelihoodArray(father->getId(), root2_);
509  double pos = getParameterValue("RootPosition");
510 
511  VVVdouble* d2pxy_root1_ = &d2pxy_[root1_];
512  VVVdouble* d2pxy_root2_ = &d2pxy_[root2_];
513  VVVdouble* dpxy_root1_ = &dpxy_[root1_];
514  VVVdouble* dpxy_root2_ = &dpxy_[root2_];
515  VVVdouble* pxy_root1_ = &pxy_[root1_];
516  VVVdouble* pxy_root2_ = &pxy_[root2_];
517  for (size_t i = 0; i < nbDistinctSites_; i++)
518  {
519  VVdouble* _likelihoodsroot1__i = &(*_likelihoodsroot1_)[i];
520  VVdouble* _likelihoodsroot2__i = &(*_likelihoodsroot2_)[i];
521  VVdouble* dLikelihoods_father_i = &dLikelihoods_father[i];
522  VVdouble* d2Likelihoods_father_i = &d2Likelihoods_father[i];
523  for (size_t c = 0; c < nbClasses_; c++)
524  {
525  Vdouble* _likelihoodsroot1__i_c = &(*_likelihoodsroot1__i)[c];
526  Vdouble* _likelihoodsroot2__i_c = &(*_likelihoodsroot2__i)[c];
527  Vdouble* dLikelihoods_father_i_c = &(*dLikelihoods_father_i)[c];
528  Vdouble* d2Likelihoods_father_i_c = &(*d2Likelihoods_father_i)[c];
529  VVdouble* d2pxy_root1__c = &(*d2pxy_root1_)[c];
530  VVdouble* d2pxy_root2__c = &(*d2pxy_root2_)[c];
531  VVdouble* dpxy_root1__c = &(*dpxy_root1_)[c];
532  VVdouble* dpxy_root2__c = &(*dpxy_root2_)[c];
533  VVdouble* pxy_root1__c = &(*pxy_root1_)[c];
534  VVdouble* pxy_root2__c = &(*pxy_root2_)[c];
535  for (size_t x = 0; x < nbStates_; x++)
536  {
537  Vdouble* d2pxy_root1__c_x = &(*d2pxy_root1__c)[x];
538  Vdouble* d2pxy_root2__c_x = &(*d2pxy_root2__c)[x];
539  Vdouble* dpxy_root1__c_x = &(*dpxy_root1__c)[x];
540  Vdouble* dpxy_root2__c_x = &(*dpxy_root2__c)[x];
541  Vdouble* pxy_root1__c_x = &(*pxy_root1__c)[x];
542  Vdouble* pxy_root2__c_x = &(*pxy_root2__c)[x];
543  double d2l1 = 0, d2l2 = 0, dl1 = 0, dl2 = 0, l1 = 0, l2 = 0;
544  for (size_t y = 0; y < nbStates_; y++)
545  {
546  d2l1 += (*d2pxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
547  d2l2 += (*d2pxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
548  dl1 += (*dpxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
549  dl2 += (*dpxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
550  l1 += (*pxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
551  l2 += (*pxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
552  }
553  double dl = pos * dl1 * l2 + (1. - pos) * dl2 * l1;
554  double d2l = pos * pos * d2l1 * l2 + (1. - pos) * (1. - pos) * d2l2 * l1 + 2 * pos * (1. - pos) * dl1 * dl2;
555  (*dLikelihoods_father_i_c)[x] *= dl;
556  (*d2Likelihoods_father_i_c)[x] *= d2l;
557  }
558  }
559  }
560  }
561  else if (son->getId() == root2_)
562  {
563  // Do nothing, this was accounted when dealing with root1_
564  }
565  else
566  {
567  // Account for a putative multifurcation:
568  VVVdouble* _likelihoods_son = &likelihoodData_->getLikelihoodArray(father->getId(), son->getId());
569 
570  VVVdouble* pxy__son = &pxy_[son->getId()];
571  for (size_t i = 0; i < nbDistinctSites_; i++)
572  {
573  VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[i];
574  VVdouble* dLikelihoods_father_i = &dLikelihoods_father[i];
575  VVdouble* d2Likelihoods_father_i = &d2Likelihoods_father[i];
576  for (size_t c = 0; c < nbClasses_; c++)
577  {
578  Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
579  Vdouble* dLikelihoods_father_i_c = &(*dLikelihoods_father_i)[c];
580  Vdouble* d2Likelihoods_father_i_c = &(*d2Likelihoods_father_i)[c];
581  VVdouble* pxy__son_c = &(*pxy__son)[c];
582  for (size_t x = 0; x < nbStates_; x++)
583  {
584  double dl = 0;
585  Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
586  for (size_t y = 0; y < nbStates_; y++)
587  {
588  dl += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
589  }
590  (*dLikelihoods_father_i_c)[x] *= dl;
591  (*d2Likelihoods_father_i_c)[x] *= dl;
592  }
593  }
594  }
595  }
596  }
597  Vdouble* rootLikelihoodsSR = &likelihoodData_->getRootRateSiteLikelihoodArray();
598  double d2l = 0, dlx, d2lx;
599  for (size_t i = 0; i < nbDistinctSites_; i++)
600  {
601  VVdouble* dLikelihoods_father_i = &dLikelihoods_father[i];
602  VVdouble* d2Likelihoods_father_i = &d2Likelihoods_father[i];
603  dlx = 0, d2lx = 0;
604  for (size_t c = 0; c < nbClasses_; c++)
605  {
606  Vdouble* dLikelihoods_father_i_c = &(*dLikelihoods_father_i)[c];
607  Vdouble* d2Likelihoods_father_i_c = &(*d2Likelihoods_father_i)[c];
608  for (size_t x = 0; x < nbStates_; x++)
609  {
610  dlx += rateDistribution_->getProbability(c) * rootFreqs_[x] * (*dLikelihoods_father_i_c)[x];
611  d2lx += rateDistribution_->getProbability(c) * rootFreqs_[x] * (*d2Likelihoods_father_i_c)[x];
612  }
613  }
614  d2l += (*w)[i] * (d2lx / (*rootLikelihoodsSR)[i] - pow(dlx / (*rootLikelihoodsSR)[i], 2));
615  }
616  return -d2l;
617  }
618  else if (variable == "RootPosition")
619  {
620  const Node* father = tree_->getRootNode();
621 
622  // Compute dLikelihoods array for the father node.
623  // Fist initialize to 1:
624  VVVdouble dLikelihoods_father(nbDistinctSites_);
625  VVVdouble d2Likelihoods_father(nbDistinctSites_);
626  for (size_t i = 0; i < nbDistinctSites_; i++)
627  {
628  VVdouble* dLikelihoods_father_i = &dLikelihoods_father[i];
629  VVdouble* d2Likelihoods_father_i = &d2Likelihoods_father[i];
630  dLikelihoods_father_i->resize(nbClasses_);
631  d2Likelihoods_father_i->resize(nbClasses_);
632  for (size_t c = 0; c < nbClasses_; c++)
633  {
634  Vdouble* dLikelihoods_father_i_c = &(*dLikelihoods_father_i)[c];
635  Vdouble* d2Likelihoods_father_i_c = &(*d2Likelihoods_father_i)[c];
636  dLikelihoods_father_i_c->resize(nbStates_);
637  d2Likelihoods_father_i_c->resize(nbStates_);
638  for (size_t s = 0; s < nbStates_; s++)
639  {
640  (*dLikelihoods_father_i_c)[s] = 1.;
641  (*d2Likelihoods_father_i_c)[s] = 1.;
642  }
643  }
644  }
645 
646  size_t nbNodes = father->getNumberOfSons();
647  for (size_t l = 0; l < nbNodes; l++)
648  {
649  const Node* son = father->getSon(l);
650 
651  if (son->getId() == root1_)
652  {
653  VVVdouble* _likelihoodsroot1_ = &likelihoodData_->getLikelihoodArray(father->getId(), root1_);
654  VVVdouble* _likelihoodsroot2_ = &likelihoodData_->getLikelihoodArray(father->getId(), root2_);
655  double len = getParameterValue("BrLenRoot");
656 
657  VVVdouble* d2pxy_root1_ = &d2pxy_[root1_];
658  VVVdouble* d2pxy_root2_ = &d2pxy_[root2_];
659  VVVdouble* dpxy_root1_ = &dpxy_[root1_];
660  VVVdouble* dpxy_root2_ = &dpxy_[root2_];
661  VVVdouble* pxy_root1_ = &pxy_[root1_];
662  VVVdouble* pxy_root2_ = &pxy_[root2_];
663  for (size_t i = 0; i < nbDistinctSites_; i++)
664  {
665  VVdouble* _likelihoodsroot1__i = &(*_likelihoodsroot1_)[i];
666  VVdouble* _likelihoodsroot2__i = &(*_likelihoodsroot2_)[i];
667  VVdouble* dLikelihoods_father_i = &dLikelihoods_father[i];
668  VVdouble* d2Likelihoods_father_i = &d2Likelihoods_father[i];
669  for (size_t c = 0; c < nbClasses_; c++)
670  {
671  Vdouble* _likelihoodsroot1__i_c = &(*_likelihoodsroot1__i)[c];
672  Vdouble* _likelihoodsroot2__i_c = &(*_likelihoodsroot2__i)[c];
673  Vdouble* dLikelihoods_father_i_c = &(*dLikelihoods_father_i)[c];
674  Vdouble* d2Likelihoods_father_i_c = &(*d2Likelihoods_father_i)[c];
675  VVdouble* d2pxy_root1__c = &(*d2pxy_root1_)[c];
676  VVdouble* d2pxy_root2__c = &(*d2pxy_root2_)[c];
677  VVdouble* dpxy_root1__c = &(*dpxy_root1_)[c];
678  VVdouble* dpxy_root2__c = &(*dpxy_root2_)[c];
679  VVdouble* pxy_root1__c = &(*pxy_root1_)[c];
680  VVdouble* pxy_root2__c = &(*pxy_root2_)[c];
681  for (size_t x = 0; x < nbStates_; x++)
682  {
683  Vdouble* d2pxy_root1__c_x = &(*d2pxy_root1__c)[x];
684  Vdouble* d2pxy_root2__c_x = &(*d2pxy_root2__c)[x];
685  Vdouble* dpxy_root1__c_x = &(*dpxy_root1__c)[x];
686  Vdouble* dpxy_root2__c_x = &(*dpxy_root2__c)[x];
687  Vdouble* pxy_root1__c_x = &(*pxy_root1__c)[x];
688  Vdouble* pxy_root2__c_x = &(*pxy_root2__c)[x];
689  double d2l1 = 0, d2l2 = 0, dl1 = 0, dl2 = 0, l1 = 0, l2 = 0;
690  for (size_t y = 0; y < nbStates_; y++)
691  {
692  d2l1 += (*d2pxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
693  d2l2 += (*d2pxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
694  dl1 += (*dpxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
695  dl2 += (*dpxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
696  l1 += (*pxy_root1__c_x)[y] * (*_likelihoodsroot1__i_c)[y];
697  l2 += (*pxy_root2__c_x)[y] * (*_likelihoodsroot2__i_c)[y];
698  }
699  double dl = len * (dl1 * l2 - dl2 * l1);
700  double d2l = len * len * (d2l1 * l2 + d2l2 * l1 - 2 * dl1 * dl2);
701  (*dLikelihoods_father_i_c)[x] *= dl;
702  (*d2Likelihoods_father_i_c)[x] *= d2l;
703  }
704  }
705  }
706  }
707  else if (son->getId() == root2_)
708  {
709  // Do nothing, this was accounted when dealing with root1_
710  }
711  else
712  {
713  // Account for a putative multifurcation:
714  VVVdouble* _likelihoods_son = &likelihoodData_->getLikelihoodArray(father->getId(), son->getId());
715 
716  VVVdouble* pxy__son = &pxy_[son->getId()];
717  for (size_t i = 0; i < nbDistinctSites_; i++)
718  {
719  VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[i];
720  VVdouble* dLikelihoods_father_i = &dLikelihoods_father[i];
721  VVdouble* d2Likelihoods_father_i = &d2Likelihoods_father[i];
722  for (size_t c = 0; c < nbClasses_; c++)
723  {
724  Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
725  Vdouble* dLikelihoods_father_i_c = &(*dLikelihoods_father_i)[c];
726  Vdouble* d2Likelihoods_father_i_c = &(*d2Likelihoods_father_i)[c];
727  VVdouble* pxy__son_c = &(*pxy__son)[c];
728  for (size_t x = 0; x < nbStates_; x++)
729  {
730  double dl = 0;
731  Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
732  for (size_t y = 0; y < nbStates_; y++)
733  {
734  dl += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
735  }
736  (*dLikelihoods_father_i_c)[x] *= dl;
737  (*d2Likelihoods_father_i_c)[x] *= dl;
738  }
739  }
740  }
741  }
742  }
743  Vdouble* rootLikelihoodsSR = &likelihoodData_->getRootRateSiteLikelihoodArray();
744  double d2l = 0, dlx, d2lx;
745  for (size_t i = 0; i < nbDistinctSites_; i++)
746  {
747  VVdouble* dLikelihoods_father_i = &dLikelihoods_father[i];
748  VVdouble* d2Likelihoods_father_i = &d2Likelihoods_father[i];
749  dlx = 0, d2lx = 0;
750  for (size_t c = 0; c < nbClasses_; c++)
751  {
752  Vdouble* dLikelihoods_father_i_c = &(*dLikelihoods_father_i)[c];
753  Vdouble* d2Likelihoods_father_i_c = &(*d2Likelihoods_father_i)[c];
754  for (size_t x = 0; x < nbStates_; x++)
755  {
756  dlx += rateDistribution_->getProbability(c) * rootFreqs_[x] * (*dLikelihoods_father_i_c)[x];
757  d2lx += rateDistribution_->getProbability(c) * rootFreqs_[x] * (*d2Likelihoods_father_i_c)[x];
758  }
759  }
760  d2l += (*w)[i] * (d2lx / (*rootLikelihoodsSR)[i] - pow(dlx / (*rootLikelihoodsSR)[i], 2));
761  }
762  return -d2l;
763  }
764  else
765  {
766  Vdouble* _dLikelihoods_branch;
767  Vdouble* _d2Likelihoods_branch;
768  // Get the node with the branch whose length must be derivated:
769  size_t brI = TextTools::to<size_t>(variable.substr(5));
770  const Node* branch = nodes_[brI];
771  _dLikelihoods_branch = &likelihoodData_->getDLikelihoodArray(branch->getId());
772  _d2Likelihoods_branch = &likelihoodData_->getD2LikelihoodArray(branch->getId());
773  double d2l = 0;
774  for (size_t i = 0; i < nbDistinctSites_; i++)
775  {
776  d2l += (*w)[i] * ((*_d2Likelihoods_branch)[i] - pow((*_dLikelihoods_branch)[i], 2));
777  }
778  return -d2l;
779  }
780 }
781 
782 /******************************************************************************/
783 
785 {
786  for (size_t n = 0; n < node->getNumberOfSons(); n++)
787  {
788  const Node* subNode = node->getSon(n);
789  resetLikelihoodArray(likelihoodData_->getLikelihoodArray(node->getId(), subNode->getId()));
790  }
791  if (node->hasFather())
792  {
793  const Node* father = node->getFather();
794  resetLikelihoodArray(likelihoodData_->getLikelihoodArray(node->getId(), father->getId()));
795  }
796 }
797 
798 /******************************************************************************/
799 
801 {
802  computeSubtreeLikelihoodPostfix(tree_->getRootNode());
803  computeSubtreeLikelihoodPrefix(tree_->getRootNode());
805 }
806 
807 /******************************************************************************/
808 
810 {
811 // if(node->isLeaf()) return;
812 // cout << node->getId() << "\t" << (node->hasName()?node->getName():"") << endl;
813  if (node->getNumberOfSons() == 0)
814  return;
815 
816  // Set all likelihood arrays to 1 for a start:
817  resetLikelihoodArrays(node);
818 
819  map<int, VVVdouble>* _likelihoods_node = &likelihoodData_->getLikelihoodArrays(node->getId());
820  size_t nbNodes = node->getNumberOfSons();
821  for (size_t l = 0; l < nbNodes; l++)
822  {
823  // For each son node...
824 
825  const Node* son = node->getSon(l);
826  VVVdouble* _likelihoods_node_son = &(*_likelihoods_node)[son->getId()];
827 
828  if (son->isLeaf())
829  {
830  VVdouble* _likelihoods_leaf = &likelihoodData_->getLeafLikelihoods(son->getId());
831  for (size_t i = 0; i < nbDistinctSites_; i++)
832  {
833  // For each site in the sequence,
834  Vdouble* _likelihoods_leaf_i = &(*_likelihoods_leaf)[i];
835  VVdouble* _likelihoods_node_son_i = &(*_likelihoods_node_son)[i];
836  for (size_t c = 0; c < nbClasses_; c++)
837  {
838  // For each rate class,
839  Vdouble* _likelihoods_node_son_i_c = &(*_likelihoods_node_son_i)[c];
840  for (size_t x = 0; x < nbStates_; x++)
841  {
842  // For each initial state,
843  (*_likelihoods_node_son_i_c)[x] = (*_likelihoods_leaf_i)[x];
844  }
845  }
846  }
847  }
848  else
849  {
850  computeSubtreeLikelihoodPostfix(son); // Recursive method:
851  size_t nbSons = son->getNumberOfSons();
852  map<int, VVVdouble>* _likelihoods_son = &likelihoodData_->getLikelihoodArrays(son->getId());
853 
854  vector<const VVVdouble*> iLik(nbSons);
855  vector<const VVVdouble*> tProb(nbSons);
856  for (size_t n = 0; n < nbSons; n++)
857  {
858  const Node* sonSon = son->getSon(n);
859  tProb[n] = &pxy_[sonSon->getId()];
860  iLik[n] = &(*_likelihoods_son)[sonSon->getId()];
861  }
862  computeLikelihoodFromArrays(iLik, tProb, *_likelihoods_node_son, nbSons, nbDistinctSites_, nbClasses_, nbStates_, false);
863  }
864  }
865 }
866 
867 /******************************************************************************/
868 
870 {
871  if (!node->hasFather())
872  {
873  // 'node' is the root of the tree.
874  // Just call the method on each son node:
875  size_t nbSons = node->getNumberOfSons();
876  for (size_t n = 0; n < nbSons; n++)
877  {
879  }
880  return;
881  }
882  else
883  {
884  const Node* father = node->getFather();
885  map<int, VVVdouble>* _likelihoods_node = &likelihoodData_->getLikelihoodArrays(node->getId());
886  map<int, VVVdouble>* _likelihoods_father = &likelihoodData_->getLikelihoodArrays(father->getId());
887  VVVdouble* _likelihoods_node_father = &(*_likelihoods_node)[father->getId()];
888  if (node->isLeaf())
889  {
890  resetLikelihoodArray(*_likelihoods_node_father);
891  }
892 
893  if (father->isLeaf())
894  {
895  // If the tree is rooted by a leaf
896  VVdouble* _likelihoods_leaf = &likelihoodData_->getLeafLikelihoods(father->getId());
897  for (size_t i = 0; i < nbDistinctSites_; i++)
898  {
899  // For each site in the sequence,
900  Vdouble* _likelihoods_leaf_i = &(*_likelihoods_leaf)[i];
901  VVdouble* _likelihoods_node_father_i = &(*_likelihoods_node_father)[i];
902  for (size_t c = 0; c < nbClasses_; c++)
903  {
904  // For each rate class,
905  Vdouble* _likelihoods_node_father_i_c = &(*_likelihoods_node_father_i)[c];
906  for (size_t x = 0; x < nbStates_; x++)
907  {
908  // For each initial state,
909  (*_likelihoods_node_father_i_c)[x] = (*_likelihoods_leaf_i)[x];
910  }
911  }
912  }
913  }
914  else
915  {
916  vector<const Node*> nodes;
917  // Add brothers:
918  size_t nbFatherSons = father->getNumberOfSons();
919  for (size_t n = 0; n < nbFatherSons; n++)
920  {
921  const Node* son = father->getSon(n);
922  if (son->getId() != node->getId())
923  nodes.push_back(son); // This is a real brother, not current node!
924  }
925  // Now the real stuff... We've got to compute the likelihoods for the
926  // subtree defined by node 'father'.
927  // This is the same as postfix method, but with different subnodes.
928 
929  size_t nbSons = nodes.size(); // In case of a bifurcating tree this is equal to 1.
930 
931  vector<const VVVdouble*> iLik(nbSons);
932  vector<const VVVdouble*> tProb(nbSons);
933  for (size_t n = 0; n < nbSons; n++)
934  {
935  const Node* fatherSon = nodes[n];
936  tProb[n] = &pxy_[fatherSon->getId()];
937  iLik[n] = &(*_likelihoods_father)[fatherSon->getId()];
938  }
939 
940  if (father->hasFather())
941  {
942  const Node* fatherFather = father->getFather();
943  computeLikelihoodFromArrays(iLik, tProb, &(*_likelihoods_father)[fatherFather->getId()], &pxy_[father->getId()], *_likelihoods_node_father, nbSons, nbDistinctSites_, nbClasses_, nbStates_, false);
944  }
945  else
946  {
947  computeLikelihoodFromArrays(iLik, tProb, *_likelihoods_node_father, nbSons, nbDistinctSites_, nbClasses_, nbStates_, false);
948  }
949  }
950 
951  if (!father->hasFather())
952  {
953  // We have to account for the root frequencies:
954  for (size_t i = 0; i < nbDistinctSites_; i++)
955  {
956  VVdouble* _likelihoods_node_father_i = &(*_likelihoods_node_father)[i];
957  for (size_t c = 0; c < nbClasses_; c++)
958  {
959  Vdouble* _likelihoods_node_father_i_c = &(*_likelihoods_node_father_i)[c];
960  for (size_t x = 0; x < nbStates_; x++)
961  {
962  (*_likelihoods_node_father_i_c)[x] *= rootFreqs_[x];
963  }
964  }
965  }
966  }
967 
968  // Call the method on each son node:
969  size_t nbNodeSons = node->getNumberOfSons();
970  for (size_t i = 0; i < nbNodeSons; i++)
971  {
972  computeSubtreeLikelihoodPrefix(node->getSon(i)); // Recursive method.
973  }
974  }
975 }
976 
977 /******************************************************************************/
978 
980 {
981  const Node* root = tree_->getRootNode();
982  VVVdouble* rootLikelihoods = &likelihoodData_->getRootLikelihoodArray();
983  // Set all likelihoods to 1 for a start:
984  if (root->isLeaf())
985  {
986  VVdouble* leavesLikelihoods_root = &likelihoodData_->getLeafLikelihoods(root->getId());
987  for (size_t i = 0; i < nbDistinctSites_; i++)
988  {
989  VVdouble* rootLikelihoods_i = &(*rootLikelihoods)[i];
990  Vdouble* leavesLikelihoods_root_i = &(*leavesLikelihoods_root)[i];
991  for (size_t c = 0; c < nbClasses_; c++)
992  {
993  Vdouble* rootLikelihoods_i_c = &(*rootLikelihoods_i)[c];
994  for (size_t x = 0; x < nbStates_; x++)
995  {
996  (*rootLikelihoods_i_c)[x] = (*leavesLikelihoods_root_i)[x];
997  }
998  }
999  }
1000  }
1001  else
1002  {
1003  resetLikelihoodArray(*rootLikelihoods);
1004  }
1005 
1006  map<int, VVVdouble>* likelihoods_root = &likelihoodData_->getLikelihoodArrays(root->getId());
1007  size_t nbNodes = root->getNumberOfSons();
1008  vector<const VVVdouble*> iLik(nbNodes);
1009  vector<const VVVdouble*> tProb(nbNodes);
1010  for (size_t n = 0; n < nbNodes; n++)
1011  {
1012  const Node* son = root->getSon(n);
1013  tProb[n] = &pxy_[son->getId()];
1014  iLik[n] = &(*likelihoods_root)[son->getId()];
1015  }
1016  computeLikelihoodFromArrays(iLik, tProb, *rootLikelihoods, nbNodes, nbDistinctSites_, nbClasses_, nbStates_, false);
1017 
1018  Vdouble p = rateDistribution_->getProbabilities();
1019  VVdouble* rootLikelihoodsS = &likelihoodData_->getRootSiteLikelihoodArray();
1020  Vdouble* rootLikelihoodsSR = &likelihoodData_->getRootRateSiteLikelihoodArray();
1021  for (size_t i = 0; i < nbDistinctSites_; i++)
1022  {
1023  // For each site in the sequence,
1024  VVdouble* rootLikelihoods_i = &(*rootLikelihoods)[i];
1025  Vdouble* rootLikelihoodsS_i = &(*rootLikelihoodsS)[i];
1026  (*rootLikelihoodsSR)[i] = 0;
1027  for (size_t c = 0; c < nbClasses_; c++)
1028  {
1029  // For each rate class,
1030  Vdouble* rootLikelihoods_i_c = &(*rootLikelihoods_i)[c];
1031  double* rootLikelihoodsS_i_c = &(*rootLikelihoodsS_i)[c];
1032  (*rootLikelihoodsS_i_c) = 0;
1033  for (size_t x = 0; x < nbStates_; x++)
1034  {
1035  // For each initial state,
1036  (*rootLikelihoodsS_i_c) += rootFreqs_[x] * (*rootLikelihoods_i_c)[x];
1037  }
1038  (*rootLikelihoodsSR)[i] += p[c] * (*rootLikelihoodsS_i_c);
1039  }
1040 
1041  // Final checking (for numerical errors):
1042  if ((*rootLikelihoodsSR)[i] < 0)
1043  (*rootLikelihoodsSR)[i] = 0.;
1044  }
1045 }
1046 
1047 /******************************************************************************/
1048 
1050 {
1051 // const Node * node = tree_->getNode(nodeId);
1052  int nodeId = node->getId();
1053  likelihoodArray.resize(nbDistinctSites_);
1054  map<int, VVVdouble>* likelihoods_node = &likelihoodData_->getLikelihoodArrays(node->getId());
1055 
1056  // Initialize likelihood array:
1057  if (node->isLeaf())
1058  {
1059  VVdouble* leavesLikelihoods_node = &likelihoodData_->getLeafLikelihoods(nodeId);
1060  for (size_t i = 0; i < nbDistinctSites_; i++)
1061  {
1062  VVdouble* likelihoodArray_i = &likelihoodArray[i];
1063  Vdouble* leavesLikelihoods_node_i = &(*leavesLikelihoods_node)[i];
1064  likelihoodArray_i->resize(nbClasses_);
1065  for (size_t c = 0; c < nbClasses_; c++)
1066  {
1067  Vdouble* likelihoodArray_i_c = &(*likelihoodArray_i)[c];
1068  likelihoodArray_i_c->resize(nbStates_);
1069  for (size_t x = 0; x < nbStates_; x++)
1070  {
1071  (*likelihoodArray_i_c)[x] = (*leavesLikelihoods_node_i)[x];
1072  }
1073  }
1074  }
1075  }
1076  else
1077  {
1078  // Otherwise:
1079  // Set all likelihoods to 1 for a start:
1080  for (size_t i = 0; i < nbDistinctSites_; i++)
1081  {
1082  VVdouble* likelihoodArray_i = &likelihoodArray[i];
1083  likelihoodArray_i->resize(nbClasses_);
1084  for (size_t c = 0; c < nbClasses_; c++)
1085  {
1086  Vdouble* likelihoodArray_i_c = &(*likelihoodArray_i)[c];
1087  likelihoodArray_i_c->resize(nbStates_);
1088  for (size_t x = 0; x < nbStates_; x++)
1089  {
1090  (*likelihoodArray_i_c)[x] = 1.;
1091  }
1092  }
1093  }
1094  }
1095 
1096  size_t nbNodes = node->getNumberOfSons();
1097 
1098  vector<const VVVdouble*> iLik(nbNodes);
1099  vector<const VVVdouble*> tProb(nbNodes);
1100  for (size_t n = 0; n < nbNodes; n++)
1101  {
1102  const Node* son = node->getSon(n);
1103  tProb[n] = &pxy_[son->getId()];
1104  iLik[n] = &(*likelihoods_node)[son->getId()];
1105  }
1106 
1107  if (node->hasFather())
1108  {
1109  const Node* father = node->getFather();
1110  computeLikelihoodFromArrays(iLik, tProb, &(*likelihoods_node)[father->getId()], &pxy_[nodeId], likelihoodArray, nbNodes, nbDistinctSites_, nbClasses_, nbStates_, false);
1111  }
1112  else
1113  {
1114  computeLikelihoodFromArrays(iLik, tProb, likelihoodArray, nbNodes, nbDistinctSites_, nbClasses_, nbStates_, false);
1115 
1116  // We have to account for the root frequencies:
1117  for (size_t i = 0; i < nbDistinctSites_; i++)
1118  {
1119  VVdouble* likelihoodArray_i = &likelihoodArray[i];
1120  for (size_t c = 0; c < nbClasses_; c++)
1121  {
1122  Vdouble* likelihoodArray_i_c = &(*likelihoodArray_i)[c];
1123  for (size_t x = 0; x < nbStates_; x++)
1124  {
1125  (*likelihoodArray_i_c)[x] *= rootFreqs_[x];
1126  }
1127  }
1128  }
1129  }
1130 }
1131 
1132 /******************************************************************************/
1133 
1135  const vector<const VVVdouble*>& iLik,
1136  const vector<const VVVdouble*>& tProb,
1137  VVVdouble& oLik,
1138  size_t nbNodes,
1139  size_t nbDistinctSites,
1140  size_t nbClasses,
1141  size_t nbStates,
1142  bool reset)
1143 {
1144  if (reset)
1145  resetLikelihoodArray(oLik);
1146 
1147  for (size_t n = 0; n < nbNodes; n++)
1148  {
1149  const VVVdouble* pxy_n = tProb[n];
1150  const VVVdouble* iLik_n = iLik[n];
1151 
1152  for (size_t i = 0; i < nbDistinctSites; i++)
1153  {
1154  // For each site in the sequence,
1155  const VVdouble* iLik_n_i = &(*iLik_n)[i];
1156  VVdouble* oLik_i = &(oLik)[i];
1157 
1158  for (size_t c = 0; c < nbClasses; c++)
1159  {
1160  // For each rate class,
1161  const Vdouble* iLik_n_i_c = &(*iLik_n_i)[c];
1162  Vdouble* oLik_i_c = &(*oLik_i)[c];
1163  const VVdouble* pxy_n_c = &(*pxy_n)[c];
1164  for (size_t x = 0; x < nbStates; x++)
1165  {
1166  // For each initial state,
1167  const Vdouble* pxy_n_c_x = &(*pxy_n_c)[x];
1168  double likelihood = 0;
1169  for (size_t y = 0; y < nbStates; y++)
1170  {
1171  likelihood += (*pxy_n_c_x)[y] * (*iLik_n_i_c)[y];
1172  }
1173  // We store this conditional likelihood into the corresponding array:
1174  (*oLik_i_c)[x] *= likelihood;
1175  }
1176  }
1177  }
1178  }
1179 }
1180 
1181 /******************************************************************************/
1182 
1184  const vector<const VVVdouble*>& iLik,
1185  const vector<const VVVdouble*>& tProb,
1186  const VVVdouble* iLikR,
1187  const VVVdouble* tProbR,
1188  VVVdouble& oLik,
1189  size_t nbNodes,
1190  size_t nbDistinctSites,
1191  size_t nbClasses,
1192  size_t nbStates,
1193  bool reset)
1194 {
1195  if (reset)
1196  resetLikelihoodArray(oLik);
1197 
1198  for (size_t n = 0; n < nbNodes; n++)
1199  {
1200  const VVVdouble* pxy_n = tProb[n];
1201  const VVVdouble* iLik_n = iLik[n];
1202 
1203  for (size_t i = 0; i < nbDistinctSites; i++)
1204  {
1205  // For each site in the sequence,
1206  const VVdouble* iLik_n_i = &(*iLik_n)[i];
1207  VVdouble* oLik_i = &(oLik)[i];
1208 
1209  for (size_t c = 0; c < nbClasses; c++)
1210  {
1211  // For each rate class,
1212  const Vdouble* iLik_n_i_c = &(*iLik_n_i)[c];
1213  Vdouble* oLik_i_c = &(*oLik_i)[c];
1214  const VVdouble* pxy_n_c = &(*pxy_n)[c];
1215  for (size_t x = 0; x < nbStates; x++)
1216  {
1217  // For each initial state,
1218  const Vdouble* pxy_n_c_x = &(*pxy_n_c)[x];
1219  double likelihood = 0;
1220  for (size_t y = 0; y < nbStates; y++)
1221  {
1222  // cout << "1:" << (* pxy_n_c_x)[y] << endl;
1223  // cout << "2:" << (* iLik_n_i_c)[y] << endl;
1224  likelihood += (*pxy_n_c_x)[y] * (*iLik_n_i_c)[y];
1225  // cout << i << "\t" << c << "\t" << x << "\t" << y << "\t" << (* pxy__son_c_x)[y] << "\t" << (* likelihoods_root_son_i_c)[y] << endl;
1226  }
1227  // We store this conditional likelihood into the corresponding array:
1228  (*oLik_i_c)[x] *= likelihood;
1229  }
1230  }
1231  }
1232  }
1233 
1234  // Now deal with the subtree containing the root:
1235  for (size_t i = 0; i < nbDistinctSites; i++)
1236  {
1237  // For each site in the sequence,
1238  const VVdouble* iLikR_i = &(*iLikR)[i];
1239  VVdouble* oLik_i = &(oLik)[i];
1240 
1241  for (size_t c = 0; c < nbClasses; c++)
1242  {
1243  // For each rate class,
1244  const Vdouble* iLikR_i_c = &(*iLikR_i)[c];
1245  Vdouble* oLik_i_c = &(*oLik_i)[c];
1246  const VVdouble* pxyR_c = &(*tProbR)[c];
1247  for (size_t x = 0; x < nbStates; x++)
1248  {
1249  double likelihood = 0;
1250  for (size_t y = 0; y < nbStates; y++)
1251  {
1252  // For each final state,
1253  const Vdouble* pxyR_c_y = &(*pxyR_c)[y];
1254  likelihood += (*pxyR_c_y)[x] * (*iLikR_i_c)[y];
1255  }
1256  // We store this conditional likelihood into the corresponding array:
1257  (*oLik_i_c)[x] *= likelihood;
1258  }
1259  }
1260  }
1261 }
1262 
1263 /******************************************************************************/
1264 
1266 {
1267  cout << "Likelihoods at node " << node->getId() << ": " << endl;
1268  for (size_t n = 0; n < node->getNumberOfSons(); n++)
1269  {
1270  const Node* subNode = node->getSon(n);
1271  cout << "Array for sub-node " << subNode->getId() << endl;
1272  displayLikelihoodArray(likelihoodData_->getLikelihoodArray(node->getId(), subNode->getId()));
1273  }
1274  if (node->hasFather())
1275  {
1276  const Node* father = node->getFather();
1277  cout << "Array for father node " << father->getId() << endl;
1278  displayLikelihoodArray(likelihoodData_->getLikelihoodArray(node->getId(), father->getId()));
1279  }
1280  cout << " ***" << endl;
1281 }
1282 
1283 /*******************************************************************************/
static void displayLikelihoodArray(const VVVdouble &likelihoodArray)
Print the likelihood array to terminal (debugging tool).
static void resetLikelihoodArray(VVVdouble &likelihoodArray)
Set all conditional likelihoods to 1.
Partial implementation for branch non-homogeneous models of the TreeLikelihood interface.
AbstractNonHomogeneousTreeLikelihood & operator=(const AbstractNonHomogeneousTreeLikelihood &lik)
Assignation operator.
virtual void computeAllTransitionProbabilities()
Fill the pxy_, dpxy_ and d2pxy_ arrays for all nodes.
ParameterList getSubstitutionModelParameters() const override
Get the parameters associated to substitution model(s).
virtual void computeTransitionProbabilitiesForNode(const Node *node)
Fill the pxy_, dpxy_ and d2pxy_ arrays for one node.
std::map< int, const Node * > idToNode_
An index linking nodes to their id, for faster access than the getNode() method.
std::vector< Node * > nodes_
Pointer toward all nodes in the tree.
virtual void applyParameters()
All parameters are stored in a parameter list. This function apply these parameters to the substituti...
ParameterList getRateDistributionParameters() const override
Get the parameters associated to the rate distribution.
void setParametersValues(const ParameterList &parameters) override
bool hasParameter(const std::string &name) const override
double getParameterValue(const std::string &name) const override
const AlignmentDataInterface & data() const
Get the dataset for which the likelihood must be evaluated.
const Tree & tree() const
Get the tree (topology and branch lengths).
std::unique_ptr< const AlignmentDataInterface > data_
std::shared_ptr< TreeTemplate< Node > > tree_
static void displayTask(const std::string &text, bool eof=false)
static void displayTaskDone()
static void displayResult(const std::string &text, const T &result)
This class implements the likelihood computation for a tree using the double-recursive algorithm,...
virtual void computeSubtreeLikelihoodPostfix(const Node *node)
DRNonHomogeneousTreeLikelihood(const Tree &tree, std::shared_ptr< SubstitutionModelSet > modelSet, std::shared_ptr< DiscreteDistributionInterface > rDist, bool verbose=true, bool reparametrizeRoot=false)
Build a new DRNonHomogeneousTreeLikelihood object without data.
void setParameters(const ParameterList &parameters)
Implements the Function interface.
double getLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const
Get the likelihood for a site knowing its rate class and its ancestral state.
void setData(const AlignmentDataInterface &sites)
Set the dataset for which the likelihood must be evaluated.
double getLogLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const
Get the logarithm of the likelihood for a site knowing its rate class and its ancestral state.
std::unique_ptr< DRASDRTreeLikelihoodData > likelihoodData_
double getLogLikelihood() const
Get the logarithm of the likelihood for the whole dataset.
double getLikelihood() const
Get the likelihood for the whole dataset.
virtual void computeTreeDLikelihoodAtNode(const Node *node)
double getFirstOrderDerivative(const std::string &variable) const
virtual void displayLikelihood(const Node *node)
This method is mainly for debugging purpose.
DRNonHomogeneousTreeLikelihood & operator=(const DRNonHomogeneousTreeLikelihood &lik)
double getLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
Get the likelihood for a site knowing its rate class.
double getLikelihoodForASite(size_t site) const
Get the likelihood for a site.
double getLogLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
Get the logarithm of the likelihood for a site knowing its rate class.
void fireParameterChanged(const ParameterList &params)
virtual void computeLikelihoodAtNode_(const Node *node, VVVdouble &likelihoodArray) const
virtual void computeTreeD2LikelihoodAtNode(const Node *node)
virtual void computeSubtreeLikelihoodPrefix(const Node *node)
double getSecondOrderDerivative(const std::string &variable) const
static void computeLikelihoodFromArrays(const std::vector< const VVVdouble * > &iLik, const std::vector< const VVVdouble * > &tProb, VVVdouble &oLik, size_t nbNodes, size_t nbDistinctSites, size_t nbClasses, size_t nbStates, bool reset=true)
Compute conditional likelihoods.
double getLogLikelihoodForASite(size_t site) const
Get the logarithm of the likelihood for a site.
double getValue() const
Function and NNISearchable interface.
void init_()
Method called by constructors.
The phylogenetic node class.
Definition: Node.h:59
virtual int getId() const
Get the node's id.
Definition: Node.h:170
virtual const Node * getSon(size_t pos) const
Definition: Node.h:362
virtual const Node * getFather() const
Get the father of this node is there is one.
Definition: Node.h:306
virtual bool isLeaf() const
Definition: Node.h:667
virtual bool hasFather() const
Tell if this node has a father node.
Definition: Node.h:346
virtual size_t getNumberOfSons() const
Definition: Node.h:355
size_t size() const
virtual ParameterList getCommonParametersWith(const ParameterList &params) const
virtual std::vector< std::string > getParameterNames() const
static std::unique_ptr< AlignmentDataInterface > getSequenceSubset(const AlignmentDataInterface &sequenceSet, const std::shared_ptr< N > node, const AssociationTreeGraphImplObserver< N, E, I > &tree)
Extract the sequences corresponding to a given subtree.
Definition: PatternTools.h:44
Interface for phylogenetic tree objects.
Definition: Tree.h:115
static std::vector< T > vectorUnion(const std::vector< T > &vec1, const std::vector< T > &vec2)
std::string toString(T t)
Defines the basic types of data flow nodes.
double log(const ExtendedFloat &ef)
std::vector< double > Vdouble
ExtendedFloat pow(const ExtendedFloat &ef, double exp)
std::vector< VVdouble > VVVdouble
std::vector< Vdouble > VVdouble