bpp-phyl  2.2.0
RHomogeneousClockTreeLikelihood.cpp
Go to the documentation of this file.
1 //
2 // File: RHomogeneousClockTreeLikelihood.cpp
3 // Created by: Benoît Nabholz
4 // Julien Dutheil
5 // Created on: Fri Apr 06 14:11 2007
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 
42 #include "../TreeTemplateTools.h"
43 
44 #include <iostream>
45 
46 using namespace std;
47 
48 #include <Bpp/App/ApplicationTools.h>
49 
50 using namespace bpp;
51 
52 /******************************************************************************/
53 
54 RHomogeneousClockTreeLikelihood::RHomogeneousClockTreeLikelihood(
55  const Tree & tree,
56  SubstitutionModel * model,
57  DiscreteDistribution * rDist,
58  bool checkRooted,
59  bool verbose)
60 throw (Exception):
61  RHomogeneousTreeLikelihood(tree, model, rDist, false, verbose, true)
62 {
63  init_();
64 }
65 
66 /******************************************************************************/
67 
68 RHomogeneousClockTreeLikelihood::RHomogeneousClockTreeLikelihood(
69  const Tree & tree,
70  const SiteContainer & data,
71  SubstitutionModel * model,
72  DiscreteDistribution * rDist,
73  bool checkRooted,
74  bool verbose)
75 throw (Exception):
76  RHomogeneousTreeLikelihood(tree, data, model, rDist, false, verbose, true)
77 {
78  init_();
79 }
80 
81 /******************************************************************************/
82 
83 void RHomogeneousClockTreeLikelihood::init_()
84 {
85  //Check if the tree is rooted:
86  if (!tree_->isRooted()) throw Exception("RHomogeneousClockTreeLikelihood::init_(). Tree is unrooted!");
87  if (TreeTemplateTools::isMultifurcating(*tree_->getRootNode())) throw Exception("HomogeneousClockTreeLikelihood::init_(). Tree is multifurcating.");
88  setMinimumBranchLength(0.);
89 }
90 
91 /******************************************************************************/
92 
93 void RHomogeneousClockTreeLikelihood::applyParameters() throw (Exception)
94 {
95  if (!initialized_) throw Exception("RHomogeneousClockTreeLikelihood::applyParameters(). Object not initialized.");
96  //Apply branch lengths:
97  brLenParameters_.matchParametersValues(getParameters());
98  computeBranchLengthsFromHeights(tree_->getRootNode(), brLenParameters_.getParameter("TotalHeight").getValue());
99 
100  //Apply substitution model parameters:
101  model_->matchParametersValues(getParameters());
102  //Apply rate distribution parameters:
103  rateDistribution_->matchParametersValues(getParameters());
104 }
105 
106 /******************************************************************************/
107 
108 void RHomogeneousClockTreeLikelihood::fireParameterChanged(const ParameterList& params)
109 {
110  applyParameters();
111 
112  computeAllTransitionProbabilities();
113 
114  computeTreeLikelihood();
115 
116  minusLogLik_ = - getLogLikelihood();
117 }
118 
119 /******************************************************************************/
120 
121 void RHomogeneousClockTreeLikelihood::initBranchLengthsParameters()
122 {
123  //Check branch lengths first:
124  for(unsigned int i = 0; i < nbNodes_; i++)
125  {
126  double d = minimumBrLen_;
127  if(!nodes_[i]->hasDistanceToFather())
128  {
129  ApplicationTools::displayWarning("Missing branch length " + TextTools::toString(i) + ". Value is set to " + TextTools::toString(minimumBrLen_));
130  nodes_[i]->setDistanceToFather(minimumBrLen_);
131  }
132  else
133  {
134  d = nodes_[i]->getDistanceToFather();
135  if (d < minimumBrLen_)
136  {
137  ApplicationTools::displayWarning("Branch length " + TextTools::toString(i) + " is too small: " + TextTools::toString(d) + ". Value is set to " + TextTools::toString(minimumBrLen_));
138  nodes_[i]->setDistanceToFather(minimumBrLen_);
139  }
140  }
141  }
142 
143  brLenParameters_.reset();
144 
145  map<const Node*, double> heights;
146  TreeTemplateTools::getHeights(*tree_->getRootNode(), heights);
147  double totalHeight = heights[tree_->getRootNode()];
148  brLenParameters_.addParameter(Parameter("TotalHeight", totalHeight, brLenConstraint_->clone(), true));
149  for (map<const Node *, double>::iterator it = heights.begin(); it != heights.end(); it++)
150  {
151  if (!it->first->isLeaf() && it->first->hasFather())
152  {
153  double fatherHeight = heights[it->first->getFather()];
154  brLenParameters_.addParameter(Parameter("HeightP" + TextTools::toString(it->first->getId()), it->second / fatherHeight, &Parameter::PROP_CONSTRAINT_IN));
155  }
156  }
157 }
158 
159 /******************************************************************************/
160 
161 void RHomogeneousClockTreeLikelihood::computeBranchLengthsFromHeights(Node* node, double height) throw (Exception)
162 {
163  for (unsigned int i = 0; i < node->getNumberOfSons(); i++)
164  {
165  Node* son = node->getSon(i);
166  if (son->isLeaf())
167  {
168  son->setDistanceToFather(std::max(minimumBrLen_, height));
169  }
170  else
171  {
172  Parameter* p = &brLenParameters_.getParameter(string("HeightP") + TextTools::toString(son->getId()));
173  double sonHeightP = p->getValue();
174  double sonHeight = sonHeightP * height;
175  son->setDistanceToFather(std::max(minimumBrLen_, height - sonHeight));
176  computeBranchLengthsFromHeights(son, sonHeight);
177  }
178  }
179 }
180 
181 /******************************************************************************/
182 
183 ParameterList RHomogeneousClockTreeLikelihood::getDerivableParameters() const throw (Exception)
184 {
185  if (!initialized_) throw Exception("RHomogeneousClockTreeLikelihood::getDerivableParameters(). Object is not initialized.");
186  return ParameterList();
187 }
188 
189 /******************************************************************************/
190 
191 ParameterList RHomogeneousClockTreeLikelihood::getNonDerivableParameters() const throw (Exception)
192 {
193  if (!initialized_) throw Exception("RHomogeneousClockTreeLikelihood::getNonDerivableParameters(). Object is not initialized.");
194  return getParameters();
195 }
196 
197 /******************************************************************************
198  * First Order Derivatives *
199  ******************************************************************************/
200 
201 double RHomogeneousClockTreeLikelihood::getFirstOrderDerivative(const std::string& variable) const
202 throw (Exception)
203 {
204  throw Exception("No first order derivative is implemented for this function.");
205 }
206 
207 /******************************************************************************
208  * Second Order Derivatives *
209  ******************************************************************************/
210 
211 double RHomogeneousClockTreeLikelihood::getSecondOrderDerivative(const std::string& variable) const
212 throw (Exception)
213 {
214  throw Exception("No second order derivative is implemented for this function.");
215 }
216 
217 /******************************************************************************/
218 
Interface for all substitution models.
virtual const Node * getSon(size_t pos) const
Definition: Node.h:395
STL namespace.
Interface for phylogenetic tree objects.
Definition: Tree.h:148
This class implement the &#39;traditional&#39; way of computing likelihood for a tree.
virtual bool isLeaf() const
Definition: Node.h:692
virtual int getId() const
Get the node&#39;s id.
Definition: Node.h:203
The phylogenetic node class.
Definition: Node.h:90
virtual void setDistanceToFather(double distance)
Set or update the distance toward the father node.
Definition: Node.h:299