bpp-seq3  3.0.0
DistanceMatrix.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_SEQ_DISTANCEMATRIX_H
6 #define BPP_SEQ_DISTANCEMATRIX_H
7 
8 #include <Bpp/Exceptions.h>
10 #include <Bpp/Numeric/VectorExceptions.h> // DimensionException
11 #include <string>
12 #include <vector>
13 
14 
15 namespace bpp
16 {
18 class DistanceMatrix final : public virtual Clonable
19 {
20 private:
22  std::vector<std::string> names_;
23 
24 public:
30  DistanceMatrix(const std::vector<std::string>& names)
31  : distances_(names.size(), names.size())
32  , names_(names)
33  {
34  reset();
35  }
36 
42  DistanceMatrix(std::size_t n)
43  : distances_(n, n)
44  , names_(n)
45  {
46  resize(n);
47  }
48 
50  : distances_(dist.distances_)
51  , names_(dist.names_)
52  {}
53 
55  {
56  std::size_t n = dist.size();
57  resize(n);
58  for (std::size_t i = 0; i < n; ++i)
59  {
60  for (std::size_t j = 0; j < n; ++j)
61  {
62  distances_(i, j) = dist(i, j);
63  }
64  }
65  names_ = dist.names_;
66  return *this;
67  }
68 
69  DistanceMatrix* clone() const { return new DistanceMatrix(*this); }
70 
71 public:
75  void reset()
76  {
77  std::size_t n = size();
78  for (std::size_t i = 0; i < n; ++i)
79  {
80  for (std::size_t j = 0; j < n; ++j)
81  {
82  distances_(i, j) = 0;
83  }
84  }
85  }
86 
90  std::size_t size() const { return names_.size(); }
91 
95  const std::vector<std::string>& getNames() const { return names_; }
96 
102  const std::string& getName(std::size_t i) const
103  {
104  if (i >= size())
105  throw IndexOutOfBoundsException("DistanceMatrix::getName. Invalid indice.", i, 0, size());
106  return names_[i];
107  }
108 
116  void setName(std::size_t i, const std::string& name)
117  {
118  if (i >= size())
119  throw IndexOutOfBoundsException("DistanceMatrix::setName. Invalid indice.", i, 0, size());
120  names_[i] = name;
121  }
122 
129  void setNames(const std::vector<std::string>& names)
130  {
131  if (names.size() != names_.size())
132  throw DimensionException("DistanceMatrix::setNames. Invalid number of names.", names.size(), names_.size());
133  names_ = names;
134  }
135 
143  std::size_t getNameIndex(const std::string& name) const;
144 
150  void resize(std::size_t n)
151  {
152  // RowMatrix<double>::resize(n, n);
153  distances_.resize(n, n);
154  names_.resize(n);
155  for (std::size_t i = 0; i < n; ++i)
156  {
157  names_[i] = "Taxon " + std::to_string(i);
158  }
159  reset();
160  }
161 
170  virtual const double& operator()(const std::string& iName, const std::string& jName) const
171  {
172  std::size_t i = getNameIndex(iName);
173  std::size_t j = getNameIndex(jName);
174  // return operator()(i,j);
175  return distances_(i, j);
176  }
177 
186  virtual double& operator()(const std::string& iName, const std::string& jName)
187  {
188  std::size_t i = getNameIndex(iName);
189  std::size_t j = getNameIndex(jName);
190  // return operator()(i,j);
191  return distances_(i, j);
192  }
193 
194  virtual const double& operator()(std::size_t i, std::size_t j) const
195  {
196  // return RowMatrix<double>::operator()(i, j);
197  return distances_(i, j);
198  }
199  virtual double& operator()(std::size_t i, std::size_t j)
200  {
201  // return RowMatrix<double>::operator()(i, j);
202  return distances_(i, j);
203  }
204 
205  virtual const Matrix<double>& asMatrix() const { return distances_; }
206 
207  virtual Matrix<double>& asMatrix() { return distances_; }
208 };
209 } // end of namespace bpp.
210 #endif // BPP_SEQ_DISTANCEMATRIX_H
A Matrix class to store phylogenetic distances.
const std::string & getName(std::size_t i) const
std::size_t getNameIndex(const std::string &name) const
Get the index of a given name.
DistanceMatrix(std::size_t n)
Build a new distance matrix with specified size. Row names will be named 'Taxon 0',...
RowMatrix< double > distances_
DistanceMatrix * clone() const
virtual const Matrix< double > & asMatrix() const
virtual const double & operator()(std::size_t i, std::size_t j) const
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.
void setNames(const std::vector< std::string > &names)
Set the names associated to the matrix.
virtual double & operator()(std::size_t i, std::size_t j)
virtual Matrix< double > & asMatrix()
DistanceMatrix & operator=(const DistanceMatrix &dist)
void setName(std::size_t i, const std::string &name)
Set the ith name.
std::vector< std::string > names_
DistanceMatrix(const std::vector< std::string > &names)
Build a new distance matrix with specified names. The dimension of the matrix will be equal to the nu...
std::size_t size() const
DistanceMatrix(const DistanceMatrix &dist)
const std::vector< std::string > & getNames() const
void resize(std::size_t n)
Change the dimension of the matrix.
virtual double & operator()(const std::string &iName, const std::string &jName)
Access by name.
void resize(size_t nRows, size_t nCols)
This alphabet is used to deal NumericAlphabet.