bpp-seq3  3.0.0
IntSymbolList.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_INTSYMBOLLIST_H
6 #define BPP_SEQ_INTSYMBOLLIST_H
7 
8 #include <Bpp/Clonable.h>
9 
10 #include "Alphabet/Alphabet.h"
11 #include "SymbolList.h"
12 #include "StringSequenceTools.h"
13 
14 // From the STL:
15 #include <string>
16 #include <vector>
17 #include <algorithm>
18 #include <iostream>
19 
20 namespace bpp
21 {
28  public virtual IntCoreSymbolListInterface
29 {
30 public:
31  typedef int SymbolType;
32 
33 public:
35 
36  // Class destructor
38 
39 public:
46 
53  virtual void setContent(const std::vector<std::string>& list) = 0;
54 
62 
68  virtual void addElement(const std::string& c) = 0;
69 
76  virtual void addElement(size_t pos, const std::string& c) = 0;
77 
84  virtual void setElement(size_t pos, const std::string& c) = 0;
85 
91  virtual std::string getChar(size_t pos) const = 0;
92 
94 };
95 
96 
110  public virtual IntSymbolListInterface,
111  public virtual AbstractTemplateSymbolList<int> // This needs to be virtual because of diamond inheritance
112 {
113 public:
114  IntSymbolList(std::shared_ptr<const Alphabet> alpha) :
115  AbstractTemplateSymbolList<int>(alpha)
116  {}
117 
125  IntSymbolList(const std::vector<std::string>& list, std::shared_ptr<const Alphabet> alpha) :
126  AbstractTemplateSymbolList<int>(alpha)
127  {
128  setContent(list);
129  }
130 
138  IntSymbolList(const std::vector<int>& list, std::shared_ptr<const Alphabet> alpha) :
139  AbstractTemplateSymbolList<int>(list, alpha)
140  {}
141 
146  AbstractTemplateSymbolList<int>(list)
147  {}
148 
153 
158  {
160  return *this;
161  }
162 
168  IntSymbolList* clone() const override { return new IntSymbolList(*this); }
171  // Class destructor
172  virtual ~IntSymbolList() {}
173 
174 public:
175  void setContent(const std::vector<int>& list) override;
176 
177  void setContent(const std::vector<std::string>& list) override;
178 
179  std::string toString() const override
180  {
181  auto alphaPtr = getAlphabet();
183  }
184 
185  void addElement(const std::string& c) override
186  {
187  content_.push_back(getAlphabet()->charToInt(c));
188  }
189 
191 
192  void addElement(size_t pos, const std::string& c) override
193  {
194  if (pos >= content_.size())
195  throw IndexOutOfBoundsException("IntSymbolList::addElement. Invalid position.", pos, 0, size() - 1);
196  content_.insert(content_.begin() + static_cast<ptrdiff_t>(pos), getAlphabet()->charToInt(c));
197  }
198 
200 
201  void setElement(size_t pos, const std::string& c) override
202  {
203  if (pos >= content_.size())
204  throw IndexOutOfBoundsException("IntSymbolList::setElement. Invalid position.", pos, 0, size() - 1);
205  content_[pos] = getAlphabet()->charToInt(c);
206  }
207 
208  std::string getChar(size_t pos) const override;
209 
210  double getStateValueAt(size_t siteIndex, int state) const override
211  {
212  if (siteIndex >= content_.size())
213  throw IndexOutOfBoundsException("IntSymbolList::getStateValueAt.", siteIndex, 0, content_.size() - 1);
214 
215  return getAlphabet()->isResolvedIn(content_[siteIndex], state) ? 1. : 0.;
216  }
217 
218  double operator()(size_t siteIndex, int state) const override
219  {
220  return getAlphabet()->isResolvedIn(content_[siteIndex], state) ? 1. : 0.;
221  }
222 };
223 
225 
227 
229 
231 
233 
248  public IntSymbolList,
250 {
251 public:
257  EventDrivenIntSymbolList(std::shared_ptr<const Alphabet> alpha) :
258  AbstractTemplateSymbolList<int>(alpha),
259  IntSymbolList(alpha),
261  {}
262 
271  const std::vector<std::string>& list,
272  std::shared_ptr<const Alphabet> alpha) :
273  AbstractTemplateSymbolList<int>(alpha),
274  IntSymbolList(alpha),
276  {
277  setContent(list);
278  }
279 
280 
289  const std::vector<int>& list,
290  std::shared_ptr<const Alphabet> alpha) :
291  AbstractTemplateSymbolList<int>(list, alpha),
292  IntSymbolList(list, alpha),
293  AbstractTemplateEventDrivenSymbolList<int>(list, alpha)
294  {}
295 
296 
301  AbstractTemplateSymbolList<int>(list),
302  IntSymbolList(list),
304  {}
305 
310  AbstractTemplateSymbolList<int>(list),
311  IntSymbolList(list),
313  {}
314 
315 
320  {
324  return *this;
325  }
326 
327 
332  {
336  return *this;
337  }
338 
344  EventDrivenIntSymbolList* clone() const override { return new EventDrivenIntSymbolList(*this); }
348 
349 public:
350  virtual void setContent(const std::vector<std::string>& list) override;
351 
352  void setContent(const std::vector<int>& list) override;
353 
354  virtual std::string toString() const override;
355 
357 
358  virtual void addElement(const std::string& c) override;
359 
360  virtual void addElement(size_t pos, const std::string& c) override;
361 
363 
364  virtual void setElement(size_t pos, const std::string& c) override;
365 
366  virtual std::string getChar(size_t pos) const override;
367 
368  void addIntSymbolListListener(std::shared_ptr<IntSymbolListListener> listener)
369  {
371  }
372 
373 protected:
374  virtual void beforeSequenceChanged(const IntSymbolListEditionEvent& event) override {}
375  virtual void afterSequenceChanged(const IntSymbolListEditionEvent& event) override {}
376  virtual void beforeSequenceInserted(const IntSymbolListInsertionEvent& event) override {}
377  virtual void afterSequenceInserted(const IntSymbolListInsertionEvent& event) override {}
378  virtual void beforeSequenceDeleted(const IntSymbolListDeletionEvent& event) override {}
379  virtual void afterSequenceDeleted(const IntSymbolListDeletionEvent& event) override {}
380  virtual void beforeSequenceSubstituted(const IntSymbolListSubstitutionEvent& event) override {}
381  virtual void afterSequenceSubstituted(const IntSymbolListSubstitutionEvent& event) override {}
382 };
383 } // end of namespace bpp.
384 #endif // BPP_SEQ_INTSYMBOLLIST_H
A partial implementation of a EventDrivenSymbolList object.
Definition: SymbolList.h:210
AbstractTemplateEventDrivenSymbolList< T > & operator=(const TemplateCoreSymbolListInterface< T > &list)
The generic assignment operator.
Definition: SymbolList.h:274
virtual const CoreSymbolListListener< int > & listener(size_t i) const override
Definition: SymbolList.h:367
virtual void addCoreSymbolListListener(std::shared_ptr< CoreSymbolListListener< int >> listener) override
Definition: SymbolList.h:391
A partial implementation of a SymbolList object.
Definition: SymbolList.h:34
void addElement(const T &c) override
Add a character to the end of the list.
Definition: SymbolList.h:152
void setElement(size_t pos, const T &c) override
Set the element at position 'pos' to character 'c'.
Definition: SymbolList.h:165
std::shared_ptr< const Alphabet > getAlphabet() const override
Definition: SymbolList.h:120
size_t size() const override
Definition: SymbolList.h:124
std::vector< int > content_
The list content.
Definition: SymbolList.h:47
AbstractTemplateSymbolList< T > & operator=(const TemplateCoreSymbolListInterface< T > &list)
The generic assignment operator.
Definition: SymbolList.h:95
A event-driven IntSymbolList object.
EventDrivenIntSymbolList(const IntSymbolList &list)
The generic copy constructor.
virtual void beforeSequenceInserted(const IntSymbolListInsertionEvent &event) override
EventDrivenIntSymbolList * clone() const override
virtual std::string toString() const override
Convert the list as a string.
virtual void afterSequenceSubstituted(const IntSymbolListSubstitutionEvent &event) override
virtual void beforeSequenceSubstituted(const IntSymbolListSubstitutionEvent &event) override
EventDrivenIntSymbolList(std::shared_ptr< const Alphabet > alpha)
Build a new void EventDrivenIntSymbolList object with the specified alphabet.
EventDrivenIntSymbolList(const std::vector< std::string > &list, std::shared_ptr< const Alphabet > alpha)
Build a new EventDrivenIntSymbolList object with the specified alphabet. The content of the site is i...
EventDrivenIntSymbolList(const EventDrivenIntSymbolList &list)
The copy constructor.
void addIntSymbolListListener(std::shared_ptr< IntSymbolListListener > listener)
virtual void beforeSequenceDeleted(const IntSymbolListDeletionEvent &event) override
EventDrivenIntSymbolList & operator=(const EventDrivenIntSymbolList &list)
The assignment operator.
virtual void afterSequenceInserted(const IntSymbolListInsertionEvent &event) override
EventDrivenIntSymbolList & operator=(const IntSymbolList &list)
The generic assignment operator.
virtual void afterSequenceDeleted(const IntSymbolListDeletionEvent &event) override
virtual void afterSequenceChanged(const IntSymbolListEditionEvent &event) override
EventDrivenIntSymbolList(const std::vector< int > &list, std::shared_ptr< const Alphabet > alpha)
Build a new EventDrivenIntSymbolList object with the specified alphabet. The content of the site is i...
virtual void beforeSequenceChanged(const IntSymbolListEditionEvent &event) override
virtual std::string getChar(size_t pos) const override
Get the element at position 'pos' as a character.
The specific IntSymbolList interface.
Definition: IntSymbolList.h:29
virtual std::string getChar(size_t pos) const =0
Get the element at position 'pos' as a character.
virtual void setElement(size_t pos, const std::string &c)=0
Set the element at position 'pos' to character 'c'.
virtual void setContent(const std::vector< T > &list)=0
Set the whole content of the list.
virtual void addElement(size_t pos, const std::string &c)=0
Add a character at a certain position in the list.
virtual void addElement(const std::string &c)=0
Add a character to the end of the list.
virtual void setContent(const std::vector< std::string > &list)=0
Set the whole content of the list.
A basic IntSymbolList object.
void addElement(const T &c) override
Definition: SymbolList.h:152
void setElement(size_t pos, const std::string &c) override
Set the element at position 'pos' to character 'c'.
virtual ~IntSymbolList()
void addElement(size_t pos, const std::string &c) override
Add a character at a certain position in the list.
double getStateValueAt(size_t siteIndex, int state) const override
get value of a state at a position
IntSymbolList & operator=(const IntSymbolListInterface &list)
The generic assignment operator.
IntSymbolList(std::shared_ptr< const Alphabet > alpha)
IntSymbolList * clone() const override
IntSymbolList & operator=(const IntSymbolList &list)
The assignment operator.
std::string getChar(size_t pos) const override
Get the element at position 'pos' as a character.
IntSymbolList(const std::vector< int > &list, std::shared_ptr< const Alphabet > alpha)
Build a new IntSymbolList object with the specified alphabet. The content of the site is initialized ...
void setElement(size_t pos, const T &c) override
Definition: SymbolList.h:165
std::string toString() const override
Convert the list as a string.
double operator()(size_t siteIndex, int state) const override
get value of a state at a position
void addElement(const std::string &c) override
Add a character to the end of the list.
IntSymbolList(const std::vector< std::string > &list, std::shared_ptr< const Alphabet > alpha)
Build a new IntSymbolList object with the specified alphabet. The content of the site is initialized ...
IntSymbolList(const IntSymbolList &list)
The copy constructor.
static std::string decodeSequence(const std::vector< int > &sequence, std::shared_ptr< const Alphabet > &alphabet)
Convert a sequence to its string representation.
The CoreSymbolList interface.
virtual void addElement(const T &c)=0
Add a character to the end of the list.
virtual void setElement(size_t pos, const T &c)=0
Set the element at position 'pos' to character 'c'.
virtual void setContent(const std::vector< T > &list)=0
Set the whole content of the list.
This alphabet is used to deal NumericAlphabet.
CoreSymbolListEditionEvent< int > IntSymbolListEditionEvent
CoreSymbolListListener< int > IntSymbolListListener
CoreSymbolListSubstitutionEvent< int > IntSymbolListSubstitutionEvent
CoreSymbolListInsertionEvent< int > IntSymbolListInsertionEvent
CoreSymbolListDeletionEvent< int > IntSymbolListDeletionEvent