bpp-popgen  2.2.0
PolymorphismSequenceContainer.cpp
Go to the documentation of this file.
1 //
2 // File: PolymorphismSequenceContainer.h
3 // Created by: Eric Bazin
4 // Sylvain Gaillard
5 // Created on: Wednesday August 04 2004
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11 
12  This software is a computer program whose purpose is to provide classes
13  for population genetics analysis.
14 
15  This software is governed by the CeCILL license under French law and
16  abiding by the rules of distribution of free software. You can use,
17  modify and/ or redistribute the software under the terms of the CeCILL
18  license as circulated by CEA, CNRS and INRIA at the following URL
19  "http://www.cecill.info".
20 
21  As a counterpart to the access to the source code and rights to copy,
22  modify and redistribute granted by the license, users are provided only
23  with a limited warranty and the software's author, the holder of the
24  economic rights, and the successive licensors have only limited
25  liability.
26 
27  In this respect, the user's attention is drawn to the risks associated
28  with loading, using, modifying and/or developing or reproducing the
29  software by the user in light of its specific status of free software,
30  that may mean that it is complicated to manipulate, and that also
31  therefore means that it is reserved for developers and experienced
32  professionals having in-depth computer knowledge. Users are therefore
33  encouraged to load and test the software's suitability as regards their
34  requirements in conditions enabling the security of their systems and/or
35  data to be ensured and, more generally, to use and operate it in the
36  same conditions as regards security.
37 
38  The fact that you are presently reading this means that you have had
39  knowledge of the CeCILL license and that you accept its terms.
40  */
41 
43 
44 using namespace bpp;
45 using namespace std;
46 
47 /******************************************************************************/
48 
50  VectorSiteContainer(alpha),
51  ingroup_(vector<bool>()),
52  count_(0),
53  group_(0) {}
54 
55 /******************************************************************************/
56 
58  VectorSiteContainer(size, alpha),
59  ingroup_(size),
60  count_(size),
61  group_(size) {}
62 
63 /******************************************************************************/
64 
66  VectorSiteContainer(sc),
67  ingroup_(sc.getNumberOfSequences(), true),
68  count_(sc.getNumberOfSequences(), 1),
69  group_(sc.getNumberOfSequences(), 1) {}
70 
71 /******************************************************************************/
72 
74  VectorSiteContainer(sc),
75  ingroup_(sc.getNumberOfSequences(), true),
76  count_(sc.getNumberOfSequences(), 1),
77  group_(sc.getNumberOfSequences(), 1) {}
78 
79 /******************************************************************************/
80 
82  VectorSiteContainer(psc),
83  ingroup_(psc.getNumberOfSequences()),
84  count_(psc.getNumberOfSequences()),
85  group_(psc.getNumberOfSequences())
86 {
87  for (size_t i = 0; i < psc.getNumberOfSequences(); i++)
88  {
89  count_[i] = psc.getSequenceCount(i);
90  ingroup_[i] = psc.isIngroupMember(i);
91  group_[i] = psc.getGroupId(i);
92  }
93 }
94 
95 /******************************************************************************/
96 
98 {
99  VectorSiteContainer::operator=(psc);
100  // Setting up the sequences comments, numbers and ingroup state
101  size_t nbSeq = psc.getNumberOfSequences();
102  count_.resize(nbSeq);
103  ingroup_.resize(nbSeq);
104  group_.resize(nbSeq);
105  for (size_t i = 0; i < nbSeq; i++)
106  {
107  count_[i] = psc.getSequenceCount(i);
108  ingroup_[i] = psc.isIngroupMember(i);
109  group_[i] = psc.getGroupId(i);
110  }
111  return *this;
112 }
113 
114 /******************************************************************************/
115 
116 // ** Class destructor: *******************************************************/
117 
119 {
120  clear();
121 }
122 
123 /******************************************************************************/
124 
125 // ** Other methodes: *********************************************************/
126 
127 Sequence* PolymorphismSequenceContainer::removeSequence(size_t index) throw (IndexOutOfBoundsException)
128 {
129  if (index >= getNumberOfSequences())
130  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::removeSequence: index out of bounds.", index, 0, getNumberOfSequences());
131  count_.erase(count_.begin() + static_cast<ptrdiff_t>(index));
132  ingroup_.erase(ingroup_.begin() + static_cast<ptrdiff_t>(index));
133  group_.erase(group_.begin() + static_cast<ptrdiff_t>(index));
134  return VectorSiteContainer::removeSequence(index);
135 }
136 
137 /******************************************************************************/
138 
139 Sequence* PolymorphismSequenceContainer::removeSequence(const std::string& name) throw (SequenceNotFoundException)
140 {
141  try
142  {
143  return removeSequence(getSequencePosition(name));
144  }
145  catch (SequenceNotFoundException& snfe)
146  {
147  throw SequenceNotFoundException("PolymorphismSequenceContainer::removeSequence.", name);
148  }
149 }
150 
151 /******************************************************************************/
152 
153 void PolymorphismSequenceContainer::deleteSequence(size_t index) throw (IndexOutOfBoundsException)
154 {
155  try
156  {
157  delete removeSequence(index);
158  }
159  catch (IndexOutOfBoundsException& ioobe)
160  {
161  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::deleteSequence.", index, 0, getNumberOfSequences());
162  }
163 }
164 
165 /******************************************************************************/
166 
167 void PolymorphismSequenceContainer::deleteSequence(const std::string& name) throw (SequenceNotFoundException)
168 {
169  try
170  {
171  delete removeSequence(name);
172  }
173  catch (SequenceNotFoundException& snfe)
174  {
175  throw SequenceNotFoundException("PolymorphismSequenceContainer::deleteSequence.", name);
176  }
177 }
178 
179 /******************************************************************************/
180 
181 void PolymorphismSequenceContainer::addSequenceWithFrequency(const Sequence& sequence, unsigned int frequency, bool checkName) throw (Exception)
182 {
183  try
184  {
185  VectorSiteContainer::addSequence(sequence, checkName);
186  }
187  catch (Exception& e)
188  {
189  throw e;
190  }
191  count_.push_back(frequency);
192  ingroup_.push_back(true);
193  group_.push_back(0);
194 }
195 
196 /******************************************************************************/
197 
198 void PolymorphismSequenceContainer::addSequenceWithFrequency(const Sequence& sequence, size_t sequenceIndex, unsigned int frequency, bool checkName) throw (Exception)
199 {
200  try
201  {
202  VectorSiteContainer::addSequence(sequence, sequenceIndex, checkName);
203  }
204  catch (Exception& e)
205  {
206  throw e;
207  }
208  count_.insert(count_.begin() + static_cast<ptrdiff_t>(sequenceIndex), frequency);
209  ingroup_.insert(ingroup_.begin() + static_cast<ptrdiff_t>(sequenceIndex), true);
210  group_.insert(group_.begin() + static_cast<ptrdiff_t>(sequenceIndex), 0);
211 }
212 
213 /******************************************************************************/
214 
216 {
217  VectorSiteContainer::clear();
218  count_.clear();
219  ingroup_.clear();
220  group_.clear();
221 }
222 
223 /******************************************************************************/
224 
225 size_t PolymorphismSequenceContainer::getGroupId(size_t index) const throw (IndexOutOfBoundsException)
226 {
227  if (index >= getNumberOfSequences())
228  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::getGroupId: index out of bounds.", index, 0, getNumberOfSequences());
229  return group_[index];
230 }
231 
232 /******************************************************************************/
233 
234 size_t PolymorphismSequenceContainer::getGroupId(const std::string& name) const throw (SequenceNotFoundException)
235 {
236  try
237  {
238  return group_[getSequencePosition(name)];
239  }
240  catch (SequenceNotFoundException& snfe)
241  {
242  throw SequenceNotFoundException("PolymorphismSequenceContainer::getGroupId.", name);
243  }
244 }
245 
246 /******************************************************************************/
247 
249 {
250  set<size_t> grp_ids;
251  for (size_t i = 0; i < group_.size(); i++)
252  {
253  grp_ids.insert(group_[i]);
254  }
255  return grp_ids;
256 }
257 
258 /******************************************************************************/
259 
260 void PolymorphismSequenceContainer::setGroupId(size_t index, size_t group_id) throw (IndexOutOfBoundsException)
261 {
262  if (index >= getNumberOfSequences())
263  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::setGroupId: index out of bounds.", index, 0, getNumberOfSequences());
264  group_[index] = group_id;
265 }
266 
267 /******************************************************************************/
268 
269 void PolymorphismSequenceContainer::setGroupId(const std::string& name, size_t group_id) throw (SequenceNotFoundException)
270 {
271  try
272  {
273  group_[getSequencePosition(name)] = group_id;
274  }
275  catch (SequenceNotFoundException& snfe)
276  {
277  throw SequenceNotFoundException("PolymorphismSequenceContainer::setGroupId.", name);
278  }
279 }
280 
281 /******************************************************************************/
282 
284 {
285  return getAllGroupsIds().size();
286 }
287 
288 /******************************************************************************/
289 
290 bool PolymorphismSequenceContainer::isIngroupMember(size_t index) const throw (IndexOutOfBoundsException)
291 {
292  if (index >= getNumberOfSequences())
293  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::isIngroupMember: index out of bounds.", index, 0, getNumberOfSequences());
294  return ingroup_[index];
295 }
296 
297 /******************************************************************************/
298 
299 bool PolymorphismSequenceContainer::isIngroupMember(const std::string& name) const throw (SequenceNotFoundException)
300 {
301  try
302  {
303  return ingroup_[getSequencePosition(name)];
304  }
305  catch (SequenceNotFoundException& snfe)
306  {
307  throw SequenceNotFoundException("PolymorphismSequenceContainer::isIngroupMember.", name);
308  }
309 }
310 
311 /******************************************************************************/
312 
313 void PolymorphismSequenceContainer::setAsIngroupMember(size_t index) throw (IndexOutOfBoundsException)
314 {
315  if (index >= getNumberOfSequences())
316  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::setAsIngroupMember.", index, 0, getNumberOfSequences());
317  ingroup_[index] = true;
318 }
319 
320 /******************************************************************************/
321 
322 void PolymorphismSequenceContainer::setAsIngroupMember(const std::string& name) throw (SequenceNotFoundException)
323 {
324  try
325  {
326  size_t seqPos = getSequencePosition(name);
327  ingroup_[seqPos] = true;
328  }
329  catch (SequenceNotFoundException& snfe)
330  {
331  throw SequenceNotFoundException("PolymorphismSequenceContainer::setAsIngroupMember.", name);
332  }
333 }
334 
335 /******************************************************************************/
336 
337 void PolymorphismSequenceContainer::setAsOutgroupMember(size_t index) throw (IndexOutOfBoundsException)
338 {
339  if (index >= getNumberOfSequences())
340  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::setAsOutgroupMember.", index, 0, getNumberOfSequences());
341  ingroup_[index] = false;
342 }
343 
344 /******************************************************************************/
345 
346 void PolymorphismSequenceContainer::setAsOutgroupMember(const std::string& name) throw (SequenceNotFoundException)
347 {
348  try
349  {
350  size_t seqPos = getSequencePosition(name);
351  ingroup_[seqPos] = false;
352  }
353  catch (SequenceNotFoundException& snfe)
354  {
355  throw SequenceNotFoundException("PolymorphismSequenceContainer::setAsOutgroupMember.", name);
356  }
357 }
358 
359 /******************************************************************************/
360 
361 void PolymorphismSequenceContainer::setSequenceCount(size_t index, unsigned int count) throw (Exception)
362 {
363  if (index >= getNumberOfSequences())
364  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::setSequenceCount.", index, 0, getNumberOfSequences());
365  if (count < 1)
366  throw BadIntegerException("PolymorphismSequenceContainer::setSequenceCount: count can't be < 1.", static_cast<int>(count));
367  count_[index] = count;
368 }
369 
370 /******************************************************************************/
371 
372 void PolymorphismSequenceContainer::setSequenceCount(const std::string& name, unsigned int count) throw (Exception)
373 {
374  try
375  {
376  setSequenceCount(getSequencePosition(name), count);
377  }
378  catch (BadIntegerException& bie)
379  {
380  throw bie;
381  }
382  catch (SequenceNotFoundException& snfe)
383  {
384  throw SequenceNotFoundException("PolymorphismSequenceContainer::setSequenceCount.", name);
385  }
386 }
387 
388 /******************************************************************************/
389 
390 void PolymorphismSequenceContainer::incrementSequenceCount(size_t index) throw (IndexOutOfBoundsException)
391 {
392  if (index >= getNumberOfSequences())
393  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::incrementSequenceCount.", index, 0, getNumberOfSequences());
394  count_[index]++;
395 }
396 
397 /******************************************************************************/
398 
399 void PolymorphismSequenceContainer::incrementSequenceCount(const std::string& name) throw (SequenceNotFoundException)
400 {
401  try
402  {
403  incrementSequenceCount(getSequencePosition(name));
404  }
405  catch (SequenceNotFoundException& snfe)
406  {
407  throw SequenceNotFoundException("PolymorphismSequenceContainer::incrementSequenceCount.", name);
408  }
409 }
410 
411 /******************************************************************************/
412 
413 void PolymorphismSequenceContainer::decrementSequenceCount(size_t index) throw (IndexOutOfBoundsException, BadIntegerException)
414 {
415  if (index >= getNumberOfSequences())
416  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::decrementSequenceCount.", index, 0, getNumberOfSequences());
417  if (count_[index] - 1 < 1)
418  throw BadIntegerException("PolymorphismSequenceContainer::decrementSequenceCount: count can't be < 1.", static_cast<int>(count_[index] - 1));
419  count_[index]--;
420 }
421 
422 /******************************************************************************/
423 
424 void PolymorphismSequenceContainer::decrementSequenceCount(const std::string& name) throw (SequenceNotFoundException, BadIntegerException)
425 {
426  try
427  {
428  decrementSequenceCount(getSequencePosition(name));
429  }
430  catch (BadIntegerException& bie)
431  {
432  throw bie;
433  }
434  catch (SequenceNotFoundException& snfe)
435  {
436  throw SequenceNotFoundException("PolymorphismSequenceContainer::decrementSequenceCount.", name);
437  }
438 }
439 
440 /******************************************************************************/
441 
442 unsigned int PolymorphismSequenceContainer::getSequenceCount(size_t index) const throw (IndexOutOfBoundsException)
443 {
444  if (index >= getNumberOfSequences())
445  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::getSequenceCount.", index, 0, getNumberOfSequences());
446  return count_[index];
447 }
448 
449 /******************************************************************************/
450 
451 unsigned int PolymorphismSequenceContainer::getSequenceCount(const std::string& name) const throw (SequenceNotFoundException)
452 {
453  try
454  {
455  return getSequenceCount(getSequencePosition(name));
456  }
457  catch (SequenceNotFoundException& snfe)
458  {
459  throw SequenceNotFoundException("PolymorphismSequenceContainer::getSequenceCount.", name);
460  }
461 }
462 
463 /******************************************************************************/
464 
void setAsOutgroupMember(size_t index)
Set a sequence as outgroup member by index.
void clear()
Clear the container of all its sequences.
STL namespace.
void incrementSequenceCount(size_t index)
Add 1 to the sequence count.
PolymorphismSequenceContainer(const Alphabet *alpha)
Build a new empty PolymorphismSequenceContainer.
size_t getGroupId(size_t index) const
Get the group identifier of the sequence.
bool isIngroupMember(size_t index) const
Tell if the sequence is ingroup by index.
PolymorphismSequenceContainer & operator=(const PolymorphismSequenceContainer &psc)
Operator= : copy operator.
unsigned int getSequenceCount(size_t index) const
Get the count of a sequence by index.
void addSequenceWithFrequency(const Sequence &sequence, unsigned int frequency, bool checkName=true)
Add a sequence to the container.
void setGroupId(size_t index, size_t group_id)
Set the group identifier of a sequence.
size_t getNumberOfGroups() const
Get the number of groups.
void deleteSequence(size_t index)
Delete a sequence by index.
virtual ~PolymorphismSequenceContainer()
Destroy a PolymorphismSequenceContainer.
void decrementSequenceCount(size_t index)
Removz 1 to the sequence count.
void setAsIngroupMember(size_t index)
Set a sequence as ingroup member by index.
Sequence * removeSequence(size_t index)
Remove a sequence by index and return a pointer to this removed sequence.
void setSequenceCount(size_t index, unsigned int count)
Set the count of a sequence by index.
The PolymorphismSequenceContainer class.
std::set< size_t > getAllGroupsIds() const
Get all the groups identifiers.