bpp-popgen  3.0.0
AnalyzedLoci.cpp
Go to the documentation of this file.
1 //
2 // File AnalyzedLoci.cpp
3 // Author : Sylvain Gaillard
4 // Last modification : Thursday July 29 2004
5 //
6 
7 /*
8  Copyright or © or Copr. CNRS, (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 "AnalyzedLoci.h"
41 
42 using namespace bpp;
43 using namespace std;
44 
45 /******************************************************************************/
46 
47 AnalyzedLoci::AnalyzedLoci(size_t number_of_loci) : loci_(vector<LocusInfo*>(number_of_loci))
48 {
49  for (size_t i = 0; i < loci_.size(); i++)
50  {
51  loci_[i] = 0;
52  }
53 }
54 
55 /******************************************************************************/
56 
57 AnalyzedLoci::AnalyzedLoci(const AnalyzedLoci& analyzed_loci) : loci_(vector<LocusInfo*>(analyzed_loci.loci_.size()))
58 {
59  for (size_t i = 0; i < analyzed_loci.getNumberOfLoci(); i++)
60  {
61  loci_[i] = new LocusInfo(analyzed_loci.getLocusInfoAtPosition(i));
62  }
63 }
64 
65 /******************************************************************************/
66 
68 {
69  for (size_t i = 0; i < loci_.size(); i++)
70  {
71  delete loci_[i];
72  }
73 }
74 
75 /******************************************************************************/
76 
78  size_t locus_position,
79  const LocusInfo& locus)
80 {
81  if (locus_position < loci_.size())
82  loci_[locus_position] = new LocusInfo(locus);
83  else
84  throw IndexOutOfBoundsException("AnalyzedLoci::setLocusInfo: locus_position out of bounds",
85  locus_position, 0, loci_.size());
86 }
87 
88 /******************************************************************************/
89 
91  const std::string& locus_name) const
92 {
93  for (size_t i = 0; i < loci_.size(); i++)
94  {
95  if (loci_[i] != NULL && loci_[i]->getName() == locus_name)
96  return i;
97  }
98  throw BadIdentifierException("AnalyzedLoci::getLocusInfoPosition: locus not found.", locus_name);
99 }
100 
101 /******************************************************************************/
102 
104  const std::string& locus_name) const
105 {
106  for (size_t i = 0; i < loci_.size(); i++)
107  {
108  if (loci_[i] != NULL && loci_[i]->getName() == locus_name)
109  return *(loci_[i]);
110  }
111  throw BadIdentifierException("AnalyzedLoci::getLocusInfo: locus not found.",
112  locus_name);
113 }
114 
115 /******************************************************************************/
116 
118  size_t locus_position) const
119 {
120  if (locus_position >= loci_.size())
121  throw IndexOutOfBoundsException("AnalyzedLoci::getLocusInfoAtPosition: locus_position out of bounds.", locus_position, 0, loci_.size());
122  if (loci_[locus_position] != NULL)
123  return *(loci_[locus_position]);
124  else
125  throw NullPointerException("AnalyzedLoci::getLocusInfo: no locus defined here.");
126 }
127 
128 /******************************************************************************/
129 
130 // AlleleInfo
131 void AnalyzedLoci::addAlleleInfoByLocusName(const std::string& locus_name,
132  const AlleleInfo& allele)
133 {
134  bool locus_found = false;
135  for (vector<LocusInfo*>::iterator it = loci_.begin(); it != loci_.end(); it++)
136  {
137  if ((*it)->getName() == locus_name)
138  {
139  locus_found = true;
140  try
141  {
142  (*it)->addAlleleInfo(allele);
143  }
144  catch (BadIdentifierException& bie)
145  {
146  throw BadIdentifierException("AnalyzedLoci::addAlleleInfoByLocusName: allele id already in use.", bie.getIdentifier());
147  }
148  }
149  }
150  if (!locus_found)
151  throw LocusNotFoundException("AnalyzedLoci::addAlleleInfoByLocusName: locus_name not found.",
152  locus_name);
153 }
154 
155 /******************************************************************************/
156 
158  const AlleleInfo& allele)
159 {
160  if (locus_position < loci_.size())
161  {
162  try
163  {
164  loci_[locus_position]->addAlleleInfo(allele);
165  }
166  catch (BadIdentifierException& bie)
167  {
168  throw BadIdentifierException("AnalyzedLoci::addAlleleInfoByLocusPosition: allele id is already in use.", bie.getIdentifier());
169  }
170  }
171  else
172  throw IndexOutOfBoundsException("AnalyzedLoci::addAlleleInfoByLocusPosition: locus_position out of bounds.",
173  locus_position, 0, loci_.size());
174 }
175 
176 /******************************************************************************/
177 
178 std::vector<size_t> AnalyzedLoci::getNumberOfAlleles() const
179 {
180  vector<size_t> allele_count;
181  for (size_t i = 0; i < loci_.size(); i++)
182  {
183  allele_count.push_back(loci_[i]->getNumberOfAlleles());
184  }
185  return allele_count;
186 }
187 
188 /******************************************************************************/
189 
190 unsigned int AnalyzedLoci::getPloidyByLocusName(const std::string& locus_name) const
191 {
192  for (size_t i = 0; i < loci_.size(); i++)
193  {
194  if (loci_[i] != NULL && loci_[i]->getName() == locus_name)
195  return loci_[i]->getPloidy();
196  }
197  throw LocusNotFoundException("AnalyzedLoci::getLocusInfo: locus_name not found.",
198  locus_name);
199 }
200 
201 /******************************************************************************/
202 
203 unsigned int AnalyzedLoci::getPloidyByLocusPosition(size_t locus_position) const
204 {
205  if (locus_position >= loci_.size())
206  throw IndexOutOfBoundsException("AnalyzedLoci::getPloidyByLocusPosition: locus_position out of bounds.", locus_position, 0, loci_.size());
207  return loci_[locus_position]->getPloidy();
208 }
209 
210 /******************************************************************************/
211 
The AlleleInfo interface.
Definition: AlleleInfo.h:60
The AnalyzedLoci class.
Definition: AnalyzedLoci.h:65
std::vector< size_t > getNumberOfAlleles() const
Get the number of alleles at each locus.
size_t getNumberOfLoci() const
Get the number of loci.
Definition: AnalyzedLoci.h:138
unsigned int getPloidyByLocusName(const std::string &locus_name) const
Get the ploidy of a locus by name.
const LocusInfo & getLocusInfoByName(const std::string &locus_name) const
Get a LocusInfo by name.
AnalyzedLoci(size_t number_of_loci)
Build a void AnalyzedLoci with a specific number of loci.
const LocusInfo & getLocusInfoAtPosition(size_t locus_position) const
Get a LocusInfo by its position.
unsigned int getPloidyByLocusPosition(size_t locus_position) const
Get the ploidy of a locus by its position.
std::vector< LocusInfo * > loci_
Definition: AnalyzedLoci.h:67
~AnalyzedLoci()
Destroy the AnalyzedLoci.
void addAlleleInfoByLocusPosition(size_t locus_position, const AlleleInfo &allele)
Add an AlleleInfo to a LocusInfo by its position.
size_t getLocusInfoPosition(const std::string &locus_name) const
Get the position of a LocusInfo.
void addAlleleInfoByLocusName(const std::string &locus_name, const AlleleInfo &allele)
Add an AlleleInfo to a LocusInfo by LocusInfo name.
void setLocusInfo(size_t locus_position, const LocusInfo &locus)
Set 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:64
The LocusNotFoundException class.