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