bpp-phyl3  3.0.0
DRTreeParsimonyData.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_PARSIMONY_DRTREEPARSIMONYDATA_H
6 #define BPP_PHYL_PARSIMONY_DRTREEPARSIMONYDATA_H
7 
8 
9 #include "../Model/StateMap.h"
11 
12 // From SeqLib
14 
15 // From the STL:
16 #include <bitset>
17 
18 namespace bpp
19 {
20 typedef std::bitset<21> Bitset; // 20AA + gaps, codon not lalowed so far :s
21 
35 {
36 private:
37  mutable std::map<int, std::vector<Bitset>> nodeBitsets_;
38  mutable std::map<int, std::vector<unsigned int>> nodeScores_;
39  const Node* node_;
40 
41 public:
43  nodeBitsets_(),
44  nodeScores_(),
45  node_(0)
46  {}
47 
51  node_(tpnd.node_)
52  {}
53 
55  {
57  nodeScores_ = tpnd.nodeScores_;
58  node_ = tpnd.node_;
59  return *this;
60  }
61 
62  DRTreeParsimonyNodeData* clone() const { return new DRTreeParsimonyNodeData(*this); }
63 
64 public:
65  const Node* getNode() const { return node_; }
66 
67  void setNode(const Node* node) { node_ = node; }
68 
69  std::vector<Bitset>& getBitsetsArrayForNeighbor(int neighborId)
70  {
71  return nodeBitsets_[neighborId];
72  }
73  const std::vector<Bitset>& getBitsetsArrayForNeighbor(int neighborId) const
74  {
75  return nodeBitsets_[neighborId];
76  }
77  std::vector<unsigned int>& getScoresArrayForNeighbor(int neighborId)
78  {
79  return nodeScores_[neighborId];
80  }
81  const std::vector<unsigned int>& getScoresArrayForNeighbor(int neighborId) const
82  {
83  return nodeScores_[neighborId];
84  }
85 
86  bool isNeighbor(int neighborId) const
87  {
88  return nodeBitsets_.find(neighborId) != nodeBitsets_.end();
89  }
90 
92  {
93  nodeBitsets_.erase(nodeBitsets_.begin(), nodeBitsets_.end());
94  nodeScores_.erase(nodeScores_.begin(), nodeScores_.end());
95  }
96 };
97 
109 {
110 private:
111  mutable std::vector<Bitset> leafBitsets_;
112  const Node* leaf_;
113 
114 public:
116  leafBitsets_(),
117  leaf_(0)
118  {}
119 
122  leaf_(tpld.leaf_)
123  {}
124 
126  {
127  leafBitsets_ = tpld.leafBitsets_;
128  leaf_ = tpld.leaf_;
129  return *this;
130  }
131 
132 
133  DRTreeParsimonyLeafData* clone() const { return new DRTreeParsimonyLeafData(*this); }
134 
135 public:
136  const Node* getNode() const { return leaf_; }
137  void setNode(const Node* node) { leaf_ = node; }
138 
139  std::vector<Bitset>& getBitsetsArray()
140  {
141  return leafBitsets_;
142  }
143  const std::vector<Bitset>& getBitsetsArray() const
144  {
145  return leafBitsets_;
146  }
147 };
148 
162 {
163 private:
164  mutable std::map<int, DRTreeParsimonyNodeData> nodeData_;
165  mutable std::map<int, DRTreeParsimonyLeafData> leafData_;
166  mutable std::vector<Bitset> rootBitsets_;
167  mutable std::vector<unsigned int> rootScores_;
168  std::unique_ptr<SiteContainerInterface> shrunkData_;
169  size_t nbSites_;
170  size_t nbStates_;
172 
173 public:
174  DRTreeParsimonyData(std::shared_ptr<const TreeTemplate<Node>> tree) :
176  nodeData_(),
177  leafData_(),
178  rootBitsets_(),
179  rootScores_(),
180  shrunkData_(nullptr),
181  nbSites_(0),
182  nbStates_(0),
184  {}
185 
187 
189 
190  virtual ~DRTreeParsimonyData() {}
191 
192  DRTreeParsimonyData* clone() const override { return new DRTreeParsimonyData(*this); }
193 
194 public:
204  void setTree(std::shared_ptr<const TreeTemplate<Node>> tree)
205  {
207  for (auto& it : nodeData_)
208  {
209  int id = it.second.getNode()->getId();
210  it.second.setNode(tree_->getNode(id));
211  }
212  for (auto& it : leafData_)
213  {
214  int id = it.second.getNode()->getId();
215  it.second.setNode(tree_->getNode(id));
216  }
217  }
218 
219  DRTreeParsimonyNodeData& nodeData(int nodeId) override
220  {
221  return nodeData_[nodeId];
222  }
223  const DRTreeParsimonyNodeData& nodeData(int nodeId) const override
224  {
225  return nodeData_[nodeId];
226  }
227 
229  {
230  return leafData_[nodeId];
231  }
232  const DRTreeParsimonyLeafData& leafData(int nodeId) const
233  {
234  return leafData_[nodeId];
235  }
236 
237  std::vector<Bitset>& getBitsetsArray(int nodeId, int neighborId)
238  {
239  return nodeData_[nodeId].getBitsetsArrayForNeighbor(neighborId);
240  }
241  const std::vector<Bitset>& getBitsetsArray(int nodeId, int neighborId) const
242  {
243  return nodeData_[nodeId].getBitsetsArrayForNeighbor(neighborId);
244  }
245 
246  std::vector<unsigned int>& getScoresArray(int nodeId, int neighborId)
247  {
248  return nodeData_[nodeId].getScoresArrayForNeighbor(neighborId);
249  }
250  const std::vector<unsigned int>& getScoresArray(int nodeId, int neighborId) const
251  {
252  return nodeData_[nodeId].getScoresArrayForNeighbor(neighborId);
253  }
254 
255  size_t getArrayPosition(int parentId, int sonId, size_t currentPosition) const override
256  {
257  return currentPosition;
258  }
259 
260  std::vector<Bitset>& getRootBitsets() { return rootBitsets_; }
261  const std::vector<Bitset>& getRootBitsets() const { return rootBitsets_; }
262  const Bitset& getRootBitset(size_t i) const { return rootBitsets_[i]; }
263 
264  std::vector<unsigned int>& getRootScores() { return rootScores_; }
265  const std::vector<unsigned int>& getRootScores() const { return rootScores_; }
266  unsigned int getRootScore(size_t i) const { return rootScores_[i]; }
267 
268  size_t getNumberOfDistinctSites() const { return nbDistinctSites_; }
269  size_t getNumberOfSites() const { return nbSites_; }
270  size_t getNumberOfStates() const { return nbStates_; }
271 
272  void init(std::shared_ptr<const SiteContainerInterface> sites,
273  std::shared_ptr<const StateMapInterface> stateMap);
274  void reInit();
275 
276 protected:
277  void init_(const Node* node,
278  std::shared_ptr<const SiteContainerInterface> sites,
279  std::shared_ptr<const StateMapInterface> stateMap);
280 
281  void reInit_(const Node* node);
282 };
283 } // end of namespace bpp.
284 #endif // BPP_PHYL_PARSIMONY_DRTREEPARSIMONYDATA_H
Partial implementation of the TreeParsimonyData interface.
const TreeTemplate< Node > & tree() const override
void setTree(std::shared_ptr< const TreeTemplate< Node >> tree)
std::shared_ptr< const TreeTemplate< Node > > tree_
Parsimony data structure for double-recursive (DR) algorithm.
const std::vector< unsigned int > & getRootScores() const
DRTreeParsimonyNodeData & nodeData(int nodeId) override
std::unique_ptr< SiteContainerInterface > shrunkData_
std::vector< unsigned int > & getScoresArray(int nodeId, int neighborId)
void reInit_(const Node *node)
const DRTreeParsimonyNodeData & nodeData(int nodeId) const override
size_t getNumberOfDistinctSites() const
std::vector< Bitset > rootBitsets_
unsigned int getRootScore(size_t i) const
const std::vector< Bitset > & getRootBitsets() const
void init(std::shared_ptr< const SiteContainerInterface > sites, std::shared_ptr< const StateMapInterface > stateMap)
DRTreeParsimonyData * clone() const override
void setTree(std::shared_ptr< const TreeTemplate< Node >> tree)
Set the tree associated to the data.
const std::vector< unsigned int > & getScoresArray(int nodeId, int neighborId) const
size_t getArrayPosition(int parentId, int sonId, size_t currentPosition) const override
void init_(const Node *node, std::shared_ptr< const SiteContainerInterface > sites, std::shared_ptr< const StateMapInterface > stateMap)
std::map< int, DRTreeParsimonyLeafData > leafData_
DRTreeParsimonyData & operator=(const DRTreeParsimonyData &data)
DRTreeParsimonyData(std::shared_ptr< const TreeTemplate< Node >> tree)
std::vector< Bitset > & getBitsetsArray(int nodeId, int neighborId)
std::vector< Bitset > & getRootBitsets()
const Bitset & getRootBitset(size_t i) const
DRTreeParsimonyLeafData & leafData(int nodeId)
const std::vector< Bitset > & getBitsetsArray(int nodeId, int neighborId) const
std::vector< unsigned int > & getRootScores()
std::vector< unsigned int > rootScores_
const DRTreeParsimonyLeafData & leafData(int nodeId) const
std::map< int, DRTreeParsimonyNodeData > nodeData_
Parsimony data structure for a leaf.
DRTreeParsimonyLeafData * clone() const
std::vector< Bitset > & getBitsetsArray()
const Node * getNode() const
Get the node associated to this data structure.
void setNode(const Node *node)
Set the node associated to this data.
DRTreeParsimonyLeafData & operator=(const DRTreeParsimonyLeafData &tpld)
const std::vector< Bitset > & getBitsetsArray() const
std::vector< Bitset > leafBitsets_
DRTreeParsimonyLeafData(const DRTreeParsimonyLeafData &tpld)
Parsimony data structure for a node.
DRTreeParsimonyNodeData(const DRTreeParsimonyNodeData &tpnd)
std::vector< Bitset > & getBitsetsArrayForNeighbor(int neighborId)
DRTreeParsimonyNodeData & operator=(const DRTreeParsimonyNodeData &tpnd)
const std::vector< Bitset > & getBitsetsArrayForNeighbor(int neighborId) const
const Node * getNode() const
Get the node associated to this data structure.
const std::vector< unsigned int > & getScoresArrayForNeighbor(int neighborId) const
bool isNeighbor(int neighborId) const
std::map< int, std::vector< Bitset > > nodeBitsets_
std::vector< unsigned int > & getScoresArrayForNeighbor(int neighborId)
DRTreeParsimonyNodeData * clone() const
void setNode(const Node *node)
Set the node associated to this data.
std::map< int, std::vector< unsigned int > > nodeScores_
The phylogenetic node class.
Definition: Node.h:59
TreeParsimonyScore node data structure.
The phylogenetic tree class.
Definition: TreeTemplate.h:59
Defines the basic types of data flow nodes.
std::bitset< 21 > Bitset