bpp-seq3  3.0.0
SequenceWithAnnotationTools.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_SEQUENCEWITHANNOTATIONTOOLS_H
6 #define BPP_SEQ_SEQUENCEWITHANNOTATIONTOOLS_H
7 
9 #include <cstddef>
10 
11 #include "SequenceTools.h"
12 #include "SequenceWithAnnotation.h"
13 
14 namespace bpp
15 {
16 class SequenceMask :
17  public virtual SequenceAnnotation
18 {
19 private:
20  bool removable_;
21  std::vector<bool> mask_;
22 
23 public:
24  static const std::string MASK;
25 
26 public:
40  SequenceMask(size_t size = 0, bool removable = true) :
41  removable_(removable),
42  mask_(size, false)
43  {}
44 
45 
54  SequenceMask(const std::vector<bool>& mask, bool removable = true) :
55  removable_(removable),
56  mask_(mask)
57  {}
58 
65  virtual ~SequenceMask() {}
72  SequenceMask* clone() const override { return new SequenceMask(*this); }
75 public:
76  void init(const Sequence& seq) override
77  {
78  mask_.resize(seq.size());
79  std::fill(mask_.begin(), mask_.end(), false);
80  }
81 
82  const std::string& getType() const override { return MASK; }
83 
84  bool isValidWith(const SequenceWithAnnotation& sequence, bool throwException = true) const override
85  {
86  if (throwException && mask_.size() != sequence.size()) throw Exception("SequenceMask. The mask size must match the sequence size.");
87  return mask_.size() == sequence.size();
88  }
89 
90  bool isRemovable() const override { return removable_; }
91  bool isShared() const override { return false; }
92  void beforeSequenceChanged(const IntSymbolListEditionEvent& event) override {}
93  void afterSequenceChanged(const IntSymbolListEditionEvent& event) override;
94  void beforeSequenceInserted(const IntSymbolListInsertionEvent& event) override {}
95  void afterSequenceInserted(const IntSymbolListInsertionEvent& event) override;
96  void beforeSequenceDeleted(const IntSymbolListDeletionEvent& event) override {}
97  void afterSequenceDeleted(const IntSymbolListDeletionEvent& event) override;
100 
101  size_t getSize() const { return mask_.size(); }
102 
103  const bool operator[](size_t i) const { return mask_[i]; }
104 
105  void setMask(const std::vector<bool>& mask)
106  {
107  if (mask.size() != mask_.size())
108  throw DimensionException("SequenceMask::setMask. Trying to replace mask by a vector with different length.", mask.size(), mask_.size());
109  mask_ = mask;
110  }
111 
115  const std::vector<bool>& getMask() const { return mask_; }
116 
117  void setMask(size_t pos, bool mask)
118  {
119  if (pos >= mask_.size())
120  throw Exception("SequenceMask::setMask. Vector overflow. Scores number: " + TextTools::toString(mask_.size()) + ", but trying to insert mask at position " + TextTools::toString(pos) + ".");
121  mask_[pos] = mask;
122  }
123 
124  void setMask(size_t pos, const std::vector<bool>& mask)
125  {
126  if (pos + mask.size() > mask_.size())
127  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) + ".");
128  std::copy(mask.begin(), mask.end(), mask_.begin() + static_cast<ptrdiff_t>(pos));
129  }
130 
131  bool merge(const SequenceAnnotation& anno) override
132  {
133  try
134  {
135  const SequenceMask* mask = &dynamic_cast<const SequenceMask&>(anno);
137  return true;
138  }
139  catch (std::exception& e)
140  {
141  return false;
142  }
143  }
144 
145  std::unique_ptr<SequenceAnnotation> getPartAnnotation(size_t pos, size_t len) const override
146  {
147  return std::unique_ptr<SequenceAnnotation>(new SequenceMask(std::vector<bool>(mask_.begin() + static_cast<ptrdiff_t>(pos), mask_.begin() + static_cast<ptrdiff_t>(pos + len)), removable_));
148  }
149 };
150 
160 {
161 public:
169  std::unique_ptr<SequenceWithAnnotation> createMaskAnnotation(const Sequence& seq);
170 };
171 }
172 #endif // BPP_SEQ_SEQUENCEWITHANNOTATIONTOOLS_H
size_t size() const override
Get the number of elements in the list.
Definition: SymbolList.h:124
virtual size_t size() const =0
Get the number of elements in the list.
Interface for sequence annotations.
SequenceMask(const std::vector< bool > &mask, bool removable=true)
Build a new SequenceMask object.
void beforeSequenceSubstituted(const IntSymbolListSubstitutionEvent &event) override
void setMask(size_t pos, bool mask)
void beforeSequenceChanged(const IntSymbolListEditionEvent &event) override
SequenceMask * clone() const override
void setMask(size_t pos, const std::vector< bool > &mask)
bool isShared() const override
std::unique_ptr< SequenceAnnotation > getPartAnnotation(size_t pos, size_t len) const override
void afterSequenceChanged(const IntSymbolListEditionEvent &event) override
const std::vector< bool > & getMask() const
void init(const Sequence &seq) override
void afterSequenceInserted(const IntSymbolListInsertionEvent &event) override
const std::string & getType() const override
static const std::string MASK
void afterSequenceSubstituted(const IntSymbolListSubstitutionEvent &event) override
bool merge(const SequenceAnnotation &anno) override
Merge the input annotation with the current one.
void afterSequenceDeleted(const IntSymbolListDeletionEvent &event) override
SequenceMask(size_t size=0, bool removable=true)
Build a new SequenceMask object.
bool isRemovable() const override
bool isValidWith(const SequenceWithAnnotation &sequence, bool throwException=true) const override
Test is the annotation is valid for a given sequence.
void beforeSequenceDeleted(const IntSymbolListDeletionEvent &event) override
const bool operator[](size_t i) const
void setMask(const std::vector< bool > &mask)
void beforeSequenceInserted(const IntSymbolListInsertionEvent &event) override
The SequenceWithAnnotationTools static class.
std::unique_ptr< SequenceWithAnnotation > createMaskAnnotation(const Sequence &seq)
Parse a sequence with a CaseMaskedAlphabet and creates a new SequenceWithAnnotation object with origi...
An implementation of the Sequence interface that supports annotation.
A basic implementation of the Sequence interface.
Definition: Sequence.h:117
static void append(std::vector< T > &vec1, const std::vector< T > &vec2)
std::string toString(T t)
This alphabet is used to deal NumericAlphabet.