bpp-popgen  3.0.0
Individual.cpp
Go to the documentation of this file.
1 //
2 // File Individual.cpp
3 // Author : Sylvain Gaillard
4 // Last modification : Tuesday August 03 2004
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for population genetics analysis.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
40 #include "Individual.h"
41 
42 using namespace bpp;
43 
44 using namespace std;
45 
46 // ** Class constructor: *******************************************************/
48  sex_(0),
49  date_(),
50  coord_(),
51  locality_(0),
52  sequences_(),
53  genotype_() {}
54 
55 Individual::Individual(const std::string& id) : id_(id),
56  sex_(0),
57  date_(),
58  coord_(),
59  locality_(0),
60  sequences_(),
61  genotype_() {}
62 
63 Individual::Individual(const string& id,
64  const Date& date,
65  const Point2D<double>& coord,
66  Locality<double>* locality,
67  const unsigned short sex) :
68  id_(id),
69  sex_(sex),
70  date_(new Date(date)),
71  coord_(new Point2D<double>(coord)),
72  locality_(locality),
73  sequences_(),
74  genotype_() {}
75 
76 Individual::Individual(const Individual& ind) : id_(ind.getId()),
77  sex_(ind.getSex()),
78  date_(),
79  coord_(),
80  locality_(0),
81  sequences_(),
82  genotype_()
83 {
84  try
85  {
86  setDate(ind.getDate());
87  }
88  catch (...)
89  {}
90  try
91  {
92  setCoord(ind.getCoord());
93  }
94  catch (...)
95  {}
96  try
97  {
98  setLocality(ind.getLocality());
99  }
100  catch (...)
101  {}
102  try
103  {
104  setSequences(dynamic_cast<const MapSequenceContainer&>(ind.getSequences()));
105  }
106  catch (...)
107  {}
108  if (ind.hasGenotype())
109  genotype_.reset(new MultilocusGenotype(ind.getGenotype()));
110 }
111 
112 // ** Class destructor: *******************************************************/
114 
115 // ** Other methodes: *********************************************************/
116 
118 {
119  setId(ind.getId());
120  setSex(ind.getSex());
121  try
122  {
123  setDate(ind.getDate());
124  }
125  catch (NullPointerException&)
126  {
127  date_.reset();
128  }
129  try
130  {
131  setCoord(ind.getCoord());
132  }
133  catch (NullPointerException&)
134  {
135  coord_.reset();
136  }
137  try
138  {
139  setLocality(ind.getLocality());
140  }
141  catch (NullPointerException&)
142  {
143  locality_ = 0;
144  }
145  try
146  {
147  setSequences(dynamic_cast<const MapSequenceContainer&>(ind.getSequences()));
148  }
149  catch (NullPointerException&)
150  {
151  sequences_.reset();
152  }
153  genotype_.reset(ind.hasGenotype() ? new MultilocusGenotype(ind.getGenotype()) : 0);
154  return *this;
155 }
156 
157 /******************************************************************************/
158 
159 // Id
160 void Individual::setId(const std::string& id)
161 {
162  id_ = id;
163 }
164 
165 /******************************************************************************/
166 
167 // Sex
168 void Individual::setSex(const unsigned short sex)
169 {
170  sex_ = sex;
171 }
172 
173 /******************************************************************************/
174 
175 // Date
176 void Individual::setDate(const Date& date)
177 {
178  date_.reset(new Date(date));
179 }
180 
181 /******************************************************************************/
182 
183 const Date& Individual::getDate() const
184 {
185  if (hasDate())
186  return *date_.get();
187  else
188  throw (NullPointerException("Individual::getDate: no date associated to this individual."));
189 }
190 
191 /******************************************************************************/
192 
194 {
195  return date_.get() != 0;
196 }
197 
198 /******************************************************************************/
199 
200 // Coord
202 {
203  coord_.reset(new Point2D<double>(coord));
204 }
205 
206 /******************************************************************************/
207 
208 void Individual::setCoord(const double x, const double y)
209 {
210  coord_.reset(new Point2D<double>(x, y));
211 }
212 
213 /******************************************************************************/
214 
216 {
217  if (hasCoord())
218  return *coord_.get();
219  else
220  throw (NullPointerException("Individual::getCoord: no coord associated to this individual."));
221 }
222 
223 /******************************************************************************/
224 
226 {
227  return coord_.get() != 0;
228 }
229 
230 /******************************************************************************/
231 
232 void Individual::setX(const double x)
233 {
234  if (hasCoord())
235  coord_->setX(x);
236  else
237  throw (NullPointerException("Individual::setX: no coord associated to this individual."));
238 }
239 
240 /******************************************************************************/
241 
242 void Individual::setY(const double y)
243 {
244  if (hasCoord())
245  coord_->setY(y);
246  else
247  throw (NullPointerException("Individual::setY: no coord associated to this individual."));
248 }
249 
250 /******************************************************************************/
251 
252 double Individual::getX() const
253 {
254  if (hasCoord())
255  return coord_->getX();
256  else
257  throw (NullPointerException("Individual::getX: no coord associated to this individual."));
258 }
259 
260 /******************************************************************************/
261 
262 double Individual::getY() const
263 {
264  if (hasCoord())
265  return coord_->getY();
266  else
267  throw (NullPointerException("Individual::getY: no coord associated to this individual."));
268 }
269 
270 /******************************************************************************/
271 
272 // Locality
274 {
275  locality_ = locality;
276 }
277 
278 /******************************************************************************/
279 
281 {
282  if (hasLocality())
283  return locality_;
284  else
285  throw (NullPointerException("Individual::getLocality: no locality associated to this individual."));
286 }
287 
288 /******************************************************************************/
289 
291 {
292  return locality_ != 0;
293 }
294 
295 /******************************************************************************/
296 
297 // Sequences
298 void Individual::addSequence(size_t sequence_key, const Sequence& sequence)
299 {
300  if (sequences_.get() == 0)
301  sequences_.reset(new MapSequenceContainer(sequence.getAlphabet()));
302  try
303  {
304  sequences_->addSequence(TextTools::toString(sequence_key), sequence);
305  }
306  catch (AlphabetMismatchException& ame)
307  {
308  throw (AlphabetMismatchException("Individual::addSequence: alphabets don't match.", ame.getAlphabets()[0], ame.getAlphabets()[1]));
309  }
310  catch (Exception& e)
311  {
312  if (string(e.what()).find("name") < string(e.what()).size())
313  throw (BadIdentifierException("Individual::addSequence: sequence's name already in use.", sequence.getName()));
314  // if (string(e.what()).find("key") < string(e.what()).size())
315  else
316  throw (Exception("Individual::addSequence: sequence_key already in use:" + TextTools::toString(sequence_key)));
317  }
318 }
319 
320 /******************************************************************************/
321 
322 const Sequence& Individual::getSequenceByName(const std::string& sequence_name) const
323 {
324  if (sequences_.get() == 0)
325  throw NullPointerException("Individual::getSequenceByName: no sequence data.");
326  try
327  {
328  return sequences_->getSequence(sequence_name);
329  }
330  catch (SequenceNotFoundException& snfe)
331  {
332  throw SequenceNotFoundException("Individual::getSequenceByName: sequence_name not found.", snfe.getSequenceId());
333  }
334 }
335 
336 /******************************************************************************/
337 
338 const Sequence& Individual::getSequenceAtPosition(size_t sequence_position) const
339 {
340  if (sequences_.get() == 0)
341  throw NullPointerException("Individual::getSequenceAtPosition: no sequence data.");
342  try
343  {
344  return sequences_->getSequenceByKey(TextTools::toString(sequence_position));
345  }
346  catch (SequenceNotFoundException& snfe)
347  {
348  throw SequenceNotFoundException("Individual::getSequenceAtPosition: sequence_position not found", snfe.getSequenceId());
349  }
350 }
351 
352 /******************************************************************************/
353 
354 void Individual::deleteSequenceByName(const std::string& sequence_name)
355 {
356  if (sequences_.get() == 0)
357  throw NullPointerException("Individual::deleteSequenceByName: no sequence data.");
358  try
359  {
360  sequences_->deleteSequence(sequence_name);
361  }
362  catch (SequenceNotFoundException& snfe)
363  {
364  throw SequenceNotFoundException("Individual::deleteSequenceByName: sequence_name not found.", snfe.getSequenceId());
365  }
366 }
367 
368 /******************************************************************************/
369 
370 void Individual::deleteSequenceAtPosition(size_t sequence_position)
371 {
372  if (sequences_.get() == 0)
373  throw NullPointerException("Individual::deleteSequenceAtPosition: no sequence data.");
374  try
375  {
376  sequences_->deleteSequenceByKey(TextTools::toString(sequence_position));
377  }
378  catch (SequenceNotFoundException& snfe)
379  {
380  throw SequenceNotFoundException("Individual::deleteSequenceAtPosition: sequence_position not found.", snfe.getSequenceId());
381  }
382 }
383 
384 /******************************************************************************/
385 
386 std::vector<std::string> Individual::getSequencesNames() const
387 {
388  if (sequences_.get() == 0)
389  throw NullPointerException("Individual::getSequencesNames: no sequence data.");
390  return sequences_->getSequencesNames();
391 }
392 
393 /******************************************************************************/
394 
395 std::vector<size_t> Individual::getSequencesPositions() const
396 {
397  if (sequences_.get() == 0)
398  throw NullPointerException("Individual::getSequencesPositions: no sequence data.");
399  vector<size_t> seqpos;
400  vector<string> seqkeys = sequences_->getKeys();
401  for (size_t i = 0; i < seqkeys.size(); i++)
402  {
403  seqpos.push_back((size_t) TextTools::toInt(seqkeys[i]));
404  }
405  return seqpos;
406 }
407 
408 /******************************************************************************/
409 
410 size_t Individual::getSequencePosition(const std::string& sequence_name) const
411 {
412  if (sequences_.get() == 0)
413  throw NullPointerException("Individual::getSequencePosition: no sequence data.");
414  try
415  {
416  return (size_t) TextTools::toInt(sequences_->getKey(getSequencePosition(sequence_name)));
417  }
418  catch (SequenceNotFoundException& snfe)
419  {
420  throw SequenceNotFoundException("Individual::getSequencePosition: sequence_name not found.", snfe.getSequenceId());
421  }
422 }
423 
424 /******************************************************************************/
425 
427 {
428  return !(getNumberOfSequences() == 0);
429 }
430 
431 /******************************************************************************/
432 
433 bool Individual::hasSequenceAtPosition(size_t position) const
434 {
435  if (hasSequences())
436  {
437  vector<size_t> pos = getSequencesPositions();
438  for (size_t i = 0; i < pos.size(); i++)
439  {
440  if (pos[i] == position)
441  return true;
442  }
443  }
444  return false;
445 }
446 
447 /******************************************************************************/
448 
450 {
451  if (sequences_.get() == 0)
452  throw NullPointerException("Individual::getSequenceAlphabet: no sequence data.");
453  return sequences_->getAlphabet();
454 }
455 
456 /******************************************************************************/
457 
459 {
460  if (sequences_.get() == 0)
461  return 0;
462  return sequences_->getNumberOfSequences();
463 }
464 
465 /******************************************************************************/
466 
468 {
469  sequences_.reset(new MapSequenceContainer(msc));
470 }
471 
472 /******************************************************************************/
473 
475 {
476  if (sequences_.get() == 0)
477  throw NullPointerException("Individual::getSequences: no sequence data.");
478  return *sequences_;
479 }
480 
481 /******************************************************************************/
482 
483 // MultilocusGenotype
484 
486 {
487  genotype_.reset(new MultilocusGenotype(genotype));
488 }
489 
490 /******************************************************************************/
491 
492 void Individual::initGenotype(size_t loci_number)
493 {
494  if (hasGenotype())
495  throw Exception("Individual::initGenotype: individual already has a genotype.");
496  try
497  {
498  genotype_.reset(new MultilocusGenotype(loci_number));
499  }
500  catch (BadIntegerException& bie)
501  {
502  throw BadIntegerException("Individual::initGenotype: loci_number must be > 0.", bie.getBadInteger());
503  }
504 }
505 
506 /******************************************************************************/
507 
509 {
510  if (!hasGenotype())
511  throw NullPointerException("Individual::getGenotype: individual has no genotype.");
512  return *genotype_;
513 }
514 
515 /******************************************************************************/
516 
518 {
519  genotype_.reset();
520 }
521 
522 /******************************************************************************/
523 
525 {
526  return genotype_.get() != 0;
527 }
528 
529 /******************************************************************************/
530 
531 void Individual::setMonolocusGenotype(size_t locus_position, const MonolocusGenotype& monogen)
532 {
533  if (!hasGenotype())
534  throw NullPointerException("Individual::setMonolocusGenotype: individual has no genotype.");
535  try
536  {
537  genotype_->setMonolocusGenotype(locus_position, monogen);
538  }
539  catch (IndexOutOfBoundsException& ioobe)
540  {
541  throw IndexOutOfBoundsException("Individual::setMonolocusGenotype: locus_position out of boubds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
542  }
543 }
544 
545 /******************************************************************************/
546 
547 void Individual::setMonolocusGenotypeByAlleleKey(size_t locus_position, const std::vector<size_t> allele_keys)
548 {
549  if (!hasGenotype())
550  throw NullPointerException("Individual::setMonolocusGenotypeByAlleleKey: individual has no genotype.");
551  try
552  {
553  genotype_->setMonolocusGenotypeByAlleleKey(locus_position, allele_keys);
554  }
555  catch (IndexOutOfBoundsException& ioobe)
556  {
557  throw IndexOutOfBoundsException("Individual::setMonolocusGenotypeByAlleleKey: locus_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
558  }
559  catch (Exception&)
560  {
561  throw Exception("Individual::setMonolocusGenotypeByAlleleKey: no key in allele_keys.");
562  }
563 }
564 
565 /******************************************************************************/
566 
567 void Individual::setMonolocusGenotypeByAlleleId(size_t locus_position, const std::vector<std::string> allele_id, const LocusInfo& locus_info)
568 {
569  if (!hasGenotype())
570  throw NullPointerException("Individual::setMonolocusGenotypeByAlleleId: individual has no genotype.");
571  try
572  {
573  genotype_->setMonolocusGenotypeByAlleleId(locus_position, allele_id, locus_info);
574  }
575  catch (IndexOutOfBoundsException& ioobe)
576  {
577  throw IndexOutOfBoundsException("Individual::setMonolocusGenotypeByAlleleId: locus_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
578  }
579  catch (AlleleNotFoundException& anfe)
580  {
581  throw AlleleNotFoundException("Individual::setMonolocusGenotypeByAlleleId: id not found.", anfe.getIdentifier());
582  }
583 }
584 
585 /******************************************************************************/
586 
588 {
589  if (!hasGenotype())
590  throw NullPointerException("Individual::getMonolocusGenotype: individual has no genotype.");
591  try
592  {
593  return genotype_->getMonolocusGenotype(locus_position);
594  }
595  catch (IndexOutOfBoundsException& ioobe)
596  {
597  throw IndexOutOfBoundsException("Individual::getMonolocusGenotype: locus_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
598  }
599 }
600 
601 /******************************************************************************/
602 
604 {
605  if (!hasGenotype())
606  throw NullPointerException("Individual::countNonMissingLoci: individual has no genotype.");
607  return genotype_->countNonMissingLoci();
608 }
609 
610 /******************************************************************************/
611 
613 {
614  if (!hasGenotype())
615  throw NullPointerException("Individual::countHomozygousLoci: individual has no genotype.");
616  return genotype_->countHomozygousLoci();
617 }
618 
619 /******************************************************************************/
620 
622 {
623  if (!hasGenotype())
624  throw NullPointerException("Individual::countHeterozygousLoci: individual has no genotype.");
625  return genotype_->countHeterozygousLoci();
626 }
627 
628 /******************************************************************************/
629 
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 BadIdentifierException class.
virtual const Alphabet * getAlphabet() const=0
The Date class.
Definition: Date.h:57
const char * what() const noexcept override
std::size_t getBadIndex() const
const std::array< std::size_t, 2 > & getBounds() const
The Individual class.
Definition: Individual.h:76
double getY() const
Get the Y coordinate of the Individual.
Definition: Individual.cpp:262
std::unique_ptr< Point2D< double > > coord_
Definition: Individual.h:81
bool hasCoord() const
Tell if this Individual has coordinates.
Definition: Individual.cpp:225
const Sequence & getSequenceAtPosition(const size_t sequence_position) const
Get a sequence by its position.
Definition: Individual.cpp:338
std::vector< size_t > getSequencesPositions() const
Get the sequences' positions.
Definition: Individual.cpp:395
bool hasSequences() const
Tell if the Individual has some sequences.
Definition: Individual.cpp:426
virtual ~Individual()
Destroy an Individual.
Definition: Individual.cpp:113
void setMonolocusGenotypeByAlleleKey(size_t locus_position, const std::vector< size_t > allele_keys)
Set a MonolocusGenotype.
Definition: Individual.cpp:547
Individual()
Build a void new Individual.
Definition: Individual.cpp:47
unsigned short getSex() const
Get the sex of the Individual.
Definition: Individual.h:160
void setSex(const unsigned short sex)
Set the sex of the Individual.
Definition: Individual.cpp:168
std::unique_ptr< Date > date_
Definition: Individual.h:80
std::vector< std::string > getSequencesNames() const
Get the sequences' names.
Definition: Individual.cpp:386
size_t countNonMissingLoci() const
Count the number of non missing MonolocusGenotype.
Definition: Individual.cpp:603
void addSequence(size_t sequence_key, const Sequence &sequence)
Add a sequence to the Individual.
Definition: Individual.cpp:298
std::string id_
Definition: Individual.h:78
void setGenotype(const MultilocusGenotype &genotype)
Set a genotype.
Definition: Individual.cpp:485
void setMonolocusGenotypeByAlleleId(size_t locus_position, const std::vector< std::string > allele_id, const LocusInfo &locus_info)
Set a MonolocusGenotype.
Definition: Individual.cpp:567
std::unique_ptr< MapSequenceContainer > sequences_
Definition: Individual.h:83
void initGenotype(size_t loci_number)
Init the genotype.
Definition: Individual.cpp:492
const std::string & getId() const
Get the id of the Individual.
Definition: Individual.h:146
void setCoord(const Point2D< double > &coord)
Set the coodinates of the Individual.
Definition: Individual.cpp:201
void setY(const double y)
Set the Y coordinate of th Individual.
Definition: Individual.cpp:242
void setId(const std::string &id)
Set the id of the Individual.
Definition: Individual.cpp:160
bool hasGenotype() const
Tell if the Individual has a MultilocusGenotype.
Definition: Individual.cpp:524
Individual & operator=(const Individual &ind)
The Individual copy operator.
Definition: Individual.cpp:117
void deleteSequenceByName(const std::string &sequence_name)
Delete a sequence.
Definition: Individual.cpp:354
const Sequence & getSequenceByName(const std::string &sequence_name) const
Get a sequence by its name.
Definition: Individual.cpp:322
void setDate(const Date &date)
Set the date of the Individual.
Definition: Individual.cpp:176
const MultilocusGenotype & getGenotype() const
Get the genotype.
Definition: Individual.cpp:508
const OrderedSequenceContainer & getSequences() const
Get a reference to the sequence container.
Definition: Individual.cpp:474
const Locality< double > * getLocality() const
Get the locality of the Individual.
Definition: Individual.cpp:280
const Locality< double > * locality_
Definition: Individual.h:82
bool hasSequenceAtPosition(size_t position) const
Tell if the Individual has a sequence at a given position.
Definition: Individual.cpp:433
size_t countHomozygousLoci() const
Count the number of homozygous MonolocusGenotype.
Definition: Individual.cpp:612
void deleteSequenceAtPosition(size_t sequence_position)
Delete a sequence.
Definition: Individual.cpp:370
void setMonolocusGenotype(size_t locus_position, const MonolocusGenotype &monogen)
Set a MonolocusGenotype.
Definition: Individual.cpp:531
void setX(const double x)
Set the X coordinate of the Individual.
Definition: Individual.cpp:232
void setLocality(const Locality< double > *locality)
Set the locality of the Individual.
Definition: Individual.cpp:273
bool hasDate() const
Tell if this Individual has a date.
Definition: Individual.cpp:193
const MonolocusGenotype & getMonolocusGenotype(size_t locus_position)
Get a MonolocusGenotype.
Definition: Individual.cpp:587
void setSequences(const MapSequenceContainer &msc)
Set all the sequences with a MapSequenceContainer.
Definition: Individual.cpp:467
size_t getSequencePosition(const std::string &sequence_name) const
Get the position of a sequence.
Definition: Individual.cpp:410
void deleteGenotype()
Delete the genotype of the individual.
Definition: Individual.cpp:517
std::unique_ptr< MultilocusGenotype > genotype_
Definition: Individual.h:84
unsigned short sex_
Definition: Individual.h:79
size_t countHeterozygousLoci() const
Count the number of heterozygous MonolocusGenotype.
Definition: Individual.cpp:621
double getX() const
Get the X coordinate of the Individual.
Definition: Individual.cpp:252
const Date & getDate() const
Get the date of the Individual.
Definition: Individual.cpp:183
const Alphabet * getSequenceAlphabet() const
Return the alphabet of the sequences.
Definition: Individual.cpp:449
size_t getNumberOfSequences() const
Get the number of sequences.
Definition: Individual.cpp:458
const Point2D< double > & getCoord() const
Get the coordinates of the Induvidual.
Definition: Individual.cpp:215
bool hasLocality() const
Tell if this Individual has a locality.
Definition: Individual.cpp:290
The LocusInfo class.
Definition: LocusInfo.h:64
The MonolocusGenotype virtual class.
The MultilocusGenotype class.
virtual const std::string getSequenceId() const
virtual const std::string & getName() const=0
int toInt(const std::string &s, char scientificNotation='e')
std::string toString(T t)