bpp-phyl  2.4.0
Mapping.h
Go to the documentation of this file.
1 //
2 // File: Mapping.h
3 // Created by: Julien Dutheil
4 // Created on: Wed Apr 5 09:51 2005
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 16, 2004, 2005, 2006)
9 
10  This software is a computer program whose purpose is to provide classes
11  for phylogenetic data analysis.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #ifndef _MAPPING_H_
41 #define _MAPPING_H_
42 
43 #include <Bpp/Clonable.h>
44 
45 #include "../Tree.h"
46 #include "../TreeTemplate.h"
47 
48 //From the STL:
49 #include <vector>
50 #include <memory>
51 
52 namespace bpp
53 {
54 
59  class Mapping:
60  public virtual Clonable
61  {
62 
63  public:
64  Mapping() {}
65  virtual ~Mapping() {}
66 
67  Mapping* clone() const = 0;
68 
69  public:
70 
74  virtual const Tree& getTree() const = 0;
75 
79  virtual bool isEmpty() const = 0;
80 
84  virtual size_t getNumberOfSites() const = 0;
85 
89  virtual size_t getNumberOfBranches() const = 0;
90 
95  virtual int getSitePosition(size_t index) const = 0;
96 
100  virtual std::vector<double> getBranchLengths() const = 0;
101 
106  virtual size_t getNodeIndex(int nodeId) const = 0;
107 
115  virtual void setSitePosition(size_t index, int position) = 0;
116  };
117 
118 
119 
120 
121 
122 
129  virtual public Mapping
130  {
131  private:
132  std::unique_ptr<const TreeTemplate<Node> > tree_;
133  std::vector<int> sitesPositions_;
134  std::vector<const Node *> nodes_;
135  size_t nbSites_;
136  size_t nbBranches_;
137 
138  public:
139  // AbstractMapping() : tree_(0), sitesPositions_(), nodes_(), nbSites_(0), nbBranches_(0) {}
140 
141  AbstractMapping(const Tree& tree) : tree_(new TreeTemplate<Node>(tree)), sitesPositions_(), nodes_(), nbSites_(0), nbBranches_(0)
142  {
143  nodes_ = tree_->getNodes();
144  nodes_.pop_back(); // remove root node.
145  nbBranches_ = nodes_.size();
146  }
147 
149  tree_(dynamic_cast<const TreeTemplate<Node>*>(absm.tree_->clone())),
150  sitesPositions_(absm.sitesPositions_),
151  nodes_(),
152  nbSites_(absm.nbSites_),
153  nbBranches_(absm.nbBranches_)
154  {
155  nodes_ = tree_->getNodes();
156  nodes_.pop_back(); // remove root node.
157  }
158 
160  {
161  tree_.reset(dynamic_cast<const TreeTemplate<Node>*>(absm.tree_->clone()));
162  sitesPositions_ = absm.sitesPositions_;
163  nbSites_ = absm.nbSites_;
164  nbBranches_ = absm.nbBranches_;
165  nodes_ = tree_->getNodes();
166  nodes_.pop_back(); // remove root node.
167  return *this;
168  }
169 
170  AbstractMapping* clone() const = 0;
171 
172  virtual ~AbstractMapping() {}
173 
174  public:
175 
176  bool isEmpty() const { return tree_.get() == 0; }
177 
179  {
180  if (isEmpty()) throw Exception("AbstractSubstitutionMapping::getSitePosition. No tree is assigned to this map yet.");
181  return *tree_.get();
182  }
183 
184  void setTree(const Tree& tree)
185  {
186  tree_.reset(new TreeTemplate<Node>(tree));
187  nodes_ = tree_->getNodes();
188  nodes_.pop_back(); // remove root node.
189  nbBranches_ = nodes_.size();
190  }
191 
192  int getSitePosition(size_t index) const
193  {
194  if (isEmpty()) throw Exception("AbstractMapping::getSitePosition. No tree is assigned to this map yet.");
195  return sitesPositions_[index];
196  }
197 
198  void setSitePosition(size_t index, int position)
199  {
200  if (isEmpty()) throw Exception("AbstractMapping::setSitePosition. No tree is assigned to this map yet.");
201  sitesPositions_[index] = position;
202  }
203 
204  size_t getNumberOfSites() const { return nbSites_; }
205 
206  size_t getNumberOfBranches() const { return nbBranches_; }
207 
208  virtual const Node* getNode(size_t nodeIndex) const { return nodes_[nodeIndex]; }
209 
210  virtual void setNumberOfSites(size_t numberOfSites)
211  {
212  nbSites_ = numberOfSites;
213  sitesPositions_.resize(numberOfSites);
214  for (size_t i = 0; i < numberOfSites; i++)
215  sitesPositions_[i] = static_cast<int>(i + 1); //Default: all sizes numbered for 1 to n.
216  }
217 
218  virtual std::vector<double> getBranchLengths() const
219  {
220  std::vector<double> brLen(nbBranches_);
221  for (size_t i = 0; i < nbBranches_; i++)
222  brLen[i] = nodes_[i]->getDistanceToFather();
223  return brLen;
224  }
225 
226  virtual size_t getNodeIndex(int nodeId) const
227  {
228  for (size_t i = 0; i < nbBranches_; i++)
229  if(nodes_[i]->getId() == nodeId) return i;
230  throw NodeNotFoundException("AbstractMapping::getNodeIndex(nodeId).", TextTools::toString(nodeId));
231  }
232 
233 
234  };
235 
236 } //end of namespace bpp.
237 
238 #endif //_MAPPING_H_
239 
Mapping * clone() const =0
AbstractMapping & operator=(const AbstractMapping &absm)
Definition: Mapping.h:159
virtual ~Mapping()
Definition: Mapping.h:65
AbstractMapping(const Tree &tree)
Definition: Mapping.h:141
virtual size_t getNodeIndex(int nodeId) const =0
std::vector< const Node * > nodes_
Definition: Mapping.h:134
General interface for storing mapping data.
Definition: Mapping.h:59
void setTree(const Tree &tree)
Definition: Mapping.h:184
The phylogenetic tree class.
virtual const Node * getNode(size_t nodeIndex) const
Definition: Mapping.h:208
Interface for phylogenetic tree objects.
Definition: Tree.h:148
virtual size_t getNumberOfBranches() const =0
virtual int getSitePosition(size_t index) const =0
virtual void setSitePosition(size_t index, int position)=0
Set the position of a given site.
virtual void setNumberOfSites(size_t numberOfSites)
Definition: Mapping.h:210
Exception thrown when something is wrong with a particular node.
const TreeTemplate< Node > & getTree() const
Definition: Mapping.h:178
void setSitePosition(size_t index, int position)
Set the position of a given site.
Definition: Mapping.h:198
virtual bool isEmpty() const =0
The phylogenetic node class.
Definition: Node.h:90
size_t getNumberOfBranches() const
Definition: Mapping.h:206
virtual std::vector< double > getBranchLengths() const =0
virtual ~AbstractMapping()
Definition: Mapping.h:172
std::vector< int > sitesPositions_
Definition: Mapping.h:133
virtual std::vector< double > getBranchLengths() const
Definition: Mapping.h:218
virtual const Tree & getTree() const =0
bool isEmpty() const
Definition: Mapping.h:176
AbstractMapping(const AbstractMapping &absm)
Definition: Mapping.h:148
virtual size_t getNumberOfSites() const =0
std::string toString(T t)
std::unique_ptr< const TreeTemplate< Node > > tree_
Definition: Mapping.h:132
int getSitePosition(size_t index) const
Definition: Mapping.h:192
size_t getNumberOfSites() const
Definition: Mapping.h:204
Partial implementation of the mapping interface.
Definition: Mapping.h:128
virtual size_t getNodeIndex(int nodeId) const
Definition: Mapping.h:226