bpp-phyl  2.2.0
DRASDRTreeLikelihoodData.h
Go to the documentation of this file.
1 //
2 // File: DRASDRTreeLikelihoodData.h
3 // Created by: Julien Dutheil
4 // Created on: Sat Dec 30 14:20 2006
5 // From file DRHomogeneousTreeLikelihood.h
6 //
7 
8 /*
9 Copyright or © or Copr. CNRS, (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 #ifndef _DRASDRHOMOGENEOUSTREELIKELIHOODDATA_H_
42 #define _DRASDRHOMOGENEOUSTREELIKELIHOODDATA_H_
43 
45 #include "../Model/SubstitutionModel.h"
46 #include "../PatternTools.h"
47 #include "../SitePatterns.h"
48 
49 //From SeqLib:
50 #include <Bpp/Seq/Container/AlignedSequenceContainer.h>
51 
52 // From the STL:
53 #include <map>
54 
55 namespace bpp
56 {
57 
68  public virtual TreeLikelihoodNodeData
69 {
70  private:
71  mutable VVdouble leafLikelihood_;
72  const Node* leaf_;
73 
74  public:
76 
79 
81  {
83  leaf_ = data.leaf_;
84  return *this;
85  }
86 
87 #ifndef NO_VIRTUAL_COV
89 #else
90  Clonable*
91 #endif
92  clone() const
93  {
94  return new DRASDRTreeLikelihoodLeafData(*this);
95  }
96 
97  public:
98  const Node* getNode() const { return leaf_; }
99  void setNode(const Node* node) { leaf_ = node; }
100 
101  VVdouble& getLikelihoodArray() { return leafLikelihood_; }
102 };
103 
114  public virtual TreeLikelihoodNodeData
115 {
116  private:
130  mutable std::map<int, VVVdouble> nodeLikelihoods_;
140  mutable Vdouble nodeDLikelihoods_;
141 
151  mutable Vdouble nodeD2Likelihoods_;
152 
153  const Node* node_;
154 
155  public:
157 
162  node_(data.node_)
163  {}
164 
166  {
170  node_ = data.node_;
171  return *this;
172  }
173 
175 
176 #ifndef NO_VIRTUAL_COV
178 #else
179  Clonable*
180 #endif
181  clone() const
182  {
183  return new DRASDRTreeLikelihoodNodeData(*this);
184  }
185 
186  public:
187  const Node* getNode() const { return node_; }
188 
189  void setNode(const Node* node) { node_ = node; }
190 
191  const std::map<int, VVVdouble>& getLikelihoodArrays() const { return nodeLikelihoods_; }
192 
193  std::map<int, VVVdouble>& getLikelihoodArrays() { return nodeLikelihoods_; }
194 
195  VVVdouble& getLikelihoodArrayForNeighbor(int neighborId)
196  {
197  return nodeLikelihoods_[neighborId];
198  }
199 
200  const VVVdouble& getLikelihoodArrayForNeighbor(int neighborId) const
201  {
202  return nodeLikelihoods_[neighborId];
203  }
204 
205  Vdouble& getDLikelihoodArray() { return nodeDLikelihoods_; }
206 
207  const Vdouble& getDLikelihoodArray() const { return nodeDLikelihoods_; }
208 
210 
211  const Vdouble& getD2LikelihoodArrayForNeighbor() const { return nodeD2Likelihoods_; }
212 
213  bool isNeighbor(int neighborId) const
214  {
215  return nodeLikelihoods_.find(neighborId) != nodeLikelihoods_.end();
216  }
217 
219  {
220  nodeLikelihoods_.erase(nodeLikelihoods_.begin(), nodeLikelihoods_.end());
223  }
224 };
225 
230  public virtual AbstractTreeLikelihoodData
231 {
232  private:
233 
234  mutable std::map<int, DRASDRTreeLikelihoodNodeData> nodeData_;
235  mutable std::map<int, DRASDRTreeLikelihoodLeafData> leafData_;
236  mutable VVVdouble rootLikelihoods_;
237  mutable VVdouble rootLikelihoodsS_;
238  mutable Vdouble rootLikelihoodsSR_;
239 
240  SiteContainer* shrunkData_;
241  size_t nbSites_;
242  size_t nbStates_;
243  size_t nbClasses_;
245 
246  public:
247  DRASDRTreeLikelihoodData(const TreeTemplate<Node>* tree, size_t nbClasses) :
250  shrunkData_(0), nbSites_(0), nbStates_(0), nbClasses_(nbClasses), nbDistinctSites_(0)
251  {}
252 
255  nodeData_(data.nodeData_), leafData_(data.leafData_),
259  shrunkData_(0),
260  nbSites_(data.nbSites_), nbStates_(data.nbStates_),
262  {
263  if (data.shrunkData_)
264  shrunkData_ = dynamic_cast<SiteContainer*>(data.shrunkData_->clone());
265  }
266 
268  {
270  nodeData_ = data.nodeData_;
271  leafData_ = data.leafData_;
275  nbSites_ = data.nbSites_;
276  nbStates_ = data.nbStates_;
277  nbClasses_ = data.nbClasses_;
279  if (shrunkData_) delete shrunkData_;
280  if (data.shrunkData_)
281  shrunkData_ = dynamic_cast<SiteContainer *>(data.shrunkData_->clone());
282  else
283  shrunkData_ = 0;
284  return *this;
285  }
286 
288 
290 
291  public:
301  void setTree(const TreeTemplate<Node>* tree)
302  {
303  tree_ = tree;
304  for (std::map<int, DRASDRTreeLikelihoodNodeData>::iterator it = nodeData_.begin(); it != nodeData_.end(); it++)
305  {
306  int id = it->second.getNode()->getId();
307  it->second.setNode(tree_->getNode(id));
308  }
309  for (std::map<int, DRASDRTreeLikelihoodLeafData>::iterator it = leafData_.begin(); it != leafData_.end(); it++)
310  {
311  int id = it->second.getNode()->getId();
312  it->second.setNode(tree_->getNode(id));
313  }
314  }
315 
317  {
318  return nodeData_[nodeId];
319  }
320 
321  const DRASDRTreeLikelihoodNodeData& getNodeData(int nodeId) const
322  {
323  return nodeData_[nodeId];
324  }
325 
327  {
328  return leafData_[nodeId];
329  }
330 
331  const DRASDRTreeLikelihoodLeafData& getLeafData(int nodeId) const
332  {
333  return leafData_[nodeId];
334  }
335 
336  size_t getArrayPosition(int parentId, int sonId, size_t currentPosition) const
337  {
338  return currentPosition;
339  }
340 
341  const std::map<int, VVVdouble>& getLikelihoodArrays(int nodeId) const
342  {
343  return nodeData_[nodeId].getLikelihoodArrays();
344  }
345 
346  std::map<int, VVVdouble>& getLikelihoodArrays(int nodeId)
347  {
348  return nodeData_[nodeId].getLikelihoodArrays();
349  }
350 
351  VVVdouble& getLikelihoodArray(int parentId, int neighborId)
352  {
353  return nodeData_[parentId].getLikelihoodArrayForNeighbor(neighborId);
354  }
355 
356  const VVVdouble& getLikelihoodArray(int parentId, int neighborId) const
357  {
358  return nodeData_[parentId].getLikelihoodArrayForNeighbor(neighborId);
359  }
360 
361  Vdouble& getDLikelihoodArray(int nodeId)
362  {
363  return nodeData_[nodeId].getDLikelihoodArray();
364  }
365 
366  const Vdouble& getDLikelihoodArray(int nodeId) const
367  {
368  return nodeData_[nodeId].getDLikelihoodArray();
369  }
370 
371  Vdouble& getD2LikelihoodArray(int nodeId)
372  {
373  return nodeData_[nodeId].getD2LikelihoodArray();
374  }
375 
376  const Vdouble& getD2LikelihoodArray(int nodeId) const
377  {
378  return nodeData_[nodeId].getD2LikelihoodArray();
379  }
380 
381  VVdouble& getLeafLikelihoods(int nodeId)
382  {
383  return leafData_[nodeId].getLikelihoodArray();
384  }
385 
386  const VVdouble& getLeafLikelihoods(int nodeId) const
387  {
388  return leafData_[nodeId].getLikelihoodArray();
389  }
390 
391  VVVdouble& getRootLikelihoodArray() { return rootLikelihoods_; }
392  const VVVdouble & getRootLikelihoodArray() const { return rootLikelihoods_; }
393 
395  const VVdouble& getRootSiteLikelihoodArray() const { return rootLikelihoodsS_; }
396 
398  const Vdouble& getRootRateSiteLikelihoodArray() const { return rootLikelihoodsSR_; }
399 
400  size_t getNumberOfDistinctSites() const { return nbDistinctSites_; }
401 
402  size_t getNumberOfSites() const { return nbSites_; }
403 
404  size_t getNumberOfStates() const { return nbStates_; }
405 
406  size_t getNumberOfClasses() const { return nbClasses_; }
407 
408  const SiteContainer* getShrunkData() const { return shrunkData_; }
409 
417  void initLikelihoods(const SiteContainer& sites, const SubstitutionModel& model) throw (Exception);
418 
426  void reInit() throw (Exception);
427 
428  void reInit(const Node* node) throw (Exception);
429 
430  protected:
447  void initLikelihoods(const Node* node, const SiteContainer& sites, const SubstitutionModel& model) throw (Exception);
448 
449 };
450 
451 } //end of namespace bpp.
452 
453 #endif //_DRASDRHOMOGENEOUSTREELIKELIHOODDATA_H_
454 
Vdouble nodeD2Likelihoods_
This contains all likelihood second order derivatives values used for computation.
Interface for all substitution models.
DRASDRTreeLikelihoodData & operator=(const DRASDRTreeLikelihoodData &data)
AbstractTreeLikelihoodData & operator=(const AbstractTreeLikelihoodData &atd)
Partial implementation of the TreeLikelihoodData interface.
const DRASDRTreeLikelihoodNodeData & getNodeData(int nodeId) const
DRASDRTreeLikelihoodNodeData(const DRASDRTreeLikelihoodNodeData &data)
void setNode(const Node *node)
Set the node associated to this data.
const DRASDRTreeLikelihoodLeafData & getLeafData(int nodeId) const
const Vdouble & getD2LikelihoodArray(int nodeId) const
DRASDRTreeLikelihoodData(const TreeTemplate< Node > *tree, size_t nbClasses)
DRASDRTreeLikelihoodNodeData & getNodeData(int nodeId)
Likelihood data structure for a node.
Vdouble nodeDLikelihoods_
This contains all likelihood first order derivatives values used for computation. ...
std::map< int, DRASDRTreeLikelihoodLeafData > leafData_
const Vdouble & getD2LikelihoodArrayForNeighbor() const
DRASDRTreeLikelihoodNodeData * clone() const
const std::map< int, VVVdouble > & getLikelihoodArrays(int nodeId) const
The phylogenetic tree class.
VVVdouble & getLikelihoodArrayForNeighbor(int neighborId)
Vdouble & getD2LikelihoodArray(int nodeId)
const Vdouble & getDLikelihoodArray(int nodeId) const
DRASDRTreeLikelihoodLeafData & getLeafData(int nodeId)
const TreeTemplate< Node > * tree_
const Node * getNode() const
Get the node associated to this data structure.
std::map< int, VVVdouble > & getLikelihoodArrays(int nodeId)
DRASDRTreeLikelihoodNodeData & operator=(const DRASDRTreeLikelihoodNodeData &data)
void reInit()
Rebuild likelihood arrays at inner nodes.
VVdouble & getLeafLikelihoods(int nodeId)
DRASDRTreeLikelihoodLeafData & operator=(const DRASDRTreeLikelihoodLeafData &data)
DRASDRTreeLikelihoodLeafData(const DRASDRTreeLikelihoodLeafData &data)
std::map< int, VVVdouble > & getLikelihoodArrays()
const VVdouble & getLeafLikelihoods(int nodeId) const
const VVVdouble & getLikelihoodArrayForNeighbor(int neighborId) const
VVVdouble & getLikelihoodArray(int parentId, int neighborId)
const VVVdouble & getRootLikelihoodArray() const
void setNode(const Node *node)
Set the node associated to this data.
The phylogenetic node class.
Definition: Node.h:90
DRASDRTreeLikelihoodData(const DRASDRTreeLikelihoodData &data)
const std::map< int, VVVdouble > & getLikelihoodArrays() const
const Node * getNode() const
Get the node associated to this data structure.
const VVVdouble & getLikelihoodArray(int parentId, int neighborId) const
std::map< int, VVVdouble > nodeLikelihoods_
This contains all likelihood values used for computation.
const Vdouble & getRootRateSiteLikelihoodArray() const
Likelihood data structure for rate across sites models, using a double-recursive algorithm.
const VVdouble & getRootSiteLikelihoodArray() const
DRASDRTreeLikelihoodData * clone() const
void initLikelihoods(const SiteContainer &sites, const SubstitutionModel &model)
Resize and initialize all likelihood arrays according to the given data set and substitution model...
size_t getArrayPosition(int parentId, int sonId, size_t currentPosition) const
DRASDRTreeLikelihoodLeafData * clone() const
std::map< int, DRASDRTreeLikelihoodNodeData > nodeData_
Likelihood data structure for a leaf.
TreeLikelihood partial data structure.
virtual N * getNode(int id, bool checkId=false)
Definition: TreeTemplate.h:419
void setTree(const TreeTemplate< Node > *tree)
Set the tree associated to the data.
const SiteContainer * getShrunkData() const