bpp-seq3  3.0.0
SimpleIndexDistance.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_ALPHABETINDEX_SIMPLEINDEXDISTANCE_H
6 #define BPP_SEQ_ALPHABETINDEX_SIMPLEINDEXDISTANCE_H
7 
8 
9 // from the STL:
10 #include <string>
11 
12 #include "AlphabetIndex1.h"
13 #include "AlphabetIndex2.h"
14 #include "../Alphabet/AlphabetExceptions.h"
15 #include <Bpp/Exceptions.h>
17 
18 namespace bpp
19 {
28  public virtual AlphabetIndex2
29 {
30 private:
31  std::shared_ptr<AlphabetIndex1> index_;
32  size_t size_;
34  bool sym_;
35 
36 public:
37  SimpleIndexDistance(std::shared_ptr<AlphabetIndex1> index) :
38  index_(index),
39  size_(index->getAlphabet()->getSize()),
40  indexMatrix_(index->getAlphabet()->getSize(), index->getAlphabet()->getSize()),
41  sym_(false)
42  {
44  }
45 
47  index_(sid.index_),
48  size_(sid.size_),
50  sym_(sid.sym_)
51  {}
52 
54  {
55  index_ = sid.index_;
56  size_ = sid.size_;
58  sym_ = sid.sym_;
59  return *this;
60  }
61 
62  virtual ~SimpleIndexDistance() {}
63 
64 public:
65  double getIndex(int state1, int state2) const override
66  {
67  return indexMatrix_(getAlphabet()->getStateIndex(state1) - 1, getAlphabet()->getStateIndex(state2) - 1);
68  }
69 
70  double getIndex(const std::string& state1, const std::string& state2) const override
71  {
72  return indexMatrix_(getAlphabet()->getStateIndex(state1) - 1, getAlphabet()->getStateIndex(state2) - 1);
73  }
74 
75  std::shared_ptr<const Alphabet> getAlphabet() const override { return index_->getAlphabet(); }
76 
77  const Alphabet& alphabet() const override { return index_->alphabet(); }
78 
79  SimpleIndexDistance* clone() const override { return new SimpleIndexDistance(*this); }
80 
81  const Matrix<double>& getIndexMatrix() const override { return indexMatrix_; }
82 
83 protected:
85  {
86  for (size_t i = 0; i < size_; ++i)
87  {
88  for (size_t j = 0; j < size_; ++j)
89  {
90  auto d = index_->getIndex(getAlphabet()->getIntCodeAt(i + 1)) - index_->getIndex(getAlphabet()->getIntCodeAt(j + 1));
91  if (sym_) d = NumTools::abs<double>(d);
92  indexMatrix_(i, j) = d;
93  }
94  }
95  }
96 
97 public:
98  void setSymmetric(bool yn)
99  {
100  sym_ = yn;
102  }
103 
104  bool isSymmetric() const override { return sym_; }
105 
109  std::shared_ptr<const AlphabetIndex1> getAlphabetIndex1() const { return index_; }
110 };
111 } // end of namespace bpp.
112 #endif // BPP_SEQ_ALPHABETINDEX_SIMPLEINDEXDISTANCE_H
Two dimensionnal alphabet index interface.
The Alphabet interface.
Definition: Alphabet.h:99
Simple dissimilarity distance.
RowMatrix< double > indexMatrix_
std::shared_ptr< const AlphabetIndex1 > getAlphabetIndex1() const
const Matrix< double > & getIndexMatrix() const override
double getIndex(int state1, int state2) const override
Get the index associated to a pair of states.
const Alphabet & alphabet() const override
Get the alphabet associated to this index.
std::shared_ptr< const Alphabet > getAlphabet() const override
Get the alphabet associated to this index.
bool isSymmetric() const override
SimpleIndexDistance(std::shared_ptr< AlphabetIndex1 > index)
std::shared_ptr< AlphabetIndex1 > index_
SimpleIndexDistance & operator=(const SimpleIndexDistance &sid)
double getIndex(const std::string &state1, const std::string &state2) const override
Get the index associated to a pair of states.
SimpleIndexDistance * clone() const override
SimpleIndexDistance(const SimpleIndexDistance &sid)
This alphabet is used to deal NumericAlphabet.