8 #include "../PatternTools.h"
9 #include "../Tree/TreeTemplateTools.h"
19 std::shared_ptr<const SiteContainerInterface> data,
31 std::shared_ptr<const SiteContainerInterface> data,
32 std::shared_ptr<const StateMapInterface> statesMap,
60 nbDistinctSites_(tp.nbDistinctSites_)
69 AbstractTreeParsimonyScore::operator=(tp);
107 for (
unsigned int i = 0; i < sonBitsets->size(); i++)
109 (*bitsets)[i] = (*sonBitsets)[i];
129 size_t nbNeighbors = node->
degree();
130 vector< const vector<Bitset>*> iBitsets;
131 vector< const vector<unsigned int>*> iScores;
132 for (
unsigned int k = 0; k < nbNeighbors; k++)
134 const Node* n = neighbors[k];
159 for (
unsigned int i = 0; i < sonBitsets->size(); i++)
161 (*bitsets)[i] = (*sonBitsets)[i];
186 size_t nbNeighbors = node->
degree();
187 vector< const vector<Bitset>*> iBitsets;
188 vector< const vector<unsigned int>*> iScores;
189 for (
unsigned int k = 0; k < nbNeighbors; k++)
191 const Node* n = neighbors[k];
205 size_t nbNeighbors = node->
degree();
208 vector< const vector<Bitset>*> iBitsets(nbNeighbors);
209 vector< const vector<unsigned int>*> iScores(nbNeighbors);
210 for (
unsigned int k = 0; k < nbNeighbors; k++)
212 const Node* n = neighbors[k];
223 unsigned int score = 0;
239 const vector<
const vector<Bitset>*>& iBitsets,
240 const vector<
const vector<unsigned int>*>& iScores,
241 vector<Bitset>& oBitsets,
242 vector<unsigned int>& oScores)
244 size_t nbPos = oBitsets.size();
245 size_t nbNodes = iBitsets.size();
246 if (iScores.size() != nbNodes)
247 throw Exception(
"DRTreeParsimonyScore::computeScores(); Error, input arrays must have the same length.");
249 throw Exception(
"DRTreeParsimonyScore::computeScores(); Error, input arrays must have a size >= 1.");
250 const vector<Bitset>* bitsets0 = iBitsets[0];
251 const vector<unsigned int>* scores0 = iScores[0];
252 for (
size_t i = 0; i < nbPos; i++)
254 oBitsets[i] = (*bitsets0)[i];
255 oScores[i] = (*scores0)[i];
257 for (
size_t k = 1; k < nbNodes; k++)
259 const vector<Bitset>* bitsetsk = iBitsets[k];
260 const vector<unsigned int>* scoresk = iScores[k];
261 for (
unsigned int i = 0; i < nbPos; i++)
263 Bitset bs = oBitsets[i] & (*bitsetsk)[i];
264 oScores[i] += (*scoresk)[i];
267 bs = oBitsets[i] | (*bitsetsk)[i];
280 throw NodePException(
"DRTreeParsimonyScore::testNNI(). Node 'son' must not be the root node.", son);
283 throw NodePException(
"DRTreeParsimonyScore::testNNI(). Node 'parent' must not be the root node.", parent);
289 const Node* uncle = grandFather->
getSon(parentPosition > 1 ? parentPosition - 1 : 1 - parentPosition);
296 size_t nbParentNeighbors = parentNeighbors.size();
297 vector< const vector<Bitset>*> parentBitsets(nbParentNeighbors);
298 vector< const vector<unsigned int>*> parentScores(nbParentNeighbors);
299 for (
unsigned int k = 0; k < nbParentNeighbors; k++)
301 const Node* n = parentNeighbors[k];
310 size_t nbGrandFatherNeighbors = grandFatherNeighbors.size();
311 vector< const vector<Bitset>*> grandFatherBitsets(nbGrandFatherNeighbors);
312 vector< const vector<unsigned int>*> grandFatherScores(nbGrandFatherNeighbors);
313 for (
unsigned int k = 0; k < nbGrandFatherNeighbors; k++)
315 const Node* n = grandFatherNeighbors[k];
321 grandFatherBitsets.push_back(sonBitsets);
322 grandFatherScores.push_back(sonScores);
324 vector<Bitset> gfBitsets(sonBitsets->size());
325 vector<unsigned int> gfScores(sonScores->size());
330 parentBitsets.push_back(uncleBitsets);
331 parentScores.push_back(uncleScores);
332 parentBitsets.push_back(&gfBitsets);
333 parentScores.push_back(&gfScores);
335 vector<Bitset> pBitsets(sonBitsets->size());
336 vector<unsigned int> pScores(sonScores->size());
341 unsigned int score = 0;
346 return (
double)score - (double)
getScore();
355 throw NodePException(
"DRTreeParsimonyScore::doNNI(). Node 'son' must not be the root node.", son);
358 throw NodePException(
"DRTreeParsimonyScore::doNNI(). Node 'parent' must not be the root node.", parent);
364 Node* uncle = grandFather->
getSon(parentPosition > 1 ? parentPosition - 1 : 1 - parentPosition);
Partial implementation of the TreeParsimonyScore interface.
virtual const TreeTemplate< Node > & treeTemplate() const
virtual std::shared_ptr< const TreeTemplate< Node > > getTreeTemplate() const
virtual TreeTemplate< Node > & treeTemplate_()
std::shared_ptr< const StateMapInterface > getStateMap() const override
Get the state map associated to this instance.
Parsimony data structure for double-recursive (DR) algorithm.
Parsimony data structure for a node.
std::vector< Bitset > & getBitsetsArrayForNeighbor(int neighborId)
const Node * getNode() const
Get the node associated to this data structure.
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 ...
virtual void computeScoresPreorder(const Node *)
Compute scores (preorder algorithm).
DRTreeParsimonyScore(std::shared_ptr< TreeTemplate< Node >> tree, std::shared_ptr< const SiteContainerInterface > data, bool verbose=true, bool includeGaps=false)
std::unique_ptr< DRTreeParsimonyData > parsimonyData_
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.
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.
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 void addSon(size_t pos, Node *node)
virtual const Node * getSon(size_t pos) const
virtual const Node * getFather() const
Get the father of this node is there is one.
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
The phylogenetic tree class.
std::string toString(T t)
Defines the basic types of data flow nodes.