8#include "../PatternTools.h"
9#include "../Tree/TreeTemplateTools.h"
10#include "../Tree/TreeIterator.h"
18DRTreeParsimonyScore::DRTreeParsimonyScore(
20 std::shared_ptr<const SiteContainerInterface> data,
32 std::shared_ptr<const SiteContainerInterface> data,
33 std::shared_ptr<const StateMapInterface> statesMap,
61 nbDistinctSites_(tp.nbDistinctSites_)
70 AbstractTreeParsimonyScore::operator=(tp);
108 for (
unsigned int i = 0; i < sonBitsets->size(); i++)
110 (*bitsets)[i] = (*sonBitsets)[i];
130 size_t nbNeighbors = node->
degree();
131 vector< const vector<Bitset>*> iBitsets;
132 vector< const vector<unsigned int>*> iScores;
133 for (
unsigned int k = 0; k < nbNeighbors; k++)
135 const Node* n = neighbors[k];
160 for (
unsigned int i = 0; i < sonBitsets->size(); i++)
162 (*bitsets)[i] = (*sonBitsets)[i];
187 size_t nbNeighbors = node->
degree();
188 vector< const vector<Bitset>*> iBitsets;
189 vector< const vector<unsigned int>*> iScores;
190 for (
unsigned int k = 0; k < nbNeighbors; k++)
192 const Node* n = neighbors[k];
206 size_t nbNeighbors = node->
degree();
209 vector< const vector<Bitset>*> iBitsets(nbNeighbors);
210 vector< const vector<unsigned int>*> iScores(nbNeighbors);
211 for (
unsigned int k = 0; k < nbNeighbors; k++)
213 const Node* n = neighbors[k];
224 unsigned int score = 0;
240 const vector<
const vector<Bitset>*>& iBitsets,
241 const vector<
const vector<unsigned int>*>& iScores,
242 vector<Bitset>& oBitsets,
243 vector<unsigned int>& oScores)
245 size_t nbPos = oBitsets.size();
246 size_t nbNodes = iBitsets.size();
247 if (iScores.size() != nbNodes)
248 throw Exception(
"DRTreeParsimonyScore::computeScores(); Error, input arrays must have the same length.");
250 throw Exception(
"DRTreeParsimonyScore::computeScores(); Error, input arrays must have a size >= 1.");
251 const vector<Bitset>* bitsets0 = iBitsets[0];
252 const vector<unsigned int>* scores0 = iScores[0];
253 for (
size_t i = 0; i < nbPos; i++)
255 oBitsets[i] = (*bitsets0)[i];
256 oScores[i] = (*scores0)[i];
258 for (
size_t k = 1; k < nbNodes; k++)
260 const vector<Bitset>* bitsetsk = iBitsets[k];
261 const vector<unsigned int>* scoresk = iScores[k];
262 for (
unsigned int i = 0; i < nbPos; i++)
264 Bitset bs = oBitsets[i] & (*bitsetsk)[i];
265 oScores[i] += (*scoresk)[i];
268 bs = oBitsets[i] | (*bitsetsk)[i];
281 throw NodePException(
"DRTreeParsimonyScore::testNNI(). Node 'son' must not be the root node.", son);
284 throw NodePException(
"DRTreeParsimonyScore::testNNI(). Node 'parent' must not be the root node.", parent);
290 const Node* uncle = grandFather->
getSon(parentPosition > 1 ? parentPosition - 1 : 1 - parentPosition);
297 size_t nbParentNeighbors = parentNeighbors.size();
298 vector< const vector<Bitset>*> parentBitsets(nbParentNeighbors);
299 vector< const vector<unsigned int>*> parentScores(nbParentNeighbors);
300 for (
unsigned int k = 0; k < nbParentNeighbors; k++)
302 const Node* n = parentNeighbors[k];
311 size_t nbGrandFatherNeighbors = grandFatherNeighbors.size();
312 vector< const vector<Bitset>*> grandFatherBitsets(nbGrandFatherNeighbors);
313 vector< const vector<unsigned int>*> grandFatherScores(nbGrandFatherNeighbors);
314 for (
unsigned int k = 0; k < nbGrandFatherNeighbors; k++)
316 const Node* n = grandFatherNeighbors[k];
322 grandFatherBitsets.push_back(sonBitsets);
323 grandFatherScores.push_back(sonScores);
325 vector<Bitset> gfBitsets(sonBitsets->size());
326 vector<unsigned int> gfScores(sonScores->size());
331 parentBitsets.push_back(uncleBitsets);
332 parentScores.push_back(uncleScores);
333 parentBitsets.push_back(&gfBitsets);
334 parentScores.push_back(&gfScores);
336 vector<Bitset> pBitsets(sonBitsets->size());
337 vector<unsigned int> pScores(sonScores->size());
342 unsigned int score = 0;
347 return (
double)score - (double)
getScore();
356 throw NodePException(
"DRTreeParsimonyScore::doNNI(). Node 'son' must not be the root node.", son);
359 throw NodePException(
"DRTreeParsimonyScore::doNNI(). Node 'parent' must not be the root node.", parent);
365 Node* uncle = grandFather->
getSon(parentPosition > 1 ? parentPosition - 1 : 1 - parentPosition);
394 map< int, vector<size_t>> nodeToPossibleStates;
396 vector<Bitset> nodeBitsets;
397 for (
size_t n = 0; n < nodes.size(); ++n)
400 if (nodes[n]->isLeaf())
402 nodeBitsets =
parsimonyData_->leafData(nodes[n]->getId()).getBitsetsArray();
404 else if (nodes[n]->hasFather())
406 nodeBitsets =
parsimonyData_->nodeData(nodes[n]->getFather()->getId()).getBitsetsArrayForNeighbor(nodes[n]->getId());
411 vector<Node*> neighbors = nodes[n]->getNeighbors();
412 for (
size_t nn = 0; nn < neighbors.size(); ++nn)
414 if (!neighbors[nn]->isLeaf())
416 neighborId = neighbors[nn]->getId();
420 nodeBitsets =
parsimonyData_->nodeData(neighborId).getBitsetsArrayForNeighbor(nodes[n]->getId());
423 vector<size_t> possibleStates;
426 if (nodeBitsets[0].test(s))
428 possibleStates.push_back(s);
431 nodeToPossibleStates[nodes[n]->getId()] = possibleStates;
436 for (
const Node* node = treeIt->
begin(); node != treeIt->
end(); node = treeIt->
next())
439 vector<size_t> possibleStates = nodeToPossibleStates[node->getId()];
440 if (possibleStates.size() == 1)
442 nodeState = possibleStates[0];
444 else if (node->hasFather())
Partial implementation of the TreeParsimonyScore interface.
std::shared_ptr< const StateMapInterface > getStateMap() const override
Get the state map associated to this instance.
virtual std::shared_ptr< const TreeTemplate< Node > > getTreeTemplate() const
virtual TreeTemplate< Node > & treeTemplate_()
virtual const TreeTemplate< Node > & treeTemplate() const
const StateMapInterface & stateMap() const override
Get the state map associated to this instance.
Parsimony data structure for double-recursive (DR) algorithm.
Parsimony data structure for a node.
const Node * getNode() const
Get the node associated to this data structure.
std::vector< Bitset > & getBitsetsArrayForNeighbor(int neighborId)
std::vector< unsigned int > & getScoresArrayForNeighbor(int neighborId)
Double recursive implementation of interface TreeParsimonyScore.
static void computeScoresPostorderForNode(const DRTreeParsimonyNodeData &pData, std::vector< Bitset > &rBitsets, std::vector< unsigned int > &rScores)
Compute bitsets and scores for each site for a node, in postorder.
void init_(std::shared_ptr< const SiteContainerInterface > data, bool verbose)
static void computeScoresFromArrays(const std::vector< const std::vector< Bitset > * > &iBitsets, const std::vector< const std::vector< unsigned int > * > &iScores, std::vector< Bitset > &oBitsets, std::vector< unsigned int > &oScores)
Compute bitsets and scores from an array of arrays.
unsigned int getScoreForSite(size_t site) const override
Get the score for a given site for the current tree, i.e. the total minimum number of changes in the ...
DRTreeParsimonyScore(std::shared_ptr< TreeTemplate< Node > > tree, std::shared_ptr< const SiteContainerInterface > data, bool verbose=true, bool includeGaps=false)
virtual void computeScoresPreorder(const Node *)
Compute scores (preorder algorithm).
static std::string PARSIMONY_SOLUTION_STATE
std::unique_ptr< DRTreeParsimonyData > parsimonyData_
size_t getNodeState(const Node *node)
Extracts the state of a node in a mapping.
DRTreeParsimonyScore & operator=(const DRTreeParsimonyScore &tp)
unsigned int getScore() const override
Get the score for the current tree, i.e. the total minimum number of changes in the tree.
void setNodeState(Node *node, size_t state)
Sets the state of a node in a mapping.
virtual void computeScores()
Compute all scores.
double testNNI(int nodeId) const override
Send the score of a NNI movement, without performing it.
static void computeScoresForNode(const DRTreeParsimonyNodeData &pData, std::vector< Bitset > &rBitsets, std::vector< unsigned int > &rScores)
Compute bitsets and scores for each site for a node, in all directions.
virtual void computeScoresPostorder(const Node *)
Compute scores (postorder algorithm).
virtual ~DRTreeParsimonyScore()
void doNNI(int nodeId) override
Perform a NNI movement.
static void computeScoresPreorderForNode(const DRTreeParsimonyNodeData &pData, const Node *source, std::vector< Bitset > &rBitsets, std::vector< unsigned int > &rScores)
Compute bitsets and scores for each site for a node, in preorder.
void computeSolution()
Compute a maximum parsimony solution in DELTRAN manner.
General exception thrown when something is wrong with a particular node.
The phylogenetic node class.
virtual Node * removeSon(size_t pos)
virtual int getId() const
Get the node's id.
virtual Clonable * getNodeProperty(const std::string &name)
virtual void addSon(size_t pos, Node *node)
virtual void setNodeProperty(const std::string &name, const Clonable &property)
Set/add a node property.
virtual const Node * getFather() const
Get the father of this node is there is one.
virtual const Node * getSon(size_t pos) const
virtual bool isLeaf() const
virtual size_t getSonPosition(const Node *son) const
std::vector< const Node * > getNeighbors() const
virtual bool hasFather() const
Tell if this node has a father node.
virtual size_t getNumberOfSons() const
virtual size_t degree() const
virtual size_t getNumberOfModelStates() const =0
virtual const Node * next()=0
The phylogenetic tree class.
std::string toString(T t)
Defines the basic types of data flow nodes.