bpp-seq3  3.0.0
SequenceWithAnnotation.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_SEQUENCEWITHANNOTATION_H
6 #define BPP_SEQ_SEQUENCEWITHANNOTATION_H
7 
8 
9 #include "CoreSequence.h"
10 #include "IntSymbolList.h"
11 #include "Sequence.h"
12 
13 // From the STL:
14 #include <string>
15 #include <vector>
16 
17 namespace bpp
18 {
19 class SequenceWithAnnotation;
20 
25  public virtual IntSymbolListListener
26 {
27 public:
28  virtual SequenceAnnotation* clone() const override = 0;
29 
35  virtual void init(const Sequence& seq) = 0;
36 
40  virtual const std::string& getType() const = 0;
41 
49  virtual bool isValidWith(const SequenceWithAnnotation& sequence, bool throwException = true) const = 0;
50 
57  virtual bool merge(const SequenceAnnotation& anno) = 0;
58 
65  virtual std::unique_ptr<SequenceAnnotation> getPartAnnotation(size_t pos, size_t len) const = 0;
66 };
67 
86  public virtual SequenceInterface,
87  public AbstractCoreSequence,
89 {
90 public:
99  SequenceWithAnnotation(std::shared_ptr<const Alphabet>& alpha) :
103  {}
104 
105 
119  const std::string& name,
120  const std::string& sequence,
121  std::shared_ptr<const Alphabet>& alpha) :
123  AbstractCoreSequence(name),
125  {
126  if (sequence != "")
127  setContent(sequence);
128  }
129 
130 
146  const std::string& name,
147  const std::string& sequence,
148  const Comments& comments,
149  std::shared_ptr<const Alphabet>& alpha) :
151  AbstractCoreSequence(name, comments),
153  {
154  if (sequence != "")
155  setContent(sequence);
156  }
157 
158 
170  const std::string& name,
171  const std::vector<std::string>& sequence,
172  std::shared_ptr<const Alphabet>& alpha) :
174  AbstractCoreSequence(name),
175  EventDrivenIntSymbolList(sequence, alpha)
176  {}
177 
178 
191  const std::string& name,
192  const std::vector<std::string>& sequence,
193  const Comments& comments,
194  std::shared_ptr<const Alphabet>& alpha) :
196  AbstractCoreSequence(name, comments),
197  EventDrivenIntSymbolList(sequence, alpha)
198  {}
199 
200 
209  const std::string& name,
210  const std::vector<int>& sequence,
211  std::shared_ptr<const Alphabet>& alpha) :
212  AbstractTemplateSymbolList(sequence, alpha),
213  AbstractCoreSequence(name),
214  EventDrivenIntSymbolList(sequence, alpha)
215  {}
216 
217 
227  const std::string& name,
228  const std::vector<int>& sequence,
229  const Comments& comments,
230  std::shared_ptr<const Alphabet>& alpha) :
231  AbstractTemplateSymbolList(sequence, alpha),
232  AbstractCoreSequence(name, comments),
233  EventDrivenIntSymbolList(sequence, alpha)
234  {}
235 
236 
244  {}
245 
246 
254  {}
255 
256 
263  {
264  setContent(s.getContent());
265  setName(s.getName());
267  return *this;
268  }
269 
270 
277  {
280  return *this;
281  }
282 
283 
285 
286 public:
292  SequenceWithAnnotation* clone() const override { return new SequenceWithAnnotation(*this); }
308  virtual void setContent(const std::string& sequence) override;
309 
310  void setContent(const std::vector<std::string>& list) override
311  {
313  }
314 
315  void setContent(const std::vector<int>& list) override
316  {
318  }
319 
320  void setToSizeR(size_t newSize) override;
321 
322  void setToSizeL(size_t newSize) override;
323 
330  void append(const SequenceInterface& seq) override;
331 
332  void append(const std::vector<int>& content) override;
333 
334  void append(const std::vector<std::string>& content) override;
335 
336  void append(const std::string& content) override;
337 
349  virtual void addAnnotation(std::shared_ptr<SequenceAnnotation> anno)
350  {
351  anno->isValidWith(*this);
353  }
354 
355  virtual bool hasAnnotation(const std::string& type) const
356  {
357  for (size_t i = 0; i < getNumberOfListeners(); ++i)
358  {
359  const auto& lstn = listener(i);
360  try
361  {
362  const SequenceAnnotation& anno = dynamic_cast<const SequenceAnnotation&>(lstn);
363  if (anno.getType() == type) return true;
364  }
365  catch (std::bad_cast&) {}
366  }
367  return false;
368  }
369 
370 
371  virtual const SequenceAnnotation& annotation(const std::string& type) const
372  {
373  for (size_t i = 0; i < getNumberOfListeners(); ++i)
374  {
375  const auto& lstn = listener(i);
376  try
377  {
378  const SequenceAnnotation& anno = dynamic_cast<const SequenceAnnotation&>(lstn);
379  if (anno.getType() == type) return anno;
380  }
381  catch (std::bad_cast&) {}
382  }
383  throw Exception("SequenceWithAnnotation::getAnnotation. No annotation found with type '" + type + "'.");
384  }
385 
386  virtual SequenceAnnotation& annotation(const std::string& type)
387  {
388  for (size_t i = 0; i < getNumberOfListeners(); ++i)
389  {
390  auto& lstn = listener(i);
391  try
392  {
393  SequenceAnnotation& anno = dynamic_cast<SequenceAnnotation&>(lstn);
394  if (anno.getType() == type) return anno;
395  }
396  catch (std::bad_cast&) {}
397  }
398  throw Exception("SequenceWithAnnotation::getAnnotation. No annotation found with type '" + type + "'.");
399  }
400 
404  virtual std::vector<std::string> getAnnotationTypes() const;
405 
418  virtual void merge(const SequenceWithAnnotation& swa);
419 };
420 } // end of namespace bpp.
421 #endif // BPP_SEQ_SEQUENCEWITHANNOTATION_H
A partial implementation of the CoreSequence interface.
Definition: CoreSequence.h:98
const std::string & getName() const override
Get the name of this sequence.
Definition: CoreSequence.h:170
void setName(const std::string &name) override
Set the name of this sequence.
Definition: CoreSequence.h:172
AbstractCoreSequence & operator=(const AbstractCoreSequence &s)
Definition: CoreSequence.h:134
virtual size_t getNumberOfListeners() const override
Definition: SymbolList.h:365
virtual const CoreSymbolListListener< int > & listener(size_t i) const override
Definition: SymbolList.h:367
A partial implementation of a SymbolList object.
Definition: SymbolList.h:34
virtual const std::vector< T > & getContent() const override
Definition: SymbolList.h:131
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
Get the alphabet associated to the list.
A event-driven IntSymbolList object.
void addIntSymbolListListener(std::shared_ptr< IntSymbolListListener > listener)
EventDrivenIntSymbolList & operator=(const IntSymbolList &list)
The generic assignment operator.
virtual void setContent(const std::vector< T > &list)=0
Set the whole content of the list.
Interface for sequence annotations.
virtual std::unique_ptr< SequenceAnnotation > getPartAnnotation(size_t pos, size_t len) const =0
virtual const std::string & getType() const =0
virtual bool merge(const SequenceAnnotation &anno)=0
Merge the input annotation with the current one.
virtual SequenceAnnotation * clone() const override=0
virtual bool isValidWith(const SequenceWithAnnotation &sequence, bool throwException=true) const =0
Test is the annotation is valid for a given sequence.
virtual void init(const Sequence &seq)=0
The sequence interface.
Definition: Sequence.h:34
An implementation of the Sequence interface that supports annotation.
SequenceWithAnnotation(const std::string &name, const std::vector< std::string > &sequence, const Comments &comments, std::shared_ptr< const Alphabet > &alpha)
General purpose constructor, can be used with any alphabet.
void setContent(const std::vector< int > &list) override
virtual SequenceAnnotation & annotation(const std::string &type)
SequenceWithAnnotation(const Sequence &s)
The Sequence generic copy constructor.
void setContent(const std::vector< std::string > &list) override
Set the whole content of the list.
SequenceWithAnnotation(const std::string &name, const std::vector< int > &sequence, std::shared_ptr< const Alphabet > &alpha)
General purpose constructor, can be used with any alphabet.
virtual bool hasAnnotation(const std::string &type) const
SequenceWithAnnotation(const std::string &name, const std::vector< std::string > &sequence, std::shared_ptr< const Alphabet > &alpha)
General purpose constructor, can be used with any alphabet.
void setToSizeL(size_t newSize) override
Set up the size of a sequence from the left side.
void append(const SequenceInterface &seq) override
Append the content of a sequence to the current one.
SequenceWithAnnotation(const std::string &name, const std::string &sequence, const Comments &comments, std::shared_ptr< const Alphabet > &alpha)
Direct constructor: build a Sequence object from a std::string.
SequenceWithAnnotation & operator=(const SequenceWithAnnotation &s)
The Sequence assignment operator. This does not perform a hard copy of the alphabet object.
SequenceWithAnnotation * clone() const override
virtual void addAnnotation(std::shared_ptr< SequenceAnnotation > anno)
Add a new annotation to the sequence.
SequenceWithAnnotation(const SequenceWithAnnotation &s)
The Sequence copy constructor.
SequenceWithAnnotation(const std::string &name, const std::string &sequence, std::shared_ptr< const Alphabet > &alpha)
Direct constructor: build a Sequence object from a std::string You can use it safely for RNA,...
virtual const SequenceAnnotation & annotation(const std::string &type) const
SequenceWithAnnotation & operator=(const Sequence &s)
The Sequence generic assignment operator.
SequenceWithAnnotation(std::shared_ptr< const Alphabet > &alpha)
Empty constructor: build a void Sequence with just an Alphabet.
SequenceWithAnnotation(const std::string &name, const std::vector< int > &sequence, const Comments &comments, std::shared_ptr< const Alphabet > &alpha)
General purpose constructor, can be used with any alphabet.
virtual std::vector< std::string > getAnnotationTypes() const
void setToSizeR(size_t newSize) override
Set up the size of a sequence from the right side.
virtual void merge(const SequenceWithAnnotation &swa)
Merge a sequence with the current one.
A basic implementation of the Sequence interface.
Definition: Sequence.h:117
const Comments & getComments() const override
Get the comments.
Definition: Commentable.h:79
void setComments(const Comments &comments) override
Set the comments.
Definition: Commentable.h:86
virtual const std::vector< T > & getContent() const =0
This alphabet is used to deal NumericAlphabet.
std::vector< std::string > Comments
Declaration of Comments type.
Definition: Commentable.h:21