bpp-seq  2.2.0
SymbolList.cpp
Go to the documentation of this file.
1 //
2 // File: SymbolList.cpp
3 // Created by: Julien Dutheil
4 // Created on: Fri Apr 9 2005
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
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 #include "SymbolList.h"
41 #include "StringSequenceTools.h"
42 
43 using namespace bpp;
44 
45 using namespace std;
46 
47 /****************************************************************************************/
48 
49 BasicSymbolList::BasicSymbolList(const std::vector<string>& list, const Alphabet* alpha) throw (BadCharException) :
50  alphabet_(alpha), content_()
51 {
52  setContent(list);
53 }
54 
55 BasicSymbolList::BasicSymbolList(const std::vector<int>& list, const Alphabet* alpha) throw (BadIntException) :
56  alphabet_(alpha), content_()
57 {
58  setContent(list);
59 }
60 
61 /****************************************************************************************/
62 
64  alphabet_(list.getAlphabet()), content_(list.getContent()) {}
65 
67  alphabet_(list.getAlphabet()), content_(list.getContent()) {}
68 
70 {
71  content_ = list.getContent();
72  alphabet_ = list.getAlphabet();
73  return *this;
74 }
75 
77 {
78  content_ = list.getContent();
79  alphabet_ = list.getAlphabet();
80  return *this;
81 }
82 
83 /****************************************************************************************/
84 
85 void BasicSymbolList::setContent(const vector<string>& list) throw (BadCharException)
86 {
87  // Check list for incorrect characters
88  vector<int> coded(list.size());
89  for (size_t i = 0; i < list.size(); i++)
90  if(!alphabet_->isCharInAlphabet(list[i])) throw BadCharException(list[i], "BasicSymbolList::setContent", alphabet_);
91 
92  for (size_t i = 0; i < list.size(); i++)
93  coded[i] = alphabet_->charToInt(list[i]);
94 
95  //BasicSymbolList is valid:
96  content_ = coded;
97 };
98 
99 /****************************************************************************************/
100 
101 void BasicSymbolList::setContent(const vector<int>& list) throw (BadIntException)
102 {
103  // Check list for incorrect characters
104  for (size_t i = 0; i < list.size(); i++)
105  if(!alphabet_->isIntInAlphabet(list[i]))
106  throw BadIntException(list[i], "BasicSymbolList::setContent", alphabet_);
107 
108  //Sequence is valid:
109  content_ = list;
110 };
111 
112 /****************************************************************************************/
113 
115 {
117 };
118 
119 /****************************************************************************************/
120 
121 void BasicSymbolList::addElement(const string& c) throw (BadCharException)
122 {
123  content_.push_back(alphabet_->charToInt(c));
124 }
125 
126 /****************************************************************************************/
127 
128 void BasicSymbolList::addElement(size_t pos, const string& c) throw (BadCharException, IndexOutOfBoundsException)
129 {
130  if(pos >= content_.size()) throw IndexOutOfBoundsException("BasicSymbolList::addElement. Invalid position.", pos, 0, size() - 1);
131  content_.insert(content_.begin() + static_cast<ptrdiff_t>(pos), alphabet_->charToInt(c));
132 }
133 
134 /****************************************************************************************/
135 
136 void BasicSymbolList::setElement(size_t pos, const string& c) throw (BadCharException, IndexOutOfBoundsException)
137 {
138  if(pos >= content_.size())
139  throw IndexOutOfBoundsException("BasicSymbolList::setElement. Invalid position.", pos, 0, size() - 1);
140  content_[pos] = alphabet_->charToInt(c);
141 }
142 
143 /****************************************************************************************/
144 
145 string BasicSymbolList::getChar(size_t pos) const throw (IndexOutOfBoundsException)
146 {
147  if(pos >= content_.size())
148  throw IndexOutOfBoundsException("BasicSymbolList::getChar. Invalid position.", pos, 0, size() - 1);
149  string c = "";
150  try
151  {
152  c = alphabet_->intToChar(content_[pos]);
153  }
154  catch(BadIntException bie)
155  {
156  //This should never happen!
157  }
158  return c;
159 }
160 
161 /****************************************************************************************/
162 
163 void BasicSymbolList::deleteElement(size_t pos) throw (IndexOutOfBoundsException)
164 {
165  if(pos >= content_.size())
166  throw IndexOutOfBoundsException("BasicSymbolList::deleteElement. Invalid position.", pos, 0, size() - 1);
167  content_.erase(content_.begin() + static_cast<ptrdiff_t>(pos));
168 }
169 
170 /****************************************************************************************/
171 
172 void BasicSymbolList::deleteElements(size_t pos, size_t len) throw (IndexOutOfBoundsException)
173 {
174  if (pos + len > content_.size())
175  throw IndexOutOfBoundsException("BasicSymbolList::deleteElements. Invalid position.", pos + len, 0, size() - 1);
176  content_.erase(content_.begin() + static_cast<ptrdiff_t>(pos), content_.begin() + static_cast<ptrdiff_t>(pos + len));
177 }
178 
179 /****************************************************************************************/
180 
182 {
183  //test:
184  alphabet_->intToChar(v);
185  content_.push_back(v);
186 }
187 
188 /****************************************************************************************/
189 
190 void BasicSymbolList::addElement(size_t pos, int v) throw (BadIntException, IndexOutOfBoundsException)
191 {
192  //test:
193  if(pos >= content_.size())
194  throw IndexOutOfBoundsException("BasicSymbolList::addElement. Invalid position.", pos, 0, size() - 1);
195  alphabet_->intToChar(v);
196  content_.insert(content_.begin() + static_cast<ptrdiff_t>(pos), v);
197 }
198 
199 /****************************************************************************************/
200 
201 void BasicSymbolList::setElement(size_t pos, int v) throw (BadIntException, IndexOutOfBoundsException)
202 {
203  //test:
204  if(pos >= content_.size())
205  throw IndexOutOfBoundsException("BasicSymbolList::setElement. Invalid position.", pos, 0, size() - 1);
206  alphabet_->intToChar(v);
207  content_[pos] = v;
208 }
209 
210 /****************************************************************************************/
211 
212 int BasicSymbolList::getValue(size_t pos) const throw (IndexOutOfBoundsException)
213 {
214  if(pos >= content_.size())
215  throw IndexOutOfBoundsException("BasicSymbolList::getValue. Invalid position.", pos, 0, size() - 1);
216  return content_[pos];
217 }
218 
219 /****************************************************************************************/
220 
221 
222 /****************************************************************************************/
223 
224 EdSymbolList::EdSymbolList(const std::vector<string>& list, const Alphabet* alpha) throw (BadCharException) :
225  alphabet_(alpha), propagateEvents_(true), content_(), listeners_()
226 {
227  setContent(list);
228 }
229 
230 EdSymbolList::EdSymbolList(const std::vector<int>& list, const Alphabet* alpha) throw (BadIntException) :
231  alphabet_(alpha), propagateEvents_(true), content_(), listeners_()
232 {
233  setContent(list);
234 }
235 
236 /****************************************************************************************/
237 
239  alphabet_(list.getAlphabet()), propagateEvents_(true), content_(list.getContent()), listeners_() {}
240 
242  alphabet_(list.getAlphabet()), propagateEvents_(list.propagateEvents_), content_(list.getContent()), listeners_(list.listeners_)
243 {
244  for (size_t i = 0; i < listeners_.size(); ++i)
245  if (!list.listeners_[i]->isShared())
246  listeners_[i] = dynamic_cast<SymbolListListener*>(list.listeners_[i]->clone());
247 }
248 
250 {
251  content_ = list.getContent();
252  alphabet_ = list.getAlphabet();
253  propagateEvents_ = true;
254  for (size_t i = 0; i < listeners_.size(); ++i)
255  if (!listeners_[i]->isShared())
256  delete listeners_[i];
257  listeners_.clear();
258  return *this;
259 }
260 
262 {
263  content_ = list.getContent();
264  alphabet_ = list.getAlphabet();
266  for (size_t i = 0; i < listeners_.size(); ++i)
267  delete listeners_[i];
268  listeners_ = list.listeners_;
269  for (size_t i = 0; i < listeners_.size(); ++i)
270  if (!list.listeners_[i]->isShared())
271  listeners_[i] = dynamic_cast<SymbolListListener*>(list.listeners_[i]->clone());
272  return *this;
273 }
274 
275 /****************************************************************************************/
276 
277 void EdSymbolList::setContent(const vector<string>& list) throw (BadCharException)
278 {
279  SymbolListEditionEvent event(this);
280  fireBeforeSequenceChanged(event);
281 
282  // Check list for incorrect characters
283  vector<int> coded(list.size());
284  for (size_t i = 0; i < list.size(); i++)
285  if (!alphabet_->isCharInAlphabet(list[i])) throw BadCharException(list[i], "EdSymbolList::setContent", alphabet_);
286 
287  for (size_t i = 0; i < list.size(); i++)
288  coded[i] = alphabet_->charToInt(list[i]);
289 
290  //SymbolList is valid:
291  content_ = coded;
292  fireAfterSequenceChanged(event);
293 };
294 
295 /****************************************************************************************/
296 
297 void EdSymbolList::setContent(const vector<int>& list) throw (BadIntException)
298 {
299  SymbolListEditionEvent event(this);
300  fireBeforeSequenceChanged(event);
301 
302  // Check list for incorrect characters
303  for (size_t i = 0; i < list.size(); i++)
304  if(!alphabet_->isIntInAlphabet(list[i]))
305  throw BadIntException(list[i], "EdSymbolList::setContent", alphabet_);
306 
307  //Sequence is valid:
308  content_ = list;
309  fireAfterSequenceChanged(event);
310 };
311 
312 /****************************************************************************************/
313 
315 {
317 };
318 
319 /****************************************************************************************/
320 
321 void EdSymbolList::addElement(const string& c) throw (BadCharException)
322 {
323  SymbolListInsertionEvent event(this, size(), 1);
324  fireBeforeSequenceInserted(event);
325  content_.push_back(alphabet_->charToInt(c));
326  fireAfterSequenceInserted(event);
327 }
328 
329 /****************************************************************************************/
330 
331 void EdSymbolList::addElement(size_t pos, const string& c) throw (BadCharException, IndexOutOfBoundsException)
332 {
333  if (pos >= content_.size()) throw IndexOutOfBoundsException("EdSymbolList::addElement. Invalid position.", pos, 0, size() - 1);
334  SymbolListInsertionEvent event(this, pos, 1);
335  fireBeforeSequenceInserted(event);
336  content_.insert(content_.begin() + static_cast<ptrdiff_t>(pos), alphabet_->charToInt(c));
337  fireAfterSequenceInserted(event);
338 }
339 
340 /****************************************************************************************/
341 
342 void EdSymbolList::setElement(size_t pos, const string& c) throw (BadCharException, IndexOutOfBoundsException)
343 {
344  if (pos >= content_.size())
345  throw IndexOutOfBoundsException("EdSymbolList::setElement. Invalid position.", pos, 0, size() - 1);
346  SymbolListSubstitutionEvent event(this, pos, pos);
347  fireBeforeSequenceSubstituted(event);
348  content_[pos] = alphabet_->charToInt(c);
349  fireAfterSequenceSubstituted(event);
350 }
351 
352 /****************************************************************************************/
353 
354 string EdSymbolList::getChar(size_t pos) const throw (IndexOutOfBoundsException)
355 {
356  if (pos >= content_.size())
357  throw IndexOutOfBoundsException("EdSymbolList::getChar. Invalid position.", pos, 0, size() - 1);
358  string c = "";
359  try {
360  c = alphabet_->intToChar(content_[pos]);
361  } catch(BadIntException bie) {
362  //This should never happen!
363  }
364  return c;
365 }
366 
367 /****************************************************************************************/
368 
369 void EdSymbolList::deleteElement(size_t pos) throw (IndexOutOfBoundsException)
370 {
371  if (pos >= content_.size())
372  throw IndexOutOfBoundsException("EdSymbolList::deleteElement. Invalid position.", pos, 0, size() - 1);
373  SymbolListDeletionEvent event(this, pos, 1);
374  fireBeforeSequenceDeleted(event);
375  content_.erase(content_.begin() + static_cast<ptrdiff_t>(pos));
376  fireAfterSequenceDeleted(event);
377 }
378 
379 /****************************************************************************************/
380 
381 void EdSymbolList::deleteElements(size_t pos, size_t len) throw (IndexOutOfBoundsException)
382 {
383  if(pos + len > content_.size())
384  throw IndexOutOfBoundsException("EdSymbolList::deleteElements. Invalid position.", pos + len, 0, size() - 1);
385  SymbolListDeletionEvent event(this, pos, len);
386  fireBeforeSequenceDeleted(event);
387  content_.erase(content_.begin() + static_cast<ptrdiff_t>(pos), content_.begin() + static_cast<ptrdiff_t>(pos + len));
388  fireAfterSequenceDeleted(event);
389 }
390 
391 
392 /****************************************************************************************/
393 
395 {
396  SymbolListInsertionEvent event(this, size(), 1);
397  fireBeforeSequenceInserted(event);
398  //test:
399  alphabet_->intToChar(v);
400  content_.push_back(v);
401  fireAfterSequenceInserted(event);
402 }
403 
404 /****************************************************************************************/
405 
406 void EdSymbolList::addElement(size_t pos, int v) throw (BadIntException, IndexOutOfBoundsException)
407 {
408  //test:
409  if (pos >= content_.size())
410  throw IndexOutOfBoundsException("EdSymbolList::addElement. Invalid position.", pos, 0, size() - 1);
411  SymbolListInsertionEvent event(this, pos, 1);
412  fireBeforeSequenceInserted(event);
413  alphabet_->intToChar(v);
414  content_.insert(content_.begin() + static_cast<ptrdiff_t>(pos), v);
415  fireAfterSequenceInserted(event);
416 }
417 
418 /****************************************************************************************/
419 
420 void EdSymbolList::setElement(size_t pos, int v) throw (BadIntException, IndexOutOfBoundsException)
421 {
422  //test:
423  if (pos >= content_.size())
424  throw IndexOutOfBoundsException("EdSymbolList::setElement. Invalid position.", pos, 0, size() - 1);
425  SymbolListSubstitutionEvent event(this, pos, pos);
426  fireBeforeSequenceSubstituted(event);
427  alphabet_->intToChar(v);
428  content_[pos] = v;
429  fireAfterSequenceSubstituted(event);
430 }
431 
432 /****************************************************************************************/
433 
434 int EdSymbolList::getValue(size_t pos) const throw (IndexOutOfBoundsException)
435 {
436  if (pos >= content_.size())
437  throw IndexOutOfBoundsException("EdSymbolList::getValue. Invalid position.", pos, 0, size() - 1);
438  return content_[pos];
439 }
440 
441 /****************************************************************************************/
442 
std::vector< int > content_
The list content.
Definition: SymbolList.h:518
virtual const std::vector< int > & getContent() const
Get the whole content of the list as a vector of int.
Definition: SymbolList.h:603
An alphabet exception thrown when trying to specify a bad char to the alphabet.
EdSymbolList(const Alphabet *alpha)
Build a new void BasicSymbolList object with the specified alphabet.
Definition: SymbolList.h:532
The SymbolList interface.
Definition: SymbolList.h:60
virtual const Alphabet * getAlphabet() const
Get the alphabet associated to the list.
Definition: SymbolList.h:599
const Alphabet * alphabet_
The Alphabet attribute must be initialized in constructor and then can never be changed.
Definition: SymbolList.h:274
BasicSymbolList & operator=(const SymbolList &list)
The generic assignment operator.
Definition: SymbolList.cpp:69
This alphabet is used to deal NumericAlphabet.
virtual void setContent(const std::vector< int > &list)
Set the whole content of the list.
Definition: SymbolList.cpp:297
virtual std::string getChar(size_t pos) const
Get the element at position &#39;pos&#39; as a character.
Definition: SymbolList.cpp:145
virtual std::string toString() const
Convert the list as a string.
Definition: SymbolList.cpp:314
The Alphabet interface.
Definition: Alphabet.h:130
STL namespace.
static std::string decodeSequence(const std::vector< int > &sequence, const Alphabet *alphabet)
Convert a sequence to its string representation.
virtual void deleteElement(size_t pos)
Delete the element at position &#39;pos&#39;.
Definition: SymbolList.cpp:369
std::vector< SymbolListListener * > listeners_
Contains the listeners.
Definition: SymbolList.h:523
virtual void deleteElements(size_t pos, size_t len)
Delete the elements at position &#39;pos&#39;.
Definition: SymbolList.cpp:172
virtual int getValue(size_t pos) const
Get the element at position &#39;pos&#39; as an int.
Definition: SymbolList.cpp:434
virtual const std::vector< int > & getContent() const
Get the whole content of the list as a vector of int.
Definition: SymbolList.h:352
virtual void setElement(size_t pos, const std::string &c)
Set the element at position &#39;pos&#39; to character &#39;c&#39;.
Definition: SymbolList.cpp:342
virtual std::string getChar(size_t pos) const
Get the element at position &#39;pos&#39; as a character.
Definition: SymbolList.cpp:354
virtual void setContent(const std::vector< int > &list)
Set the whole content of the list.
Definition: SymbolList.cpp:101
virtual void addElement(const std::string &c)
Add a character to the end of the list.
Definition: SymbolList.cpp:121
virtual int getValue(size_t pos) const
Get the element at position &#39;pos&#39; as an int.
Definition: SymbolList.cpp:212
A basic SymbolList object.
Definition: SymbolList.h:264
virtual void deleteElements(size_t pos, size_t len)
Delete the elements at position &#39;pos&#39;.
Definition: SymbolList.cpp:381
std::vector< int > content_
The list content.
Definition: SymbolList.h:280
EdSymbolList & operator=(const SymbolList &list)
The generic assignment operator.
Definition: SymbolList.cpp:249
virtual const Alphabet * getAlphabet() const =0
Get the alphabet associated to the list.
virtual const std::vector< int > & getContent() const =0
Get the whole content of the list as a vector of int.
virtual std::string toString() const
Convert the list as a string.
Definition: SymbolList.cpp:114
An alphabet exception thrown when trying to specify a bad int to the alphabet.
virtual const Alphabet * getAlphabet() const
Get the alphabet associated to the list.
Definition: SymbolList.h:348
A event-driven SymbolList object.
Definition: SymbolList.h:500
const Alphabet * alphabet_
The Alphabet attribute must be initialized in constructor and then can never be changed.
Definition: SymbolList.h:510
virtual void setElement(size_t pos, const std::string &c)
Set the element at position &#39;pos&#39; to character &#39;c&#39;.
Definition: SymbolList.cpp:136
virtual void deleteElement(size_t pos)
Delete the element at position &#39;pos&#39;.
Definition: SymbolList.cpp:163
virtual void addElement(const std::string &c)
Add a character to the end of the list.
Definition: SymbolList.cpp:321
BasicSymbolList(const Alphabet *alpha)
Build a new void BasicSymbolList object with the specified alphabet.
Definition: SymbolList.h:288