bpp-phyl3 3.0.0
AbstractTreeParsimonyScore.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: The Bio++ Development Group
2//
3// SPDX-License-Identifier: CECILL-2.1
4
6
7#include "../PatternTools.h"
8#include "../Tree/TreeTemplateTools.h"
10
11using namespace bpp;
12using namespace std;
13
14AbstractTreeParsimonyScore::AbstractTreeParsimonyScore(
15 std::shared_ptr<TreeTemplate<Node>> tree,
16 std::shared_ptr<const SiteContainerInterface> data,
17 bool verbose,
18 bool includeGaps) :
19 treePtr_(std::move(tree)),
20 data_(nullptr),
21 alphabet_(data->getAlphabet()),
22 statesMap_(nullptr),
23 nbStates_(0)
24{
25 statesMap_ = make_shared<CanonicalStateMap>(alphabet_, includeGaps);
26 nbStates_ = statesMap_->getNumberOfModelStates();
27 init_(data, verbose);
28}
29
30
32 std::shared_ptr<TreeTemplate<Node>> tree,
33 std::shared_ptr<const SiteContainerInterface> data,
34 std::shared_ptr<const StateMapInterface> statesMap,
35 bool verbose) :
36 treePtr_(std::move(tree)),
37 data_(nullptr),
38 alphabet_(data->getAlphabet()),
39 statesMap_(statesMap),
40 nbStates_(statesMap->getNumberOfModelStates())
41{
42 init_(data, verbose);
43}
44
45void AbstractTreeParsimonyScore::init_(std::shared_ptr<const SiteContainerInterface> data, bool verbose)
46{
47 if (treePtr_->isRooted())
48 {
49 if (verbose)
50 ApplicationTools::displayWarning("Tree has been unrooted.");
51 treePtr_->unroot();
52 }
54
55 // Sequences will be in the same order than in the tree:
56 shared_ptr<AlignmentDataInterface> tmp = PatternTools::getSequenceSubset(*data, *treePtr_->getRootNode());
57 data_ = dynamic_pointer_cast<const SiteContainerInterface>(tmp);
58 if (!data_)
59 throw Exception("AbstractTreeParsimonyScore::init_ : Data must be plain alignments.");
60
61 if (data_->getNumberOfSequences() == 1)
62 throw Exception("Error, only 1 sequence!");
63 if (data_->getNumberOfSequences() == 0)
64 throw Exception("Error, no sequence!");
65 if (data_->getAlphabet()->getSize() > 20)
66 throw Exception("Error, only alphabet with size <= 20 are supported. See the source file of AbstractTreeParsimonyScore.");
67}
68
69std::vector<unsigned int> AbstractTreeParsimonyScore::getScorePerSite() const
70{
71 vector<unsigned int> scores(data_->getNumberOfSites());
72 for (size_t i = 0; i < scores.size(); i++)
73 {
74 scores[i] = getScoreForSite(i);
75 }
76 return scores;
77}
void init_(std::shared_ptr< const SiteContainerInterface > data, bool verbose)
std::shared_ptr< TreeTemplate< Node > > treePtr_
AbstractTreeParsimonyScore(std::shared_ptr< TreeTemplate< Node > > tree, std::shared_ptr< const SiteContainerInterface > data, bool verbose, bool includeGaps)
std::vector< unsigned int > getScorePerSite() const override
Get the score for each site for the current tree, i.e. the total minimum number of changes in the tre...
std::shared_ptr< const SiteContainerInterface > data_
std::shared_ptr< const Alphabet > alphabet_
std::shared_ptr< const StateMapInterface > statesMap_
static void displayWarning(const std::string &text)
static std::unique_ptr< AlignmentDataInterface > getSequenceSubset(const AlignmentDataInterface &sequenceSet, const std::shared_ptr< N > node, const AssociationTreeGraphImplObserver< N, E, I > &tree)
Extract the sequences corresponding to a given subtree.
Definition: PatternTools.h:44
virtual unsigned int getScoreForSite(size_t site) const =0
Get the score for a given site for the current tree, i.e. the total minimum number of changes in the ...
static void deleteBranchLengths(Node &node)
Remove all the branch lengths of a subtree.
The phylogenetic tree class.
Definition: TreeTemplate.h:59
Defines the basic types of data flow nodes.