A library for working with phylogenetic and population genetic data.
v0.27.0
simulator.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2019 Lucas Czech and HITS gGmbH
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
34 
35 #include <cassert>
36 
37 namespace genesis {
38 namespace placement {
39 
40 // =================================================================================================
41 // Placement Simulator
42 // =================================================================================================
43 
47 void Simulator::generate( Sample& sample, size_t n )
48 {
49  // Prepare distributions.
50  edge_distribution_.prepare( sample );
51  extra_placement_distribution_.prepare( sample );
52  like_weight_ratio_distribution_.prepare( sample );
53  proximal_length_distribution_.prepare( sample );
54  pendant_length_distribution_.prepare( sample );
55 
56  // Helper function to add a Placement with random properties.
57  auto add_random_placement = [&] ( Pquery& pqry, PlacementTreeEdge& edge ) {
58  PqueryPlacement& place = pqry.add_placement( edge );
59  place.proximal_length = proximal_length_distribution_.generate( edge );
60  place.pendant_length = pendant_length_distribution_.generate( edge );
61  place.like_weight_ratio = like_weight_ratio_distribution_.generate();
62  };
63 
64  for (size_t i = 0; i < n; ++i) {
65  // Generate one Pquery.
66  Pquery& pqry = sample.add();
67  pqry.add_name( "pquery_" + std::to_string( sample.size() - 1 ));
68 
69  // Get a random edge.
70  size_t edge_idx = edge_distribution_.generate();
71  assert( edge_idx < sample.tree().edge_count() );
72  auto& edge = sample.tree().edge_at( edge_idx );
73 
74  // Add a placement at the edge.
75  add_random_placement( pqry, edge );
76 
77  // Generate additional placements around that edge.
78  auto extra_edge_idcs = extra_placement_distribution_.generate( edge );
79  for( auto extra_edge_idx : extra_edge_idcs ) {
80 
81  // Make sure that the distribution works correctly and actually gives us only indices
82  // that are not our current central one.
83  assert( extra_edge_idx != edge_idx );
84  assert( extra_edge_idx < sample.tree().edge_count() );
85 
86  // Add a placement at the edge.
87  auto& extra_edge = sample.tree().edge_at( extra_edge_idx );
88  add_random_placement( pqry, extra_edge );
89  }
90 
91  // Make sure that our generating process ends up with correct like weights ratios.
92  // We generated them randomly, so their sum will not be 1.0 in general.
94  }
95 }
96 
97 } // namespace placement
98 } // namespace genesis
genesis::placement::Sample::tree
PlacementTree & tree()
Get the PlacementTree of this Sample.
Definition: sample.cpp:124
genesis::placement::SimulatorLikeWeightRatioDistribution::generate
double generate()
Return a randomly chosen like weight ratio.
Definition: distributions.hpp:214
genesis::placement::Simulator::generate
void generate(Sample &sample, size_t n)
Generate n many Pqueries and place them in the Sample.
Definition: simulator.cpp:47
genesis::placement::Sample::size
size_t size() const
Return the number of Pqueries that are stored in this Sample.
Definition: sample.cpp:138
genesis::placement::SimulatorEdgeDistribution::generate
size_t generate()
Return a randomly chosen edge index, according to the distribution.
Definition: distributions.hpp:94
genesis::placement::Sample
Manage a set of Pqueries along with the PlacementTree where the PqueryPlacements are placed on.
Definition: sample.hpp:68
genesis::placement::SimulatorProximalLengthDistribution::prepare
void prepare(Sample const &sample)
Prepare the distribution for usage. Needs to be called before generate().
Definition: distributions.hpp:320
genesis::tree::Tree::edge_at
TreeEdge & edge_at(size_t index)
Return the TreeEdge at a certain index.
Definition: tree/tree.hpp:238
genesis::placement::Sample::add
Pquery & add()
Create an empty Pquery, add it to the Sample and return it.
Definition: sample.cpp:152
genesis::placement::SimulatorPendantLengthDistribution::prepare
void prepare(Sample const &sample)
Prepare the distribution for usage. Needs to be called before generate().
Definition: distributions.hpp:261
functions.hpp
Provides functions for working with Placements and Pqueries.
genesis::population::to_string
std::string to_string(GenomeLocus const &locus)
Definition: functions/genome_locus.hpp:48
genesis::placement::SimulatorLikeWeightRatioDistribution::prepare
void prepare(Sample const &sample)
Prepare the distribution for usage. Needs to be called before generate().
Definition: distributions.cpp:223
genesis::placement::SimulatorExtraPlacementDistribution::prepare
void prepare(Sample const &sample)
Prepare the distribution for usage. Needs to be called before generate().
Definition: distributions.cpp:56
simulator.hpp
genesis::placement::PqueryPlacement::like_weight_ratio
double like_weight_ratio
Likelihood weight ratio of this placement.
Definition: placement/pquery/placement.hpp:137
genesis::placement::Pquery
A pquery holds a set of PqueryPlacements and a set of PqueryNames.
Definition: pquery.hpp:82
genesis::placement::Pquery::add_name
PqueryName & add_name(std::string name="", double multiplicity=1.0)
Create a new PqueryName using the provided parameters, add it to the Pquery and return it.
Definition: pquery.cpp:181
genesis::tree::TreeEdge
Definition: edge.hpp:60
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::placement::SimulatorEdgeDistribution::prepare
void prepare(Sample const &sample)
Prepare the distribution for usage. Needs to be called before generate().
Definition: distributions.hpp:73
genesis::placement::normalize_weight_ratios
void normalize_weight_ratios(Pquery &pquery)
Recalculate the like_weight_ratio of the PqueryPlacement&s of a Pquery, so that their sum is 1....
Definition: placement/function/functions.cpp:123
genesis::placement::SimulatorExtraPlacementDistribution::generate
std::vector< size_t > generate(PlacementTreeEdge const &edge)
Definition: distributions.cpp:119
genesis::placement::PqueryPlacement
One placement position of a Pquery on a Tree.
Definition: placement/pquery/placement.hpp:75
genesis::placement::Pquery::add_placement
PqueryPlacement & add_placement(PlacementTreeEdge &edge)
Create a new PqueryPlacement at a given PlacementTreeEdge, add it to the Pquery and return it.
Definition: pquery.cpp:92
genesis::placement::SimulatorProximalLengthDistribution::generate
double generate(PlacementTreeEdge const &edge)
Return a randomly chosen position on an edge.
Definition: distributions.hpp:329
genesis::placement::PqueryPlacement::proximal_length
double proximal_length
Distance of this placement to the next node towards the root.
Definition: placement/pquery/placement.hpp:155
genesis::placement::SimulatorPendantLengthDistribution::generate
double generate(PlacementTreeEdge const &edge)
Return a randomly chosen position on an edge.
Definition: distributions.hpp:270
genesis::tree::Tree::edge_count
size_t edge_count() const
Return the number of TreeEdges of the Tree.
Definition: tree/tree.hpp:272
genesis::placement::PqueryPlacement::pendant_length
double pendant_length
Length of the attached branch of this placement.
Definition: placement/pquery/placement.hpp:165