bpp-popgen3  3.0.0
MultilocusGenotype.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "MultilocusGenotype.h"
6 
7 using namespace bpp;
8 using namespace std;
9 
10 // ** Class constructor: *******************************************************/
11 
13  loci_(loci_number)
14 {
15  if (loci_number < 1)
16  throw BadIntegerException("MultilocusGenotype::MultilocusGenotype: loci_number must be > 0.", static_cast<int>(loci_number));
17 
18  // Set all the loci_ pointers to nullptr
19  for (size_t i = 0; i < loci_number; ++i)
20  {
21  loci_[i] = nullptr;
22  }
23 }
24 
26  loci_(genotype.size())
27 {
28  for (size_t i = 0; i < genotype.size(); ++i)
29  {
30  if (!genotype.isMonolocusGenotypeMissing(i))
31  loci_[i].reset(genotype.monolocusGenotype(i).clone());
32  else
33  loci_[i] = nullptr;
34  }
35 }
36 
37 // ** Class destructor: *******************************************************/
38 
40 {
41  loci_.clear();
42 }
43 
44 // ** Other methodes: *********************************************************/
45 
47  size_t locusPosition,
48  const MonolocusGenotypeInterface& monogen)
49 {
50  if (locusPosition < loci_.size())
51  loci_[locusPosition].reset(monogen.clone());
52  else
53  throw IndexOutOfBoundsException("MultilocusGenotype::setMonolocusGenotype: locusPosition out of bounds.", locusPosition, 0, loci_.size());
54 }
55 
57  size_t locusPosition,
58  const std::vector<size_t>& alleleKeys)
59 {
60  if (alleleKeys.size() < 1)
61  throw Exception("MultilocusGenotype::setMonolocusGenotypeByAlleleKey: no key in alleleKeys.");
62 
63  if (locusPosition < loci_.size())
64  {
66  }
67  else
68  throw IndexOutOfBoundsException("MultilocusGenotype::setMonolocusGenotype: locusPosition out of bounds.", locusPosition, 0, loci_.size());
69 }
70 
72  size_t locusPosition,
73  const vector<string>& alleleId,
74  const LocusInfo& locusInfo)
75 {
76  vector<size_t> alleleKeys;
77  for (size_t i = 0; i < alleleId.size(); i++)
78  {
79  try
80  {
81  alleleKeys.push_back(locusInfo.getAlleleInfoKey(alleleId[i]));
82  }
83  catch (AlleleNotFoundException& anfe)
84  {
85  throw AlleleNotFoundException("MultilocusGenotype::setMonolocusGenotypeByAlleleId: id not found.", anfe.getIdentifier());
86  }
87  }
88  try
89  {
90  setMonolocusGenotypeByAlleleKey(locusPosition, alleleKeys);
91  }
92  catch (IndexOutOfBoundsException& ioobe)
93  {
94  throw IndexOutOfBoundsException("MultilocusGenotype::setMonolocusGenotypeByAlleleId: locusPosition out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
95  }
96 }
97 
99 {
100  if (locusPosition >= loci_.size())
101  throw IndexOutOfBoundsException("MultilocusGenotype::setMonolocusGenotypeAsMissing: locusPosition out of bounds.", locusPosition, 0, loci_.size());
102  loci_[locusPosition] = nullptr;
103 }
104 
105 bool MultilocusGenotype::isMonolocusGenotypeMissing(size_t locusPosition) const
106 {
107  if (locusPosition >= loci_.size())
108  throw IndexOutOfBoundsException("MultilocusGenotype::isMonolocusGenotypeMissing: locusPosition out of bounds.", locusPosition, 0, loci_.size());
109  return loci_[locusPosition] == nullptr;
110 }
111 
113 {
114  if (locusPosition >= loci_.size())
115  throw IndexOutOfBoundsException("MultilocusGenotype::getMonolocusGenotype: locusPosition out of bounds", locusPosition, 0, loci_.size());
116  return *loci_[locusPosition];
117 }
118 
120 {
121  return loci_.size();
122 }
123 
125 {
126  size_t count = 0;
127  for (size_t i = 0; i < loci_.size(); ++i)
128  {
129  if (loci_[i])
130  count++;
131  }
132  return count;
133 }
134 
136 {
137  size_t count = 0;
138  for (size_t i = 0; i < loci_.size(); ++i)
139  {
140  try
141  {
142  if (dynamic_cast<BiAlleleMonolocusGenotype&>(*loci_[i]).isHomozygous())
143  count++;
144  }
145  catch (...)
146  {}
147  }
148  return count;
149 }
150 
152 {
153  size_t count = 0;
154  for (size_t i = 0; i < loci_.size(); i++)
155  {
156  try
157  {
158  if (!(dynamic_cast<BiAlleleMonolocusGenotype&>(*loci_[i]).isHomozygous()))
159  count++;
160  }
161  catch (...)
162  {}
163  }
164  return count;
165 }
The AlleleNotFoundException class.
virtual const std::string getIdentifier() const
Return the value of the identifier as a string.
The BiAlleleMonolocusGenotype class.
std::size_t getBadIndex() const
const std::array< std::size_t, 2 > & getBounds() const
The LocusInfo class.
Definition: LocusInfo.h:31
unsigned int getAlleleInfoKey(const std::string &id) const
Get the position of an AlleleInfo.
Definition: LocusInfo.cpp:49
The MonolocusGenotype virtual class.
MonolocusGenotypeInterface * clone() const override=0
static std::unique_ptr< MonolocusGenotypeInterface > buildMonolocusGenotypeByAlleleKey(const std::vector< size_t > alleleKeys)
Build a proper MonolocusGenotype accordig to the number of alleles.
The MultilocusGenotype class.
const MonolocusGenotypeInterface & monolocusGenotype(size_t locusPosition) const
Get a MonolocusGenotype.
void setMonolocusGenotypeByAlleleId(size_t locusPosition, const std::vector< std::string > &alleleId, const LocusInfo &locusInfo)
Set a MonolocusGenotype by allele id.
virtual ~MultilocusGenotype()
Destroy a MultilocusGenotype.
MultilocusGenotype(size_t lociNumber)
Build a MultilocusGenotype linked to an AnalyzedLoci object.
bool isMonolocusGenotypeMissing(size_t locusPosition) const
Tell if a MonolocusGenotype is a missing data.
void setMonolocusGenotypeByAlleleKey(size_t locusPosition, const std::vector< size_t > &alleleKeys)
Set a MonolocusGenotype by allele keys.
size_t countHeterozygousLoci() const
Count the number of heterozygous MonolocusGenotype.
size_t countHomozygousLoci() const
Count the number of homozygous MonolocusGenotype.
void setMonolocusGenotypeAsMissing(size_t locusPosition)
Set a MonolocusGenotype as missing data.
void setMonolocusGenotype(size_t locusPosition, const MonolocusGenotypeInterface &monogen)
Set a MonolocusGenotype.
size_t countNonMissingLoci() const
Count the number of non missing MonolocusGenotype.
std::vector< std::unique_ptr< MonolocusGenotypeInterface > > loci_
size_t size() const
Count the number of loci.
std::size_t count(const std::string &s, const std::string &pattern)