bpp-phyl  2.2.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 #ifndef NO_VIRTUAL_COV
68  Mapping* clone() const = 0;
69 #endif
70 
71  public:
72 
76  virtual const Tree& getTree() const = 0;
77 
81  virtual bool isEmpty() const = 0;
82 
86  virtual size_t getNumberOfSites() const = 0;
87 
91  virtual size_t getNumberOfBranches() const = 0;
92 
97  virtual int getSitePosition(size_t index) const = 0;
98 
102  virtual std::vector<double> getBranchLengths() const = 0;
103 
108  virtual size_t getNodeIndex(int nodeId) const throw (NodeNotFoundException) = 0;
109 
117  virtual void setSitePosition(size_t index, int position) = 0;
118  };
119 
120 
121 
122 
123 
124 
131  virtual public Mapping
132  {
133  private:
134  std::auto_ptr<const TreeTemplate<Node> > tree_;
135  std::vector<int> sitesPositions_;
136  std::vector<const Node *> nodes_;
137  size_t nbSites_;
138  size_t nbBranches_;
139 
140  public:
141  // AbstractMapping() : tree_(0), sitesPositions_(), nodes_(), nbSites_(0), nbBranches_(0) {}
142 
144  {
145  nodes_ = tree_->getNodes();
146  nodes_.pop_back(); // remove root node.
147  nbBranches_ = nodes_.size();
148  }
149 
151  tree_(dynamic_cast<const TreeTemplate<Node>*>(absm.tree_->clone())),
153  nodes_(),
154  nbSites_(absm.nbSites_),
156  {
157  nodes_ = tree_->getNodes();
158  nodes_.pop_back(); // remove root node.
159  }
160 
162  {
163  tree_.reset(dynamic_cast<const TreeTemplate<Node>*>(absm.tree_->clone()));
165  nbSites_ = absm.nbSites_;
166  nbBranches_ = absm.nbBranches_;
167  nodes_ = tree_->getNodes();
168  nodes_.pop_back(); // remove root node.
169  return *this;
170  }
171 
172 #ifndef NO_VIRTUAL_COV
173  AbstractMapping* clone() const = 0;
174 #endif
175 
176  virtual ~AbstractMapping() {}
177 
178  public:
179 
180  bool isEmpty() const { return tree_.get() == 0; }
181 
182  const TreeTemplate<Node>& getTree() const throw (Exception)
183  {
184  if (isEmpty()) throw Exception("AbstractSubstitutionMapping::getSitePosition. No tree is assigned to this map yet.");
185  return *tree_.get();
186  }
187 
188  void setTree(const Tree& tree)
189  {
190  tree_.reset(new TreeTemplate<Node>(tree));
191  nodes_ = tree_->getNodes();
192  nodes_.pop_back(); // remove root node.
193  nbBranches_ = nodes_.size();
194  }
195 
196  int getSitePosition(size_t index) const throw (Exception)
197  {
198  if (isEmpty()) throw Exception("AbstractMapping::getSitePosition. No tree is assigned to this map yet.");
199  return sitesPositions_[index];
200  }
201 
202  void setSitePosition(size_t index, int position) throw (Exception)
203  {
204  if (isEmpty()) throw Exception("AbstractMapping::setSitePosition. No tree is assigned to this map yet.");
205  sitesPositions_[index] = position;
206  }
207 
208  size_t getNumberOfSites() const { return nbSites_; }
209 
210  size_t getNumberOfBranches() const { return nbBranches_; }
211 
212  virtual const Node* getNode(size_t nodeIndex) const { return nodes_[nodeIndex]; }
213 
214  virtual void setNumberOfSites(size_t numberOfSites)
215  {
216  nbSites_ = numberOfSites;
217  sitesPositions_.resize(numberOfSites);
218  for (size_t i = 0; i < numberOfSites; i++)
219  sitesPositions_[i] = static_cast<int>(i + 1); //Default: all sizes numbered for 1 to n.
220  }
221 
222  virtual std::vector<double> getBranchLengths() const
223  {
224  std::vector<double> brLen(nbBranches_);
225  for (size_t i = 0; i < nbBranches_; i++)
226  brLen[i] = nodes_[i]->getDistanceToFather();
227  return brLen;
228  }
229 
230  virtual size_t getNodeIndex(int nodeId) const throw (NodeNotFoundException)
231  {
232  for (size_t i = 0; i < nbBranches_; i++)
233  if(nodes_[i]->getId() == nodeId) return i;
234  throw NodeNotFoundException("AbstractMapping::getNodeIndex(nodeId).", TextTools::toString(nodeId));
235  }
236 
237 
238  };
239 
240 } //end of namespace bpp.
241 
242 #endif //_MAPPING_H_
243 
Mapping * clone() const =0
const TreeTemplate< Node > & getTree() const
Definition: Mapping.h:182
virtual const Node * getNode(size_t nodeIndex) const
Definition: Mapping.h:212
AbstractMapping & operator=(const AbstractMapping &absm)
Definition: Mapping.h:161
virtual ~Mapping()
Definition: Mapping.h:65
std::auto_ptr< const TreeTemplate< Node > > tree_
Definition: Mapping.h:134
AbstractMapping(const Tree &tree)
Definition: Mapping.h:143
virtual size_t getNodeIndex(int nodeId) const =0
int getSitePosition(size_t index) const
Definition: Mapping.h:196
std::vector< const Node * > nodes_
Definition: Mapping.h:136
size_t getNumberOfBranches() const
Definition: Mapping.h:210
General interface for storing mapping data.
Definition: Mapping.h:59
virtual std::vector< double > getBranchLengths() const
Definition: Mapping.h:222
void setTree(const Tree &tree)
Definition: Mapping.h:188
The phylogenetic tree class.
bool isEmpty() const
Definition: Mapping.h:180
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:214
Exception thrown when something is wrong with a particular node.
void setSitePosition(size_t index, int position)
Set the position of a given site.
Definition: Mapping.h:202
virtual bool isEmpty() const =0
The phylogenetic node class.
Definition: Node.h:90
virtual std::vector< double > getBranchLengths() const =0
virtual ~AbstractMapping()
Definition: Mapping.h:176
std::vector< int > sitesPositions_
Definition: Mapping.h:135
virtual const Tree & getTree() const =0
AbstractMapping * clone() const =0
AbstractMapping(const AbstractMapping &absm)
Definition: Mapping.h:150
virtual size_t getNodeIndex(int nodeId) const
Definition: Mapping.h:230
virtual size_t getNumberOfSites() const =0
size_t getNumberOfSites() const
Definition: Mapping.h:208
Partial implementation of the mapping interface.
Definition: Mapping.h:130