2
// File: DRASDRTreeLikelihoodData.h
3
// Created by: Julien Dutheil
4
// Created on: Sat Dec 30 14:20 2006
5
// From file DRHomogeneousTreeLikelihood.h
9
Copyright or Ā© or Copr. CNRS, (November 16, 2004)
11
This software is a computer program whose purpose is to provide classes
12
for phylogenetic data analysis.
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".
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
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.
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.
41
#ifndef _DRASDRHOMOGENEOUSTREELIKELIHOODDATA_H_
42
#define _DRASDRHOMOGENEOUSTREELIKELIHOODDATA_H_
44
#include "AbstractTreeLikelihoodData.h"
45
#include "../Model/SubstitutionModel.h"
46
#include "../PatternTools.h"
47
#include "../SitePatterns.h"
50
#include <Bpp/Seq/Container/AlignedSequenceContainer.h>
59
* @brief Likelihood data structure for a leaf.
61
* This class is for use with the DRASDRTreeLikelihoodData class.
63
* Store the likelihoods arrays associated to a leaf.
65
* @see DRASDRTreeLikelihoodData
67
class DRASDRTreeLikelihoodLeafData :
68
public virtual TreeLikelihoodNodeData
71
mutable VVdouble leafLikelihood_;
75
DRASDRTreeLikelihoodLeafData() : leafLikelihood_(), leaf_(0) {}
77
DRASDRTreeLikelihoodLeafData(const DRASDRTreeLikelihoodLeafData& data) :
78
leafLikelihood_(data.leafLikelihood_), leaf_(data.leaf_) {}
80
DRASDRTreeLikelihoodLeafData& operator=(const DRASDRTreeLikelihoodLeafData& data)
82
leafLikelihood_ = data.leafLikelihood_;
87
#ifndef NO_VIRTUAL_COV
88
DRASDRTreeLikelihoodLeafData*
94
return new DRASDRTreeLikelihoodLeafData(*this);
98
const Node* getNode() const { return leaf_; }
99
void setNode(const Node* node) { leaf_ = node; }
101
VVdouble& getLikelihoodArray() { return leafLikelihood_; }
105
* @brief Likelihood data structure for a node.
107
* This class is for use with the DRASDRTreeLikelihoodData class.
109
* Store for each neighbor node an array with conditionnal likelihoods.
111
* @see DRASDRTreeLikelihoodData
113
class DRASDRTreeLikelihoodNodeData :
114
public virtual TreeLikelihoodNodeData
118
* @brief This contains all likelihood values used for computation.
122
* |------------> Neighbor node of n (id)
124
* |------> Rate class c
125
* |---> Ancestral state s
127
* We call this the <i>likelihood array</i> for each node.
130
mutable std::map<int, VVVdouble> nodeLikelihoods_;
132
* @brief This contains all likelihood first order derivatives values used for computation.
138
* We call this the <i>dLikelihood array</i> for each node.
140
mutable Vdouble nodeDLikelihoods_;
143
* @brief This contains all likelihood second order derivatives values used for computation.
149
* We call this the <i>d2Likelihood array</i> for each node.
151
mutable Vdouble nodeD2Likelihoods_;
156
DRASDRTreeLikelihoodNodeData() : nodeLikelihoods_(), nodeDLikelihoods_(), nodeD2Likelihoods_(), node_(0) {}
158
DRASDRTreeLikelihoodNodeData(const DRASDRTreeLikelihoodNodeData& data) :
159
nodeLikelihoods_(data.nodeLikelihoods_),
160
nodeDLikelihoods_(data.nodeDLikelihoods_),
161
nodeD2Likelihoods_(data.nodeD2Likelihoods_),
165
DRASDRTreeLikelihoodNodeData& operator=(const DRASDRTreeLikelihoodNodeData& data)
167
nodeLikelihoods_ = data.nodeLikelihoods_;
168
nodeDLikelihoods_ = data.nodeDLikelihoods_;
169
nodeD2Likelihoods_ = data.nodeD2Likelihoods_;
174
virtual ~DRASDRTreeLikelihoodNodeData() {}
176
#ifndef NO_VIRTUAL_COV
177
DRASDRTreeLikelihoodNodeData*
183
return new DRASDRTreeLikelihoodNodeData(*this);
187
const Node* getNode() const { return node_; }
189
void setNode(const Node* node) { node_ = node; }
191
const std::map<int, VVVdouble>& getLikelihoodArrays() const { return nodeLikelihoods_; }
193
std::map<int, VVVdouble>& getLikelihoodArrays() { return nodeLikelihoods_; }
195
VVVdouble& getLikelihoodArrayForNeighbor(int neighborId)
197
return nodeLikelihoods_[neighborId];
200
const VVVdouble& getLikelihoodArrayForNeighbor(int neighborId) const
202
return nodeLikelihoods_[neighborId];
205
Vdouble& getDLikelihoodArray() { return nodeDLikelihoods_; }
207
const Vdouble& getDLikelihoodArray() const { return nodeDLikelihoods_; }
209
Vdouble& getD2LikelihoodArray() { return nodeD2Likelihoods_; }
211
const Vdouble& getD2LikelihoodArrayForNeighbor() const { return nodeD2Likelihoods_; }
213
bool isNeighbor(int neighborId) const
215
return nodeLikelihoods_.find(neighborId) != nodeLikelihoods_.end();
218
void eraseNeighborArrays()
220
nodeLikelihoods_.erase(nodeLikelihoods_.begin(), nodeLikelihoods_.end());
221
nodeDLikelihoods_.erase(nodeDLikelihoods_.begin(), nodeDLikelihoods_.end());
222
nodeD2Likelihoods_.erase(nodeD2Likelihoods_.begin(), nodeD2Likelihoods_.end());
227
* @brief Likelihood data structure for rate across sites models, using a double-recursive algorithm.
229
class DRASDRTreeLikelihoodData :
230
public virtual AbstractTreeLikelihoodData
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_;
240
SiteContainer* shrunkData_;
241
unsigned int nbSites_;
242
unsigned int nbStates_;
243
unsigned int nbClasses_;
244
unsigned int nbDistinctSites_;
247
DRASDRTreeLikelihoodData(const TreeTemplate<Node>* tree, unsigned int nbClasses) :
248
AbstractTreeLikelihoodData(tree),
249
nodeData_(), leafData_(), rootLikelihoods_(), rootLikelihoodsS_(), rootLikelihoodsSR_(),
250
shrunkData_(0), nbSites_(0), nbStates_(0), nbClasses_(nbClasses), nbDistinctSites_(0)
253
DRASDRTreeLikelihoodData(const DRASDRTreeLikelihoodData& data):
254
AbstractTreeLikelihoodData(data),
255
nodeData_(data.nodeData_), leafData_(data.leafData_),
256
rootLikelihoods_(data.rootLikelihoods_),
257
rootLikelihoodsS_(data.rootLikelihoodsS_),
258
rootLikelihoodsSR_(data.rootLikelihoodsSR_),
260
nbSites_(data.nbSites_), nbStates_(data.nbStates_),
261
nbClasses_(data.nbClasses_), nbDistinctSites_(data.nbDistinctSites_)
263
if (data.shrunkData_)
264
shrunkData_ = dynamic_cast<SiteContainer*>(data.shrunkData_->clone());
267
DRASDRTreeLikelihoodData& operator=(const DRASDRTreeLikelihoodData& data)
269
AbstractTreeLikelihoodData::operator=(data);
270
nodeData_ = data.nodeData_;
271
leafData_ = data.leafData_;
272
rootLikelihoods_ = data.rootLikelihoods_;
273
rootLikelihoodsS_ = data.rootLikelihoodsS_;
274
rootLikelihoodsSR_ = data.rootLikelihoodsSR_;
275
nbSites_ = data.nbSites_;
276
nbStates_ = data.nbStates_;
277
nbClasses_ = data.nbClasses_;
278
nbDistinctSites_ = data.nbDistinctSites_;
279
if (shrunkData_) delete shrunkData_;
280
if (data.shrunkData_)
281
shrunkData_ = dynamic_cast<SiteContainer *>(data.shrunkData_->clone());
287
virtual ~DRASDRTreeLikelihoodData() { delete shrunkData_; }
289
DRASDRTreeLikelihoodData* clone() const { return new DRASDRTreeLikelihoodData(*this); }
293
* @brief Set the tree associated to the data.
295
* All node data will be actualized accordingly by calling the setNode() method on the corresponding nodes.
296
* @warning: the old tree and the new tree must be two clones! And particularly, they have to share the
297
* same topology and nodes id.
299
* @param tree The tree to be associated to this data.
301
void setTree(const TreeTemplate<Node>* tree)
304
for (std::map<int, DRASDRTreeLikelihoodNodeData>::iterator it = nodeData_.begin(); it != nodeData_.end(); it++)
306
int id = it->second.getNode()->getId();
307
it->second.setNode(tree_->getNode(id));
309
for (std::map<int, DRASDRTreeLikelihoodLeafData>::iterator it = leafData_.begin(); it != leafData_.end(); it++)
311
int id = it->second.getNode()->getId();
312
it->second.setNode(tree_->getNode(id));
316
DRASDRTreeLikelihoodNodeData& getNodeData(int nodeId)
318
return nodeData_[nodeId];
321
const DRASDRTreeLikelihoodNodeData& getNodeData(int nodeId) const
323
return nodeData_[nodeId];
326
DRASDRTreeLikelihoodLeafData& getLeafData(int nodeId)
328
return leafData_[nodeId];
331
const DRASDRTreeLikelihoodLeafData& getLeafData(int nodeId) const
333
return leafData_[nodeId];
336
unsigned int getArrayPosition(int parentId, int sonId, unsigned int currentPosition) const
338
return currentPosition;
341
const std::map<int, VVVdouble>& getLikelihoodArrays(int nodeId) const
343
return nodeData_[nodeId].getLikelihoodArrays();
346
std::map<int, VVVdouble>& getLikelihoodArrays(int nodeId)
348
return nodeData_[nodeId].getLikelihoodArrays();
351
VVVdouble& getLikelihoodArray(int parentId, int neighborId)
353
return nodeData_[parentId].getLikelihoodArrayForNeighbor(neighborId);
356
const VVVdouble& getLikelihoodArray(int parentId, int neighborId) const
358
return nodeData_[parentId].getLikelihoodArrayForNeighbor(neighborId);
361
Vdouble& getDLikelihoodArray(int nodeId)
363
return nodeData_[nodeId].getDLikelihoodArray();
366
const Vdouble& getDLikelihoodArray(int nodeId) const
368
return nodeData_[nodeId].getDLikelihoodArray();
371
Vdouble& getD2LikelihoodArray(int nodeId)
373
return nodeData_[nodeId].getD2LikelihoodArray();
376
const Vdouble& getD2LikelihoodArray(int nodeId) const
378
return nodeData_[nodeId].getD2LikelihoodArray();
381
VVdouble& getLeafLikelihoods(int nodeId)
383
return leafData_[nodeId].getLikelihoodArray();
386
const VVdouble& getLeafLikelihoods(int nodeId) const
388
return leafData_[nodeId].getLikelihoodArray();
391
VVVdouble& getRootLikelihoodArray() { return rootLikelihoods_; }
392
const VVVdouble & getRootLikelihoodArray() const { return rootLikelihoods_; }
394
VVdouble& getRootSiteLikelihoodArray() { return rootLikelihoodsS_; }
395
const VVdouble& getRootSiteLikelihoodArray() const { return rootLikelihoodsS_; }
397
Vdouble& getRootRateSiteLikelihoodArray() { return rootLikelihoodsSR_; }
398
const Vdouble& getRootRateSiteLikelihoodArray() const { return rootLikelihoodsSR_; }
400
unsigned int getNumberOfDistinctSites() const { return nbDistinctSites_; }
402
unsigned int getNumberOfSites() const { return nbSites_; }
404
unsigned int getNumberOfStates() const { return nbStates_; }
406
unsigned int getNumberOfClasses() const { return nbClasses_; }
408
const SiteContainer* getShrunkData() const { return shrunkData_; }
411
* @brief Resize and initialize all likelihood arrays according to the given data set and substitution model.
413
* @param sites The sequences to use as data.
414
* @param model The substitution model to use.
415
* @throw Exception if an error occures.
417
void initLikelihoods(const SiteContainer& sites, const SubstitutionModel& model) throw (Exception);
420
* @brief Rebuild likelihood arrays at inner nodes.
422
* This method is to be called when the topology of the tree has changed.
423
* Node arrays relationship are rebuilt according to the new topology of the tree.
424
* The leaves likelihood remain unchanged, so as for the first and second order derivatives.
426
void reInit() throw (Exception);
428
void reInit(const Node* node) throw (Exception);
432
* @brief This method initializes the leaves according to a sequence container.
434
* Here the container shrunkData_ is used.
435
* Likelihood is set to 1 for the state corresponding to the sequence site,
436
* otherwise it is set to 0.
438
* All likelihood arrays at each nodes are initialized according to alphabet
439
* size and sequences length, and filled with 1.
441
* NB: This method is recursive.
443
* @param node The node defining the subtree to analyse.
444
* @param sites The sequence container to use.
445
* @param model The model, used for initializing leaves' likelihoods.
447
void initLikelihoods(const Node* node, const SiteContainer& sites, const SubstitutionModel& model) throw (Exception);
451
} //end of namespace bpp.
453
#endif //_DRASDRHOMOGENEOUSTREELIKELIHOODDATA_H_