bpp-seq  2.1.0
 All Classes Namespaces Files Functions Variables Friends Pages
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"
48 
49 // From the STL
50 
51 #include <string>
52 #include <vector>
53 
54 namespace bpp {
65  public virtual SequenceAnnotation
66  {
67  private:
68  bool removable_;
69  std::vector<int> qualScores_;
70 
71  public:
72  static const std::string QUALITY_SCORE;
73  static const int DEFAULT_QUALITY_VALUE;
74 
75  public:
76 
91  SequenceQuality(size_t size = 0, bool removable = true) :
92  removable_(removable),
94 
95 
105  SequenceQuality(const std::vector<int>& quality, bool removable = true) :
106  removable_(removable),
107  qualScores_(quality)
108  {
109  // if (size() != qualScores_.size())
110  // throw DimensionException("SequenceWithQuality constructor: sequence and quality must have the same length", qualScores_.size(), size());
111  }
112 
119  virtual ~SequenceQuality() {}
126 #ifdef NO_VIRTUAL_COV
127  Clonable*
128 #else
130 #endif
131  clone() const { return new SequenceQuality(*this); }
134  public:
135  void init(const Sequence& seq)
136  {
137  qualScores_.resize(seq.size());
138  std::fill(qualScores_.begin(), qualScores_.end(), DEFAULT_QUALITY_VALUE);
139  }
140 
141  const std::string& getType() const { return QUALITY_SCORE; }
142 
143  bool isValidWith(const SequenceWithAnnotation& sequence, bool throwException = true) const
144  {
145  if (throwException && qualScores_.size() != sequence.size()) throw Exception("SequenceQuality. Quality scores must match the sequence size.");
146  return (qualScores_.size() == sequence.size());
147  }
148 
149  bool isRemovable() const { return removable_; }
150  bool isShared() const { return false; }
152  void afterSequenceChanged(const SymbolListEditionEvent& event);
159 
160  size_t getSize() const { return qualScores_.size(); }
161 
162  const int& operator[](size_t i) const { return qualScores_[i]; }
163  int& operator[](size_t i) { return qualScores_[i]; }
164 
165  void setScores(const std::vector<int>& scores) {
166  if (scores.size() != qualScores_.size())
167  throw DimensionException("SequenceQuality::setScores. Trying to replace score by a vector with different length.", scores.size(), qualScores_.size());
168  qualScores_ = scores;
169  }
170 
174  const std::vector<int>& getScores() const { return qualScores_; }
175 
176  void setScore(size_t pos, int score) {
177  if (pos >= qualScores_.size())
178  throw Exception("SequenceQuality::setScore. Vector overflow. Scores number: " + TextTools::toString(qualScores_.size()) + ", but trying to insert score at position " + TextTools::toString(pos) + ".");
179  qualScores_[pos] = score;
180  }
181 
182  void setScores(size_t pos, const std::vector<int>& scores) {
183  if (pos + scores.size() > qualScores_.size())
184  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) + ".");
185  std::copy(scores.begin(), scores.end(), qualScores_.begin() + pos);
186  }
187 
188  bool merge(const SequenceAnnotation& anno) {
189  try {
190  const SequenceQuality* qual = & dynamic_cast<const SequenceQuality&>(anno);
192  return true;
193  } catch (std::exception& e) {
194  return false;
195  }
196  }
197 
198  SequenceQuality* getPartAnnotation(size_t pos, size_t len) const throw (Exception) {
199  return new SequenceQuality(std::vector<int>(qualScores_.begin() + pos, qualScores_.begin() + pos + len), removable_);
200  }
201  };
202 
203 
204 
215  {
216  private:
218 
219  public:
220 
234  const Alphabet* alpha
235  ):
236  SequenceWithAnnotation(alpha),
237  qualScores_(new SequenceQuality(0, false))
238  {
240  }
241 
255  const std::string& name,
256  const std::string& sequence,
257  const Alphabet* alpha
258  ) throw (BadCharException):
259  SequenceWithAnnotation(name, sequence, alpha),
260  qualScores_(new SequenceQuality(sequence.size(), false))
261  {
263  }
264 
281  const std::string& name,
282  const std::string& sequence,
283  const Comments& comments,
284  const Alphabet* alpha
285  ) throw (BadCharException):
286  SequenceWithAnnotation(name, sequence, comments, alpha),
287  qualScores_(new SequenceQuality(sequence.size(), false))
288  {
290  }
291 
308  const std::string& name,
309  const std::string& sequence,
310  const std::vector<int>& quality,
311  const Alphabet* alpha)
313  SequenceWithAnnotation(name, sequence, alpha),
314  qualScores_(new SequenceQuality(quality, false))
315  {
317  }
318 
338  const std::string& name,
339  const std::string& sequence,
340  const std::vector<int>& quality,
341  const Comments& comments,
342  const Alphabet* alpha)
344  SequenceWithAnnotation(name, sequence, comments, alpha),
345  qualScores_(new SequenceQuality(quality, false))
346  {
348  }
349 
363  const std::string& name,
364  const std::vector<int>& sequence,
365  const Alphabet* alpha)
366  throw (BadIntException):
367  SequenceWithAnnotation(name, sequence, alpha),
368  qualScores_(new SequenceQuality(sequence.size(), false))
369  {
371  }
372 
389  const std::string& name,
390  const std::vector<int>& sequence,
391  const Comments& comments,
392  const Alphabet* alpha)
393  throw (BadIntException):
394  SequenceWithAnnotation(name, sequence, comments, alpha),
395  qualScores_(new SequenceQuality(sequence.size(), false))
396  {
398  }
399 
416  const std::string& name,
417  const std::vector<int>& sequence,
418  const std::vector<int>& quality,
419  const Alphabet* alpha)
421  SequenceWithAnnotation(name, sequence, alpha),
422  qualScores_(new SequenceQuality(quality, false))
423  {
425  }
426 
446  const std::string& name,
447  const std::vector<int>& sequence,
448  const std::vector<int>& quality,
449  const Comments& comments,
450  const Alphabet* alpha)
452  SequenceWithAnnotation(name, sequence, comments, alpha),
453  qualScores_(new SequenceQuality(quality, false))
454  {
456  }
457 
468  {
470  }
471 
485  const Sequence& s,
486  const std::vector<int>& sc)
487  throw (DimensionException):
489  qualScores_(new SequenceQuality(sc, false))
490  {
492  }
493 
500  virtual ~SequenceWithQuality() {}
504  SequenceWithAnnotation(sequence), qualScores_(0)
505  {
507  }
508 
510  {
513  return *this;
514  }
515 
520 #ifdef NO_VIRTUAL_COV
521  Clonable*
522 #else
524 #endif
525  clone() const { return new SequenceWithQuality(*this); }
542  void setQuality(size_t pos, int quality) throw (IndexOutOfBoundsException) {
543  //if (pos >= qualScores_->getSize())
544  // throw IndexOutOfBoundsException("SequenceWithQuality::setQuality: pos out of bounds", pos, 0, qualScores_->getSize() - 1);
545  //qualScores_[pos] = quality;
546  qualScores_->setScore(pos, quality);
547  }
548 
559  int getQuality(size_t pos) const throw (IndexOutOfBoundsException) {
560  if (pos >= qualScores_->getSize())
561  throw IndexOutOfBoundsException("SequenceWithQuality::getQuality: pos out of bounds", pos, 0, qualScores_->getSize() - 1);
562  return (*qualScores_)[pos];
563  }
564 
573  void setQualities(const std::vector<int>& quality) throw (DimensionException) {
574  if (quality.size() != qualScores_->getSize())
575  throw DimensionException("SequenceWithQuality::setQualities: quality must fit sequence size", quality.size(), qualScores_->getSize());
576  qualScores_->setScores(quality);
577  }
578 
584  const std::vector<int>& getQualities() const {
585  return qualScores_->getScores();
586  }
587 
588  void append(const std::vector<int>& content)
589  throw (BadIntException)
590  {
592  }
593 
605  void append(
606  const std::vector<int>& content,
607  const std::vector<int>& qualities)
609  {
610  if (content.size() != qualities.size())
611  throw DimensionException("SequenceWithQuality::append: qualities must fit content size", qualities.size(), content.size());
612  size_t pos = qualScores_->getSize();
613  append(content); //This automatically extend scores array with default values through the listener
614  //Update scores:
615  qualScores_->setScores(pos, qualities);
616  }
617 
618  void append(const std::vector<std::string>& content)
619  throw (BadCharException)
620  {
622  }
623 
635  void append(
636  const std::vector<std::string>& content,
637  const std::vector<int>& qualities)
639  {
640  if (content.size() != qualities.size())
641  throw DimensionException("SequenceWithQuality::append: qualities must fit content size", qualities.size(), content.size());
642  size_t pos = qualScores_->getSize();
643  SequenceWithAnnotation::append(content); //This automatically extend scores array with default values through the listener
644  //Update scores:
645  qualScores_->setScores(pos, qualities);
646  }
647 
648  void append(const std::string& content)
649  throw (BadCharException)
650  {
652  }
653 
665  void append(
666  const std::string& content,
667  const std::vector<int>& qualities)
669  {
670  if (content.size() / this->getAlphabet()->getStateCodingSize()
671  != qualities.size())
672  throw DimensionException("SequenceWithQuality::append: qualities must fit content size", qualities.size(), content.size() / this->getAlphabet()->getStateCodingSize());
673  size_t pos = qualScores_->getSize();
674  SequenceWithAnnotation::append(content); //This automatically extend scores array with default values through the listener
675  //Update scores:
676  qualScores_->setScores(pos, qualities);
677  }
678 
680  const std::string& c)
681  throw (BadCharException)
682  {
684  }
685 
696  const std::string& c, int q)
697  throw (BadCharException)
698  {
700  qualScores_->setScore(size() - 1, q);
701  }
702 
703  void addElement(size_t pos, const std::string& c)
705  {
707  }
708 
722  size_t pos, const std::string& c, int q)
724  {
726  qualScores_->setScore(pos, q);
727  }
728 
729  void addElement(int v)
730  throw (BadIntException)
731  {
733  }
734 
743  void addElement(int v, int q)
744  throw (BadIntException)
745  {
747  qualScores_->setScore(size() - 1, q);
748  }
749 
750  void addElement(size_t pos, int v)
752  {
754  }
755 
767  void addElement(size_t pos, int v, int q)
769  {
771  qualScores_->setScore(pos, q);
772  }
773 
776  };
777 
778 } // end of namespace bpp.
779 
780 #endif // _SEQUENCEWITHQUALITY_H_
781