bpp-popgen3  3.0.0
PolymorphismSequenceContainer.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
6 
8 
9 using namespace bpp;
10 using namespace std;
11 
12 /******************************************************************************/
13 
15  VectorSiteContainer(sc.getAlphabet()),
16  ingroup_(),
17  count_(),
18  group_()
19 {
20  if (sc.getNumberOfSequences() == 0)
21  return; // done.
22 
23  // Add first sequence:
24  auto seqTmp0 = unique_ptr<Sequence>(sc.sequence(0).clone());
25  addSequenceWithFrequency(seqTmp0->getName(), seqTmp0, 1);
26  for (size_t i = 1; i < sc.getNumberOfSequences(); ++i)
27  {
28  const Sequence& seq = sc.sequence(i);
29  // Check if this sequence already exists in this container:
30  bool exists = false;
31  for (size_t j = 0; !exists && j < getNumberOfSequences(); ++j)
32  {
34  {
35  incrementSequenceCount(j); // We increase frequency, meaning that we discard this sequence name.
36  exists = true;
37  }
38  }
39  if (!exists)
40  {
41  auto seqTmp = unique_ptr<Sequence>(seq.clone());
42  addSequenceWithFrequency(seqTmp->getName(), seqTmp, 1);
43  }
44  }
45  ingroup_.resize(getNumberOfSequences(), true);
46  group_.resize(getNumberOfSequences());
47 }
48 
49 /******************************************************************************/
50 
53  ingroup_(psc.getNumberOfSequences()),
54  count_(psc.getNumberOfSequences()),
55  group_(psc.getNumberOfSequences())
56 {
57  for (size_t i = 0; i < psc.getNumberOfSequences(); i++)
58  {
59  count_[i] = psc.getSequenceCount(i);
60  ingroup_[i] = psc.isIngroupMember(i);
61  group_[i] = psc.getGroupId(i);
62  }
63 }
64 
65 /******************************************************************************/
66 
68 {
70  // Setting up the sequences comments, numbers and ingroup state
71  size_t nbSeq = psc.getNumberOfSequences();
72  count_.resize(nbSeq);
73  ingroup_.resize(nbSeq);
74  group_.resize(nbSeq);
75  for (size_t i = 0; i < nbSeq; i++)
76  {
77  count_[i] = psc.getSequenceCount(i);
78  ingroup_[i] = psc.isIngroupMember(i);
79  group_[i] = psc.getGroupId(i);
80  }
81  return *this;
82 }
83 
84 /******************************************************************************/
85 
89 {
90  clear();
91 }
92 
93 /*****************************************************************************/
94 
97 std::unique_ptr<Sequence> PolymorphismSequenceContainer::removeSequence(size_t sequencePosition)
98 {
99  if (sequencePosition >= getNumberOfSequences())
100  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::removeSequence: index out of bounds.", sequencePosition, 0, getNumberOfSequences());
101  count_.erase(count_.begin() + static_cast<ptrdiff_t>(sequencePosition));
102  ingroup_.erase(ingroup_.begin() + static_cast<ptrdiff_t>(sequencePosition));
103  group_.erase(group_.begin() + static_cast<ptrdiff_t>(sequencePosition));
104  return VectorSiteContainer::removeSequence(sequencePosition);
105 }
106 
107 /******************************************************************************/
108 
109 std::unique_ptr<Sequence> PolymorphismSequenceContainer::removeSequence(const std::string& sequenceName)
110 {
111  try
112  {
113  return removeSequence(getSequencePosition(sequenceName));
114  }
115  catch (SequenceNotFoundException& snfe)
116  {
117  throw SequenceNotFoundException("PolymorphismSequenceContainer::removeSequence.", sequenceName);
118  }
119 }
120 
121 /******************************************************************************/
122 
124 {
125  try
126  {
127  removeSequence(sequencePosition); // This returns a smart pointer, which automatically deletes the object.
128  }
129  catch (IndexOutOfBoundsException& ioobe)
130  {
131  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::deleteSequence.", sequencePosition, 0, getNumberOfSequences());
132  }
133 }
134 
135 /******************************************************************************/
136 
137 void PolymorphismSequenceContainer::deleteSequence(const std::string& sequenceName)
138 {
139  try
140  {
141  removeSequence(sequenceName); // This returns a smart pointer, which automatically deletes the object.
142  }
143  catch (SequenceNotFoundException& snfe)
144  {
145  throw SequenceNotFoundException("PolymorphismSequenceContainer::deleteSequence.", sequenceName);
146  }
147 }
148 
149 /******************************************************************************/
150 
152 {
153  set<size_t> grp_ids;
154  for (size_t i = 0; i < group_.size(); i++)
155  {
156  grp_ids.insert(group_[i]);
157  }
158  return grp_ids;
159 }
160 
161 /******************************************************************************/
162 
164 {
165  for (auto i : ingroup_)
166  {
167  if (!i)
168  return true;
169  }
170  return false;
171 }
172 
173 /******************************************************************************/
174 
176 {
177  if (index >= getNumberOfSequences())
178  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::setAsIngroupMember.", index, 0, getNumberOfSequences());
179  ingroup_[index] = true;
180 }
181 
182 /******************************************************************************/
183 
185 {
186  try
187  {
188  size_t seqPos = getSequencePosition(name);
189  ingroup_[seqPos] = true;
190  }
191  catch (SequenceNotFoundException& snfe)
192  {
193  throw SequenceNotFoundException("PolymorphismSequenceContainer::setAsIngroupMember.", name);
194  }
195 }
196 
197 /******************************************************************************/
198 
200 {
201  if (index >= getNumberOfSequences())
202  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::setAsOutgroupMember.", index, 0, getNumberOfSequences());
203  ingroup_[index] = false;
204 }
205 
206 /******************************************************************************/
207 
209 {
210  try
211  {
212  size_t seqPos = getSequencePosition(name);
213  ingroup_[seqPos] = false;
214  }
215  catch (SequenceNotFoundException& snfe)
216  {
217  throw SequenceNotFoundException("PolymorphismSequenceContainer::setAsOutgroupMember.", name);
218  }
219 }
220 
221 /******************************************************************************/
222 
223 void PolymorphismSequenceContainer::setSequenceCount(size_t index, unsigned int count)
224 {
225  if (index >= getNumberOfSequences())
226  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::setSequenceCount.", index, 0, getNumberOfSequences());
227  if (count < 1)
228  throw BadIntegerException("PolymorphismSequenceContainer::setSequenceCount: count can't be < 1.", static_cast<int>(count));
229  count_[index] = count;
230 }
231 
232 /******************************************************************************/
233 
234 void PolymorphismSequenceContainer::setSequenceCount(const std::string& name, unsigned int count)
235 {
236  try
237  {
239  }
240  catch (BadIntegerException& bie)
241  {
242  throw bie;
243  }
244  catch (SequenceNotFoundException& snfe)
245  {
246  throw SequenceNotFoundException("PolymorphismSequenceContainer::setSequenceCount.", name);
247  }
248 }
249 
250 /******************************************************************************/
251 
253 {
254  if (index >= getNumberOfSequences())
255  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::incrementSequenceCount.", index, 0, getNumberOfSequences());
256  count_[index]++;
257 }
258 
259 /******************************************************************************/
260 
262 {
263  try
264  {
266  }
267  catch (SequenceNotFoundException& snfe)
268  {
269  throw SequenceNotFoundException("PolymorphismSequenceContainer::incrementSequenceCount.", name);
270  }
271 }
272 
273 /******************************************************************************/
274 
276 {
277  if (index >= getNumberOfSequences())
278  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::decrementSequenceCount.", index, 0, getNumberOfSequences());
279  if (count_[index] - 1 < 1)
280  throw BadIntegerException("PolymorphismSequenceContainer::decrementSequenceCount: count can't be < 1.", static_cast<int>(count_[index] - 1));
281  count_[index]--;
282 }
283 
284 /******************************************************************************/
285 
287 {
288  try
289  {
291  }
292  catch (BadIntegerException& bie)
293  {
294  throw bie;
295  }
296  catch (SequenceNotFoundException& snfe)
297  {
298  throw SequenceNotFoundException("PolymorphismSequenceContainer::decrementSequenceCount.", name);
299  }
300 }
301 
302 /******************************************************************************/
303 
304 unsigned int PolymorphismSequenceContainer::getSequenceCount(size_t index) const
305 {
306  if (index >= getNumberOfSequences())
307  throw IndexOutOfBoundsException("PolymorphismSequenceContainer::getSequenceCount.", index, 0, getNumberOfSequences());
308  return count_[index];
309 }
310 
311 /******************************************************************************/
312 
313 unsigned int PolymorphismSequenceContainer::getSequenceCount(const std::string& name) const
314 {
315  try
316  {
318  }
319  catch (SequenceNotFoundException& snfe)
320  {
321  throw SequenceNotFoundException("PolymorphismSequenceContainer::getSequenceCount.", name);
322  }
323 }
324 
325 /******************************************************************************/
326 
327 unique_ptr<SiteContainerInterface> PolymorphismSequenceContainer::toSiteContainer() const
328 {
329  unique_ptr<SiteContainerInterface> sites(new VectorSiteContainer(getAlphabet()));
330  for (size_t i = 0; i < getNumberOfSequences(); ++i)
331  {
332  const auto& seq = sequence(i);
333  unsigned int freq = getSequenceCount(i);
334  if (freq > 1)
335  {
336  for (unsigned int j = 0; j < freq; ++j)
337  {
338  unique_ptr<Sequence> seqdup(seq.clone());
339  seqdup->setName(seq.getName() + "_" + TextTools::toString(j + 1));
340  sites->addSequence(seqdup->getName(), seqdup);
341  }
342  }
343  else
344  {
345  unique_ptr<Sequence> seqdup(seq.clone());
346  sites->addSequence(seqdup->getName(), seqdup);
347  }
348  }
349  sites->setSiteCoordinates(getSiteCoordinates());
350  return sites;
351 }
352 
353 /******************************************************************************/
std::shared_ptr< const Alphabet > getAlphabet() const override
The PolymorphismSequenceContainer class.
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 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(std::shared_ptr< const Alphabet > alpha)
Build a new empty PolymorphismSequenceContainer.
bool isIngroupMember(size_t index) const
Tell if the sequence is ingroup by index.
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.
std::unique_ptr< Sequence > removeSequence(size_t sequencePosition) override
void deleteSequence(size_t sequencePosition) override
static bool areSequencesIdentical(const SequenceInterface &seq1, const SequenceInterface &seq2)
Sequence * clone() const override
virtual const SequenceType & sequence(const HashType &sequenceKey) const override=0
virtual size_t getNumberOfSequences() const =0
std::unique_ptr< SequenceType > removeSequence(size_t sequencePosition) override
Vint getSiteCoordinates() const override
const SequenceType & sequence(const std::string &sequenceKey) const override
size_t getSequencePosition(const std::string &sequenceKey) const override
size_t getNumberOfSequences() const override
TemplateVectorSiteContainer< SiteType, SequenceType > & operator=(const TemplateVectorSiteContainer< SiteType, SequenceType > &vsc)
std::string toString(T t)
std::size_t count(const std::string &s, const std::string &pattern)
TemplateVectorSiteContainer< Site, Sequence > VectorSiteContainer