9 #include "../Likelihood/DataFlow/ForwardLikelihoodTree.h"
17 using namespace numeric;
23 calcul_->makeLikelihoods();
28 outputInternalSites(outputInternalSites_);
32 const auto& dRate = process_->getRateDistribution();
34 std::vector<DataLik> qR;
36 for (
size_t i = 0; i < nbClasses_; i++)
38 qR.push_back(calcul_->getSiteLikelihoodsForAClass(i)(pos_));
44 for (
size_t i = 0; i < nbClasses_; i++)
46 qRates_.push_back(
convert(qR[i] / sQ));
51 const auto dRoot = process_->getRootFrequencies();
52 qRoots_.resize(nbClasses_);
53 RowLik temp((
int)nbStates_);
55 for (
size_t c = 0; c < nbClasses_; c++)
57 temp = calcul_->getForwardLikelihoodsAtNodeForClass(tree_.getNodeIndex(tree_.getRoot()), c)->targetValue().
col(pos_);
69 auto edges = tree_.getAllEdges();
71 std::vector<DataLik> postTrans(nbStates_);
73 for (
auto& edge : edges)
75 const auto model = edge->getModel();
76 auto outid = tree_.getEdgeIndex(edge);
81 const auto transmodel = dynamic_pointer_cast<const TransitionModelInterface>(model);
83 throw Exception(
"SubstitutionProcessSiteSimulator::init : model " + model->getName() +
" on branch " +
TextTools::toString(tree_.getEdgeIndex(edge)) +
" is not a TransitionModel.");
86 cumpxy_node_->resize(nbClasses_);
88 for (
size_t c = 0; c < nbClasses_; c++)
90 double brlen = dRate->getCategory(c) * phyloTree_->getEdge(edge->getSpeciesIndex())->getLength();
92 VVdouble* cumpxy_node_c_ = &(*cumpxy_node_)[c];
94 cumpxy_node_c_->resize(nbStates_);
101 const auto& vSub(edge->subModelNumbers());
103 if (vSub.size() == 0)
104 P = &transmodel->getPij_t(brlen);
108 throw Exception(
"SubstitutionProcessSiteSimulator::init : only 1 submodel can be used.");
110 const auto mmodel = dynamic_pointer_cast<const MixedTransitionModelInterface>(transmodel);
112 const auto model2 = mmodel->getNModel(vSub[0]);
114 P = &model2->getPij_t(brlen);
119 const auto& siteForwLik = calcul_->getForwardLikelihoodsAtNodeForClass(calcul_->getForwardLikelihoodTree(c)->getSon(outid), c)->targetValue().
col(pos_);
121 for (
size_t x = 0; x < size_t(nbStates_); x++)
123 for (
size_t y = 0; y < size_t(nbStates_); y++)
125 postTrans[y] = std::max((*P)(x, y),
NumConstants::PICO()) * siteForwLik(Eigen::Index(y));
130 for (
size_t i = 0; i < postTrans.size(); i++)
132 pT2[i] =
convert(postTrans[i]);
141 auto nodes = tree_.getAllNodes();
143 for (
auto node:nodes)
145 if (node->isMixture())
147 auto outEdges = tree_.getOutgoingEdges(node);
148 std::vector<std::vector<DataLik>> vprob;
149 vprob.resize(nbClasses_);
151 for (
auto edge : outEdges)
153 auto outid = tree_.getEdgeIndex(edge);
155 auto model = dynamic_pointer_cast<const MixedTransitionModelInterface>(edge->getModel());
157 throw Exception(
"SubstitutionProcessSiteSimulator::init : model in edge " +
TextTools::toString(tree_.getEdgeIndex(edge)) +
" is not a mixture.");
160 const auto& vNb(edge->subModelNumbers());
165 x += model->getNProbability(nb);
169 for (
size_t c = 0; c < nbClasses_; c++)
171 auto forwLik = calcul_->getForwardLikelihoodTree(c)->getEdge(outid)->targetValue().col(pos_).sum();
172 vprob[c].push_back(x * forwLik);
175 node->sons_.push_back(tree_.getSon(edge));
178 for (
size_t c = 0; c < nbClasses_; c++)
185 for (
size_t i = 0; i < vprob.size(); i++)
std::enable_if< std::is_same< M, EFMatrix< R, C > >::value, ExtendedFloatCol< R, C, EigenType > >::type col(Eigen::Index pos)
const ExtendedFloat & sum() const
void init() override
Init all probabilities.
virtual std::vector< Scalar > col(size_t j) const=0
std::string toString(T t)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
template void copyEigenToBpp(const ExtendedFloatMatrixXd &eigenMatrix, std::vector< std::vector< double >> &bppMatrix)
std::vector< VVdouble > VVVdouble
double convert(const bpp::ExtendedFloat &ef)
std::vector< Vdouble > VVdouble