bpp-popgen  3.0.0
DataSetTools.cpp
Go to the documentation of this file.
1 //
2 // File DataSetTools.cpp
3 // Author : Sylvain Gaillard
4 // Last modification : Wednesday August 04 2004
5 //
6 
7 /*
8  Copyright or © or Copr. CNRS, (November 17, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for population genetics analysis.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
40 #include "DataSetTools.h"
41 
42 using namespace bpp;
43 using namespace std;
44 
45 std::unique_ptr<DataSet> DataSetTools::buildDataSet(const OrderedSequenceContainer& osc)
46 {
47  unique_ptr<DataSet> d_s(new DataSet());
48  d_s->addEmptyGroup(0);
49  for (size_t i = 0; i < osc.getNumberOfSequences(); i++)
50  {
51  d_s->addEmptyIndividualToGroup(0, string("Individual_") + TextTools::toString(i + 1));
52  try
53  {
54  d_s->addIndividualSequenceInGroup(0, i, 0, osc.getSequence(i));
55  }
56  catch (Exception& e)
57  {
58  throw e;
59  }
60  }
61  return d_s;
62 }
63 
64 std::unique_ptr<DataSet> DataSetTools::buildDataSet(const PolymorphismSequenceContainer& psc)
65 {
66  unique_ptr<DataSet> d_s(new DataSet());
67  set<size_t> grp_ids = psc.getAllGroupsIds();
68  for (set<size_t>::iterator it = grp_ids.begin(); it != grp_ids.end(); it++)
69  {
70  d_s->addEmptyGroup(*it);
71  }
72  size_t ind_count = 0;
73  for (size_t i = 0; i < psc.getNumberOfSequences(); i++)
74  {
75  for (size_t j = 0; j < psc.getSequenceCount(i); j++)
76  {
77  d_s->addEmptyIndividualToGroup(psc.getGroupId(i), string("Individual_") + TextTools::toString(ind_count++));
78  try
79  {
80  d_s->addIndividualSequenceInGroup(psc.getGroupId(i), i, 0, psc.getSequence(i));
81  }
82  catch (Exception& e)
83  {
84  throw e;
85  }
86  }
87  }
88  return d_s;
89 }
90 
static std::unique_ptr< DataSet > buildDataSet(const OrderedSequenceContainer &osc)
General method to build a DataSet from an OrderedSequenceContainer.
The DataSet class.
Definition: DataSet.h:73
virtual const Sequence & getSequence(size_t sequenceIndex) const=0
virtual size_t getNumberOfSequences() const=0
The PolymorphismSequenceContainer class.
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.
size_t getGroupId(size_t index) const
Get the group identifier of the sequence.
const Sequence & getSequence(size_t sequenceIndex) const
size_t getNumberOfSequences() const
std::string toString(T t)