bpp-popgen3  3.0.0
DataSetTools.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "DataSetTools.h"
6 
7 using namespace bpp;
8 using namespace std;
9 
10 std::unique_ptr<DataSet> DataSetTools::buildDataSet(const SequenceContainerInterface& sc)
11 {
12  auto dataset = make_unique<DataSet>();
13  dataset->addEmptyGroup(0);
14  for (size_t i = 0; i < sc.getNumberOfSequences(); ++i)
15  {
16  dataset->addEmptyIndividualToGroup(0, string("Individual_") + TextTools::toString(i + 1));
17  try
18  {
19  auto tmpSeq = unique_ptr<Sequence>(sc.sequence(i).clone());
20  dataset->addIndividualSequenceInGroup(0, i, 0, tmpSeq);
21  }
22  catch (Exception& e)
23  {
24  throw e;
25  }
26  }
27  return dataset;
28 }
29 
30 std::unique_ptr<DataSet> DataSetTools::buildDataSet(const PolymorphismSequenceContainer& psc)
31 {
32  auto dataset = make_unique<DataSet>();
33  set<size_t> grpIds = psc.getAllGroupsIds();
34  for (auto& it : grpIds)
35  {
36  dataset->addEmptyGroup(it);
37  }
38  size_t indCount = 0;
39  for (size_t i = 0; i < psc.getNumberOfSequences(); ++i)
40  {
41  for (size_t j = 0; j < psc.getSequenceCount(i); ++j)
42  {
43  dataset->addEmptyIndividualToGroup(psc.getGroupId(i), string("Individual_") + TextTools::toString(indCount++));
44  try
45  {
46  auto tmpSeq = unique_ptr<Sequence>(psc.sequence(i).clone());
47  dataset->addIndividualSequenceInGroup(psc.getGroupId(i), i, 0, tmpSeq);
48  }
49  catch (Exception& e)
50  {
51  throw e;
52  }
53  }
54  }
55  return dataset;
56 }
static std::unique_ptr< DataSet > buildDataSet(const SequenceContainerInterface &sc)
General method to build a DataSet from an OrderedSequenceContainer.
The PolymorphismSequenceContainer class.
size_t getGroupId(size_t index) const
Get the group identifier of the sequence.
unsigned int getSequenceCount(size_t index) const
Get the count of a sequence by index.
std::set< size_t > getAllGroupsIds() const
Get all the groups identifiers.
virtual const SequenceType & sequence(const HashType &sequenceKey) const override=0
virtual size_t getNumberOfSequences() const =0
const SequenceType & sequence(const std::string &sequenceKey) const override
size_t getNumberOfSequences() const override
std::string toString(T t)