bpp-seq  2.1.0
Bpp/Seq/SequenceWithQuality.h
Go to the documentation of this file.
00001 // 
00002 // File:    SequenceWithQuality.h
00003 // Authors: Sylvain Gaillard
00004 //          Vincent Cahais
00005 //          Julien Dutheil
00006 // Created: 19/01/2010 16:01:20
00007 // 
00008 
00009 /*
00010 Copyright or © or Copr. Bio++ Development Team, (January 19, 2010)
00011 
00012 This software is a computer program whose purpose is to provide classes
00013 for sequences analysis.
00014 
00015 This software is governed by the CeCILL  license under French law and
00016 abiding by the rules of distribution of free software.  You can  use, 
00017 modify and/ or redistribute the software under the terms of the CeCILL
00018 license as circulated by CEA, CNRS and INRIA at the following URL
00019 "http://www.cecill.info". 
00020 
00021 As a counterpart to the access to the source code and  rights to copy,
00022 modify and redistribute granted by the license, users are provided only
00023 with a limited warranty  and the software's author,  the holder of the
00024 economic rights,  and the successive licensors  have only  limited
00025 liability. 
00026 
00027 In this respect, the user's attention is drawn to the risks associated
00028 with loading,  using,  modifying and/or developing or reproducing the
00029 software by the user in light of its specific status of free software,
00030 that may mean  that it is complicated to manipulate,  and  that  also
00031 therefore means  that it is reserved for developers  and  experienced
00032 professionals having in-depth computer knowledge. Users are therefore
00033 encouraged to load and test the software's suitability as regards their
00034 requirements in conditions enabling the security of their systems and/or 
00035 data to be ensured and,  more generally, to use and operate it in the 
00036 same conditions as regards security. 
00037 
00038 The fact that you are presently reading this means that you have had
00039 knowledge of the CeCILL license and that you accept its terms.
00040 */
00041 
00042 #ifndef _SEQUENCEQUALITY_H_
00043 #define _SEQUENCEQUALITY_H_
00044 
00045 #include "SequenceWithAnnotation.h"
00046 #include <Bpp/Numeric/VectorTools.h>
00047 #include <Bpp/Numeric/VectorExceptions.h>
00048 
00049 // From the STL
00050 
00051 #include <string>
00052 #include <vector>
00053 
00054 namespace bpp {
00064   class SequenceQuality :
00065     public virtual SequenceAnnotation
00066   {
00067     private:
00068       bool removable_;
00069       std::vector<int> qualScores_;
00070 
00071     public:
00072       static const std::string QUALITY_SCORE; 
00073       static const int DEFAULT_QUALITY_VALUE;
00074 
00075     public:
00076 
00091       SequenceQuality(size_t size = 0, bool removable = true) :
00092         removable_(removable),
00093         qualScores_(size, DEFAULT_QUALITY_VALUE) {}
00094 
00095 
00105       SequenceQuality(const std::vector<int>& quality, bool removable = true) :
00106         removable_(removable),
00107         qualScores_(quality)
00108       {
00109       //    if (size() != qualScores_.size())
00110       //      throw DimensionException("SequenceWithQuality constructor: sequence and quality must have the same length", qualScores_.size(), size());
00111       }
00112 
00119       virtual ~SequenceQuality() {}
00126 #ifdef NO_VIRTUAL_COV
00127       Clonable*
00128 #else
00129       SequenceQuality*
00130 #endif
00131       clone() const { return new SequenceQuality(*this); }
00134     public:
00135       void init(const Sequence& seq)
00136       {
00137         qualScores_.resize(seq.size());
00138         std::fill(qualScores_.begin(), qualScores_.end(), DEFAULT_QUALITY_VALUE);
00139       }
00140 
00141       const std::string& getType() const { return QUALITY_SCORE; }
00142 
00143       bool isValidWith(const SequenceWithAnnotation& sequence, bool throwException = true) const
00144       {
00145         if (throwException && qualScores_.size() != sequence.size()) throw Exception("SequenceQuality. Quality scores must match the sequence size.");
00146         return (qualScores_.size() == sequence.size());
00147       }
00148 
00149       bool isRemovable() const { return removable_; }
00150       bool isShared() const { return false; }
00151       void beforeSequenceChanged(const SymbolListEditionEvent& event) {}
00152       void afterSequenceChanged(const SymbolListEditionEvent& event);
00153       void beforeSequenceInserted(const SymbolListInsertionEvent& event) {}
00154       void afterSequenceInserted(const SymbolListInsertionEvent& event);
00155       void beforeSequenceDeleted(const SymbolListDeletionEvent& event) {}
00156       void afterSequenceDeleted(const SymbolListDeletionEvent& event);
00157       void beforeSequenceSubstituted(const SymbolListSubstitutionEvent& event) {}
00158       void afterSequenceSubstituted(const SymbolListSubstitutionEvent& event) {}
00159 
00160       size_t getSize() const { return qualScores_.size(); }
00161 
00162       const int& operator[](size_t i) const { return qualScores_[i]; }
00163       int& operator[](size_t i) { return qualScores_[i]; }
00164 
00165       void setScores(const std::vector<int>& scores) {
00166         if (scores.size() != qualScores_.size())
00167           throw DimensionException("SequenceQuality::setScores. Trying to replace score by a vector with different length.", scores.size(), qualScores_.size());
00168         qualScores_ = scores;
00169       }
00170 
00174       const std::vector<int>& getScores() const { return qualScores_; }
00175 
00176       void setScore(size_t pos, int score) {
00177         if (pos >= qualScores_.size())
00178           throw Exception("SequenceQuality::setScore. Vector overflow. Scores number: " + TextTools::toString(qualScores_.size()) + ", but trying to insert score at position " + TextTools::toString(pos) + ".");
00179         qualScores_[pos] = score;
00180       }
00181       
00182       void setScores(size_t pos, const std::vector<int>& scores) {
00183         if (pos + scores.size() > qualScores_.size())
00184           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) + ".");
00185         std::copy(scores.begin(), scores.end(), qualScores_.begin() + pos); 
00186       }
00187     
00188       bool merge(const SequenceAnnotation& anno) {
00189         try {
00190           const SequenceQuality* qual = & dynamic_cast<const SequenceQuality&>(anno);
00191           VectorTools::append(qualScores_, qual->getScores());
00192           return true;
00193         } catch (std::exception& e) {
00194           return false;
00195         }
00196       }
00197 
00198       SequenceQuality* getPartAnnotation(size_t pos, size_t len) const throw (Exception) {
00199         return new SequenceQuality(std::vector<int>(qualScores_.begin() + pos, qualScores_.begin() + pos + len), removable_);
00200       }
00201   };
00202 
00203 
00204 
00213   class SequenceWithQuality :
00214     public SequenceWithAnnotation
00215   {
00216     private:
00217       SequenceQuality* qualScores_;
00218 
00219     public:
00220 
00233       SequenceWithQuality(
00234           const Alphabet* alpha
00235           ):
00236         SequenceWithAnnotation(alpha),
00237         qualScores_(new SequenceQuality(0, false))
00238       {
00239         addAnnotation(qualScores_);
00240       }
00241 
00254       SequenceWithQuality(
00255           const std::string& name,
00256           const std::string& sequence,
00257           const Alphabet* alpha
00258           ) throw (BadCharException):
00259         SequenceWithAnnotation(name, sequence, alpha),
00260         qualScores_(new SequenceQuality(sequence.size(), false))
00261       {
00262         addAnnotation(qualScores_);
00263       }
00264 
00280       SequenceWithQuality(
00281           const std::string& name,
00282           const std::string& sequence,
00283           const Comments& comments,
00284           const Alphabet* alpha
00285           ) throw (BadCharException):
00286         SequenceWithAnnotation(name, sequence, comments, alpha),
00287         qualScores_(new SequenceQuality(sequence.size(), false))
00288       {
00289         addAnnotation(qualScores_);
00290       }
00291 
00307       SequenceWithQuality(
00308           const std::string& name,
00309           const std::string& sequence,
00310           const std::vector<int>& quality,
00311           const Alphabet* alpha)
00312         throw (BadCharException, DimensionException):
00313         SequenceWithAnnotation(name, sequence, alpha),
00314         qualScores_(new SequenceQuality(quality, false))
00315       {
00316         addAnnotation(qualScores_);
00317       }
00318 
00337       SequenceWithQuality(
00338           const std::string& name,
00339           const std::string& sequence,
00340           const std::vector<int>& quality,
00341           const Comments& comments,
00342           const Alphabet* alpha)
00343         throw (BadCharException, DimensionException):
00344         SequenceWithAnnotation(name, sequence, comments, alpha),
00345         qualScores_(new SequenceQuality(quality, false))
00346       {
00347         addAnnotation(qualScores_);
00348       }
00349 
00362       SequenceWithQuality(
00363           const std::string& name,
00364           const std::vector<int>& sequence,
00365           const Alphabet* alpha)
00366         throw (BadIntException):
00367         SequenceWithAnnotation(name, sequence, alpha),
00368         qualScores_(new SequenceQuality(sequence.size(), false))
00369       {
00370         addAnnotation(qualScores_);
00371       }
00372 
00388       SequenceWithQuality(
00389           const std::string& name,
00390           const std::vector<int>& sequence,
00391           const Comments& comments,
00392           const Alphabet* alpha)
00393         throw (BadIntException):
00394         SequenceWithAnnotation(name, sequence, comments, alpha),
00395         qualScores_(new SequenceQuality(sequence.size(), false))
00396       {
00397         addAnnotation(qualScores_);
00398       }
00399 
00415       SequenceWithQuality(
00416           const std::string& name,
00417           const std::vector<int>& sequence,
00418           const std::vector<int>& quality,
00419           const Alphabet* alpha)
00420         throw (BadIntException, DimensionException):
00421         SequenceWithAnnotation(name, sequence, alpha),
00422         qualScores_(new SequenceQuality(quality, false))
00423       {
00424         addAnnotation(qualScores_);
00425       }
00426 
00445       SequenceWithQuality(
00446           const std::string& name,
00447           const std::vector<int>& sequence,
00448           const std::vector<int>& quality,
00449           const Comments& comments,
00450           const Alphabet* alpha)
00451         throw (BadIntException, DimensionException):
00452         SequenceWithAnnotation(name, sequence, comments, alpha),
00453         qualScores_(new SequenceQuality(quality, false))
00454       {
00455         addAnnotation(qualScores_);
00456       }
00457 
00466       SequenceWithQuality(const Sequence& s) :
00467         SequenceWithAnnotation(s), qualScores_(new SequenceQuality(s.size(), false))
00468       {
00469         addAnnotation(qualScores_);
00470       }
00471 
00484       SequenceWithQuality(
00485           const Sequence& s,
00486           const std::vector<int>& sc)
00487         throw (DimensionException):
00488         SequenceWithAnnotation(s),
00489         qualScores_(new SequenceQuality(sc, false))
00490       {
00491         addAnnotation(qualScores_);
00492       }
00493 
00500       virtual ~SequenceWithQuality() {}
00503       SequenceWithQuality(const SequenceWithQuality& sequence) : 
00504         SequenceWithAnnotation(sequence), qualScores_(0)
00505       {
00506         qualScores_ = dynamic_cast<SequenceQuality*>(&getAnnotation(SequenceQuality::QUALITY_SCORE));                  
00507       }
00508 
00509       SequenceWithQuality& operator=(const SequenceWithQuality& sequence)
00510       { 
00511         SequenceWithAnnotation::operator=(sequence);
00512         qualScores_ = dynamic_cast<SequenceQuality*>(&getAnnotation(SequenceQuality::QUALITY_SCORE));
00513         return *this;
00514       }
00515 
00520 #ifdef NO_VIRTUAL_COV
00521       Clonable*
00522 #else
00523       SequenceWithQuality*
00524 #endif
00525       clone() const { return new SequenceWithQuality(*this); }
00542       void setQuality(size_t pos, int quality) throw (IndexOutOfBoundsException) {
00543         //if (pos >= qualScores_->getSize())
00544         //  throw IndexOutOfBoundsException("SequenceWithQuality::setQuality: pos out of bounds", pos, 0, qualScores_->getSize() - 1);
00545         //qualScores_[pos] = quality;
00546         qualScores_->setScore(pos, quality);
00547       }
00548       
00559       int getQuality(size_t pos) const throw (IndexOutOfBoundsException) {
00560         if (pos >= qualScores_->getSize())
00561           throw IndexOutOfBoundsException("SequenceWithQuality::getQuality: pos out of bounds", pos, 0, qualScores_->getSize() - 1);
00562         return (*qualScores_)[pos];
00563       }
00564 
00573       void setQualities(const std::vector<int>& quality) throw (DimensionException) {
00574         if (quality.size() != qualScores_->getSize())
00575           throw DimensionException("SequenceWithQuality::setQualities: quality must fit sequence size", quality.size(), qualScores_->getSize());
00576         qualScores_->setScores(quality);
00577       }
00578 
00584       const std::vector<int>& getQualities() const {
00585         return qualScores_->getScores();
00586       }
00587 
00588       void append(const std::vector<int>& content)
00589         throw (BadIntException)
00590       {
00591         SequenceWithAnnotation::append(content);
00592       }
00593 
00605       void append(
00606           const std::vector<int>& content,
00607           const std::vector<int>& qualities)
00608         throw (BadIntException, DimensionException)
00609       {
00610         if (content.size() != qualities.size())
00611           throw DimensionException("SequenceWithQuality::append: qualities must fit content size", qualities.size(), content.size());
00612         size_t pos = qualScores_->getSize();
00613         append(content); //This automatically extend scores array with default values through the listener
00614         //Update scores:
00615         qualScores_->setScores(pos, qualities);
00616       }
00617 
00618       void append(const std::vector<std::string>& content)
00619         throw (BadCharException)
00620       {
00621         SequenceWithAnnotation::append(content);
00622       }
00623 
00635       void append(
00636           const std::vector<std::string>& content,
00637           const std::vector<int>& qualities)
00638         throw (BadCharException, DimensionException)
00639       {
00640         if (content.size() != qualities.size())
00641           throw DimensionException("SequenceWithQuality::append: qualities must fit content size", qualities.size(), content.size());
00642         size_t pos = qualScores_->getSize();
00643         SequenceWithAnnotation::append(content); //This automatically extend scores array with default values through the listener
00644         //Update scores:
00645         qualScores_->setScores(pos, qualities);
00646       }
00647 
00648       void append(const std::string& content)
00649         throw (BadCharException)
00650       {
00651         SequenceWithAnnotation::append(content);
00652       }
00653 
00665       void append(
00666           const std::string& content,
00667           const std::vector<int>& qualities)
00668         throw (BadCharException, DimensionException)
00669       {
00670         if (content.size() / this->getAlphabet()->getStateCodingSize()
00671             != qualities.size())
00672           throw DimensionException("SequenceWithQuality::append: qualities must fit content size", qualities.size(), content.size() / this->getAlphabet()->getStateCodingSize());
00673         size_t pos = qualScores_->getSize();
00674         SequenceWithAnnotation::append(content); //This automatically extend scores array with default values through the listener
00675         //Update scores:
00676         qualScores_->setScores(pos, qualities);
00677       }
00678 
00679       void addElement(
00680           const std::string& c)
00681         throw (BadCharException)
00682       {
00683         SequenceWithAnnotation::addElement(c);
00684       }
00685 
00695       void addElement(
00696           const std::string& c, int q)
00697         throw (BadCharException)
00698       {
00699         SequenceWithAnnotation::addElement(c);
00700         qualScores_->setScore(size() - 1, q);
00701       }
00702 
00703       void addElement(size_t pos, const std::string& c)
00704         throw (BadCharException, IndexOutOfBoundsException)
00705       {
00706         SequenceWithAnnotation::addElement(pos, c);
00707       }
00708 
00721       void addElement(
00722           size_t pos, const std::string& c, int q)
00723         throw (BadCharException, IndexOutOfBoundsException)
00724       {
00725         SequenceWithAnnotation::addElement(pos, c);
00726         qualScores_->setScore(pos, q);
00727       }
00728 
00729       void addElement(int v)
00730         throw (BadIntException)
00731       {
00732         SequenceWithAnnotation::addElement(v);
00733       }
00734 
00743       void addElement(int v, int q)
00744         throw (BadIntException)
00745       {
00746         SequenceWithAnnotation::addElement(v);
00747         qualScores_->setScore(size() - 1, q);
00748       }
00749 
00750       void addElement(size_t pos, int v)
00751         throw (BadIntException, IndexOutOfBoundsException)
00752       {
00753         SequenceWithAnnotation::addElement(pos, v);
00754       }
00755 
00767       void addElement(size_t pos, int v, int q)
00768         throw (BadCharException, IndexOutOfBoundsException)
00769       {
00770         SequenceWithAnnotation::addElement(pos, v);
00771         qualScores_->setScore(pos, q);
00772       }
00773 
00776   };
00777 
00778 } // end of namespace bpp.
00779 
00780 #endif // _SEQUENCEWITHQUALITY_H_
00781 
 All Classes Namespaces Files Functions Variables Typedefs Friends