bpp-popgen3  3.0.0
AnalyzedLoci.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 "AnalyzedLoci.h"
6 
7 using namespace bpp;
8 using namespace std;
9 
10 /******************************************************************************/
11 
13  size_t locusPosition,
14  const LocusInfo& locus)
15 {
16  if (locusPosition < loci_.size())
17  loci_[locusPosition].reset(locus.clone());
18  else
19  throw IndexOutOfBoundsException("AnalyzedLoci::setLocusInfo: locus_position out of bounds",
20  locusPosition, 0, loci_.size());
21 }
22 
23 /******************************************************************************/
24 
26  const std::string& locusName) const
27 {
28  for (size_t i = 0; i < loci_.size(); ++i)
29  {
30  if (loci_[i] && loci_[i]->getName() == locusName)
31  return i;
32  }
33  throw BadIdentifierException("AnalyzedLoci::getLocusInfoPosition: locus not found.", locusName);
34 }
35 
36 /******************************************************************************/
37 
39  const std::string& locusName) const
40 {
41  for (const auto& locus : loci_)
42  {
43  if (locus && locus->getName() == locusName)
44  return *locus;
45  }
46  throw BadIdentifierException("AnalyzedLoci::getLocusInfo: locus not found.",
47  locusName);
48 }
49 
50 /******************************************************************************/
51 
53  size_t locusPosition) const
54 {
55  if (locusPosition >= loci_.size())
56  throw IndexOutOfBoundsException("AnalyzedLoci::getLocusInfoAtPosition: locus_position out of bounds.", locusPosition, 0, loci_.size());
57  if (loci_[locusPosition])
58  return *loci_[locusPosition];
59  else
60  throw NullPointerException("AnalyzedLoci::getLocusInfo: no locus defined here.");
61 }
62 
63 /******************************************************************************/
64 
65 // AlleleInfo
66 void AnalyzedLoci::addAlleleInfoByLocusName(const std::string& locusName,
67  const AlleleInfo& allele)
68 {
69  bool locusFound = false;
70  for (auto& locus : loci_)
71  {
72  if (locus->getName() == locusName)
73  {
74  locusFound = true;
75  try
76  {
77  locus->addAlleleInfo(allele);
78  }
79  catch (BadIdentifierException& bie)
80  {
81  throw BadIdentifierException("AnalyzedLoci::addAlleleInfoByLocusName: allele id already in use.", bie.getIdentifier());
82  }
83  }
84  }
85  if (!locusFound)
86  throw LocusNotFoundException("AnalyzedLoci::addAlleleInfoByLocusName: locus_name not found.",
87  locusName);
88 }
89 
90 /******************************************************************************/
91 
93  const AlleleInfo& allele)
94 {
95  if (locusPosition < loci_.size())
96  {
97  try
98  {
99  loci_[locusPosition]->addAlleleInfo(allele);
100  }
101  catch (BadIdentifierException& bie)
102  {
103  throw BadIdentifierException("AnalyzedLoci::addAlleleInfoByLocusPosition: allele id is already in use.", bie.getIdentifier());
104  }
105  }
106  else
107  throw IndexOutOfBoundsException("AnalyzedLoci::addAlleleInfoByLocusPosition: locus_position out of bounds.",
108  locusPosition, 0, loci_.size());
109 }
110 
111 /******************************************************************************/
112 
113 std::vector<size_t> AnalyzedLoci::getNumberOfAlleles() const
114 {
115  vector<size_t> alleleCount;
116  for (const auto& locus: loci_)
117  {
118  alleleCount.push_back(locus->getNumberOfAlleles());
119  }
120  return alleleCount;
121 }
122 
123 /******************************************************************************/
124 
125 unsigned int AnalyzedLoci::getPloidyByLocusName(const std::string& locusName) const
126 {
127  for (const auto& locus : loci_)
128  {
129  if (locus && locus->getName() == locusName)
130  return locus->getPloidy();
131  }
132  throw LocusNotFoundException("AnalyzedLoci::getLocusInfo: locus_name not found.",
133  locusName);
134 }
135 
136 /******************************************************************************/
137 
138 unsigned int AnalyzedLoci::getPloidyByLocusPosition(size_t locusPosition) const
139 {
140  if (locusPosition >= loci_.size())
141  throw IndexOutOfBoundsException("AnalyzedLoci::getPloidyByLocusPosition: locus_position out of bounds.", locusPosition, 0, loci_.size());
142  return loci_[locusPosition]->getPloidy();
143 }
144 
145 /******************************************************************************/
The AlleleInfo interface.
Definition: AlleleInfo.h:25
std::vector< size_t > getNumberOfAlleles() const
Get the number of alleles at each locus.
void addAlleleInfoByLocusName(const std::string &locusName, const AlleleInfo &allele)
Add an AlleleInfo to a LocusInfo by LocusInfo name.
const LocusInfo & getLocusInfoByName(const std::string &locusName) const
Get a LocusInfo by name.
unsigned int getPloidyByLocusName(const std::string &locusName) const
Get the ploidy of a locus by name.
const LocusInfo & getLocusInfoAtPosition(size_t locusPosition) const
Get a LocusInfo by its position.
unsigned int getPloidyByLocusPosition(size_t locusPosition) const
Get the ploidy of a locus by its position.
void addAlleleInfoByLocusPosition(size_t locusosition, const AlleleInfo &allele)
Add an AlleleInfo to a LocusInfo by its position.
void setLocusInfo(size_t locusPosition, const LocusInfo &locus)
Set a LocusInfo.
size_t getLocusInfoPosition(const std::string &locusName) const
Get the position of a LocusInfo.
The BadIdentifierException class.
virtual const std::string getIdentifier() const
Return the value of the identifier as a string.
The LocusInfo class.
Definition: LocusInfo.h:31
LocusInfo * clone() const override
Definition: LocusInfo.h:88
The LocusNotFoundException class.