bpp-phyl  2.2.0
DRTreeParsimonyData.cpp
Go to the documentation of this file.
1 //
2 // File: DRTreeParsimonyData.cpp
3 // Created by: Julien Dutheil
4 // Created on: Tue Jan O9 17:38 2007
5 // From file: DRHTreeParsimonyScore.cpp
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (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 #include "DRTreeParsimonyData.h"
42 #include "../SitePatterns.h"
43 
44 // From SeqLib:
45 #include <Bpp/Seq/Container/AlignedSequenceContainer.h>
46 
47 using namespace bpp;
48 using namespace std;
49 
50 /******************************************************************************/
51 
54  nodeData_(data.nodeData_),
55  leafData_(data.leafData_),
56  rootBitsets_(data.rootBitsets_),
57  rootScores_(data.rootScores_),
58  shrunkData_(0),
59  nbSites_(data.nbSites_),
60  nbStates_(data.nbStates_),
61  nbDistinctSites_(data.nbDistinctSites_)
62 {
63  if (data.shrunkData_)
64  shrunkData_ = dynamic_cast<SiteContainer*>(data.shrunkData_->clone());
65  else
66  shrunkData_ = 0;
67 }
68 
69 /******************************************************************************/
70 
72 {
74  nodeData_ = data.nodeData_;
75  leafData_ = data.leafData_;
77  rootScores_ = data.rootScores_;
78  if (shrunkData_) delete shrunkData_;
79  if (data.shrunkData_)
80  shrunkData_ = dynamic_cast<SiteContainer*>(data.shrunkData_->clone());
81  else
82  shrunkData_ = 0;
83  nbSites_ = data.nbSites_;
84  nbStates_ = data.nbStates_;
86  return *this;
87 }
88 
89 /******************************************************************************/
90 void DRTreeParsimonyData::init(const SiteContainer& sites, const StateMap& stateMap) throw (Exception)
91 {
92  nbStates_ = stateMap.getNumberOfModelStates();
93  nbSites_ = sites.getNumberOfSites();
94  SitePatterns pattern(&sites);
95  shrunkData_ = pattern.getSites();
96  rootWeights_ = pattern.getWeights();
97  rootPatternLinks_ = pattern.getIndices();
98  nbDistinctSites_ = shrunkData_->getNumberOfSites();
99 
100  // Init data:
101  // Clone data for more efficiency on sequences access:
102  const SiteContainer* sequences = new AlignedSequenceContainer(*shrunkData_);
103  init(getTreeP_()->getRootNode(), *sequences, stateMap);
104  delete sequences;
105 
106  // Now initialize root arrays:
107  rootBitsets_.resize(nbDistinctSites_);
108  rootScores_.resize(nbDistinctSites_);
109 }
110 
111 /******************************************************************************/
112 void DRTreeParsimonyData::init(const Node* node, const SiteContainer& sites, const StateMap& stateMap) throw (Exception)
113 {
114  const Alphabet* alphabet = sites.getAlphabet();
115  if (node->isLeaf())
116  {
117  const Sequence* seq;
118  try
119  {
120  seq = &sites.getSequence(node->getName());
121  }
122  catch (SequenceNotFoundException& snfe)
123  {
124  throw SequenceNotFoundException("DRTreeParsimonyData:init(node, sites). Leaf name in tree not found in site container: ", (node->getName()));
125  }
126  DRTreeParsimonyLeafData* leafData = &leafData_[node->getId()];
127  vector<Bitset>* leafData_bitsets = &leafData->getBitsetsArray();
128  leafData->setNode(node);
129 
130  leafData_bitsets->resize(nbDistinctSites_);
131 
132  for (unsigned int i = 0; i < nbDistinctSites_; i++)
133  {
134  Bitset* leafData_bitsets_i = &(*leafData_bitsets)[i];
135  for (unsigned int s = 0; s < nbStates_; s++)
136  {
137  // Leaves bitset are set to 1 if the char correspond to the site in the sequence,
138  // otherwise value set to 0:
139  int state = seq->getValue(i);
140  vector<int> states = alphabet->getAlias(state);
141  for (size_t j = 0; j < states.size(); j++)
142  {
143  if (stateMap.getAlphabetStateAsInt(s) == states[j])
144  (*leafData_bitsets_i)[s].flip();
145  }
146  }
147  }
148  }
149  else
150  {
151  DRTreeParsimonyNodeData* nodeData = &nodeData_[node->getId()];
152  nodeData->setNode(node);
153  nodeData->eraseNeighborArrays();
154 
155  int nbSons = static_cast<int>(node->getNumberOfSons());
156 
157  for (int n = (node->hasFather() ? -1 : 0); n < nbSons; n++)
158  {
159  const Node* neighbor = (*node)[n];
160  vector<Bitset>* neighborData_bitsets = &nodeData->getBitsetsArrayForNeighbor(neighbor->getId());
161  vector<unsigned int>* neighborData_scores = &nodeData->getScoresArrayForNeighbor(neighbor->getId());
162 
163  neighborData_bitsets->resize(nbDistinctSites_);
164  neighborData_scores->resize(nbDistinctSites_);
165  }
166  }
167 
168  // We initialize each son node:
169  size_t nbSonNodes = node->getNumberOfSons();
170  for (unsigned int l = 0; l < nbSonNodes; l++)
171  {
172  // For each son node,
173  init(node->getSon(l), sites, stateMap);
174  }
175 }
176 
177 /******************************************************************************/
178 void DRTreeParsimonyData::reInit() throw (Exception)
179 {
180  reInit(getTreeP_()->getRootNode());
181 }
182 
183 /******************************************************************************/
184 void DRTreeParsimonyData::reInit(const Node* node) throw (Exception)
185 {
186  if (node->isLeaf())
187  {
188  return;
189  }
190  else
191  {
192  DRTreeParsimonyNodeData* nodeData = &nodeData_[node->getId()];
193  nodeData->setNode(node);
194  nodeData->eraseNeighborArrays();
195 
196  int nbSons = static_cast<int>(node->getNumberOfSons());
197 
198  for (int n = (node->hasFather() ? -1 : 0); n < nbSons; n++)
199  {
200  const Node* neighbor = (*node)[n];
201  vector<Bitset>* neighborData_bitsets = &nodeData->getBitsetsArrayForNeighbor(neighbor->getId());
202  vector<unsigned int>* neighborData_scores = &nodeData->getScoresArrayForNeighbor(neighbor->getId());
203 
204  neighborData_bitsets->resize(nbDistinctSites_);
205  neighborData_scores->resize(nbDistinctSites_);
206  }
207  }
208 
209  // We initialize each son node:
210  size_t nbSonNodes = node->getNumberOfSons();
211  for (unsigned int l = 0; l < nbSonNodes; l++)
212  {
213  // For each son node,
214  reInit(node->getSon(l));
215  }
216 }
217 
218 /******************************************************************************/
219 
Parsimony data structure for a node.
std::vector< Bitset > & getBitsetsArrayForNeighbor(int neighborId)
const std::vector< size_t > & getIndices() const
Definition: SitePatterns.h:175
STL namespace.
const std::vector< unsigned int > & getWeights() const
Definition: SitePatterns.h:171
void setNode(const Node *node)
Set the node associated to this data.
std::vector< unsigned int > & getScoresArrayForNeighbor(int neighborId)
Parsimony data structure for a leaf.
AbstractTreeParsimonyData & operator=(const AbstractTreeParsimonyData &atpd)
std::map< int, DRTreeParsimonyLeafData > leafData_
virtual int getId() const
Get the node&#39;s id.
Definition: Node.h:203
const TreeTemplate< Node > * getTreeP_() const
void init(const SiteContainer &sites, const StateMap &stateMap)
std::vector< unsigned int > rootScores_
SiteContainer * getSites() const
The phylogenetic node class.
Definition: Node.h:90
std::bitset< 21 > Bitset
DRTreeParsimonyData & operator=(const DRTreeParsimonyData &data)
std::map< int, DRTreeParsimonyNodeData > nodeData_
Parsimony data structure for double-recursive (DR) algorithm.
DRTreeParsimonyData(const TreeTemplate< Node > *tree)
Partial implementation of the TreeParsimonyData interface.
Data structure for site patterns.
Definition: SitePatterns.h:69
std::vector< Bitset > & getBitsetsArray()
std::vector< Bitset > rootBitsets_
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:58
void setNode(const Node *node)
Set the node associated to this data.