bpp-seq  2.2.0
VectorSiteContainer.h
Go to the documentation of this file.
1 //
2 // File: VectorSiteContainer.h
3 // Created by: Julien Dutheil
4 // Created on: Mon Oct 6 11:50:40 2003
5 //
6 
7 /*
8  Copyright or © or Copr. CNRS, (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 _VECTORSITECONTAINER_H_
41 #define _VECTORSITECONTAINER_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 {
70  // This container implements the SequenceContainer interface
71  // and use the AbstractSequenceContainer adapter.
72  public virtual SiteContainer // This container is a SiteContainer.
73 {
74 protected:
75  std::vector<Site*> sites_;
76  std::vector<std::string> names_;
77  std::vector<Comments*> comments_; // Sequences comments.
78  mutable std::vector<Sequence*> sequences_; // To store pointer toward sequences retrieves (cf. AlignedSequenceContainer).
79 
80 public:
89  VectorSiteContainer(const std::vector<const Site*>& vs, const Alphabet* alpha, bool checkPositions = true) throw (Exception);
96  VectorSiteContainer(size_t size, const Alphabet* alpha);
103  VectorSiteContainer(const std::vector<std::string>& names, const Alphabet* alpha);
104 
110  VectorSiteContainer(const Alphabet* alpha);
111 
116 
117  VectorSiteContainer& operator=(const VectorSiteContainer& vsc);
118  VectorSiteContainer& operator=(const SiteContainer& sc);
119  VectorSiteContainer& operator=(const OrderedSequenceContainer& osc);
120  VectorSiteContainer& operator=(const SequenceContainer& sc);
121 
122  virtual ~VectorSiteContainer() { clear(); }
123 
124 public:
130  VectorSiteContainer* clone() const { return new VectorSiteContainer(*this); }
138  const Site& getSite(size_t siteIndex) const throw (IndexOutOfBoundsException);
139  void setSite(size_t siteIndex, const Site& site, bool checkPosition = true) throw (Exception);
140  Site* removeSite(size_t siteIndex) throw (IndexOutOfBoundsException);
141  void deleteSite(size_t siteIndex) throw (IndexOutOfBoundsException);
142  void deleteSites(size_t siteIndex, size_t length) throw (IndexOutOfBoundsException);
143  void addSite(const Site& site, bool checkPosition = true) throw (Exception);
144  void addSite(const Site& site, int position, bool checkPosition = true) throw (Exception);
145  void addSite(const Site& site, size_t siteIndex, bool checkPosition = true) throw (Exception);
146  void addSite(const Site& site, size_t siteIndex, int position, bool checkPosition = true) throw (Exception);
147  size_t getNumberOfSites() const;
148  void reindexSites();
149  Vint getSitePositions() const;
152  // Theses methods are implemented for this class:
153 
159  void setComments(size_t sequenceIndex, const Comments& comments) throw (IndexOutOfBoundsException);
160 
161  // Method to get a sequence object from sequence container
162  const Sequence& getSequence(size_t sequenceIndex) const throw (IndexOutOfBoundsException);
163  const Sequence& getSequence(const std::string& name) const throw (SequenceNotFoundException);
164  bool hasSequence(const std::string& name) const;
165 
166  // Methods to get position of a sequence in sequence container from his name
167  // This method is used by delete and remove methods
168  size_t getSequencePosition(const std::string& name) const throw (SequenceNotFoundException);
169 
170  Sequence* removeSequence(size_t sequenceIndex) throw (IndexOutOfBoundsException);
171  Sequence* removeSequence(const std::string& name) throw (SequenceNotFoundException);
172 
173  void deleteSequence(size_t sequenceIndex) throw (IndexOutOfBoundsException);
174  void deleteSequence(const std::string& name) throw (SequenceNotFoundException);
175 
176  size_t getNumberOfSequences() const { return names_.size(); }
177 
178  std::vector<std::string> getSequencesNames() const;
179 
180  void setSequencesNames(const std::vector<std::string>& names, bool checkNames = true) throw (Exception);
181 
182  void clear();
183 
185 
186  int& valueAt(const std::string& sequenceName, size_t elementIndex) throw (SequenceNotFoundException, IndexOutOfBoundsException)
187  {
188  if (elementIndex >= getNumberOfSites()) throw IndexOutOfBoundsException("VectorSiteContainer::valueAt(std::string, size_t).", elementIndex, 0, getNumberOfSites() - 1);
189  return (*sites_[elementIndex])[getSequencePosition(sequenceName)];
190  }
191  const int& valueAt(const std::string& sequenceName, size_t elementIndex) const throw (SequenceNotFoundException, IndexOutOfBoundsException)
192  {
193  if (elementIndex >= getNumberOfSites()) throw IndexOutOfBoundsException("VectorSiteContainer::valueAt(std::string, size_t).", elementIndex, 0, getNumberOfSites() - 1);
194  return (*sites_[elementIndex])[getSequencePosition(sequenceName)];
195  }
196  int& operator()(const std::string& sequenceName, size_t elementIndex)
197  {
198  return (*sites_[elementIndex])[getSequencePosition(sequenceName)];
199  }
200  const int& operator()(const std::string& sequenceName, size_t elementIndex) const
201  {
202  return (*sites_[elementIndex])[getSequencePosition(sequenceName)];
203  }
204 
205  int& valueAt(size_t sequenceIndex, size_t elementIndex) throw (IndexOutOfBoundsException)
206  {
207  if (sequenceIndex >= getNumberOfSequences()) throw IndexOutOfBoundsException("VectorSiteContainer::valueAt(size_t, size_t).", sequenceIndex, 0, getNumberOfSequences() - 1);
208  if (elementIndex >= getNumberOfSites()) throw IndexOutOfBoundsException("VectorSiteContainer::valueAt(size_t, size_t).", elementIndex, 0, getNumberOfSites() - 1);
209  return (*sites_[elementIndex])[sequenceIndex];
210  }
211  const int& valueAt(size_t sequenceIndex, size_t elementIndex) const throw (IndexOutOfBoundsException)
212  {
213  if (sequenceIndex >= getNumberOfSequences()) throw IndexOutOfBoundsException("VectorSiteContainer::valueAt(size_t, size_t).", sequenceIndex, 0, getNumberOfSequences() - 1);
214  if (elementIndex >= getNumberOfSites()) throw IndexOutOfBoundsException("VectorSiteContainer::valueAt(size_t, size_t).", elementIndex, 0, getNumberOfSites() - 1);
215  return (*sites_[elementIndex])[sequenceIndex];
216  }
217  int& operator()(size_t sequenceIndex, size_t elementIndex)
218  {
219  return (*sites_[elementIndex])[sequenceIndex];
220  }
221  const int& operator()(size_t sequenceIndex, size_t elementIndex) const
222  {
223  return (*sites_[elementIndex])[sequenceIndex];
224  }
227  void addSequence(const Sequence& sequence, bool checkName = true) throw (Exception);
228  void addSequence(const Sequence& sequence, size_t sequenceIndex, bool checkName = true) throw (Exception);
229 
230  void setSequence(const std::string& name, const Sequence& sequence, bool checkName) throw (Exception);
231  void setSequence(size_t sequenceIndex, const Sequence& sequence, bool checkName) throw (Exception);
232 
233 protected:
234  // Create n void sites:
235  void realloc(size_t n);
236 };
237 } // end of namespace bpp.
238 
239 #endif // _VECTORSITECONTAINER_H_
240 
Exception thrown when a sequence is not found The sequence not found exception base class...
const int & valueAt(const std::string &sequenceName, size_t elementIndex) const
Element access function.
void addSequence(const Sequence &sequence, bool checkName=true)
Add a sequence to the container.
std::vector< std::string > Comments
Declaration of Comments type.
Definition: Sequence.h:60
std::vector< Comments * > comments_
void setSite(size_t siteIndex, const Site &site, bool checkPosition=true)
Set a site in the container.
The SiteContainer interface.
Definition: SiteContainer.h:63
The OrderedSequenceContainer interface.
const Sequence & getSequence(size_t sequenceIndex) const
Retrieve a sequence object from the container.
This alphabet is used to deal NumericAlphabet.
The Alphabet interface.
Definition: Alphabet.h:130
STL namespace.
Site * removeSite(size_t siteIndex)
Remove a site from the container.
std::vector< std::string > names_
Partial implementation of the OrderedSequenceContainer interface.
void clear()
Delete all sequences in the container.
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.
void setSequencesNames(const std::vector< std::string > &names, bool checkNames=true)
Set all sequence names.
std::vector< Sequence * > sequences_
Vint getSitePositions() const
Get all position attributes of sites.
int & operator()(const std::string &sequenceName, size_t elementIndex)
Element access operator.
void setSequence(const std::string &name, const Sequence &sequence, bool checkName)
Replace a sequence in the container.
void addSite(const Site &site, bool checkPosition=true)
Add a site in the container.
const int & operator()(const std::string &sequenceName, size_t elementIndex) const
Element access operator.
Sequence * removeSequence(size_t sequenceIndex)
Extract (and remove) a sequence from the container.
void deleteSequence(size_t sequenceIndex)
Delete a sequence of the container.
void deleteSites(size_t siteIndex, size_t length)
Delete a continuous range of sites in the container.
const Site & getSite(size_t siteIndex) const
Get a site from the container.
size_t getNumberOfSites() const
Get the number of sites in the container.
void reindexSites()
Set all positions attributes.
VectorSiteContainer * createEmptyContainer() const
Return a copy of this container, but with no sequence inside.
int & valueAt(size_t sequenceIndex, size_t elementIndex)
Element access operator.
size_t getSequencePosition(const std::string &name) const
Get the position of a sequence in sequence container from its name.
std::vector< Site * > sites_
The sequence interface.
Definition: Sequence.h:74
const int & operator()(size_t sequenceIndex, size_t elementIndex) const
Element access operator.
int & valueAt(const std::string &sequenceName, size_t elementIndex)
Element access function.
VectorSiteContainer * clone() const
std::vector< std::string > getSequencesNames() const
Get all the names of the sequences in the container.
The Site class.
Definition: Site.h:61
VectorSiteContainer(const std::vector< const Site *> &vs, const Alphabet *alpha, bool checkPositions=true)
Build a new container from a set of sites.
bool hasSequence(const std::string &name) const
Check if a sequence with a given name is present in the container.
int & operator()(size_t sequenceIndex, size_t elementIndex)
Element access operator.
const int & valueAt(size_t sequenceIndex, size_t elementIndex) const
Element access operator.
The SequenceContainer interface.
The VectorSiteContainer class.
size_t getNumberOfSequences() const
Get the number of sequences in the container.