bpp-phyl  2.2.0
AbstractHomogeneousTreeLikelihood.h
Go to the documentation of this file.
1 //
2 // File: AbstractHomogeneousTreeLikelihood.h
3 // Created by: Julien Dutheil
4 // Created on: Thr Dec 23 12:03 2004
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 _ABSTRACTHOMOGENEOUSTREELIKELIHOOD_H_
41 #define _ABSTRACTHOMOGENEOUSTREELIKELIHOOD_H_
42 
45 
46 //From STL:
47 #include<memory>
48 
49 namespace bpp
50 {
51 
58  public virtual HomogeneousTreeLikelihood,
60 {
61  public:
62 
64  public ConstSiteModelIterator
65  {
66  private:
68  size_t index_;
69 
70  public:
72  siteModelDescription_(model, tree.getBranchesId()), index_(0) {}
73 
74  public:
75  ConstSiteModelDescription* next() throw (Exception)
76  {
77  if (!hasNext())
78  throw Exception("AbstractHomogeneousTreeLikelihood::ConstHomogeneousSiteModelIterator::next(). No more site in the set.");
79  index_++;
80  return &siteModelDescription_;
81  }
82 
83  bool hasNext() const { return index_ == 0; }
84  };
85 
86 
87  protected:
89  ParameterList brLenParameters_;
90 
91  mutable std::map<int, VVVdouble> pxy_;
92 
93  mutable std::map<int, VVVdouble> dpxy_;
94 
95  mutable std::map<int, VVVdouble> d2pxy_;
96 
97  std::vector<double> rootFreqs_;
98 
105  std::vector<Node*> nodes_;
106 
107  //some values we'll need:
108  size_t nbSites_, //the number of sites in the container
109  nbDistinctSites_, //the number of distinct sites
110  nbClasses_, //the number of rate classes
111  nbStates_, //the number of states in the alphabet
112  nbNodes_; //the number of nodes in the tree
113 
114  bool verbose_;
115 
118  std::auto_ptr<Constraint> brLenConstraint_;
119 
120  public:
122  const Tree& tree,
123  SubstitutionModel* model,
124  DiscreteDistribution* rDist,
125  bool checkRooted = true,
126  bool verbose = true)
127  throw (Exception);
128 
135 
142 
144 
145  private:
146 
150  void init_(const Tree& tree,
151  SubstitutionModel* model,
152  DiscreteDistribution* rDist,
153  bool checkRooted,
154  bool verbose) throw (Exception);
155 
156  public:
157 
165  size_t getNumberOfStates() const { return model_->getNumberOfStates(); }
166 
167  const std::vector<int>& getAlphabetStates() const { return model_->getAlphabetStates(); }
168 
169  int getAlphabetStateAsInt(size_t i) const { return model_->getAlphabetStateAsInt(i); }
170 
171  std::string getAlphabetStateAsChar(size_t i) const { return model_->getAlphabetStateAsChar(i); }
172 
173  void initialize() throw(Exception);
174 
175  ParameterList getBranchLengthsParameters() const;
176 
177  ParameterList getSubstitutionModelParameters() const;
178 
179  ParameterList getRateDistributionParameters() const
180  {
182  }
183 
184  const std::vector<double>& getRootFrequencies(size_t siteIndex) const { return model_->getFrequencies(); }
185 
186  VVVdouble getTransitionProbabilitiesPerRateClass(int nodeId, size_t siteIndex) const { return pxy_[nodeId]; }
187 
189  {
191  }
192 
194  {
196  }
197 
207  const SubstitutionModel* getSubstitutionModel() const { return model_; }
208  const SubstitutionModel* getSubstitutionModel(int nodeId, size_t siteIndex) const throw (NodeNotFoundException) { return model_; }
209 
211  SubstitutionModel* getSubstitutionModel(int nodeId, size_t siteIndex) throw (NodeNotFoundException) { return model_; }
212 
213  void setSubstitutionModel(SubstitutionModel* model) throw (Exception);
216  public: //Specific methods:
217 
222  virtual void initParameters();
223 
229  virtual void applyParameters() throw (Exception);
230 
231  virtual void initBranchLengthsParameters();
232 
233  virtual void setMinimumBranchLength(double minimum) throw (Exception)
234  {
235  if (minimum > maximumBrLen_)
236  throw Exception("AbstractHomogeneousTreeLikelihood::setMinimumBranchLength. Minimum branch length sould be lower than the maximum one: " + TextTools::toString(maximumBrLen_));
237  minimumBrLen_ = minimum;
238  if (brLenConstraint_.get()) brLenConstraint_.release();
239  brLenConstraint_.reset(new IntervalConstraint(minimumBrLen_, maximumBrLen_, true, true));
241  }
242 
243  virtual void setMaximumBranchLength(double maximum) throw (Exception)
244  {
245  if (maximum < minimumBrLen_)
246  throw Exception("AbstractHomogeneousTreeLikelihood::setMaximumBranchLength. Maximum branch length sould be higher than the minimum one: " + TextTools::toString(minimumBrLen_));
247  maximumBrLen_ = maximum;
248  if (brLenConstraint_.get()) brLenConstraint_.release();
249  brLenConstraint_.reset(new IntervalConstraint(minimumBrLen_, maximumBrLen_, true, true));
251  }
252 
253  virtual double getMinimumBranchLength() const { return minimumBrLen_; }
254  virtual double getMaximumBranchLength() const { return maximumBrLen_; }
255 
256  protected:
260  virtual void computeAllTransitionProbabilities();
264  virtual void computeTransitionProbabilitiesForNode(const Node * node);
265 
266 };
267 
268 } //end of namespace bpp.
269 
270 #endif //_ABSTRACTHOMOGENEOUSTREELIKELIHOOD_H_
271 
Interface for all substitution models.
virtual void computeAllTransitionProbabilities()
Fill the pxy_, dpxy_ and d2pxy_ arrays for all nodes.
const std::vector< int > & getAlphabetStates() const
virtual int getAlphabetStateAsInt(size_t index) const =0
SubstitutionModel * getSubstitutionModel(int nodeId, size_t siteIndex)
Get the substitution model associated to a given node and alignment column.
VVVdouble getTransitionProbabilitiesPerRateClass(int nodeId, size_t siteIndex) const
Retrieves all Pij(t) for a particular branch, defined by the upper node.
virtual const std::vector< int > & getAlphabetStates() const =0
Partial implementation of the DiscreteRatesAcrossSitesTreeLikelihood interface.
Specialization of the TreeLikelihood interface for the Homogeneous case.
Iterates through all models used for all sites on a given branch.
const SubstitutionModel * getSubstitutionModel() const
virtual void initParameters()
This builds the parameters list from all parametrizable objects, i.e. substitution model...
ParameterList getRateDistributionParameters() const
Get the parameters associated to the rate distirbution.
Interface for phylogenetic tree objects.
Definition: Tree.h:148
AbstractHomogeneousTreeLikelihood(const Tree &tree, SubstitutionModel *model, DiscreteDistribution *rDist, bool checkRooted=true, bool verbose=true)
void init_(const Tree &tree, SubstitutionModel *model, DiscreteDistribution *rDist, bool checkRooted, bool verbose)
Method called by constructor.
virtual const Vdouble & getFrequencies() const =0
ParameterList getRateDistributionParameters() const
Get the parameters associated to the rate distirbution.
Iterates through all models used for all branches on a given site.
ConstSiteModelIterator * getNewSiteModelIterator(size_t siteIndex) const
Exception thrown when something is wrong with a particular node.
ParameterList getSubstitutionModelParameters() const
Get the parameters associated to substitution model(s).
The phylogenetic node class.
Definition: Node.h:90
const SubstitutionModel * getSubstitutionModel(int nodeId, size_t siteIndex) const
Get the substitution model associated to a given node and alignment column.
virtual std::string getAlphabetStateAsChar(size_t index) const =0
virtual void computeTransitionProbabilitiesForNode(const Node *node)
Fill the pxy_, dpxy_ and d2pxy_ arrays for one node.
std::vector< Node * > nodes_
Pointer toward all nodes in the tree.
ParameterList getBranchLengthsParameters() const
Get the branch lengths parameters.
virtual void applyParameters()
All parameters are stored in a parameter list. This function apply these parameters to the substituti...
A pair of SubstitutionModel / BranchIterator.
virtual size_t getNumberOfStates() const =0
Get the number of states.
Partial implementation for homogeneous model of the TreeLikelihood interface.
ConstBranchModelIterator * getNewBranchModelIterator(int nodeId) const
const std::vector< double > & getRootFrequencies(size_t siteIndex) const
Get the values of the frequencies for each state in the alphabet at the root node.