bpp-popgen  2.1.0
Bpp/PopGen/MultiSeqIndividual.cpp
Go to the documentation of this file.
00001 //
00002 // File MultiSeqIndividual.cpp
00003 // Author : Sylvain Gaillard
00004 // Last modification : Tuesday August 03 2004
00005 //
00006 
00007 /*
00008    Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
00009 
00010    This software is a computer program whose purpose is to provide classes
00011    for population genetics analysis.
00012 
00013    This software is governed by the CeCILL  license under French law and
00014    abiding by the rules of distribution of free software.  You can  use,
00015    modify and/ or redistribute the software under the terms of the CeCILL
00016    license as circulated by CEA, CNRS and INRIA at the following URL
00017    "http://www.cecill.info".
00018 
00019    As a counterpart to the access to the source code and  rights to copy,
00020    modify and redistribute granted by the license, users are provided only
00021    with a limited warranty  and the software's author,  the holder of the
00022    economic rights,  and the successive licensors  have only  limited
00023    liability.
00024 
00025    In this respect, the user's attention is drawn to the risks associated
00026    with loading,  using,  modifying and/or developing or reproducing the
00027    software by the user in light of its specific status of free software,
00028    that may mean  that it is complicated to manipulate,  and  that  also
00029    therefore means  that it is reserved for developers  and  experienced
00030    professionals having in-depth computer knowledge. Users are therefore
00031    encouraged to load and test the software's suitability as regards their
00032    requirements in conditions enabling the security of their systems and/or
00033    data to be ensured and,  more generally, to use and operate it in the
00034    same conditions as regards security.
00035 
00036    The fact that you are presently reading this means that you have had
00037    knowledge of the CeCILL license and that you accept its terms.
00038  */
00039 
00040 #include "MultiSeqIndividual.h"
00041 
00042 using namespace bpp;
00043 using namespace std;
00044 
00045 // ** Class constructor: *******************************************************/
00046 
00047 MultiSeqIndividual::MultiSeqIndividual() : id_(""),
00048   sex_(0),
00049   date_(0),
00050   coord_(0),
00051   locality_(0),
00052   sequences_(map<string, VectorSequenceContainer*>()),
00053   genotype_(0) {}
00054 
00055 MultiSeqIndividual::MultiSeqIndividual(const std::string& id) : id_(id),
00056   sex_(0),
00057   date_(0),
00058   coord_(0),
00059   locality_(0),
00060   sequences_(map<string, VectorSequenceContainer*>()),
00061   genotype_(0) {}
00062 
00063 MultiSeqIndividual::MultiSeqIndividual(
00064   const std::string& id,
00065   const Date& date,
00066   const Point2D<double>& coord,
00067   Locality<double>* locality,
00068   const unsigned short sex) :
00069   id_(id),
00070   sex_(sex),
00071   date_(new Date(date)),
00072   coord_(new Point2D<double>(coord)),
00073   locality_(locality),
00074   sequences_(map<string, VectorSequenceContainer*>()),
00075   genotype_(0) {}
00076 
00077 MultiSeqIndividual::MultiSeqIndividual(const MultiSeqIndividual& ind) : id_(ind.getId()),
00078   sex_(ind.getSex()),
00079   date_(0),
00080   coord_(0),
00081   locality_(0),
00082   sequences_(map<string, VectorSequenceContainer*>()),
00083   genotype_(0)
00084 {
00085   try
00086   {
00087     setDate(*ind.getDate());
00088   }
00089   catch (NullPointerException)
00090   {
00091     date_ = 0;
00092   }
00093   try
00094   {
00095     setCoord(*ind.getCoord());
00096   }
00097   catch (NullPointerException)
00098   {
00099     coord_ = 0;
00100   }
00101   try
00102   {
00103     setLocality(ind.getLocality());
00104   }
00105   catch (NullPointerException)
00106   {
00107     locality_ = 0;
00108   }
00109   if (ind.hasSequences())
00110   {
00111     vector<string> keys = ind.getSequencesKeys();
00112     for (size_t i = 0; i < keys.size(); i++)
00113     {
00114       sequences_[keys[i]] = new VectorSequenceContainer(*const_cast<const VectorSequenceContainer*>(ind.getVectorSequenceContainer(keys[i])));
00115     }
00116   }
00117   genotype_ = ind.hasGenotype() ? new MultilocusGenotype(*ind.getGenotype()) : 0;
00118 }
00119 
00120 // ** Class destructor: *******************************************************/
00121 
00122 MultiSeqIndividual::~MultiSeqIndividual()
00123 {
00124   delete date_;
00125   delete coord_;
00126 }
00127 
00128 // ** Other methodes: *********************************************************/
00129 
00130 MultiSeqIndividual& MultiSeqIndividual::operator=(const MultiSeqIndividual& ind)
00131 {
00132   setId(ind.getId());
00133   setSex(ind.getSex());
00134   try
00135   {
00136     setDate(*ind.getDate());
00137   }
00138   catch (NullPointerException)
00139   {
00140     date_ = 0;
00141   }
00142   try
00143   {
00144     setCoord(*ind.getCoord());
00145   }
00146   catch (NullPointerException)
00147   {
00148     coord_ = 0;
00149   }
00150   try
00151   {
00152     setLocality(ind.getLocality());
00153   }
00154   catch (NullPointerException)
00155   {
00156     locality_ = 0;
00157   }
00158   if (ind.hasSequences())
00159   {
00160     vector<string> keys = ind.getSequencesKeys();
00161     for (size_t i = 0; i < keys.size(); i++)
00162     {
00163       sequences_[keys[i]] = new VectorSequenceContainer(*const_cast<const VectorSequenceContainer*>(ind.getVectorSequenceContainer(keys[i])));
00164     }
00165   }
00166   genotype_ = ind.hasGenotype() ? new MultilocusGenotype(*ind.getGenotype()) : 0;
00167   return *this;
00168 }
00169 
00170 /******************************************************************************/
00171 
00172 // Id
00173 void MultiSeqIndividual::setId(const std::string id)
00174 {
00175   id_ = id;
00176 }
00177 
00178 /******************************************************************************/
00179 
00180 std::string MultiSeqIndividual::getId() const
00181 {
00182   return id_;
00183 }
00184 
00185 /******************************************************************************/
00186 
00187 // Sex
00188 void MultiSeqIndividual::setSex(const unsigned short sex)
00189 {
00190   sex_ = sex;
00191 }
00192 
00193 /******************************************************************************/
00194 
00195 unsigned short MultiSeqIndividual::getSex() const
00196 {
00197   return sex_;
00198 }
00199 
00200 /******************************************************************************/
00201 
00202 // Date
00203 void MultiSeqIndividual::setDate(const Date& date)
00204 {
00205   if (!hasDate())
00206   {
00207     date_ = new Date(date);
00208   }
00209   else if (*date_ != date)
00210   {
00211     delete date_;
00212     date_ = new Date(date);
00213   }
00214 }
00215 
00216 /******************************************************************************/
00217 
00218 const Date* MultiSeqIndividual::getDate() const throw (NullPointerException)
00219 {
00220   if (hasDate())
00221     return new Date(*date_);
00222   else
00223     throw (NullPointerException("MultiSeqIndividual::getDate: no date associated to this individual."));
00224 }
00225 
00226 /******************************************************************************/
00227 
00228 bool MultiSeqIndividual::hasDate() const
00229 {
00230   return date_ != 0;
00231 }
00232 
00233 /******************************************************************************/
00234 
00235 // Coord
00236 void MultiSeqIndividual::setCoord(const Point2D<double>& coord)
00237 {
00238   if (!hasCoord())
00239   {
00240     coord_ = new Point2D<double>(coord);
00241   }
00242   else if  (*coord_ != coord)
00243   {
00244     delete coord_;
00245     coord_ = new Point2D<double>(coord);
00246   }
00247 }
00248 
00249 /******************************************************************************/
00250 
00251 void MultiSeqIndividual::setCoord(const double x, const double y)
00252 {
00253   if (!hasCoord())
00254   {
00255     coord_ = new Point2D<double>(x, y);
00256   }
00257   else if (this->getX() != x || this->getY() != y)
00258   {
00259     delete coord_;
00260     coord_ = new Point2D<double>(x, y);
00261   }
00262 }
00263 
00264 /******************************************************************************/
00265 
00266 const Point2D<double>* MultiSeqIndividual::getCoord() const throw (NullPointerException)
00267 {
00268   if (hasCoord())
00269     return new Point2D<double>(*coord_);
00270   else
00271     throw (NullPointerException("MultiSeqIndividual::getCoord: no coord associated to this individual."));
00272 }
00273 
00274 /******************************************************************************/
00275 
00276 bool MultiSeqIndividual::hasCoord() const
00277 {
00278   return coord_ != 0;
00279 }
00280 
00281 /******************************************************************************/
00282 
00283 void MultiSeqIndividual::setX(const double x) throw (NullPointerException)
00284 {
00285   if (hasCoord())
00286     coord_->setX(x);
00287   else
00288     throw (NullPointerException("MultiSeqIndividual::setX: no coord associated to this individual."));
00289 }
00290 
00291 /******************************************************************************/
00292 
00293 void MultiSeqIndividual::setY(const double y) throw (NullPointerException)
00294 {
00295   if (hasCoord())
00296     coord_->setY(y);
00297   else
00298     throw (NullPointerException("MultiSeqIndividual::setY: no coord associated to this individual."));
00299 }
00300 
00301 /******************************************************************************/
00302 
00303 double MultiSeqIndividual::getX() const throw (NullPointerException)
00304 {
00305   if (hasCoord())
00306     return coord_->getX();
00307   else
00308     throw (NullPointerException("MultiSeqIndividual::getX: no coord associated to this individual."));
00309 }
00310 
00311 /******************************************************************************/
00312 
00313 double MultiSeqIndividual::getY() const throw (NullPointerException)
00314 {
00315   if (hasCoord())
00316     return coord_->getY();
00317   else
00318     throw (NullPointerException("MultiSeqIndividual::getY: no coord associated to this individual."));
00319 }
00320 
00321 /******************************************************************************/
00322 
00323 // Locality
00324 void MultiSeqIndividual::setLocality(const Locality<double>* locality)
00325 {
00326   locality_ = locality;
00327 }
00328 
00329 /******************************************************************************/
00330 
00331 const Locality<double>* MultiSeqIndividual::getLocality() const throw (NullPointerException)
00332 {
00333   if (hasLocality())
00334     return locality_;
00335   else
00336     throw (NullPointerException("MultiSeqIndividual::getLocality: no locality associated to this individual."));
00337 }
00338 
00339 /******************************************************************************/
00340 
00341 bool MultiSeqIndividual::hasLocality() const
00342 {
00343   return locality_ != 0;
00344 }
00345 
00346 /******************************************************************************/
00347 
00348 // Sequences
00349 const VectorSequenceContainer* MultiSeqIndividual::getVectorSequenceContainer(const std::string& id) const throw (Exception)
00350 {
00351   map<string, VectorSequenceContainer*>::const_iterator it;
00352   it = sequences_.find(id);
00353   // Test existence of id in the map.
00354   if (it == sequences_.end())
00355   {
00356     string mes = "MultiSeqIndividual::getSequence: sequence set not found (" + id
00357                  + ").";
00358     throw (Exception(mes));
00359   }
00360   return const_cast<const VectorSequenceContainer*>(it->second);
00361 }
00362 
00363 /******************************************************************************/
00364 
00365 void MultiSeqIndividual::addSequence(const std::string& id, const Sequence& sequence)
00366 throw (Exception)
00367 {
00368   try
00369   {
00370     sequences_[id]->addSequence(sequence);
00371   }
00372   catch (AlphabetMismatchException& ame)
00373   {
00374     throw (AlphabetMismatchException("MultiSeqIndividual::addSequence: alphabets don't match.", ame.getAlphabets()[0], ame.getAlphabets()[1]));
00375   }
00376   catch (Exception& e)
00377   {
00378     throw (BadIdentifierException("MultiSeqIndividual::addSequence: sequence's name already in use.", sequence.getName()));
00379   }
00380 }
00381 
00382 /******************************************************************************/
00383 
00384 const Sequence& MultiSeqIndividual::getSequence(const std::string& id, const std::string& name)
00385 const throw (Exception)
00386 {
00387   map<string, VectorSequenceContainer*>::const_iterator it;
00388   it = sequences_.find(id);
00389   // Test existence of id in the map.
00390   if (it == sequences_.end())
00391   {
00392     string mes = "MultiSeqIndividual::getSequence: sequence set not found (" + id
00393                  + ").";
00394     throw (Exception(mes));
00395   }
00396   try
00397   {
00398     return const_cast<const VectorSequenceContainer*>(it->second)->getSequence(name);
00399   }
00400   catch (SequenceNotFoundException& snfe)
00401   {
00402     throw (snfe);
00403   }
00404 }
00405 
00406 /******************************************************************************/
00407 
00408 const Sequence& MultiSeqIndividual::getSequence(const std::string& id, size_t i)
00409 const throw (Exception)
00410 {
00411   map<string, VectorSequenceContainer*>::const_iterator it;
00412   it = sequences_.find(id);
00413   // Test existence of id in the map.
00414   if (it == sequences_.end())
00415   {
00416     string mes = "MultiSeqIndividual::getSequence: sequence set not found (" + id
00417                  + ").";
00418     throw (Exception(mes));
00419   }
00420   try
00421   {
00422     return const_cast<const VectorSequenceContainer*>(it->second)->getSequence(i);
00423   }
00424   catch (IndexOutOfBoundsException& ioobe)
00425   {
00426     throw (ioobe);
00427   }
00428 }
00429 
00430 /******************************************************************************/
00431 
00432 std::vector<std::string> MultiSeqIndividual::getSequencesKeys() const
00433 {
00434   vector<string> keys;
00435   map<string, VectorSequenceContainer*>::const_iterator it;
00436   for (it = sequences_.begin(); it != sequences_.end(); it++)
00437   {
00438     keys.push_back(it->first);
00439   }
00440   return keys;
00441 }
00442 
00443 /******************************************************************************/
00444 
00445 bool MultiSeqIndividual::hasSequences() const
00446 {
00447   return sequences_.size() != 0;
00448 }
00449 
00450 /******************************************************************************/
00451 
00452 size_t MultiSeqIndividual::getNumberOfSequenceSet() const
00453 {
00454   return sequences_.size();
00455 }
00456 
00457 /******************************************************************************/
00458 
00459 size_t MultiSeqIndividual::getNumberOfSequences(const std::string& id) const
00460 throw (Exception)
00461 {
00462   map<string, VectorSequenceContainer*>::const_iterator it;
00463   it = sequences_.find(id);
00464   // Test existence of id in the map.
00465   if (it == sequences_.end())
00466   {
00467     string mes = "MultiSeqIndividual::getSequence: sequence set not found (" + id
00468                  + ").";
00469     throw (Exception(mes));
00470   }
00471 
00472   return const_cast<const VectorSequenceContainer*>(it->second)->getNumberOfSequences();
00473 }
00474 
00475 /******************************************************************************/
00476 
00477 // MultilocusGenotype
00478 
00479 void MultiSeqIndividual::addGenotype(const MultilocusGenotype& genotype)
00480 {
00481   genotype_ = new MultilocusGenotype(genotype);
00482 }
00483 
00484 /******************************************************************************/
00485 
00486 const MultilocusGenotype* MultiSeqIndividual::getGenotype() const throw (NullPointerException)
00487 {
00488   return genotype_;
00489 }
00490 
00491 /******************************************************************************/
00492 
00493 bool MultiSeqIndividual::hasGenotype() const
00494 {
00495   return genotype_ != 0;
00496 }
00497 
00498 /******************************************************************************/
00499 
 All Classes Namespaces Files Functions Variables Typedefs Friends