A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
simulator.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2017 Lucas Czech
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 <assert.h>
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  auto& edge = sample.tree().edge_at( edge_idx );
72 
73  // Add a placement at the edge.
74  add_random_placement( pqry, edge );
75 
76  // Generate additional placements around that edge.
77  auto extra_edge_idcs = extra_placement_distribution_.generate( edge );
78  for( auto extra_edge_idx : extra_edge_idcs ) {
79 
80  // Make sure that the distribution works correctly and actually gives us only indices
81  // that are not our current central one.
82  assert( extra_edge_idx != edge_idx );
83 
84  // Add a placement at the edge.
85  auto& extra_edge = sample.tree().edge_at( extra_edge_idx );
86  add_random_placement( pqry, extra_edge );
87  }
88 
89  // Make sure that our generating process ends up with correct like weights ratios.
90  // We generated them randomly, so their sum will not be 1.0 in general.
92  }
93 }
94 
95 } // namespace placement
96 } // namespace genesis
size_t size() const
Return the number of Pqueries that are stored in this Sample.
Definition: sample.cpp:133
std::vector< size_t > generate(PlacementTreeEdge const &edge)
double proximal_length
Distance of this placement to the next node towards the root.
PlacementTree & tree()
Get the PlacementTree of this Sample.
Definition: sample.cpp:119
A pquery holds a set of PqueryPlacements and a set of PqueryNames.
Definition: pquery.hpp:82
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
void prepare(Sample const &sample)
Prepare the distribution for usage. Needs to be called before generate().
void generate(Sample &sample, size_t n)
Generate n many Pqueries and place them in the Sample.
Definition: simulator.cpp:47
Provides functions for working with Placements and Pqueries.
void normalize_weight_ratios(Pquery &pquery)
Recalculate the like_weight_ratio of the PqueryPlacement&s of a Pquery, so that their sum is 1...
One placement position of a Pquery on a Tree.
std::string to_string(T const &v)
Return a string representation of a given value.
Definition: string.hpp:381
size_t generate()
Return a randomly chosen edge index, according to the distribution.
double generate(PlacementTreeEdge const &edge)
Return a randomly chosen position on an edge.
void prepare(Sample const &sample)
Prepare the distribution for usage. Needs to be called before generate().
void prepare(Sample const &sample)
Prepare the distribution for usage. Needs to be called before generate().
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
void prepare(Sample const &sample)
Prepare the distribution for usage. Needs to be called before generate().
Manage a set of Pqueries along with the PlacementTree where the PqueryPlacements are placed on...
Definition: sample.hpp:68
TreeEdge & edge_at(size_t index)
Return the TreeEdge at a certain index.
Definition: tree/tree.cpp:324
void prepare(Sample const &sample)
Prepare the distribution for usage. Needs to be called before generate().
double pendant_length
Length of the attached branch of this placement.
double generate()
Return a randomly chosen like weight ratio.
double like_weight_ratio
Likelihood weight ratio of this placement.
double generate(PlacementTreeEdge const &edge)
Return a randomly chosen position on an edge.
Pquery & add()
Create an empty Pquery, add it to the Sample and return it.
Definition: sample.cpp:147