bpp-popgen3  3.0.0
LocusInfo.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 <Bpp/Text/TextTools.h>
6 
7 #include "LocusInfo.h"
8 #include "GeneralExceptions.h"
9 
10 using namespace bpp;
11 using namespace std;
12 
13 unsigned int LocusInfo::HAPLODIPLOID = 0;
14 unsigned int LocusInfo::HAPLOID = 1;
15 unsigned int LocusInfo::DIPLOID = 2;
16 unsigned int LocusInfo::UNKNOWN = 9999;
17 
18 // ** Other methods: *********************************************************/
19 
20 // AlleleInfos
22 {
23  // Check if the allele id is not already in use
24  for (const auto& existingAllele : alleles_)
25  {
26  if (existingAllele->getId() == allele.getId())
27  throw BadIdentifierException("LocusInfo::addAlleleInfo: Id already in use.", allele.getId());
28  }
29  alleles_.push_back(unique_ptr<AlleleInfo>(allele.clone()));
30 }
31 
32 const AlleleInfo& LocusInfo::getAlleleInfoById(const std::string& id) const
33 {
34  for (unsigned int i = 0; i < alleles_.size(); i++)
35  {
36  if (alleles_[i]->getId() == id)
37  return *alleles_[i];
38  }
39  throw AlleleNotFoundException("LocusInfo::getAlleleInfoById: AlleleInfo id unknown.", id);
40 }
41 
42 const AlleleInfo& LocusInfo::getAlleleInfoByKey(size_t key) const
43 {
44  if (key >= alleles_.size())
45  throw IndexOutOfBoundsException("LocusInfo::getAlleleInfoByKey: key out of bounds.", key, 0, alleles_.size());
46  return *(alleles_[key]);
47 }
48 
49 unsigned int LocusInfo::getAlleleInfoKey(const std::string& id) const
50 {
51  for (unsigned int i = 0; i < alleles_.size(); ++i)
52  {
53  if (alleles_[i]->getId() == id)
54  return i;
55  }
56  throw AlleleNotFoundException("LocusInfo::getAlleleInfoKey: AlleleInfo id not found.", id);
57 }
The AlleleInfo interface.
Definition: AlleleInfo.h:25
virtual const std::string & getId() const =0
Get the identitier of the allele.
AlleleInfo * clone() const =0
The AlleleNotFoundException class.
The BadIdentifierException class.
static unsigned int UNKNOWN
Definition: LocusInfo.h:41
unsigned int getAlleleInfoKey(const std::string &id) const
Get the position of an AlleleInfo.
Definition: LocusInfo.cpp:49
static unsigned int DIPLOID
Definition: LocusInfo.h:40
const AlleleInfo & getAlleleInfoByKey(size_t key) const
Retrieve an AlleleInfo object of the LocusInfo.
Definition: LocusInfo.cpp:42
static unsigned int HAPLOID
Definition: LocusInfo.h:39
void addAlleleInfo(const AlleleInfo &allele)
Add an AlleleInfo to the LocusInfo.
Definition: LocusInfo.cpp:21
static unsigned int HAPLODIPLOID
Definition: LocusInfo.h:38
const AlleleInfo & getAlleleInfoById(const std::string &id) const
Retrieve an AlleleInfo object of the LocusInfo.
Definition: LocusInfo.cpp:32