bpp-popgen3  3.0.0
PolymorphismSequenceContainer.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 _POLYMORPHISMSEQUENCECONTAINER_H_
6 #define _POLYMORPHISMSEQUENCECONTAINER_H_
7 
8 #include <set>
9 #include <string>
10 
11 #include <Bpp/Clonable.h>
13 #include <Bpp/Text/TextTools.h>
14 
17 
56 namespace bpp
57 {
67  public VectorSiteContainer
68 {
69 private:
70  std::vector<bool> ingroup_;
71  std::vector<unsigned int> count_;
72  std::vector<size_t> group_;
73 
74 public:
75  // Constructors and destructor
79  PolymorphismSequenceContainer(std::shared_ptr<const Alphabet> alpha) :
80  VectorSiteContainer(alpha),
81  ingroup_(std::vector<bool>()),
82  count_(0),
83  group_(0)
84  {}
85 
89  PolymorphismSequenceContainer(size_t size, std::shared_ptr<const Alphabet> alpha) :
90  VectorSiteContainer(size, alpha),
91  ingroup_(size),
92  count_(size),
93  group_(size)
94  {}
95 
99  PolymorphismSequenceContainer(const std::vector<std::string>& names, std::shared_ptr<const Alphabet> alpha) :
100  VectorSiteContainer(names, alpha),
101  ingroup_(names.size()),
102  count_(names.size()),
103  group_(names.size())
104  {}
105 
113  ingroup_(sc.getNumberOfSequences(), true),
114  count_(sc.getNumberOfSequences(), 1),
116  {}
117 
128 
133 
138 
143 
148  {
149  return new PolymorphismSequenceContainer(*this);
150  }
151 
152 public:
153  // Other methods
154  std::unique_ptr<Sequence> removeSequence(size_t sequencePosition) override;
155 
156  std::unique_ptr<Sequence> removeSequence(const std::string& sequenceKey)override;
157 
158  void deleteSequence(size_t sequencePosition) override;
159 
160  void deleteSequence(const std::string& sequenceKey) override;
161 
170  const std::string& sequenceKey,
171  std::unique_ptr<Sequence>& sequence,
172  unsigned int frequency)
173  {
175  count_.push_back(frequency);
176  ingroup_.push_back(true);
177  group_.push_back(0);
178  }
179 
180 
182  size_t sequencePosition,
183  std::unique_ptr<Sequence>& sequence,
184  const std::string& sequenceKey,
185  unsigned int frequency)
186  {
188  count_.insert(count_.begin() + static_cast<ptrdiff_t>(sequencePosition), frequency);
189  ingroup_.insert(ingroup_.begin() + static_cast<ptrdiff_t>(sequencePosition), true);
190  group_.insert(group_.begin() + static_cast<ptrdiff_t>(sequencePosition), 0);
191  }
192 
194  const std::string& sequenceKey,
195  std::unique_ptr<Sequence>& sequence) override
196  {
198  }
199 
200  void insertSequence(size_t sequencePosition, std::unique_ptr<Sequence>& sequence, const std::string& sequenceKey) override
201  {
202  insertSequenceWithFrequency(sequencePosition, sequence, sequenceKey, 1);
203  }
204 
208  void clear() override
209  {
211  count_.clear();
212  ingroup_.clear();
213  group_.clear();
214  }
215 
221  size_t getGroupId(size_t index) const
222  {
223  if (index >= getNumberOfSequences())
224  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::getGroupId: index out of bounds.", index, 0, getNumberOfSequences());
225  return group_[index];
226  }
227 
233  size_t getGroupId(const std::string& name) const
234  {
235  try
236  {
237  return group_[getSequencePosition(name)];
238  }
239  catch (SequenceNotFoundException& snfe)
240  {
241  throw SequenceNotFoundException("PolymorphismSequenceContainer::getGroupId.", name);
242  }
243  }
244 
245 
249  std::set<size_t> getAllGroupsIds() const;
250 
256  void setGroupId(size_t index, size_t group_id)
257  {
258  if (index >= getNumberOfSequences())
259  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::setGroupId: index out of bounds.", index, 0, getNumberOfSequences());
260  group_[index] = group_id;
261  }
262 
268  void setGroupId(const std::string& name, size_t group_id)
269  {
270  try
271  {
272  group_[getSequencePosition(name)] = group_id;
273  }
274  catch (SequenceNotFoundException& snfe)
275  {
276  throw SequenceNotFoundException("PolymorphismSequenceContainer::setGroupId.", name);
277  }
278  }
279 
283  size_t getNumberOfGroups() const
284  {
285  return getAllGroupsIds().size();
286  }
287 
291  bool hasOutgroup() const;
292 
298  bool isIngroupMember(size_t index) const
299  {
300  if (index >= getNumberOfSequences())
301  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::isIngroupMember: index out of bounds.", index, 0, getNumberOfSequences());
302  return ingroup_[index];
303  }
304 
310  bool isIngroupMember(const std::string& name) const
311  {
312  try
313  {
314  return ingroup_[getSequencePosition(name)];
315  }
316  catch (SequenceNotFoundException& snfe)
317  {
318  throw SequenceNotFoundException("PolymorphismSequenceContainer::isIngroupMember.", name);
319  }
320  }
321 
327  void setAsIngroupMember(size_t index);
328 
334  void setAsIngroupMember(const std::string& name);
335 
341  void setAsOutgroupMember(size_t index);
342 
348  void setAsOutgroupMember(const std::string& name);
349 
356  void setSequenceCount(size_t index, unsigned int count);
357 
364  void setSequenceCount(const std::string& name, unsigned int count);
365 
371  void incrementSequenceCount(size_t index);
372 
378  void incrementSequenceCount(const std::string& name);
379 
386  void decrementSequenceCount(size_t index);
387 
394  void decrementSequenceCount(const std::string& name);
395 
401  unsigned int getSequenceCount(size_t index) const;
402 
408  unsigned int getSequenceCount(const std::string& name) const;
409 
415  std::unique_ptr<SiteContainerInterface> toSiteContainer() const;
416 };
417 } // end of namespace bpp;
418 
419 #endif // _POLYMORPHISMSEQUENCECONTAINER_H_
The PolymorphismSequenceContainer class.
PolymorphismSequenceContainer * clone() const override
Clone a PolymorphismSequenceContainer.
void setGroupId(size_t index, size_t group_id)
Set the group identifier of a sequence.
std::unique_ptr< SiteContainerInterface > toSiteContainer() const
convert the container to a site container, with sequences dulicated according to their respective fre...
size_t getGroupId(size_t index) const
Get the group identifier of the sequence.
void setAsIngroupMember(size_t index)
Set a sequence as ingroup member by index.
void setAsOutgroupMember(size_t index)
Set a sequence as outgroup member by index.
void insertSequence(size_t sequencePosition, std::unique_ptr< Sequence > &sequence, const std::string &sequenceKey) override
void addSequenceWithFrequency(const std::string &sequenceKey, std::unique_ptr< Sequence > &sequence, unsigned int frequency)
Add a sequence to the container.
virtual ~PolymorphismSequenceContainer()
Destroy a PolymorphismSequenceContainer.
PolymorphismSequenceContainer(size_t size, std::shared_ptr< const Alphabet > alpha)
Build a new empty PolymorphismSequenceContainer of given size.
PolymorphismSequenceContainer(std::shared_ptr< const Alphabet > alpha)
Build a new empty PolymorphismSequenceContainer.
bool isIngroupMember(const std::string &name) const
Tell if a sequence is ingroup by name.
size_t getGroupId(const std::string &name) const
Get the group identifier of a sequence.
bool isIngroupMember(size_t index) const
Tell if the sequence is ingroup by index.
size_t getNumberOfGroups() const
Get the number of groups.
void clear() override
Clear the container of all its sequences.
unsigned int getSequenceCount(size_t index) const
Get the count of a sequence by index.
PolymorphismSequenceContainer & operator=(const PolymorphismSequenceContainer &psc)
Operator= : copy operator.
void incrementSequenceCount(size_t index)
Add 1 to the sequence count.
void decrementSequenceCount(size_t index)
Removz 1 to the sequence count.
std::set< size_t > getAllGroupsIds() const
Get all the groups identifiers.
void setSequenceCount(size_t index, unsigned int count)
Set the count of a sequence by index.
PolymorphismSequenceContainer(const SequenceContainerInterface &sc)
Build a PolymorphismSequenceContainer by copying data from a SequenceContainer.
void setGroupId(const std::string &name, size_t group_id)
Set the group identifier of a sequence.
void addSequence(const std::string &sequenceKey, std::unique_ptr< Sequence > &sequence) override
std::unique_ptr< Sequence > removeSequence(size_t sequencePosition) override
PolymorphismSequenceContainer(const std::vector< std::string > &names, std::shared_ptr< const Alphabet > alpha)
Build a new empty PolymorphismSequenceContainer with given sequence names.
void deleteSequence(size_t sequencePosition) override
void insertSequenceWithFrequency(size_t sequencePosition, std::unique_ptr< Sequence > &sequence, const std::string &sequenceKey, unsigned int frequency)
void addSequence(const std::string &sequenceKey, std::unique_ptr< SequenceType > &sequence) override
const SequenceType & sequence(const std::string &sequenceKey) const override
void insertSequence(size_t sequencePosition, std::unique_ptr< SequenceType > &sequence, const std::string &sequenceKey) override
size_t getSequencePosition(const std::string &sequenceKey) const override
size_t getNumberOfSequences() const override
const std::string & sequenceKey(size_t sequencePosition) const override