bpp-seq3  3.0.0
VectorSequenceContainer.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_VECTORSEQUENCECONTAINER_H
6 #define BPP_SEQ_CONTAINER_VECTORSEQUENCECONTAINER_H
7 
8 #include <Bpp/Exceptions.h>
9 
10 #include "../Alphabet/Alphabet.h"
11 #include "../Sequence.h"
12 #include "../ProbabilisticSequence.h"
14 #include "VectorMappedContainer.h"
15 
16 // From the STL:
17 #include <algorithm>
18 #include <vector>
19 #include <memory>
20 
21 namespace bpp
22 {
29 template<class SequenceType>
31  public AbstractTemplateSequenceContainer<SequenceType, std::string>
32 {
33 protected:
35 
36 public:
46  std::shared_ptr<const Alphabet> alphabet,
47  std::vector<std::unique_ptr<SequenceType>>& vs) :
48  AbstractTemplateSequenceContainer<SequenceType, std::string>(alphabet),
50  {
51  for (auto& seqPtr : vs)
52  {
53  addSequence(seqPtr->getName(), seqPtr);
54  }
55  }
56 
62  TemplateVectorSequenceContainer(std::shared_ptr<const Alphabet> alphabet) :
65  {}
66 
80  AbstractTemplateSequenceContainer<SequenceType, std::string>(vsc),
82  {
83  size_t max = vsc.getNumberOfSequences();
84  for (size_t i = 0; i < max; ++i)
85  {
86  auto seqPtr = std::unique_ptr<SequenceType>(vsc.sequence(i).clone());
87  addSequence(vsc.sequenceKey(i), seqPtr);
88  }
89  }
90 
91 
100  {
101  size_t max = sc.getNumberOfSequences();
102  for (size_t i = 0; i < max; ++i)
103  {
104  auto seqPtr = std::unique_ptr<SequenceType>(sc.sequence(i).clone());
105  addSequence(sc.sequenceKey(i), seqPtr);
106  }
107  setComments(sc.getComments());
108  }
109 
110 
119  {
120  clear();
122  size_t max = vsc.getNumberOfSequences();
123  for (size_t i = 0; i < max; ++i)
124  {
125  auto seqPtr = std::unique_ptr<SequenceType>(vsc.sequence(i).clone());
126  addSequence(vsc.sequenceKey(i), seqPtr);
127  }
128  return *this;
129  }
130 
137  {
138  clear();
140  size_t max = sc.getNumberOfSequences();
141  for (size_t i = 0; i < max; ++i)
142  {
143  auto seqPtr = std::unique_ptr<SequenceType>(sc.sequence(i).clone());
144  addSequence(sc.sequenceKey(i), seqPtr);
145  }
146  setComments(sc.getComments());
147  return *this;
148  }
149 
151 
152  void clear() override
153  {
155  }
156 
157 public:
175 
177  {
178  auto alphaPtr = getAlphabet();
180  vsc->setComments(getComments());
181  return vsc;
182  }
183 
184  double getStateValueAt(size_t sitePosition, const std::string& sequenceKey, int state) const override
185  {
186  return sequence(sequenceKey).getStateValueAt(sitePosition, state);
187  }
188 
189  double operator()(size_t sitePosition, const std::string& sequenceKey, int state) const override
190  {
191  return sequence(sequenceKey)(sitePosition, state);
192  }
193 
194  double getStateValueAt(size_t sitePosition, size_t sequencePosition, int state) const override
195  {
196  return sequence(sequencePosition).getStateValueAt(sitePosition, state);
197  }
198 
199  double operator()(size_t sitePosition, size_t sequencePosition, int state) const override
200  {
201  return sequence(sequencePosition)(sitePosition, state);
202  }
203 
204  size_t getNumberOfSequences() const override
205  {
206  return sequenceVectorMap_.getSize();
207  }
208 
209  std::vector<std::string> getSequenceKeys() const override
210  {
212  }
213 
214  void setSequenceKeys(const std::vector<std::string>& sequenceKeys) override
215  {
216  sequenceVectorMap_.setObjectNames(sequenceKeys);
217  }
218 
219  const std::string& sequenceKey(size_t sequencePosition) const override
220  {
221  return sequenceVectorMap_.getObjectName(sequencePosition);
222  }
223 
224  size_t getSequencePosition(const std::string& sequenceKey) const override
225  {
227  }
228 
229  const typename SequenceType::ElementType& valueAt(const std::string& sequenceKey, size_t elementPosition) const override
230  {
231  return sequence(sequenceKey)[elementPosition];
232  }
233 
234  typename SequenceType::ElementType& valueAt(const std::string& sequenceKey, size_t elementPosition) override
235  {
236  return sequence_(sequenceKey)[elementPosition];
237  }
238 
239  const typename SequenceType::ElementType& valueAt(size_t sequencePosition, size_t elementPosition) const override
240  {
241  return sequence(sequencePosition)[elementPosition];
242  }
243 
244  typename SequenceType::ElementType& valueAt(size_t sequencePosition, size_t elementPosition) override
245  {
246  return sequence_(sequencePosition)[elementPosition];
247  }
248 
249  bool hasSequence(const std::string& sequenceKey) const override
250  {
252  }
253 
254  const SequenceType& sequence(const std::string& sequenceKey) const override
255  {
257  }
258 
259  void setSequence(const std::string& sequenceKey, std::unique_ptr<SequenceType>& sequencePtr) override
260  {
262  }
263 
264  void addSequence(const std::string& sequenceKey, std::unique_ptr<SequenceType>& sequencePtr) override
265  {
266  if (sequencePtr->getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
267  throw AlphabetMismatchException("VectorSequenceContainer::addSequence : Alphabets do not match.", getAlphabet(), sequencePtr->getAlphabet());
268 
269  std::shared_ptr<SequenceType> sequencePtr2(sequencePtr.release(), SwitchDeleter<SequenceType>());
270  sequenceVectorMap_.appendObject(sequencePtr2, sequenceKey, true);
271  }
272 
273  std::unique_ptr<SequenceType> removeSequence(const std::string& sequenceKey) override
274  {
275  std::shared_ptr<SequenceType> ptr = sequenceVectorMap_.removeObject(sequenceKey);
276  std::get_deleter<SwitchDeleter<SequenceType>, SequenceType>(ptr)->off();
277  return std::unique_ptr<SequenceType>(ptr.get()); // Not elegant but safe because of the complete encapsulation.
278  }
279 
280  void deleteSequence(const std::string& sequenceKey) override
281  {
283  }
284 
285  void deleteSequence(size_t sequencePosition) override
286  {
287  sequenceVectorMap_.deleteObject(sequencePosition);
288  }
289 
290  const SequenceType& sequence(size_t sequencePosition) const override
291  {
292  return *sequenceVectorMap_.getObject(sequencePosition);
293  }
294 
295  void setSequence(size_t sequencePosition, std::unique_ptr<SequenceType>& sequencePtr) override
296  {
297  if (sequencePtr->getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
298  throw AlphabetMismatchException("VectorSequenceContainer::setSequence : Alphabets don't match", getAlphabet(), sequencePtr->getAlphabet());
299 
300  sequenceVectorMap_.addObject(std::move(sequencePtr), sequencePosition, sequenceKey(sequencePosition), false);
301  }
302 
303  void setSequence(size_t sequencePosition, std::unique_ptr<SequenceType>& sequencePtr, const std::string& sequenceKey) override
304  {
305  if (sequencePtr->getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
306  throw AlphabetMismatchException("VectorSequenceContainer::setSequence : Alphabets don't match", getAlphabet(), sequencePtr->getAlphabet());
307 
308  sequenceVectorMap_.addObject(std::move(sequencePtr), sequencePosition, sequenceKey, false);
309  }
310 
311  void insertSequence(size_t sequencePosition, std::unique_ptr<SequenceType>& sequencePtr, const std::string& sequenceKey) override
312  {
313  if (sequencePtr->getAlphabet()->getAlphabetType() != getAlphabet()->getAlphabetType())
314  throw AlphabetMismatchException("VectorSequenceContainer::insertSequence : Alphabets don't match", getAlphabet(), sequencePtr->getAlphabet());
315 
316  sequenceVectorMap_.insertObject(std::move(sequencePtr), sequencePosition, sequenceKey);
317  }
318 
319  std::unique_ptr<SequenceType> removeSequence(size_t sequencePosition) override
320  {
321  std::shared_ptr<SequenceType> ptr = sequenceVectorMap_.removeObject(sequencePosition);
322  std::get_deleter<SwitchDeleter<SequenceType>, SequenceType>(ptr)->off();
323  return std::unique_ptr<SequenceType>(ptr.get()); // Not elegant but safe because of the complete encapsulation.
324  }
325 
326 
327  std::vector<std::string> getSequenceNames() const override
328  {
329  size_t nbSeq = getNumberOfSequences();
330  std::vector<std::string> vs(nbSeq);
331  for (size_t i = 0; i < nbSeq; ++i)
332  {
333  vs[i] = sequence(i).getName();
334  }
335  return vs;
336  }
337 
338  void setSequenceNames(const std::vector<std::string>& names, bool updateKeys) override
339  {
340  if (names.size() != getNumberOfSequences())
341  throw DimensionException("VectorSequenceContainer::setSequenceNames : bad number of names", names.size(), getNumberOfSequences());
342  for (size_t i = 0; i < names.size(); ++i)
343  {
344  sequence_(i).setName(names[i]);
345  }
346  if (updateKeys)
347  {
349  }
350  }
351 
352  std::vector<Comments> getSequenceComments() const override
353  {
354  size_t nbSeq = getNumberOfSequences();
355  std::vector<Comments> vs(nbSeq);
356  for (size_t i = 0; i < nbSeq; ++i)
357  {
358  vs[i] = sequence(i).getComments();
359  }
360  return vs;
361  }
362 
371  virtual void addSequence(std::unique_ptr<SequenceType>& sequencePtr)
372  {
373  addSequence(sequencePtr->getName(), sequencePtr);
374  }
375 
376 protected:
377  virtual SequenceType& sequence_(size_t sequencePosition)
378  {
379  return sequenceVectorMap_.object(sequencePosition);
380  }
381 
382 
387  virtual SequenceType& sequence_(const std::string& sequenceKey)
388  {
390  }
391 };
392 
393 // Aliases:
396 } // end of namespace bpp.
397 #endif // BPP_SEQ_CONTAINER_VECTORSEQUENCECONTAINER_H
Partial implementation of the SequenceContainer interface.
AbstractTemplateSequenceContainer & operator=(const AbstractTemplateSequenceContainer< SequenceType, HashType > &sc)
std::shared_ptr< const Alphabet > getAlphabet() const override
Exception thrown when two alphabets do not match.
virtual const Comments & getComments() const =0
Get the comments.
const T & object(const std::string &name) const override
bool hasObject(const std::string &name) const override
Check if a object with a given name is present in the container.
const std::shared_ptr< T > getObject(const std::string &name) const override
Retrieve an object from the container.
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
virtual const SequenceType & sequence(const HashType &sequenceKey) const override=0
Retrieve a sequence object from the container.
virtual size_t getNumberOfSequences() const =0
Get the number of sequences in the container.
virtual const HashType & sequenceKey(size_t sequencePosition) const =0
Get the key associated to a given sequence.
The VectorSequenceContainer class.
VectorMappedContainer< SequenceType > sequenceVectorMap_
bool hasSequence(const std::string &sequenceKey) const override
Check if a certain key is associated to a sequence in the container.
const SequenceType & sequence(size_t sequencePosition) const override
Retrieve a sequence object from the container.
void setSequenceKeys(const std::vector< std::string > &sequenceKeys) override
Reset all sequence keys.
SequenceType::ElementType & valueAt(size_t sequencePosition, size_t elementPosition) override
Get the content of the dataset at a specific position (sequence position, site position).
void addSequence(const std::string &sequenceKey, std::unique_ptr< SequenceType > &sequencePtr) override
Add a sequence to the container.
void setSequence(size_t sequencePosition, std::unique_ptr< SequenceType > &sequencePtr) override
Replace a sequence in the container.
TemplateVectorSequenceContainer & operator=(const TemplateSequenceContainerInterface< SequenceType, std::string > &sc)
Copy from a SequenceContainer.
const SequenceType::ElementType & valueAt(size_t sequencePosition, size_t elementPosition) const override
Get the content of the dataset at a specific position (sequence position, site position).
double getStateValueAt(size_t sitePosition, const std::string &sequenceKey, int state) const override
Get the value of a state at a given position.
double operator()(size_t sitePosition, size_t sequencePosition, int state) const override
Get the value of a state at a given position.
TemplateVectorSequenceContainer(std::shared_ptr< const Alphabet > alphabet)
Build an empty container that will contain sequences of a particular alphabet.
virtual SequenceType & sequence_(const std::string &sequenceKey)
getSequence with given key
void clear() override
Delete all data in the container.
void deleteSequence(size_t sequencePosition) override
Remove and delete a sequence from the container.
TemplateVectorSequenceContainer< SequenceType > * clone() const override
std::unique_ptr< SequenceType > removeSequence(size_t sequencePosition) override
Remove a sequence from the container.
TemplateVectorSequenceContainer & operator=(const TemplateVectorSequenceContainer &vsc)
Assign from a VectorSequenceContainer.
double operator()(size_t sitePosition, const std::string &sequenceKey, int state) const override
Get the value of a state at a given position.
void setSequence(size_t sequencePosition, std::unique_ptr< SequenceType > &sequencePtr, const std::string &sequenceKey) override
Replace a sequence in the container.
std::vector< std::string > getSequenceNames() const override
TemplateVectorSequenceContainer(const TemplateSequenceContainerInterface< SequenceType, std::string > &sc)
Copy from a SequenceContainer.
std::vector< Comments > getSequenceComments() const override
virtual void addSequence(std::unique_ptr< SequenceType > &sequencePtr)
Add a sequence to the container, using its name as a key.
const SequenceType::ElementType & valueAt(const std::string &sequenceKey, size_t elementPosition) const override
Get the content of the dataset at a specific position (sequence key, site position).
double getStateValueAt(size_t sitePosition, size_t sequencePosition, int state) const override
Get value of a state at a given position.
TemplateVectorSequenceContainer< SequenceType > * createEmptyContainer() const override
Return a copy of this container, but with no data inside.
TemplateVectorSequenceContainer(std::shared_ptr< const Alphabet > alphabet, std::vector< std::unique_ptr< SequenceType >> &vs)
Build a container with pointers to sequence objects.
TemplateVectorSequenceContainer(const TemplateVectorSequenceContainer< SequenceType > &vsc)
Copy from a VectorSequenceContainer.
size_t getSequencePosition(const std::string &sequenceKey) const override
Get the position of a sequence with a given key in the container.
size_t getNumberOfSequences() const override
Get the number of sequences in the container.
std::vector< std::string > getSequenceKeys() const override
void setSequence(const std::string &sequenceKey, std::unique_ptr< SequenceType > &sequencePtr) override
Replace a sequence in the container.
const std::string & sequenceKey(size_t sequencePosition) const override
Get the key associated to a given sequence.
void insertSequence(size_t sequencePosition, std::unique_ptr< SequenceType > &sequencePtr, const std::string &sequenceKey) override
Insert a sequence in the container.
SequenceType::ElementType & valueAt(const std::string &sequenceKey, size_t elementPosition) override
Get the content of the dataset at a specific position (sequence key, site position).
void setSequenceNames(const std::vector< std::string > &names, bool updateKeys) override
Batch-set all sequence names.
const SequenceType & sequence(const std::string &sequenceKey) const override
Retrieve a sequence object from the container.
std::unique_ptr< SequenceType > removeSequence(const std::string &sequenceKey) override
Remove a sequence from the container.
void deleteSequence(const std::string &sequenceKey) override
Remove and delete a sequence from the container.
virtual SequenceType & sequence_(size_t sequencePosition)
void addObject(std::shared_ptr< T > newObject, size_t objectIndex, const std::string &name, bool check=false) override
void insertObject(std::shared_ptr< T > newObject, size_t objectIndex, const std::string &name) override
void deleteObject(size_t objectIndex) override
Delete an object from the container.
virtual void appendObject(std::shared_ptr< T > newObject, const std::string &name, bool checkNames=true)
void clear() override
Delete all objects in the container.
const std::string & getObjectName(size_t objectIndex) const override
std::vector< std::string > getObjectNames() const override
size_t getObjectPosition(const std::string &name) const override
Link between position & name.
void setObjectNames(const std::vector< std::string > &names)
size_t getSize() const override
Get the number of objects in the container.
std::shared_ptr< T > removeObject(size_t objectIndex) override
Extract and remove a object from the container.
This alphabet is used to deal NumericAlphabet.