15 vector<size_t> ancestors(nbSites_);
17 auto vv = likelihood_->getLikelihoodsAtNode(nodeId)->targetValue();
19 probs.resize(nbSites_);
21 for (uint i = 0; i < static_cast<uint>(nbSites_); ++i)
23 copyEigenToBpp(vv.col(i) / vv.col(i).sum(), probs[
static_cast<size_t>(i)]);
28 for (
size_t i = 0; i < nbSites_; ++i)
30 const auto& coli = probs[i];
32 for (
size_t j = 0; j < nbStates_; ++j)
46 for (
size_t i = 0; i < nbSites_; ++i)
48 vv.col(Eigen::Index(i)).maxCoeff(&pos);
57 map<uint, vector<size_t>> ancestors;
59 shared_ptr<AlignmentDataInterface> data = make_shared<AlignedSequenceContainer>(
dynamic_cast<const SiteContainerInterface&
>(likelihood_->shrunkData()));
60 recursiveMarginalAncestralStates(tree_->getRoot(), ancestors, *data);
66 string name = tree_->getNode(nodeId)->hasName() ? tree_->getNode(nodeId)->getName() : (
"" +
TextTools::toString(nodeId));
67 vector<int> allStates(nbSites_);
69 const auto& stateMap = likelihood_->stateMap();
75 auto states = getAncestralStatesForNode(nodeId, *probs, sample);
76 for (
size_t i = 0; i < nbSites_; ++i)
78 allStates[i] = stateMap.getAlphabetStateAsInt(states[i]);
83 auto states = getAncestralStatesForNode(nodeId, patternedProbs, sample);
84 for (
size_t i = 0; i < nbSites_; ++i)
86 allStates[i] = stateMap.getAlphabetStateAsInt(states[i]);
90 return make_unique<Sequence>(name, allStates, alphabet_);
94 const std::shared_ptr<PhyloNode> node,
95 map<uint, vector<size_t>>& ancestors,
98 if (tree_->isLeaf(node))
103 const Sequence& seq = sc.sequence(node->getName());
104 vector<size_t>* v = &ancestors[tree_->getNodeIndex(node)];
105 v->resize(seq.
size());
109 const auto& stateMap = likelihood_->stateMap();
110 for (
size_t i = 0; i < seq.
size(); ++i)
112 (*v)[i] = stateMap.getModelStates(seq[i])[0];
117 ancestors[tree_->getNodeIndex(node)] = getAncestralStatesForNode(tree_->getNodeIndex(node));
122 ancestors[tree_->getNodeIndex(node)] = getAncestralStatesForNode(tree_->getNodeIndex(node));
123 vector<shared_ptr<PhyloNode>> vsons = tree_->getSons(node);
125 for (
size_t i = 0; i < vsons.size(); i++)
127 recursiveMarginalAncestralStates(vsons[i], ancestors, data);
134 auto asc = make_unique<AlignedSequenceContainer>(alphabet_);
135 vector<shared_ptr<PhyloNode>> inNodes = tree_->getAllInnerNodes();
136 for (
size_t i = 0; i < inNodes.size(); ++i)
138 auto seq = getAncestralSequenceForNode(tree_->getNodeIndex(inNodes[i]),
nullptr, sample);
139 asc->addSequence(seq->getName(), seq);
virtual size_t size() const=0
std::unique_ptr< AlignedSequenceContainer > getAncestralSequences() const override
Get all the ancestral sequences for all nodes.
void recursiveMarginalAncestralStates(const std::shared_ptr< PhyloNode > node, std::map< uint, std::vector< size_t >> &ancestors, AlignmentDataInterface &data) const
std::unique_ptr< Sequence > getAncestralSequenceForNode(uint nodeId, VVdouble *probs, bool sample) const
Get an ancestral sequence for a given node.
std::map< uint, std::vector< size_t > > getAllAncestralStates() const override
Get all ancestral states for all nodes.
std::vector< size_t > getAncestralStatesForNode(uint nodeId, VVdouble &probs, bool sample) const
Get ancestral states for a given node as a vector of int.
std::string toString(T t)
Defines the basic types of data flow nodes.
template void copyEigenToBpp(const ExtendedFloatMatrixXd &eigenMatrix, std::vector< std::vector< double >> &bppMatrix)
std::vector< Vdouble > VVdouble