6 #include "../Likelihood/DRTreeLikelihoodTools.h"
7 #include "../Likelihood/MarginalAncestralStateReconstruction.h"
24 std::shared_ptr<const DRTreeLikelihoodInterface> drtl,
25 const vector<int>& nodeIds,
26 std::shared_ptr<Reward> reward,
30 if (!drtl->isInitialized())
31 throw Exception(
"RewardMappingTools::computeRewardVectors(). Likelihood object is not initialized.");
36 const auto& sequences = drtl->data();
37 auto& rDist = drtl->rateDistribution();
39 size_t nbSites = sequences.getNumberOfSites();
40 size_t nbDistinctSites = drtl->likelihoodData().getNumberOfDistinctSites();
41 size_t nbStates = sequences.alphabet().getSize();
42 size_t nbClasses = rDist.getNumberOfCategories();
43 vector<const Node*> nodes = tree.
getNodes();
44 const vector<size_t>& rootPatternLinks = drtl->likelihoodData().getRootArrayPositions();
46 size_t nbNodes = nodes.size();
49 auto rewards = make_unique<LegacyProbabilisticRewardMapping>(tree, reward, nbSites);
53 drtl->computeLikelihoodAtNode(tree.
getRootId(), lik);
55 Vdouble rcProbs = rDist.getProbabilities();
56 Vdouble rcRates = rDist.getCategories();
57 for (
size_t i = 0; i < nbDistinctSites; i++)
60 for (
size_t c = 0; c < nbClasses; c++)
63 double rc = rDist.getProbability(c);
64 for (
size_t s = 0; s < nbStates; s++)
66 Lr[i] += lik_i_c[s] * rc;
75 for (
size_t l = 0; l < nbNodes; ++l)
78 const Node* currentNode = nodes[l];
88 Vdouble rewardsForCurrentNode(nbDistinctSites);
91 VVVdouble likelihoodsFatherConstantPart(nbDistinctSites);
92 for (
size_t i = 0; i < nbDistinctSites; i++)
94 VVdouble& likelihoodsFatherConstantPart_i = likelihoodsFatherConstantPart[i];
95 likelihoodsFatherConstantPart_i.resize(nbClasses);
96 for (
size_t c = 0; c < nbClasses; c++)
98 Vdouble& likelihoodsFatherConstantPart_i_c = likelihoodsFatherConstantPart_i[c];
99 likelihoodsFatherConstantPart_i_c.resize(nbStates);
100 double rc = rDist.getProbability(c);
101 for (
size_t s = 0; s < nbStates; s++)
105 likelihoodsFatherConstantPart_i_c[s] = rc;
112 for (
size_t n = 0; n < nbSons; n++)
115 if (currentSon->
getId() != currentNode->
getId())
117 const VVVdouble& likelihoodsFather_son = drtl->likelihoodData().getLikelihoodArray(father->
getId(), currentSon->
getId());
120 unique_ptr<TreeLikelihoodInterface::ConstBranchModelIterator> mit(drtl->getNewBranchModelIterator(currentSon->
getId()));
123 while (mit->hasNext())
125 auto bmd = mit->next();
126 unique_ptr<TreeLikelihoodInterface::SiteIterator> sit(bmd->getNewSiteIterator());
128 while (sit->hasNext())
130 size_t i = sit->next();
134 pxy = drtl->getTransitionProbabilitiesPerRateClass(currentSon->
getId(), i);
137 const VVdouble& likelihoodsFather_son_i = likelihoodsFather_son[i];
138 VVdouble& likelihoodsFatherConstantPart_i = likelihoodsFatherConstantPart[i];
139 for (
size_t c = 0; c < nbClasses; c++)
141 const Vdouble& likelihoodsFather_son_i_c = likelihoodsFather_son_i[c];
142 Vdouble& likelihoodsFatherConstantPart_i_c = likelihoodsFatherConstantPart_i[c];
144 for (
size_t x = 0; x < nbStates; x++)
147 double likelihood = 0.;
148 for (
size_t y = 0; y < nbStates; y++)
150 likelihood += pxy_c_x[y] * likelihoodsFather_son_i_c[y];
152 likelihoodsFatherConstantPart_i_c[x] *= likelihood;
162 const VVVdouble& likelihoodsFather_son = drtl->likelihoodData().getLikelihoodArray(father->
getId(), currentSon->
getId());
164 unique_ptr<TreeLikelihoodInterface::ConstBranchModelIterator> mit(drtl->getNewBranchModelIterator(father->
getId()));
167 while (mit->hasNext())
169 auto bmd = mit->next();
170 unique_ptr<TreeLikelihoodInterface::SiteIterator> sit(bmd->getNewSiteIterator());
172 while (sit->hasNext())
174 size_t i = sit->next();
178 pxy = drtl->getTransitionProbabilitiesPerRateClass(father->
getId(), i);
181 const VVdouble& likelihoodsFather_son_i = likelihoodsFather_son[i];
182 VVdouble& likelihoodsFatherConstantPart_i = likelihoodsFatherConstantPart[i];
183 for (
size_t c = 0; c < nbClasses; c++)
185 const Vdouble& likelihoodsFather_son_i_c = likelihoodsFather_son_i[c];
186 Vdouble& likelihoodsFatherConstantPart_i_c = likelihoodsFatherConstantPart_i[c];
188 for (
size_t x = 0; x < nbStates; x++)
190 double likelihood = 0.;
191 for (
size_t y = 0; y < nbStates; y++)
194 likelihood += pxy_c_x[x] * likelihoodsFather_son_i_c[y];
196 likelihoodsFatherConstantPart_i_c[x] *= likelihood;
205 for (
size_t i = 0; i < nbDistinctSites; i++)
207 vector<double> freqs = drtl->getRootFrequencies(i);
208 VVdouble* likelihoodsFatherConstantPart_i = &likelihoodsFatherConstantPart[i];
209 for (
size_t c = 0; c < nbClasses; c++)
211 Vdouble* likelihoodsFatherConstantPart_i_c = &(*likelihoodsFatherConstantPart_i)[c];
212 for (
size_t x = 0; x < nbStates; x++)
214 (*likelihoodsFatherConstantPart_i_c)[x] *= freqs[x];
225 const VVVdouble& likelihoodsFather_node = drtl->likelihoodData().getLikelihoodArray(father->
getId(), currentNode->
getId());
226 unique_ptr<TreeLikelihoodInterface::ConstBranchModelIterator> mit(drtl->getNewBranchModelIterator(currentNode->
getId()));
229 while (mit->hasNext())
231 auto bmd = mit->next();
232 reward->setSubstitutionModel(bmd->getSubstitutionModel());
235 for (
size_t c = 0; c < nbClasses; ++c)
238 double rc = rcRates[c];
239 auto nij = reward->getAllRewards(d * rc);
240 nxy_c.resize(nbStates);
241 for (
size_t x = 0; x < nbStates; ++x)
244 nxy_c_x.resize(nbStates);
245 for (
size_t y = 0; y < nbStates; ++y)
247 nxy_c_x[y] = (*nij)(x, y);
253 unique_ptr<TreeLikelihoodInterface::SiteIterator> sit(bmd->getNewSiteIterator());
255 while (sit->hasNext())
257 size_t i = sit->next();
261 pxy = drtl->getTransitionProbabilitiesPerRateClass(currentNode->
getId(), i);
264 const VVdouble& likelihoodsFather_node_i = likelihoodsFather_node[i];
265 VVdouble& likelihoodsFatherConstantPart_i = likelihoodsFatherConstantPart[i];
266 for (
size_t c = 0; c < nbClasses; ++c)
268 const Vdouble& likelihoodsFather_node_i_c = likelihoodsFather_node_i[c];
269 Vdouble& likelihoodsFatherConstantPart_i_c = likelihoodsFatherConstantPart_i[c];
272 for (
size_t x = 0; x < nbStates; ++x)
274 double& likelihoodsFatherConstantPart_i_c_x = likelihoodsFatherConstantPart_i_c[x];
275 const Vdouble& pxy_c_x = pxy_c[x];
276 for (
size_t y = 0; y < nbStates; ++y)
278 double likelihood_cxy = likelihoodsFatherConstantPart_i_c_x
280 * likelihoodsFather_node_i_c[y];
283 rewardsForCurrentNode[i] += likelihood_cxy * nxy_c[x][y];
297 for (
size_t i = 0; i < nbSites; ++i)
299 (*rewards)(l, i) = rewardsForCurrentNode[rootPatternLinks[i]] / Lr[rootPatternLinks[i]];
319 throw IOException(
"RewardMappingTools::writeToFile. Can't write to stream.");
333 out <<
"\t" << rewards(j, i);
346 vector<string> ids = data->getColumn(0);
347 data->deleteColumn(0);
348 data->deleteColumn(0);
350 size_t nbSites = data->getNumberOfColumns();
352 size_t nbBranches = data->getNumberOfRows();
353 for (
size_t i = 0; i < nbBranches; i++)
357 for (
size_t j = 0; j < nbSites; j++)
363 for (
size_t i = 0; i < nbSites; i++)
365 string siteTxt = data->getColumnName(i);
367 if (siteTxt.substr(0, 4) ==
"Site")
368 site = TextTools::to<int>(siteTxt.substr(4));
370 site = TextTools::to<int>(siteTxt);
386 for (
size_t i = 0; i < nbSites; ++i)
388 v += smap(branchIndex, i);
399 for (
size_t i = 0; i < nbBranches; ++i)
401 v += smap(i, siteIndex);
int getCoordinate() const override
static std::unique_ptr< DataTable > read(std::istream &in, const std::string &sep="\t", bool header=true, int rowNames=-1)
const char * what() const noexcept override
size_t getNumberOfSites() const override
void setSitePosition(size_t index, int position) override
Set the position of a given site.
virtual size_t getNodeIndex(int nodeId) const override
size_t getNumberOfBranches() const override
virtual const Node * getNode(size_t nodeIndex) const
virtual size_t getNumberOfBranches() const =0
virtual size_t getNumberOfSites() const =0
Legacy data storage class for probabilistic rewards mappings.
virtual void setNumberOfSites(size_t numberOfSites) override
Legacy interface for storing reward mapping data.
The phylogenetic node class.
virtual int getId() const
Get the node's id.
virtual const Node * getSon(size_t pos) const
virtual const Node * getFather() const
Get the father of this node is there is one.
virtual bool hasFather() const
Tell if this node has a father node.
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
virtual size_t getNumberOfSons() const
virtual const Site & site(size_t sitePosition) const override=0
The phylogenetic tree class.
virtual std::vector< const N * > getNodes() const
int toInt(const std::string &s, char scientificNotation='e')
double toDouble(const std::string &s, char dec='.', char scientificNotation='e')
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
std::vector< VVdouble > VVVdouble
std::vector< Vdouble > VVdouble