bpp-popgen  3.0.0
MultilocusGenotype.cpp
Go to the documentation of this file.
1 //
2 // File MultilocusGenotype.cpp
3 // Author : Sylvain Gaillard <sylvain.gaillard@angers.inra.fr>
4 // Last modification : April 4, 2008
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 "MultilocusGenotype.h"
41 
42 using namespace bpp;
43 using namespace std;
44 
45 // ** Class constructor: *******************************************************/
46 
47 MultilocusGenotype::MultilocusGenotype(size_t loci_number) : loci_(vector<MonolocusGenotype*>(loci_number))
48 {
49  if (loci_number < 1)
50  throw BadIntegerException("MultilocusGenotype::MultilocusGenotype: loci_number must be > 0.", static_cast<int>(loci_number));
51 
52  // Set all the loci_ pointers to nullptr
53  for (size_t i = 0; i < loci_number; i++)
54  {
55  loci_[i] = 0;
56  }
57 }
58 
59 MultilocusGenotype::MultilocusGenotype(const MultilocusGenotype& genotype) : loci_(vector<MonolocusGenotype*>(genotype.size()))
60 {
61  for (size_t i = 0; i < genotype.size(); i++)
62  {
63  if (!genotype.isMonolocusGenotypeMissing(i))
64  loci_[i] = dynamic_cast<MonolocusGenotype*>(genotype.getMonolocusGenotype(i).clone());
65  else
66  loci_[i] = 0;
67  }
68 }
69 
70 // ** Class destructor: *******************************************************/
71 
73 {
74  for (size_t i = 0; i < loci_.size(); i++)
75  {
76  delete loci_[i];
77  }
78  loci_.clear();
79 }
80 
81 // ** Other methodes: *********************************************************/
82 
83 void MultilocusGenotype::setMonolocusGenotype(size_t locus_position,
84  const MonolocusGenotype& monogen)
85 {
86  if (locus_position < loci_.size())
87  loci_[locus_position] = dynamic_cast<MonolocusGenotype*>(monogen.clone());
88  else
89  throw IndexOutOfBoundsException("MultilocusGenotype::setMonolocusGenotype: locus_position out of bounds.",
90  locus_position, 0, loci_.size());
91 }
92 
94  const std::vector<size_t>& allele_keys)
95 {
96  if (allele_keys.size() < 1)
97  throw Exception("MultilocusGenotype::setMonolocusGenotypeByAlleleKey: no key in allele_keys.");
98 
99  if (locus_position < loci_.size())
100  {
102  }
103  else
104  throw IndexOutOfBoundsException("MultilocusGenotype::setMonolocusGenotype: locus_position out of bounds.",
105  locus_position, 0, loci_.size());
106 }
107 
109  const std::vector<std::string>& allele_id, const LocusInfo& locus_info)
110 {
111  vector<size_t> allele_keys;
112  for (size_t i = 0; i < allele_id.size(); i++)
113  {
114  try
115  {
116  allele_keys.push_back(locus_info.getAlleleInfoKey(allele_id[i]));
117  }
118  catch (AlleleNotFoundException& anfe)
119  {
120  throw AlleleNotFoundException("MultilocusGenotype::setMonolocusGenotypeByAlleleId: id not found.", anfe.getIdentifier());
121  }
122  }
123  try
124  {
125  setMonolocusGenotypeByAlleleKey(locus_position, allele_keys);
126  }
127  catch (IndexOutOfBoundsException& ioobe)
128  {
129  throw IndexOutOfBoundsException("MultilocusGenotype::setMonolocusGenotypeByAlleleId: locus_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
130  }
131 }
132 
134 {
135  if (locus_position >= loci_.size())
136  throw IndexOutOfBoundsException("MultilocusGenotype::setMonolocusGenotypeAsMissing: locus_position out of bounds.", locus_position, 0, loci_.size());
137  if (loci_[locus_position] != NULL)
138  delete loci_[locus_position];
139  loci_[locus_position] = NULL;
140 }
141 
142 bool MultilocusGenotype::isMonolocusGenotypeMissing(size_t locus_position) const
143 {
144  if (locus_position >= loci_.size())
145  throw IndexOutOfBoundsException("MultilocusGenotype::isMonolocusGenotypeMissing: locus_position out of bounds.", locus_position, 0, loci_.size());
146  return loci_[locus_position] == NULL;
147 }
148 
150 {
151  if (locus_position >= loci_.size())
152  throw IndexOutOfBoundsException("MultilocusGenotype::getMonolocusGenotype: locus_position out of bounds", locus_position, 0, loci_.size());
153  return *loci_[locus_position];
154 }
155 
157 {
158  return loci_.size();
159 }
160 
162 {
163  size_t count = 0;
164  for (size_t i = 0; i < loci_.size(); i++)
165  {
166  if (loci_[i] != NULL)
167  count++;
168  }
169  return count;
170 }
171 
173 {
174  size_t count = 0;
175  for (size_t i = 0; i < loci_.size(); i++)
176  {
177  try
178  {
179  if (dynamic_cast<BiAlleleMonolocusGenotype*>(loci_[i])->isHomozygous())
180  count++;
181  }
182  catch (...)
183  {}
184  }
185  return count;
186 }
187 
189 {
190  size_t count = 0;
191  for (size_t i = 0; i < loci_.size(); i++)
192  {
193  try
194  {
195  if (!(dynamic_cast<BiAlleleMonolocusGenotype*>(loci_[i])->isHomozygous()))
196  count++;
197  }
198  catch (...)
199  {}
200  }
201  return count;
202 }
203 
The AlleleNotFoundException class.
virtual const std::string getIdentifier() const
Return the value of the identifier as a string.
The BiAlleleMonolocusGenotype class.
virtual Clonable * clone() const=0
std::size_t getBadIndex() const
const std::array< std::size_t, 2 > & getBounds() const
The LocusInfo class.
Definition: LocusInfo.h:64
unsigned int getAlleleInfoKey(const std::string &id) const
Get the position of an AlleleInfo.
Definition: LocusInfo.cpp:111
static std::unique_ptr< MonolocusGenotype > buildMonolocusGenotypeByAlleleKey(const std::vector< size_t > allele_keys)
Build a proper MonolocusGenotype accordig to the number of alleles.
The MonolocusGenotype virtual class.
The MultilocusGenotype class.
void setMonolocusGenotypeAsMissing(size_t locus_position)
Set a MonolocusGenotype as missing data.
void setMonolocusGenotype(size_t locus_position, const MonolocusGenotype &monogen)
Set a MonolocusGenotype.
~MultilocusGenotype()
Destroy a MultilocusGenotype.
void setMonolocusGenotypeByAlleleKey(size_t locus_position, const std::vector< size_t > &allele_keys)
Set a MonolocusGenotype by allele keys.
MultilocusGenotype(size_t loci_number)
Build a MultilocusGenotype linked to an AnalyzedLoci object.
const MonolocusGenotype & getMonolocusGenotype(size_t locus_position) const
Get a MonolocusGenotype.
size_t countHeterozygousLoci() const
Count the number of heterozygous MonolocusGenotype.
std::vector< MonolocusGenotype * > loci_
size_t countHomozygousLoci() const
Count the number of homozygous MonolocusGenotype.
bool isMonolocusGenotypeMissing(size_t locus_position) const
Tell if a MonolocusGenotype is a missing data.
size_t countNonMissingLoci() const
Count the number of non missing MonolocusGenotype.
size_t size() const
Count the number of loci.
void setMonolocusGenotypeByAlleleId(size_t locus_position, const std::vector< std::string > &allele_id, const LocusInfo &locus_info)
Set a MonolocusGenotype by allele id.
std::size_t count(const std::string &s, const std::string &pattern)