bpp-popgen  2.2.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. CNRS, (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_(0),
50  coord_(0),
51  locality_(0),
52  sequences_(0),
53  genotype_(0) {}
54 
55 Individual::Individual(const std::string& id) : id_(id),
56  sex_(0),
57  date_(0),
58  coord_(0),
59  locality_(0),
60  sequences_(0),
61  genotype_(0) {}
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_(0),
74  genotype_(0) {}
75 
76 Individual::Individual(const Individual& ind) : id_(ind.getId()),
77  sex_(ind.getSex()),
78  date_(0),
79  coord_(0),
80  locality_(0),
81  sequences_(0),
82  genotype_(0)
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 throw (NullPointerException)
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
201 void Individual::setCoord(const Point2D<double>& 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 
215 const Point2D<double>& Individual::getCoord() const throw (NullPointerException)
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) throw (NullPointerException)
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) throw (NullPointerException)
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 throw (NullPointerException)
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 throw (NullPointerException)
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 
280 const Locality<double>* Individual::getLocality() const throw (NullPointerException)
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 throw (Exception)
300 {
301  if (sequences_.get() == 0)
302  sequences_.reset(new MapSequenceContainer(sequence.getAlphabet()));
303  try
304  {
305  sequences_->addSequence(TextTools::toString(sequence_key), sequence);
306  }
307  catch (AlphabetMismatchException& ame)
308  {
309  throw (AlphabetMismatchException("Individual::addSequence: alphabets don't match.", ame.getAlphabets()[0], ame.getAlphabets()[1]));
310  }
311  catch (Exception& e)
312  {
313  if (string(e.what()).find("name") < string(e.what()).size())
314  throw (BadIdentifierException("Individual::addSequence: sequence's name already in use.", sequence.getName()));
315  // if (string(e.what()).find("key") < string(e.what()).size())
316  else
317  throw (Exception("Individual::addSequence: sequence_key already in use:" + TextTools::toString(sequence_key)));
318  }
319 }
320 
321 /******************************************************************************/
322 
323 const Sequence& Individual::getSequenceByName(const std::string& sequence_name)
324 const throw (Exception)
325 {
326  if (sequences_.get() == 0)
327  throw NullPointerException("Individual::getSequenceByName: no sequence data.");
328  try
329  {
330  return sequences_->getSequence(sequence_name);
331  }
332  catch (SequenceNotFoundException& snfe)
333  {
334  throw SequenceNotFoundException("Individual::getSequenceByName: sequence_name not found.", snfe.getSequenceId());
335  }
336 }
337 
338 /******************************************************************************/
339 
340 const Sequence& Individual::getSequenceAtPosition(size_t sequence_position)
341 const throw (Exception)
342 {
343  if (sequences_.get() == 0)
344  throw NullPointerException("Individual::getSequenceAtPosition: no sequence data.");
345  try
346  {
347  return sequences_->getSequenceByKey(TextTools::toString(sequence_position));
348  }
349  catch (SequenceNotFoundException& snfe)
350  {
351  throw SequenceNotFoundException("Individual::getSequenceAtPosition: sequence_position not found", snfe.getSequenceId());
352  }
353 }
354 
355 /******************************************************************************/
356 
357 void Individual::deleteSequenceByName(const std::string& sequence_name) throw (Exception)
358 {
359  if (sequences_.get() == 0)
360  throw NullPointerException("Individual::deleteSequenceByName: no sequence data.");
361  try
362  {
363  sequences_->deleteSequence(sequence_name);
364  }
365  catch (SequenceNotFoundException& snfe)
366  {
367  throw SequenceNotFoundException("Individual::deleteSequenceByName: sequence_name not found.", snfe.getSequenceId());
368  }
369 }
370 
371 /******************************************************************************/
372 
373 void Individual::deleteSequenceAtPosition(size_t sequence_position) throw (Exception)
374 {
375  if (sequences_.get() == 0)
376  throw NullPointerException("Individual::deleteSequenceAtPosition: no sequence data.");
377  try
378  {
379  sequences_->deleteSequenceByKey(TextTools::toString(sequence_position));
380  }
381  catch (SequenceNotFoundException& snfe)
382  {
383  throw SequenceNotFoundException("Individual::deleteSequenceAtPosition: sequence_position not found.", snfe.getSequenceId());
384  }
385 }
386 
387 /******************************************************************************/
388 
389 std::vector<std::string> Individual::getSequencesNames() const throw (NullPointerException)
390 {
391  if (sequences_.get() == 0)
392  throw NullPointerException("Individual::getSequencesNames: no sequence data.");
393  return sequences_->getSequencesNames();
394 }
395 
396 /******************************************************************************/
397 
398 std::vector<size_t> Individual::getSequencesPositions() const throw (NullPointerException)
399 {
400  if (sequences_.get() == 0)
401  throw NullPointerException("Individual::getSequencesPositions: no sequence data.");
402  vector<size_t> seqpos;
403  vector<string> seqkeys = sequences_->getKeys();
404  for (size_t i = 0; i < seqkeys.size(); i++)
405  {
406  seqpos.push_back((size_t) TextTools::toInt(seqkeys[i]));
407  }
408  return seqpos;
409 }
410 
411 /******************************************************************************/
412 
413 size_t Individual::getSequencePosition(const std::string& sequence_name) const throw (Exception)
414 {
415  if (sequences_.get() == 0)
416  throw NullPointerException("Individual::getSequencePosition: no sequence data.");
417  try
418  {
419  return (size_t) TextTools::toInt(sequences_->getKey(getSequencePosition(sequence_name)));
420  }
421  catch (SequenceNotFoundException& snfe)
422  {
423  throw SequenceNotFoundException("Individual::getSequencePosition: sequence_name not found.", snfe.getSequenceId());
424  }
425 }
426 
427 /******************************************************************************/
428 
430 {
431  return !(getNumberOfSequences() == 0);
432 }
433 
434 /******************************************************************************/
435 
436 bool Individual::hasSequenceAtPosition(size_t position) const
437 {
438  if (hasSequences())
439  {
440  vector<size_t> pos = getSequencesPositions();
441  for (size_t i = 0; i < pos.size(); i++)
442  {
443  if (pos[i] == position)
444  return true;
445  }
446  }
447  return false;
448 }
449 
450 /******************************************************************************/
451 
452 const Alphabet* Individual::getSequenceAlphabet() const throw (NullPointerException)
453 {
454  if (sequences_.get() == 0)
455  throw NullPointerException("Individual::getSequenceAlphabet: no sequence data.");
456  return sequences_->getAlphabet();
457 }
458 
459 /******************************************************************************/
460 
462 {
463  if (sequences_.get() == 0)
464  return 0;
465  return sequences_->getNumberOfSequences();
466 }
467 
468 /******************************************************************************/
469 
470 void Individual::setSequences(const MapSequenceContainer& msc)
471 {
472  sequences_.reset(new MapSequenceContainer(msc));
473 }
474 
475 /******************************************************************************/
476 
477 const OrderedSequenceContainer& Individual::getSequences() const throw (NullPointerException)
478 {
479  if (sequences_.get() == 0)
480  throw NullPointerException("Individual::getSequences: no sequence data.");
481  return *sequences_;
482 }
483 
484 /******************************************************************************/
485 
486 // MultilocusGenotype
487 
489 {
490  genotype_.reset(new MultilocusGenotype(genotype));
491 }
492 
493 /******************************************************************************/
494 
495 void Individual::initGenotype(size_t loci_number) throw (Exception)
496 {
497  if (hasGenotype())
498  throw Exception("Individual::initGenotype: individual already has a genotype.");
499  try
500  {
501  genotype_.reset(new MultilocusGenotype(loci_number));
502  }
503  catch (BadIntegerException& bie)
504  {
505  throw BadIntegerException("Individual::initGenotype: loci_number must be > 0.", bie.getBadInteger());
506  }
507 }
508 
509 /******************************************************************************/
510 
511 const MultilocusGenotype& Individual::getGenotype() const throw (NullPointerException)
512 {
513  if (!hasGenotype())
514  throw NullPointerException("Individual::getGenotype: individual has no genotype.");
515  return *genotype_;
516 }
517 
518 /******************************************************************************/
519 
521 {
522  genotype_.reset();
523 }
524 
525 /******************************************************************************/
526 
528 {
529  return genotype_.get() != 0;
530 }
531 
532 /******************************************************************************/
533 
534 void Individual::setMonolocusGenotype(size_t locus_position, const MonolocusGenotype& monogen) throw (Exception)
535 {
536  if (!hasGenotype())
537  throw NullPointerException("Individual::setMonolocusGenotype: individual has no genotype.");
538  try
539  {
540  genotype_->setMonolocusGenotype(locus_position, monogen);
541  }
542  catch (IndexOutOfBoundsException& ioobe)
543  {
544  throw IndexOutOfBoundsException("Individual::setMonolocusGenotype: locus_position out of boubds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
545  }
546 }
547 
548 /******************************************************************************/
549 
550 void Individual::setMonolocusGenotypeByAlleleKey(size_t locus_position, const std::vector<size_t> allele_keys) throw (Exception)
551 {
552  if (!hasGenotype())
553  throw NullPointerException("Individual::setMonolocusGenotypeByAlleleKey: individual has no genotype.");
554  try
555  {
556  genotype_->setMonolocusGenotypeByAlleleKey(locus_position, allele_keys);
557  }
558  catch (IndexOutOfBoundsException& ioobe)
559  {
560  throw IndexOutOfBoundsException("Individual::setMonolocusGenotypeByAlleleKey: locus_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
561  }
562  catch (Exception)
563  {
564  throw Exception("Individual::setMonolocusGenotypeByAlleleKey: no key in allele_keys.");
565  }
566 }
567 
568 /******************************************************************************/
569 
570 void Individual::setMonolocusGenotypeByAlleleId(size_t locus_position, const std::vector<std::string> allele_id, const LocusInfo& locus_info) throw (Exception)
571 {
572  if (!hasGenotype())
573  throw NullPointerException("Individual::setMonolocusGenotypeByAlleleId: individual has no genotype.");
574  try
575  {
576  genotype_->setMonolocusGenotypeByAlleleId(locus_position, allele_id, locus_info);
577  }
578  catch (IndexOutOfBoundsException& ioobe)
579  {
580  throw IndexOutOfBoundsException("Individual::setMonolocusGenotypeByAlleleId: locus_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
581  }
582  catch (AlleleNotFoundException& anfe)
583  {
584  throw AlleleNotFoundException("Individual::setMonolocusGenotypeByAlleleId: id not found.", anfe.getIdentifier());
585  }
586 }
587 
588 /******************************************************************************/
589 
590 const MonolocusGenotype& Individual::getMonolocusGenotype(size_t locus_position) throw (Exception)
591 {
592  if (!hasGenotype())
593  throw NullPointerException("Individual::getMonolocusGenotype: individual has no genotype.");
594  try
595  {
596  return genotype_->getMonolocusGenotype(locus_position);
597  }
598  catch (IndexOutOfBoundsException& ioobe)
599  {
600  throw IndexOutOfBoundsException("Individual::getMonolocusGenotype: locus_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
601  }
602 }
603 
604 /******************************************************************************/
605 
606 size_t Individual::countNonMissingLoci() const throw (NullPointerException)
607 {
608  if (!hasGenotype())
609  throw NullPointerException("Individual::countNonMissingLoci: individual has no genotype.");
610  return genotype_->countNonMissingLoci();
611 }
612 
613 /******************************************************************************/
614 
615 size_t Individual::countHomozygousLoci() const throw (NullPointerException)
616 {
617  if (!hasGenotype())
618  throw NullPointerException("Individual::countHomozygousLoci: individual has no genotype.");
619  return genotype_->countHomozygousLoci();
620 }
621 
622 /******************************************************************************/
623 
624 size_t Individual::countHeterozygousLoci() const throw (NullPointerException)
625 {
626  if (!hasGenotype())
627  throw NullPointerException("Individual::countHeterozygousLoci: individual has no genotype.");
628  return genotype_->countHeterozygousLoci();
629 }
630 
631 /******************************************************************************/
632 
virtual ~Individual()
Destroy an Individual.
Definition: Individual.cpp:113
size_t getNumberOfSequences() const
Get the number of sequences.
Definition: Individual.cpp:461
void setSex(const unsigned short sex)
Set the sex of the Individual.
Definition: Individual.cpp:168
std::string id_
Definition: Individual.h:78
void setDate(const Date &date)
Set the date of the Individual.
Definition: Individual.cpp:176
const Point2D< double > & getCoord() const
Get the coordinates of the Induvidual.
Definition: Individual.cpp:215
void initGenotype(size_t loci_number)
Init the genotype.
Definition: Individual.cpp:495
double getX() const
Get the X coordinate of the Individual.
Definition: Individual.cpp:252
const Alphabet * getSequenceAlphabet() const
Return the alphabet of the sequences.
Definition: Individual.cpp:452
bool hasCoord() const
Tell if this Individual has coordinates.
Definition: Individual.cpp:225
void setX(const double x)
Set the X coordinate of the Individual.
Definition: Individual.cpp:232
bool hasLocality() const
Tell if this Individual has a locality.
Definition: Individual.cpp:290
std::vector< std::string > getSequencesNames() const
Get the sequences&#39; names.
Definition: Individual.cpp:389
void setSequences(const MapSequenceContainer &msc)
Set all the sequences with a MapSequenceContainer.
Definition: Individual.cpp:470
virtual const std::string getIdentifier() const
Return the value of the identifier as a string.
const Locality< double > * locality_
Definition: Individual.h:82
const MonolocusGenotype & getMonolocusGenotype(size_t locus_position)
Get a MonolocusGenotype.
Definition: Individual.cpp:590
Individual()
Build a void new Individual.
Definition: Individual.cpp:47
The BadIdentifierException class.
STL namespace.
void setId(const std::string &id)
Set the id of the Individual.
Definition: Individual.cpp:160
The Individual class.
Definition: Individual.h:75
const Sequence & getSequenceByName(const std::string &sequence_name) const
Get a sequence by its name.
Definition: Individual.cpp:323
The MultilocusGenotype class.
The Date class.
Definition: Date.h:56
const Locality< double > * getLocality() const
Get the locality of the Individual.
Definition: Individual.cpp:280
bool hasGenotype() const
Tell if the Individual has a MultilocusGenotype.
Definition: Individual.cpp:527
The AlleleNotFoundException class.
std::vector< size_t > getSequencesPositions() const
Get the sequences&#39; positions.
Definition: Individual.cpp:398
bool hasSequences() const
Tell if the Individual has some sequences.
Definition: Individual.cpp:429
size_t getSequencePosition(const std::string &sequence_name) const
Get the position of a sequence.
Definition: Individual.cpp:413
bool hasDate() const
Tell if this Individual has a date.
Definition: Individual.cpp:193
void setMonolocusGenotypeByAlleleKey(size_t locus_position, const std::vector< size_t > allele_keys)
Set a MonolocusGenotype.
Definition: Individual.cpp:550
const std::string & getId() const
Get the id of the Individual.
Definition: Individual.h:146
void setMonolocusGenotypeByAlleleId(size_t locus_position, const std::vector< std::string > allele_id, const LocusInfo &locus_info)
Set a MonolocusGenotype.
Definition: Individual.cpp:570
std::auto_ptr< Point2D< double > > coord_
Definition: Individual.h:81
std::auto_ptr< Date > date_
Definition: Individual.h:80
void setGenotype(const MultilocusGenotype &genotype)
Set a genotype.
Definition: Individual.cpp:488
size_t countHeterozygousLoci() const
Count the number of heterozygous MonolocusGenotype.
Definition: Individual.cpp:624
unsigned short sex_
Definition: Individual.h:79
Individual & operator=(const Individual &ind)
The Individual copy operator.
Definition: Individual.cpp:117
bool hasSequenceAtPosition(size_t position) const
Tell if the Individual has a sequence at a given position.
Definition: Individual.cpp:436
const Sequence & getSequenceAtPosition(const size_t sequence_position) const
Get a sequence by its position.
Definition: Individual.cpp:340
size_t countHomozygousLoci() const
Count the number of homozygous MonolocusGenotype.
Definition: Individual.cpp:615
void deleteGenotype()
Delete the genotype of the individual.
Definition: Individual.cpp:520
void deleteSequenceByName(const std::string &sequence_name)
Delete a sequence.
Definition: Individual.cpp:357
std::auto_ptr< MultilocusGenotype > genotype_
Definition: Individual.h:84
const Date & getDate() const
Get the date of the Individual.
Definition: Individual.cpp:183
The MonolocusGenotype virtual class.
void setCoord(const Point2D< double > &coord)
Set the coodinates of the Individual.
Definition: Individual.cpp:201
The LocusInfo class.
Definition: LocusInfo.h:63
unsigned short getSex() const
Get the sex of the Individual.
Definition: Individual.h:160
void setLocality(const Locality< double > *locality)
Set the locality of the Individual.
Definition: Individual.cpp:273
const OrderedSequenceContainer & getSequences() const
Get a reference to the sequence container.
Definition: Individual.cpp:477
const MultilocusGenotype & getGenotype() const
Get the genotype.
Definition: Individual.cpp:511
void addSequence(size_t sequence_key, const Sequence &sequence)
Add a sequence to the Individual.
Definition: Individual.cpp:298
void setMonolocusGenotype(size_t locus_position, const MonolocusGenotype &monogen)
Set a MonolocusGenotype.
Definition: Individual.cpp:534
double getY() const
Get the Y coordinate of the Individual.
Definition: Individual.cpp:262
void setY(const double y)
Set the Y coordinate of th Individual.
Definition: Individual.cpp:242
std::auto_ptr< MapSequenceContainer > sequences_
Definition: Individual.h:83
void deleteSequenceAtPosition(size_t sequence_position)
Delete a sequence.
Definition: Individual.cpp:373
size_t countNonMissingLoci() const
Count the number of non missing MonolocusGenotype.
Definition: Individual.cpp:606