bpp-popgen  3.0.0
LocusInfo.cpp
Go to the documentation of this file.
1 //
2 // File LocusInfo.cpp
3 // Author : Sylvain Gaillard
4 // Last modification : Thursday July 29 2004
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 <Bpp/Text/TextTools.h>
41 
42 #include "LocusInfo.h"
43 #include "GeneralExceptions.h"
44 
45 using namespace bpp;
46 using namespace std;
47 
48 unsigned int LocusInfo::HAPLODIPLOID = 0;
49 unsigned int LocusInfo::HAPLOID = 1;
50 unsigned int LocusInfo::DIPLOID = 2;
51 unsigned int LocusInfo::UNKNOWN = 9999;
52 
53 // ** Class constructor: *******************************************************/
54 
55 LocusInfo::LocusInfo(const std::string& name, const unsigned int ploidy) : name_(name),
56  ploidy_(ploidy),
57  alleles_(vector<AlleleInfo*>()) {}
58 
59 LocusInfo::LocusInfo(const LocusInfo& locus_info) : name_(locus_info.getName()),
60  ploidy_(locus_info.getPloidy()),
61  alleles_(vector<AlleleInfo*>(locus_info.getNumberOfAlleles()))
62 {
63  for (unsigned int i = 0; i < locus_info.getNumberOfAlleles(); i++)
64  {
65  alleles_[i] = dynamic_cast<AlleleInfo*>(locus_info.getAlleleInfoByKey(i).clone());
66  }
67 }
68 
69 // ** Class destructor: *******************************************************/
70 
72 {
73  for (unsigned int i = 0; i < alleles_.size(); i++)
74  {
75  delete alleles_[i];
76  }
77  alleles_.clear();
78 }
79 
80 // ** Other methodes: *********************************************************/
81 
82 // AlleleInfos
84 {
85  // Check if the allele id is not already in use
86  for (unsigned int i = 0; i < alleles_.size(); i++)
87  {
88  if (alleles_[i]->getId() == allele.getId())
89  throw BadIdentifierException("LocusInfo::addAlleleInfo: Id already in use.", allele.getId());
90  }
91  alleles_.push_back(allele.clone());
92 }
93 
94 const AlleleInfo& LocusInfo::getAlleleInfoById(const std::string& id) const
95 {
96  for (unsigned int i = 0; i < alleles_.size(); i++)
97  {
98  if (alleles_[i]->getId() == id)
99  return *(alleles_[i]);
100  }
101  throw AlleleNotFoundException("LocusInfo::getAlleleInfoById: AlleleInfo id unknown.", id);
102 }
103 
105 {
106  if (key >= alleles_.size())
107  throw IndexOutOfBoundsException("LocusInfo::getAlleleInfoByKey: key out of bounds.", key, 0, alleles_.size());
108  return *(alleles_[key]);
109 }
110 
111 unsigned int LocusInfo::getAlleleInfoKey(const std::string& id) const
112 {
113  for (unsigned int i = 0; i < alleles_.size(); i++)
114  {
115  if (alleles_[i]->getId() == id)
116  return i;
117  }
118  throw AlleleNotFoundException("LocusInfo::getAlleleInfoKey: AlleleInfo id not found.", id);
119 }
120 
122 {
123  return alleles_.size();
124 }
125 
127 {
128  for (unsigned int i = 0; i < alleles_.size(); i++)
129  {
130  delete alleles_[i];
131  }
132  alleles_.clear();
133 }
134 
The AlleleInfo interface.
Definition: AlleleInfo.h:60
virtual const std::string & getId() const =0
Get the identitier of the allele.
AlleleInfo * clone() const =0
The AlleleNotFoundException class.
The BadIdentifierException class.
The LocusInfo class.
Definition: LocusInfo.h:64
static unsigned int UNKNOWN
Definition: LocusInfo.h:74
LocusInfo(const std::string &name, const unsigned int ploidy=DIPLOID)
Build a new LocusInfo object.
Definition: LocusInfo.cpp:55
virtual ~LocusInfo()
Destroy the LocusInfo.
Definition: LocusInfo.cpp:71
size_t getNumberOfAlleles() const
Get the number of alleles at this locus.
Definition: LocusInfo.cpp:121
void clear()
Delete all alleles from the locus.
Definition: LocusInfo.cpp:126
unsigned int getAlleleInfoKey(const std::string &id) const
Get the position of an AlleleInfo.
Definition: LocusInfo.cpp:111
static unsigned int DIPLOID
Definition: LocusInfo.h:73
const AlleleInfo & getAlleleInfoByKey(size_t key) const
Retrieve an AlleleInfo object of the LocusInfo.
Definition: LocusInfo.cpp:104
static unsigned int HAPLOID
Definition: LocusInfo.h:72
void addAlleleInfo(const AlleleInfo &allele)
Add an AlleleInfo to the LocusInfo.
Definition: LocusInfo.cpp:83
static unsigned int HAPLODIPLOID
Definition: LocusInfo.h:71
const AlleleInfo & getAlleleInfoById(const std::string &id) const
Retrieve an AlleleInfo object of the LocusInfo.
Definition: LocusInfo.cpp:94
std::vector< AlleleInfo * > alleles_
Definition: LocusInfo.h:68