bpp-seq3  3.0.0
SequenceWithQuality.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_SEQUENCEWITHQUALITY_H
6 #define BPP_SEQ_SEQUENCEWITHQUALITY_H
7 
10 
11 #include "SequenceWithAnnotation.h"
12 
13 // From the STL
14 
15 #include <string>
16 #include <vector>
17 #include <cstddef>
18 
19 namespace bpp
20 {
31  public virtual SequenceAnnotation
32 {
33 private:
34  bool removable_;
35  std::vector<int> qualScores_;
36 
37 public:
38  static const std::string QUALITY_SCORE;
39  static const int DEFAULT_QUALITY_VALUE;
40 
41 public:
56  SequenceQuality(size_t size = 0, bool removable = true) :
57  removable_(removable),
59 
60 
70  SequenceQuality(const std::vector<int>& quality, bool removable = true) :
71  removable_(removable),
72  qualScores_(quality)
73  {}
74 
81  virtual ~SequenceQuality() {}
88  SequenceQuality* clone() const override { return new SequenceQuality(*this); }
91 public:
92  void init(const Sequence& seq) override
93  {
94  qualScores_.resize(seq.size());
95  std::fill(qualScores_.begin(), qualScores_.end(), DEFAULT_QUALITY_VALUE);
96  }
97 
98  const std::string& getType() const override { return QUALITY_SCORE; }
99 
100  bool isValidWith(const SequenceWithAnnotation& sequence, bool throwException = true) const override
101  {
102  if (throwException && qualScores_.size() != sequence.size()) throw Exception("SequenceQuality. Quality scores must match the sequence size.");
103  return qualScores_.size() == sequence.size();
104  }
105 
106  bool isRemovable() const override { return removable_; }
107  bool isShared() const override { return false; }
108  void beforeSequenceChanged(const IntSymbolListEditionEvent& event) override {}
109  void afterSequenceChanged(const IntSymbolListEditionEvent& event) override;
111  void afterSequenceInserted(const IntSymbolListInsertionEvent& event) override;
112  void beforeSequenceDeleted(const IntSymbolListDeletionEvent& event) override {}
113  void afterSequenceDeleted(const IntSymbolListDeletionEvent& event) override;
116 
117  size_t getSize() const { return qualScores_.size(); }
118 
119  const int& operator[](size_t i) const { return qualScores_[i]; }
120  int& operator[](size_t i) { return qualScores_[i]; }
121 
122  void setScores(const std::vector<int>& scores)
123  {
124  if (scores.size() != qualScores_.size())
125  throw DimensionException("SequenceQuality::setScores. Trying to replace score by a vector with different length.", scores.size(), qualScores_.size());
126  qualScores_ = scores;
127  }
128 
132  const std::vector<int>& getScores() const { return qualScores_; }
133 
134  void setScore(size_t pos, int score)
135  {
136  if (pos >= qualScores_.size())
137  throw Exception("SequenceQuality::setScore. Vector overflow. Scores number: " + TextTools::toString(qualScores_.size()) + ", but trying to insert score at position " + TextTools::toString(pos) + ".");
138  qualScores_[pos] = score;
139  }
140 
141  void setScores(size_t pos, const std::vector<int>& scores)
142  {
143  if (pos + scores.size() > qualScores_.size())
144  throw Exception("SequenceQuality::setScores. Vector overflow. Scores number: " + TextTools::toString(qualScores_.size()) + ", but trying to insert " + TextTools::toString(scores.size()) + " scores at position " + TextTools::toString(pos) + ".");
145  std::copy(scores.begin(), scores.end(), qualScores_.begin() + static_cast<ptrdiff_t>(pos));
146  }
147 
148  bool merge(const SequenceAnnotation& anno) override
149  {
150  try
151  {
152  const SequenceQuality* qual = &dynamic_cast<const SequenceQuality&>(anno);
154  return true;
155  }
156  catch (std::exception& e)
157  {
158  return false;
159  }
160  }
161 
162  std::unique_ptr<SequenceAnnotation> getPartAnnotation(size_t pos, size_t len) const override
163  {
164  return std::unique_ptr<SequenceAnnotation>(new SequenceQuality(
165  std::vector<int>(
166  qualScores_.begin() + static_cast<ptrdiff_t>(pos),
167  qualScores_.begin() + static_cast<ptrdiff_t>(pos + len)),
168  removable_));
169  }
170 };
171 
172 
183 {
184 private:
185  std::shared_ptr<SequenceQuality> qualScores_;
186 
187 public:
201  std::shared_ptr<const Alphabet>& alpha) :
202  AbstractTemplateSymbolList<int>(alpha),
203  SequenceWithAnnotation(alpha),
204  qualScores_(new SequenceQuality(0, false))
205  {
207  }
208 
222  const std::string& name,
223  const std::string& sequence,
224  std::shared_ptr<const Alphabet>& alpha) :
225  AbstractTemplateSymbolList<int>(alpha),
226  SequenceWithAnnotation(name, sequence, alpha),
227  qualScores_(new SequenceQuality(sequence.size(), false))
228  {
230  }
231 
248  const std::string& name,
249  const std::string& sequence,
250  const Comments& comments,
251  std::shared_ptr<const Alphabet>& alpha) :
252  AbstractTemplateSymbolList<int>(alpha),
253  SequenceWithAnnotation(name, sequence, comments, alpha),
254  qualScores_(new SequenceQuality(sequence.size(), false))
255  {
257  }
258 
275  const std::string& name,
276  const std::string& sequence,
277  const std::vector<int>& quality,
278  std::shared_ptr<const Alphabet>& alpha) :
279  AbstractTemplateSymbolList<int>(alpha),
280  SequenceWithAnnotation(name, sequence, alpha),
281  qualScores_(new SequenceQuality(quality, false))
282  {
284  }
285 
305  const std::string& name,
306  const std::string& sequence,
307  const std::vector<int>& quality,
308  const Comments& comments,
309  std::shared_ptr<const Alphabet>& alpha) :
310  AbstractTemplateSymbolList<int>(alpha),
311  SequenceWithAnnotation(name, sequence, comments, alpha),
312  qualScores_(new SequenceQuality(quality, false))
313  {
315  }
316 
330  const std::string& name,
331  const std::vector<int>& sequence,
332  std::shared_ptr<const Alphabet>& alpha) :
333  AbstractTemplateSymbolList<int>(alpha),
334  SequenceWithAnnotation(name, sequence, alpha),
335  qualScores_(new SequenceQuality(sequence.size(), false))
336  {
338  }
339 
356  const std::string& name,
357  const std::vector<int>& sequence,
358  const Comments& comments,
359  std::shared_ptr<const Alphabet>& alpha) :
360  AbstractTemplateSymbolList<int>(alpha),
361  SequenceWithAnnotation(name, sequence, comments, alpha),
362  qualScores_(new SequenceQuality(sequence.size(), false))
363  {
365  }
366 
383  const std::string& name,
384  const std::vector<int>& sequence,
385  const std::vector<int>& quality,
386  std::shared_ptr<const Alphabet>& alpha) :
387  AbstractTemplateSymbolList<int>(alpha),
388  SequenceWithAnnotation(name, sequence, alpha),
389  qualScores_(new SequenceQuality(quality, false))
390  {
392  }
393 
413  const std::string& name,
414  const std::vector<int>& sequence,
415  const std::vector<int>& quality,
416  const Comments& comments,
417  std::shared_ptr<const Alphabet>& alpha) :
418  AbstractTemplateSymbolList<int>(alpha),
419  SequenceWithAnnotation(name, sequence, comments, alpha),
420  qualScores_(new SequenceQuality(quality, false))
421  {
423  }
424 
436  {
438  }
439 
453  const Sequence& s,
454  const std::vector<int>& sc) :
457  qualScores_(new SequenceQuality(sc, false))
458  {
460  }
461 
468  virtual ~SequenceWithQuality() {}
472  AbstractTemplateSymbolList<int>(sequence.getAlphabet()),
473  SequenceWithAnnotation(sequence), qualScores_(nullptr)
474  {
476  }
477 
479  {
482  return *this;
483  }
484 
489  SequenceWithQuality* clone() const override { return new SequenceWithQuality(*this); }
506  void setQuality(size_t pos, int quality)
507  {
508  qualScores_->setScore(pos, quality);
509  }
510 
521  int getQuality(size_t pos) const
522  {
523  if (pos >= qualScores_->getSize())
524  throw IndexOutOfBoundsException("SequenceWithQuality::getQuality: pos out of bounds", pos, 0, qualScores_->getSize() - 1);
525  return (*qualScores_)[pos];
526  }
527 
536  void setQualities(const std::vector<int>& quality)
537  {
538  if (quality.size() != qualScores_->getSize())
539  throw DimensionException("SequenceWithQuality::setQualities: quality must fit sequence size", quality.size(), qualScores_->getSize());
540  qualScores_->setScores(quality);
541  }
542 
548  const std::vector<int>& getQualities() const
549  {
550  return qualScores_->getScores();
551  }
552 
554 
566  virtual void append(
567  const std::vector<int>& content,
568  const std::vector<int>& qualities)
569  {
570  if (content.size() != qualities.size())
571  throw DimensionException("SequenceWithQuality::append: qualities must fit content size", qualities.size(), content.size());
572  size_t pos = qualScores_->getSize();
573  append(content); // This automatically extend scores array with default values through the listener
574  // Update scores:
575  qualScores_->setScores(pos, qualities);
576  }
577 
589  virtual void append(
590  const std::vector<std::string>& content,
591  const std::vector<int>& qualities)
592  {
593  if (content.size() != qualities.size())
594  throw DimensionException("SequenceWithQuality::append: qualities must fit content size", qualities.size(), content.size());
595  size_t pos = qualScores_->getSize();
596  SequenceWithAnnotation::append(content); // This automatically extend scores array with default values through the listener
597  // Update scores:
598  qualScores_->setScores(pos, qualities);
599  }
600 
612  virtual void append(
613  const std::string& content,
614  const std::vector<int>& qualities)
615  {
616  if (content.size() / this->getAlphabet()->getStateCodingSize()
617  != qualities.size())
618  throw DimensionException("SequenceWithQuality::append: qualities must fit content size", qualities.size(), content.size() / this->getAlphabet()->getStateCodingSize());
619  size_t pos = qualScores_->getSize();
620  SequenceWithAnnotation::append(content); // This automatically extend scores array with default values through the listener
621  // Update scores:
622  qualScores_->setScores(pos, qualities);
623  }
624 
626 
637  void addElement(const std::string& c, int q)
638  {
640  qualScores_->setScore(size() - 1, q);
641  }
642 
656  size_t pos, const std::string& c, int q)
657  {
659  qualScores_->setScore(pos, q);
660  }
661 
663 
672  void addElement(int v, int q)
673  {
675  qualScores_->setScore(size() - 1, q);
676  }
677 
689  void addElement(size_t pos, int v, int q)
690  {
692  qualScores_->setScore(pos, q);
693  }
694 
697  // const Comments& getComments() const { return Commentable::getComments(); }
698 
699  // void setComments(const Comments& comments) { Commentable::setComments(comments); }
700 
701  // void clearComments() { Commentable::clearComments(); }
702 };
703 } // end of namespace bpp.
704 #endif // BPP_SEQ_SEQUENCEWITHQUALITY_H
A partial implementation of a SymbolList object.
Definition: SymbolList.h:34
size_t size() const override
Get the number of elements in the list.
Definition: SymbolList.h:124
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
Get the alphabet associated to the list.
virtual size_t size() const =0
Get the number of elements in the list.
virtual void addElement(const T &c)=0
Add a character to the end of the list.
void addElement(const T &c) override
Definition: SymbolList.h:152
Interface for sequence annotations.
The SequenceQuality class.
void afterSequenceChanged(const IntSymbolListEditionEvent &event) override
const std::string & getType() const override
static const std::string QUALITY_SCORE
const int & operator[](size_t i) const
void init(const Sequence &seq) override
void setScores(const std::vector< int > &scores)
bool isShared() const override
SequenceQuality(const std::vector< int > &quality, bool removable=true)
Build a new SequenceQuality object.
const std::vector< int > & getScores() const
SequenceQuality * clone() const override
void afterSequenceInserted(const IntSymbolListInsertionEvent &event) override
std::vector< int > qualScores_
void setScore(size_t pos, int score)
bool merge(const SequenceAnnotation &anno) override
Merge the input annotation with the current one.
bool isValidWith(const SequenceWithAnnotation &sequence, bool throwException=true) const override
Test is the annotation is valid for a given sequence.
void afterSequenceDeleted(const IntSymbolListDeletionEvent &event) override
int & operator[](size_t i)
SequenceQuality(size_t size=0, bool removable=true)
Build a new SequenceQuality object.
void beforeSequenceInserted(const IntSymbolListInsertionEvent &event) override
void afterSequenceSubstituted(const IntSymbolListSubstitutionEvent &event) override
static const int DEFAULT_QUALITY_VALUE
void beforeSequenceDeleted(const IntSymbolListDeletionEvent &event) override
void beforeSequenceSubstituted(const IntSymbolListSubstitutionEvent &event) override
void setScores(size_t pos, const std::vector< int > &scores)
bool isRemovable() const override
std::unique_ptr< SequenceAnnotation > getPartAnnotation(size_t pos, size_t len) const override
void beforeSequenceChanged(const IntSymbolListEditionEvent &event) override
An implementation of the Sequence interface that supports annotation.
void append(const SequenceInterface &seq) override
Append the content of a sequence to the current one.
virtual void addAnnotation(std::shared_ptr< SequenceAnnotation > anno)
Add a new annotation to the sequence.
virtual const SequenceAnnotation & annotation(const std::string &type) const
SequenceWithAnnotation & operator=(const Sequence &s)
The Sequence generic assignment operator.
A SequenceWithAnnotation class with quality scores attached.
void addElement(int v, int q)
Add a character to the end of the list with quality.
SequenceWithQuality(std::shared_ptr< const Alphabet > &alpha)
Build a new empty SequenceWithQuality.
SequenceWithQuality & operator=(const SequenceWithQuality &sequence)
SequenceWithQuality(const std::string &name, const std::string &sequence, const std::vector< int > &quality, const Comments &comments, std::shared_ptr< const Alphabet > &alpha)
Build a new SequenceWithQuality from a std::string.
SequenceWithQuality(const std::string &name, const std::vector< int > &sequence, const std::vector< int > &quality, const Comments &comments, std::shared_ptr< const Alphabet > &alpha)
Build a new SequenceWithQuality from a std::vector<int>
SequenceWithQuality(const std::string &name, const std::vector< int > &sequence, std::shared_ptr< const Alphabet > &alpha)
Build a new SequenceWithQuality from a std::vector<int>
SequenceWithQuality(const std::string &name, const std::vector< int > &sequence, const std::vector< int > &quality, std::shared_ptr< const Alphabet > &alpha)
Build a new SequenceWithQuality from a std::vector<int>
void setQuality(size_t pos, int quality)
Set the quality score.
void setQualities(const std::vector< int > &quality)
Set the whole quality scores.
void addElement(const std::string &c, int q)
SequenceWithQuality(const std::string &name, const std::string &sequence, const Comments &comments, std::shared_ptr< const Alphabet > &alpha)
Build a new SequenceWithQuality from a std::string.
SequenceWithQuality(const std::string &name, const std::vector< int > &sequence, const Comments &comments, std::shared_ptr< const Alphabet > &alpha)
Build a new SequenceWithQuality from a std::vector<int>
void append(const SequenceInterface &seq) override
Append the content of a sequence to the current one.
SequenceWithQuality * clone() const override
int getQuality(size_t pos) const
Get the quality score.
void addElement(size_t pos, const std::string &c, int q)
Add a character to a certain position in the list with quality.
std::shared_ptr< SequenceQuality > qualScores_
virtual void append(const std::vector< int > &content, const std::vector< int > &qualities)
Append content with quality.
SequenceWithQuality(const std::string &name, const std::string &sequence, std::shared_ptr< const Alphabet > &alpha)
Build a new SequenceWithQuality from a std::string.
SequenceWithQuality(const Sequence &s, const std::vector< int > &sc)
Build a new SequenceWithQuality.
virtual void append(const std::string &content, const std::vector< int > &qualities)
Append content with quality.
void addElement(size_t pos, int v, int q)
Add a character to a certain position in the list with quality.
const std::vector< int > & getQualities() const
Get the whole quality scores.
SequenceWithQuality(const Sequence &s)
Build a new SequenceWithQuality.
SequenceWithQuality(const std::string &name, const std::string &sequence, const std::vector< int > &quality, std::shared_ptr< const Alphabet > &alpha)
Build a new SequenceWithQuality from a std::string.
virtual void append(const std::vector< std::string > &content, const std::vector< int > &qualities)
Append content with quality.
SequenceWithQuality(const SequenceWithQuality &sequence)
A basic implementation of the Sequence interface.
Definition: Sequence.h:117
virtual void addElement(const T &c)=0
Add a character to the end of the list.
static void append(std::vector< T > &vec1, const std::vector< T > &vec2)
std::string toString(T t)
This alphabet is used to deal NumericAlphabet.
std::vector< std::string > Comments
Declaration of Comments type.
Definition: Commentable.h:21