bpp-phyl3  3.0.0
StateMap.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_PHYL_MODEL_STATEMAP_H
6 #define BPP_PHYL_MODEL_STATEMAP_H
7 
8 #include <Bpp/Clonable.h>
12 
13 
14 // From the STL:
15 #include <vector>
16 #include <string>
17 
18 namespace bpp
19 {
24  public virtual Clonable
25 {
26 public:
27  virtual ~StateMapInterface() {}
28  virtual StateMapInterface* clone() const override = 0;
29 
30 public:
34  virtual const Alphabet& alphabet() const = 0;
35 
39  virtual std::shared_ptr<const Alphabet> getAlphabet() const = 0;
40 
44  virtual size_t getNumberOfModelStates() const = 0;
45 
50  virtual std::string getStateDescription(size_t index) const = 0;
51 
57  virtual const std::vector<int>& getAlphabetStates() const = 0;
58 
63  virtual std::string getAlphabetStateAsChar(size_t index) const = 0;
64 
69  virtual int getAlphabetStateAsInt(size_t index) const = 0;
70 
75  virtual std::vector<size_t> getModelStates(const std::string& code) const = 0;
76 
81  virtual std::vector<size_t> getModelStates(int code) const = 0;
82 
88  virtual bool operator==(const StateMapInterface& sm) const = 0;
89 
95  virtual bool operator!=(const StateMapInterface& sm) const = 0;
96 };
97 
105  public virtual StateMapInterface
106 {
107 protected:
108  std::shared_ptr<const Alphabet> alphabet_;
109  std::vector<int> states_;
110 
111 public:
112  AbstractStateMap(std::shared_ptr<const Alphabet> alphabet) :
114  states_()
115  {}
116 
118  alphabet_(absm.alphabet_),
119  states_(absm.states_)
120  {}
121 
123  {
124  alphabet_ = absm.alphabet_;
125  states_ = absm.states_;
126  return *this;
127  }
128 
129 public:
130  const Alphabet& alphabet() const override { return *alphabet_; }
131  std::shared_ptr<const Alphabet> getAlphabet() const override { return alphabet_; }
132  size_t getNumberOfModelStates() const override { return states_.size(); }
133  const std::vector<int>& getAlphabetStates() const override { return states_; }
134  int getAlphabetStateAsInt(size_t index) const override { return states_[index]; }
135  std::string getAlphabetStateAsChar(size_t index) const override { return alphabet_->intToChar(states_[index]); }
136  std::vector<size_t> getModelStates(int code) const override
137  {
138  return VectorTools::whichAll(states_, code);
139  }
140  std::vector<size_t> getModelStates(const std::string& code) const override
141  {
142  return VectorTools::whichAll(states_, alphabet_->charToInt(code));
143  }
144 
145  bool operator==(const StateMapInterface& sm) const override
146  {
147  if (alphabet_->getAlphabetType() != sm.alphabet().getAlphabetType()) return false;
148  if (states_ != sm.getAlphabetStates()) return false;
149  return true;
150  }
151 
152  bool operator!=(const StateMapInterface& sm) const override
153  {
154  if (alphabet_->getAlphabetType() != sm.alphabet().getAlphabetType()) return true;
155  if (states_ != sm.getAlphabetStates()) return true;
156  return false;
157  }
158 };
159 
167  public AbstractStateMap
168 {
169 public:
170  CanonicalStateMap(std::shared_ptr<const Alphabet> alphabet, bool includeGaps);
171 
176  CanonicalStateMap(const StateMapInterface& sm, bool includeGaps);
177 
178  virtual CanonicalStateMap* clone() const { return new CanonicalStateMap(*this); }
179 
180  virtual std::string getStateDescription(size_t index) const { return getAlphabetStateAsChar(index); }
181 };
182 
183 
191  public AbstractStateMap
192 {
193 private:
194  unsigned int nbClasses_;
195 
196 public:
197  MarkovModulatedStateMap(const StateMapInterface& unitMap, unsigned int nbClasses);
198  virtual MarkovModulatedStateMap* clone() const { return new MarkovModulatedStateMap(*this); }
199 
200  virtual std::string getStateDescription(size_t index) const { return getAlphabetStateAsChar(index) + TextTools::toString(index % nbClasses_); }
201 };
202 } // end of namespace bpp
203 #endif // BPP_PHYL_MODEL_STATEMAP_H
A convenience partial implementation of the StateMap interface.
Definition: StateMap.h:106
size_t getNumberOfModelStates() const override
Definition: StateMap.h:132
AbstractStateMap(std::shared_ptr< const Alphabet > alphabet)
Definition: StateMap.h:112
std::shared_ptr< const Alphabet > getAlphabet() const override
Definition: StateMap.h:131
const Alphabet & alphabet() const override
Definition: StateMap.h:130
AbstractStateMap & operator=(const AbstractStateMap &absm)
Definition: StateMap.h:122
bool operator!=(const StateMapInterface &sm) const override
Comparison operator!=.
Definition: StateMap.h:152
AbstractStateMap(const AbstractStateMap &absm)
Definition: StateMap.h:117
std::string getAlphabetStateAsChar(size_t index) const override
Definition: StateMap.h:135
std::vector< size_t > getModelStates(int code) const override
Definition: StateMap.h:136
std::vector< size_t > getModelStates(const std::string &code) const override
Definition: StateMap.h:140
int getAlphabetStateAsInt(size_t index) const override
Definition: StateMap.h:134
const std::vector< int > & getAlphabetStates() const override
Definition: StateMap.h:133
std::shared_ptr< const Alphabet > alphabet_
Definition: StateMap.h:108
bool operator==(const StateMapInterface &sm) const override
Comparison operator==.
Definition: StateMap.h:145
std::vector< int > states_
Definition: StateMap.h:109
virtual std::string getAlphabetType() const=0
This class implements a state map where all resolved states are modeled.
Definition: StateMap.h:168
virtual std::string getStateDescription(size_t index) const
Definition: StateMap.h:180
CanonicalStateMap(std::shared_ptr< const Alphabet > alphabet, bool includeGaps)
Definition: StateMap.cpp:10
virtual CanonicalStateMap * clone() const
Definition: StateMap.h:178
This class implements a state map for Markov modulated models.
Definition: StateMap.h:192
virtual MarkovModulatedStateMap * clone() const
Definition: StateMap.h:198
MarkovModulatedStateMap(const StateMapInterface &unitMap, unsigned int nbClasses)
Definition: StateMap.cpp:36
virtual std::string getStateDescription(size_t index) const
Definition: StateMap.h:200
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:25
virtual std::string getAlphabetStateAsChar(size_t index) const =0
virtual const Alphabet & alphabet() const =0
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
virtual bool operator!=(const StateMapInterface &sm) const =0
Comparison operator!=.
virtual ~StateMapInterface()
Definition: StateMap.h:27
virtual size_t getNumberOfModelStates() const =0
virtual bool operator==(const StateMapInterface &sm) const =0
Comparison operator==.
virtual std::string getStateDescription(size_t index) const =0
virtual std::vector< size_t > getModelStates(const std::string &code) const =0
virtual int getAlphabetStateAsInt(size_t index) const =0
virtual std::vector< size_t > getModelStates(int code) const =0
virtual StateMapInterface * clone() const override=0
virtual const std::vector< int > & getAlphabetStates() const =0
static std::vector< size_t > whichAll(const std::vector< T > &v, const T &which)
std::string toString(T t)
Defines the basic types of data flow nodes.