bpp-seq  2.2.0
DistanceMatrix.h
Go to the documentation of this file.
1 //
2 // File: DistanceMatrix.h
3 // Created on: Wed jun 08 10:39 2005
4 //
5 
6 /*
7 Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
8 
9 This software is a computer program whose purpose is to provide classes
10 for phylogenetic data analysis.
11 
12 This software is governed by the CeCILL license under French law and
13 abiding by the rules of distribution of free software. You can use,
14 modify and/ or redistribute the software under the terms of the CeCILL
15 license as circulated by CEA, CNRS and INRIA at the following URL
16 "http://www.cecill.info".
17 
18 As a counterpart to the access to the source code and rights to copy,
19 modify and redistribute granted by the license, users are provided only
20 with a limited warranty and the software's author, the holder of the
21 economic rights, and the successive licensors have only limited
22 liability.
23 
24 In this respect, the user's attention is drawn to the risks associated
25 with loading, using, modifying and/or developing or reproducing the
26 software by the user in light of its specific status of free software,
27 that may mean that it is complicated to manipulate, and that also
28 therefore means that it is reserved for developers and experienced
29 professionals having in-depth computer knowledge. Users are therefore
30 encouraged to load and test the software's suitability as regards their
31 requirements in conditions enabling the security of their systems and/or
32 data to be ensured and, more generally, to use and operate it in the
33 same conditions as regards security.
34 
35 The fact that you are presently reading this means that you have had
36 knowledge of the CeCILL license and that you accept its terms.
37 */
38 
39 #ifndef _DISTANCEMATRIX_H_
40 #define _DISTANCEMATRIX_H_
41 
42 // From the STL:
43 #include <vector>
44 #include <string>
45 #include <Bpp/Exceptions.h>
46 #include <Bpp/Numeric/VectorExceptions.h> //DimensionException
47 #include <Bpp/Numeric/Matrix/Matrix.h>
48 
49 namespace bpp
50 {
51 
56  public virtual Clonable
57 {
58 
59  private:
60  RowMatrix<double> distances_;
61  std::vector<std::string> names_;
62 
63  public:
64 
72  DistanceMatrix(const std::vector<std::string>& names):
73  distances_(names.size(), names.size()),
74  names_(names)
75  {
76  reset();
77  }
78 
86  DistanceMatrix(size_t n):
87  distances_(n, n), names_(n)
88  {
89  resize(n);
90  }
91 
92  virtual ~DistanceMatrix() {}
93 
95  distances_(dist.distances_),
96  names_(dist.names_) {}
97 
99  {
100  size_t n = dist.size();
101  resize(n);
102  for(size_t i = 0; i < n; ++i)
103  {
104  for(size_t j = 0; j < n; ++j)
105  {
106  distances_(i, j) = dist(i, j);
107  }
108  }
109  names_ = dist.names_;
110  return *this;
111  }
112 
113  DistanceMatrix* clone() const { return new DistanceMatrix(*this); }
114 
115  public:
116 
120  void reset()
121  {
122  size_t n = size();
123  for (size_t i = 0; i < n; ++i)
124  {
125  for (size_t j = 0; j < n; ++j)
126  {
127  distances_(i, j) = 0;
128  }
129  }
130  }
131 
135  size_t size() const { return names_.size(); }
136 
140  const std::vector<std::string>& getNames() const { return names_; }
141 
147  const std::string& getName(size_t i) const throw (IndexOutOfBoundsException)
148  {
149  if (i >= size()) throw IndexOutOfBoundsException("DistanceMatrix::getName. Invalid indice.", i, 0, size());
150  return names_[i];
151  }
152 
160  void setName(size_t i, const std::string& name) throw (IndexOutOfBoundsException)
161  {
162  if (i >= size()) throw IndexOutOfBoundsException("DistanceMatrix::setName. Invalid indice.", i, 0, size());
163  names_[i] = name;
164  }
165 
172  void setNames(const std::vector<std::string>& names) throw (DimensionException)
173  {
174  if (names.size() != names_.size()) throw DimensionException("DistanceMatrix::setNames. Invalid number of names.", names.size(), names_.size());
175  names_ = names;
176  }
177 
185  size_t getNameIndex(const std::string& name) const throw (Exception);
186 
192  void resize(size_t n) {
193  //RowMatrix<double>::resize(n, n);
194  distances_.resize(n, n);
195  names_.resize(n);
196  for (size_t i = 0; i < n; ++i)
197  names_[i] = "Taxon " + TextTools::toString(i);
198  reset();
199  }
200 
209  virtual const double& operator()(const std::string& iName, const std::string& jName) const throw (Exception)
210  {
211  size_t i = getNameIndex(iName);
212  size_t j = getNameIndex(jName);
213  //return operator()(i,j);
214  return distances_(i,j);
215  }
216 
225  virtual double& operator()(const std::string& iName, const std::string& jName) throw (Exception)
226  {
227  size_t i = getNameIndex(iName);
228  size_t j = getNameIndex(jName);
229  //return operator()(i,j);
230  return distances_(i,j);
231  }
232 
233  virtual const double& operator()(size_t i, size_t j) const
234  {
235  //return RowMatrix<double>::operator()(i, j);
236  return distances_(i, j);
237  }
238  virtual double& operator()(size_t i, size_t j)
239  {
240  //return RowMatrix<double>::operator()(i, j);
241  return distances_(i, j);
242  }
243 
244  virtual const Matrix<double>& asMatrix() const {
245  return distances_;
246  }
247 
248  virtual Matrix<double>& asMatrix() {
249  return distances_;
250  }
251 };
252 
253 } //end of namespace bpp.
254 
255 #endif //_DISTANCEMATRIX_H_
256 
DistanceMatrix * clone() const
void resize(size_t n)
Change the dimension of the matrix.
DistanceMatrix(const std::vector< std::string > &names)
Build a new distance matrix with specified names.
DistanceMatrix(size_t n)
Build a new distance matrix with specified size.
This alphabet is used to deal NumericAlphabet.
const std::string & getName(size_t i) const
const std::vector< std::string > & getNames() const
void setNames(const std::vector< std::string > &names)
Set the names associated to the matrix.
virtual double & operator()(const std::string &iName, const std::string &jName)
Access by name.
size_t getNameIndex(const std::string &name) const
Get the index of a given name.
void setName(size_t i, const std::string &name)
Set the ith name.
DistanceMatrix(const DistanceMatrix &dist)
size_t size() const
DistanceMatrix & operator=(const DistanceMatrix &dist)
virtual const double & operator()(size_t i, size_t j) const
virtual const Matrix< double > & asMatrix() const
std::vector< std::string > names_
virtual double & operator()(size_t i, size_t j)
virtual ~DistanceMatrix()
A Matrix class to store phylogenetic distances.
virtual Matrix< double > & asMatrix()
void reset()
Reset the distance matrix: all distances are set to 0.
virtual const double & operator()(const std::string &iName, const std::string &jName) const
Access by name.
RowMatrix< double > distances_