bpp-seq  2.1.0
 All Classes Namespaces Files Functions Variables Friends Pages
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"
46 
47 namespace bpp {
48 
49  class SequenceMask :
50  public virtual SequenceAnnotation
51  {
52  private:
53  bool removable_;
54  std::vector<bool> mask_;
55 
56  public:
57  static const std::string MASK;
58 
59  public:
60 
74  SequenceMask(size_t size = 0, bool removable = true) :
75  removable_(removable),
76  mask_(size, false)
77  {}
78 
79 
88  SequenceMask(const std::vector<bool>& mask, bool removable = true) :
89  removable_(removable),
90  mask_(mask)
91  {}
92 
99  virtual ~SequenceMask() {}
106 #ifdef NO_VIRTUAL_COV
107  Clonable*
108 #else
109  SequenceMask*
110 #endif
111  clone() const { return new SequenceMask(*this); }
114  public:
115  void init(const Sequence& seq)
116  {
117  mask_.resize(seq.size());
118  std::fill(mask_.begin(), mask_.end(), false);
119  }
120 
121  const std::string& getType() const { return MASK; }
122 
123  bool isValidWith(const SequenceWithAnnotation& sequence, bool throwException = true) const
124  {
125  if (throwException && mask_.size() != sequence.size()) throw Exception("SequenceMask. The mask size must match the sequence size.");
126  return (mask_.size() == sequence.size());
127  }
128 
129  bool isRemovable() const { return removable_; }
130  bool isShared() const { return false; }
132  void afterSequenceChanged(const SymbolListEditionEvent& event);
139 
140  size_t getSize() const { return mask_.size(); }
141 
142  const bool operator[](size_t i) const { return mask_[i]; }
143 
144  void setMask(const std::vector<bool>& mask) {
145  if (mask.size() != mask_.size())
146  throw DimensionException("SequenceMask::setMask. Trying to replace mask by a vector with different length.", mask.size(), mask_.size());
147  mask_ = mask;
148  }
149 
153  const std::vector<bool>& getMask() const { return mask_; }
154 
155  void setMask(size_t pos, bool mask) {
156  if (pos >= mask_.size())
157  throw Exception("SequenceMask::setMask. Vector overflow. Scores number: " + TextTools::toString(mask_.size()) + ", but trying to insert mask at position " + TextTools::toString(pos) + ".");
158  mask_[pos] = mask;
159  }
160 
161  void setMask(size_t pos, const std::vector<bool>& mask) {
162  if (pos + mask.size() > mask_.size())
163  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) + ".");
164  std::copy(mask.begin(), mask.end(), mask_.begin() + pos);
165  }
166 
167  bool merge(const SequenceAnnotation& anno) {
168  try {
169  const SequenceMask* mask = & dynamic_cast<const SequenceMask&>(anno);
171  return true;
172  } catch (std::exception& e) {
173  return false;
174  }
175  }
176 
177  SequenceAnnotation* getPartAnnotation(size_t pos, size_t len) const throw (Exception) {
178  return new SequenceMask(std::vector<bool>(mask_.begin() + pos, mask_.begin() + pos + len), removable_);
179  }
180  };
181 
191 
192  public:
201 
202  };
203 }
204 
205 #endif // _SEQUENCEWITHANNOTATIONTOOLS_H_