bpp-phyl  2.2.0
DRASRTreeLikelihoodData.h
Go to the documentation of this file.
1 //
2 // File: DRASRTreeLikelihoodData.h
3 // Created by: Julien Dutheil
4 // Created on: Sat Dec 30 14:20 2006
5 // From file HomogeneousTreeLikelihood.h
6 //
7 
8 /*
9 Copyright or © or Copr. CNRS, (November 16, 2004)
10 
11 This software is a computer program whose purpose is to provide classes
12 for phylogenetic data analysis.
13 
14 This software is governed by the CeCILL license under French law and
15 abiding by the rules of distribution of free software. You can use,
16 modify and/ or redistribute the software under the terms of the CeCILL
17 license as circulated by CEA, CNRS and INRIA at the following URL
18 "http://www.cecill.info".
19 
20 As a counterpart to the access to the source code and rights to copy,
21 modify and redistribute granted by the license, users are provided only
22 with a limited warranty and the software's author, the holder of the
23 economic rights, and the successive licensors have only limited
24 liability.
25 
26 In this respect, the user's attention is drawn to the risks associated
27 with loading, using, modifying and/or developing or reproducing the
28 software by the user in light of its specific status of free software,
29 that may mean that it is complicated to manipulate, and that also
30 therefore means that it is reserved for developers and experienced
31 professionals having in-depth computer knowledge. Users are therefore
32 encouraged to load and test the software's suitability as regards their
33 requirements in conditions enabling the security of their systems and/or
34 data to be ensured and, more generally, to use and operate it in the
35 same conditions as regards security.
36 
37 The fact that you are presently reading this means that you have had
38 knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef _DRASRHOMOGENEOUSTREELIKELIHOODDATA_H_
42 #define _DRASRHOMOGENEOUSTREELIKELIHOODDATA_H_
43 
45 #include "../Model/SubstitutionModel.h"
46 #include "../SitePatterns.h"
47 
48 #include <Bpp/Numeric/VectorTools.h>
49 
50 // From the STL:
51 #include <map>
52 
53 namespace bpp
54 {
55 
74  public virtual TreeLikelihoodNodeData
75 {
76  private:
77  mutable VVVdouble nodeLikelihoods_;
78  mutable VVVdouble nodeDLikelihoods_;
79  mutable VVVdouble nodeD2Likelihoods_;
80  const Node* node_;
81 
82  public:
84 
89  node_(data.node_)
90  {}
91 
93  {
97  node_ = data.node_;
98  return *this;
99  }
100 
101 #ifndef NO_VIRTUAL_COV
103 #else
104  Clonable*
105 #endif
106  clone() const
107  {
108  return new DRASRTreeLikelihoodNodeData(*this);
109  }
110 
111  public:
112  const Node* getNode() const { return node_; }
113  void setNode(const Node* node) { node_ = node; }
114 
115  VVVdouble& getLikelihoodArray() { return nodeLikelihoods_; }
116  const VVVdouble& getLikelihoodArray() const { return nodeLikelihoods_; }
117 
118  VVVdouble& getDLikelihoodArray() { return nodeDLikelihoods_; }
119  const VVVdouble& getDLikelihoodArray() const { return nodeDLikelihoods_; }
120 
121  VVVdouble& getD2LikelihoodArray() { return nodeD2Likelihoods_; }
122  const VVVdouble& getD2LikelihoodArray() const { return nodeD2Likelihoods_; }
123 };
124 
129  public virtual AbstractTreeLikelihoodData
130 {
131  private:
136  mutable std::map<int, DRASRTreeLikelihoodNodeData> nodeData_;
137 
150  mutable std::map<int, std::map<int, std::vector<size_t> > > patternLinks_;
151  SiteContainer* shrunkData_;
152  size_t nbSites_;
153  size_t nbStates_;
154  size_t nbClasses_;
157 
158  public:
159  DRASRTreeLikelihoodData(const TreeTemplate<Node>* tree, size_t nbClasses, bool usePatterns = true) :
162  nbClasses_(nbClasses), nbDistinctSites_(0), usePatterns_(usePatterns)
163  {}
164 
167  nodeData_(data.nodeData_),
169  shrunkData_(0),
170  nbSites_(data.nbSites_), nbStates_(data.nbStates_),
173  {
174  if (data.shrunkData_)
175  shrunkData_ = dynamic_cast<SiteContainer *>(data.shrunkData_->clone());
176  }
177 
179  {
181  nodeData_ = data.nodeData_;
183  nbSites_ = data.nbSites_;
184  nbStates_ = data.nbStates_;
185  nbClasses_ = data.nbClasses_;
187  if (shrunkData_) delete shrunkData_;
188  if (data.shrunkData_)
189  shrunkData_ = dynamic_cast<SiteContainer*>(data.shrunkData_->clone());
190  else
191  shrunkData_ = 0;
192  usePatterns_ = data.usePatterns_;
193  return *this;
194  }
195 
196  virtual ~DRASRTreeLikelihoodData() { delete shrunkData_; }
197 
198  DRASRTreeLikelihoodData* clone() const { return new DRASRTreeLikelihoodData(*this); }
199 
200  public:
210  void setTree(const TreeTemplate<Node>* tree)
211  {
212  tree_ = tree;
213  for (std::map<int, DRASRTreeLikelihoodNodeData>::iterator it = nodeData_.begin(); it != nodeData_.end(); it++)
214  {
215  int id = it->second.getNode()->getId();
216  it->second.setNode(tree_->getNode(id));
217  }
218  }
219 
221  {
222  return nodeData_[nodeId];
223  }
224  const DRASRTreeLikelihoodNodeData& getNodeData(int nodeId) const
225  {
226  return nodeData_[nodeId];
227  }
228  size_t getArrayPosition(int parentId, int sonId, size_t currentPosition) const
229  {
230  return patternLinks_[parentId][sonId][currentPosition];
231  }
232  size_t getRootArrayPosition(size_t currentPosition) const
233  {
234  return rootPatternLinks_[currentPosition];
235  }
236  const std::vector<size_t>& getArrayPositions(int parentId, int sonId) const
237  {
238  return patternLinks_[parentId][sonId];
239  }
240  std::vector<size_t>& getArrayPositions(int parentId, int sonId)
241  {
242  return patternLinks_[parentId][sonId];
243  }
244  size_t getArrayPosition(int parentId, int sonId, size_t currentPosition)
245  {
246  return patternLinks_[parentId][sonId][currentPosition];
247  }
248 
249  VVVdouble& getLikelihoodArray(int nodeId)
250  {
251  return nodeData_[nodeId].getLikelihoodArray();
252  }
253 
254  VVVdouble& getDLikelihoodArray(int nodeId)
255  {
256  return nodeData_[nodeId].getDLikelihoodArray();
257  }
258 
259  VVVdouble& getD2LikelihoodArray(int nodeId)
260  {
261  return nodeData_[nodeId].getD2LikelihoodArray();
262  }
263 
264  size_t getNumberOfDistinctSites() const { return nbDistinctSites_; }
265  size_t getNumberOfSites() const { return nbSites_; }
266  size_t getNumberOfStates() const { return nbStates_; }
267  size_t getNumberOfClasses() const { return nbClasses_; }
268 
269  void initLikelihoods(const SiteContainer& sites, const SubstitutionModel& model) throw (Exception);
270 
271  protected:
286  virtual void initLikelihoods(const Node* node, const SiteContainer& sequences, const SubstitutionModel& model) throw (Exception);
287 
304  virtual SitePatterns* initLikelihoodsWithPatterns(const Node* node, const SiteContainer& sequences, const SubstitutionModel& model) throw (Exception);
305 
306 };
307 
308 } //end of namespace bpp.
309 
310 #endif //_DRASRHOMOGENEOUSTREELIKELIHOODDATA_H_
311 
DRASRTreeLikelihoodNodeData(const DRASRTreeLikelihoodNodeData &data)
size_t getArrayPosition(int parentId, int sonId, size_t currentPosition)
Interface for all substitution models.
AbstractTreeLikelihoodData & operator=(const AbstractTreeLikelihoodData &atd)
Partial implementation of the TreeLikelihoodData interface.
const VVVdouble & getDLikelihoodArray() const
size_t getRootArrayPosition(size_t currentPosition) const
void setNode(const Node *node)
Set the node associated to this data.
void setTree(const TreeTemplate< Node > *tree)
Set the tree associated to the data.
std::map< int, DRASRTreeLikelihoodNodeData > nodeData_
This contains all likelihood values used for computation.
The phylogenetic tree class.
discrete Rate Across Sites, (simple) Recursive likelihood data structure.
DRASRTreeLikelihoodNodeData * clone() const
const TreeTemplate< Node > * tree_
DRASRTreeLikelihoodData(const DRASRTreeLikelihoodData &data)
std::map< int, std::map< int, std::vector< size_t > > > patternLinks_
This map defines the pattern network.
const VVVdouble & getD2LikelihoodArray() const
std::vector< size_t > rootPatternLinks_
Links between sites and patterns.
VVVdouble & getDLikelihoodArray(int nodeId)
DRASRTreeLikelihoodData & operator=(const DRASRTreeLikelihoodData &data)
const DRASRTreeLikelihoodNodeData & getNodeData(int nodeId) const
size_t getArrayPosition(int parentId, int sonId, size_t currentPosition) const
void initLikelihoods(const SiteContainer &sites, const SubstitutionModel &model)
The phylogenetic node class.
Definition: Node.h:90
DRASRTreeLikelihoodData * clone() const
const VVVdouble & getLikelihoodArray() const
DRASRTreeLikelihoodData(const TreeTemplate< Node > *tree, size_t nbClasses, bool usePatterns=true)
DRASRTreeLikelihoodNodeData & getNodeData(int nodeId)
Data structure for site patterns.
Definition: SitePatterns.h:69
const std::vector< size_t > & getArrayPositions(int parentId, int sonId) const
Likelihood data structure for a node.
virtual SitePatterns * initLikelihoodsWithPatterns(const Node *node, const SiteContainer &sequences, const SubstitutionModel &model)
This method initializes the leaves according to a sequence file.
std::vector< size_t > & getArrayPositions(int parentId, int sonId)
VVVdouble & getLikelihoodArray(int nodeId)
VVVdouble & getD2LikelihoodArray(int nodeId)
TreeLikelihood partial data structure.
const Node * getNode() const
Get the node associated to this data structure.
virtual N * getNode(int id, bool checkId=false)
Definition: TreeTemplate.h:419
DRASRTreeLikelihoodNodeData & operator=(const DRASRTreeLikelihoodNodeData &data)