bpp-popgen  3.0.0
PolymorphismMultiGContainer.cpp
Go to the documentation of this file.
1 //
2 // File PolymorphismMultiGContainer.cpp
3 // Author : Sylvain Gaillard
4 // Khalid Belkhir
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 
41 
42 using namespace bpp;
43 using namespace std;
44 
45 // ** Constructors : **********************************************************/
46 
48  groups_(std::vector<size_t>()),
49  groups_names_(std::map<size_t, std::string>()) {}
50 
52  groups_(std::vector<size_t>(pmgc.size())),
53  groups_names_(std::map<size_t, std::string>())
54 {
55  for (size_t i = 0; i < pmgc.size(); i++)
56  {
58  groups_[i] = pmgc.getGroupId(i);
59  }
60  set<size_t> grp_ids = pmgc.getAllGroupsIds();
61  for (set<size_t>::iterator it = grp_ids.begin(); it != grp_ids.end(); it++)
62  {
63  size_t id = *it;
64  string name = pmgc.getGroupName(id);
65  groups_names_[id] = name;
66  }
67 }
68 
69 // ** Destructor : ************************************************************/
70 
72 {
73  clear();
74 }
75 
76 // ** Other methodes : ********************************************************/
77 
79 {
80  clear();
81  for (size_t i = 0; i < pmgc.size(); i++)
82  {
84  groups_.push_back(pmgc.getGroupId(i));
85  }
86  set<size_t> grp_ids = pmgc.getAllGroupsIds();
87  for (set<size_t>::iterator it = grp_ids.begin(); it != grp_ids.end(); it++)
88  {
89  size_t id = *it;
90  string name = pmgc.getGroupName(id);
91  groups_names_[id] = name;
92  }
93 
94  return *this;
95 }
96 
97 /******************************************************************************/
98 
100 {
101  multilocusGenotypes_.push_back(new MultilocusGenotype(mg));
102  groups_.push_back(group);
103  map<size_t, string>::const_iterator it = groups_names_.find(group);
104  if (!(it != groups_names_.end()) )
105  {
106  // ajouter ce groupe avec un nom vide
107  groups_names_[group] = "";
108  }
109 }
110 
111 /******************************************************************************/
112 
114 {
115  if (position >= size())
116  throw IndexOutOfBoundsException("PolymorphismMultiGContainer::getMultilocusGenotype: position out of bounds.", position, 0, size() - 1);
117  return multilocusGenotypes_[position];
118 }
119 
120 /******************************************************************************/
121 
123 {
124  if (position >= size())
125  throw IndexOutOfBoundsException("PolymorphismMultiGContainer::removeMultilocusGenotype: position out of bounds.", position, 0, size() - 1);
126  MultilocusGenotype* tmp_mg = multilocusGenotypes_[position];
127  multilocusGenotypes_.erase(multilocusGenotypes_.begin() + static_cast<ptrdiff_t>(position));
128  groups_.erase(groups_.begin() + static_cast<ptrdiff_t>(position));
129  return tmp_mg;
130 }
131 
132 /******************************************************************************/
133 
135 {
136  if (position >= size())
137  throw IndexOutOfBoundsException("PolymorphismMultiGContainer::deleteMultilocusGenotype: position out of bounds.", position, 0, size() - 1);
138  delete multilocusGenotypes_[position];
139  multilocusGenotypes_.erase(multilocusGenotypes_.begin() + static_cast<ptrdiff_t>(position));
140  groups_.erase(groups_.begin() + static_cast<ptrdiff_t>(position));
141 }
142 
143 /******************************************************************************/
144 
146 {
147  size_t value = 0;
148  for (size_t i = 0; i < size(); i++)
149  {
150  if (i == 0)
151  value = multilocusGenotypes_[i]->size();
152  else if (multilocusGenotypes_[i]->size() != value)
153  return false;
154  }
155  return true;
156 }
157 
158 /******************************************************************************/
159 
161 {
162  if (!isAligned())
163  throw Exception("MultilocusGenotypes are not aligned.");
164  if (size() < 1)
165  return 0;
166  return multilocusGenotypes_[0]->size();
167 }
168 
169 /******************************************************************************/
170 
171 size_t PolymorphismMultiGContainer::getGroupId(size_t position) const
172 {
173  if (position >= size())
174  throw IndexOutOfBoundsException("PolymorphismMultiGContainer::getGroupId: position out of bounds.", position, 0, size() - 1);
175  return groups_[position];
176 }
177 
178 /******************************************************************************/
179 
180 void PolymorphismMultiGContainer::setGroupId(size_t position, size_t group_id)
181 {
182  if (position >= size())
183  throw IndexOutOfBoundsException("PolymorphismMultiGContainer::setGroupId: position out of bounds.", position, 0, size() - 1);
184  groups_[position] = group_id;
185 }
186 
187 /******************************************************************************/
188 
190 {
191  set<size_t> groups_ids;
192  for (size_t i = 0; i < size(); i++)
193  {
194  groups_ids.insert(groups_[i]);
195  }
196  return groups_ids;
197 }
198 
199 /******************************************************************************/
200 
201 std::vector<std::string> PolymorphismMultiGContainer::getAllGroupsNames() const
202 {
203  vector<string> grps_names;
204  map<size_t, string>::const_iterator it;
205  for (it = groups_names_.begin(); it != groups_names_.end(); it++)
206  {
207  string name = it->second;
208  if (!name.empty())
209  grps_names.push_back(name);
210  else
211  grps_names.push_back(TextTools::toString(it->first) );
212  }
213 
214  return grps_names;
215 }
216 
217 /******************************************************************************/
218 
220 {
221  for (size_t i = 0; i < size(); i++)
222  {
223  if (groups_[i] == group)
224  return true;
225  }
226  return false;
227 }
228 
229 /******************************************************************************/
230 
232 {
233  return getAllGroupsIds().size();
234 }
235 
236 /******************************************************************************/
237 
239 {
240  size_t counter = 0;
241  for (size_t i = 0; i < size(); i++)
242  {
243  if (groups_[i] == group)
244  counter++;
245  }
246  return counter;
247 }
248 
249 /******************************************************************************/
250 
251 std::string PolymorphismMultiGContainer::getGroupName(size_t group_id) const
252 {
253  string name = TextTools::toString(group_id); // par defaut on retourne le n° de groupe
254  map<size_t, string>::const_iterator it = groups_names_.find(group_id);
255  if (it != groups_names_.end() )
256  name = it->second;
257  else
258  throw GroupNotFoundException("PolymorphismMultiGContainer::getGroupName: group not found.", group_id);
259  return name;
260 }
261 
262 /******************************************************************************/
263 
264 void PolymorphismMultiGContainer::setGroupName(size_t group_id, std::string name)
265 {
266  map<size_t, string>::iterator it = groups_names_.find(group_id);
267  if (it != groups_names_.end() )
268  it->second = name;
269  else
270  throw GroupNotFoundException("PolymorphismMultiGContainer::getGroupName: group not found.", group_id);
271  return;
272 }
273 
274 /******************************************************************************/
275 
276 void PolymorphismMultiGContainer::addGroupName(size_t group_id, std::string name)
277 {
278  groups_names_[group_id] = name;
279  return;
280 }
281 
282 /******************************************************************************/
283 
284 size_t PolymorphismMultiGContainer::getLocusGroupSize(size_t group, size_t locus_position) const
285 {
286  size_t counter = 0;
287  for (size_t i = 0; i < size(); i++)
288  {
289  try
290  {
291  if (groups_[i] == group && !multilocusGenotypes_[i]->isMonolocusGenotypeMissing(locus_position))
292  counter++;
293  }
294  catch (IndexOutOfBoundsException& ioobe)
295  {
296  throw IndexOutOfBoundsException("PolymorphismMultiGContainer::getGroupSize: locus_position out of bounds.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
297  }
298  }
299  return counter;
300 }
301 
302 /******************************************************************************/
303 
305 {
306  return multilocusGenotypes_.size();
307 }
308 
309 /******************************************************************************/
310 
312 {
313  for (size_t i = 0; i < multilocusGenotypes_.size(); i++)
314  {
315  delete multilocusGenotypes_[i];
316  }
317  multilocusGenotypes_.clear();
318  groups_.clear();
319  groups_names_.clear();
320 }
321 
322 /******************************************************************************/
323 
The GroupNotFoundException class.
std::size_t getBadIndex() const
const std::array< std::size_t, 2 > & getBounds() const
The MultilocusGenotype class.
The PolymorphismMultiGContainer class.
void addGroupName(size_t group_id, std::string name)
Inserts a name for the given group id.
void deleteMultilocusGenotype(size_t position)
Delete a MultilocusGenotype.
~PolymorphismMultiGContainer()
Destroy a PolymorphismMultilocusGenotypeContainer.
size_t getGroupId(size_t position) const
Get the Group id of a MultilocusGenotype.
PolymorphismMultiGContainer & operator=(const PolymorphismMultiGContainer &pmgc)
The assignation operator=.
PolymorphismMultiGContainer()
Build a new PolymorphismMultilocusGenotypeContainer.
const MultilocusGenotype * getMultilocusGenotype(size_t position) const
Get a MultilocusGenotype at a position.
bool groupExists(size_t group) const
Tell if a group exists.
void setGroupId(size_t position, size_t group_id)
Set the Group id of a MultilocusGenotype.
std::map< size_t, std::string > groups_names_
size_t getNumberOfLoci() const
Get the number of loci if the MultilocusGenotypes are aligned.
std::string getGroupName(size_t group_id) const
Get the group name for a given group id or just the id if not available juste return it's id.
std::vector< std::string > getAllGroupsNames() const
Get the groups names or ids if not available.
void addMultilocusGenotype(const MultilocusGenotype &mg, size_t group)
Add a MultilocusGenotype to the container.
std::set< size_t > getAllGroupsIds() const
Get the groups' ids.
MultilocusGenotype * removeMultilocusGenotype(size_t position)
Remove a MultilocusGenotype.
bool isAligned() const
Tell if the MultilocusGenotypes are aligned (i.e. same size).
std::vector< MultilocusGenotype * > multilocusGenotypes_
size_t getLocusGroupSize(size_t group, size_t locus_position) const
Get the size of a group for a given locus.
size_t size() const
Get the number of MultilocusGenotype.
size_t getGroupSize(size_t group) const
Get group size.
void setGroupName(size_t group_id, std::string name)
Set the name for the given group id.
size_t getNumberOfGroups() const
Get the number of groups.
std::string toString(T t)