~ubuntu-branches/ubuntu/trusty/libbpp-popgen/trusty

« back to all changes in this revision

Viewing changes to src/Bpp/PopGen/Group.cpp

  • Committer: Package Import Robot
  • Author(s): Julien Dutheil
  • Date: 2013-03-07 10:51:00 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130307105100-7ij3w7iyab9t2mbt
Tags: 2.1.0-1
Bug fixed and warnings removed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
   for population genetics analysis.
13
13
 
14
14
   This software is governed by the CeCILL  license under French law and
15
 
   abiding by the rules of distribution of free software.  You can  use, 
 
15
   abiding by the rules of distribution of free software.  You can  use,
16
16
   modify and/ or redistribute the software under the terms of the CeCILL
17
17
   license as circulated by CEA, CNRS and INRIA at the following URL
18
 
   "http://www.cecill.info". 
 
18
   "http://www.cecill.info".
19
19
 
20
20
   As a counterpart to the access to the source code and  rights to copy,
21
21
   modify and redistribute granted by the license, users are provided only
22
22
   with a limited warranty  and the software's author,  the holder of the
23
23
   economic rights,  and the successive licensors  have only  limited
24
 
   liability. 
 
24
   liability.
25
25
 
26
26
   In this respect, the user's attention is drawn to the risks associated
27
27
   with loading,  using,  modifying and/or developing or reproducing the
30
30
   therefore means  that it is reserved for developers  and  experienced
31
31
   professionals having in-depth computer knowledge. Users are therefore
32
32
   encouraged to load and test the software's suitability as regards their
33
 
   requirements in conditions enabling the security of their systems and/or 
34
 
   data to be ensured and,  more generally, to use and operate it in the 
35
 
   same conditions as regards security. 
 
33
   requirements in conditions enabling the security of their systems and/or
 
34
   data to be ensured and,  more generally, to use and operate it in the
 
35
   same conditions as regards security.
36
36
 
37
37
   The fact that you are presently reading this means that you have had
38
38
   knowledge of the CeCILL license and that you accept its terms.
39
 
   */
 
39
 */
40
40
 
41
41
#include "Group.h"
42
42
 
43
43
using namespace bpp;
44
44
using namespace std;
45
45
 
46
 
//** Class constructors: ******************************************************/
47
 
Group::Group(unsigned int group_id): id_(group_id), name_(""), individuals_(vector<Individual*>()) {}
48
 
 
49
 
Group::Group(const Group & group): id_(group.getGroupId()), name_(group.getGroupName()),
50
 
  //individuals_(vector<Individuals*>(group.getNumberOfIndividuals()))
51
 
  individuals_(vector<Individual*>())
52
 
{
53
 
  for (unsigned int i = 0 ; i < group.getNumberOfIndividuals() ; i++)
54
 
    addIndividual(group.getIndividualAtPosition(i));
55
 
}
56
 
 
57
 
Group::Group(const Group & group, unsigned int group_id): id_(group_id), name_(group.getGroupName()), individuals_(vector<Individual*>())
58
 
{
59
 
  for (unsigned int i = 0 ; i < group.getNumberOfIndividuals() ; i++)
60
 
    addIndividual(group.getIndividualAtPosition(i));
61
 
}
62
 
 
63
 
//** Class destructor: ********************************************************/
 
46
// ** Class constructors: ******************************************************/
 
47
Group::Group(size_t group_id) : id_(group_id),
 
48
  name_(""),
 
49
  individuals_(vector<Individual*>()) {}
 
50
 
 
51
Group::Group(const Group& group) : id_(group.getGroupId()),
 
52
  name_(group.getGroupName()),
 
53
  // individuals_(vector<Individuals*>(group.getNumberOfIndividuals()))
 
54
  individuals_(vector<Individual*>())
 
55
{
 
56
  for (size_t i = 0; i < group.getNumberOfIndividuals(); i++)
 
57
  {
 
58
    addIndividual(group.getIndividualAtPosition(i));
 
59
  }
 
60
}
 
61
 
 
62
Group::Group(const Group& group, size_t group_id) : id_(group_id),
 
63
  name_(group.getGroupName()),
 
64
  individuals_(vector<Individual*>())
 
65
{
 
66
  for (size_t i = 0; i < group.getNumberOfIndividuals(); i++)
 
67
  {
 
68
    addIndividual(group.getIndividualAtPosition(i));
 
69
  }
 
70
}
 
71
 
 
72
// ** Class destructor: ********************************************************/
64
73
 
65
74
Group::~Group () {}
66
75
 
67
 
//** Other methodes: **********************************************************/
 
76
// ** Other methodes: **********************************************************/
68
77
 
69
 
Group& Group::operator= (const Group & group)
 
78
Group& Group::operator=(const Group& group)
70
79
{
71
80
  setGroupId(group.getGroupId());
72
 
  for (unsigned int i = 0 ; i < group.getNumberOfIndividuals() ; i++)
 
81
  for (size_t i = 0; i < group.getNumberOfIndividuals(); i++)
 
82
  {
73
83
    addIndividual(group.getIndividualAtPosition(i));
74
 
  return * this;
 
84
  }
 
85
  return *this;
75
86
}
76
87
 
77
 
void Group::setGroupId(unsigned int group_id)
 
88
void Group::setGroupId(size_t group_id)
78
89
{
79
90
  id_ = group_id;
80
91
}
86
97
 
87
98
void Group::addIndividual(const Individual& ind) throw (BadIdentifierException)
88
99
{
89
 
  try {
 
100
  try
 
101
  {
90
102
    getIndividualPosition(ind.getId());
91
103
    throw BadIdentifierException("Group::addIndividual: individual id already used.", ind.getId());
92
104
  }
93
 
  catch (BadIdentifierException & bie) {}
 
105
  catch (BadIdentifierException& bie)
 
106
  {}
94
107
  individuals_.push_back(new Individual(ind));
95
108
}
96
109
 
97
110
void Group::addEmptyIndividual(const std::string& individual_id) throw (BadIdentifierException)
98
111
{
99
 
  for (unsigned int i = 0 ; i < getNumberOfIndividuals() ; i++)
 
112
  for (size_t i = 0; i < getNumberOfIndividuals(); i++)
 
113
  {
100
114
    if (individuals_[i]->getId() == individual_id)
101
115
      throw BadIdentifierException("Group::addEmptyIndividual: individual_id already in use.", individual_id);
 
116
  }
102
117
  individuals_.push_back(new Individual(individual_id));
103
118
}
104
119
 
105
 
unsigned int Group::getIndividualPosition(const std::string& individual_id) const throw (IndividualNotFoundException)
 
120
size_t Group::getIndividualPosition(const std::string& individual_id) const throw (IndividualNotFoundException)
106
121
{
107
 
  for (unsigned int i = 0 ; i < getNumberOfIndividuals() ; i++)
 
122
  for (size_t i = 0; i < getNumberOfIndividuals(); i++)
 
123
  {
108
124
    if (individuals_[i]->getId() == individual_id)
109
125
      return i;
 
126
  }
110
127
  throw IndividualNotFoundException("Group::getIndividualPosition: individual_id not found.", individual_id);
111
128
}
112
129
 
113
130
std::auto_ptr<Individual> Group::removeIndividualById(const std::string& individual_id) throw (IndividualNotFoundException)
114
131
{
115
 
  try {
116
 
    unsigned int indPos = getIndividualPosition(individual_id);
 
132
  try
 
133
  {
 
134
    size_t indPos = getIndividualPosition(individual_id);
117
135
    auto_ptr<Individual> ind(individuals_[indPos]);
118
136
    individuals_.erase(individuals_.begin() + indPos);
119
137
    return ind;
120
138
  }
121
 
  catch (IndividualNotFoundException & infe) {
 
139
  catch (IndividualNotFoundException& infe)
 
140
  {
122
141
    throw IndividualNotFoundException("Group::removeIndividualById: individual_id not found.", individual_id);
123
142
  }
124
143
}
125
144
 
126
 
std::auto_ptr<Individual> Group::removeIndividualAtPosition(unsigned int individual_position) throw (IndexOutOfBoundsException)
 
145
std::auto_ptr<Individual> Group::removeIndividualAtPosition(size_t individual_position) throw (IndexOutOfBoundsException)
127
146
{
128
147
  if (individual_position >= individuals_.size())
129
148
    throw IndexOutOfBoundsException("Group::removeIndividualAtPosition.", individual_position, 0, individuals_.size());
134
153
 
135
154
void Group::deleteIndividualById(const std::string& individual_id) throw (IndividualNotFoundException)
136
155
{
137
 
  try {
 
156
  try
 
157
  {
138
158
    removeIndividualById(individual_id);
139
159
  }
140
 
  catch (IndividualNotFoundException & infe) {
 
160
  catch (IndividualNotFoundException& infe)
 
161
  {
141
162
    throw IndividualNotFoundException("Group::deleteIndividualById: individual_id not found.", individual_id);
142
163
  }
143
164
}
144
165
 
145
 
void Group::deleteIndividualAtPosition(unsigned int individual_position) throw (IndexOutOfBoundsException)
 
166
void Group::deleteIndividualAtPosition(size_t individual_position) throw (IndexOutOfBoundsException)
146
167
{
147
 
  try {
 
168
  try
 
169
  {
148
170
    removeIndividualAtPosition(individual_position);
149
171
  }
150
 
  catch (IndexOutOfBoundsException & ioobe) {
 
172
  catch (IndexOutOfBoundsException& ioobe)
 
173
  {
151
174
    throw IndexOutOfBoundsException("Group::deleteIndividualAtPosition.", individual_position, 0, getNumberOfIndividuals());
152
175
  }
153
176
}
154
177
 
155
178
void Group::clear()
156
179
{
157
 
  for (unsigned int i = 0 ; i < individuals_.size() ; i++)
158
 
    delete(individuals_[i]);
 
180
  for (size_t i = 0; i < individuals_.size(); i++)
 
181
  {
 
182
    delete (individuals_[i]);
 
183
  }
159
184
  individuals_.clear();
160
185
}
161
186
 
162
187
const Individual& Group::getIndividualById(const std::string& individual_id) const throw (IndividualNotFoundException)
163
188
{
164
 
  for (unsigned int i = 0 ; i < individuals_.size() ; i++) {
 
189
  for (size_t i = 0; i < individuals_.size(); i++)
 
190
  {
165
191
    if (individuals_[i]->getId() == individual_id)
166
192
      return getIndividualAtPosition(i);
167
193
  }
168
194
  throw IndividualNotFoundException("Group::getIndividualById: individual_id not found.", individual_id);
169
195
}
170
196
 
171
 
  const Individual& Group::getIndividualAtPosition(unsigned int individual_position) const
 
197
const Individual& Group::getIndividualAtPosition(size_t individual_position) const
172
198
throw (IndexOutOfBoundsException)
173
199
{
174
200
  if (individual_position >= individuals_.size())
175
201
    throw IndexOutOfBoundsException("Group::getIndividualAtPosition: individual_position out of bounds.", individual_position, 0, individuals_.size());
176
 
  return * individuals_[individual_position];
 
202
  return *individuals_[individual_position];
177
203
}
178
204
 
179
 
unsigned int Group::getNumberOfIndividuals() const
 
205
size_t Group::getNumberOfIndividuals() const
180
206
{
181
207
  return individuals_.size();
182
208
}
183
209
 
184
 
unsigned int Group::getMaxNumberOfSequences() const
 
210
size_t Group::getMaxNumberOfSequences() const
185
211
{
186
 
  unsigned int maxnum = 0;
187
 
  for (unsigned int i = 0 ; i < getNumberOfIndividuals() ; i++) {
188
 
    vector<unsigned int> seqpos = individuals_[i]->getSequencesPositions();
189
 
    for (unsigned int j = 0 ; j < seqpos.size() ; j++)
190
 
      if (maxnum < seqpos[j]) maxnum = seqpos[j];
 
212
  size_t maxnum = 0;
 
213
  for (size_t i = 0; i < getNumberOfIndividuals(); i++)
 
214
  {
 
215
    vector<size_t> seqpos = individuals_[i]->getSequencesPositions();
 
216
    for (size_t j = 0; j < seqpos.size(); j++)
 
217
    {
 
218
      if (maxnum < seqpos[j])
 
219
        maxnum = seqpos[j];
 
220
    }
191
221
  }
192
222
  return maxnum + 1;
193
223
}
194
224
 
195
 
//-- Dealing with individual's properties -----------------
 
225
// -- Dealing with individual's properties -----------------
196
226
 
197
 
void Group::setIndividualSexAtPosition(unsigned int individual_position, const unsigned short sex) throw (IndexOutOfBoundsException)
 
227
void Group::setIndividualSexAtPosition(size_t individual_position, const unsigned short sex) throw (IndexOutOfBoundsException)
198
228
{
199
229
  if (individual_position >= getNumberOfIndividuals())
200
230
    throw IndexOutOfBoundsException("Group::setIndividualSexAtPosition: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
201
231
  individuals_[individual_position]->setSex(sex);
202
232
}
203
233
 
204
 
unsigned short Group::getIndividualSexAtPosition(unsigned int individual_position) const throw (IndexOutOfBoundsException)
 
234
unsigned short Group::getIndividualSexAtPosition(size_t individual_position) const throw (IndexOutOfBoundsException)
205
235
{
206
236
  if (individual_position >= getNumberOfIndividuals())
207
237
    throw IndexOutOfBoundsException("Group::getIndividualSexAtPosition: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
208
238
  return individuals_[individual_position]->getSex();
209
239
}
210
240
 
211
 
void Group::setIndividualDateAtPosition(unsigned int individual_position, const Date & date) throw (IndexOutOfBoundsException)
 
241
void Group::setIndividualDateAtPosition(size_t individual_position, const Date& date) throw (IndexOutOfBoundsException)
212
242
{
213
243
  if (individual_position >= getNumberOfIndividuals())
214
244
    throw IndexOutOfBoundsException("Group::setIndividualDateAtPosition: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
215
245
  individuals_[individual_position]->setDate(date);
216
246
}
217
247
 
218
 
const Date& Group::getIndividualDateAtPosition(unsigned int individual_position) const throw (Exception)
 
248
const Date& Group::getIndividualDateAtPosition(size_t individual_position) const throw (Exception)
219
249
{
220
250
  if (individual_position >= getNumberOfIndividuals())
221
251
    throw IndexOutOfBoundsException("Group::getIndividualDateAtPosition: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
222
 
  try {
 
252
  try
 
253
  {
223
254
    return individuals_[individual_position]->getDate();
224
255
  }
225
 
  catch (NullPointerException & npe) {
 
256
  catch (NullPointerException& npe)
 
257
  {
226
258
    throw NullPointerException("Group::getIndividualDateAtPosition: individual has no date.");
227
259
  }
228
260
}
229
261
 
230
 
void Group::setIndividualCoordAtPosition(unsigned int individual_position, const Point2D<double> & coord) throw (IndexOutOfBoundsException)
 
262
void Group::setIndividualCoordAtPosition(size_t individual_position, const Point2D<double>& coord) throw (IndexOutOfBoundsException)
231
263
{
232
264
  if (individual_position >= getNumberOfIndividuals())
233
265
    throw IndexOutOfBoundsException("Group::setIndividualCoordAtPosition: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
234
266
  individuals_[individual_position]->setCoord(coord);
235
267
}
236
268
 
237
 
const Point2D<double>& Group::getIndividualCoordAtPosition(unsigned int individual_position) const throw (Exception)
 
269
const Point2D<double>& Group::getIndividualCoordAtPosition(size_t individual_position) const throw (Exception)
238
270
{
239
271
  if (individual_position >= getNumberOfIndividuals())
240
272
    throw IndexOutOfBoundsException("Group::getIndividualCoordAtPosition: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
241
 
  try {
 
273
  try
 
274
  {
242
275
    return individuals_[individual_position]->getCoord();
243
276
  }
244
 
  catch (NullPointerException & npe) {
 
277
  catch (NullPointerException& npe)
 
278
  {
245
279
    throw NullPointerException("Group::getIndividualCoordAtPosition: individual has no coordinates.");
246
280
  }
247
281
}
248
282
 
249
 
void Group::setIndividualLocalityAtPosition(unsigned int individual_position, const Locality<double> * locality) throw (IndexOutOfBoundsException)
 
283
void Group::setIndividualLocalityAtPosition(size_t individual_position, const Locality<double>* locality) throw (IndexOutOfBoundsException)
250
284
{
251
285
  if (individual_position >= getNumberOfIndividuals())
252
286
    throw IndexOutOfBoundsException("Group::setIndividualLocalityAtPosition: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
253
287
  individuals_[individual_position]->setLocality(locality);
254
288
}
255
289
 
256
 
const Locality<double>& Group::getIndividualLocalityAtPosition(unsigned int individual_position) const throw (Exception)
 
290
const Locality<double>& Group::getIndividualLocalityAtPosition(size_t individual_position) const throw (Exception)
257
291
{
258
292
  if (individual_position >= getNumberOfIndividuals())
259
293
    throw IndexOutOfBoundsException("Group::getIndividualLocalityAtPosition: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
260
 
  try {
261
 
    return * individuals_[individual_position]->getLocality();
 
294
  try
 
295
  {
 
296
    return *individuals_[individual_position]->getLocality();
262
297
  }
263
 
  catch (NullPointerException & npe) {
 
298
  catch (NullPointerException& npe)
 
299
  {
264
300
    throw NullPointerException("Group::getIndividualLocalityAtPosition: individuals has no locality.");
265
301
  }
266
302
}
267
303
 
268
 
void Group::addIndividualSequenceAtPosition(unsigned int individual_position, unsigned int sequence_position, const Sequence & sequence) throw (Exception)
 
304
void Group::addIndividualSequenceAtPosition(size_t individual_position, size_t sequence_position, const Sequence& sequence) throw (Exception)
269
305
{
270
306
  if (individual_position >= getNumberOfIndividuals())
271
307
    throw IndexOutOfBoundsException("Group::addIndividualSequenceAtPosition: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
272
 
  try {
 
308
  try
 
309
  {
273
310
    individuals_[individual_position]->addSequence(sequence_position, sequence);
274
311
  }
275
 
  catch (AlphabetMismatchException & ame) {
 
312
  catch (AlphabetMismatchException& ame)
 
313
  {
276
314
    throw AlphabetMismatchException("Group::addIndividualSequenceAtPosition: sequence's alphabet doesn't match.", ame.getAlphabets()[0], ame.getAlphabets()[1]);
277
315
  }
278
 
  catch (BadIdentifierException & bie) {
 
316
  catch (BadIdentifierException& bie)
 
317
  {
279
318
    throw BadIdentifierException("Group::addIndividualSequenceAtPosition: sequence's name already in use.", bie.getIdentifier());
280
319
  }
281
 
  catch (BadIntegerException & bie) {
 
320
  catch (BadIntegerException& bie)
 
321
  {
282
322
    throw BadIntegerException("Group::addIndividualSequenceAtPosition: sequence_position already in use.", bie.getBadInteger());
283
323
  }
284
324
}
285
325
 
286
 
const Sequence& Group::getIndividualSequenceByName(unsigned int individual_position, const string & sequence_name) const throw (Exception)
 
326
const Sequence& Group::getIndividualSequenceByName(size_t individual_position, const string& sequence_name) const throw (Exception)
287
327
{
288
328
  if (individual_position >= getNumberOfIndividuals())
289
329
    throw IndexOutOfBoundsException("Group::getIndividualSequenceByName: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
290
 
  try {
 
330
  try
 
331
  {
291
332
    return individuals_[individual_position]->getSequenceByName(sequence_name);
292
333
  }
293
 
  catch (NullPointerException & npe) {
 
334
  catch (NullPointerException& npe)
 
335
  {
294
336
    throw NullPointerException("Group::getIndividualSequenceByName: no sequence data in individual.");
295
337
  }
296
 
  catch (SequenceNotFoundException & snfe) {
 
338
  catch (SequenceNotFoundException& snfe)
 
339
  {
297
340
    throw SequenceNotFoundException("Group::getIndividualSequenceByName: sequence_name not found.", snfe.getSequenceId());
298
341
  }
299
342
}
300
343
 
301
 
const Sequence& Group::getIndividualSequenceAtPosition(unsigned int individual_position, unsigned int sequence_position) const throw (Exception)
 
344
const Sequence& Group::getIndividualSequenceAtPosition(size_t individual_position, size_t sequence_position) const throw (Exception)
302
345
{
303
346
  if (individual_position >= getNumberOfIndividuals())
304
347
    throw IndexOutOfBoundsException("Group::getIndividualAtPosition: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
305
 
  try {
 
348
  try
 
349
  {
306
350
    return individuals_[individual_position]->getSequenceAtPosition(sequence_position);
307
351
  }
308
 
  catch (NullPointerException & npe) {
 
352
  catch (NullPointerException& npe)
 
353
  {
309
354
    throw NullPointerException("Group::getIndividualSequenceAtPosition: no sequence data in individual.");
310
355
  }
311
 
  catch (SequenceNotFoundException & snfe) {
 
356
  catch (SequenceNotFoundException& snfe)
 
357
  {
312
358
    throw SequenceNotFoundException("Group::getIndividualSequenceAtPosition: sequence_position not found.", snfe.getSequenceId());
313
359
  }
314
360
}
315
361
 
316
 
void Group::deleteIndividualSequenceByName(unsigned int individual_position, const string & sequence_name) throw (Exception)
 
362
void Group::deleteIndividualSequenceByName(size_t individual_position, const string& sequence_name) throw (Exception)
317
363
{
318
364
  if (individual_position >= getNumberOfIndividuals())
319
365
    throw IndexOutOfBoundsException("Group::deleteIndividualSequenceByName: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
320
 
  try {
 
366
  try
 
367
  {
321
368
    individuals_[individual_position]->deleteSequenceByName(sequence_name);
322
369
  }
323
 
  catch (NullPointerException & npe) {
 
370
  catch (NullPointerException& npe)
 
371
  {
324
372
    throw NullPointerException("Group::deleteSequenceByName: no sequence data in individual.");
325
373
  }
326
 
  catch (SequenceNotFoundException & snfe) {
 
374
  catch (SequenceNotFoundException& snfe)
 
375
  {
327
376
    throw SequenceNotFoundException("Group::deleteSequenceByName: sequence_name not found.", snfe.getSequenceId());
328
377
  }
329
378
}
330
379
 
331
 
void Group::deleteIndividualSequenceAtPosition(unsigned int individual_position, unsigned int sequence_position) throw (Exception)
 
380
void Group::deleteIndividualSequenceAtPosition(size_t individual_position, size_t sequence_position) throw (Exception)
332
381
{
333
382
  if (individual_position >= getNumberOfIndividuals())
334
383
    throw IndexOutOfBoundsException("Group::deleteIndividualSequenceAtPosition: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
335
 
  try {
 
384
  try
 
385
  {
336
386
    individuals_[individual_position]->deleteSequenceAtPosition(sequence_position);
337
387
  }
338
 
  catch (NullPointerException & npe) {
 
388
  catch (NullPointerException& npe)
 
389
  {
339
390
    throw NullPointerException("Group::deleteSequenceAtPosition: no sequence data in individual.");
340
391
  }
341
 
  catch (SequenceNotFoundException & snfe) {
 
392
  catch (SequenceNotFoundException& snfe)
 
393
  {
342
394
    throw SequenceNotFoundException("Group::deleteSequenceAtPosition: sequence_position not found.", snfe.getSequenceId());
343
395
  }
344
396
}
345
397
 
346
 
bool Group::hasIndividualSequences(unsigned int individual_position) const throw (IndexOutOfBoundsException)
 
398
bool Group::hasIndividualSequences(size_t individual_position) const throw (IndexOutOfBoundsException)
347
399
{
348
400
  if (individual_position >= getNumberOfIndividuals())
349
401
    throw IndexOutOfBoundsException("Group::hasIndividualSequences: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
350
402
  return individuals_[individual_position]->hasSequences();
351
403
}
352
404
 
353
 
vector<string> Group::getIndividualSequencesNames(unsigned int individual_position) const throw (Exception)
 
405
vector<string> Group::getIndividualSequencesNames(size_t individual_position) const throw (Exception)
354
406
{
355
407
  if (individual_position >= getNumberOfIndividuals())
356
408
    throw IndexOutOfBoundsException("Group::getIndividualSequencesNames: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
357
 
  try {
 
409
  try
 
410
  {
358
411
    return individuals_[individual_position]->getSequencesNames();
359
412
  }
360
 
  catch (NullPointerException & npe) {
 
413
  catch (NullPointerException& npe)
 
414
  {
361
415
    throw NullPointerException("Group::getSequencesNames: no sequence data in individual.");
362
416
  }
363
417
}
364
418
 
365
 
unsigned int Group::getIndividualSequencePosition(unsigned int individual_position, const string & sequence_name) const throw (Exception)
 
419
size_t Group::getIndividualSequencePosition(size_t individual_position, const string& sequence_name) const throw (Exception)
366
420
{
367
421
  if (individual_position >= getNumberOfIndividuals())
368
422
    throw IndexOutOfBoundsException("Group::getIndividualSequencePosition: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
369
 
  try {
 
423
  try
 
424
  {
370
425
    return individuals_[individual_position]->getSequencePosition(sequence_name);
371
426
  }
372
 
  catch (NullPointerException & npe) {
 
427
  catch (NullPointerException& npe)
 
428
  {
373
429
    throw NullPointerException("Group::getSequencePosition: no sequence data in individual.");
374
430
  }
375
 
  catch (SequenceNotFoundException & snfe) {
 
431
  catch (SequenceNotFoundException& snfe)
 
432
  {
376
433
    throw SequenceNotFoundException("Group::getSequencePosition: sequence_name not found.", snfe.getSequenceId());
377
434
  }
378
435
}
379
436
 
380
 
unsigned int Group::getIndividualNumberOfSequences(unsigned int individual_position) const throw (Exception)
 
437
size_t Group::getIndividualNumberOfSequences(size_t individual_position) const throw (Exception)
381
438
{
382
439
  if (individual_position >= getNumberOfIndividuals())
383
440
    throw IndexOutOfBoundsException("Group::getIndividualNumberOfSequences: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
384
 
  try {
 
441
  try
 
442
  {
385
443
    return individuals_[individual_position]->getNumberOfSequences();
386
444
  }
387
 
  catch (NullPointerException & npe) {
 
445
  catch (NullPointerException& npe)
 
446
  {
388
447
    throw NullPointerException("Group::getIndividualNumberOfSequences: no sequence data in individual.");
389
448
  }
390
449
}
391
450
 
392
 
void Group::setIndividualSequences(unsigned int individual_position, const MapSequenceContainer & msc) throw (IndexOutOfBoundsException)
 
451
void Group::setIndividualSequences(size_t individual_position, const MapSequenceContainer& msc) throw (IndexOutOfBoundsException)
393
452
{
394
453
  if (individual_position >= getNumberOfIndividuals())
395
454
    throw IndexOutOfBoundsException("Group::setIndividualSequences: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
396
455
  individuals_[individual_position]->setSequences(msc);
397
456
}
398
457
 
399
 
void Group::setIndividualGenotype(unsigned int individual_position, const MultilocusGenotype & genotype) throw (IndexOutOfBoundsException)
 
458
void Group::setIndividualGenotype(size_t individual_position, const MultilocusGenotype& genotype) throw (IndexOutOfBoundsException)
400
459
{
401
460
  if (individual_position >= getNumberOfIndividuals())
402
461
    throw IndexOutOfBoundsException("Group::setIndividualGenotype: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
403
462
  individuals_[individual_position]->setGenotype(genotype);
404
463
}
405
464
 
406
 
void Group::initIndividualGenotype(unsigned int individual_position, unsigned int loci_number) throw (Exception)
 
465
void Group::initIndividualGenotype(size_t individual_position, size_t loci_number) throw (Exception)
407
466
{
408
467
  if (individual_position >= getNumberOfIndividuals())
409
468
    throw IndexOutOfBoundsException("Group::initIndividualGenotype: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
410
 
  try {
 
469
  try
 
470
  {
411
471
    individuals_[individual_position]->initGenotype(loci_number);
412
472
  }
413
 
  catch (BadIntegerException & bie) {
 
473
  catch (BadIntegerException& bie)
 
474
  {
414
475
    throw BadIntegerException("Group::initIndividualGenotype: loci_number must be > 0.", bie.getBadInteger());
415
476
  }
416
 
  catch (Exception) {
 
477
  catch (Exception)
 
478
  {
417
479
    throw Exception("Group::initIndividualGenotype: individual already has a genotype.");
418
480
  }
419
481
}
420
482
 
421
 
void Group::deleteIndividualGenotype(unsigned int individual_position) throw (IndexOutOfBoundsException)
 
483
void Group::deleteIndividualGenotype(size_t individual_position) throw (IndexOutOfBoundsException)
422
484
{
423
485
  if (individual_position >= getNumberOfIndividuals())
424
486
    throw IndexOutOfBoundsException("Group::deleteIndividualGenotype: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
425
487
  individuals_[individual_position]->deleteGenotype();
426
488
}
427
489
 
428
 
bool Group::hasIndividualGenotype(unsigned int individual_position) const throw (IndexOutOfBoundsException)
 
490
bool Group::hasIndividualGenotype(size_t individual_position) const throw (IndexOutOfBoundsException)
429
491
{
430
492
  if (individual_position >= getNumberOfIndividuals())
431
493
    throw IndexOutOfBoundsException("Group::hasIndividualGenotype: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
432
494
  return individuals_[individual_position]->hasGenotype();
433
495
}
434
496
 
435
 
void Group::setIndividualMonolocusGenotype(unsigned int individual_position, unsigned int locus_position, const MonolocusGenotype & monogen) throw (Exception)
 
497
void Group::setIndividualMonolocusGenotype(size_t individual_position, size_t locus_position, const MonolocusGenotype& monogen) throw (Exception)
436
498
{
437
499
  if (individual_position >= getNumberOfIndividuals())
438
500
    throw IndexOutOfBoundsException("Group::setIndividualMonolocusGenotype: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
439
 
  try {
 
501
  try
 
502
  {
440
503
    individuals_[individual_position]->setMonolocusGenotype(locus_position, monogen);
441
504
  }
442
 
  catch (NullPointerException & npe) {
 
505
  catch (NullPointerException& npe)
 
506
  {
443
507
    throw NullPointerException("Group::setIndividualMonolocusGenotype: individual has no genotype.");
444
508
  }
445
 
  catch (IndexOutOfBoundsException & ioobe) {
446
 
    throw IndexOutOfBoundsException("Group::setIndividualMonolocusGenotype: locus_position excedes the number of locus.", ioobe.getBadInteger(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
 
509
  catch (IndexOutOfBoundsException& ioobe)
 
510
  {
 
511
    throw IndexOutOfBoundsException("Group::setIndividualMonolocusGenotype: locus_position excedes the number of locus.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
447
512
  }
448
513
}
449
514
 
450
 
void Group::setIndividualMonolocusGenotypeByAlleleKey(unsigned int individual_position, unsigned int locus_position, const std::vector<unsigned int>& allele_keys) throw (Exception)
 
515
void Group::setIndividualMonolocusGenotypeByAlleleKey(size_t individual_position, size_t locus_position, const std::vector<size_t>& allele_keys) throw (Exception)
451
516
{
452
517
  if (individual_position >= getNumberOfIndividuals())
453
518
    throw IndexOutOfBoundsException("Group::setIndividualMonolocusGenotypeByAlleleKey: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
454
 
  try {
 
519
  try
 
520
  {
455
521
    individuals_[individual_position]->setMonolocusGenotypeByAlleleKey(locus_position, allele_keys);
456
522
  }
457
 
  catch (NullPointerException & npe) {
 
523
  catch (NullPointerException& npe)
 
524
  {
458
525
    throw NullPointerException("Group::setIndividualMonolocusGenotypeByAlleleKey: individual has no genotype.");
459
526
  }
460
 
  catch (IndexOutOfBoundsException & ioobe) {
461
 
    throw IndexOutOfBoundsException("Group::setIndividualMonolocusGenotypeByAlleleKey: locus_position excedes the number of locus.", ioobe.getBadInteger(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
 
527
  catch (IndexOutOfBoundsException& ioobe)
 
528
  {
 
529
    throw IndexOutOfBoundsException("Group::setIndividualMonolocusGenotypeByAlleleKey: locus_position excedes the number of locus.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
462
530
  }
463
 
  catch (Exception) {
 
531
  catch (Exception)
 
532
  {
464
533
    throw Exception("Group::setIndividualMonolocusGenotypeByAlleleKey: no key in allele_keys.");
465
534
  }
466
535
}
467
536
 
468
 
void Group::setIndividualMonolocusGenotypeByAlleleId(unsigned int individual_position, unsigned int locus_position, const std::vector<std::string>& allele_id, const LocusInfo & locus_info) throw (Exception)
 
537
void Group::setIndividualMonolocusGenotypeByAlleleId(size_t individual_position, size_t locus_position, const std::vector<std::string>& allele_id, const LocusInfo& locus_info) throw (Exception)
469
538
{
470
539
  if (individual_position >= getNumberOfIndividuals())
471
540
    throw IndexOutOfBoundsException("Group::setIndividualMonolocusGenotypeByAlleleId: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
472
 
  try {
 
541
  try
 
542
  {
473
543
    individuals_[individual_position]->setMonolocusGenotypeByAlleleId(locus_position, allele_id, locus_info);
474
544
  }
475
 
  catch (NullPointerException & npe) {
 
545
  catch (NullPointerException& npe)
 
546
  {
476
547
    throw NullPointerException("Group::setIndividualMonolocusGenotypeByAlleleId: individual has no genotype.");
477
548
  }
478
 
  catch (IndexOutOfBoundsException & ioobe) {
479
 
    throw IndexOutOfBoundsException("Group::setIndividualMonolocusGenotypeByAlleleId: locus_position excedes the number of locus.", ioobe.getBadInteger(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
 
549
  catch (IndexOutOfBoundsException& ioobe)
 
550
  {
 
551
    throw IndexOutOfBoundsException("Group::setIndividualMonolocusGenotypeByAlleleId: locus_position excedes the number of locus.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
480
552
  }
481
 
  catch (AlleleNotFoundException & anfe) {
 
553
  catch (AlleleNotFoundException& anfe)
 
554
  {
482
555
    throw AlleleNotFoundException("Group::setIndividualMonolocusGenotypeByAlleleId: id not found.", anfe.getIdentifier());
483
556
  }
484
557
}
485
558
 
486
 
const MonolocusGenotype&  Group::getIndividualMonolocusGenotype(unsigned int individual_position, unsigned int locus_position) const throw (Exception)
 
559
const MonolocusGenotype&  Group::getIndividualMonolocusGenotype(size_t individual_position, size_t locus_position) const throw (Exception)
487
560
{
488
561
  if (individual_position >= getNumberOfIndividuals())
489
562
    throw IndexOutOfBoundsException("Group::getIndividualMonolocusGenotype: individual_position out of bounds.", individual_position, 0, getNumberOfIndividuals());
490
 
  try {
 
563
  try
 
564
  {
491
565
    return individuals_[individual_position]->getMonolocusGenotype(locus_position);
492
566
  }
493
 
  catch (NullPointerException & npe) {
 
567
  catch (NullPointerException& npe)
 
568
  {
494
569
    throw NullPointerException("Group::getIndividualMonolocusGenotype: individual has no genotype.");
495
570
  }
496
 
  catch (IndexOutOfBoundsException & ioobe) {
497
 
    throw IndexOutOfBoundsException("Group::getIndividualMonolocusGenotype: locus_position excedes the number of locus.", ioobe.getBadInteger(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
 
571
  catch (IndexOutOfBoundsException& ioobe)
 
572
  {
 
573
    throw IndexOutOfBoundsException("Group::getIndividualMonolocusGenotype: locus_position excedes the number of locus.", ioobe.getBadIndex(), ioobe.getBounds()[0], ioobe.getBounds()[1]);
498
574
  }
499
575
}
500
576
 
501
577
bool Group::hasSequenceData() const
502
578
{
503
 
  for (unsigned int i = 0 ; i < getNumberOfIndividuals() ; i++)
504
 
    if (hasIndividualSequences(i)) return true;
 
579
  for (size_t i = 0; i < getNumberOfIndividuals(); i++)
 
580
  {
 
581
    if (hasIndividualSequences(i))
 
582
      return true;
 
583
  }
505
584
  return false;
506
585
}
507
586
 
508
 
const Alphabet * Group::getAlphabet() const throw (NullPointerException)
 
587
const Alphabet* Group::getAlphabet() const throw (NullPointerException)
509
588
{
510
 
  for (unsigned int i = 0 ; i < getNumberOfIndividuals() ; i++)
 
589
  for (size_t i = 0; i < getNumberOfIndividuals(); i++)
 
590
  {
511
591
    if (hasIndividualSequences(i))
512
592
      return individuals_[i]->getSequenceAlphabet();
 
593
  }
513
594
  throw NullPointerException("Group::getAlphabet: individual has no sequence data.");
514
595
}
515
596
 
516
 
unsigned int Group::getGroupSizeForLocus(unsigned int locus_position) const
 
597
size_t Group::getGroupSizeForLocus(size_t locus_position) const
517
598
{
518
 
  unsigned int count = 0;
519
 
  for (unsigned int i = 0 ; i < individuals_.size() ; i++)
 
599
  size_t count = 0;
 
600
  for (size_t i = 0; i < individuals_.size(); i++)
 
601
  {
520
602
    if (individuals_[i]->hasGenotype() && !individuals_[i]->getGenotype().isMonolocusGenotypeMissing(locus_position))
521
603
      count++;
 
604
  }
522
605
  return count;
523
606
}
524
607
 
525
 
unsigned int Group::getGroupSizeForSequence(unsigned int sequence_position) const
 
608
size_t Group::getGroupSizeForSequence(size_t sequence_position) const
526
609
{
527
 
  unsigned int count = 0;
528
 
  for (unsigned int i = 0 ; i < individuals_.size() ; i++)
529
 
    if (individuals_[i]->hasSequences()) {
530
 
      try {
 
610
  size_t count = 0;
 
611
  for (size_t i = 0; i < individuals_.size(); i++)
 
612
  {
 
613
    if (individuals_[i]->hasSequences())
 
614
    {
 
615
      try
 
616
      {
531
617
        individuals_[i]->getSequenceAtPosition(sequence_position);
532
618
        count++;
533
619
      }
534
 
      catch (...) {}
 
620
      catch (...)
 
621
      {}
535
622
    }
 
623
  }
536
624
  return count;
537
625
}
538
626