bpp-seq  2.2.0
SequenceWithAnnotationTools.h
Go to the documentation of this file.
1 //
2 // File: SequenceWithAnnotationTools.h
3 // Authors: Julien Dutheil
4 // Created on: 06 Sep 2010
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (Sep 06, 2010)
9 
10 This software is a computer program whose purpose is to provide classes
11 for sequences analysis.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #ifndef _SEQUENCEWITHANNOTATIONTOOLS_H_
41 #define _SEQUENCEWITHANNOTATIONTOOLS_H_
42 
43 #include "SequenceTools.h"
44 #include "SequenceWithAnnotation.h"
45 #include <Bpp/Numeric/VectorTools.h>
46 
47 #include <cstddef>
48 
49 namespace bpp {
50 
51  class SequenceMask :
52  public virtual SequenceAnnotation
53  {
54  private:
55  bool removable_;
56  std::vector<bool> mask_;
57 
58  public:
59  static const std::string MASK;
60 
61  public:
62 
76  SequenceMask(size_t size = 0, bool removable = true) :
77  removable_(removable),
78  mask_(size, false)
79  {}
80 
81 
90  SequenceMask(const std::vector<bool>& mask, bool removable = true) :
91  removable_(removable),
92  mask_(mask)
93  {}
94 
101  virtual ~SequenceMask() {}
108 #ifdef NO_VIRTUAL_COV
109  Clonable*
110 #else
111  SequenceMask*
112 #endif
113  clone() const { return new SequenceMask(*this); }
116  public:
117  void init(const Sequence& seq)
118  {
119  mask_.resize(seq.size());
120  std::fill(mask_.begin(), mask_.end(), false);
121  }
122 
123  const std::string& getType() const { return MASK; }
124 
125  bool isValidWith(const SequenceWithAnnotation& sequence, bool throwException = true) const
126  {
127  if (throwException && mask_.size() != sequence.size()) throw Exception("SequenceMask. The mask size must match the sequence size.");
128  return (mask_.size() == sequence.size());
129  }
130 
131  bool isRemovable() const { return removable_; }
132  bool isShared() const { return false; }
134  void afterSequenceChanged(const SymbolListEditionEvent& event);
141 
142  size_t getSize() const { return mask_.size(); }
143 
144  const bool operator[](size_t i) const { return mask_[i]; }
145 
146  void setMask(const std::vector<bool>& mask) {
147  if (mask.size() != mask_.size())
148  throw DimensionException("SequenceMask::setMask. Trying to replace mask by a vector with different length.", mask.size(), mask_.size());
149  mask_ = mask;
150  }
151 
155  const std::vector<bool>& getMask() const { return mask_; }
156 
157  void setMask(size_t pos, bool mask) {
158  if (pos >= mask_.size())
159  throw Exception("SequenceMask::setMask. Vector overflow. Scores number: " + TextTools::toString(mask_.size()) + ", but trying to insert mask at position " + TextTools::toString(pos) + ".");
160  mask_[pos] = mask;
161  }
162 
163  void setMask(size_t pos, const std::vector<bool>& mask) {
164  if (pos + mask.size() > mask_.size())
165  throw Exception("SequenceMask::setMask. Vector overflow. Scores number: " + TextTools::toString(mask_.size()) + ", but trying to insert " + TextTools::toString(mask.size()) + " scores at position " + TextTools::toString(pos) + ".");
166  std::copy(mask.begin(), mask.end(), mask_.begin() + static_cast<ptrdiff_t>(pos));
167  }
168 
169  bool merge(const SequenceAnnotation& anno) {
170  try {
171  const SequenceMask* mask = & dynamic_cast<const SequenceMask&>(anno);
172  VectorTools::append(mask_, mask->getMask());
173  return true;
174  } catch (std::exception& e) {
175  return false;
176  }
177  }
178 
179  SequenceAnnotation* getPartAnnotation(size_t pos, size_t len) const throw (Exception) {
180  return new SequenceMask(std::vector<bool>(mask_.begin() + static_cast<ptrdiff_t>(pos), mask_.begin() + static_cast<ptrdiff_t>(pos + len)), removable_);
181  }
182  };
183 
193 
194  public:
203 
204  };
205 }
206 
207 #endif // _SEQUENCEWITHANNOTATIONTOOLS_H_
void beforeSequenceInserted(const SymbolListInsertionEvent &event)
const std::vector< bool > & getMask() const
This alphabet is used to deal NumericAlphabet.
SequenceAnnotation * getPartAnnotation(size_t pos, size_t len) const
The SequenceWithAnnotationTools static class.
void setMask(size_t pos, bool mask)
bool merge(const SequenceAnnotation &anno)
Merge the input annotation with the current one.
SequenceMask(const std::vector< bool > &mask, bool removable=true)
Build a new SequenceMask object.
SequenceWithAnnotation * createMaskAnnotation(const Sequence &seq)
Parse a sequence with a CaseMaskedAlphabet and creates a new SequenceWithAnnotation object with origi...
const std::string & getType() const
void afterSequenceInserted(const SymbolListInsertionEvent &event)
SequenceMask(size_t size=0, bool removable=true)
Build a new SequenceMask object.
const bool operator[](size_t i) const
void afterSequenceDeleted(const SymbolListDeletionEvent &event)
void beforeSequenceDeleted(const SymbolListDeletionEvent &event)
void setMask(size_t pos, const std::vector< bool > &mask)
The alphabet exception base class.
Interface for sequence annotations.
An implementation of the Sequence interface that supports annotation.
void beforeSequenceSubstituted(const SymbolListSubstitutionEvent &event)
void afterSequenceSubstituted(const SymbolListSubstitutionEvent &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.
virtual size_t size() const =0
Get the number of elements in the list.
SequenceMask * clone() const
The sequence interface.
Definition: Sequence.h:74
static const std::string MASK
void setMask(const std::vector< bool > &mask)
virtual size_t size() const
Get the number of elements in the list.
Definition: SymbolList.h:601
void init(const Sequence &seq)
void beforeSequenceChanged(const SymbolListEditionEvent &event)