bpp-phyl3 3.0.0
DRTreeParsimonyData.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: The Bio++ Development Group
2//
3// SPDX-License-Identifier: CECILL-2.1
4
5#include "../SitePatterns.h"
7
8// From SeqLib:
10
11using namespace bpp;
12using namespace std;
13
14/******************************************************************************/
15
16DRTreeParsimonyData::DRTreeParsimonyData(const DRTreeParsimonyData& data) :
18 nodeData_(data.nodeData_),
19 leafData_(data.leafData_),
20 rootBitsets_(data.rootBitsets_),
21 rootScores_(data.rootScores_),
22 shrunkData_(nullptr),
23 nbSites_(data.nbSites_),
24 nbStates_(data.nbStates_),
25 nbDistinctSites_(data.nbDistinctSites_)
26{
27 if (data.shrunkData_)
28 shrunkData_.reset(data.shrunkData_->clone());
29}
30
31/******************************************************************************/
32
34{
36 nodeData_ = data.nodeData_;
37 leafData_ = data.leafData_;
40 if (data.shrunkData_)
41 shrunkData_.reset(data.shrunkData_->clone());
42 else
43 shrunkData_.reset(nullptr);
44 nbSites_ = data.nbSites_;
45 nbStates_ = data.nbStates_;
47 return *this;
48}
49
50/******************************************************************************/
51
53 shared_ptr<const SiteContainerInterface> sites,
54 shared_ptr<const StateMapInterface> stateMap)
55{
56 nbStates_ = stateMap->getNumberOfModelStates();
57 nbSites_ = sites->getNumberOfSites();
58
59 SitePatterns pattern(*sites);
60
61 auto tmp = pattern.getSites();
62 shrunkData_.reset(dynamic_cast<SiteContainerInterface*>(tmp.release()));
63
64 if (shrunkData_ == nullptr)
65 throw Exception("DRTreeParsimonyData::init : Data must be plain alignments.");
66
67 rootWeights_ = pattern.getWeights();
68
69 rootPatternLinks_.resize(size_t(pattern.getIndices().size()));
70 SitePatterns::IndicesType::Map(&rootPatternLinks_[0], pattern.getIndices().size()) = pattern.getIndices();
71 nbDistinctSites_ = shrunkData_->getNumberOfSites();
72
73 // Init data:
74 // Clone data for more efficiency on sequences access:
75 auto sequences = make_shared<AlignedSequenceContainer>(*shrunkData_);
76 init_(tree().getRootNode(), sequences, stateMap);
77
78 // Now initialize root arrays:
81}
82
83/******************************************************************************/
84
86 const Node* node,
87 shared_ptr<const SiteContainerInterface> sites,
88 shared_ptr<const StateMapInterface> stateMap)
89{
90 auto alphabet = sites->getAlphabet();
91 if (node->isLeaf())
92 {
93 const Sequence* seq;
94 try
95 {
96 seq = &sites->sequence(node->getName());
97 }
98 catch (SequenceNotFoundException& snfe)
99 {
100 throw SequenceNotFoundException("DRTreeParsimonyData:init(node, sites). Leaf name in tree not found in site container: ", (node->getName()));
101 }
103 vector<Bitset>* leafData_bitsets = &leafData->getBitsetsArray();
104 leafData->setNode(node);
105
106 leafData_bitsets->resize(nbDistinctSites_);
107
108 for (unsigned int i = 0; i < nbDistinctSites_; i++)
109 {
110 Bitset* leafData_bitsets_i = &(*leafData_bitsets)[i];
111 for (unsigned int s = 0; s < nbStates_; s++)
112 {
113 // Leaves bitset are set to 1 if the char correspond to the site in the sequence,
114 // otherwise value set to 0:
115 int state = seq->getValue(i);
116 vector<int> states = alphabet->getAlias(state);
117 for (size_t j = 0; j < states.size(); j++)
118 {
119 if (stateMap->getAlphabetStateAsInt(s) == states[j])
120 (*leafData_bitsets_i)[s].flip();
121 }
122 }
123 }
124 }
125 else
126 {
128 nodeData->setNode(node);
130
131 int nbSons = static_cast<int>(node->getNumberOfSons());
132
133 for (int n = (node->hasFather() ? -1 : 0); n < nbSons; n++)
134 {
135 const Node* neighbor = (*node)[n];
136 vector<Bitset>* neighborData_bitsets = &nodeData->getBitsetsArrayForNeighbor(neighbor->getId());
137 vector<unsigned int>* neighborData_scores = &nodeData->getScoresArrayForNeighbor(neighbor->getId());
138
139 neighborData_bitsets->resize(nbDistinctSites_);
140 neighborData_scores->resize(nbDistinctSites_);
141 }
142 }
143
144 // We initialize each son node:
145 size_t nbSonNodes = node->getNumberOfSons();
146 for (unsigned int l = 0; l < nbSonNodes; l++)
147 {
148 // For each son node,
149 init_(node->getSon(l), sites, stateMap);
150 }
151}
152
153/******************************************************************************/
154
156{
157 reInit_(tree().getRootNode());
158}
159
160/******************************************************************************/
161
163{
164 if (node->isLeaf())
165 {
166 return;
167 }
168 else
169 {
171 nodeData->setNode(node);
173
174 int nbSons = static_cast<int>(node->getNumberOfSons());
175
176 for (int n = (node->hasFather() ? -1 : 0); n < nbSons; n++)
177 {
178 const Node* neighbor = (*node)[n];
179 vector<Bitset>* neighborData_bitsets = &nodeData->getBitsetsArrayForNeighbor(neighbor->getId());
180 vector<unsigned int>* neighborData_scores = &nodeData->getScoresArrayForNeighbor(neighbor->getId());
181
182 neighborData_bitsets->resize(nbDistinctSites_);
183 neighborData_scores->resize(nbDistinctSites_);
184 }
185 }
186
187 // We initialize each son node:
188 size_t nbSonNodes = node->getNumberOfSons();
189 for (unsigned int l = 0; l < nbSonNodes; l++)
190 {
191 // For each son node,
192 reInit_(node->getSon(l));
193 }
194}
195
196/******************************************************************************/
const int & getValue(size_t pos) const override
Partial implementation of the TreeParsimonyData interface.
AbstractTreeParsimonyData & operator=(const AbstractTreeParsimonyData &atpd)
const TreeTemplate< Node > & tree() const override
std::vector< unsigned int > rootWeights_
Parsimony data structure for double-recursive (DR) algorithm.
std::unique_ptr< SiteContainerInterface > shrunkData_
void reInit_(const Node *node)
std::vector< Bitset > rootBitsets_
DRTreeParsimonyNodeData & nodeData(int nodeId) override
void init(std::shared_ptr< const SiteContainerInterface > sites, std::shared_ptr< const StateMapInterface > stateMap)
DRTreeParsimonyLeafData & leafData(int nodeId)
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)
std::vector< unsigned int > rootScores_
std::map< int, DRTreeParsimonyNodeData > nodeData_
Parsimony data structure for a leaf.
std::vector< Bitset > & getBitsetsArray()
void setNode(const Node *node)
Set the node associated to this data.
Parsimony data structure for a node.
std::vector< Bitset > & getBitsetsArrayForNeighbor(int neighborId)
std::vector< unsigned int > & getScoresArrayForNeighbor(int neighborId)
void setNode(const Node *node)
Set the node associated to this data.
The phylogenetic node class.
Definition: Node.h:59
virtual std::string getName() const
Get the name associated to this node, if there is one, otherwise throw a NodeException.
Definition: Node.h:203
virtual int getId() const
Get the node's id.
Definition: Node.h:170
virtual const Node * getSon(size_t pos) const
Definition: Node.h:362
virtual bool isLeaf() const
Definition: Node.h:679
virtual bool hasFather() const
Tell if this node has a father node.
Definition: Node.h:346
virtual size_t getNumberOfSons() const
Definition: Node.h:355
Data structure for site patterns.
Definition: SitePatterns.h:35
const IndicesType & getIndices() const
Definition: SitePatterns.h:103
const std::vector< unsigned int > & getWeights() const
Definition: SitePatterns.h:99
std::unique_ptr< AlignmentDataInterface > getSites() const
Defines the basic types of data flow nodes.
std::bitset< 21 > Bitset