bpp-phyl3  3.0.0
DRASRTreeLikelihoodData.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_PHYL_LEGACY_LIKELIHOOD_DRASRTREELIKELIHOODDATA_H
6 #define BPP_PHYL_LEGACY_LIKELIHOOD_DRASRTREELIKELIHOODDATA_H
7 
9 
10 #include "../../Model/SubstitutionModel.h"
11 #include "../../SitePatterns.h"
13 
14 // From the STL:
15 #include <map>
16 
17 namespace bpp
18 {
37  public virtual TreeLikelihoodNodeData
38 {
39 private:
43  const Node* node_;
44 
45 public:
47 
52  node_(data.node_)
53  {}
54 
56  {
60  node_ = data.node_;
61  return *this;
62  }
63 
65  {
66  return new DRASRTreeLikelihoodNodeData(*this);
67  }
68 
69 public:
70  const Node* getNode() const { return node_; }
71  void setNode(const Node* node) { node_ = node; }
72 
74  const VVVdouble& getLikelihoodArray() const { return nodeLikelihoods_; }
75 
77  const VVVdouble& getDLikelihoodArray() const { return nodeDLikelihoods_; }
78 
81 };
82 
87  public virtual AbstractTreeLikelihoodData
88 {
89 private:
93  mutable std::map<int, DRASRTreeLikelihoodNodeData> nodeData_;
94 
107  mutable std::map<int, std::map<int, std::vector<size_t>>> patternLinks_;
108  std::shared_ptr<AlignmentDataInterface> shrunkData_;
109  size_t nbSites_;
110  size_t nbStates_;
111  size_t nbClasses_;
114 
115 public:
116  DRASRTreeLikelihoodData(std::shared_ptr< const TreeTemplate<Node>> tree, size_t nbClasses, bool usePatterns = true) :
119  nbClasses_(nbClasses), nbDistinctSites_(0), usePatterns_(usePatterns)
120  {}
121 
124  nodeData_(data.nodeData_),
126  shrunkData_(),
127  nbSites_(data.nbSites_), nbStates_(data.nbStates_),
130  {
131  if (data.shrunkData_)
132  shrunkData_ = std::unique_ptr<AlignmentDataInterface>(data.shrunkData_->clone());
133  }
134 
136  {
138  nodeData_ = data.nodeData_;
140  nbSites_ = data.nbSites_;
141  nbStates_ = data.nbStates_;
142  nbClasses_ = data.nbClasses_;
144  if (data.shrunkData_)
145  shrunkData_ = std::unique_ptr<AlignmentDataInterface>(data.shrunkData_->clone());
146  else
147  shrunkData_ = 0;
148  usePatterns_ = data.usePatterns_;
149  return *this;
150  }
151 
153 
154  DRASRTreeLikelihoodData* clone() const { return new DRASRTreeLikelihoodData(*this); }
155 
156 public:
166  void setTree(std::shared_ptr< const TreeTemplate<Node>> tree)
167  {
168  tree_ = tree;
169  for (auto& it : nodeData_)
170  {
171  int id = it.second.getNode()->getId();
172  it.second.setNode(tree_->getNode(id));
173  }
174  }
175 
177  {
178  return nodeData_[nodeId];
179  }
180  const DRASRTreeLikelihoodNodeData& getNodeData(int nodeId) const
181  {
182  return nodeData_[nodeId];
183  }
184  size_t getArrayPosition(int parentId, int sonId, size_t currentPosition) const
185  {
186  return patternLinks_[parentId][sonId][currentPosition];
187  }
188  size_t getRootArrayPosition(size_t currentPosition) const
189  {
190  return rootPatternLinks_[currentPosition];
191  }
192  const std::vector<size_t>& getArrayPositions(int parentId, int sonId) const
193  {
194  return patternLinks_[parentId][sonId];
195  }
196  std::vector<size_t>& getArrayPositions(int parentId, int sonId)
197  {
198  return patternLinks_[parentId][sonId];
199  }
200  size_t getArrayPosition(int parentId, int sonId, size_t currentPosition)
201  {
202  return patternLinks_[parentId][sonId][currentPosition];
203  }
204 
206  {
207  return nodeData_[nodeId].getLikelihoodArray();
208  }
209 
211  {
212  return nodeData_[nodeId].getDLikelihoodArray();
213  }
214 
216  {
217  return nodeData_[nodeId].getD2LikelihoodArray();
218  }
219 
220  size_t getNumberOfDistinctSites() const { return nbDistinctSites_; }
221  size_t getNumberOfSites() const { return nbSites_; }
222  size_t getNumberOfStates() const { return nbStates_; }
223  size_t getNumberOfClasses() const { return nbClasses_; }
224 
225  void initLikelihoods(
226  const AlignmentDataInterface& sites,
227  const TransitionModelInterface& model);
228 
229 protected:
244  virtual void initLikelihoods(
245  const Node* node,
246  const AlignmentDataInterface& sequences,
247  const TransitionModelInterface& model);
248 
265  virtual std::unique_ptr<SitePatterns> initLikelihoodsWithPatterns(
266  const Node* node,
267  const AlignmentDataInterface& sequences,
268  const TransitionModelInterface& model);
269 };
270 } // end of namespace bpp.
271 #endif // BPP_PHYL_LEGACY_LIKELIHOOD_DRASRTREELIKELIHOODDATA_H
Partial implementation of the TreeLikelihoodData interface.
AbstractTreeLikelihoodData & operator=(const AbstractTreeLikelihoodData &atd)
std::vector< size_t > rootPatternLinks_
Links between sites and patterns.
std::shared_ptr< const TreeTemplate< Node > > tree_
discrete Rate Across Sites, (simple) Recursive likelihood data structure.
DRASRTreeLikelihoodData(std::shared_ptr< const TreeTemplate< Node >> tree, size_t nbClasses, bool usePatterns=true)
std::shared_ptr< AlignmentDataInterface > shrunkData_
size_t getArrayPosition(int parentId, int sonId, size_t currentPosition) const
size_t getArrayPosition(int parentId, int sonId, size_t currentPosition)
DRASRTreeLikelihoodData & operator=(const DRASRTreeLikelihoodData &data)
const DRASRTreeLikelihoodNodeData & getNodeData(int nodeId) const
VVVdouble & getDLikelihoodArray(int nodeId)
virtual std::unique_ptr< SitePatterns > initLikelihoodsWithPatterns(const Node *node, const AlignmentDataInterface &sequences, const TransitionModelInterface &model)
This method initializes the leaves according to a sequence file.
void initLikelihoods(const AlignmentDataInterface &sites, const TransitionModelInterface &model)
std::map< int, std::map< int, std::vector< size_t > > > patternLinks_
This map defines the pattern network.
size_t getRootArrayPosition(size_t currentPosition) const
std::vector< size_t > & getArrayPositions(int parentId, int sonId)
const std::vector< size_t > & getArrayPositions(int parentId, int sonId) const
DRASRTreeLikelihoodData * clone() const
VVVdouble & getLikelihoodArray(int nodeId)
void setTree(std::shared_ptr< const TreeTemplate< Node >> tree)
Set the tree associated to the data.
DRASRTreeLikelihoodData(const DRASRTreeLikelihoodData &data)
VVVdouble & getD2LikelihoodArray(int nodeId)
std::map< int, DRASRTreeLikelihoodNodeData > nodeData_
This contains all likelihood values used for computation.
DRASRTreeLikelihoodNodeData & getNodeData(int nodeId)
Likelihood data structure for a node.
const VVVdouble & getDLikelihoodArray() const
DRASRTreeLikelihoodNodeData * clone() const
const VVVdouble & getLikelihoodArray() const
DRASRTreeLikelihoodNodeData(const DRASRTreeLikelihoodNodeData &data)
void setNode(const Node *node)
Set the node associated to this data.
DRASRTreeLikelihoodNodeData & operator=(const DRASRTreeLikelihoodNodeData &data)
const VVVdouble & getD2LikelihoodArray() const
const Node * getNode() const
Get the node associated to this data structure.
The phylogenetic node class.
Definition: Node.h:59
Interface for all transition models.
TreeLikelihood partial data structure.
The phylogenetic tree class.
Definition: TreeTemplate.h:59
Defines the basic types of data flow nodes.
std::vector< VVdouble > VVVdouble