bpp-seq  2.2.0
CompressedVectorSiteContainer.h
Go to the documentation of this file.
1 //
2 // File: CompressedVectorSiteContainer.h
3 // Created by: Julien Dutheil
4 // Created on: Wed Dec 16 12:08 2009
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 sequences 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 #ifndef _COMPRESSEDVECTORSITECONTAINER_H_
41 #define _COMPRESSEDVECTORSITECONTAINER_H_
42 
43 #include "../Site.h"
44 #include "SiteContainer.h"
48 #include <Bpp/Numeric/VectorTools.h>
49 
50 // From the STL library:
51 #include <string>
52 #include <vector>
53 #include <iostream>
54 
55 namespace bpp
56 {
78  // This container implements the SequenceContainer interface
79  // and use the AbstractSequenceContainer adapter.
80  public virtual SiteContainer // This container is a SiteContainer.
81 {
82 protected:
83  std::vector<Site*> sites_; //A set of unique sites.
84  std::vector<size_t> index_; //For all sites, give the actual position in the set.
85  std::vector<std::string> names_;
86  std::vector<Comments*> comments_; // Sequences comments.
87  mutable std::vector<Sequence*> sequences_; // To store pointer toward sequences retrieved (cf. AlignedSequenceContainer).
88 
89 public:
97  CompressedVectorSiteContainer(const std::vector<const Site*>& vs, const Alphabet* alpha) throw (Exception);
104  CompressedVectorSiteContainer(size_t size, const Alphabet* alpha);
111  CompressedVectorSiteContainer(const std::vector<std::string>& names, const Alphabet* alpha);
112 
119 
122 
125 
127 
128 public:
142  const Site& getSite(size_t siteIndex) const throw (IndexOutOfBoundsException);
143  void setSite(size_t siteIndex, const Site& site, bool checkPosition = false) throw (Exception);
144  Site* removeSite(size_t siteIndex) throw (IndexOutOfBoundsException);
145  void deleteSite(size_t siteIndex) throw (IndexOutOfBoundsException);
146  void deleteSites(size_t siteIndex, size_t length) throw (IndexOutOfBoundsException);
147  void addSite(const Site& site, bool checkPosition = false) throw (Exception);
148  void addSite(const Site& site, int position, bool checkPosition = false) throw (Exception)
149  {
150  addSite(site, checkPosition);
151  }
152  void addSite(const Site& site, size_t siteIndex, bool checkPosition = false) throw (Exception);
153  void addSite(const Site& site, size_t siteIndex, int position, bool checkPosition = false) throw (Exception)
154  {
155  addSite(site, siteIndex, checkPosition);
156  }
157  size_t getNumberOfSites() const { return index_.size(); }
158  void reindexSites();
159  Vint getSitePositions() const;
162  // Theses methods are implemented for this class:
163 
169  void setComments(size_t sequenceIndex, const Comments& comments) throw (IndexOutOfBoundsException);
170 
171  // Method to get a sequence object from sequence container
172  const Sequence& getSequence(size_t sequenceIndex) const throw (IndexOutOfBoundsException);
173  const Sequence& getSequence(const std::string& name) const throw (SequenceNotFoundException);
174  bool hasSequence(const std::string& name) const;
175 
176  // Methods to get position of a sequence in sequence container from his name
177  // This method is used by delete and remove methods
178  size_t getSequencePosition(const std::string& name) const throw (SequenceNotFoundException);
179 
180  Sequence* removeSequence(size_t sequenceIndex) throw (IndexOutOfBoundsException, NotImplementedException)
181  {
182  //Implementing this function would involve (partially) decompressing the data...
183  throw NotImplementedException("CompressedVectorSiteContainer::removeSequence.");
184  }
185 
186  Sequence* removeSequence(const std::string& name) throw (SequenceNotFoundException, NotImplementedException)
187  {
188  //Implementing this function would involve (partially) decompressing the data...
189  throw NotImplementedException("CompressedVectorSiteContainer::removeSequence.");
190  }
191 
192  void deleteSequence(size_t sequenceIndex) throw (IndexOutOfBoundsException, NotImplementedException)
193  {
194  //Implementing this function would involve (partially) decompressing the data...
195  throw NotImplementedException("CompressedVectorSiteContainer::deleteSequence.");
196  }
197 
198  void deleteSequence(const std::string& name) throw (SequenceNotFoundException, NotImplementedException)
199  {
200  //Implementing this function would involve (partially) decompressing the data...
201  throw NotImplementedException("CompressedVectorSiteContainer::deleteSequence.");
202  }
203 
204  size_t getNumberOfSequences() const { return names_.size(); }
205 
206  std::vector<std::string> getSequencesNames() const;
207 
208  void setSequencesNames(const std::vector<std::string>& names, bool checkNames = true) throw (Exception);
209 
210  void clear();
211 
213 
214  int& valueAt(const std::string& sequenceName, size_t elementIndex) throw (SequenceNotFoundException, IndexOutOfBoundsException)
215  {
216  if (elementIndex >= getNumberOfSites()) throw IndexOutOfBoundsException("VectorSiteContainer::operator(std::string, size_t).", elementIndex, 0, getNumberOfSites() - 1);
217  return (*sites_[index_[elementIndex]])[getSequencePosition(sequenceName)];
218  }
219  const int& valueAt(const std::string& sequenceName, size_t elementIndex) const throw (SequenceNotFoundException, IndexOutOfBoundsException)
220  {
221  if (elementIndex >= getNumberOfSites()) throw IndexOutOfBoundsException("VectorSiteContainer::operator(std::string, size_t).", elementIndex, 0, getNumberOfSites() - 1);
222  return (*sites_[index_[elementIndex]])[getSequencePosition(sequenceName)];
223  }
224  int& operator()(const std::string& sequenceName, size_t elementIndex)
225  {
226  return (*sites_[index_[elementIndex]])[getSequencePosition(sequenceName)];
227  }
228  const int& operator()(const std::string& sequenceName, size_t elementIndex) const
229  {
230  return (*sites_[index_[elementIndex]])[getSequencePosition(sequenceName)];
231  }
232 
233  int& valueAt(size_t sequenceIndex, size_t elementIndex) throw (IndexOutOfBoundsException)
234  {
235  if (sequenceIndex >= getNumberOfSequences()) throw IndexOutOfBoundsException("VectorSiteContainer::operator(size_t, size_t).", sequenceIndex, 0, getNumberOfSequences() - 1);
236  if (elementIndex >= getNumberOfSites()) throw IndexOutOfBoundsException("VectorSiteContainer::operator(size_t, size_t).", elementIndex, 0, getNumberOfSites() - 1);
237  return (*sites_[index_[elementIndex]])[sequenceIndex];
238  }
239  const int& valueAt(size_t sequenceIndex, size_t elementIndex) const throw (IndexOutOfBoundsException)
240  {
241  if (sequenceIndex >= getNumberOfSequences()) throw IndexOutOfBoundsException("VectorSiteContainer::operator(size_t, size_t).", sequenceIndex, 0, getNumberOfSequences() - 1);
242  if (elementIndex >= getNumberOfSites()) throw IndexOutOfBoundsException("VectorSiteContainer::operator(size_t, size_t).", elementIndex, 0, getNumberOfSites() - 1);
243  return (*sites_[index_[elementIndex]])[sequenceIndex];
244  }
245  int& operator()(size_t sequenceIndex, size_t elementIndex)
246  {
247  return (*sites_[index_[elementIndex]])[sequenceIndex];
248  }
249  const int& operator()(size_t sequenceIndex, size_t elementIndex) const
250  {
251  return (*sites_[index_[elementIndex]])[sequenceIndex];
252  }
255  void addSequence(const Sequence& sequence, bool checkName = true) throw (Exception, NotImplementedException)
256  {
257  //Implementing this function would involve (partially) decompressing the data...
258  throw NotImplementedException("CompressedVectorSiteContainer::addSequence.");
259  }
260 
261  void addSequence(const Sequence& sequence, size_t sequenceIndex, bool checkName = true) throw (Exception, NotImplementedException)
262  {
263  //Implementing this function would involve (partially) decompressing the data...
264  throw NotImplementedException("CompressedVectorSiteContainer::addSequence.");
265  }
266 
267  void setSequence(const std::string& name, const Sequence& sequence, bool checkName) throw (Exception, NotImplementedException)
268  {
269  //Implementing this function would involve (partially) decompressing the data...
270  throw NotImplementedException("CompressedVectorSiteContainer::setSequence.");
271  }
272 
273  void setSequence(size_t sequenceIndex, const Sequence& sequence, bool checkName) throw (Exception, NotImplementedException)
274  {
275  //Implementing this function would involve (partially) decompressing the data...
276  throw NotImplementedException("CompressedVectorSiteContainer::setSequence.");
277  }
278 
279 protected:
284  size_t getSiteIndex_(const Site& site);
285 };
286 
287 } // end of namespace bpp.
288 
289 #endif // _COMPRESSEDVECTORSITECONTAINER_H_
290 
Exception thrown when a sequence is not found The sequence not found exception base class...
std::vector< std::string > Comments
Declaration of Comments type.
Definition: Sequence.h:60
void addSequence(const Sequence &sequence, bool checkName=true)
Add a sequence to the container.
const Site & getSite(size_t siteIndex) const
Get a site from the container.
The SiteContainer interface.
Definition: SiteContainer.h:63
This alphabet is used to deal NumericAlphabet.
Sequence * removeSequence(size_t sequenceIndex)
Extract (and remove) a sequence from the container.
int & valueAt(size_t sequenceIndex, size_t elementIndex)
Element access operator.
The Alphabet interface.
Definition: Alphabet.h:130
const Sequence & getSequence(size_t sequenceIndex) const
Retrieve a sequence object from the container.
void setSite(size_t siteIndex, const Site &site, bool checkPosition=false)
Set a site in the container.
STL namespace.
void reindexSites()
Set all positions attributes.
CompressedVectorSiteContainer(const std::vector< const Site *> &vs, const Alphabet *alpha)
Build a new container from a set of sites.
Partial implementation of the OrderedSequenceContainer interface.
size_t getNumberOfSites() const
Get the number of sites in the container.
const int & operator()(size_t sequenceIndex, size_t elementIndex) const
Element access operator.
CompressedVectorSiteContainer * createEmptyContainer() const
Return a copy of this container, but with no sequence inside.
void deleteSequence(const std::string &name)
Delete a sequence of the container.
void addSequence(const Sequence &sequence, size_t sequenceIndex, bool checkName=true)
Vint getSitePositions() const
Get all position attributes of sites.
const int & operator()(const std::string &sequenceName, size_t elementIndex) const
Element access operator.
A low memory, yet restricted, version of the VectorSiteContainer class.
CompressedVectorSiteContainer * clone() const
Sequence * removeSequence(const std::string &name)
Extract (and remove) a sequence from the container.
size_t getNumberOfSequences() const
Get the number of sequences in the container.
bool hasSequence(const std::string &name) const
Check if a sequence with a given name is present in the container.
void addSite(const Site &site, bool checkPosition=false)
Add a site in the container.
void setSequencesNames(const std::vector< std::string > &names, bool checkNames=true)
Set all sequence names.
int & operator()(size_t sequenceIndex, size_t elementIndex)
Element access operator.
int & valueAt(const std::string &sequenceName, size_t elementIndex)
Element access function.
std::vector< std::string > getSequencesNames() const
Get all the names of the sequences in the container.
void clear()
Delete all sequences in the container.
CompressedVectorSiteContainer & operator=(const CompressedVectorSiteContainer &vsc)
const int & valueAt(size_t sequenceIndex, size_t elementIndex) const
Element access operator.
void deleteSite(size_t siteIndex)
Delete a site in the container.
void setComments(size_t sequenceIndex, const Comments &comments)
Set the comments of a particular sequence.
size_t getSequencePosition(const std::string &name) const
Get the position of a sequence in sequence container from its name.
The sequence interface.
Definition: Sequence.h:74
const int & valueAt(const std::string &sequenceName, size_t elementIndex) const
Element access function.
void setSequence(const std::string &name, const Sequence &sequence, bool checkName)
Replace a sequence in the container.
void deleteSequence(size_t sequenceIndex)
Delete a sequence of the container.
void setSequence(size_t sequenceIndex, const Sequence &sequence, bool checkName)
Replace a sequence in the container.
int & operator()(const std::string &sequenceName, size_t elementIndex)
Element access operator.
The Site class.
Definition: Site.h:61
Site * removeSite(size_t siteIndex)
Remove a site from the container.
void deleteSites(size_t siteIndex, size_t length)
Delete a continuous range of sites in the container.