bpp-phyl  2.4.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:
51 
52 // From the STL:
53 #include <map>
54 
55 namespace bpp
56 {
57 
68  public virtual TreeLikelihoodNodeData
69 {
70  private:
72  const Node* leaf_;
73 
74  public:
75  DRASDRTreeLikelihoodLeafData() : leafLikelihood_(), leaf_(0) {}
76 
78  leafLikelihood_(data.leafLikelihood_), leaf_(data.leaf_) {}
79 
81  {
82  leafLikelihood_ = data.leafLikelihood_;
83  leaf_ = data.leaf_;
84  return *this;
85  }
86 
88  {
89  return new DRASDRTreeLikelihoodLeafData(*this);
90  }
91 
92  public:
93  const Node* getNode() const { return leaf_; }
94  void setNode(const Node* node) { leaf_ = node; }
95 
97 };
98 
109  public virtual TreeLikelihoodNodeData
110 {
111  private:
125  mutable std::map<int, VVVdouble> nodeLikelihoods_;
136 
147 
148  const Node* node_;
149 
150  public:
151  DRASDRTreeLikelihoodNodeData() : nodeLikelihoods_(), nodeDLikelihoods_(), nodeD2Likelihoods_(), node_(0) {}
152 
154  nodeLikelihoods_(data.nodeLikelihoods_),
155  nodeDLikelihoods_(data.nodeDLikelihoods_),
156  nodeD2Likelihoods_(data.nodeD2Likelihoods_),
157  node_(data.node_)
158  {}
159 
161  {
162  nodeLikelihoods_ = data.nodeLikelihoods_;
163  nodeDLikelihoods_ = data.nodeDLikelihoods_;
164  nodeD2Likelihoods_ = data.nodeD2Likelihoods_;
165  node_ = data.node_;
166  return *this;
167  }
168 
170 
172  {
173  return new DRASDRTreeLikelihoodNodeData(*this);
174  }
175 
176  public:
177  const Node* getNode() const { return node_; }
178 
179  void setNode(const Node* node) { node_ = node; }
180 
181  const std::map<int, VVVdouble>& getLikelihoodArrays() const { return nodeLikelihoods_; }
182 
183  std::map<int, VVVdouble>& getLikelihoodArrays() { return nodeLikelihoods_; }
184 
186  {
187  return nodeLikelihoods_[neighborId];
188  }
189 
190  const VVVdouble& getLikelihoodArrayForNeighbor(int neighborId) const
191  {
192  return nodeLikelihoods_[neighborId];
193  }
194 
195  Vdouble& getDLikelihoodArray() { return nodeDLikelihoods_; }
196 
197  const Vdouble& getDLikelihoodArray() const { return nodeDLikelihoods_; }
198 
199  Vdouble& getD2LikelihoodArray() { return nodeD2Likelihoods_; }
200 
201  const Vdouble& getD2LikelihoodArrayForNeighbor() const { return nodeD2Likelihoods_; }
202 
203  bool isNeighbor(int neighborId) const
204  {
205  return nodeLikelihoods_.find(neighborId) != nodeLikelihoods_.end();
206  }
207 
209  {
210  nodeLikelihoods_.erase(nodeLikelihoods_.begin(), nodeLikelihoods_.end());
211  nodeDLikelihoods_.erase(nodeDLikelihoods_.begin(), nodeDLikelihoods_.end());
212  nodeD2Likelihoods_.erase(nodeD2Likelihoods_.begin(), nodeD2Likelihoods_.end());
213  }
214 };
215 
220  public virtual AbstractTreeLikelihoodData
221 {
222  private:
223 
224  mutable std::map<int, DRASDRTreeLikelihoodNodeData> nodeData_;
225  mutable std::map<int, DRASDRTreeLikelihoodLeafData> leafData_;
229 
231  size_t nbSites_;
232  size_t nbStates_;
233  size_t nbClasses_;
235 
236  public:
237  DRASDRTreeLikelihoodData(const TreeTemplate<Node>* tree, size_t nbClasses) :
239  nodeData_(), leafData_(), rootLikelihoods_(), rootLikelihoodsS_(), rootLikelihoodsSR_(),
240  shrunkData_(0), nbSites_(0), nbStates_(0), nbClasses_(nbClasses), nbDistinctSites_(0)
241  {}
242 
245  nodeData_(data.nodeData_), leafData_(data.leafData_),
246  rootLikelihoods_(data.rootLikelihoods_),
247  rootLikelihoodsS_(data.rootLikelihoodsS_),
248  rootLikelihoodsSR_(data.rootLikelihoodsSR_),
249  shrunkData_(0),
250  nbSites_(data.nbSites_), nbStates_(data.nbStates_),
251  nbClasses_(data.nbClasses_), nbDistinctSites_(data.nbDistinctSites_)
252  {
253  if (data.shrunkData_)
254  shrunkData_ = dynamic_cast<SiteContainer*>(data.shrunkData_->clone());
255  }
256 
258  {
260  nodeData_ = data.nodeData_;
261  leafData_ = data.leafData_;
262  rootLikelihoods_ = data.rootLikelihoods_;
263  rootLikelihoodsS_ = data.rootLikelihoodsS_;
264  rootLikelihoodsSR_ = data.rootLikelihoodsSR_;
265  nbSites_ = data.nbSites_;
266  nbStates_ = data.nbStates_;
267  nbClasses_ = data.nbClasses_;
268  nbDistinctSites_ = data.nbDistinctSites_;
269  if (shrunkData_) delete shrunkData_;
270  if (data.shrunkData_)
271  shrunkData_ = dynamic_cast<SiteContainer *>(data.shrunkData_->clone());
272  else
273  shrunkData_ = 0;
274  return *this;
275  }
276 
277  virtual ~DRASDRTreeLikelihoodData() { delete shrunkData_; }
278 
280 
281  public:
291  void setTree(const TreeTemplate<Node>* tree)
292  {
293  tree_ = tree;
294  for (std::map<int, DRASDRTreeLikelihoodNodeData>::iterator it = nodeData_.begin(); it != nodeData_.end(); it++)
295  {
296  int id = it->second.getNode()->getId();
297  it->second.setNode(tree_->getNode(id));
298  }
299  for (std::map<int, DRASDRTreeLikelihoodLeafData>::iterator it = leafData_.begin(); it != leafData_.end(); it++)
300  {
301  int id = it->second.getNode()->getId();
302  it->second.setNode(tree_->getNode(id));
303  }
304  }
305 
307  {
308  return nodeData_[nodeId];
309  }
310 
311  const DRASDRTreeLikelihoodNodeData& getNodeData(int nodeId) const
312  {
313  return nodeData_[nodeId];
314  }
315 
317  {
318  return leafData_[nodeId];
319  }
320 
321  const DRASDRTreeLikelihoodLeafData& getLeafData(int nodeId) const
322  {
323  return leafData_[nodeId];
324  }
325 
326  size_t getArrayPosition(int parentId, int sonId, size_t currentPosition) const
327  {
328  return currentPosition;
329  }
330 
331  const std::map<int, VVVdouble>& getLikelihoodArrays(int nodeId) const
332  {
333  return nodeData_[nodeId].getLikelihoodArrays();
334  }
335 
336  std::map<int, VVVdouble>& getLikelihoodArrays(int nodeId)
337  {
338  return nodeData_[nodeId].getLikelihoodArrays();
339  }
340 
341  VVVdouble& getLikelihoodArray(int parentId, int neighborId)
342  {
343  return nodeData_[parentId].getLikelihoodArrayForNeighbor(neighborId);
344  }
345 
346  const VVVdouble& getLikelihoodArray(int parentId, int neighborId) const
347  {
348  return nodeData_[parentId].getLikelihoodArrayForNeighbor(neighborId);
349  }
350 
352  {
353  return nodeData_[nodeId].getDLikelihoodArray();
354  }
355 
356  const Vdouble& getDLikelihoodArray(int nodeId) const
357  {
358  return nodeData_[nodeId].getDLikelihoodArray();
359  }
360 
362  {
363  return nodeData_[nodeId].getD2LikelihoodArray();
364  }
365 
366  const Vdouble& getD2LikelihoodArray(int nodeId) const
367  {
368  return nodeData_[nodeId].getD2LikelihoodArray();
369  }
370 
372  {
373  return leafData_[nodeId].getLikelihoodArray();
374  }
375 
376  const VVdouble& getLeafLikelihoods(int nodeId) const
377  {
378  return leafData_[nodeId].getLikelihoodArray();
379  }
380 
381  VVVdouble& getRootLikelihoodArray() { return rootLikelihoods_; }
382  const VVVdouble & getRootLikelihoodArray() const { return rootLikelihoods_; }
383 
384  VVdouble& getRootSiteLikelihoodArray() { return rootLikelihoodsS_; }
385  const VVdouble& getRootSiteLikelihoodArray() const { return rootLikelihoodsS_; }
386 
387  Vdouble& getRootRateSiteLikelihoodArray() { return rootLikelihoodsSR_; }
388  const Vdouble& getRootRateSiteLikelihoodArray() const { return rootLikelihoodsSR_; }
389 
390  size_t getNumberOfDistinctSites() const { return nbDistinctSites_; }
391 
392  size_t getNumberOfSites() const { return nbSites_; }
393 
394  size_t getNumberOfStates() const { return nbStates_; }
395 
396  size_t getNumberOfClasses() const { return nbClasses_; }
397 
398  const SiteContainer* getShrunkData() const { return shrunkData_; }
399 
407  void initLikelihoods(const SiteContainer& sites, const TransitionModel& model);
408 
416  void reInit();
417 
418  void reInit(const Node* node);
419 
420  protected:
437  void initLikelihoods(const Node* node, const SiteContainer& sites, const TransitionModel& model);
438 
439 };
440 
441 } //end of namespace bpp.
442 
443 #endif //_DRASDRHOMOGENEOUSTREELIKELIHOODDATA_H_
444 
const VVdouble & getRootSiteLikelihoodArray() const
Vdouble nodeD2Likelihoods_
This contains all likelihood second order derivatives values used for computation.
DRASDRTreeLikelihoodData & operator=(const DRASDRTreeLikelihoodData &data)
AbstractTreeLikelihoodData & operator=(const AbstractTreeLikelihoodData &atd)
Partial implementation of the TreeLikelihoodData interface.
DRASDRTreeLikelihoodNodeData(const DRASDRTreeLikelihoodNodeData &data)
void setNode(const Node *node)
Set the node associated to this data.
DRASDRTreeLikelihoodData(const TreeTemplate< Node > *tree, size_t nbClasses)
DRASDRTreeLikelihoodNodeData & getNodeData(int nodeId)
const Vdouble & getD2LikelihoodArrayForNeighbor() const
const Vdouble & getRootRateSiteLikelihoodArray() const
Likelihood data structure for a node.
const VVVdouble & getLikelihoodArrayForNeighbor(int neighborId) const
Vdouble nodeDLikelihoods_
This contains all likelihood first order derivatives values used for computation. ...
const VVdouble & getLeafLikelihoods(int nodeId) const
std::map< int, DRASDRTreeLikelihoodLeafData > leafData_
The phylogenetic tree class.
VVVdouble & getLikelihoodArrayForNeighbor(int neighborId)
Vdouble & getD2LikelihoodArray(int nodeId)
DRASDRTreeLikelihoodLeafData & getLeafData(int nodeId)
const DRASDRTreeLikelihoodLeafData & getLeafData(int nodeId) const
const Vdouble & getD2LikelihoodArray(int nodeId) const
std::map< int, VVVdouble > & getLikelihoodArrays(int nodeId)
DRASDRTreeLikelihoodNodeData & operator=(const DRASDRTreeLikelihoodNodeData &data)
const VVVdouble & getRootLikelihoodArray() const
size_t getArrayPosition(int parentId, int sonId, size_t currentPosition) const
VVdouble & getLeafLikelihoods(int nodeId)
DRASDRTreeLikelihoodLeafData & operator=(const DRASDRTreeLikelihoodLeafData &data)
DRASDRTreeLikelihoodLeafData(const DRASDRTreeLikelihoodLeafData &data)
std::map< int, VVVdouble > & getLikelihoodArrays()
std::vector< double > Vdouble
const Node * getNode() const
Get the node associated to this data structure.
SiteContainer * clone() const =0
VVVdouble & getLikelihoodArray(int parentId, int neighborId)
DRASDRTreeLikelihoodLeafData * clone() const
void setNode(const Node *node)
Set the node associated to this data.
const VVVdouble & getLikelihoodArray(int parentId, int neighborId) const
bool isNeighbor(int neighborId) const
The phylogenetic node class.
Definition: Node.h:90
DRASDRTreeLikelihoodData(const DRASDRTreeLikelihoodData &data)
const std::map< int, VVVdouble > & getLikelihoodArrays() const
const Vdouble & getDLikelihoodArray(int nodeId) const
std::map< int, VVVdouble > nodeLikelihoods_
This contains all likelihood values used for computation.
Likelihood data structure for rate across sites models, using a double-recursive algorithm.
const SiteContainer * getShrunkData() const
DRASDRTreeLikelihoodData * clone() const
DRASDRTreeLikelihoodNodeData * clone() const
const std::map< int, VVVdouble > & getLikelihoodArrays(int nodeId) const
std::map< int, DRASDRTreeLikelihoodNodeData > nodeData_
const Vdouble & getDLikelihoodArray() const
Likelihood data structure for a leaf.
const Node * getNode() const
Get the node associated to this data structure.
std::vector< VVdouble > VVVdouble
std::vector< Vdouble > VVdouble
TreeLikelihood partial data structure.
virtual N * getNode(int id, bool checkId=false)
Definition: TreeTemplate.h:421
void setTree(const TreeTemplate< Node > *tree)
Set the tree associated to the data.
const DRASDRTreeLikelihoodNodeData & getNodeData(int nodeId) const