bpp-popgen  2.2.0
MultiSeqIndividual.cpp
Go to the documentation of this file.
1 //
2 // File MultiSeqIndividual.cpp
3 // Author : Sylvain Gaillard
4 // Last modification : Tuesday August 03 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 "MultiSeqIndividual.h"
41 
42 using namespace bpp;
43 using namespace std;
44 
45 // ** Class constructor: *******************************************************/
46 
48  sex_(0),
49  date_(0),
50  coord_(0),
51  locality_(0),
52  sequences_(map<string, VectorSequenceContainer*>()),
53  genotype_(0) {}
54 
55 MultiSeqIndividual::MultiSeqIndividual(const std::string& id) : id_(id),
56  sex_(0),
57  date_(0),
58  coord_(0),
59  locality_(0),
60  sequences_(map<string, VectorSequenceContainer*>()),
61  genotype_(0) {}
62 
64  const std::string& id,
65  const Date& date,
66  const Point2D<double>& coord,
67  Locality<double>* locality,
68  const unsigned short sex) :
69  id_(id),
70  sex_(sex),
71  date_(new Date(date)),
72  coord_(new Point2D<double>(coord)),
73  locality_(locality),
74  sequences_(map<string, VectorSequenceContainer*>()),
75  genotype_(0) {}
76 
78  sex_(ind.getSex()),
79  date_(0),
80  coord_(0),
81  locality_(0),
82  sequences_(map<string, VectorSequenceContainer*>()),
83  genotype_(0)
84 {
85  try
86  {
87  setDate(*ind.getDate());
88  }
89  catch (NullPointerException)
90  {
91  date_ = 0;
92  }
93  try
94  {
95  setCoord(*ind.getCoord());
96  }
97  catch (NullPointerException)
98  {
99  coord_ = 0;
100  }
101  try
102  {
103  setLocality(ind.getLocality());
104  }
105  catch (NullPointerException)
106  {
107  locality_ = 0;
108  }
109  if (ind.hasSequences())
110  {
111  vector<string> keys = ind.getSequencesKeys();
112  for (size_t i = 0; i < keys.size(); i++)
113  {
114  sequences_[keys[i]] = new VectorSequenceContainer(*const_cast<const VectorSequenceContainer*>(ind.getVectorSequenceContainer(keys[i])));
115  }
116  }
117  genotype_ = ind.hasGenotype() ? new MultilocusGenotype(*ind.getGenotype()) : 0;
118 }
119 
120 // ** Class destructor: *******************************************************/
121 
123 {
124  delete date_;
125  delete coord_;
126 }
127 
128 // ** Other methodes: *********************************************************/
129 
131 {
132  setId(ind.getId());
133  setSex(ind.getSex());
134  try
135  {
136  setDate(*ind.getDate());
137  }
138  catch (NullPointerException)
139  {
140  date_ = 0;
141  }
142  try
143  {
144  setCoord(*ind.getCoord());
145  }
146  catch (NullPointerException)
147  {
148  coord_ = 0;
149  }
150  try
151  {
152  setLocality(ind.getLocality());
153  }
154  catch (NullPointerException)
155  {
156  locality_ = 0;
157  }
158  if (ind.hasSequences())
159  {
160  vector<string> keys = ind.getSequencesKeys();
161  for (size_t i = 0; i < keys.size(); i++)
162  {
163  sequences_[keys[i]] = new VectorSequenceContainer(*const_cast<const VectorSequenceContainer*>(ind.getVectorSequenceContainer(keys[i])));
164  }
165  }
166  genotype_ = ind.hasGenotype() ? new MultilocusGenotype(*ind.getGenotype()) : 0;
167  return *this;
168 }
169 
170 /******************************************************************************/
171 
172 // Id
173 void MultiSeqIndividual::setId(const std::string id)
174 {
175  id_ = id;
176 }
177 
178 /******************************************************************************/
179 
180 std::string MultiSeqIndividual::getId() const
181 {
182  return id_;
183 }
184 
185 /******************************************************************************/
186 
187 // Sex
188 void MultiSeqIndividual::setSex(const unsigned short sex)
189 {
190  sex_ = sex;
191 }
192 
193 /******************************************************************************/
194 
195 unsigned short MultiSeqIndividual::getSex() const
196 {
197  return sex_;
198 }
199 
200 /******************************************************************************/
201 
202 // Date
204 {
205  if (!hasDate())
206  {
207  date_ = new Date(date);
208  }
209  else if (*date_ != date)
210  {
211  delete date_;
212  date_ = new Date(date);
213  }
214 }
215 
216 /******************************************************************************/
217 
218 const Date* MultiSeqIndividual::getDate() const throw (NullPointerException)
219 {
220  if (hasDate())
221  return new Date(*date_);
222  else
223  throw (NullPointerException("MultiSeqIndividual::getDate: no date associated to this individual."));
224 }
225 
226 /******************************************************************************/
227 
229 {
230  return date_ != 0;
231 }
232 
233 /******************************************************************************/
234 
235 // Coord
236 void MultiSeqIndividual::setCoord(const Point2D<double>& coord)
237 {
238  if (!hasCoord())
239  {
240  coord_ = new Point2D<double>(coord);
241  }
242  else if (*coord_ != coord)
243  {
244  delete coord_;
245  coord_ = new Point2D<double>(coord);
246  }
247 }
248 
249 /******************************************************************************/
250 
251 void MultiSeqIndividual::setCoord(const double x, const double y)
252 {
253  if (!hasCoord())
254  {
255  coord_ = new Point2D<double>(x, y);
256  }
257  else if (this->getX() != x || this->getY() != y)
258  {
259  delete coord_;
260  coord_ = new Point2D<double>(x, y);
261  }
262 }
263 
264 /******************************************************************************/
265 
266 const Point2D<double>* MultiSeqIndividual::getCoord() const throw (NullPointerException)
267 {
268  if (hasCoord())
269  return new Point2D<double>(*coord_);
270  else
271  throw (NullPointerException("MultiSeqIndividual::getCoord: no coord associated to this individual."));
272 }
273 
274 /******************************************************************************/
275 
277 {
278  return coord_ != 0;
279 }
280 
281 /******************************************************************************/
282 
283 void MultiSeqIndividual::setX(const double x) throw (NullPointerException)
284 {
285  if (hasCoord())
286  coord_->setX(x);
287  else
288  throw (NullPointerException("MultiSeqIndividual::setX: no coord associated to this individual."));
289 }
290 
291 /******************************************************************************/
292 
293 void MultiSeqIndividual::setY(const double y) throw (NullPointerException)
294 {
295  if (hasCoord())
296  coord_->setY(y);
297  else
298  throw (NullPointerException("MultiSeqIndividual::setY: no coord associated to this individual."));
299 }
300 
301 /******************************************************************************/
302 
303 double MultiSeqIndividual::getX() const throw (NullPointerException)
304 {
305  if (hasCoord())
306  return coord_->getX();
307  else
308  throw (NullPointerException("MultiSeqIndividual::getX: no coord associated to this individual."));
309 }
310 
311 /******************************************************************************/
312 
313 double MultiSeqIndividual::getY() const throw (NullPointerException)
314 {
315  if (hasCoord())
316  return coord_->getY();
317  else
318  throw (NullPointerException("MultiSeqIndividual::getY: no coord associated to this individual."));
319 }
320 
321 /******************************************************************************/
322 
323 // Locality
325 {
326  locality_ = locality;
327 }
328 
329 /******************************************************************************/
330 
331 const Locality<double>* MultiSeqIndividual::getLocality() const throw (NullPointerException)
332 {
333  if (hasLocality())
334  return locality_;
335  else
336  throw (NullPointerException("MultiSeqIndividual::getLocality: no locality associated to this individual."));
337 }
338 
339 /******************************************************************************/
340 
342 {
343  return locality_ != 0;
344 }
345 
346 /******************************************************************************/
347 
348 // Sequences
349 const VectorSequenceContainer* MultiSeqIndividual::getVectorSequenceContainer(const std::string& id) const throw (Exception)
350 {
351  map<string, VectorSequenceContainer*>::const_iterator it;
352  it = sequences_.find(id);
353  // Test existence of id in the map.
354  if (it == sequences_.end())
355  {
356  string mes = "MultiSeqIndividual::getSequence: sequence set not found (" + id
357  + ").";
358  throw (Exception(mes));
359  }
360  return const_cast<const VectorSequenceContainer*>(it->second);
361 }
362 
363 /******************************************************************************/
364 
365 void MultiSeqIndividual::addSequence(const std::string& id, const Sequence& sequence)
366 throw (Exception)
367 {
368  try
369  {
370  sequences_[id]->addSequence(sequence);
371  }
372  catch (AlphabetMismatchException& ame)
373  {
374  throw (AlphabetMismatchException("MultiSeqIndividual::addSequence: alphabets don't match.", ame.getAlphabets()[0], ame.getAlphabets()[1]));
375  }
376  catch (Exception& e)
377  {
378  throw (BadIdentifierException("MultiSeqIndividual::addSequence: sequence's name already in use.", sequence.getName()));
379  }
380 }
381 
382 /******************************************************************************/
383 
384 const Sequence& MultiSeqIndividual::getSequence(const std::string& id, const std::string& name)
385 const throw (Exception)
386 {
387  map<string, VectorSequenceContainer*>::const_iterator it;
388  it = sequences_.find(id);
389  // Test existence of id in the map.
390  if (it == sequences_.end())
391  {
392  string mes = "MultiSeqIndividual::getSequence: sequence set not found (" + id
393  + ").";
394  throw (Exception(mes));
395  }
396  try
397  {
398  return const_cast<const VectorSequenceContainer*>(it->second)->getSequence(name);
399  }
400  catch (SequenceNotFoundException& snfe)
401  {
402  throw (snfe);
403  }
404 }
405 
406 /******************************************************************************/
407 
408 const Sequence& MultiSeqIndividual::getSequence(const std::string& id, size_t i)
409 const throw (Exception)
410 {
411  map<string, VectorSequenceContainer*>::const_iterator it;
412  it = sequences_.find(id);
413  // Test existence of id in the map.
414  if (it == sequences_.end())
415  {
416  string mes = "MultiSeqIndividual::getSequence: sequence set not found (" + id
417  + ").";
418  throw (Exception(mes));
419  }
420  try
421  {
422  return const_cast<const VectorSequenceContainer*>(it->second)->getSequence(i);
423  }
424  catch (IndexOutOfBoundsException& ioobe)
425  {
426  throw (ioobe);
427  }
428 }
429 
430 /******************************************************************************/
431 
432 std::vector<std::string> MultiSeqIndividual::getSequencesKeys() const
433 {
434  vector<string> keys;
435  map<string, VectorSequenceContainer*>::const_iterator it;
436  for (it = sequences_.begin(); it != sequences_.end(); it++)
437  {
438  keys.push_back(it->first);
439  }
440  return keys;
441 }
442 
443 /******************************************************************************/
444 
446 {
447  return sequences_.size() != 0;
448 }
449 
450 /******************************************************************************/
451 
453 {
454  return sequences_.size();
455 }
456 
457 /******************************************************************************/
458 
459 size_t MultiSeqIndividual::getNumberOfSequences(const std::string& id) const
460 throw (Exception)
461 {
462  map<string, VectorSequenceContainer*>::const_iterator it;
463  it = sequences_.find(id);
464  // Test existence of id in the map.
465  if (it == sequences_.end())
466  {
467  string mes = "MultiSeqIndividual::getSequence: sequence set not found (" + id
468  + ").";
469  throw (Exception(mes));
470  }
471 
472  return const_cast<const VectorSequenceContainer*>(it->second)->getNumberOfSequences();
473 }
474 
475 /******************************************************************************/
476 
477 // MultilocusGenotype
478 
480 {
481  genotype_ = new MultilocusGenotype(genotype);
482 }
483 
484 /******************************************************************************/
485 
486 const MultilocusGenotype* MultiSeqIndividual::getGenotype() const throw (NullPointerException)
487 {
488  return genotype_;
489 }
490 
491 /******************************************************************************/
492 
494 {
495  return genotype_ != 0;
496 }
497 
498 /******************************************************************************/
499 
MultiSeqIndividual & operator=(const MultiSeqIndividual &ind)
The MultiSeqIndividual copy operator.
std::map< std::string, VectorSequenceContainer * > sequences_
size_t getNumberOfSequences(const std::string &id) const
Get the number of sequences in a sequence set.
unsigned short getSex() const
Get the sex of the MultiSeqIndividual.
void setX(const double x)
Set the X coordinate of the MultiSeqIndividual.
bool hasLocality() const
Tell if this MultiSeqIndividual has a locality.
bool hasSequences() const
Tell if the MultiSeqIndividual has some sequences.
The BadIdentifierException class.
STL namespace.
size_t getNumberOfSequenceSet() const
Count the number of sequece set.
const Sequence & getSequence(const std::string &id, const std::string &name) const
Get a named sequence from a named sequence set.
bool hasGenotype() const
Tell if the MultiSeqIndividual has a MultilocusGenotype.
void addGenotype(const MultilocusGenotype &genotype)
Add a genotype.
The MultilocusGenotype class.
The Date class.
Definition: Date.h:56
void setId(const std::string id)
Set the id of the MultiSeqIndividual.
bool hasDate() const
Tell if this MultiSeqIndividual has a date.
double getX() const
Get the X coordinate of the MultiSeqIndividual.
const Locality< double > * getLocality() const
Get the locality of the MultiSeqIndividual.
virtual ~MultiSeqIndividual()
Destroy an MultiSeqIndividual.
std::string getId() const
Get the id of the MultiSeqIndividual.
const MultilocusGenotype * getGenotype() const
Get the genotype.
MultilocusGenotype * genotype_
void setY(const double y)
Set the Y coordinate of th MultiSeqIndividual.
void setCoord(const Point2D< double > &coord)
Set the coodinates of the MultiSeqIndividual.
double getY() const
Get the Y coordinate of the MultiSeqIndividual.
Point2D< double > * coord_
MultiSeqIndividual()
Build a void new MultiSeqIndividual.
const Locality< double > * locality_
void setLocality(const Locality< double > *locality)
Set the locality of the MultiSeqIndividual.
const VectorSequenceContainer * getVectorSequenceContainer(const std::string &id) const
Get a pointer to the VectorSequenceContainer at a named locus.
bool hasCoord() const
Tell if this MultiSeqIndividual has coordinates.
*** UNUSED CLASS ***The MultiSeqIndividual class.
void setDate(const Date &date)
Set the date of the MultiSeqIndividual.
void setSex(const unsigned short sex)
Set the sex of the MultiSeqIndividual.
std::vector< std::string > getSequencesKeys() const
Get the sequence set ids.
const Date * getDate() const
Get the date of the MultiSeqIndividual.
void addSequence(const std::string &id, const Sequence &sequence)
Add a sequence in a named sequence set.
const Point2D< double > * getCoord() const
Get the coordinates of the Induvidual.