bpp-popgen  3.0.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 
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
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 
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)
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)
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 
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 
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 
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
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 {
367  try
368  {
369  sequences_[id]->addSequence(sequence);
370  }
371  catch (AlphabetMismatchException& ame)
372  {
373  throw (AlphabetMismatchException("MultiSeqIndividual::addSequence: alphabets don't match.", ame.getAlphabets()[0], ame.getAlphabets()[1]));
374  }
375  catch (Exception& e)
376  {
377  throw (BadIdentifierException("MultiSeqIndividual::addSequence: sequence's name already in use.", sequence.getName()));
378  }
379 }
380 
381 /******************************************************************************/
382 
383 const Sequence& MultiSeqIndividual::getSequence(const std::string& id, const std::string& name) const
384 {
385  map<string, VectorSequenceContainer*>::const_iterator it;
386  it = sequences_.find(id);
387  // Test existence of id in the map.
388  if (it == sequences_.end())
389  {
390  string mes = "MultiSeqIndividual::getSequence: sequence set not found (" + id
391  + ").";
392  throw (Exception(mes));
393  }
394  try
395  {
396  return const_cast<const VectorSequenceContainer*>(it->second)->getSequence(name);
397  }
398  catch (SequenceNotFoundException& snfe)
399  {
400  throw (snfe);
401  }
402 }
403 
404 /******************************************************************************/
405 
406 const Sequence& MultiSeqIndividual::getSequence(const std::string& id, size_t i) const
407 {
408  map<string, VectorSequenceContainer*>::const_iterator it;
409  it = sequences_.find(id);
410  // Test existence of id in the map.
411  if (it == sequences_.end())
412  {
413  string mes = "MultiSeqIndividual::getSequence: sequence set not found (" + id
414  + ").";
415  throw (Exception(mes));
416  }
417  try
418  {
419  return const_cast<const VectorSequenceContainer*>(it->second)->getSequence(i);
420  }
421  catch (IndexOutOfBoundsException& ioobe)
422  {
423  throw (ioobe);
424  }
425 }
426 
427 /******************************************************************************/
428 
429 std::vector<std::string> MultiSeqIndividual::getSequencesKeys() const
430 {
431  vector<string> keys;
432  map<string, VectorSequenceContainer*>::const_iterator it;
433  for (it = sequences_.begin(); it != sequences_.end(); it++)
434  {
435  keys.push_back(it->first);
436  }
437  return keys;
438 }
439 
440 /******************************************************************************/
441 
443 {
444  return sequences_.size() != 0;
445 }
446 
447 /******************************************************************************/
448 
450 {
451  return sequences_.size();
452 }
453 
454 /******************************************************************************/
455 
456 size_t MultiSeqIndividual::getNumberOfSequences(const std::string& id) const
457 {
458  map<string, VectorSequenceContainer*>::const_iterator it;
459  it = sequences_.find(id);
460  // Test existence of id in the map.
461  if (it == sequences_.end())
462  {
463  string mes = "MultiSeqIndividual::getSequence: sequence set not found (" + id
464  + ").";
465  throw (Exception(mes));
466  }
467 
468  return const_cast<const VectorSequenceContainer*>(it->second)->getNumberOfSequences();
469 }
470 
471 /******************************************************************************/
472 
473 // MultilocusGenotype
474 
476 {
477  genotype_ = new MultilocusGenotype(genotype);
478 }
479 
480 /******************************************************************************/
481 
483 {
484  return genotype_;
485 }
486 
487 /******************************************************************************/
488 
490 {
491  return genotype_ != 0;
492 }
493 
494 /******************************************************************************/
495 
std::vector< const Alphabet * > getAlphabets() const
The BadIdentifierException class.
The Date class.
Definition: Date.h:57
const Locality< double > * getLocality() const
Get the locality of the MultiSeqIndividual.
double getY() const
Get the Y coordinate of the MultiSeqIndividual.
bool hasSequences() const
Tell if the MultiSeqIndividual has some sequences.
virtual ~MultiSeqIndividual()
Destroy an MultiSeqIndividual.
const MultilocusGenotype * getGenotype() const
Get the genotype.
const Point2D< double > * getCoord() const
Get the coordinates of the Induvidual.
void setLocality(const Locality< double > *locality)
Set the locality of the MultiSeqIndividual.
void setCoord(const Point2D< double > &coord)
Set the coodinates of the MultiSeqIndividual.
const Date * getDate() const
Get the date of the MultiSeqIndividual.
void setY(const double y)
Set the Y coordinate of th MultiSeqIndividual.
unsigned short getSex() const
Get the sex of the MultiSeqIndividual.
void setId(const std::string id)
Set the id of the MultiSeqIndividual.
MultiSeqIndividual()
Build a void new MultiSeqIndividual.
bool hasLocality() const
Tell if this MultiSeqIndividual has a locality.
void setDate(const Date &date)
Set the date of the MultiSeqIndividual.
const Sequence & getSequence(const std::string &id, const std::string &name) const
Get a named sequence from a named sequence set.
std::vector< std::string > getSequencesKeys() const
Get the sequence set ids.
std::string getId() const
Get the id of the MultiSeqIndividual.
void addSequence(const std::string &id, const Sequence &sequence)
Add a sequence in a named sequence set.
bool hasDate() const
Tell if this MultiSeqIndividual has a date.
Point2D< double > * coord_
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.
std::map< std::string, VectorSequenceContainer * > sequences_
MultiSeqIndividual & operator=(const MultiSeqIndividual &ind)
The MultiSeqIndividual copy operator.
void addGenotype(const MultilocusGenotype &genotype)
Add a genotype.
double getX() const
Get the X coordinate of the MultiSeqIndividual.
void setSex(const unsigned short sex)
Set the sex of the MultiSeqIndividual.
void setX(const double x)
Set the X coordinate of the MultiSeqIndividual.
bool hasGenotype() const
Tell if the MultiSeqIndividual has a MultilocusGenotype.
MultilocusGenotype * genotype_
size_t getNumberOfSequences(const std::string &id) const
Get the number of sequences in a sequence set.
const Locality< double > * locality_
size_t getNumberOfSequenceSet() const
Count the number of sequece set.
The MultilocusGenotype class.
void setY(const T y)
const T & getY() const
void setX(const T x)
const T & getX() const
virtual const std::string & getName() const=0