14 vector<size_t> ancestors(nbDistinctSites_);
15 probs.resize(nbDistinctSites_);
18 if (likelihood_->tree().isLeaf(nodeId))
20 VVdouble larray = likelihood_->likelihoodData().getLeafLikelihoods(nodeId);
21 for (
size_t i = 0; i < nbDistinctSites_; ++i)
24 probs_i->resize(nbStates_);
34 likelihood_->computeLikelihoodAtNode(nodeId, larray);
35 for (
size_t i = 0; i < nbDistinctSites_; i++)
39 probs_i->resize(nbStates_);
40 for (
size_t c = 0; c < nbClasses_; c++)
42 Vdouble* larray_i_c = &(*larray_i)[c];
43 for (
size_t x = 0; x < nbStates_; x++)
45 (*probs_i)[x] += (*larray_i_c)[x] * r_[c] / l_[i];
52 for (
size_t j = 0; j < nbStates_; j++)
54 cumProb += (*probs_i)[j];
71 map<int, vector<size_t>> ancestors;
73 auto data = make_unique<AlignedSequenceContainer>(
dynamic_cast<const SiteContainerInterface&
>(likelihood_->likelihoodData().shrunkData()));
74 recursiveMarginalAncestralStates(tree_.getRootNode(), ancestors, *data);
80 string name = tree_.hasNodeName(nodeId) ? tree_.getNodeName(nodeId) : (
"" +
TextTools::toString(nodeId));
81 const vector<size_t>& rootPatternLinks = likelihood_->likelihoodData().getRootArrayPositions();
82 auto model = likelihood_->getModelForSite(tree_.getNodesId()[0], 0);
83 vector<size_t> states;
84 vector<int> allStates(nbSites_);
88 states = getAncestralStatesForNode(nodeId, patternedProbs, sample);
89 probs->resize(nbSites_);
90 for (
size_t i = 0; i < nbSites_; i++)
92 allStates[i] = model->getAlphabetStateAsInt(states[rootPatternLinks[i]]);
93 (*probs)[i] = patternedProbs[rootPatternLinks[i]];
98 states = getAncestralStatesForNode(nodeId, patternedProbs, sample);
99 for (
size_t i = 0; i < nbSites_; i++)
101 allStates[i] = model->getAlphabetStateAsInt(states[rootPatternLinks[i]]);
104 std::shared_ptr<const Alphabet> alpha = alphabet_;
105 return make_unique<Sequence>(name, allStates, alpha);
110 map<
int, vector<size_t>>& ancestors,
116 vector<size_t>* v = &ancestors[node->
getId()];
117 v->resize(seq.
size());
121 auto model = likelihood_->getModelForSite(tree_.getNodesId()[0], 0);
122 for (
size_t i = 0; i < seq.
size(); i++)
124 (*v)[i] = model->getModelStates(seq[i])[0];
129 ancestors[node->
getId()] = getAncestralStatesForNode(node->
getId());
132 recursiveMarginalAncestralStates(node->
getSon(i), ancestors, data);
139 unique_ptr<SiteContainerInterface> asc = make_unique<AlignedSequenceContainer>(alphabet_);
140 vector<int> ids = tree_.getInnerNodesId();
141 for (
size_t i = 0; i < ids.size(); i++)
143 auto seq = getAncestralSequenceForNode(ids[i],
nullptr, sample);
144 asc->addSequence(seq->getName(), seq);
virtual size_t size() const=0
std::map< int, std::vector< size_t > > getAllAncestralStates() const override
Get all ancestral states for all nodes.
std::unique_ptr< Sequence > getAncestralSequenceForNode(int nodeId, VVdouble *probs, bool sample) const
Get the ancestral sequence for a given node.
std::vector< size_t > getAncestralStatesForNode(int nodeId, VVdouble &probs, bool sample) const
Get ancestral states for a given node as a vector of int.
void recursiveMarginalAncestralStates(const Node *node, std::map< int, std::vector< size_t >> &ancestors, AlignedSequenceContainer &data) const
std::unique_ptr< SiteContainerInterface > getAncestralSequences() const override
Get all the ancestral sequences for all nodes.
The phylogenetic node class.
virtual std::string getName() const
Get the name associated to this node, if there is one, otherwise throw a NodeException.
virtual int getId() const
Get the node's id.
virtual const Node * getSon(size_t pos) const
virtual bool isLeaf() const
virtual size_t getNumberOfSons() const
const SequenceType & sequence(const std::string &sequenceKey) const override
std::string toString(T t)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
std::vector< VVdouble > VVVdouble
std::vector< Vdouble > VVdouble