bpp-seq  2.2.0
SequenceWithAnnotation.cpp
Go to the documentation of this file.
1 //
2 // File: SequenceWithAnnotation.cpp
3 // Created by: Julien Dutheil
4 // Created on: Mon Jul 19 2010
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 "SequenceWithAnnotation.h" // class's header file
41 
42 #include "Alphabet/AlphabetTools.h"
43 #include "StringSequenceTools.h"
44 #include <Bpp/Text/TextTools.h>
45 
46 using namespace bpp;
47 
48 // From the STL:
49 #include <iostream>
50 
51 using namespace std;
52 
53 /* Constructors: **************************************************************/
54 
56  EdSymbolList(alpha),
57  name_(),
58  comments_()
59 {}
60 
61 SequenceWithAnnotation::SequenceWithAnnotation(const std::string& name, const std::string& sequence, const Alphabet* alpha)
62 throw (BadCharException) :
63  EdSymbolList(alpha),
64  name_(name),
65  comments_()
66 {
67  if (sequence!="")
68  setContent(sequence);
69 }
70 
71 SequenceWithAnnotation::SequenceWithAnnotation(const std::string& name, const std::string& sequence, const Comments& comments, const Alphabet* alpha)
72  throw (BadCharException) :
73  EdSymbolList(alpha),
74  name_(name),
75  comments_(comments)
76 {
77  if (sequence != "")
78  setContent(sequence);
79 }
80 
81 SequenceWithAnnotation::SequenceWithAnnotation(const std::string& name, const std::vector<std::string>& sequence, const Alphabet* alpha)
82 throw (BadCharException) :
83  EdSymbolList(sequence, alpha),
84  name_(name),
85  comments_()
86 {}
87 
88 SequenceWithAnnotation::SequenceWithAnnotation(const std::string& name, const std::vector<std::string>& sequence, const Comments& comments, const Alphabet* alpha)
89  throw (BadCharException) :
90  EdSymbolList(sequence, alpha),
91  name_(name),
92  comments_(comments)
93 {}
94 
95 SequenceWithAnnotation::SequenceWithAnnotation(const std::string& name, const std::vector<int>& sequence, const Alphabet* alpha)
96  throw (BadIntException) :
97  EdSymbolList(sequence, alpha),
98  name_(name),
99  comments_()
100 {}
101 
102 SequenceWithAnnotation::SequenceWithAnnotation(const std::string& name, const std::vector<int>& sequence, const Comments& comments, const Alphabet* alpha)
103  throw (BadIntException) :
104  EdSymbolList(sequence, alpha),
105  name_(name),
106  comments_(comments)
107 {}
108 
109 /* Copy constructors: *********************************************************/
110 
112  EdSymbolList(s),
113  name_(s.getName()),
114  comments_(s.getComments())
115 {}
116 
118  EdSymbolList(s),
119  name_(s.getName()),
120  comments_(s.getComments())
121 {}
122 
123 /* Assignation operator: ******************************************************/
124 
126 {
128  name_ = s.getName();
129  comments_ = s.getComments();
130  return *this;
131 }
132 
134 {
136  name_ = s.getName();
137  comments_ = s.getComments();
138  return *this;
139 }
140 
141 /******************************************************************************/
142 
143 void SequenceWithAnnotation::setContent(const std::string& sequence) throw (BadCharException)
144 {
145  SymbolListEditionEvent event(this);
146  fireBeforeSequenceChanged(event);
147  // Remove blanks in sequence
148  content_ = StringSequenceTools::codeSequence(TextTools::removeWhiteSpaces(sequence), getAlphabet());
149  //Warning, an exception may be thrown here!
150  fireAfterSequenceChanged(event);
151 }
152 
153 /******************************************************************************/
154 
156 {
157  // Size verification
158  size_t seqSize = content_.size();
159  if (newSize == seqSize) return;
160 
161  if (newSize < seqSize)
162  {
163  SymbolListDeletionEvent event(this, newSize, seqSize - newSize);
165  content_.resize(newSize);
167  return;
168  }
169 
170  // Add gaps up to specified size
171  SymbolListInsertionEvent event(this, seqSize, newSize - seqSize);
173  int gap = getAlphabet()->getGapCharacterCode();
174  while (content_.size() < newSize) content_.push_back(gap);
176 }
177 
178 /******************************************************************************/
179 
181 {
182  // Size verification
183  size_t seqSize = content_.size();
184  if (newSize == seqSize) return;
185 
186  if (newSize < seqSize)
187  {
188  //We must truncate sequence from the left.
189  SymbolListDeletionEvent event(this, 0, seqSize - newSize);
191  content_.erase(content_.begin(), content_.begin() + static_cast<ptrdiff_t>(seqSize - newSize));
193  return;
194  }
195 
196  // Add gaps up to specified size
197  SymbolListInsertionEvent event(this, 0, newSize - seqSize);
199  int gap = getAlphabet()->getGapCharacterCode();
200  content_.insert(content_.begin(), newSize - seqSize, gap);
202 }
203 
204 /******************************************************************************/
205 
206 void SequenceWithAnnotation::append(const std::vector<int>& content) throw (BadIntException)
207 {
208  SymbolListInsertionEvent event(this, content_.size(), content.size());
209  fireBeforeSequenceInserted(event);
210  // Check list for incorrect characters
211  for (unsigned int i = 0; i < content.size(); i++)
212  if(!getAlphabet()->isIntInAlphabet(content[i]))
213  throw BadIntException(content[i], "SequenceWithAnnotation::append", getAlphabet());
214  //SequenceWithAnnotation is valid:
215  for (unsigned int i = 0; i < content.size(); i++)
216  content_.push_back(content[i]);
217 
218  fireAfterSequenceInserted(event);
219 }
220 
221 void SequenceWithAnnotation::append(const std::vector<std::string>& content) throw (BadCharException)
222 {
223  SymbolListInsertionEvent event(this, content_.size(), content.size());
224  fireBeforeSequenceInserted(event);
225  // Check list for incorrect characters
226  for (unsigned int i = 0; i < content.size(); i++)
227  if(!getAlphabet()->isCharInAlphabet(content[i]))
228  throw BadCharException(content[i], "SequenceWithAnnotation::append", getAlphabet());
229 
230  //SequenceWithAnnotation is valid:
231  for (unsigned int i = 0; i < content.size(); i++)
232  content_.push_back(getAlphabet()->charToInt(content[i]));
233 
234  fireAfterSequenceInserted(event);
235 }
236 
237 void SequenceWithAnnotation::append(const std::string& content) throw (BadCharException)
238 {
239  append(StringSequenceTools::codeSequence(content, getAlphabet()));
240 }
241 
242 /******************************************************************************/
243 
245 {
246  vector<string> types;
247  for (unsigned int i = 0; i < getNumberOfListeners(); ++i) {
248  const SequenceAnnotation* anno = dynamic_cast<const SequenceAnnotation*>(&getListener(i));
249  if (anno)
250  types.push_back(anno->getType());
251  }
252  return types;
253 }
254 
255 /******************************************************************************/
256 
258  throw (AlphabetMismatchException, Exception)
259 {
260  // Sequence's alphabets matching verification
261  if ((swa.getAlphabet()->getAlphabetType()) != (getAlphabet()->getAlphabetType()))
262  throw AlphabetMismatchException("SequenceWithAnnotation::merge: Sequence's alphabets don't match ", swa.getAlphabet(), getAlphabet());
263 
264  // Sequence's names matching verification
265  if (swa.getName() != getName())
266  throw Exception ("SequenceWithAnnotation::merge: Sequence's names don't match");
267 
268  // Concatenate sequences and send result
269  propagateEvents(false);
270  append(swa.getContent());
271  propagateEvents(true);
272 
273  // Try to merge annotations.
274  //First start with annotations in this sequence:
275  vector<string> types = getAnnotationTypes();
276  vector<string> newTypes = swa.getAnnotationTypes();
277  for (unsigned int i = 0; i < types.size(); ++i) {
278  vector<string>::iterator it = find(newTypes.begin(), newTypes.end(), types[i]);
279  if (it != newTypes.end()) {
280  //Merge annotations:
281  getAnnotation(types[i]).merge(swa.getAnnotation(types[i]));
282  //Remove annotation from the list:
283  newTypes.erase(it);
284  } else {
285  //Extend annotation to the right:
286  auto_ptr<SequenceAnnotation> anno(getAnnotation(types[i]).clone());
287  anno->init(swa);
288  getAnnotation(types[i]).merge(*anno);
289  }
290  }
291  //Now look for annotations in the input sequence:
292  for (unsigned int i = 0; i < newTypes.size(); ++i) {
293  //Extend annotation from the left:
294  SequenceAnnotation* anno = swa.getAnnotation(newTypes[i]).clone();
295  anno->init(*this);
296  anno->merge(swa.getAnnotation(newTypes[i]));
297  addAnnotation(anno);
298  }
299 }
300 
301 
302 /******************************************************************************/
303 
std::vector< int > content_
The list content.
Definition: SymbolList.h:518
An alphabet exception thrown when trying to specify a bad char to the alphabet.
std::vector< std::string > Comments
Declaration of Comments type.
Definition: Sequence.h:60
virtual void setContent(const std::string &sequence)
Set the whole content of the sequence.
virtual SequenceAnnotation * clone() const =0
virtual const Alphabet * getAlphabet() const
Get the alphabet associated to the list.
Definition: SymbolList.h:599
const Comments & getComments() const
Get the comments associated to this sequence.
virtual void setToSizeR(size_t newSize)
Set up the size of a sequence from the right side.
virtual std::vector< std::string > getAnnotationTypes() const
virtual size_t getNumberOfListeners() const
Definition: SymbolList.h:645
This alphabet is used to deal NumericAlphabet.
SequenceWithAnnotation(const Alphabet *alpha)
Empty constructor: build a void Sequence with just an Alphabet.
std::string name_
The sequence name.
The Alphabet interface.
Definition: Alphabet.h:130
STL namespace.
virtual const std::string & getType() const =0
virtual int getGapCharacterCode() const =0
virtual const std::string & getName() const =0
Get the name of this sequence.
static std::vector< int > codeSequence(const std::string &sequence, const Alphabet *alphabet)
Convert a string sequence to a vector of int.
virtual void merge(const SequenceWithAnnotation &swa)
Merge a sequence with the current one.
virtual bool merge(const SequenceAnnotation &anno)=0
Merge the input annotation with the current one.
virtual const Comments & getComments() const =0
Get the comments associated to this sequence.
Interface for sequence annotations.
An implementation of the Sequence interface that supports annotation.
EdSymbolList & operator=(const SymbolList &list)
The generic assignment operator.
Definition: SymbolList.cpp:249
virtual void setToSizeL(size_t newSize)
Set up the size of a sequence from the left side.
SequenceWithAnnotation & operator=(const Sequence &s)
The Sequence generic assignment operator. This does not perform a hard copy of the alphabet object...
void fireBeforeSequenceDeleted(const SymbolListDeletionEvent &event)
Definition: SymbolList.h:706
The sequence interface.
Definition: Sequence.h:74
Comments comments_
The sequence comments.
void fireBeforeSequenceInserted(const SymbolListInsertionEvent &event)
Definition: SymbolList.h:692
An alphabet exception thrown when trying to specify a bad int to the alphabet.
A event-driven SymbolList object.
Definition: SymbolList.h:500
virtual const SymbolListListener & getListener(size_t i) const
Definition: SymbolList.h:647
const std::string & getName() const
Get the name of this sequence.
Exception thrown when two alphabets do not match.
virtual void init(const Sequence &seq)=0
void fireAfterSequenceDeleted(const SymbolListDeletionEvent &event)
Definition: SymbolList.h:713
void fireAfterSequenceInserted(const SymbolListInsertionEvent &event)
Definition: SymbolList.h:699
void append(const std::vector< int > &content)
Append the specified content to the sequence.