bpp-popgen  3.0.0
DataSet.cpp
Go to the documentation of this file.
1 //
2 // File DataSet.cpp
3 // Author : Sylvain Gaillard
4 // Khalid Belkhir
5 // Last modification : November 10, 2008
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 population genetics 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 
41 #include "DataSet.h"
42 
43 using namespace bpp;
44 using namespace std;
45 
46 // ** Class constructor: *******************************************************/
47 
48 DataSet::DataSet() : analyzedLoci_(0),
49  analyzedSequences_(0),
50  localities_(vector<Locality<double>*>()),
51  groups_(vector<Group*>()) {}
52 
53 /******************************************************************************/
54 
55 DataSet::DataSet(const DataSet& ds) : analyzedLoci_(0),
56  analyzedSequences_(0),
57  localities_(vector<Locality<double>*>()),
58  groups_(vector<Group*>())
59 {
60  if (ds.analyzedLoci_ != 0)
62  if (ds.analyzedSequences_ != 0)
64  if (ds.localities_.size() != 0)
65  for (size_t i = 0; i < ds.localities_.size(); i++)
66  {
67  localities_.push_back(new Locality<double>(*(ds.localities_[i])));
68  }
69  if (ds.groups_.size() != 0)
70  for (size_t i = 0; i < ds.groups_.size(); i++)
71  {
72  groups_.push_back(new Group(*(ds.groups_[i])));
73  }
74 }
75 
76 /******************************************************************************/
77 
79 {
80  if (ds.analyzedLoci_ != 0)
82  if (ds.analyzedSequences_ != 0)
84  if (ds.localities_.size() != 0)
85  for (size_t i = 0; i < ds.localities_.size(); i++)
86  {
87  localities_.push_back(new Locality<double>(*(ds.localities_[i])));
88  }
89  if (ds.groups_.size() != 0)
90  for (size_t i = 0; i < ds.groups_.size(); i++)
91  {
92  groups_.push_back(new Group(*(ds.groups_[i])));
93  }
94  return *this;
95 }
96 
97 // ** Class destructor: *******************************************************/
99 {
100  if (getNumberOfGroups() > 0)
101  for (size_t i = 0; i < getNumberOfGroups(); i++)
102  {
103  delete groups_[i];
104  }
105  if (analyzedLoci_ != 0)
106  delete analyzedLoci_;
107  if (getNumberOfLocalities() > 0)
108  for (size_t i = 0; i < getNumberOfLocalities(); i++)
109  {
110  delete localities_[i];
111  }
112  if (analyzedSequences_ != 0)
113  delete analyzedSequences_;
114 }
115 
116 // ** Other methodes: *********************************************************/
117 
118 // Dealing with Localities ---------------------------------
120 {
121  for (size_t i = 0; i < localities_.size(); i++)
122  {
123  if (localities_[i]->getName() == locality.getName())
124  throw BadIdentifierException("DataSet::addLocality: locality name already in use.", locality.getName());
125  }
126  localities_.push_back(new Locality<double>(locality));
127 }
128 
129 /******************************************************************************/
130 
131 size_t DataSet::getLocalityPosition(const std::string& name) const
132 {
133  for (size_t i = 0; i < localities_.size(); i++)
134  {
135  if (localities_[i]->getName() == name)
136  return i;
137  }
138  throw LocalityNotFoundException("DataSet::getLocalityPosition: Locality not found.", name);
139 }
140 
141 /******************************************************************************/
142 
143 const Locality<double>& DataSet::getLocalityAtPosition(size_t locality_position) const
144 {
145  if (locality_position >= localities_.size())
146  throw IndexOutOfBoundsException("DataSet::getLocalityAtPosition: locality_position out of bounds.", locality_position, 0, localities_.size());
147  return *(localities_[locality_position]);
148 }
149 
150 /******************************************************************************/
151 
152 const Locality<double>& DataSet::getLocalityByName(const std::string& name) const
153 {
154  try
155  {
157  }
158  catch (LocalityNotFoundException& lnfe)
159  {
160  throw LocalityNotFoundException("DataSet::getLocalityByName: Locality not found.", name);
161  }
162 }
163 
164 /******************************************************************************/
165 
166 void DataSet::deleteLocalityAtPosition(size_t locality_position)
167 {
168  if (locality_position >= localities_.size())
169  throw IndexOutOfBoundsException("DataSet::deleteLocalityAtPosition: locality_position out of bounds.", locality_position, 0, localities_.size());
170  delete localities_[locality_position];
171  localities_.erase(localities_.begin() + static_cast<ptrdiff_t>(locality_position));
172 }
173 
174 /******************************************************************************/
175 
176 void DataSet::deleteLocalityByName(const std::string& name)
177 {
178  try
179  {
181  }
182  catch (LocalityNotFoundException& lnfe)
183  {
184  throw LocalityNotFoundException("DataSet::deleteLocalityByName: Locality not found.", name);
185  }
186 }
187 
188 /******************************************************************************/
189 
191 {
192  return localities_.size();
193 }
194 
195 /******************************************************************************/
196 
198 {
199  return getNumberOfLocalities() > 0;
200 }
201 
202 /******************************************************************************/
203 
204 // Dealing with groups -------------------------------------
205 void DataSet::addGroup(const Group& group)
206 {
207  for (size_t i = 0; i < groups_.size(); i++)
208  {
209  if (group.getGroupId() == groups_[i]->getGroupId())
210  throw BadIdentifierException("DataSet::addGroup: group id already in use.", group.getGroupId());
211  }
212  groups_.push_back(new Group(group));
213 }
214 
215 /******************************************************************************/
216 
217 void DataSet::addEmptyGroup(size_t group_id)
218 {
219  for (size_t i = 0; i < groups_.size(); i++)
220  {
221  if (group_id == groups_[i]->getGroupId())
222  throw BadIdentifierException("DataSet::addEmptyGroup: group_id already in use.", group_id);
223  }
224  groups_.push_back(new Group(group_id));
225 }
226 
227 /******************************************************************************/
228 
229 const Group& DataSet::getGroupById(size_t group_id) const
230 {
231  for (size_t i = 0; i < groups_.size(); i++)
232  {
233  if (group_id == groups_[i]->getGroupId())
234  return *(groups_[i]);
235  }
236  throw GroupNotFoundException("DataSet::getGroupById: group_id not found.", group_id);
237 }
238 
239 /******************************************************************************/
240 
241 string DataSet::getGroupName(size_t group_id) const
242 {
243  string name;
244  name = getGroupById(group_id).getGroupName();
245  if (!name.empty() )
246  return name;
247  else
248  return TextTools::toString(group_id);
249  throw GroupNotFoundException("DataSet::getGroupName: group_id not found.", group_id);
250 }
251 
252 /******************************************************************************/
253 
254 void DataSet::setGroupName(size_t group_id, const std::string& group_name) const
255 {
256  for (size_t i = 0; i < groups_.size(); i++)
257  {
258  if (group_id == groups_[i]->getGroupId())
259  {
260  groups_[i]->setGroupName(group_name);
261  return;
262  }
263  }
264  throw GroupNotFoundException("DataSet::setGroupName: group_id not found.", group_id);
265 }
266 
267 /******************************************************************************/
268 
269 size_t DataSet::getGroupPosition(size_t group_id) const
270 {
271  for (size_t i = 0; i < groups_.size(); i++)
272  {
273  if (group_id == groups_[i]->getGroupId())
274  return i;
275  }
276  throw GroupNotFoundException("DataSet::getGroupPosition: group_id not found.", group_id);
277 }
278 
279 /******************************************************************************/
280 
281 const Group& DataSet::getGroupAtPosition(size_t group_position) const
282 {
283  if (group_position >= groups_.size())
284  throw IndexOutOfBoundsException("DataSet::getGroup.", group_position, 0, groups_.size());
285  return *(groups_[group_position]);
286 }
287 
288 /******************************************************************************/
289 
290 void DataSet::deleteGroupAtPosition(size_t group_position)
291 {
292  if (group_position >= groups_.size())
293  throw IndexOutOfBoundsException("DataSet::deleteGroup.", group_position, 0, groups_.size());
294  delete groups_[group_position];
295  groups_.erase(groups_.begin() + static_cast<ptrdiff_t>(group_position));
296 }
297 
298 /******************************************************************************/
299 
301 {
302  return groups_.size();
303 }
304 
305 /******************************************************************************/
306 
307 void DataSet::mergeTwoGroups(size_t source_id, size_t target_id)
308 {
309  // Test the existance of the two groups.
310  try
311  {
312  getGroupById(source_id);
313  }
314  catch (GroupNotFoundException& e)
315  {
316  throw GroupNotFoundException("DataSet::mergeTwoGroups: source_id not found.", source_id);
317  }
318  try
319  {
320  getGroupById(target_id);
321  }
322  catch (GroupNotFoundException& e)
323  {
324  throw GroupNotFoundException("DataSet::mergeTwoGroups: target_id not found.", target_id);
325  }
326  // Emptie the source into the target
327  size_t source_pos = getGroupPosition(source_id);
328  size_t target_pos = getGroupPosition(target_id);
329  for (size_t i = 0; i < groups_[source_pos]->getNumberOfIndividuals(); i++)
330  {
331  groups_[target_pos]->addIndividual(groups_[source_pos]->getIndividualAtPosition(i));
332  }
333  deleteGroupAtPosition(source_pos);
334 }
335 
336 /******************************************************************************/
337 
338 void DataSet::mergeGroups(std::vector<size_t>& group_ids)
339 {
340  // Test if all group id exists in the DataSet
341  for (size_t i = 0; i < group_ids.size(); i++)
342  {
343  try
344  {
345  getGroupById(group_ids[i]);
346  }
347  catch (GroupNotFoundException& e)
348  {
349  throw GroupNotFoundException("DataSet::mergeGroups: group not found.", group_ids[i]);
350  }
351  }
352  // Sort the group id
353  sort(group_ids.begin(), group_ids.end());
354  // Merge all the groups in the first
355  size_t pos_first = getGroupPosition(group_ids[0]);
356  for (size_t i = 1; i < group_ids.size(); i++)
357  {
358  size_t pos_current = getGroupPosition(group_ids[i]);
359  for (size_t j = 0; j < getGroupAtPosition(pos_current).getNumberOfIndividuals(); j++)
360  {
361  groups_[pos_first]->addIndividual(getGroupAtPosition(pos_current).getIndividualAtPosition(j));
362  }
363  deleteGroupAtPosition(pos_current);
364  }
365 }
366 
367 /******************************************************************************/
368 
369 void DataSet::splitGroup(size_t group_id, std::vector<size_t> individuals_selection)
370 {
371  size_t source_pos;
372  try
373  {
374  source_pos = getGroupPosition(group_id);
375  }
376  catch (GroupNotFoundException& gnfe)
377  {
378  throw GroupNotFoundException("DataSet::splitGroup: group_id not found.", gnfe.getIdentifier());
379  }
380  size_t new_group_id = 0;
381  for (size_t i = 0; i < groups_.size(); i++)
382  {
383  if (groups_[i]->getGroupId() > new_group_id)
384  new_group_id = groups_[i]->getGroupId();
385  }
386  new_group_id++;
387  Group new_group(new_group_id);
388  for (size_t i = 0; i < individuals_selection.size(); i++)
389  {
390  if (individuals_selection[i] >= groups_[source_pos]->getNumberOfIndividuals())
391  throw IndexOutOfBoundsException("DataSet::splitGroup: individuals_selection excedes the number of individual in the group.", individuals_selection[i], 0, groups_[source_pos]->getNumberOfIndividuals());
392  }
393  for (size_t i = 0; i < individuals_selection.size(); i++)
394  {
395  new_group.addIndividual(*groups_[source_pos]->removeIndividualAtPosition(individuals_selection[i]));
396  groups_[source_pos]->deleteIndividualAtPosition(individuals_selection[i]);
397  }
398  addGroup(new_group);
399 }
400 
401 /******************************************************************************/
402 
403 // Dealing with individuals -------------------------------
404 
405 void DataSet::addIndividualToGroup(size_t group, const Individual& individual)
406 {
407  if (group >= getNumberOfGroups())
408  throw IndexOutOfBoundsException("DataSet::addIndividualToGroup: group out of bounds.", group, 0, getNumberOfGroups());
409  try
410  {
411  groups_[group]->addIndividual(individual);
412  if (individual.hasSequences())
413  setAlphabet(individual.getSequenceAlphabet());
414  }
415  catch (BadIdentifierException& bie)
416  {
417  throw BadIdentifierException("DataSet::addIndividualToGroup: individual's id already in use in this group.", bie.getIdentifier());
418  }
419 }
420 
421 /******************************************************************************/
422 
423 void DataSet::addEmptyIndividualToGroup(size_t group, const std::string& individual_id)
424 {
425  if (group >= getNumberOfGroups())
426  throw IndexOutOfBoundsException("DataSet::addEmptyIndividual: group out of bounds.", group, 0, getNumberOfGroups());
427  try
428  {
429  groups_[group]->addEmptyIndividual(individual_id);
430  }
431  catch (BadIdentifierException& bie)
432  {
433  throw BadIdentifierException("DataSet::addEmptyIndividual: individual_id already in use.", bie.getIdentifier());
434  }
435 }
436 
437 /******************************************************************************/
438 
439 size_t DataSet::getNumberOfIndividualsInGroup(size_t group_position) const
440 {
441  if (group_position >= getNumberOfGroups())
442  throw IndexOutOfBoundsException("DataSet::getNumberOfIndividualsInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
443  return groups_[group_position]->getNumberOfIndividuals();
444 }
445 
446 /******************************************************************************/
447 
448 size_t DataSet::getIndividualPositionInGroup(size_t group_position, const std::string& individual_id) const
449 {
450  if (group_position >= getNumberOfGroups())
451  throw IndexOutOfBoundsException("DataSet::getIndividualPositionFromGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
452  try
453  {
454  return groups_[group_position]->getIndividualPosition(individual_id);
455  }
456  catch (IndividualNotFoundException& infe)
457  {
458  throw IndividualNotFoundException("DataSet::getIndividualPositionFromGroup: individual_id not found.", infe.getIdentifier());
459  }
460 }
461 
462 /******************************************************************************/
463 
464 const Individual* DataSet::getIndividualAtPositionFromGroup(size_t group_position, size_t individual_position) const
465 {
466  if (group_position >= getNumberOfGroups())
467  throw IndexOutOfBoundsException("DataSet::getIndividualAtPositionFromGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
468  try
469  {
470  return &groups_[group_position]->getIndividualAtPosition(individual_position);
471  }
472  catch (IndexOutOfBoundsException& ioobe)
473  {
474  throw IndexOutOfBoundsException("DataSet::getIndividualAtPositionFromGroup: individual_position out of bouds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
475  }
476 }
477 
478 /******************************************************************************/
479 
480 const Individual* DataSet::getIndividualByIdFromGroup(size_t group_position, const std::string& individual_id) const
481 {
482  if (group_position >= getNumberOfGroups())
483  throw IndexOutOfBoundsException("DataSet::getIndividualByIdFromGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
484  try
485  {
486  return &groups_[group_position]->getIndividualById(individual_id);
487  }
488  catch (IndividualNotFoundException& infe)
489  {
490  throw IndividualNotFoundException("DataSet::getIndividualByIdFromGroup: individual_id not found.", infe.getIdentifier());
491  }
492 }
493 
494 /******************************************************************************/
495 
496 void DataSet::deleteIndividualAtPositionFromGroup(size_t group_position, size_t individual_position)
497 {
498  if (group_position >= getNumberOfGroups())
499  throw IndexOutOfBoundsException("DataSet::deleteIndividualAtPositionFromGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
500  try
501  {
502  groups_[group_position]->deleteIndividualAtPosition(individual_position);
503  }
504  catch (IndexOutOfBoundsException& ioobe)
505  {
506  throw IndexOutOfBoundsException("DataSet::deleteIndividualAtPositionFromGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
507  }
508 }
509 
510 /******************************************************************************/
511 
512 void DataSet::deleteIndividualByIdFromGroup(size_t group_position, const std::string& individual_id)
513 {
514  if (group_position >= getNumberOfGroups())
515  throw IndexOutOfBoundsException("DataSet::deleteIndividualByIdFromGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
516  try
517  {
518  groups_[group_position]->deleteIndividualById(individual_id);
519  }
520  catch (IndividualNotFoundException& infe)
521  {
522  throw IndividualNotFoundException("DataSet::deleteIndividualByIdFromGroup: individual_id not found.", infe.getIdentifier());
523  }
524 }
525 
526 /******************************************************************************/
527 
528 void DataSet::setIndividualSexInGroup(size_t group_position, size_t individual_position, const unsigned short sex)
529 {
530  if (group_position >= getNumberOfGroups())
531  throw IndexOutOfBoundsException("DataSet::setIndividualSexInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
532  try
533  {
534  groups_[group_position]->setIndividualSexAtPosition(individual_position, sex);
535  }
536  catch (IndexOutOfBoundsException& ioobe)
537  {
538  throw IndexOutOfBoundsException("DataSet::setIndividualSexInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
539  }
540 }
541 
542 /******************************************************************************/
543 
544 unsigned short DataSet::getIndividualSexInGroup(size_t group_position, size_t individual_position) const
545 {
546  if (group_position >= getNumberOfGroups())
547  throw IndexOutOfBoundsException("DataSet::getIndividualSexInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
548  try
549  {
550  return groups_[group_position]->getIndividualSexAtPosition(individual_position);
551  }
552  catch (IndexOutOfBoundsException& ioobe)
553  {
554  throw IndexOutOfBoundsException("DataSet::getIndividualSexInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
555  }
556 }
557 
558 /******************************************************************************/
559 
560 void DataSet::setIndividualDateInGroup(size_t group_position, size_t individual_position, const Date& date)
561 {
562  if (group_position >= getNumberOfGroups())
563  throw IndexOutOfBoundsException("DataSet::setIndividualDateInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
564  try
565  {
566  groups_[group_position]->setIndividualDateAtPosition(individual_position, date);
567  }
568  catch (IndexOutOfBoundsException& ioobe)
569  {
570  throw IndexOutOfBoundsException("DataSet::setIndividualDateInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
571  }
572 }
573 
574 /******************************************************************************/
575 
576 const Date* DataSet::getIndividualDateInGroup(size_t group_position, size_t individual_position) const
577 {
578  if (group_position >= getNumberOfGroups())
579  throw IndexOutOfBoundsException("DataSet::getIndividualDateInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
580  try
581  {
582  return &groups_[group_position]->getIndividualDateAtPosition(individual_position);
583  }
584  catch (IndexOutOfBoundsException& ioobe)
585  {
586  throw IndexOutOfBoundsException("DataSet::getIndividualDateInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
587  }
588  catch (NullPointerException&)
589  {
590  throw NullPointerException("DataSet::getIndividualDateInGroup: individual has no date.");
591  }
592 }
593 
594 /******************************************************************************/
595 
596 void DataSet::setIndividualCoordInGroup(size_t group_position, size_t individual_position, const Point2D<double>& coord)
597 {
598  if (group_position >= getNumberOfGroups())
599  throw IndexOutOfBoundsException("DataSet::setIndividualCoordInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
600  try
601  {
602  groups_[group_position]->setIndividualCoordAtPosition(individual_position, coord);
603  }
604  catch (IndexOutOfBoundsException& ioobe)
605  {
606  throw IndexOutOfBoundsException("DataSet::setIndividualCoordInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
607  }
608 }
609 
610 /******************************************************************************/
611 
612 const Point2D<double>* DataSet::getIndividualCoordInGroup(size_t group_position, size_t individual_position) const
613 {
614  if (group_position >= getNumberOfGroups())
615  throw IndexOutOfBoundsException("DataSet::getIndividualCoordInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
616  try
617  {
618  return &groups_[group_position]->getIndividualCoordAtPosition(individual_position);
619  }
620  catch (IndexOutOfBoundsException& ioobe)
621  {
622  throw IndexOutOfBoundsException("DataSet::getIndividualCoordAtPosition: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
623  }
624  catch (NullPointerException&)
625  {
626  throw NullPointerException("DataSet::getIndividualCoordInGroup: individual has no coordinate.");
627  }
628 }
629 
630 /******************************************************************************/
631 
632 void DataSet::setIndividualLocalityInGroupByName(size_t group_position, size_t individual_position, const std::string& locality_name)
633 {
634  if (group_position >= getNumberOfGroups())
635  throw IndexOutOfBoundsException("DataSet::setIndividualLocalityInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
636  try
637  {
638  groups_[group_position]->setIndividualLocalityAtPosition(individual_position, &getLocalityByName(locality_name));
639  }
640  catch (IndexOutOfBoundsException& ioobe)
641  {
642  throw IndexOutOfBoundsException("DataSet::setIndividualLocalityInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
643  }
644  catch (LocalityNotFoundException& lnfe)
645  {
646  throw LocalityNotFoundException("DataSet::setIndividualLocalityInGroup: locality_name not found.", lnfe.getIdentifier());
647  }
648 }
649 
650 /******************************************************************************/
651 
652 const Locality<double>* DataSet::getIndividualLocalityInGroup(size_t group_position, size_t individual_position) const
653 {
654  if (group_position >= getNumberOfGroups())
655  throw IndexOutOfBoundsException("DataSet::getIndividualLocalityInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
656  try
657  {
658  return &groups_[group_position]->getIndividualLocalityAtPosition(individual_position);
659  }
660  catch (IndexOutOfBoundsException& ioobe)
661  {
662  throw IndexOutOfBoundsException("DataSet::getIndividualLocalityInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
663  }
664  catch (NullPointerException&)
665  {
666  throw NullPointerException("DataSet::getIndividualLocalityInGroup: individual has no locality.");
667  }
668 }
669 
670 /******************************************************************************/
671 
672 void DataSet::addIndividualSequenceInGroup(size_t group_position, size_t individual_position, size_t sequence_position, const Sequence& sequence)
673 {
674  if (group_position >= getNumberOfGroups())
675  throw IndexOutOfBoundsException("DataSet::addIndividualSequenceInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
676  try
677  {
678  groups_[group_position]->addIndividualSequenceAtPosition(individual_position, sequence_position, sequence);
679  setAlphabet(sequence.getAlphabet());
680  }
681  catch (IndexOutOfBoundsException& ioobe)
682  {
683  throw IndexOutOfBoundsException("DataSet::addIndividualSequenceInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
684  }
685  catch (AlphabetMismatchException& ame)
686  {
687  throw AlphabetMismatchException("DataSet::addIndividualSequenceInGroup: sequence's alphabet doesn't match.", ame.getAlphabets()[0], ame.getAlphabets()[1]);
688  }
689  catch (BadIdentifierException& bie)
690  {
691  throw BadIdentifierException("DataSet::addIndividualSequenceInGroup: sequence's name already in use.", bie.getIdentifier());
692  }
693  catch (BadIntegerException& bie)
694  {
695  throw BadIntegerException("DataSet::addIndividualSequenceInGroup: sequence_position already in use.", bie.getBadInteger());
696  }
697 }
698 
699 /******************************************************************************/
700 
701 const Sequence& DataSet::getIndividualSequenceByNameInGroup(size_t group_position, size_t individual_position, const std::string& sequence_name) const
702 {
703  if (group_position >= getNumberOfGroups())
704  throw IndexOutOfBoundsException("DataSet::getIndividualSequenceByNameInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
705  try
706  {
707  return groups_[group_position]->getIndividualSequenceByName(individual_position, sequence_name);
708  }
709  catch (IndexOutOfBoundsException& ioobe)
710  {
711  throw IndexOutOfBoundsException("DataSet::getIndividualSequenceByNameInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
712  }
713  catch (NullPointerException&)
714  {
715  throw NullPointerException("DataSet::getIndividualSequenceByNameInGroup: individual has no sequences.");
716  }
717  catch (SequenceNotFoundException& snfe)
718  {
719  throw SequenceNotFoundException("DataSet::getIndividualSequenceByNameInGroup: sequence_name not found.", snfe.getSequenceId());
720  }
721 }
722 
723 /******************************************************************************/
724 
725 const Sequence& DataSet::getIndividualSequenceAtPositionInGroup(size_t group_position, size_t individual_position, size_t sequence_position) const
726 {
727  if (group_position >= getNumberOfGroups())
728  throw IndexOutOfBoundsException("DataSet::getIndividualSequenceAtPositionInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
729  try
730  {
731  return groups_[group_position]->getIndividualSequenceAtPosition(individual_position, sequence_position);
732  }
733  catch (IndexOutOfBoundsException& ioobe)
734  {
735  if (string(ioobe.what()).find("individual_position") < string(ioobe.what()).size())
736  throw IndexOutOfBoundsException("DataSet::getIndividualSequenceAtPositionInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
737  // if (string(ioobe.what()).find("sequence_position") < string(ioobe.what()).size())
738  else
739  throw IndexOutOfBoundsException("DataSet::getIndividualSequenceAtPositionInGroup: sequence_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
740  }
741  catch (NullPointerException&)
742  {
743  throw NullPointerException("DataSet::getIndividualSequenceAtPositionInGroup: individual has no sequences.");
744  }
745 }
746 
747 /******************************************************************************/
748 
749 void DataSet::deleteIndividualSequenceByNameInGroup(size_t group_position, size_t individual_position, const std::string& sequence_name)
750 {
751  if (group_position >= getNumberOfGroups())
752  throw IndexOutOfBoundsException("DataSet::deleteIndividualSequenceByNameInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
753  try
754  {
755  groups_[group_position]->deleteIndividualSequenceByName(individual_position, sequence_name);
756  }
757  catch (IndexOutOfBoundsException& ioobe)
758  {
759  throw IndexOutOfBoundsException("DataSet::deleteIndividualSequenceByNameInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
760  }
761  catch (NullPointerException&)
762  {
763  throw NullPointerException("DataSet::deleteIndividualSequenceByNameInGroup: individual has no sequences.");
764  }
765  catch (SequenceNotFoundException& snfe)
766  {
767  throw SequenceNotFoundException("DataSet::deleteIndividualSequenceByNameInGroup: sequence_name not found.", snfe.getSequenceId());
768  }
769 }
770 
771 /******************************************************************************/
772 
773 void DataSet::deleteIndividualSequenceAtPositionInGroup(size_t group_position, size_t individual_position, size_t sequence_position)
774 {
775  if (group_position >= getNumberOfGroups())
776  throw IndexOutOfBoundsException("DataSet::deleteIndividualSequenceAtPositionInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
777  try
778  {
779  groups_[group_position]->deleteIndividualSequenceAtPosition(individual_position, sequence_position);
780  }
781  catch (IndexOutOfBoundsException& ioobe)
782  {
783  if (string(ioobe.what()).find("individual_position") < string(ioobe.what()).size())
784  throw IndexOutOfBoundsException("DataSet::deleteIndividualSequenceAtPositionInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
785  // if (string(ioobe.what()).find("sequence_position") < string(ioobe.what()).size())
786  else
787  throw IndexOutOfBoundsException("DataSet::deleteIndividualSequenceAtPositionInGroup: sequence_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
788  }
789  catch (NullPointerException&)
790  {
791  throw NullPointerException("DataSet::deleteIndividualSequenceAtPositionInGroup: individual has no sequences.");
792  }
793 }
794 
795 /******************************************************************************/
796 
797 std::vector<std::string> DataSet::getIndividualSequencesNamesInGroup(size_t group_position, size_t individual_position) const
798 {
799  if (group_position >= getNumberOfGroups())
800  throw IndexOutOfBoundsException("DataSet::getIndividualSequencesNamesInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
801  try
802  {
803  return groups_[group_position]->getIndividualSequencesNames(individual_position);
804  }
805  catch (IndexOutOfBoundsException& ioobe)
806  {
807  throw IndexOutOfBoundsException("DataSet::getIndividualSequencesNamesInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
808  }
809  catch (NullPointerException&)
810  {
811  throw NullPointerException("DataSet::getIndividualSequencesNamesInGroup: individual has no sequences.");
812  }
813 }
814 
815 /******************************************************************************/
816 
817 size_t DataSet::getIndividualSequencePositionInGroup(size_t group_position, size_t individual_position, const std::string& sequence_name) const
818 {
819  if (group_position >= getNumberOfGroups())
820  throw IndexOutOfBoundsException("DataSet::getIndividualSequencePositionInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
821  try
822  {
823  return groups_[group_position]->getIndividualSequencePosition(individual_position, sequence_name);
824  }
825  catch (IndexOutOfBoundsException& ioobe)
826  {
827  throw IndexOutOfBoundsException("DataSet::getIndividualSequencePositionInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
828  }
829  catch (NullPointerException&)
830  {
831  throw NullPointerException("DataSet::getIndividualSequencePositionInGroup: individual has no sequences.");
832  }
833  catch (SequenceNotFoundException& snfe)
834  {
835  throw SequenceNotFoundException("DataSet::getIndividualSequencePositionInGroup: sequence_name not found.", snfe.getSequenceId());
836  }
837 }
838 
839 /******************************************************************************/
840 
841 size_t DataSet::getIndividualNumberOfSequencesInGroup(size_t group_position, size_t individual_position) const
842 {
843  if (group_position >= getNumberOfGroups())
844  throw IndexOutOfBoundsException("DataSet::getIndividualNumberOfSequencesInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
845  try
846  {
847  return groups_[group_position]->getIndividualNumberOfSequences(individual_position);
848  }
849  catch (IndexOutOfBoundsException& ioobe)
850  {
851  throw IndexOutOfBoundsException("DataSet::getIndividualNumberOfSequencesInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
852  }
853  catch (NullPointerException&)
854  {
855  throw NullPointerException("DataSet::getIndividualNumberOfSequencesInGroup: individual has no sequences.");
856  }
857 }
858 
859 /******************************************************************************/
860 
861 void DataSet::setIndividualGenotypeInGroup(size_t group_position, size_t individual_position, const MultilocusGenotype& genotype)
862 {
863  if (group_position >= getNumberOfGroups())
864  throw IndexOutOfBoundsException("DataSet::setIndividualGenotypeInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
865  try
866  {
867  groups_[group_position]->setIndividualGenotype(individual_position, genotype);
868  }
869  catch (IndexOutOfBoundsException& ioobe)
870  {
871  throw IndexOutOfBoundsException("DataSet::setIndividualGenotypeInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
872  }
873 }
874 
875 /******************************************************************************/
876 
877 void DataSet::initIndividualGenotypeInGroup(size_t group_position, size_t individual_position)
878 {
879  if (group_position >= getNumberOfGroups())
880  throw IndexOutOfBoundsException("DataSet::initIndividualGenotypeInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
881  try
882  {
883  groups_[group_position]->initIndividualGenotype(individual_position, getNumberOfLoci());
884  }
885  catch (IndexOutOfBoundsException& ioobe)
886  {
887  throw IndexOutOfBoundsException("DataSet::initIndividualGenotypeInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
888  }
889  catch (BadIntegerException& bie)
890  {
891  throw BadIntegerException("DataSet::initIndividualGenotypeInGroup: number of loci must be > 0.", bie.getBadInteger());
892  }
893  catch (NullPointerException&)
894  {
895  throw NullPointerException("DataSet::initIndividualGenotypeInGroup: analyzed_loci is NULL.");
896  }
897  catch (Exception&)
898  {
899  throw Exception("DataSet::initIndividualGenotypeInGroup: individual already has a genotype.");
900  }
901 }
902 
903 /******************************************************************************/
904 
905 void DataSet::deleteIndividualGenotypeInGroup(size_t group_position, size_t individual_position)
906 {
907  if (group_position >= getNumberOfGroups())
908  throw IndexOutOfBoundsException("DataSet::deleteIndividualGenotypeInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
909  try
910  {
911  groups_[group_position]->deleteIndividualGenotype(individual_position);
912  }
913  catch (IndexOutOfBoundsException& ioobe)
914  {
915  throw IndexOutOfBoundsException("DataSet::deleteIndividualGenotypeInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
916  }
917 }
918 
919 /******************************************************************************/
920 
921 void DataSet::setIndividualMonolocusGenotypeInGroup(size_t group_position, size_t individual_position, size_t locus_position, const MonolocusGenotype& monogen)
922 {
923  if (group_position >= getNumberOfGroups())
924  throw IndexOutOfBoundsException("DataSet::setIndividualMonolocusGenotypeInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
925  try
926  {
927  groups_[group_position]->setIndividualMonolocusGenotype(individual_position, locus_position, monogen);
928  }
929  catch (IndexOutOfBoundsException& ioobe)
930  {
931  if (string(ioobe.what()).find("individual_position") < string(ioobe.what()).size())
932  throw IndexOutOfBoundsException("DataSet::setIndividualMonolocusGenotypeInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
933  // if (string(ioobe.what()).find("locus_position") < string(ioobe.what()).size())
934  else
935  throw IndexOutOfBoundsException("DataSet::setIndividualMonolocusGenotypeInGroup: locus_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
936  }
937  catch (NullPointerException&)
938  {
939  throw NullPointerException("DataSet::setIndividualMonolocusGenotypeInGroup: individual has no genotype.");
940  }
941 }
942 
943 /******************************************************************************/
944 
945 void DataSet::setIndividualMonolocusGenotypeByAlleleKeyInGroup(size_t group_position, size_t individual_position, size_t locus_position, const std::vector<size_t> allele_keys)
946 {
947  if (group_position >= getNumberOfGroups())
948  throw IndexOutOfBoundsException("DataSet::setIndividualMonolocusGenotypeByAlleleKeyInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
949  try
950  {
951  groups_[group_position]->setIndividualMonolocusGenotypeByAlleleKey(individual_position, locus_position, allele_keys);
952  }
953  catch (IndexOutOfBoundsException& ioobe)
954  {
955  if (string(ioobe.what()).find("individual_position") < string(ioobe.what()).size())
956  throw IndexOutOfBoundsException("DataSet::setIndividualMonolocusGenotypeByAlleleKeyInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
957  // if (string(ioobe.what()).find("locus_position") < string(ioobe.what()).size())
958  else
959  throw IndexOutOfBoundsException("DataSet::setIndividualMonolocusGenotypeByAlleleKeyInGroup: locus_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
960  }
961  catch (NullPointerException&)
962  {
963  throw NullPointerException("DataSet::setIndividualMonolocusGenotypeByAlleleKeyInGroup: individual has no genotype.");
964  }
965  catch (Exception&)
966  {
967  throw Exception("DataSet::setIndividualMonolocusGenotypeByAlleleKeyInGroup: no key in allele_keys.");
968  }
969 }
970 
971 /******************************************************************************/
972 
973 void DataSet::setIndividualMonolocusGenotypeByAlleleIdInGroup(size_t group_position, size_t individual_position, size_t locus_position, const std::vector<std::string> allele_id)
974 {
975  if (group_position >= getNumberOfGroups())
976  throw IndexOutOfBoundsException("DataSet::setIndividualMonolocusGenotypeByAlleleIdInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
977  const LocusInfo& locus_info = getLocusInfoAtPosition(locus_position);
978  try
979  {
980  groups_[group_position]->setIndividualMonolocusGenotypeByAlleleId(individual_position, locus_position, allele_id, locus_info);
981  }
982  catch (IndexOutOfBoundsException& ioobe)
983  {
984  if (string(ioobe.what()).find("individual_position") < string(ioobe.what()).size())
985  throw IndexOutOfBoundsException("DataSet::setIndividualMonolocusGenotypeByAlleleIdInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
986  // if (string(ioobe.what()).find("locus_position") < string(ioobe.what()).size())
987  else
988  throw IndexOutOfBoundsException("DataSet::setIndividualMonolocusGenotypeByAlleleIdInGroup: locus_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
989  }
990  catch (NullPointerException&)
991  {
992  throw NullPointerException("DataSet::setIndividualMonolocusGenotypeByAlleleIdInGroup: individual has no genotype.");
993  }
994  catch (AlleleNotFoundException& anfe)
995  {
996  throw AlleleNotFoundException("DataSet::setIndividualMonolocusGenotypeByAlleleIdInGroup: id not found.", anfe.getIdentifier());
997  }
998 }
999 
1000 /******************************************************************************/
1001 
1002 const MonolocusGenotype* DataSet::getIndividualMonolocusGenotypeInGroup(size_t group_position, size_t individual_position, size_t locus_position) const
1003 {
1004  if (group_position >= getNumberOfGroups())
1005  throw IndexOutOfBoundsException("DataSet::getIndividualMonolocusGenotypeInGroup: group_position out of bounds.", group_position, 0, getNumberOfGroups());
1006  try
1007  {
1008  return &groups_[group_position]->getIndividualMonolocusGenotype(individual_position, locus_position);
1009  }
1010  catch (IndexOutOfBoundsException& ioobe)
1011  {
1012  if (string(ioobe.what()).find("individual_position") < string(ioobe.what()).size())
1013  throw IndexOutOfBoundsException("DataSet::getIndividualMonolocusGenotypeInGroup: individual_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
1014  // if (string(ioobe.what()).find("locus_position") < string(ioobe.what()).size())
1015  else
1016  throw IndexOutOfBoundsException("DataSet::getIndividualMonolocusGenotypeInGroup: locus_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
1017  }
1018  catch (NullPointerException&)
1019  {
1020  throw NullPointerException("DataSet::getIndividualMonolocusGenotypeInGroup: individual has no genotype.");
1021  }
1022 }
1023 
1024 /******************************************************************************/
1025 
1026 // Dealing with AnalyzedSequences --------------------------
1027 
1028 void DataSet::setAlphabet(const Alphabet* alpha)
1029 {
1030  if (analyzedSequences_ == 0)
1033 }
1034 
1035 /******************************************************************************/
1036 
1037 void DataSet::setAlphabet(const std::string& alpha_type)
1038 {
1039  if (analyzedSequences_ == 0)
1041  analyzedSequences_->setAlphabet(alpha_type);
1042 }
1043 
1044 /******************************************************************************/
1045 
1047 {
1048  if (analyzedSequences_ != 0)
1049  return analyzedSequences_->getAlphabet();
1050  throw NullPointerException("DataSet::getAlphabet: no sequence data.");
1051 }
1052 
1053 /******************************************************************************/
1054 
1055 std::string DataSet::getAlphabetType() const
1056 {
1057  if (analyzedSequences_ != 0)
1059  throw NullPointerException("DataSet::getAlphabetType: no sequence data.");
1060 }
1061 
1062 /******************************************************************************/
1063 
1064 // Dealing with AnalyzedLoci -------------------------------
1065 
1066 void DataSet::setAnalyzedLoci(const AnalyzedLoci& analyzedLoci)
1067 {
1068  if (analyzedLoci_ != 0)
1069  {
1070  try
1071  {
1073  }
1074  catch (Exception& e)
1075  {
1076  throw Exception ("DataSet::setAnalyzedLoci: at least one individual has a genotype of the actual AnalyzedLoci.");
1077  }
1078  }
1079  analyzedLoci_ = new AnalyzedLoci(analyzedLoci);
1080 }
1081 
1082 /******************************************************************************/
1083 
1084 void DataSet::initAnalyzedLoci(size_t number_of_loci)
1085 {
1086  if (analyzedLoci_ != 0)
1087  throw Exception("DataSet::initAnalyzedLoci: analyzedLoci_ already initialyzed.");
1088  analyzedLoci_ = new AnalyzedLoci(number_of_loci);
1089 }
1090 
1091 /******************************************************************************/
1092 
1094 {
1095  if (analyzedLoci_ != 0)
1096  return analyzedLoci_;
1097  throw NullPointerException("DataSet::getAnalyzedLoci: no loci initialized.");
1098 }
1099 
1100 /******************************************************************************/
1101 
1103 {
1104  if (analyzedLoci_ != 0)
1105  delete analyzedLoci_;
1106 }
1107 
1108 /******************************************************************************/
1109 
1110 void DataSet::setLocusInfo(size_t locus_position, const LocusInfo& locus)
1111 {
1112  if (analyzedLoci_ == 0)
1113  throw NullPointerException("DataSet::setLocusInfo: there's no AnalyzedLoci to setup.");
1114  try
1115  {
1116  analyzedLoci_->setLocusInfo(locus_position, locus);
1117  }
1118  catch (IndexOutOfBoundsException& ioobe)
1119  {
1120  throw IndexOutOfBoundsException("DataSet::setLocusInfo: locus_position out of bounds.", locus_position, 0, analyzedLoci_->getNumberOfLoci());
1121  }
1122 }
1123 
1124 /******************************************************************************/
1125 
1126 const LocusInfo& DataSet::getLocusInfoByName(const std::string& locus_name) const
1127 {
1128  if (analyzedLoci_ == 0)
1129  throw NullPointerException("DataSet::getLocusInfoByName: there's no AnalyzedLoci.");
1130  try
1131  {
1132  return analyzedLoci_->getLocusInfoByName(locus_name);
1133  }
1134  catch (LocusNotFoundException& lnfe)
1135  {
1136  throw LocusNotFoundException("DataSet::getLocusInfoByName: locus_name not found", locus_name);
1137  }
1138 }
1139 
1140 /******************************************************************************/
1141 
1142 const LocusInfo& DataSet::getLocusInfoAtPosition(size_t locus_position) const
1143 {
1144  if (analyzedLoci_ == 0)
1145  throw NullPointerException("DataSet::getLocusInfoAtPosition: there's no AnalyzedLoci.");
1146  try
1147  {
1148  return analyzedLoci_->getLocusInfoAtPosition(locus_position);
1149  }
1150  catch (IndexOutOfBoundsException& ioobe)
1151  {
1152  throw IndexOutOfBoundsException("DataSet::getLocusInfoAtPosition: locus_position out of bounds.", locus_position, ioobe.getBounds()[0], ioobe.getBounds()[1]);
1153  }
1154  catch (NullPointerException& npe)
1155  {
1156  throw NullPointerException("DataSet::getLocusInfoAtPosition: no locus defined here");
1157  }
1158 }
1159 
1160 /******************************************************************************/
1161 
1162 void DataSet::addAlleleInfoByLocusName(const std::string& locus_name, const AlleleInfo& allele)
1163 {
1164  if (analyzedLoci_ == 0)
1165  throw NullPointerException("DataSet::addAlleleInfoByLocusName: there's no AnalyzedLoci.");
1166  try
1167  {
1168  analyzedLoci_->addAlleleInfoByLocusName(locus_name, allele);
1169  }
1170  catch (LocusNotFoundException& lnfe)
1171  {
1172  throw LocusNotFoundException("DataSet::addAlleleInfoByLocusName: locus_name not found.", lnfe.getIdentifier());
1173  }
1174  catch (BadIdentifierException& bie)
1175  {
1176  throw BadIdentifierException("DataSet::addAlleleInfoByLocusName: allele's id already in use.", bie.getIdentifier());
1177  }
1178 }
1179 
1180 /******************************************************************************/
1181 
1182 void DataSet::addAlleleInfoByLocusPosition(size_t locus_position, const AlleleInfo& allele)
1183 {
1184  if (analyzedLoci_ == 0)
1185  throw NullPointerException("DataSet::addAlleleInfoByLocusPosition: there's no AnalyzedLoci.");
1186  try
1187  {
1188  analyzedLoci_->addAlleleInfoByLocusPosition(locus_position, allele);
1189  }
1190  catch (IndexOutOfBoundsException& ioobe)
1191  {
1192  throw IndexOutOfBoundsException("DataSet::addAlleleInfoByLocusPosition: locus_position out of bounds.", locus_position, ioobe.getBounds()[0], ioobe.getBounds()[1]);
1193  }
1194  catch (BadIdentifierException& bie)
1195  {
1196  throw BadIdentifierException("DataSet::addAlleleInfoByLocusPosition: allele'e id already in use.", bie.getIdentifier());
1197  }
1198 }
1199 
1200 /******************************************************************************/
1201 
1203 {
1204  if (analyzedLoci_ == 0)
1205  throw NullPointerException("DataSet::getNumberOfLoci: there's no AnalyzedLoci.");
1206  return analyzedLoci_->getNumberOfLoci();
1207 }
1208 
1209 /******************************************************************************/
1210 
1211 size_t DataSet::getPloidyByLocusName(const std::string& locus_name) const
1212 {
1213  if (analyzedLoci_ == 0)
1214  throw NullPointerException("DataSet::getPloidyByLocusName: there's no AnalyzedLoci.");
1215  try
1216  {
1217  return analyzedLoci_->getPloidyByLocusName(locus_name);
1218  }
1219  catch (LocusNotFoundException& lnfe)
1220  {
1221  throw LocusNotFoundException("DataSet::getPloidyByLocusName: locus_name not found.", lnfe.getIdentifier());
1222  }
1223 }
1224 
1225 /******************************************************************************/
1226 
1227 size_t DataSet::getPloidyByLocusPosition(size_t locus_position) const
1228 {
1229  if (analyzedLoci_ == 0)
1230  throw NullPointerException("DataSet::getPloidyByLocusPosition: there's no AnalyzedLoci.");
1231  try
1232  {
1233  return analyzedLoci_->getPloidyByLocusPosition(locus_position);
1234  }
1235  catch (IndexOutOfBoundsException& ioobe)
1236  {
1237  throw IndexOutOfBoundsException("DataSet::getPloidyByLocusPosition: locus_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
1238  }
1239 }
1240 
1241 /******************************************************************************/
1242 
1243 // Container extraction -----------------------------------
1244 
1246 {
1248  for (size_t i = 0; i < getNumberOfGroups(); i++)
1249  {
1250  // nommer les groupes khalid
1251  string name = groups_[i]->getGroupName();
1252  pmgc->addGroupName(i, name);
1253  for (size_t j = 0; j < getNumberOfIndividualsInGroup(i); j++)
1254  {
1255  const Individual* tmp_ind = getIndividualAtPositionFromGroup(i, j);
1256  if (tmp_ind->hasGenotype())
1257  {
1258  const MultilocusGenotype& tmp_mg = tmp_ind->getGenotype();
1259  pmgc->addMultilocusGenotype(tmp_mg, i);
1260  }
1261  }
1262  }
1263  return pmgc;
1264 }
1265 
1266 /******************************************************************************/
1267 
1268 PolymorphismMultiGContainer* DataSet::getPolymorphismMultiGContainer(const std::map<size_t, std::vector<size_t> >& selection) const
1269 {
1271  for (map<size_t, vector<size_t> >::const_iterator it = selection.begin(); it != selection.end(); it++)
1272  {
1273  size_t i;
1274  try
1275  {
1276  i = getGroupPosition(it->first);
1277  }
1278  catch (GroupNotFoundException& gnfe)
1279  {
1280  throw gnfe;
1281  }
1282  string name = groups_[i]->getGroupName();
1283  pmgc->addGroupName(i, name);
1284  for (size_t j = 0; j < it->second.size(); j++)
1285  {
1286  const Individual* tmp_ind = 0;
1287  try
1288  {
1289  tmp_ind = getIndividualAtPositionFromGroup(i, j);
1290  }
1291  catch (IndexOutOfBoundsException& ioobe)
1292  {
1293  throw ioobe;
1294  }
1295  if (tmp_ind->hasGenotype())
1296  {
1297  const MultilocusGenotype& tmp_mg = tmp_ind->getGenotype();
1298  pmgc->addMultilocusGenotype(tmp_mg, i);
1299  }
1300  }
1301  }
1302  return pmgc;
1303 }
1304 
1305 /******************************************************************************/
1306 
1307 PolymorphismSequenceContainer* DataSet::getPolymorphismSequenceContainer(const std::map<size_t, std::vector<size_t> >& selection, size_t sequence_position) const
1308 {
1310  for (map<size_t, vector<size_t> >::const_iterator it = selection.begin(); it != selection.end(); it++)
1311  {
1312  size_t i;
1313  try
1314  {
1315  i = getGroupPosition(it->first);
1316  }
1317  catch (GroupNotFoundException& gnfe)
1318  {
1319  delete psc;
1320  throw gnfe;
1321  }
1322  for (size_t j = 0; j < it->second.size(); j++)
1323  {
1324  const Individual* tmp_ind = 0;
1325  try
1326  {
1327  tmp_ind = getIndividualAtPositionFromGroup(i, j);
1328  }
1329  catch (IndexOutOfBoundsException& ioobe)
1330  {
1331  delete psc;
1332  throw ioobe;
1333  }
1334  if (tmp_ind->hasSequenceAtPosition(sequence_position))
1335  {
1336  const Sequence& tmp_seq = tmp_ind->getSequenceAtPosition(sequence_position);
1337  psc->addSequenceWithFrequency(tmp_seq, 1, false);
1338  psc->setGroupId(tmp_seq.getName(), it->first);
1339  }
1340  }
1341  }
1342  return psc;
1343 }
1344 
1345 /******************************************************************************/
1346 
1347 // General tests ------------------------------------------
1348 
1350 {
1351  return analyzedSequences_ != 0;
1352 }
1353 
1354 /******************************************************************************/
1355 
1357 {
1358  return analyzedLoci_ != 0;
1359 }
1360 
1361 /******************************************************************************/
1362 
The AlleleInfo interface.
Definition: AlleleInfo.h:60
The AlleleNotFoundException class.
virtual const std::string getIdentifier() const
Return the value of the identifier as a string.
std::vector< const Alphabet * > getAlphabets() const
The AnalyzedLoci class.
Definition: AnalyzedLoci.h:65
size_t getNumberOfLoci() const
Get the number of loci.
Definition: AnalyzedLoci.h:138
unsigned int getPloidyByLocusName(const std::string &locus_name) const
Get the ploidy of a locus by name.
const LocusInfo & getLocusInfoByName(const std::string &locus_name) const
Get a LocusInfo by name.
const LocusInfo & getLocusInfoAtPosition(size_t locus_position) const
Get a LocusInfo by its position.
unsigned int getPloidyByLocusPosition(size_t locus_position) const
Get the ploidy of a locus by its position.
void addAlleleInfoByLocusPosition(size_t locus_position, const AlleleInfo &allele)
Add an AlleleInfo to a LocusInfo by its position.
void addAlleleInfoByLocusName(const std::string &locus_name, const AlleleInfo &allele)
Add an AlleleInfo to a LocusInfo by LocusInfo name.
void setLocusInfo(size_t locus_position, const LocusInfo &locus)
Set a LocusInfo.
The AnalyzedSequences class.
std::string getAlphabetType() const
Get the alphabet type as a string.
const Alphabet * getAlphabet() const
Get the alphabet.
void setAlphabet(const Alphabet *alpha)
Set the alphabet used for the sequences.
The BadIdentifierException class.
virtual const std::string getIdentifier() const
Return the value of the identifier as a string.
virtual const Alphabet * getAlphabet() const=0
The DataSet class.
Definition: DataSet.h:73
std::string getAlphabetType() const
Get the alphabet type as a string.
Definition: DataSet.cpp:1055
void setAnalyzedLoci(const AnalyzedLoci &analyzedLoci)
Set the AnalyzedLoci to the DataSet.
Definition: DataSet.cpp:1066
void setIndividualSexInGroup(size_t group_position, size_t individual_position, const unsigned short sex)
Set the sex of an Individual in a Group.
Definition: DataSet.cpp:528
void setIndividualDateInGroup(size_t group_position, size_t individual_position, const Date &date)
Set the Date of an Individual in a Group.
Definition: DataSet.cpp:560
bool hasAlleleicData() const
Tell if there is alelelic data.
Definition: DataSet.cpp:1356
size_t getNumberOfGroups() const
Get the number of Groups.
Definition: DataSet.cpp:300
void setIndividualMonolocusGenotypeInGroup(size_t group_position, size_t individual_position, size_t locus_position, const MonolocusGenotype &monogen)
Set a MonolocusGenotype of an Individual from a group.
Definition: DataSet.cpp:921
size_t getNumberOfLocalities() const
Get the number of Localities.
Definition: DataSet.cpp:190
void initIndividualGenotypeInGroup(size_t group_position, size_t individual_position)
Initialyze the genotype of an Individual in a Group.
Definition: DataSet.cpp:877
const Sequence & getIndividualSequenceByNameInGroup(size_t group_position, size_t individual_position, const std::string &sequence_name) const
Get a Sequence from an Individual of a Group.
Definition: DataSet.cpp:701
const Group & getGroupById(size_t group_id) const
Get a group by identifier.
Definition: DataSet.cpp:229
void setIndividualCoordInGroup(size_t group_position, size_t individual_position, const Point2D< double > &coord)
Set the coordinates of an Individual in a Group.
Definition: DataSet.cpp:596
const Group & getGroupAtPosition(size_t group_position) const
Get a group by position.
Definition: DataSet.cpp:281
std::vector< Locality< double > * > localities_
Definition: DataSet.h:77
const Locality< double > * getIndividualLocalityInGroup(size_t group_position, size_t individual_position) const
Get the Locality of an Individual in a Group.
Definition: DataSet.cpp:652
void addAlleleInfoByLocusName(const std::string &locus_name, const AlleleInfo &allele)
Add an AlleleInfo to a LocusInfo.
Definition: DataSet.cpp:1162
void setIndividualLocalityInGroupByName(size_t group_position, size_t individual_position, const std::string &locality_name)
Set the Locality of an Individual in a Group.
Definition: DataSet.cpp:632
void addAlleleInfoByLocusPosition(size_t locus_position, const AlleleInfo &allele)
Add an AlleleInfo to a LocusInfo.
Definition: DataSet.cpp:1182
size_t getPloidyByLocusPosition(size_t locus_position) const
Get the ploidy of a locus.
Definition: DataSet.cpp:1227
void mergeTwoGroups(size_t source_id, size_t target_id)
Merge two groups.
Definition: DataSet.cpp:307
~DataSet()
Destroy a DataSet.
Definition: DataSet.cpp:98
void setIndividualGenotypeInGroup(size_t group_position, size_t individual_position, const MultilocusGenotype &genotype)
Set the MultilocusGenotype of an Individual in a Group.
Definition: DataSet.cpp:861
std::vector< Group * > groups_
Definition: DataSet.h:78
bool hasLocality() const
Tell if there is at least one locality.
Definition: DataSet.cpp:197
void initAnalyzedLoci(size_t number_of_loci)
Initialize the AnalyzedLoci for number of loci.
Definition: DataSet.cpp:1084
const Date * getIndividualDateInGroup(size_t group_position, size_t individual_position) const
Get the Date of an Individual in a Group.
Definition: DataSet.cpp:576
void deleteIndividualSequenceByNameInGroup(size_t group_position, size_t individual_position, const std::string &sequence_name)
Delete a Sequence of an Individual of a Group.
Definition: DataSet.cpp:749
void mergeGroups(std::vector< size_t > &group_ids)
Merge some Groups in one.
Definition: DataSet.cpp:338
void deleteLocalityByName(const std::string &name)
Delete a Locality from the DataSet.
Definition: DataSet.cpp:176
const MonolocusGenotype * getIndividualMonolocusGenotypeInGroup(size_t group_position, size_t individual_position, size_t locus_position) const
Get a MonolocusGenotype from an Individual of a Group.
Definition: DataSet.cpp:1002
size_t getNumberOfIndividualsInGroup(size_t group_position) const
Get the number of Individuals in a Group.
Definition: DataSet.cpp:439
void setGroupName(size_t group_id, const std::string &group_name) const
set the name of a Group.
Definition: DataSet.cpp:254
void setAlphabet(const Alphabet *alpha)
Set the alphabet of the AnalyzedSequences.
Definition: DataSet.cpp:1028
void deleteIndividualSequenceAtPositionInGroup(size_t group_position, size_t individual_position, size_t sequence_position)
Delete a Sequence of an Individual of a Group.
Definition: DataSet.cpp:773
void deleteIndividualAtPositionFromGroup(size_t group_position, size_t individual_position)
Delete an Individual from a group.
Definition: DataSet.cpp:496
void deleteGroupAtPosition(size_t group_position)
Delete a Group from the DataSet.
Definition: DataSet.cpp:290
size_t getIndividualSequencePositionInGroup(size_t group_position, size_t individual_position, const std::string &sequence_name) const
Get the position of a Sequence in an Individual of a Group.
Definition: DataSet.cpp:817
void addIndividualSequenceInGroup(size_t group_position, size_t individual_position, size_t sequence_position, const Sequence &sequence)
Add a Sequence to an Individual in a Group.
Definition: DataSet.cpp:672
bool hasSequenceData() const
Tell if at least one individual has at least one sequence.
Definition: DataSet.cpp:1349
size_t getGroupPosition(size_t group_id) const
Get the position of a Group.
Definition: DataSet.cpp:269
unsigned short getIndividualSexInGroup(size_t group_position, size_t individual_position) const
Get the sex of an Individual in a Group.
Definition: DataSet.cpp:544
std::string getGroupName(size_t group_id) const
Get the name of a Group. If the name is an empty string it just returns the group_id.
Definition: DataSet.cpp:241
const Individual * getIndividualByIdFromGroup(size_t group_position, const std::string &individual_id) const
Get an Individual from a Group.
Definition: DataSet.cpp:480
DataSet & operator=(const DataSet &ds)
Definition: DataSet.cpp:78
void setIndividualMonolocusGenotypeByAlleleIdInGroup(size_t group_position, size_t individual_position, size_t locus_position, const std::vector< std::string > allele_id)
Set a MonolocusGenotype of an Individual from a group.
Definition: DataSet.cpp:973
void addGroup(const Group &group)
Add a Group to the DataSet.
Definition: DataSet.cpp:205
const Alphabet * getAlphabet() const
Get the alphabet if there is sequence data.
Definition: DataSet.cpp:1046
std::vector< std::string > getIndividualSequencesNamesInGroup(size_t group_position, size_t individual_position) const
Get the Sequences' names from an Individual of a Group.
Definition: DataSet.cpp:797
void setLocusInfo(size_t locus_position, const LocusInfo &locus)
Set a LocusInfo.
Definition: DataSet.cpp:1110
void setIndividualMonolocusGenotypeByAlleleKeyInGroup(size_t group_position, size_t individual_position, size_t locus_position, const std::vector< size_t > allele_keys)
Set a MonolocusGenotype of an Individual from a group.
Definition: DataSet.cpp:945
const Locality< double > & getLocalityAtPosition(size_t locality_position) const
Get a Locality by locality_position.
Definition: DataSet.cpp:143
PolymorphismSequenceContainer * getPolymorphismSequenceContainer(const std::map< size_t, std::vector< size_t > > &selection, size_t sequence_position) const
Get a PolymorphismSequenceContainer from a selection of groups and individuals.
Definition: DataSet.cpp:1307
void addEmptyIndividualToGroup(size_t group_position, const std::string &individual_id)
Add an empty Individual to a Group.
Definition: DataSet.cpp:423
AnalyzedSequences * analyzedSequences_
Definition: DataSet.h:76
void deleteAnalyzedLoci()
Delete the AnalyzedLoci.
Definition: DataSet.cpp:1102
const LocusInfo & getLocusInfoByName(const std::string &locus_name) const
Get a LocusInfo by its name.
Definition: DataSet.cpp:1126
size_t getPloidyByLocusName(const std::string &locus_name) const
Get the ploidy of a locus.
Definition: DataSet.cpp:1211
const Sequence & getIndividualSequenceAtPositionInGroup(size_t group_position, size_t individual_position, size_t sequence_position) const
Get a Sequence from an Individual of a Group.
Definition: DataSet.cpp:725
PolymorphismMultiGContainer * getPolymorphismMultiGContainer() const
Get a PolymorphismMultiGContainer with all allelic data of the DataSet.
Definition: DataSet.cpp:1245
size_t getLocalityPosition(const std::string &name) const
Get the position of a locality in the container.
Definition: DataSet.cpp:131
size_t getIndividualNumberOfSequencesInGroup(size_t group_position, size_t individual_position) const
Get the number of Sequences in an Individual of a Group.
Definition: DataSet.cpp:841
void deleteIndividualGenotypeInGroup(size_t group_position, size_t individual_position)
Delete the MultilocusGenotype of an Individual from a Group.
Definition: DataSet.cpp:905
DataSet()
Build a new void DataSet.
Definition: DataSet.cpp:48
const LocusInfo & getLocusInfoAtPosition(size_t locus_position) const
Get a LocusInfo by its position.
Definition: DataSet.cpp:1142
size_t getNumberOfLoci() const
Get the number of loci.
Definition: DataSet.cpp:1202
const Point2D< double > * getIndividualCoordInGroup(size_t group_position, size_t individual_position) const
Get the coordinate of an Individual in a Group.
Definition: DataSet.cpp:612
const AnalyzedLoci * getAnalyzedLoci() const
Get the AnalyzedLoci if there is one.
Definition: DataSet.cpp:1093
AnalyzedLoci * analyzedLoci_
Definition: DataSet.h:75
void addIndividualToGroup(size_t group_position, const Individual &individual)
Add an Individual to a Group.
Definition: DataSet.cpp:405
size_t getIndividualPositionInGroup(size_t group_position, const std::string &individual_id) const
Get the position of an Individual in a Group.
Definition: DataSet.cpp:448
void deleteIndividualByIdFromGroup(size_t group_position, const std::string &individual_id)
Delete an Individual from a group.
Definition: DataSet.cpp:512
void deleteLocalityAtPosition(size_t locality_position)
Delete a Locality from the DataSet.
Definition: DataSet.cpp:166
void addEmptyGroup(size_t group_id)
Add an empty Group to the DataSet.
Definition: DataSet.cpp:217
void splitGroup(size_t group_id, std::vector< size_t > individuals_selection)
Split a group in two.
Definition: DataSet.cpp:369
const Individual * getIndividualAtPositionFromGroup(size_t group_position, size_t individual_position) const
Get an Individual from a Group.
Definition: DataSet.cpp:464
const Locality< double > & getLocalityByName(const std::string &name) const
Get a Locality by name.
Definition: DataSet.cpp:152
void addLocality(Locality< double > &locality)
Add a locality to the DataSet.
Definition: DataSet.cpp:119
The Date class.
Definition: Date.h:57
const char * what() const noexcept override
The GroupNotFoundException class.
virtual const std::string getIdentifier() const
Return the value of the identifier as a string.
The Group class.
Definition: Group.h:71
size_t getNumberOfIndividuals() const
Get the number of Individual in the Group.
Definition: Group.cpp:204
size_t getGroupId() const
Get the id of the Group.
Definition: Group.h:134
void addIndividual(const Individual &ind)
Add an Individual.
Definition: Group.cpp:98
const Individual & getIndividualAtPosition(size_t individual_position) const
Get a reference to an Individual by its position.
Definition: Group.cpp:197
const std::string & getGroupName() const
Get the name of the Group.
Definition: Group.h:120
std::size_t getBadIndex() const
const std::array< std::size_t, 2 > & getBounds() const
The IndividualNotFoundException class.
virtual const std::string getIdentifier() const
Return the value of the identifier as a string.
The Individual class.
Definition: Individual.h:76
const Sequence & getSequenceAtPosition(const size_t sequence_position) const
Get a sequence by its position.
Definition: Individual.cpp:338
bool hasSequences() const
Tell if the Individual has some sequences.
Definition: Individual.cpp:426
bool hasGenotype() const
Tell if the Individual has a MultilocusGenotype.
Definition: Individual.cpp:524
const MultilocusGenotype & getGenotype() const
Get the genotype.
Definition: Individual.cpp:508
bool hasSequenceAtPosition(size_t position) const
Tell if the Individual has a sequence at a given position.
Definition: Individual.cpp:433
const Alphabet * getSequenceAlphabet() const
Return the alphabet of the sequences.
Definition: Individual.cpp:449
The LocalityNotFoundException class.
virtual const std::string getIdentifier() const
Return the value of the identifier as a string.
The Locality class.
Definition: Locality.h:60
const std::string & getName() const
Get the name of the locality.
Definition: Locality.h:125
The LocusInfo class.
Definition: LocusInfo.h:64
The LocusNotFoundException class.
virtual const std::string getIdentifier() const
Return the value of the identifier as a string.
The MonolocusGenotype virtual class.
The MultilocusGenotype class.
The PolymorphismMultiGContainer class.
void addGroupName(size_t group_id, std::string name)
Inserts a name for the given group id.
void addMultilocusGenotype(const MultilocusGenotype &mg, size_t group)
Add a MultilocusGenotype to the container.
The PolymorphismSequenceContainer class.
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.
virtual const std::string getSequenceId() const
virtual const std::string & getName() const=0
std::string toString(T t)