bpp-phyl  2.2.0
DistanceEstimation.cpp
Go to the documentation of this file.
1 //
2 // File: DistanceEstimation.cpp
3 // Created by: Julien Dutheil
4 // Vincent Ranwez
5 // Created on: Wed jun 08 10:39 2005
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for phylogenetic data analysis.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39  */
40 
41 #include "DistanceEstimation.h"
42 #include "../Tree.h"
43 #include "../PatternTools.h"
44 #include "../SitePatterns.h"
45 
46 // From bpp-core:
47 #include <Bpp/App/ApplicationTools.h>
48 #include <Bpp/Numeric/AutoParameter.h>
49 
50 // From bpp-seq:
51 #include <Bpp/Seq/SiteTools.h>
52 #include <Bpp/Seq/Sequence.h>
53 #include <Bpp/Seq/Container/AlignedSequenceContainer.h>
54 #include <Bpp/Seq/DistanceMatrix.h>
55 
56 using namespace bpp;
57 
58 // From the STL:
59 #include <vector>
60 #include <string>
61 #include <iostream>
62 #include <fstream>
63 
64 using namespace std;
65 
66 /******************************************************************************/
67 
69  const std::string& seq1, const std::string& seq2,
70  const SiteContainer& data,
71  SubstitutionModel* model,
72  DiscreteDistribution* rDist,
73  bool verbose) throw (Exception) :
75  shrunkData_(0), seqnames_(2), model_(model), brLenParameters_(), pxy_(), dpxy_(), d2pxy_(),
76  rootPatternLinks_(), rootWeights_(), nbSites_(0), nbClasses_(0), nbStates_(0), nbDistinctSites_(0),
77  rootLikelihoods_(), rootLikelihoodsS_(), rootLikelihoodsSR_(), dLikelihoods_(), d2Likelihoods_(),
78  leafLikelihoods1_(), leafLikelihoods2_(),
79  minimumBrLen_(0.000001), brLenConstraint_(0), brLen_(0)
80 {
81  seqnames_[0] = seq1;
82  seqnames_[1] = seq2;
83  data_ = PatternTools::getSequenceSubset(data, seqnames_);
84  if (data_->getAlphabet()->getAlphabetType()
85  != model_->getAlphabet()->getAlphabetType())
86  throw AlphabetMismatchException("TwoTreeTreeLikelihood::TwoTreeTreeLikelihood. Data and model must have the same alphabet type.",
87  data_->getAlphabet(),
88  model_->getAlphabet());
89 
90  nbSites_ = data_->getNumberOfSites();
91  nbClasses_ = rateDistribution_->getNumberOfCategories();
92  nbStates_ = model_->getNumberOfStates();
93  if (verbose)
94  ApplicationTools::displayMessage("Double-Recursive Homogeneous Tree Likelihood");
95 
96  // Initialize root patterns:
97  SitePatterns pattern(data_);
98  shrunkData_ = pattern.getSites();
99  rootWeights_ = pattern.getWeights();
100  rootPatternLinks_ = pattern.getIndices();
101  nbDistinctSites_ = shrunkData_->getNumberOfSites();
102  if (verbose)
103  ApplicationTools::displayResult("Number of distinct sites", TextTools::toString(nbDistinctSites_));
104 
105  // Init _likelihoods:
106  if (verbose) ApplicationTools::displayTask("Init likelihoods arrays recursively");
107  // Clone data for more efficiency on sequences access:
108  const SiteContainer* sequences = new AlignedSequenceContainer(*shrunkData_);
109  initTreeLikelihoods(*sequences);
110  delete sequences;
111 
112  brLen_ = minimumBrLen_;
113  brLenConstraint_ = new IntervalConstraint(1, minimumBrLen_, true);
114 
115  if (verbose) ApplicationTools::displayTaskDone();
116 }
117 
118 /******************************************************************************/
119 
122  shrunkData_ (dynamic_cast<SiteContainer*>(lik.shrunkData_->clone())),
123  seqnames_ (lik.seqnames_),
124  model_ (lik.model_),
125  brLenParameters_ (lik.brLenParameters_),
126  pxy_ (lik.pxy_),
127  dpxy_ (lik.dpxy_),
128  d2pxy_ (lik.d2pxy_),
129  rootPatternLinks_ (lik.rootPatternLinks_),
130  rootWeights_ (lik.rootWeights_),
131  nbSites_ (lik.nbSites_),
132  nbClasses_ (lik.nbClasses_),
133  nbStates_ (lik.nbStates_),
134  nbDistinctSites_ (lik.nbDistinctSites_),
135  rootLikelihoods_ (lik.rootLikelihoods_),
136  rootLikelihoodsS_ (lik.rootLikelihoodsS_),
137  rootLikelihoodsSR_ (lik.rootLikelihoodsSR_),
138  dLikelihoods_ (lik.dLikelihoods_),
139  d2Likelihoods_ (lik.d2Likelihoods_),
140  leafLikelihoods1_ (lik.leafLikelihoods1_),
141  leafLikelihoods2_ (lik.leafLikelihoods2_),
142  minimumBrLen_ (lik.minimumBrLen_),
143  brLenConstraint_ (dynamic_cast<Constraint*>(lik.brLenConstraint_->clone())),
144  brLen_ (lik.brLen_)
145 {}
146 
147 /******************************************************************************/
148 
150 {
152  shrunkData_ = dynamic_cast<SiteContainer*>(lik.shrunkData_->clone());
153  seqnames_ = lik.seqnames_;
154  model_ = lik.model_;
156  pxy_ = lik.pxy_;
157  dpxy_ = lik.dpxy_;
158  d2pxy_ = lik.d2pxy_;
161  nbSites_ = lik.nbSites_;
162  nbClasses_ = lik.nbClasses_;
163  nbStates_ = lik.nbStates_;
173  brLenConstraint_ = dynamic_cast<Constraint*>(brLenConstraint_->clone());
174  brLen_ = lik.brLen_;
175  return *this;
176 }
177 
178 /******************************************************************************/
179 
181 {
182  delete shrunkData_;
184 }
185 
186 /******************************************************************************/
187 
188 void TwoTreeLikelihood::initialize() throw (Exception)
189 {
190  initParameters();
191  initialized_ = true;
192  fireParameterChanged(getParameters());
193 }
194 
195 /******************************************************************************/
196 
198 {
199  if (!initialized_) throw Exception("TwoTreeLikelihood::getBranchLengthsParameters(). Object is not initialized.");
200  return brLenParameters_.getCommonParametersWith(getParameters());
201 }
202 
203 /******************************************************************************/
204 
206 {
207  if (!initialized_) throw Exception("TwoTreeLikelihood::getSubstitutionModelParameters(). Object is not initialized.");
208  return model_->getParameters().getCommonParametersWith(getParameters());
209 }
210 
211 /******************************************************************************/
212 
214 {
215  double l = 1.;
216  for (size_t i = 0; i < nbDistinctSites_; i++)
217  {
218  l *= std::pow(rootLikelihoodsSR_[i], (int)rootWeights_[i]);
219  }
220  return l;
221 }
222 
223 /******************************************************************************/
224 
226 {
227  double ll = 0;
228  for (size_t i = 0; i < nbDistinctSites_; i++)
229  {
230  ll += rootWeights_[i] * log(rootLikelihoodsSR_[i]);
231  }
232  return ll;
233 }
234 
235 /******************************************************************************/
236 
238 {
240 }
241 
242 /******************************************************************************/
243 
245 {
246  return log(rootLikelihoodsSR_[rootPatternLinks_[site]]);
247 }
248 
249 /******************************************************************************/
250 
251 double TwoTreeLikelihood::getLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
252 {
253  return rootLikelihoodsS_[rootPatternLinks_[site]][rateClass];
254 }
255 
256 /******************************************************************************/
257 
258 double TwoTreeLikelihood::getLogLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
259 {
260  return log(rootLikelihoodsS_[rootPatternLinks_[site]][rateClass]);
261 }
262 
263 /******************************************************************************/
264 
265 double TwoTreeLikelihood::getLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const
266 {
267  return rootLikelihoods_[rootPatternLinks_[site]][rateClass][static_cast<size_t>(state)];
268 }
269 
270 /******************************************************************************/
271 
272 double TwoTreeLikelihood::getLogLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const
273 {
274  return log(rootLikelihoods_[rootPatternLinks_[site]][rateClass][static_cast<size_t>(state)]);
275 }
276 
277 /******************************************************************************/
278 
280 {
281  // Reset parameters:
282  resetParameters_();
283 
284  // Branch lengths:
286  addParameters_(brLenParameters_);
287 
288  // Substitution model:
289  addParameters_(model_->getIndependentParameters());
290 
291  // Rate distribution:
292  addParameters_(rateDistribution_->getIndependentParameters());
293 }
294 
295 /******************************************************************************/
296 
297 void TwoTreeLikelihood::applyParameters() throw (Exception)
298 {
299  // Apply branch length:
300  brLen_ = getParameterValue("BrLen");
301  // Apply substitution model parameters:
302  model_->matchParametersValues(getParameters());
303  // Apply rate distribution parameters:
304  rateDistribution_->matchParametersValues(getParameters());
305 }
306 
307 /******************************************************************************/
308 
310 {
311  if (brLen_ < minimumBrLen_)
312  {
313  ApplicationTools::displayWarning("Branch length is too small: " + TextTools::toString(brLen_) + ". Value is set to " + TextTools::toString(minimumBrLen_));
315  }
316  brLenParameters_.reset();
317  brLenParameters_.addParameter(Parameter("BrLen", brLen_, brLenConstraint_));
318 }
319 
320 /******************************************************************************/
321 
322 void TwoTreeLikelihood::setParameters(const ParameterList& parameters)
323 throw (ParameterNotFoundException, ConstraintException)
324 {
325  setParametersValues(parameters);
326 }
327 
328 /******************************************************************************/
329 
330 void TwoTreeLikelihood::fireParameterChanged(const ParameterList& params)
331 {
332  applyParameters();
333 
334  // For now we ignore the parameter that changed and we recompute all arrays...
335 
336  // Computes all pxy and pyx once for all:
337  pxy_.resize(nbClasses_);
338  for (size_t c = 0; c < nbClasses_; c++)
339  {
340  VVdouble* pxy_c = &pxy_[c];
341  pxy_c->resize(nbStates_);
342  RowMatrix<double> Q = model_->getPij_t(brLen_ * rateDistribution_->getCategory(c));
343  for (size_t x = 0; x < nbStates_; x++)
344  {
345  Vdouble* pxy_c_x = &(*pxy_c)[x];
346  pxy_c_x->resize(nbStates_);
347  for (size_t y = 0; y < nbStates_; y++)
348  {
349  (*pxy_c_x)[y] = Q(x, y);
350  }
351  }
352  }
353 
355  {
356  // Computes all dpxy/dt once for all:
357  dpxy_.resize(nbClasses_);
358  for (size_t c = 0; c < nbClasses_; c++)
359  {
360  VVdouble* dpxy_c = &dpxy_[c];
361  dpxy_c->resize(nbStates_);
362  double rc = rateDistribution_->getCategory(c);
363  RowMatrix<double> dQ = model_->getdPij_dt(brLen_ * rc);
364  for (size_t x = 0; x < nbStates_; x++)
365  {
366  Vdouble* dpxy_c_x = &(*dpxy_c)[x];
367  dpxy_c_x->resize(nbStates_);
368  for (size_t y = 0; y < nbStates_; y++)
369  {
370  (*dpxy_c_x)[y] = rc * dQ(x, y);
371  }
372  }
373  }
374  }
375 
377  {
378  // Computes all d2pxy/dt2 once for all:
379  d2pxy_.resize(nbClasses_);
380  for (size_t c = 0; c < nbClasses_; c++)
381  {
382  VVdouble* d2pxy_c = &d2pxy_[c];
383  d2pxy_c->resize(nbStates_);
384  double rc = rateDistribution_->getCategory(c);
385  RowMatrix<double> d2Q = model_->getd2Pij_dt2(brLen_ * rc);
386  for (size_t x = 0; x < nbStates_; x++)
387  {
388  Vdouble* d2pxy_c_x = &(*d2pxy_c)[x];
389  d2pxy_c_x->resize(nbStates_);
390  for (size_t y = 0; y < nbStates_; y++)
391  {
392  (*d2pxy_c_x)[y] = rc * rc * d2Q(x, y);
393  }
394  }
395  }
396  }
397 
400  {
402  }
404  {
406  }
407 }
408 
409 /******************************************************************************/
410 
412 throw (Exception)
413 {
414  return -getLogLikelihood();
415 }
416 
417 /******************************************************************************/
418 
419 void TwoTreeLikelihood::initTreeLikelihoods(const SequenceContainer& sequences) throw (Exception)
420 {
421  const Sequence* seq1 = &sequences.getSequence(seqnames_[0]);
422  const Sequence* seq2 = &sequences.getSequence(seqnames_[1]);
423  leafLikelihoods1_.resize(nbDistinctSites_);
424  leafLikelihoods2_.resize(nbDistinctSites_);
425  for (size_t i = 0; i < nbDistinctSites_; i++)
426  {
427  Vdouble* leafLikelihoods1_i = &leafLikelihoods1_[i];
428  Vdouble* leafLikelihoods2_i = &leafLikelihoods2_[i];
429  leafLikelihoods1_i->resize(nbStates_);
430  leafLikelihoods2_i->resize(nbStates_);
431  int state1 = seq1->getValue(i);
432  int state2 = seq2->getValue(i);
433  for (size_t s = 0; s < nbStates_; s++)
434  {
435  // Leaves likelihood are set to 1 if the char correspond to the site in the sequence,
436  // otherwise value set to 0:
437  try
438  {
439  (*leafLikelihoods1_i)[s] = model_->getInitValue(s, state1);
440  (*leafLikelihoods2_i)[s] = model_->getInitValue(s, state2);
441  }
442  catch (SequenceNotFoundException& snfe)
443  {
444  throw SequenceNotFoundException("TwoTreeLikelihood::initTreelikelihoods. Leaf name in tree not found in site conainer: ", snfe.getSequenceId());
445  }
446  }
447  }
448 
449  // Initialize likelihood vector:
450  rootLikelihoods_.resize(nbDistinctSites_);
451  rootLikelihoodsS_.resize(nbDistinctSites_);
452  rootLikelihoodsSR_.resize(nbDistinctSites_);
453  for (size_t i = 0; i < nbDistinctSites_; i++)
454  {
455  VVdouble* rootLikelihoods_i = &rootLikelihoods_[i];
456  Vdouble* rootLikelihoodsS_i = &rootLikelihoodsS_[i];
457  rootLikelihoods_i->resize(nbClasses_);
458  rootLikelihoodsS_i->resize(nbClasses_);
459  for (size_t c = 0; c < nbClasses_; c++)
460  {
461  Vdouble* rootLikelihoods_i_c = &(*rootLikelihoods_i)[c];
462  rootLikelihoods_i_c->resize(nbStates_);
463  for (size_t s = 0; s < nbStates_; s++)
464  {
465  (*rootLikelihoods_i_c)[s] = 1.; // All likelihoods are initialized to 1.
466  }
467  }
468  }
469 
470  // Initialize d and d2 likelihoods:
471  dLikelihoods_.resize(nbDistinctSites_);
472  d2Likelihoods_.resize(nbDistinctSites_);
473 }
474 
475 /******************************************************************************/
476 
478 {
479  for (size_t i = 0; i < nbDistinctSites_; i++)
480  {
481  VVdouble* rootLikelihoods_i = &rootLikelihoods_[i];
482  Vdouble* leafLikelihoods1_i = &leafLikelihoods1_[i];
483  Vdouble* leafLikelihoods2_i = &leafLikelihoods2_[i];
484  for (size_t c = 0; c < nbClasses_; c++)
485  {
486  Vdouble* rootLikelihoods_i_c = &(*rootLikelihoods_i)[c];
487  VVdouble* pxy_c = &pxy_[c];
488  for (size_t x = 0; x < nbStates_; x++)
489  {
490  Vdouble* pxy_c_x = &(*pxy_c)[x];
491  double l = 0;
492  double l1 = (*leafLikelihoods1_i)[x];
493  for (size_t y = 0; y < nbStates_; y++)
494  {
495  double l2 = (*leafLikelihoods2_i)[y];
496  l += l1 * l2 * (*pxy_c_x)[y];
497  }
498  (*rootLikelihoods_i_c)[x] = l;
499  }
500  }
501  }
502 
503  Vdouble fr = model_->getFrequencies();
504  Vdouble p = rateDistribution_->getProbabilities();
505  for (size_t i = 0; i < nbDistinctSites_; i++)
506  {
507  // For each site in the sequence,
508  VVdouble* rootLikelihoods_i = &rootLikelihoods_[i];
509  Vdouble* rootLikelihoodsS_i = &rootLikelihoodsS_[i];
510  rootLikelihoodsSR_[i] = 0;
511  for (size_t c = 0; c < nbClasses_; c++)
512  {
513  (*rootLikelihoodsS_i)[c] = 0;
514  // For each rate classe,
515  Vdouble* rootLikelihoods_i_c = &(*rootLikelihoods_i)[c];
516  for (size_t x = 0; x < nbStates_; x++)
517  {
518  // For each initial state,
519  (*rootLikelihoodsS_i)[c] += fr[x] * (*rootLikelihoods_i_c)[x];
520  }
521  rootLikelihoodsSR_[i] += p[c] * (*rootLikelihoodsS_i)[c];
522  }
523  }
524 }
525 
526 /******************************************************************************/
527 
529 {
530  for (size_t i = 0; i < nbDistinctSites_; i++)
531  {
532  Vdouble* leafLikelihoods1_i = &leafLikelihoods1_[i];
533  Vdouble* leafLikelihoods2_i = &leafLikelihoods2_[i];
534  double dli = 0;
535  for (size_t c = 0; c < nbClasses_; c++)
536  {
537  VVdouble* dpxy_c = &dpxy_[c];
538  double dlic = 0;
539  for (size_t x = 0; x < nbStates_; x++)
540  {
541  Vdouble* dpxy_c_x = &(*dpxy_c)[x];
542  double l1 = (*leafLikelihoods1_i)[x];
543  double dlicx = 0;
544  for (size_t y = 0; y < nbStates_; y++)
545  {
546  double l2 = (*leafLikelihoods2_i)[y];
547  dlicx += l1 * l2 * (*dpxy_c_x)[y];
548  }
549  dlic += dlicx * model_->freq(x);
550  }
551  dli += dlic * rateDistribution_->getProbability(c);
552  }
553  dLikelihoods_[i] = dli / rootLikelihoodsSR_[i];
554  }
555 }
556 
557 /******************************************************************************/
558 
560 {
561  for (size_t i = 0; i < nbDistinctSites_; i++)
562  {
563  Vdouble* leafLikelihoods1_i = &leafLikelihoods1_[i];
564  Vdouble* leafLikelihoods2_i = &leafLikelihoods2_[i];
565  double d2li = 0;
566  for (size_t c = 0; c < nbClasses_; c++)
567  {
568  VVdouble* d2pxy_c = &d2pxy_[c];
569  double d2lic = 0;
570  for (size_t x = 0; x < nbStates_; x++)
571  {
572  Vdouble* d2pxy_c_x = &(*d2pxy_c)[x];
573  double l1 = (*leafLikelihoods1_i)[x];
574  double d2licx = 0;
575  for (size_t y = 0; y < nbStates_; y++)
576  {
577  double l2 = (*leafLikelihoods2_i)[y];
578  d2licx += l1 * l2 * (*d2pxy_c_x)[y];
579  }
580  d2lic += d2licx * model_->freq(x);
581  }
582  d2li += d2lic * rateDistribution_->getProbability(c);
583  }
584  d2Likelihoods_[i] = d2li / rootLikelihoodsSR_[i];
585  }
586 }
587 
588 /******************************************************************************/
589 
590 double TwoTreeLikelihood::getFirstOrderDerivative(const string& variable) const
591 throw (Exception)
592 {
593  if (!hasParameter(variable))
594  throw ParameterNotFoundException("TwoTreeLikelihood::getFirstOrderDerivative().", variable);
595  if (getRateDistributionParameters().hasParameter(variable))
596  {
597  cout << "DEBUB: WARNING!!! Derivatives respective to rate distribution parameter are not implemented." << endl;
598  return log(-1.);
599  }
600  if (getSubstitutionModelParameters().hasParameter(variable))
601  {
602  cout << "DEBUB: WARNING!!! Derivatives respective to substitution model parameters are not implemented." << endl;
603  return log(-1.);
604  }
605 
606  //
607  // Computation for branch lengths:
608  //
609 
610  // Get the node with the branch whose length must be derivated:
611  double d = 0;
612  for (size_t i = 0; i < nbDistinctSites_; i++)
613  {
614  d += rootWeights_[i] * dLikelihoods_[i];
615  }
616  return -d;
617 }
618 
619 /******************************************************************************/
620 
621 double TwoTreeLikelihood::getSecondOrderDerivative(const string& variable) const
622 throw (Exception)
623 {
624  if (!hasParameter(variable))
625  throw ParameterNotFoundException("TwoTreeLikelihood::getSecondOrderDerivative().", variable);
626  if (getRateDistributionParameters().hasParameter(variable))
627  {
628  cout << "DEBUB: WARNING!!! Derivatives respective to rate distribution parameter are not implemented." << endl;
629  return log(-1.);
630  }
631  if (getSubstitutionModelParameters().hasParameter(variable))
632  {
633  cout << "DEBUB: WARNING!!! Derivatives respective to substitution model parameters are not implemented." << endl;
634  return log(-1.);
635  }
636 
637  //
638  // Computation for branch lengths:
639  //
640 
641  // Get the node with the branch whose length must be derivated:
642  double d2 = 0;
643  for (size_t i = 0; i < nbDistinctSites_; i++)
644  {
645  d2 += rootWeights_[i] * (d2Likelihoods_[i] - pow(dLikelihoods_[i], 2));
646  }
647  return -d2;
648 }
649 
650 /******************************************************************************/
651 
652 void DistanceEstimation::computeMatrix() throw (NullPointerException)
653 {
654  size_t n = sites_->getNumberOfSequences();
655  vector<string> names = sites_->getSequencesNames();
656  if (dist_ != 0) delete dist_;
657  dist_ = new DistanceMatrix(names);
658  optimizer_->setVerbose(static_cast<unsigned int>(max(static_cast<int>(verbose_) - 2, 0)));
659  for (size_t i = 0; i < n; ++i)
660  {
661  (*dist_)(i, i) = 0;
662  if (verbose_ == 1)
663  {
664  ApplicationTools::displayGauge(i, n - 1, '=');
665  }
666  for (size_t j = i + 1; j < n; j++)
667  {
668  if (verbose_ > 1)
669  {
670  ApplicationTools::displayGauge(j - i - 1, n - i - 2, '=');
671  }
672  TwoTreeLikelihood* lik =
673  new TwoTreeLikelihood(names[i], names[j], *sites_, model_.get(), rateDist_.get(), verbose_ > 3);
674  lik->initialize();
675  lik->enableDerivatives(true);
676  size_t d = SymbolListTools::getNumberOfDistinctPositions(sites_->getSequence(i), sites_->getSequence(j));
677  size_t g = SymbolListTools::getNumberOfPositionsWithoutGap(sites_->getSequence(i), sites_->getSequence(j));
678  lik->setParameterValue("BrLen", g == 0 ? lik->getMinimumBranchLength() : std::max(lik->getMinimumBranchLength(), static_cast<double>(d) / static_cast<double>(g)));
679  // Optimization:
680  optimizer_->setFunction(lik);
681  optimizer_->setConstraintPolicy(AutoParameter::CONSTRAINTS_AUTO);
682  ParameterList params = lik->getBranchLengthsParameters();
683  params.addParameters(parameters_);
684  optimizer_->init(params);
685  optimizer_->optimize();
686  // Store results:
687  (*dist_)(i, j) = (*dist_)(j, i) = lik->getParameterValue("BrLen");
688  delete lik;
689  }
690  if (verbose_ > 1 && ApplicationTools::message) ApplicationTools::message->endLine();
691  }
692 }
693 
694 /******************************************************************************/
695 
Interface for all substitution models.
auto_ptr< DiscreteDistribution > rateDist_
const std::vector< size_t > & getIndices() const
Definition: SitePatterns.h:175
Partial implementation of the DiscreteRatesAcrossSitesTreeLikelihood interface.
TwoTreeLikelihood(const std::string &seq1, const std::string &seq2, const SiteContainer &data, SubstitutionModel *model, DiscreteDistribution *rDist, bool verbose)
ParameterList getBranchLengthsParameters() const
Get the branch lengths parameters.
This class is a simplified version of DRHomogeneousTreeLikelihood for 2-Trees.
void setParameters(const ParameterList &parameters)
Implements the Function interface.
double getLikelihoodForASite(size_t site) const
Get the likelihood for a site.
virtual void initParameters()
This builds the parameters list from all parametrizable objects, i.e. substitution model...
TwoTreeLikelihood & operator=(const TwoTreeLikelihood &lik)
SubstitutionModel * model_
const SiteContainer * sites_
void enableDerivatives(bool yn)
Tell if derivatives must be computed.
STL namespace.
virtual void initBranchLengthsParameters()
const std::vector< unsigned int > & getWeights() const
Definition: SitePatterns.h:171
virtual double getMinimumBranchLength() const
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 const Vdouble & getFrequencies() const =0
virtual const Matrix< double > & getd2Pij_dt2(double t) const =0
std::vector< unsigned int > rootWeights_
The frequency of each site.
virtual const Matrix< double > & getPij_t(double t) const =0
double getLogLikelihood() const
Get the logarithm of the likelihood for the whole dataset.
void initialize()
Init the likelihood object.
static SiteContainer * getSequenceSubset(const SiteContainer &sequenceSet, const Node &node)
Extract the sequences corresponding to a given subtree.
virtual void initTreeLikelihoods(const SequenceContainer &sequences)
This method initializes the leaves according to a sequence container.
virtual void computeTreeDLikelihood()
void computeMatrix()
Perform the distance computation.
std::vector< std::string > seqnames_
std::vector< size_t > rootPatternLinks_
As previous, but for the global container.
virtual void computeTreeLikelihood()
virtual void computeTreeD2Likelihood()
AbstractDiscreteRatesAcrossSitesTreeLikelihood & operator=(const AbstractDiscreteRatesAcrossSitesTreeLikelihood &tl)
SiteContainer * getSites() const
double getLogLikelihoodForASite(size_t site) const
Get the logarithm of the likelihood for a site.
double getSecondOrderDerivative(const std::string &variable) const
double getFirstOrderDerivative(const std::string &variable) const
double getLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
Get the likelihood for a site knowing its rate class.
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.
SiteContainer * shrunkData_
virtual double freq(size_t i) const =0
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...
virtual const Matrix< double > & getdPij_dt(double t) const =0
Data structure for site patterns.
Definition: SitePatterns.h:69
double getLikelihood() const
Get the likelihood for the whole dataset.
ParameterList brLenParameters_
virtual void applyParameters()
All parameters are stores in a parameter list.
ParameterList getSubstitutionModelParameters() const
Get the parameters associated to substitution model(s).
auto_ptr< SubstitutionModel > model_