~ubuntu-branches/ubuntu/saucy/libbpp-phyl/saucy-proposed

« back to all changes in this revision

Viewing changes to src/Bpp/Phyl/Mapping/SubstitutionMapping.h

  • Committer: Bazaar Package Importer
  • Author(s): Julien Dutheil
  • Date: 2011-06-09 11:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110609110000-yvx78svv6w7xxgph
Tags: upstream-2.0.2
Import upstream version 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// File: SubstitutionMapping.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 _SUBSTITUTIONMAPPING_H_
 
41
#define _SUBSTITUTIONMAPPING_H_
 
42
 
 
43
#include "../Tree.h"
 
44
#include "../TreeTemplate.h"
 
45
 
 
46
#include <Bpp/Clonable.h>
 
47
 
 
48
//From the STL:
 
49
#include <vector>
 
50
#include <memory>
 
51
 
 
52
namespace bpp
 
53
{
 
54
 
 
55
/**
 
56
 * @brief General interface for storing mapping data.
 
57
 *
 
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.
 
61
 *
 
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.
 
65
 */
 
66
class SubstitutionMapping:
 
67
  public virtual Clonable
 
68
{
 
69
 
 
70
  public:
 
71
    SubstitutionMapping() {}
 
72
    virtual ~SubstitutionMapping() {}
 
73
 
 
74
#ifndef NO_VIRTUAL_COV
 
75
    SubstitutionMapping* clone() const = 0;
 
76
#endif
 
77
 
 
78
  public:
 
79
    
 
80
    /**
 
81
     * @return Get the phylogenetic tree associated to this mapping.
 
82
     */
 
83
    virtual const Tree& getTree() const = 0;
 
84
    
 
85
    /**
 
86
     * @return True is the map is empty, that is, if no tree is associated to the map yet.
 
87
     */
 
88
    virtual bool isEmpty() const = 0;
 
89
 
 
90
    /**
 
91
     * @return The number of sites mapped.
 
92
     */
 
93
    virtual unsigned int getNumberOfSites() const = 0;
 
94
    
 
95
    /**
 
96
     * @return The number of branches mapped.
 
97
     */
 
98
    virtual unsigned int getNumberOfBranches() const = 0;
 
99
    
 
100
    /**
 
101
     * @return The number of distinct types of substitutions mapped.
 
102
     */
 
103
    virtual unsigned int getNumberOfSubstitutionTypes() const = 0;
 
104
    
 
105
    /**
 
106
     * @param index The site index.
 
107
     * @return The site position corresponding to the index.
 
108
     */
 
109
    virtual int getSitePosition(unsigned int index) const = 0;
 
110
    
 
111
    /**
 
112
     * @return A vector with all tree branch lengths.
 
113
     */
 
114
    virtual std::vector<double> getBranchLengths() const = 0;
 
115
    
 
116
    /**
 
117
     * @param nodeId An id of the node to look for in the map.
 
118
     * @return The mapping index for the specified node id.
 
119
     */
 
120
    virtual unsigned int getNodeIndex(int nodeId) const throw (NodeNotFoundException) = 0;
 
121
 
 
122
    /**
 
123
     * @brief Set the position of a given site.
 
124
     *
 
125
     * @warning No index checking is performed, use with care!
 
126
     * @param index The site index.
 
127
     * @param position The position of the site.
 
128
     */
 
129
    virtual void setSitePosition(unsigned int index, int position) = 0;
 
130
 
 
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;
 
133
};
 
134
 
 
135
 
 
136
 
 
137
 
 
138
 
 
139
 
 
140
/**
 
141
 * @brief Partial implementation of the substitution mapping interface.
 
142
 *
 
143
 * This implementation copies the input tree in a TreeTemplate<Node> object.
 
144
 */
 
145
class AbstractSubstitutionMapping:
 
146
  public SubstitutionMapping
 
147
{
 
148
  private:
 
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_;
 
154
 
 
155
  public:
 
156
    AbstractSubstitutionMapping() : tree_(0), sitesPositions_(), nodes_(), nbSites_(0), nbBranches_(0) {}
 
157
 
 
158
    AbstractSubstitutionMapping(const Tree& tree) : tree_(new TreeTemplate<Node>(tree)), sitesPositions_(), nodes_(), nbSites_(0), nbBranches_(0)
 
159
    {
 
160
      nodes_ = tree_->getNodes();
 
161
      nodes_.pop_back(); // remove root node.
 
162
      nbBranches_ = nodes_.size();
 
163
    }
 
164
 
 
165
    AbstractSubstitutionMapping(const AbstractSubstitutionMapping& absm):
 
166
      tree_(dynamic_cast<const TreeTemplate<Node>*>(absm.tree_->clone())),
 
167
      sitesPositions_(absm.sitesPositions_),
 
168
      nodes_(),
 
169
      nbSites_(absm.nbSites_),
 
170
      nbBranches_(absm.nbBranches_)
 
171
    {
 
172
      nodes_ = tree_->getNodes();
 
173
      nodes_.pop_back(); // remove root node.
 
174
    }
 
175
 
 
176
    AbstractSubstitutionMapping& operator=(const AbstractSubstitutionMapping& absm)
 
177
    {
 
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.
 
184
      return *this;
 
185
    }
 
186
 
 
187
    virtual ~AbstractSubstitutionMapping() {}
 
188
 
 
189
  public:
 
190
 
 
191
    bool isEmpty() const { return tree_.get() == 0; }
 
192
 
 
193
                const   TreeTemplate<Node>& getTree() const throw (Exception)
 
194
    {
 
195
      if (isEmpty()) throw Exception("AbstractSubstitutionMapping::getSitePosition. No tree is assigned to this map yet.");
 
196
      return *tree_.get();
 
197
    }
 
198
 
 
199
    void setTree(const Tree& tree)
 
200
    {
 
201
      tree_.reset(new TreeTemplate<Node>(tree));
 
202
      nodes_ = tree_->getNodes();
 
203
      nodes_.pop_back(); // remove root node.
 
204
      nbBranches_ = nodes_.size();
 
205
    }
 
206
 
 
207
    int getSitePosition(unsigned int index) const throw (Exception)
 
208
    {
 
209
      if (isEmpty()) throw Exception("AbstractSubstitutionMapping::getSitePosition. No tree is assigned to this map yet.");
 
210
      return sitesPositions_[index];
 
211
    }
 
212
    
 
213
    void setSitePosition(unsigned int index, int position) throw (Exception)
 
214
    {
 
215
      if (isEmpty()) throw Exception("AbstractSubstitutionMapping::setSitePosition. No tree is assigned to this map yet.");
 
216
      sitesPositions_[index] = position;
 
217
    }
 
218
                
 
219
    unsigned int getNumberOfSites() const { return nbSites_; }
 
220
 
 
221
    unsigned int getNumberOfBranches() const { return nbBranches_; }
 
222
    
 
223
    virtual const Node* getNode(unsigned int nodeIndex) const { return nodes_[nodeIndex]; }
 
224
 
 
225
    virtual void setNumberOfSites(unsigned int numberOfSites)
 
226
    {
 
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.
 
231
    }
 
232
 
 
233
    virtual std::vector<double> getBranchLengths() const
 
234
    {
 
235
      std::vector<double> brLen(nbBranches_);
 
236
      for (unsigned int i = 0; i < nbBranches_; i++)
 
237
        brLen[i] = nodes_[i]->getDistanceToFather();
 
238
      return brLen;
 
239
    }
 
240
 
 
241
    virtual unsigned int getNodeIndex(int nodeId) const throw (NodeNotFoundException)
 
242
    {
 
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));
 
246
    }
 
247
 
 
248
 
 
249
};
 
250
 
 
251
} //end of namespace bpp.
 
252
 
 
253
#endif //_SUBSTITUTIONMAPPING_H_
 
254