5 #include "../../PatternTools.h"
24 throw Exception(
"Error, only 1 sequence!");
29 throw AlphabetMismatchException(
"DRASDRTreeLikelihoodData::initLikelihoods. Data and model must have the same alphabet type.",
35 shared_ptr<SitePatterns> patterns;
39 patterns = initLikelihoodsWithPatterns(tree_->getRootNode(), sites, model);
40 shrunkData_ = patterns->getSites();
41 rootWeights_ = patterns->getWeights();
42 rootPatternLinks_.resize((
size_t)patterns->getIndices().size());
43 SitePatterns::IndicesType::Map(&rootPatternLinks_[0], patterns->getIndices().size()) = patterns->getIndices();
44 nbDistinctSites_ = shrunkData_->getNumberOfSites();
48 patterns = std::make_unique<SitePatterns>(sites);
49 shrunkData_ = patterns->getSites();
50 rootWeights_ = patterns->getWeights();
51 rootPatternLinks_.resize(
size_t(patterns->getIndices().size()));
52 SitePatterns::IndicesType::Map(&rootPatternLinks_[0], patterns->getIndices().size()) = patterns->getIndices();
53 nbDistinctSites_ = shrunkData_->getNumberOfSites();
54 initLikelihoods(tree_->getRootNode(), *shrunkData_, model);
72 _likelihoods_node->resize(nbDistinctSites_);
73 _dLikelihoods_node->resize(nbDistinctSites_);
74 _d2Likelihoods_node->resize(nbDistinctSites_);
76 for (
size_t i = 0; i < nbDistinctSites_; i++)
78 VVdouble* _likelihoods_node_i = &(*_likelihoods_node)[i];
79 VVdouble* _dLikelihoods_node_i = &(*_dLikelihoods_node)[i];
80 VVdouble* _d2Likelihoods_node_i = &(*_d2Likelihoods_node)[i];
81 _likelihoods_node_i->resize(nbClasses_);
82 _dLikelihoods_node_i->resize(nbClasses_);
83 _d2Likelihoods_node_i->resize(nbClasses_);
84 for (
size_t c = 0; c < nbClasses_; c++)
86 Vdouble* _likelihoods_node_i_c = &(*_likelihoods_node_i)[c];
87 Vdouble* _dLikelihoods_node_i_c = &(*_dLikelihoods_node_i)[c];
88 Vdouble* _d2Likelihoods_node_i_c = &(*_d2Likelihoods_node_i)[c];
89 _likelihoods_node_i_c->resize(nbStates_);
90 _dLikelihoods_node_i_c->resize(nbStates_);
91 _d2Likelihoods_node_i_c->resize(nbStates_);
92 for (
size_t s = 0; s < nbStates_; s++)
94 (*_likelihoods_node_i_c)[s] = 1;
95 (*_dLikelihoods_node_i_c)[s] = 0;
96 (*_d2Likelihoods_node_i_c)[s] = 0;
113 for (
size_t i = 0; i < nbDistinctSites_; i++)
115 VVdouble* _likelihoods_node_i = &(*_likelihoods_node)[i];
116 for (
size_t c = 0; c < nbClasses_; c++)
118 Vdouble* _likelihoods_node_i_c = &(*_likelihoods_node_i)[c];
121 for (
size_t s = 0; s < nbStates_; s++)
127 test += (*_likelihoods_node_i_c)[s];
130 std::cerr <<
"WARNING!!! Likelihood will be 0 for site " << i << std::endl;
137 std::map<int, std::vector<size_t>>* patternLinks__node = &patternLinks_[node->
getId()];
139 for (
size_t l = 0; l < nbSonNodes; l++)
142 const Node* son = (*node)[
static_cast<int>(l)];
143 initLikelihoods(son, sequences, model);
144 std::vector<size_t>* patternLinks__node_son = &(*patternLinks__node)[son->
getId()];
147 patternLinks__node_son->resize(nbDistinctSites_);
149 for (
size_t i = 0; i < nbDistinctSites_; i++)
151 (*patternLinks__node_son)[i] = i;
168 auto patterns = std::make_unique<SitePatterns>(*tmp);
170 auto subSequences = patterns->getSites();
171 size_t nbSites = subSequences->getNumberOfSites();
179 _likelihoods_node->resize(nbSites);
180 _dLikelihoods_node->resize(nbSites);
181 _d2Likelihoods_node->resize(nbSites);
183 for (
size_t i = 0; i < nbSites; i++)
185 VVdouble* _likelihoods_node_i = &(*_likelihoods_node)[i];
186 VVdouble* _dLikelihoods_node_i = &(*_dLikelihoods_node)[i];
187 VVdouble* _d2Likelihoods_node_i = &(*_d2Likelihoods_node)[i];
188 _likelihoods_node_i->resize(nbClasses_);
189 _dLikelihoods_node_i->resize(nbClasses_);
190 _d2Likelihoods_node_i->resize(nbClasses_);
191 for (
size_t c = 0; c < nbClasses_; c++)
193 Vdouble* _likelihoods_node_i_c = &(*_likelihoods_node_i)[c];
194 Vdouble* _dLikelihoods_node_i_c = &(*_dLikelihoods_node_i)[c];
195 Vdouble* _d2Likelihoods_node_i_c = &(*_d2Likelihoods_node_i)[c];
196 _likelihoods_node_i_c->resize(nbStates_);
197 _dLikelihoods_node_i_c->resize(nbStates_);
198 _d2Likelihoods_node_i_c->resize(nbStates_);
199 for (
size_t s = 0; s < nbStates_; s++)
201 (*_likelihoods_node_i_c)[s] = 1;
202 (*_dLikelihoods_node_i_c)[s] = 0;
203 (*_d2Likelihoods_node_i_c)[s] = 0;
215 posSeq = subSequences->getSequencePosition(node->
getName());
219 throw SequenceNotFoundException(
"HomogeneousTreeLikelihood::initTreelikelihoodsWithPatterns. Leaf name in tree not found in site container: ", (node->
getName()));
222 for (
size_t i = 0; i < nbSites; i++)
224 VVdouble* _likelihoods_node_i = &(*_likelihoods_node)[i];
226 for (
size_t c = 0; c < nbClasses_; c++)
228 Vdouble* _likelihoods_node_i_c = &(*_likelihoods_node_i)[c];
230 for (
size_t s = 0; s < nbStates_; s++)
235 (*_likelihoods_node_i_c)[s] = subSequences->getStateValueAt(i, posSeq, model.
getAlphabetStateAsInt(s));
236 test += (*_likelihoods_node_i_c)[s];
239 std::cerr <<
"WARNING!!! Likelihood will be 0 for site " << i << std::endl;
246 std::map<int, std::vector<size_t>>* patternLinks__node = &patternLinks_[node->
getId()];
250 for (
int l = 0; l < static_cast<int>(nbSonNodes); l++)
253 const Node* son = (*node)[l];
255 std::vector<size_t>* patternLinks__node_son = &(*patternLinks__node)[son->
getId()];
260 shared_ptr<SitePatterns> subPatterns(initLikelihoodsWithPatterns(son, *subSequences, model));
262 patternLinks__node_son->resize(
size_t(subPatterns->getIndices().size()));
263 SitePatterns::IndicesType::Map(&((*patternLinks__node_son)[0]), subPatterns->getIndices().size()) = subPatterns->getIndices();
virtual int getAlphabetStateAsInt(size_t index) const =0
virtual size_t getNumberOfStates() const =0
Get the number of states.
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
virtual std::unique_ptr< SitePatterns > initLikelihoodsWithPatterns(const Node *node, const AlignmentDataInterface &sequences, const TransitionModelInterface &model)
This method initializes the leaves according to a sequence file.
void initLikelihoods(const AlignmentDataInterface &sites, const TransitionModelInterface &model)
Likelihood data structure for a node.
void setNode(const Node *node)
Set the node associated to this data.
VVVdouble & getLikelihoodArray()
VVVdouble & getD2LikelihoodArray()
VVVdouble & getDLikelihoodArray()
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 bool isLeaf() const
virtual size_t getNumberOfSons() const
virtual size_t getNumberOfSites() const=0
virtual size_t getNumberOfSequences() const =0
virtual double getStateValueAt(size_t sitePosition, const std::string &sequenceKey, int state) const =0
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
virtual size_t getSequencePosition(const std::string &sequenceKey) const =0
Interface for all transition models.
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
std::vector< VVdouble > VVVdouble
std::vector< Vdouble > VVdouble