bpp-phyl  2.2.0
NNIHomogeneousTreeLikelihood.h
Go to the documentation of this file.
1 //
2 // File: NNIHomogeneousTreeLikelihood.h
3 // Created by: Julien Dutheil
4 // Created on: Fri Apr 06 14:16 2007
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 
40 #ifndef _NNIHOMOGENEOUSTREELIKELIHOOD_H_
41 #define _NNIHOMOGENEOUSTREELIKELIHOOD_H_
42 
44 #include "../NNISearchable.h"
45 
46 #include <Bpp/Numeric/VectorTools.h>
47 #include <Bpp/Numeric/Parametrizable.h>
48 #include <Bpp/Numeric/Prob/DiscreteDistribution.h>
49 #include <Bpp/Numeric/Function/BrentOneDimension.h>
50 
51 namespace bpp
52 {
63  public Function,
64  public AbstractParametrizable
65 {
66 protected:
67  const VVVdouble* array1_, * array2_;
69  const DiscreteDistribution* rDist_;
71  VVVdouble pxy_;
72  double lnL_;
73  std::vector<unsigned int> weights_;
74 
75 public:
76  BranchLikelihood(const std::vector<unsigned int>& weights) :
77  AbstractParametrizable(""),
78  array1_(0),
79  array2_(0),
80  model_(0),
81  rDist_(0),
82  nbStates_(0),
83  nbClasses_(0),
84  pxy_(),
85  lnL_(log(0.)),
86  weights_(weights)
87  {
88  addParameter_(new Parameter("BrLen", 1, 0));
89  }
90 
92  AbstractParametrizable(bl),
93  array1_(bl.array1_),
94  array2_(bl.array2_),
95  model_(bl.model_),
96  rDist_(bl.rDist_),
97  nbStates_(bl.nbStates_),
99  pxy_(bl.pxy_),
100  lnL_(bl.lnL_),
101  weights_(bl.weights_)
102  {}
103 
105  {
106  AbstractParametrizable::operator=(bl);
107  array1_ = bl.array1_;
108  array2_ = bl.array2_;
109  model_ = bl.model_;
110  rDist_ = bl.rDist_;
111  nbStates_ = bl.nbStates_;
112  nbClasses_ = bl.nbClasses_;
113  pxy_ = bl.pxy_;
114  lnL_ = bl.lnL_;
115  weights_ = bl.weights_;
116  return *this;
117  }
118 
119  virtual ~BranchLikelihood() {}
120 
121  BranchLikelihood* clone() const { return new BranchLikelihood(*this); }
122 
123 public:
124  void initModel(const SubstitutionModel* model, const DiscreteDistribution* rDist);
125 
130  void initLikelihoods(const VVVdouble* array1, const VVVdouble* array2)
131  {
132  array1_ = array1;
133  array2_ = array2;
134  }
135 
137  {
138  array1_ = 0;
139  array2_ = 0;
140  }
141 
142  void setParameters(const ParameterList& parameters)
143  throw (ParameterNotFoundException, ConstraintException)
144  {
145  setParametersValues(parameters);
146  }
147 
148  double getValue() const throw (Exception) { return lnL_; }
149 
150  void fireParameterChanged(const ParameterList& parameters)
151  {
154  }
155 
156 protected:
158  void computeLogLikelihood();
159 };
160 
161 
167  public virtual NNISearchable
168 {
169 protected:
174  BrentOneDimension* brentOptimizer_;
175 
179  mutable std::map<int, double> brLenNNIValues_;
180 
181  ParameterList brLenNNIParams_;
182 
183 public:
196  const Tree& tree,
197  SubstitutionModel* model,
198  DiscreteDistribution* rDist,
199  bool checkRooted = true,
200  bool verbose = true)
201  throw (Exception);
202 
216  const Tree& tree,
217  const SiteContainer& data,
218  SubstitutionModel* model,
219  DiscreteDistribution* rDist,
220  bool checkRooted = true,
221  bool verbose = true)
222  throw (Exception);
223 
228 
230 
231  virtual ~NNIHomogeneousTreeLikelihood();
232 
233 #ifndef NO_VIRTUAL_COV
235 #else
236  Clonable*
237 #endif
238  clone() const { return new NNIHomogeneousTreeLikelihood(*this); }
239 
240 public:
241  void setData(const SiteContainer& sites) throw (Exception)
242  {
244  if (brLikFunction_) delete brLikFunction_;
245  brLikFunction_ = new BranchLikelihood(getLikelihoodData()->getWeights());
246  }
247 
259  const Tree& getTopology() const { return getTree(); }
260 
261  double getTopologyValue() const throw (Exception) { return getValue(); }
262 
263  double testNNI(int nodeId) const throw (NodeException);
264 
265  void doNNI(int nodeId) throw (NodeException);
266 
268  {
270  // if(brLenNNIParams_.size() > 0)
272  brLenNNIParams_.reset();
273  }
274 
276  {
277  brLenNNIValues_.clear();
278  }
280 };
281 } // end of namespace bpp.
282 
283 #endif // _NNIHOMOGENEOUSTREELIKELIHOOD_H_
284 
void topologyChangeTested(const TopologyChangeEvent &event)
Notify a topology change event.
Interface for all substitution models.
BranchLikelihood(const BranchLikelihood &bl)
void fireParameterChanged(const ParameterList &parameters)
BranchLikelihood & operator=(const BranchLikelihood &bl)
Interface for Nearest Neighbor Interchanges algorithms.
Definition: NNISearchable.h:96
const Tree & getTopology() const
Get the tree associated to this NNISearchable object.
void topologyChangeSuccessful(const TopologyChangeEvent &event)
Tell that a topology change is definitive.
void initModel(const SubstitutionModel *model, const DiscreteDistribution *rDist)
This class adds support for NNI topology estimation to the DRHomogeneousTreeLikelihood class...
Interface for phylogenetic tree objects.
Definition: Tree.h:148
DRASDRTreeLikelihoodData * getLikelihoodData()
BrentOneDimension * brentOptimizer_
Optimizer used for testing NNI.
void setData(const SiteContainer &sites)
Set the dataset for which the likelihood must be evaluated.
BranchLikelihood * clone() const
double getValue() const
Function and NNISearchable interface.
NNIHomogeneousTreeLikelihood(const Tree &tree, SubstitutionModel *model, DiscreteDistribution *rDist, bool checkRooted=true, bool verbose=true)
Build a new NNIHomogeneousTreeLikelihood object.
void initLikelihoods(const VVVdouble *array1, const VVVdouble *array2)
void reInit()
Rebuild likelihood arrays at inner nodes.
void setData(const SiteContainer &sites)
Set the dataset for which the likelihood must be evaluated.
std::map< int, double > brLenNNIValues_
Hash used for backing up branch lengths when testing NNIs.
std::vector< unsigned int > weights_
virtual void fireParameterChanged(const ParameterList &params)
This class implements the likelihood computation for a tree using the double-recursive algorithm...
BranchLikelihood(const std::vector< unsigned int > &weights)
Class for notifying new toplogy change events.
const Tree & getTree() const
Get the tree (topology and branch lengths).
const SubstitutionModel * model_
Compute likelihood for a 4-tree.
NNIHomogeneousTreeLikelihood * clone() const
double getTopologyValue() const
Get the current score of this NNISearchable object.
void doNNI(int nodeId)
Perform a NNI movement.
const DiscreteDistribution * rDist_
void setParameters(const ParameterList &parameters)
General exception thrown when something is wrong with a particular node.
double testNNI(int nodeId) const
Send the score of a NNI movement, without performing it.