bpp-seq3  3.0.0
CompressedVectorSiteContainer.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_SEQ_CONTAINER_COMPRESSEDVECTORSITECONTAINER_H
6 #define BPP_SEQ_CONTAINER_COMPRESSEDVECTORSITECONTAINER_H
7 
9 
10 #include "../Site.h"
13 #include "SiteContainer.h"
14 
15 // From the STL library:
16 #include <string>
17 #include <vector>
18 #include <iostream>
19 
20 namespace bpp
21 {
42  public AbstractTemplateSequenceContainer<Sequence, std::string>,
43  public virtual TemplateSiteContainerInterface<Site, Sequence, std::string>
44 {
45 protected:
48  std::vector<std::string> sequenceNames_;
49  std::vector<Comments> sequenceComments_;
50  std::vector<size_t> index_; // For all sites, give the actual position in the set.
51 
52 public:
61  std::vector< std::unique_ptr<Site>>& vs,
62  std::shared_ptr<const Alphabet>& alphabet);
63 
71  size_t size,
72  std::shared_ptr<const Alphabet>& alphabet);
73 
81  const std::vector<std::string>& names,
82  std::shared_ptr<const Alphabet>& alphabet);
83 
89  CompressedVectorSiteContainer(std::shared_ptr<const Alphabet>& alphabet);
90 
93 
96 
98 
99 public:
106  {
107  return new CompressedVectorSiteContainer(*this);
108  }
116  const Site& site(size_t sitePosition) const override
117  {
118  return *siteContainer_.getObject(index_[sitePosition]);
119  }
120 
121  void setSite(size_t sitePosition, std::unique_ptr<Site>& site, bool checkCoordinate = true) override;
122 
123  std::unique_ptr<Site> removeSite(size_t sitePosition) override;
124 
125  void deleteSite(size_t sitePosition) override;
126 
127  void addSite(std::unique_ptr<Site>& site, bool checkCoordinate = false) override;
128 
129  void addSite(std::unique_ptr<Site>& site, size_t sitePosition, bool checkCoordinate = false) override;
130 
131  void deleteSites(size_t sitePosition, size_t length) override;
132 
133  size_t getNumberOfSites() const override { return index_.size(); }
134 
135  void reindexSites() override;
136 
137  Vint getSiteCoordinates() const override;
138 
139  void setSiteCoordinates(const Vint& coordinates) override;
140 
141 
148  size_t getNumberOfUniqueSites() const
149  {
150  return siteContainer_.getSize();
151  }
152 
153 
154  // These methods are implemented for this class:
155 
161  const Sequence& sequence(size_t sequenceIndex) const override;
162 
163  const Sequence& sequence(const std::string& sequenceKey) const override
164  {
165  // Look for sequence name:
166  size_t pos = getSequencePosition(sequenceKey);
167  return sequence(pos);
168  }
169 
170  bool hasSequence(const std::string& name) const override
171  {
172  // Look for sequence key:
173  return sequenceContainer_.hasObject(name);
174  }
175 
176  size_t getSequencePosition(const std::string& sequenceKey) const override
177  {
178  // Look for sequence key:
179  return sequenceContainer_.getObjectPosition(sequenceKey);
180  }
181 
182  std::unique_ptr<Sequence> removeSequence(size_t sequencePosition) override
183  {
184  // Implementing this function would involve (partially) decompressing the data...
185  throw NotImplementedException("CompressedVectorSiteContainer::removeSequence.");
186  }
187 
188  std::unique_ptr<Sequence> removeSequence(const std::string& sequenceKey) override
189  {
190  // Implementing this function would involve (partially) decompressing the data...
191  throw NotImplementedException("CompressedVectorSiteContainer::removeSequence.");
192  }
193 
194  void deleteSequence(size_t sequencePosition) override
195  {
196  // Implementing this function would involve (partially) decompressing the data...
197  throw NotImplementedException("CompressedVectorSiteContainer::deleteSequence.");
198  }
199 
200  void deleteSequence(const std::string& sequenceKey) override
201  {
202  // Implementing this function would involve (partially) decompressing the data...
203  throw NotImplementedException("CompressedVectorSiteContainer::deleteSequence.");
204  }
205 
206  size_t getNumberOfSequences() const override
207  {
208  return sequenceContainer_.getNumberOfObjects();
209  }
210 
211  std::vector<std::string> getSequenceKeys() const override
212  {
213  return sequenceContainer_.getObjectNames();
214  }
215 
216  void setSequenceKeys(const std::vector<std::string>& sequenceKeys) override
217  {
218  sequenceContainer_.setObjectNames(sequenceKeys);
219  }
220 
221  const std::string& sequenceKey(size_t sequencePosition) const override
222  {
223  return sequenceContainer_.getObjectName(sequencePosition);
224  }
225 
226  std::vector<std::string> getSequenceNames() const override
227  {
228  return sequenceNames_;
229  }
230 
231  void setSequenceNames(const std::vector<std::string>& names, bool updateKeys) override
232  {
233  if (names.size() != getNumberOfSequences())
234  throw DimensionException("CompressedVectorSiteContainer::setSequenceNames : bad number of names", names.size(), getNumberOfSequences());
235  sequenceContainer_.clear();
236  sequenceNames_ = names;
237  if (updateKeys)
238  {
239  setSequenceKeys(names);
240  }
241  }
242 
243  std::vector<Comments> getSequenceComments() const override
244  {
245  return sequenceComments_;
246  }
247 
248  void clear() override
249  {
250  siteContainer_.clear();
251  sequenceContainer_.clear();
252  sequenceNames_.clear();
253  sequenceComments_.clear();
254  index_.clear();
255  }
256 
258  {
259  auto alphaP = getAlphabet();
261  vsc->setComments(getComments());
262  return vsc;
263  }
264 
267  void setSequence(size_t sequencePosition, std::unique_ptr<Sequence>& sequence, const std::string& sequenceKey) override
268  {
269  // Implementing this function would involve (partially) decompressing the data...
270  throw NotImplementedException("CompressedVectorSiteContainer::setSequence.");
271  }
272 
273  void setSequence(size_t sequencePosition, std::unique_ptr<Sequence>& sequence) override
274  {
275  // Implementing this function would involve (partially) decompressing the data...
276  throw NotImplementedException("CompressedVectorSiteContainer::setSequence.");
277  }
278 
279  void setSequence(const std::string& sequenceKey, std::unique_ptr<Sequence>& sequence) override
280  {
281  // Implementing this function would involve (partially) decompressing the data...
282  throw NotImplementedException("CompressedVectorSiteContainer::setSequence.");
283  }
284 
285  void addSequence(const std::string& sequenceKey, std::unique_ptr<Sequence>& sequence) override
286  {
287  // Implementing this function would involve (partially) decompressing the data...
288  throw NotImplementedException("CompressedVectorSiteContainer::addSequence.");
289  }
290 
291  void insertSequence(size_t sequencePosition, std::unique_ptr<Sequence>& sequence, const std::string& sequenceKey) override
292  {
293  // Implementing this function would involve (partially) decompressing the data...
294  throw NotImplementedException("CompressedVectorSiteContainer::insertSequence.");
295  }
296 
297  const int& valueAt(const std::string& sequenceKey, size_t sitePosition) const override
298  {
299  return site(sitePosition)[getSequencePosition(sequenceKey)];
300  }
301 
302  int& valueAt(const std::string& sequenceKey, size_t sitePosition) override
303  {
304  // Implementing this function would involve (partially) decompressing the data...
305  throw NotImplementedException("CompressedVectorSiteContainer::valueAt (non const).");
306  }
307 
308  const int& valueAt(size_t sequencePosition, size_t sitePosition) const override
309  {
310  return site(sitePosition)[sequencePosition];
311  }
312 
313  int& valueAt(size_t sequencePosition, size_t sitePosition) override
314  {
315  // Implementing this function would involve (partially) decompressing the data...
316  throw NotImplementedException("CompressedVectorSiteContainer::valueAt (non const).");
317  }
318 
324  double getStateValueAt(size_t sitePosition, const std::string& sequenceKey, int state) const override
325  {
326  return site(sitePosition).getStateValueAt(getSequencePosition(sequenceKey), state);
327  }
328 
329  double operator()(size_t sitePosition, const std::string& sequenceKey, int state) const override
330  {
331  return site(sitePosition).getStateValueAt(getSequencePosition(sequenceKey), state);
332  }
333 
334  double getStateValueAt(size_t sitePosition, size_t sequencePosition, int state) const override
335  {
336  return site(sitePosition).getStateValueAt(sequencePosition, state);
337  }
338 
339  double operator()(size_t sitePosition, size_t sequencePosition, int state) const override
340  {
341  return site(sitePosition).getStateValueAt(sequencePosition, state);
342  }
343 
346 protected:
354  Site& getSite_(size_t sitePosition)
355  {
356  return *siteContainer_.getObject(index_[sitePosition]);
357  }
358 
363  size_t getSiteIndex_(const Site& site);
364 };
365 } // end of namespace bpp.
366 #endif // BPP_SEQ_CONTAINER_COMPRESSEDVECTORSITECONTAINER_H
Partial implementation of the SequenceContainer interface.
std::shared_ptr< const Alphabet > getAlphabet() const override
A low memory, yet restricted, version of the VectorSiteContainer class.
const int & valueAt(const std::string &sequenceKey, size_t sitePosition) const override
size_t getNumberOfSites() const override
Get the number of aligned positions in the container.
std::vector< std::string > getSequenceKeys() const override
int & valueAt(size_t sequencePosition, size_t sitePosition) override
Get the content of the dataset at a specific position (sequence position, site position).
std::unique_ptr< Site > removeSite(size_t sitePosition) override
Remove a site from the container.
size_t getNumberOfSequences() const override
Get the number of sequences in the container.
void deleteSequence(size_t sequencePosition) override
Remove and delete a sequence from the container.
const std::string & sequenceKey(size_t sequencePosition) const override
Get the key associated to a given sequence.
void setSequence(size_t sequencePosition, std::unique_ptr< Sequence > &sequence, const std::string &sequenceKey) override
void setSequenceNames(const std::vector< std::string > &names, bool updateKeys) override
Batch-set all sequence names.
const Site & site(size_t sitePosition) const override
Get a site from the container.
void setSiteCoordinates(const Vint &coordinates) override
Set all coordinates of sites.
void reindexSites() override
Set all coordinate attributes.
VectorMappedContainer< Sequence > sequenceContainer_
const Sequence & sequence(size_t sequenceIndex) const override
Retrieve a sequence object from the container.
CompressedVectorSiteContainer(std::vector< std::unique_ptr< Site >> &vs, std::shared_ptr< const Alphabet > &alphabet)
Build a new container from a set of sites.
void setSequenceKeys(const std::vector< std::string > &sequenceKeys) override
Reset all sequence keys.
std::unique_ptr< Sequence > removeSequence(size_t sequencePosition) override
Remove a sequence from the container.
Vint getSiteCoordinates() const override
Get all coordinates of sites.
std::unique_ptr< Sequence > removeSequence(const std::string &sequenceKey) override
int & valueAt(const std::string &sequenceKey, size_t sitePosition) override
std::vector< Comments > getSequenceComments() const override
void deleteSequence(const std::string &sequenceKey) override
Remove and delete a sequence from the container.
void clear() override
Delete all data in the container.
VectorPositionedContainer< Site > siteContainer_
bool hasSequence(const std::string &name) const override
Check if a certain key is associated to a sequence in the container.
void insertSequence(size_t sequencePosition, std::unique_ptr< Sequence > &sequence, const std::string &sequenceKey) override
size_t getSequencePosition(const std::string &sequenceKey) const override
Get the position of a sequence with a given key in the container.
std::vector< std::string > getSequenceNames() const override
const int & valueAt(size_t sequencePosition, size_t sitePosition) const override
Get the content of the dataset at a specific position (sequence position, site position).
double operator()(size_t sitePosition, const std::string &sequenceKey, int state) const override
Get the value of a state at a given position.
CompressedVectorSiteContainer * clone() const override
void setSequence(size_t sequencePosition, std::unique_ptr< Sequence > &sequence) override
CompressedVectorSiteContainer & operator=(const CompressedVectorSiteContainer &vsc)
const Sequence & sequence(const std::string &sequenceKey) const override
Retrieve a sequence object from the container.
void addSite(std::unique_ptr< Site > &site, bool checkCoordinate=false) override
Add a site in the container.
double getStateValueAt(size_t sitePosition, size_t sequencePosition, int state) const override
Get value of a state at a given position.
void deleteSites(size_t sitePosition, size_t length) override
Remove a continuous range of sites in the container.
void deleteSite(size_t sitePosition) override
Delete a site from the container.
void setSite(size_t sitePosition, std::unique_ptr< Site > &site, bool checkCoordinate=true) override
Set a site in the container.
double operator()(size_t sitePosition, size_t sequencePosition, int state) const override
Get the value of a state at a given position.
CompressedVectorSiteContainer * createEmptyContainer() const override
Return a copy of this container, but with no data inside.
void setSequence(const std::string &sequenceKey, std::unique_ptr< Sequence > &sequence) override
void addSequence(const std::string &sequenceKey, std::unique_ptr< Sequence > &sequence) override
double getStateValueAt(size_t sitePosition, const std::string &sequenceKey, int state) const override
Get the value of a state at a given position.
A basic implementation of the Sequence interface.
Definition: Sequence.h:117
const Comments & getComments() const override
Get the comments.
Definition: Commentable.h:79
void setComments(const Comments &comments) override
Set the comments.
Definition: Commentable.h:86
The Site class.
Definition: Site.h:73
double getStateValueAt(size_t sequencePosition, int state) const
get value of a state at a position
Definition: Site.h:187
The SiteContainer interface.
Definition: SiteContainer.h:29
The template VectorMappedContainer class.
The template VectorPositionedContainer class.
This alphabet is used to deal NumericAlphabet.
std::vector< int > Vint