bpp-seq  2.2.0
SequenceWithQuality.h
Go to the documentation of this file.
1 //
2 // File: SequenceWithQuality.h
3 // Authors: Sylvain Gaillard
4 // Vincent Cahais
5 // Julien Dutheil
6 // Created: 19/01/2010 16:01:20
7 //
8 
9 /*
10 Copyright or © or Copr. Bio++ Development Team, (January 19, 2010)
11 
12 This software is a computer program whose purpose is to provide classes
13 for sequences analysis.
14 
15 This software is governed by the CeCILL license under French law and
16 abiding by the rules of distribution of free software. You can use,
17 modify and/ or redistribute the software under the terms of the CeCILL
18 license as circulated by CEA, CNRS and INRIA at the following URL
19 "http://www.cecill.info".
20 
21 As a counterpart to the access to the source code and rights to copy,
22 modify and redistribute granted by the license, users are provided only
23 with a limited warranty and the software's author, the holder of the
24 economic rights, and the successive licensors have only limited
25 liability.
26 
27 In this respect, the user's attention is drawn to the risks associated
28 with loading, using, modifying and/or developing or reproducing the
29 software by the user in light of its specific status of free software,
30 that may mean that it is complicated to manipulate, and that also
31 therefore means that it is reserved for developers and experienced
32 professionals having in-depth computer knowledge. Users are therefore
33 encouraged to load and test the software's suitability as regards their
34 requirements in conditions enabling the security of their systems and/or
35 data to be ensured and, more generally, to use and operate it in the
36 same conditions as regards security.
37 
38 The fact that you are presently reading this means that you have had
39 knowledge of the CeCILL license and that you accept its terms.
40 */
41 
42 #ifndef _SEQUENCEQUALITY_H_
43 #define _SEQUENCEQUALITY_H_
44 
45 #include "SequenceWithAnnotation.h"
46 #include <Bpp/Numeric/VectorTools.h>
47 #include <Bpp/Numeric/VectorExceptions.h>
48 
49 // From the STL
50 
51 #include <string>
52 #include <vector>
53 #include <cstddef>
54 
55 namespace bpp {
66  public virtual SequenceAnnotation
67  {
68  private:
69  bool removable_;
70  std::vector<int> qualScores_;
71 
72  public:
73  static const std::string QUALITY_SCORE;
74  static const int DEFAULT_QUALITY_VALUE;
75 
76  public:
77 
92  SequenceQuality(size_t size = 0, bool removable = true) :
93  removable_(removable),
95 
96 
106  SequenceQuality(const std::vector<int>& quality, bool removable = true) :
107  removable_(removable),
108  qualScores_(quality)
109  {
110  // if (size() != qualScores_.size())
111  // throw DimensionException("SequenceWithQuality constructor: sequence and quality must have the same length", qualScores_.size(), size());
112  }
113 
120  virtual ~SequenceQuality() {}
127 #ifdef NO_VIRTUAL_COV
128  Clonable*
129 #else
131 #endif
132  clone() const { return new SequenceQuality(*this); }
135  public:
136  void init(const Sequence& seq)
137  {
138  qualScores_.resize(seq.size());
139  std::fill(qualScores_.begin(), qualScores_.end(), DEFAULT_QUALITY_VALUE);
140  }
141 
142  const std::string& getType() const { return QUALITY_SCORE; }
143 
144  bool isValidWith(const SequenceWithAnnotation& sequence, bool throwException = true) const
145  {
146  if (throwException && qualScores_.size() != sequence.size()) throw Exception("SequenceQuality. Quality scores must match the sequence size.");
147  return (qualScores_.size() == sequence.size());
148  }
149 
150  bool isRemovable() const { return removable_; }
151  bool isShared() const { return false; }
153  void afterSequenceChanged(const SymbolListEditionEvent& event);
160 
161  size_t getSize() const { return qualScores_.size(); }
162 
163  const int& operator[](size_t i) const { return qualScores_[i]; }
164  int& operator[](size_t i) { return qualScores_[i]; }
165 
166  void setScores(const std::vector<int>& scores) {
167  if (scores.size() != qualScores_.size())
168  throw DimensionException("SequenceQuality::setScores. Trying to replace score by a vector with different length.", scores.size(), qualScores_.size());
169  qualScores_ = scores;
170  }
171 
175  const std::vector<int>& getScores() const { return qualScores_; }
176 
177  void setScore(size_t pos, int score) {
178  if (pos >= qualScores_.size())
179  throw Exception("SequenceQuality::setScore. Vector overflow. Scores number: " + TextTools::toString(qualScores_.size()) + ", but trying to insert score at position " + TextTools::toString(pos) + ".");
180  qualScores_[pos] = score;
181  }
182 
183  void setScores(size_t pos, const std::vector<int>& scores) {
184  if (pos + scores.size() > qualScores_.size())
185  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) + ".");
186  std::copy(scores.begin(), scores.end(), qualScores_.begin() + static_cast<ptrdiff_t>(pos));
187  }
188 
189  bool merge(const SequenceAnnotation& anno) {
190  try {
191  const SequenceQuality* qual = & dynamic_cast<const SequenceQuality&>(anno);
192  VectorTools::append(qualScores_, qual->getScores());
193  return true;
194  } catch (std::exception& e) {
195  return false;
196  }
197  }
198 
199  SequenceQuality* getPartAnnotation(size_t pos, size_t len) const throw (Exception) {
200  return new SequenceQuality(
201  std::vector<int>(
202  qualScores_.begin() + static_cast<ptrdiff_t>(pos),
203  qualScores_.begin() + static_cast<ptrdiff_t>(pos + len)),
204  removable_);
205  }
206  };
207 
208 
209 
220  {
221  private:
223 
224  public:
225 
239  const Alphabet* alpha
240  ):
241  SequenceWithAnnotation(alpha),
242  qualScores_(new SequenceQuality(0, false))
243  {
245  }
246 
260  const std::string& name,
261  const std::string& sequence,
262  const Alphabet* alpha
263  ) throw (BadCharException):
264  SequenceWithAnnotation(name, sequence, alpha),
265  qualScores_(new SequenceQuality(sequence.size(), false))
266  {
268  }
269 
286  const std::string& name,
287  const std::string& sequence,
288  const Comments& comments,
289  const Alphabet* alpha
290  ) throw (BadCharException):
291  SequenceWithAnnotation(name, sequence, comments, alpha),
292  qualScores_(new SequenceQuality(sequence.size(), false))
293  {
295  }
296 
313  const std::string& name,
314  const std::string& sequence,
315  const std::vector<int>& quality,
316  const Alphabet* alpha)
317  throw (BadCharException, DimensionException):
318  SequenceWithAnnotation(name, sequence, alpha),
319  qualScores_(new SequenceQuality(quality, false))
320  {
322  }
323 
343  const std::string& name,
344  const std::string& sequence,
345  const std::vector<int>& quality,
346  const Comments& comments,
347  const Alphabet* alpha)
348  throw (BadCharException, DimensionException):
349  SequenceWithAnnotation(name, sequence, comments, alpha),
350  qualScores_(new SequenceQuality(quality, false))
351  {
353  }
354 
368  const std::string& name,
369  const std::vector<int>& sequence,
370  const Alphabet* alpha)
371  throw (BadIntException):
372  SequenceWithAnnotation(name, sequence, alpha),
373  qualScores_(new SequenceQuality(sequence.size(), false))
374  {
376  }
377 
394  const std::string& name,
395  const std::vector<int>& sequence,
396  const Comments& comments,
397  const Alphabet* alpha)
398  throw (BadIntException):
399  SequenceWithAnnotation(name, sequence, comments, alpha),
400  qualScores_(new SequenceQuality(sequence.size(), false))
401  {
403  }
404 
421  const std::string& name,
422  const std::vector<int>& sequence,
423  const std::vector<int>& quality,
424  const Alphabet* alpha)
425  throw (BadIntException, DimensionException):
426  SequenceWithAnnotation(name, sequence, alpha),
427  qualScores_(new SequenceQuality(quality, false))
428  {
430  }
431 
451  const std::string& name,
452  const std::vector<int>& sequence,
453  const std::vector<int>& quality,
454  const Comments& comments,
455  const Alphabet* alpha)
456  throw (BadIntException, DimensionException):
457  SequenceWithAnnotation(name, sequence, comments, alpha),
458  qualScores_(new SequenceQuality(quality, false))
459  {
461  }
462 
473  {
475  }
476 
490  const Sequence& s,
491  const std::vector<int>& sc)
492  throw (DimensionException):
494  qualScores_(new SequenceQuality(sc, false))
495  {
497  }
498 
505  virtual ~SequenceWithQuality() {}
509  SequenceWithAnnotation(sequence), qualScores_(0)
510  {
512  }
513 
515  {
518  return *this;
519  }
520 
525 #ifdef NO_VIRTUAL_COV
526  Clonable*
527 #else
529 #endif
530  clone() const { return new SequenceWithQuality(*this); }
547  void setQuality(size_t pos, int quality) throw (IndexOutOfBoundsException) {
548  //if (pos >= qualScores_->getSize())
549  // throw IndexOutOfBoundsException("SequenceWithQuality::setQuality: pos out of bounds", pos, 0, qualScores_->getSize() - 1);
550  //qualScores_[pos] = quality;
551  qualScores_->setScore(pos, quality);
552  }
553 
564  int getQuality(size_t pos) const throw (IndexOutOfBoundsException) {
565  if (pos >= qualScores_->getSize())
566  throw IndexOutOfBoundsException("SequenceWithQuality::getQuality: pos out of bounds", pos, 0, qualScores_->getSize() - 1);
567  return (*qualScores_)[pos];
568  }
569 
578  void setQualities(const std::vector<int>& quality) throw (DimensionException) {
579  if (quality.size() != qualScores_->getSize())
580  throw DimensionException("SequenceWithQuality::setQualities: quality must fit sequence size", quality.size(), qualScores_->getSize());
581  qualScores_->setScores(quality);
582  }
583 
589  const std::vector<int>& getQualities() const {
590  return qualScores_->getScores();
591  }
592 
593  void append(const std::vector<int>& content)
594  throw (BadIntException)
595  {
597  }
598 
610  void append(
611  const std::vector<int>& content,
612  const std::vector<int>& qualities)
613  throw (BadIntException, DimensionException)
614  {
615  if (content.size() != qualities.size())
616  throw DimensionException("SequenceWithQuality::append: qualities must fit content size", qualities.size(), content.size());
617  size_t pos = qualScores_->getSize();
618  append(content); //This automatically extend scores array with default values through the listener
619  //Update scores:
620  qualScores_->setScores(pos, qualities);
621  }
622 
623  void append(const std::vector<std::string>& content)
624  throw (BadCharException)
625  {
627  }
628 
640  void append(
641  const std::vector<std::string>& content,
642  const std::vector<int>& qualities)
643  throw (BadCharException, DimensionException)
644  {
645  if (content.size() != qualities.size())
646  throw DimensionException("SequenceWithQuality::append: qualities must fit content size", qualities.size(), content.size());
647  size_t pos = qualScores_->getSize();
648  SequenceWithAnnotation::append(content); //This automatically extend scores array with default values through the listener
649  //Update scores:
650  qualScores_->setScores(pos, qualities);
651  }
652 
653  void append(const std::string& content)
654  throw (BadCharException)
655  {
657  }
658 
670  void append(
671  const std::string& content,
672  const std::vector<int>& qualities)
673  throw (BadCharException, DimensionException)
674  {
675  if (content.size() / this->getAlphabet()->getStateCodingSize()
676  != qualities.size())
677  throw DimensionException("SequenceWithQuality::append: qualities must fit content size", qualities.size(), content.size() / this->getAlphabet()->getStateCodingSize());
678  size_t pos = qualScores_->getSize();
679  SequenceWithAnnotation::append(content); //This automatically extend scores array with default values through the listener
680  //Update scores:
681  qualScores_->setScores(pos, qualities);
682  }
683 
685  const std::string& c)
686  throw (BadCharException)
687  {
689  }
690 
701  const std::string& c, int q)
702  throw (BadCharException)
703  {
705  qualScores_->setScore(size() - 1, q);
706  }
707 
708  void addElement(size_t pos, const std::string& c)
709  throw (BadCharException, IndexOutOfBoundsException)
710  {
712  }
713 
727  size_t pos, const std::string& c, int q)
728  throw (BadCharException, IndexOutOfBoundsException)
729  {
731  qualScores_->setScore(pos, q);
732  }
733 
734  void addElement(int v)
735  throw (BadIntException)
736  {
738  }
739 
748  void addElement(int v, int q)
749  throw (BadIntException)
750  {
752  qualScores_->setScore(size() - 1, q);
753  }
754 
755  void addElement(size_t pos, int v)
756  throw (BadIntException, IndexOutOfBoundsException)
757  {
759  }
760 
772  void addElement(size_t pos, int v, int q)
773  throw (BadCharException, IndexOutOfBoundsException)
774  {
776  qualScores_->setScore(pos, q);
777  }
778 
781  };
782 
783 } // end of namespace bpp.
784 
785 #endif // _SEQUENCEWITHQUALITY_H_
786 
void addElement(size_t pos, const std::string &c)
Add a character at a certain position in the list.
SequenceWithQuality(const Alphabet *alpha)
Build a new empty SequenceWithQuality.
void append(const std::vector< std::string > &content, const std::vector< int > &qualities)
Append content with quality.
An alphabet exception thrown when trying to specify a bad char to the alphabet.
void append(const std::string &content, const std::vector< int > &qualities)
Append content with quality.
void addElement(const std::string &c)
Add a character to the end of the list.
SequenceWithQuality(const std::string &name, const std::vector< int > &sequence, const std::vector< int > &quality, const Comments &comments, const Alphabet *alpha)
Build a new SequenceWithQuality from a std::vector<int>
std::vector< std::string > Comments
Declaration of Comments type.
Definition: Sequence.h:60
virtual const Alphabet * getAlphabet() const
Get the alphabet associated to the list.
Definition: SymbolList.h:599
std::vector< int > qualScores_
const int & operator[](size_t i) const
This alphabet is used to deal NumericAlphabet.
void init(const Sequence &seq)
SequenceWithAnnotation(const Alphabet *alpha)
Empty constructor: build a void Sequence with just an Alphabet.
void afterSequenceDeleted(const SymbolListDeletionEvent &event)
The Alphabet interface.
Definition: Alphabet.h:130
The SequenceQuality class.
virtual unsigned int getStateCodingSize() const =0
Get the size of the string coding a state.
void setScore(size_t pos, int score)
A SequenceWithAnnotation class with quality scores attached.
bool merge(const SequenceAnnotation &anno)
Merge the input annotation with the current one.
const std::string & getType() const
SequenceQuality * clone() const
void setScores(size_t pos, const std::vector< int > &scores)
virtual void addAnnotation(SequenceAnnotation *anno)
Add a new annotation to the sequence.
void addElement(size_t pos, int v)
Add a character at a certain position in the list.
void append(const std::string &content)
Append the specified content to the sequence.
SequenceWithQuality(const std::string &name, const std::vector< int > &sequence, const std::vector< int > &quality, const Alphabet *alpha)
Build a new SequenceWithQuality from a std::vector<int>
void beforeSequenceSubstituted(const SymbolListSubstitutionEvent &event)
void setQuality(size_t pos, int quality)
Set the quality score.
void afterSequenceInserted(const SymbolListInsertionEvent &event)
const std::vector< int > & getScores() const
void beforeSequenceChanged(const SymbolListEditionEvent &event)
SequenceQuality * getPartAnnotation(size_t pos, size_t len) const
SequenceWithQuality(const std::string &name, const std::string &sequence, const Alphabet *alpha)
Build a new SequenceWithQuality from a std::string.
SequenceWithQuality(const std::string &name, const std::string &sequence, const std::vector< int > &quality, const Comments &comments, const Alphabet *alpha)
Build a new SequenceWithQuality from a std::string.
void addElement(int v)
Add a character to the end of the list.
SequenceWithQuality(const Sequence &s, const std::vector< int > &sc)
Build a new SequenceWithQuality.
SequenceWithQuality(const std::string &name, const std::vector< int > &sequence, const Comments &comments, const Alphabet *alpha)
Build a new SequenceWithQuality from a std::vector<int>
SequenceWithQuality(const std::string &name, const std::string &sequence, const Comments &comments, const Alphabet *alpha)
Build a new SequenceWithQuality from a std::string.
SequenceWithQuality(const Sequence &s)
Build a new SequenceWithQuality.
void setScores(const std::vector< int > &scores)
void afterSequenceSubstituted(const SymbolListSubstitutionEvent &event)
virtual const SequenceAnnotation & getAnnotation(const std::string &type) const
SequenceWithQuality(const std::string &name, const std::string &sequence, const std::vector< int > &quality, const Alphabet *alpha)
Build a new SequenceWithQuality from a std::string.
Interface for sequence annotations.
An implementation of the Sequence interface that supports annotation.
void append(const std::vector< std::string > &content)
Append the specified content to the sequence.
void addElement(const std::string &c, int q)
Add a character to the end of the list with quality.
void beforeSequenceDeleted(const SymbolListDeletionEvent &event)
int getQuality(size_t pos) const
Get the quality score.
SequenceWithAnnotation & operator=(const Sequence &s)
The Sequence generic assignment operator. This does not perform a hard copy of the alphabet object...
SequenceWithQuality & operator=(const SequenceWithQuality &sequence)
void addElement(size_t pos, const std::string &c, int q)
Add a character to a certain position in the list with quality.
void setQualities(const std::vector< int > &quality)
Set the whole quality scores.
virtual size_t size() const =0
Get the number of elements in the list.
The sequence interface.
Definition: Sequence.h:74
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.
static const std::string QUALITY_SCORE
An alphabet exception thrown when trying to specify a bad int to the alphabet.
static const int DEFAULT_QUALITY_VALUE
virtual size_t size() const
Get the number of elements in the list.
Definition: SymbolList.h:601
void append(const std::vector< int > &content)
Append the specified content to the sequence.
void addElement(int v, int q)
Add a character to the end of the list with quality.
SequenceQuality(size_t size=0, bool removable=true)
Build a new SequenceQuality object.
SequenceQuality(const std::vector< int > &quality, bool removable=true)
Build a new SequenceQuality object.
SequenceWithQuality * clone() const
virtual void addElement(const std::string &c)
Add a character to the end of the list.
Definition: SymbolList.cpp:321
SequenceWithQuality(const std::string &name, const std::vector< int > &sequence, const Alphabet *alpha)
Build a new SequenceWithQuality from a std::vector<int>
void append(const std::vector< int > &content, const std::vector< int > &qualities)
Append content with quality.
int & operator[](size_t i)
void beforeSequenceInserted(const SymbolListInsertionEvent &event)
void afterSequenceChanged(const SymbolListEditionEvent &event)
bool isValidWith(const SequenceWithAnnotation &sequence, bool throwException=true) const
Test is the annotation is valid for a given sequence.
void append(const std::vector< int > &content)
Append the specified content to the sequence.
SequenceWithQuality(const SequenceWithQuality &sequence)