bpp-seq  2.2.0
VectorSequenceContainer.cpp
Go to the documentation of this file.
1 //
2 // File VectorSequenceContainer.cpp
3 // Author : Guillaume Deuchst
4 // Julien Dutheil
5 // Last modification : Wednesday July 30 2003
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for sequences analysis.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39  */
40 
42 #include <Bpp/Text/TextTools.h>
43 
44 using namespace bpp;
45 using namespace std;
46 
50  const std::vector<const Sequence*>& vs,
51  const Alphabet* alpha)
54  sequences_()
55 {
56  for (std::vector<const Sequence*>::const_iterator i = vs.begin(); i < vs.end(); i++)
57  {
58  addSequence(**i);
59  }
60 }
61 
65  const VectorSequenceContainer& vsc) :
67  sequences_()
68 {
69  size_t max = vsc.getNumberOfSequences();
70  for (size_t i = 0; i < max; i++)
71  {
72  addSequence(vsc.getSequence(i), false);
73  }
74 }
75 
77  const OrderedSequenceContainer& osc) :
78  AbstractSequenceContainer(osc.getAlphabet()),
79  sequences_()
80 {
81  // Sequences insertion
82  for (unsigned int i = 0; i < osc.getNumberOfSequences(); i++)
83  {
84  addSequence(osc.getSequence(i), false);
85  }
86 }
87 
89  const SequenceContainer& sc) :
90  AbstractSequenceContainer(sc.getAlphabet()),
91  sequences_()
92 {
93  // Sequences insertion
94  std::vector<std::string> names = sc.getSequencesNames();
95  for (unsigned int i = 0; i < names.size(); i++)
96  {
97  addSequence(sc.getSequence(names[i]), false);
98  }
99 
101 }
102 
106  const VectorSequenceContainer& vsc)
107 {
108  clear();
110 
111  // Sequences insertion
112  size_t max = vsc.getNumberOfSequences();
113  for (size_t i = 0; i < max; i++)
114  {
115  addSequence(vsc.getSequence(i), false);
116  }
117 
118  return *this;
119 }
120 
122  const OrderedSequenceContainer& osc)
123 {
124  clear();
126 
127  // Sequences insertion
128  size_t max = osc.getNumberOfSequences();
129  for (unsigned int i = 0; i < max; i++)
130  {
131  addSequence(osc.getSequence(i), false);
132  }
133 
134  return *this;
135 }
136 
137 /******************************************************************************/
138 
140  const SequenceContainer& sc)
141 {
142  clear();
144 
145  // Seq names:
146  std::vector<std::string> names = sc.getSequencesNames();
147 
148  for (unsigned int i = 0; i < names.size(); i++)
149  {
150  addSequence(sc.getSequence(names[i]), false);
151  }
152 
153  return *this;
154 }
155 
156 /******************************************************************************/
157 
158 const Sequence& VectorSequenceContainer::getSequence(size_t sequenceIndex) const throw (IndexOutOfBoundsException)
159 {
160  // Specified sequence existence verification
161  if (sequenceIndex < sequences_.size())
162  return *sequences_[sequenceIndex];
163  throw IndexOutOfBoundsException("VectorSequenceContainer::getSequence.", sequenceIndex, 0, sequences_.size() - 1);
164 }
165 
166 /******************************************************************************/
167 
168 bool VectorSequenceContainer::hasSequence(const string& name) const
169 {
170  // Specified sequence name research into all sequences
171  for (size_t i = 0; i < sequences_.size(); i++)
172  {
173  if (sequences_[i]->getName() == name)
174  return true;
175  }
176  return false;
177 }
178 
179 /******************************************************************************/
180 
182 {
183  // Specified sequence name research into all sequences
184  for (size_t i = 0; i < sequences_.size(); i++)
185  {
186  if (sequences_[i]->getName() == name)
187  return *sequences_[i];
188  }
189  throw SequenceNotFoundException("VectorSequenceContainer::getSequence : Specified sequence doesn't exist", name);
190 }
191 
192 /******************************************************************************/
193 
194 Sequence& VectorSequenceContainer::getSequence_(size_t sequenceIndex) throw (IndexOutOfBoundsException)
195 {
196  // Specified sequence existence verification
197  if (sequenceIndex < sequences_.size())
198  return *sequences_[sequenceIndex];
199  throw IndexOutOfBoundsException("VectorSequenceContainer::getSequence.", sequenceIndex, 0, sequences_.size() - 1);
200 }
201 
202 /******************************************************************************/
203 
205 {
206  // Specified sequence name research into all sequences
207  for (size_t i = 0; i < sequences_.size(); i++)
208  {
209  if (sequences_[i]->getName() == name)
210  return *sequences_[i];
211  }
212  throw SequenceNotFoundException("VectorSequenceContainer::getSequence : Specified sequence doesn't exist", name);
213 }
214 
215 /******************************************************************************/
216 
218 {
219  // Specified sequence name research into all sequences
220  for (size_t i = 0; i < sequences_.size(); i++)
221  {
222  if (sequences_[i]->getName() == name)
223  return i;
224  }
225  throw SequenceNotFoundException("VectorSequenceContainer::getSequencePosition : Specified sequence doesn't exist", name);
226 }
227 
228 /******************************************************************************/
229 
230 void VectorSequenceContainer::setSequence(size_t sequenceIndex, const Sequence& sequence, bool checkName) throw (Exception)
231 {
232  // Sequence's name existence checking
233  if (checkName)
234  {
235  // For all names in vector : throw exception if name already exists
236  for (size_t j = 0; j < sequences_.size(); j++)
237  {
238  if (sequences_[j]->getName() == sequence.getName())
239  if (j != sequenceIndex)
240  throw Exception("VectorSequenceContainer::setSequence : Sequence's name already exists in container");
241  }
242  }
243 
244  // New sequence's alphabet and sequence container's alphabet matching verification
245  if (sequence.getAlphabet()->getAlphabetType() == getAlphabet()->getAlphabetType())
246  {
247  // Delete old sequence
248  delete sequences_[sequenceIndex];
249  // New sequence insertion in sequence container
250  sequences_[sequenceIndex] = dynamic_cast<Sequence*>(sequence.clone());
251  }
252  else
253  throw AlphabetMismatchException("VectorSequenceContainer::setSequence : Alphabets don't match", getAlphabet(), sequence.getAlphabet());
254 }
255 
256 /******************************************************************************/
257 
258 Sequence* VectorSequenceContainer::removeSequence(size_t sequenceIndex) throw (IndexOutOfBoundsException)
259 {
260  // Copy sequence:
261  if (sequenceIndex >= sequences_.size())
262  throw IndexOutOfBoundsException("VectorSequenceContainer::removeSequence.", sequenceIndex, 0, sequences_.size() - 1);
263  Sequence* old = sequences_[sequenceIndex];
264  // Remove pointer toward old sequence:
265  sequences_.erase(sequences_.begin() + static_cast<ptrdiff_t>(sequenceIndex));
266  // Send copy:
267  return old;
268 }
269 
270 /******************************************************************************/
271 
272 void VectorSequenceContainer::deleteSequence(size_t sequenceIndex) throw (IndexOutOfBoundsException)
273 {
274  // Delete sequence
275  if (sequenceIndex >= sequences_.size())
276  throw IndexOutOfBoundsException("VectorSequenceContainer::deleteSequence.", sequenceIndex, 0, sequences_.size() - 1);
277  delete sequences_[sequenceIndex];
278  // Remove pointer toward old sequence:
279  sequences_.erase(sequences_.begin() + static_cast<ptrdiff_t>(sequenceIndex));
280 }
281 
282 /******************************************************************************/
283 
284 void VectorSequenceContainer::addSequence(const Sequence& sequence, bool checkName) throw (Exception)
285 {
286  // Sequence's name existence checking
287  if (checkName)
288  {
289  // For all names in vector : throw exception if name already exists
290  for (size_t i = 0; i < sequences_.size(); i++)
291  {
292  if (sequences_[i]->getName() == sequence.getName())
293  throw Exception("VectorSequenceContainer::addSequence : Sequence '" + sequence.getName() + "' already exists in container");
294  }
295  }
296 
297  // New sequence's alphabet and sequence container's alphabet matching verification
298  if (sequence.getAlphabet()->getAlphabetType() == getAlphabet()->getAlphabetType())
299  {
300  // push_back(new Sequence(sequence.getName(), sequence.getContent(), alphabet));
301  sequences_.push_back(dynamic_cast<Sequence*>(sequence.clone()));
302  }
303  else
304  throw AlphabetMismatchException("VectorSequenceContainer::addSequence : Alphabets don't match", getAlphabet(), sequence.getAlphabet());
305 }
306 
307 void VectorSequenceContainer::addSequence(const Sequence& sequence, size_t sequenceIndex, bool checkName) throw (Exception)
308 {
309  // Sequence's name existence checking
310  if (checkName)
311  {
312  // For all names in vector : throw exception if name already exists
313  for (size_t i = 0; i < sequences_.size(); i++)
314  {
315  if (sequences_[i]->getName() == sequence.getName())
316  throw Exception("VectorSequenceContainer::addSequence : Sequence '" + sequence.getName() + "' already exists in container");
317  }
318  }
319 
320  // New sequence's alphabet and sequence container's alphabet matching verification
321  if (sequence.getAlphabet()->getAlphabetType() == getAlphabet()->getAlphabetType())
322  {
323  // insert(begin() + pos, new Sequence(sequence.getName(), sequence.getContent(), alphabet));
324  sequences_.insert(sequences_.begin() + static_cast<ptrdiff_t>(sequenceIndex), dynamic_cast<Sequence*>(sequence.clone()));
325  }
326  else
327  throw AlphabetMismatchException("VectorSequenceContainer::addSequence : Alphabets don't match", getAlphabet(), sequence.getAlphabet());
328 }
329 
330 /******************************************************************************/
331 
332 std::vector<std::string> VectorSequenceContainer::getSequencesNames() const
333 {
334  std::vector<std::string> names;
335  for (size_t i = 0; i < sequences_.size(); i++)
336  {
337  names.push_back(sequences_[i]->getName());
338  }
339  return names;
340 }
341 
342 /******************************************************************************/
343 
345  const std::vector<std::string>& names,
346  bool checkNames)
347 throw (Exception)
348 {
349  if (names.size() != getNumberOfSequences())
350  throw IndexOutOfBoundsException("VectorSequenceContainer::setSequenceNames : bad number of names", names.size(), getNumberOfSequences(), getNumberOfSequences());
351  if (checkNames)
352  {
353  for (size_t i = 0; i < names.size(); i++)
354  {
355  // For all names in vector : throw exception if name already exists
356  for (size_t j = 0; j < i; j++)
357  {
358  if (names[j] == names[i])
359  throw Exception("VectorSiteContainer::setSequencesNames : Sequence's name already exists in container");
360  }
361  }
362  }
363  for (size_t i = 0; i < names.size(); i++)
364  {
365  sequences_[i]->setName(names[i]);
366  }
367 }
368 
369 /******************************************************************************/
370 
372 {
373  // Delete sequences
374  for (size_t i = 0; i < sequences_.size(); i++)
375  {
376  delete sequences_[i];
377  }
378  // Delete all sequence pointers
379  sequences_.clear();
380 }
381 
382 /******************************************************************************/
383 
384 void VectorSequenceContainer::setComments(size_t sequenceIndex, const Comments& comments) throw (IndexOutOfBoundsException)
385 {
386  sequences_[sequenceIndex]->setComments(comments);
387 }
388 
389 /******************************************************************************/
390 
392 {
395  return vsc;
396 }
397 
398 /******************************************************************************/
399 
size_t getNumberOfSequences() const
Get the number of sequences in the container.
Exception thrown when a sequence is not found The sequence not found exception base class...
bool hasSequence(const std::string &name) const
Check if a sequence with a given name is present in the container.
VectorSequenceContainer & operator=(const VectorSequenceContainer &vsc)
Assign from a VectorSequenceContainer.
void setComments(const std::string &name, const Comments &comments)
Set the comments of a particular sequence.
std::vector< std::string > Comments
Declaration of Comments type.
Definition: Sequence.h:60
void deleteSequence(const std::string &name)
Delete a sequence of the container.
The OrderedSequenceContainer interface.
This alphabet is used to deal NumericAlphabet.
const Alphabet * getAlphabet() const
Get sequence container&#39;s alphabet.
std::vector< Sequence * > sequences_
A std::vector of pointers toward the sequences stored in the container.
The VectorSequenceContainer class.
The Alphabet interface.
Definition: Alphabet.h:130
STL namespace.
AbstractSequenceContainer & operator=(const AbstractSequenceContainer &sc)
Partial implementation of the OrderedSequenceContainer interface.
const Comments & getGeneralComments() const
Get the comments of this container.
virtual void addSequence(const Sequence &sequence, bool checkName=true)
Add a sequence at the end of the container.
virtual const Sequence & getSequence(size_t sequenceIndex) const =0
Retrieve a sequence object from the container.
void setSequence(const std::string &name, const Sequence &sequence, bool checkName=true)
Replace a sequence in the container.
virtual const Comments & getGeneralComments() const =0
Get the comments of this container.
void setSequencesNames(const std::vector< std::string > &names, bool checkNames=true)
Set all sequence names.
const Sequence & getSequence(const std::string &name) const
Retrieve a sequence object from the container.
Sequence * removeSequence(const std::string &name)
Extract (and remove) a sequence from the container.
virtual const Sequence & getSequence(const std::string &name) const =0
Retrieve a sequence object from the container.
virtual const std::string & getName(size_t sequenceIndex) const
Get the name of a particular sequence.
size_t getSequencePosition(const std::string &name) const
Get the position of a sequence in sequence container from its name.
virtual std::vector< std::string > getSequencesNames() const =0
Get all the names of the sequences in the container.
The sequence interface.
Definition: Sequence.h:74
void clear()
Delete all sequences in the container.
std::vector< std::string > getSequencesNames() const
Get all the names of the sequences in the container.
Exception thrown when two alphabets do not match.
VectorSequenceContainer(const std::vector< const Sequence *> &vs, const Alphabet *alpha)
Build a new container from a std::vector of pointers toward sequence objects.
VectorSequenceContainer * createEmptyContainer() const
Return a copy of this container, but with no sequence inside.
The SequenceContainer interface.
void setGeneralComments(const Comments &comments)
Set the comments of this container.
virtual size_t getNumberOfSequences() const =0
Get the number of sequences in the container.