2
// File: SubstitutionMapping.h
3
// Created by: Julien Dutheil
4
// Created on: Wed Apr 5 09:51 2005
8
Copyright or © or Copr. Bio++ Development Team, (November 16, 2004, 2005, 2006)
10
This software is a computer program whose purpose is to provide classes
11
for phylogenetic data analysis.
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".
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
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.
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.
40
#ifndef _SUBSTITUTIONMAPPING_H_
41
#define _SUBSTITUTIONMAPPING_H_
44
#include "../TreeTemplate.h"
46
#include <Bpp/Clonable.h>
56
* @brief General interface for storing mapping data.
58
* There are several kinds of mapping:
59
* - Exact mapping, storing the positions of each substitution onto each branch,
60
* - Probabilistic mapping, storing the number of substitutions onto each branch.
62
* Since only probabilistic substitution mapping is implemented for now, the basal
63
* interface only contains a few methods.
64
* More methods are expected to be added later.
66
class SubstitutionMapping:
67
public virtual Clonable
71
SubstitutionMapping() {}
72
virtual ~SubstitutionMapping() {}
74
#ifndef NO_VIRTUAL_COV
75
SubstitutionMapping* clone() const = 0;
81
* @return Get the phylogenetic tree associated to this mapping.
83
virtual const Tree& getTree() const = 0;
86
* @return True is the map is empty, that is, if no tree is associated to the map yet.
88
virtual bool isEmpty() const = 0;
91
* @return The number of sites mapped.
93
virtual unsigned int getNumberOfSites() const = 0;
96
* @return The number of branches mapped.
98
virtual unsigned int getNumberOfBranches() const = 0;
101
* @return The number of distinct types of substitutions mapped.
103
virtual unsigned int getNumberOfSubstitutionTypes() const = 0;
106
* @param index The site index.
107
* @return The site position corresponding to the index.
109
virtual int getSitePosition(unsigned int index) const = 0;
112
* @return A vector with all tree branch lengths.
114
virtual std::vector<double> getBranchLengths() const = 0;
117
* @param nodeId An id of the node to look for in the map.
118
* @return The mapping index for the specified node id.
120
virtual unsigned int getNodeIndex(int nodeId) const throw (NodeNotFoundException) = 0;
123
* @brief Set the position of a given site.
125
* @warning No index checking is performed, use with care!
126
* @param index The site index.
127
* @param position The position of the site.
129
virtual void setSitePosition(unsigned int index, int position) = 0;
131
virtual double& operator()(unsigned int nodeIndex, unsigned int siteIndex, unsigned int type) = 0;
132
virtual const double& operator()(unsigned int nodeIndex, unsigned int siteIndex, unsigned int type) const = 0;
141
* @brief Partial implementation of the substitution mapping interface.
143
* This implementation copies the input tree in a TreeTemplate<Node> object.
145
class AbstractSubstitutionMapping:
146
public SubstitutionMapping
149
std::auto_ptr<const TreeTemplate<Node> > tree_;
150
std::vector<int> sitesPositions_;
151
std::vector<const Node *> nodes_;
152
unsigned int nbSites_;
153
unsigned int nbBranches_;
156
AbstractSubstitutionMapping() : tree_(0), sitesPositions_(), nodes_(), nbSites_(0), nbBranches_(0) {}
158
AbstractSubstitutionMapping(const Tree& tree) : tree_(new TreeTemplate<Node>(tree)), sitesPositions_(), nodes_(), nbSites_(0), nbBranches_(0)
160
nodes_ = tree_->getNodes();
161
nodes_.pop_back(); // remove root node.
162
nbBranches_ = nodes_.size();
165
AbstractSubstitutionMapping(const AbstractSubstitutionMapping& absm):
166
tree_(dynamic_cast<const TreeTemplate<Node>*>(absm.tree_->clone())),
167
sitesPositions_(absm.sitesPositions_),
169
nbSites_(absm.nbSites_),
170
nbBranches_(absm.nbBranches_)
172
nodes_ = tree_->getNodes();
173
nodes_.pop_back(); // remove root node.
176
AbstractSubstitutionMapping& operator=(const AbstractSubstitutionMapping& absm)
178
tree_.reset(dynamic_cast<const TreeTemplate<Node>*>(absm.tree_->clone()));
179
sitesPositions_ = absm.sitesPositions_;
180
nbSites_ = absm.nbSites_;
181
nbBranches_ = absm.nbBranches_;
182
nodes_ = tree_->getNodes();
183
nodes_.pop_back(); // remove root node.
187
virtual ~AbstractSubstitutionMapping() {}
191
bool isEmpty() const { return tree_.get() == 0; }
193
const TreeTemplate<Node>& getTree() const throw (Exception)
195
if (isEmpty()) throw Exception("AbstractSubstitutionMapping::getSitePosition. No tree is assigned to this map yet.");
199
void setTree(const Tree& tree)
201
tree_.reset(new TreeTemplate<Node>(tree));
202
nodes_ = tree_->getNodes();
203
nodes_.pop_back(); // remove root node.
204
nbBranches_ = nodes_.size();
207
int getSitePosition(unsigned int index) const throw (Exception)
209
if (isEmpty()) throw Exception("AbstractSubstitutionMapping::getSitePosition. No tree is assigned to this map yet.");
210
return sitesPositions_[index];
213
void setSitePosition(unsigned int index, int position) throw (Exception)
215
if (isEmpty()) throw Exception("AbstractSubstitutionMapping::setSitePosition. No tree is assigned to this map yet.");
216
sitesPositions_[index] = position;
219
unsigned int getNumberOfSites() const { return nbSites_; }
221
unsigned int getNumberOfBranches() const { return nbBranches_; }
223
virtual const Node* getNode(unsigned int nodeIndex) const { return nodes_[nodeIndex]; }
225
virtual void setNumberOfSites(unsigned int numberOfSites)
227
nbSites_ = numberOfSites;
228
sitesPositions_.resize(numberOfSites);
229
for (unsigned int i = 0; i < numberOfSites; i++)
230
sitesPositions_[i] = i + 1; //Default: all sizes numbered for 1 to n.
233
virtual std::vector<double> getBranchLengths() const
235
std::vector<double> brLen(nbBranches_);
236
for (unsigned int i = 0; i < nbBranches_; i++)
237
brLen[i] = nodes_[i]->getDistanceToFather();
241
virtual unsigned int getNodeIndex(int nodeId) const throw (NodeNotFoundException)
243
for (unsigned int i = 0; i < nbBranches_; i++)
244
if(nodes_[i]->getId() == nodeId) return i;
245
throw NodeNotFoundException("ProbabilisticSubstitutionMapping::getNodeIndex(nodeId).", TextTools::toString(nodeId));
251
} //end of namespace bpp.
253
#endif //_SUBSTITUTIONMAPPING_H_