41 #include "../PatternTools.h" 42 #include "../TreeTemplateTools.h" 44 #include <Bpp/App/ApplicationTools.h> 45 #include <Bpp/Numeric/VectorTools.h> 54 const SiteContainer& data,
67 const SiteContainer& data,
81 ApplicationTools::displayTask(
"Initializing data structure");
82 parsimonyData_->init(data, getStateMap());
83 nbDistinctSites_ = parsimonyData_->getNumberOfDistinctSites();
86 ApplicationTools::displayTaskDone();
88 ApplicationTools::displayResult(
"Number of distinct sites",
89 TextTools::toString(nbDistinctSites_));
97 nbDistinctSites_(tp.nbDistinctSites_)
133 if (node->
isLeaf())
return;
145 for (
unsigned int i = 0; i < sonBitsets->size(); i++)
147 (*bitsets)[i] = (*sonBitsets)[i];
167 size_t nbNeighbors = node->
degree();
168 vector< const vector<Bitset>*> iBitsets;
169 vector< const vector<unsigned int>*> iScores;
170 for (
unsigned int k = 0; k < nbNeighbors; k++)
172 const Node* n = neighbors[k];
196 for (
unsigned int i = 0; i < sonBitsets->size(); i++)
198 (*bitsets)[i] = (*sonBitsets)[i];
223 size_t nbNeighbors = node->
degree();
224 vector< const vector<Bitset>*> iBitsets;
225 vector< const vector<unsigned int>*> iScores;
226 for (
unsigned int k = 0; k < nbNeighbors; k++)
228 const Node* n = neighbors[k];
242 size_t nbNeighbors = node->
degree();
245 vector< const vector<Bitset>*> iBitsets(nbNeighbors);
246 vector< const vector<unsigned int>*> iScores(nbNeighbors);
247 for (
unsigned int k = 0; k < nbNeighbors; k++)
249 const Node* n = neighbors[k];
260 unsigned int score = 0;
276 const vector<
const vector<Bitset>*>& iBitsets,
277 const vector<
const vector<unsigned int>*>& iScores,
278 vector<Bitset>& oBitsets,
279 vector<unsigned int>& oScores)
281 size_t nbPos = oBitsets.size();
282 size_t nbNodes = iBitsets.size();
283 if (iScores.size() != nbNodes)
284 throw Exception(
"DRTreeParsimonyScore::computeScores(); Error, input arrays must have the same length.");
286 throw Exception(
"DRTreeParsimonyScore::computeScores(); Error, input arrays must have a size >= 1.");
287 const vector<Bitset>* bitsets0 = iBitsets[0];
288 const vector<unsigned int>* scores0 = iScores[0];
289 for (
size_t i = 0; i < nbPos; i++)
291 oBitsets[i] = (*bitsets0)[i];
292 oScores[i] = (*scores0)[i];
294 for (
size_t k = 1; k < nbNodes; k++)
296 const vector<Bitset>* bitsetsk = iBitsets[k];
297 const vector<unsigned int>* scoresk = iScores[k];
298 for (
unsigned int i = 0; i < nbPos; i++)
300 Bitset bs = oBitsets[i] & (*bitsetsk)[i];
301 oScores[i] += (*scoresk)[i];
304 bs = oBitsets[i] | (*bitsetsk)[i];
315 const Node* son = getTreeP_()->getNode(nodeId);
316 if (!son->
hasFather())
throw NodePException(
"DRTreeParsimonyScore::testNNI(). Node 'son' must not be the root node.", son);
318 if (!parent->
hasFather())
throw NodePException(
"DRTreeParsimonyScore::testNNI(). Node 'parent' must not be the root node.", parent);
324 const Node* uncle = grandFather->
getSon(parentPosition > 1 ? parentPosition - 1 : 1 - parentPosition);
331 size_t nbParentNeighbors = parentNeighbors.size();
332 vector< const vector<Bitset>*> parentBitsets(nbParentNeighbors);
333 vector< const vector<unsigned int>*> parentScores(nbParentNeighbors);
334 for (
unsigned int k = 0; k < nbParentNeighbors; k++)
336 const Node* n = parentNeighbors[k];
345 size_t nbGrandFatherNeighbors = grandFatherNeighbors.size();
346 vector< const vector<Bitset>*> grandFatherBitsets(nbGrandFatherNeighbors);
347 vector< const vector<unsigned int>*> grandFatherScores(nbGrandFatherNeighbors);
348 for (
unsigned int k = 0; k < nbGrandFatherNeighbors; k++)
350 const Node* n = grandFatherNeighbors[k];
356 grandFatherBitsets.push_back(sonBitsets);
357 grandFatherScores.push_back(sonScores);
359 vector<Bitset> gfBitsets(sonBitsets->size());
360 vector<unsigned int> gfScores(sonScores->size());
362 computeScoresFromArrays(grandFatherBitsets, grandFatherScores, gfBitsets, gfScores);
365 parentBitsets.push_back(uncleBitsets);
366 parentScores.push_back(uncleScores);
367 parentBitsets.push_back(&gfBitsets);
368 parentScores.push_back(&gfScores);
370 vector<Bitset> pBitsets(sonBitsets->size());
371 vector<unsigned int> pScores(sonScores->size());
373 computeScoresFromArrays(parentBitsets, parentScores, pBitsets, pScores);
376 unsigned int score = 0;
377 for (
unsigned int i = 0; i < nbDistinctSites_; i++)
379 score += pScores[i] * parsimonyData_->getWeight(i);
381 return (
double)score - (double)getScore();
387 Node* son = getTreeP_()->getNode(nodeId);
388 if (!son->
hasFather())
throw NodePException(
"DRTreeParsimonyScore::doNNI(). Node 'son' must not be the root node.", son);
390 if (!parent->
hasFather())
throw NodePException(
"DRTreeParsimonyScore::doNNI(). Node 'parent' must not be the root node.", parent);
396 Node* uncle = grandFather->
getSon(parentPosition > 1 ? parentPosition - 1 : 1 - parentPosition);
Parsimony data structure for a node.
unsigned int getRootScore(size_t i) const
virtual void addSon(size_t pos, Node *node)
std::vector< Bitset > & getBitsetsArrayForNeighbor(int neighborId)
void setTree(const TreeTemplate< Node > *tree)
Set the tree associated to the data.
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.
double testNNI(int nodeId) const
Send the score of a NNI movement, without performing it.
virtual const Node * getSon(size_t pos) const
Double recursive implementation of interface TreeParsimonyScore.
size_t getRootArrayPosition(size_t site) const
unsigned int getWeight(size_t pos) const
DRTreeParsimonyScore & operator=(const DRTreeParsimonyScore &tp)
virtual const Node * getFather() const
Get the father of this node is there is one.
Interface for phylogenetic tree objects.
AbstractTreeParsimonyScore & operator=(const AbstractTreeParsimonyScore &tp)
virtual void computeScoresPreorder(const Node *)
Compute scores (preorder algorithm).
DRTreeParsimonyNodeData & getNodeData(int nodeId)
virtual bool hasFather() const
Tell if this node has a father node.
std::vector< unsigned int > & getRootScores()
virtual bool isLeaf() const
void init_(const SiteContainer &data, bool verbose)
void doNNI(int nodeId)
Perform a NNI movement.
std::vector< unsigned int > & getScoresArrayForNeighbor(int neighborId)
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.
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.
virtual int getId() const
Get the node's id.
const Node * getNode() const
Get the node associated to this data structure.
std::vector< const Node * > getNeighbors() const
virtual Node * removeSon(size_t pos)
std::vector< Bitset > & getRootBitsets()
virtual size_t getSonPosition(const Node *son) const
virtual void computeScoresPostorder(const Node *)
Compute scores (postorder algorithm).
const TreeTemplate< Node > * getTreeP_() const
DRTreeParsimonyData * clone() const
The phylogenetic node class.
virtual ~DRTreeParsimonyScore()
Partial implementation of the TreeParsimonyScore interface.
virtual void computeScores()
Compute all scores.
Parsimony data structure for double-recursive (DR) algorithm.
DRTreeParsimonyData * parsimonyData_
virtual size_t getNumberOfSons() const
unsigned int getScore() const
Get the score for the current tree, i.e. the total minimum number of changes in the tree...
General exception thrown when something is wrong with a particular node.
Map the states of a given alphabet which have a model state.
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.
virtual size_t degree() const
unsigned int getScoreForSite(size_t site) const
Get the score for a given site for the current tree, i.e. the total minimum number of changes in the ...
General exception thrown when something is wrong with a particular node.
DRTreeParsimonyLeafData & getLeafData(int nodeId)
DRTreeParsimonyScore(const Tree &tree, const SiteContainer &data, bool verbose=true, bool includeGaps=false)
virtual const Tree & getTree() const
Get the tree for wich scores are computed.