bpp-phyl3  3.0.0
SequenceSimulationTools.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
7 #include "SequenceSimulator.h"
9 #include "SiteSimulator.h"
10 
11 // From bpp-seq:
13 
14 using namespace bpp;
15 using namespace std;
16 
17 unique_ptr<SiteContainerInterface> SequenceSimulationTools::simulateSites(
18  const SequenceSimulatorInterface& simulator,
19  const vector<double>& rates)
20 {
21  size_t numberOfSites = rates.size();
22  vector<unique_ptr<Site>> vs(numberOfSites);
23  for (size_t i = 0; i < numberOfSites; ++i)
24  {
25  auto s = simulator.siteSimulator(i).simulateSite(rates[i]);
26  s->setCoordinate(static_cast<int>(i));
27  vs[i] = std::move(s);
28  }
29  auto sites = make_unique<VectorSiteContainer>(vs, simulator.getAlphabet());
30  sites->setSequenceNames(simulator.getSequenceNames(), true);
31 
32  return sites;
33 }
34 
35 unique_ptr<SiteContainerInterface> SequenceSimulationTools::simulateSites(
36  const SequenceSimulatorInterface& simulator,
37  const vector<double>& rates,
38  const vector<size_t>& states)
39 {
40  size_t numberOfSites = rates.size();
41  if (states.size() != numberOfSites)
42  throw Exception("SequenceSimulationTools::simulateSites., 'rates' and 'states' must have the same length.");
43  vector<unique_ptr<Site>> vs(numberOfSites);
44  for (size_t i = 0; i < numberOfSites; ++i)
45  {
46  auto s = simulator.siteSimulator(i).simulateSite(states[i], rates[i]);
47  s->setCoordinate(static_cast<int>(i));
48  vs[i] = std::move(s);
49  }
50 
51  auto sites = make_unique<VectorSiteContainer>(vs, simulator.getAlphabet());
52  sites->setSequenceNames(simulator.getSequenceNames(), true);
53 
54  return sites;
55 }
56 
57 unique_ptr<SiteContainerInterface> SequenceSimulationTools::simulateSites(
58  const SequenceSimulatorInterface& simulator,
59  const vector<size_t>& states)
60 {
61  size_t numberOfSites = states.size();
62  vector<unique_ptr<Site>> vs(numberOfSites);
63  for (size_t i = 0; i < numberOfSites; ++i)
64  {
65  auto s = simulator.siteSimulator(i).simulateSite(states[i], 1);
66  s->setCoordinate(static_cast<int>(i));
67  vs[i] = std::move(s);
68  }
69 
70  auto sites = make_unique<VectorSiteContainer>(vs, simulator.getAlphabet());
71  sites->setSequenceNames(simulator.getSequenceNames(), true);
72 
73  return sites;
74 }
static std::unique_ptr< SiteContainerInterface > simulateSites(const SequenceSimulatorInterface &simulator, const std::vector< double > &rates)
Simulate a set of sites knowing their rate.
The SequenceSimulator interface. SequenceSimulator classes can simulate whole datasets.
virtual std::vector< std::string > getSequenceNames() const =0
virtual const SiteSimulatorInterface & siteSimulator(size_t pos) const =0
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
virtual std::unique_ptr< Site > simulateSite() const =0
Defines the basic types of data flow nodes.