bpp-popgen  2.2.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 throw (IndexOutOfBoundsException)
81 {
82  if (locus_position < loci_.size())
83  loci_[locus_position] = new LocusInfo(locus);
84  else
85  throw IndexOutOfBoundsException("AnalyzedLoci::setLocusInfo: locus_position out of bounds",
86  locus_position, 0, loci_.size());
87 }
88 
89 /******************************************************************************/
90 
92  const std::string& locus_name) const
94 {
95  for (size_t i = 0; i < loci_.size(); i++)
96  {
97  if (loci_[i] != NULL && loci_[i]->getName() == locus_name)
98  return i;
99  }
100  throw BadIdentifierException("AnalyzedLoci::getLocusInfoPosition: locus not found.", locus_name);
101 }
102 
103 /******************************************************************************/
104 
106  const std::string& locus_name) const
108 {
109  for (size_t i = 0; i < loci_.size(); i++)
110  {
111  if (loci_[i] != NULL && loci_[i]->getName() == locus_name)
112  return *(loci_[i]);
113  }
114  throw BadIdentifierException("AnalyzedLoci::getLocusInfo: locus not found.",
115  locus_name);
116 }
117 
118 /******************************************************************************/
119 
121  size_t locus_position) const
122 throw (Exception)
123 {
124  if (locus_position >= loci_.size())
125  throw IndexOutOfBoundsException("AnalyzedLoci::getLocusInfoAtPosition: locus_position out of bounds.", locus_position, 0, loci_.size());
126  if (loci_[locus_position] != NULL)
127  return *(loci_[locus_position]);
128  else
129  throw NullPointerException("AnalyzedLoci::getLocusInfo: no locus defined here.");
130 }
131 
132 /******************************************************************************/
133 
134 // AlleleInfo
135 void AnalyzedLoci::addAlleleInfoByLocusName(const std::string& locus_name,
136  const AlleleInfo& allele)
137 throw (Exception)
138 {
139  bool locus_found = false;
140  for (vector<LocusInfo*>::iterator it = loci_.begin(); it != loci_.end(); it++)
141  {
142  if ((*it)->getName() == locus_name)
143  {
144  locus_found = true;
145  try
146  {
147  (*it)->addAlleleInfo(allele);
148  }
149  catch (BadIdentifierException& bie)
150  {
151  throw BadIdentifierException("AnalyzedLoci::addAlleleInfoByLocusName: allele id already in use.", bie.getIdentifier());
152  }
153  }
154  }
155  if (!locus_found)
156  throw LocusNotFoundException("AnalyzedLoci::addAlleleInfoByLocusName: locus_name not found.",
157  locus_name);
158 }
159 
160 /******************************************************************************/
161 
163  const AlleleInfo& allele)
164 throw (Exception)
165 {
166  if (locus_position < loci_.size())
167  {
168  try
169  {
170  loci_[locus_position]->addAlleleInfo(allele);
171  }
172  catch (BadIdentifierException& bie)
173  {
174  throw BadIdentifierException("AnalyzedLoci::addAlleleInfoByLocusPosition: allele id is already in use.", bie.getIdentifier());
175  }
176  }
177  else
178  throw IndexOutOfBoundsException("AnalyzedLoci::addAlleleInfoByLocusPosition: locus_position out of bounds.",
179  locus_position, 0, loci_.size());
180 }
181 
182 /******************************************************************************/
183 
184 std::vector<size_t> AnalyzedLoci::getNumberOfAlleles() const
185 {
186  vector<size_t> allele_count;
187  for (size_t i = 0; i < loci_.size(); i++)
188  {
189  allele_count.push_back(loci_[i]->getNumberOfAlleles());
190  }
191  return allele_count;
192 }
193 
194 /******************************************************************************/
195 
196 unsigned int AnalyzedLoci::getPloidyByLocusName(const std::string& locus_name) const
198 {
199  for (size_t i = 0; i < loci_.size(); i++)
200  {
201  if (loci_[i] != NULL && loci_[i]->getName() == locus_name)
202  return loci_[i]->getPloidy();
203  }
204  throw LocusNotFoundException("AnalyzedLoci::getLocusInfo: locus_name not found.",
205  locus_name);
206 }
207 
208 /******************************************************************************/
209 
210 unsigned int AnalyzedLoci::getPloidyByLocusPosition(size_t locus_position) const
211 throw (IndexOutOfBoundsException)
212 {
213  if (locus_position >= loci_.size())
214  throw IndexOutOfBoundsException("AnalyzedLoci::getPloidyByLocusPosition: locus_position out of bounds.", locus_position, 0, loci_.size());
215  return loci_[locus_position]->getPloidy();
216 }
217 
218 /******************************************************************************/
219 
size_t getNumberOfLoci() const
Get the number of loci.
Definition: AnalyzedLoci.h:144
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.
const LocusInfo & getLocusInfoByName(const std::string &locus_name) const
Get a LocusInfo by name.
void addAlleleInfoByLocusPosition(size_t locus_position, const AlleleInfo &allele)
Add an AlleleInfo to a LocusInfo by its position.
The BadIdentifierException class.
STL namespace.
unsigned int getPloidyByLocusPosition(size_t locus_position) const
Get the ploidy of a locus by its position.
virtual const std::string getIdentifier() const
Return the value of the identifier as a string.
const LocusInfo & getLocusInfoAtPosition(size_t locus_position) const
Get a LocusInfo by its position.
unsigned int getPloidyByLocusName(const std::string &locus_name) const
Get the ploidy of a locus by name.
std::vector< size_t > getNumberOfAlleles() const
Get the number of alleles at each locus.
void setLocusInfo(size_t locus_position, const LocusInfo &locus)
Set a LocusInfo.
The AnalyzedLoci class.
Definition: AnalyzedLoci.h:64
std::vector< LocusInfo * > loci_
Definition: AnalyzedLoci.h:67
The AlleleInfo interface.
Definition: AlleleInfo.h:58
The LocusInfo class.
Definition: LocusInfo.h:63
AnalyzedLoci(size_t number_of_loci)
Build a void AnalyzedLoci with a specific number of loci.
~AnalyzedLoci()
Destroy the AnalyzedLoci.
The LocusNotFoundException class.