A library for working with phylogenetic and population genetic data.
v0.27.0
distributions.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_PLACEMENT_SIMULATOR_DISTRIBUTIONS_H_
2 #define GENESIS_PLACEMENT_SIMULATOR_DISTRIBUTIONS_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2017 Lucas Czech
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
36 
37 #include <random>
38 #include <stdexcept>
39 #include <string>
40 #include <vector>
41 
42 namespace genesis {
43 namespace placement {
44 
45 // =================================================================================================
46 // Placement Simulator Edge Distribution
47 // =================================================================================================
48 
50 {
51 public:
52 
53  // -------------------------------------------------
54  // Constructor and Rule of Five
55  // -------------------------------------------------
56 
57  SimulatorEdgeDistribution() = default;
58  ~SimulatorEdgeDistribution() = default;
59 
62 
65 
66  // -------------------------------------------------
67  // Generate Random Edges
68  // -------------------------------------------------
69 
73  void prepare( Sample const& sample )
74  {
75  // If nothing was set, initialize to uniform distrib.
76  if( edge_weights.size() == 0 ) {
77  edge_weights = std::vector<double>( sample.tree().edge_count(), 1.0 );
78  }
79 
80  // Check size.
81  if( edge_weights.size() != sample.tree().edge_count() ) {
82  throw std::runtime_error(
83  "Incorrect number of edge weights for SimulatorEdgeDistribution."
84  );
85  }
86 
87  // Prepare the actual distrib.
88  distrib_ = std::discrete_distribution<size_t>( edge_weights.begin(), edge_weights.end() );
89  }
90 
94  size_t generate()
95  {
96  return distrib_( utils::Options::get().random_engine() );
97  }
98 
99  // -------------------------------------------------
100  // Data Members
101  // -------------------------------------------------
102 
103 public:
104 
105  std::vector<double> edge_weights;
106 
107 private:
108 
109  std::discrete_distribution<size_t> distrib_;
110 };
111 
112 // =================================================================================================
113 // Placement Simulator Extra Placement Distribution
114 // =================================================================================================
115 
127 {
128 public:
129 
130  // -----------------------------------------------------
131  // Typedefs and Structs
132  // -----------------------------------------------------
133 
135  {
136  using EdgeCandidates = std::vector<size_t>;
137  std::vector<EdgeCandidates> candidates_per_level;
138  size_t total_candidates = 0;
139  };
140 
141  // -----------------------------------------------------
142  // Constructor and Rule of Five
143  // -----------------------------------------------------
144 
147 
150 
153 
154  // -----------------------------------------------------
155  // Generate Random Positions
156  // -----------------------------------------------------
157 
158  void prepare( Sample const& sample );
159  std::vector<size_t> generate( PlacementTreeEdge const& edge );
160 
161  std::string dump_edge_proximities() const;
162  std::vector<size_t> edge_proximity_maxima() const;
163 
164  // -----------------------------------------------------
165  // Data Members
166  // -----------------------------------------------------
167 
168 public:
169 
170  std::vector<double> placement_number_weights;
171  std::vector<double> placement_path_length_weights;
172 
173 private:
174 
175  std::discrete_distribution<size_t> placement_number_distrib_;
176  std::discrete_distribution<size_t> placement_path_length_distrib_;
177 
178  std::vector<EdgeProximity> edge_proximities_;
179 };
180 
181 // =================================================================================================
182 // Placement Simulator Like Weight Ratio Distribution
183 // =================================================================================================
184 
186 {
187 public:
188 
189  // -------------------------------------------------
190  // Constructor and Rule of Five
191  // -------------------------------------------------
192 
195 
198 
201 
202  // -----------------------------------------------------
203  // Generate Random Ratios
204  // -----------------------------------------------------
205 
209  void prepare( Sample const& sample );
210 
214  double generate()
215  {
216  return distrib_( utils::Options::get().random_engine() );
217  }
218 
219  // -----------------------------------------------------
220  // Data Members
221  // -----------------------------------------------------
222 
223 public:
224 
225  std::vector<double> intervals;
226  std::vector<double> weights;
227 
228 private:
229 
230  std::piecewise_linear_distribution<double> distrib_;
231 };
232 
233 // =================================================================================================
234 // Placement Simulator Pendant Length Distribution
235 // =================================================================================================
236 
238 {
239 public:
240 
241  // -------------------------------------------------
242  // Constructor and Rule of Five
243  // -------------------------------------------------
244 
247 
250 
253 
254  // -----------------------------------------------------
255  // Generate Random Length
256  // -----------------------------------------------------
257 
261  void prepare( Sample const& sample )
262  {
263  (void) sample;
264  distrib_ = std::uniform_real_distribution<double>(min, max);
265  }
266 
270  double generate( PlacementTreeEdge const& edge )
271  {
272  // We don't use the edge in the default distribution.
273  (void) edge;
274 
275  return distrib_( utils::Options::get().random_engine() );
276  }
277 
278  // -----------------------------------------------------
279  // Data Members
280  // -----------------------------------------------------
281 
282 public:
283 
284  double min = 0.0;
285  double max = 1.0;
286 
287 private:
288 
289  std::uniform_real_distribution<double> distrib_;
290 };
291 
292 // =================================================================================================
293 // Placement Simulator Proximal Length Distribution
294 // =================================================================================================
295 
297 {
298 public:
299 
300  // -------------------------------------------------
301  // Constructor and Rule of Five
302  // -------------------------------------------------
303 
306 
309 
312 
313  // -----------------------------------------------------
314  // Generate Random Positions
315  // -----------------------------------------------------
316 
320  void prepare( Sample const& sample )
321  {
322  (void) sample;
323  distrib_ = std::uniform_real_distribution<double>( 0.0, 1.0 );
324  }
325 
329  double generate( PlacementTreeEdge const& edge )
330  {
331  // We do a multiplication with the branch length here, because this allows for a single
332  // distribution instance instead of one per different length.
333  auto branch_length = edge.data<PlacementEdgeData>().branch_length;
334  return distrib_( utils::Options::get().random_engine() ) * branch_length;
335  }
336 
337  // -----------------------------------------------------
338  // Data Members
339  // -----------------------------------------------------
340 
341 private:
342 
343  std::uniform_real_distribution<double> distrib_;
344 };
345 
346 } // namespace placement
347 } // namespace genesis
348 
349 #endif // include guard
genesis::placement::SimulatorLikeWeightRatioDistribution
Definition: distributions.hpp:185
genesis::placement::SimulatorExtraPlacementDistribution::EdgeProximity::total_candidates
size_t total_candidates
Definition: distributions.hpp:138
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::SimulatorEdgeDistribution::edge_weights
std::vector< double > edge_weights
Definition: distributions.hpp:105
genesis::placement::SimulatorPendantLengthDistribution::operator=
SimulatorPendantLengthDistribution & operator=(SimulatorPendantLengthDistribution const &)=default
genesis::placement::SimulatorPendantLengthDistribution::SimulatorPendantLengthDistribution
SimulatorPendantLengthDistribution()=default
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::SimulatorPendantLengthDistribution::max
double max
Definition: distributions.hpp:285
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::placement::SimulatorEdgeDistribution::SimulatorEdgeDistribution
SimulatorEdgeDistribution()=default
genesis::placement::SimulatorLikeWeightRatioDistribution::SimulatorLikeWeightRatioDistribution
SimulatorLikeWeightRatioDistribution()=default
genesis::placement::SimulatorExtraPlacementDistribution::EdgeProximity
Definition: distributions.hpp:134
genesis::placement::SimulatorExtraPlacementDistribution::dump_edge_proximities
std::string dump_edge_proximities() const
Definition: distributions.cpp:179
genesis::placement::SimulatorLikeWeightRatioDistribution::operator=
SimulatorLikeWeightRatioDistribution & operator=(SimulatorLikeWeightRatioDistribution const &)=default
genesis::placement::SimulatorPendantLengthDistribution::prepare
void prepare(Sample const &sample)
Prepare the distribution for usage. Needs to be called before generate().
Definition: distributions.hpp:261
genesis::placement::SimulatorExtraPlacementDistribution
Generate a certain number of additional PqueryPlacements around a given PlacementTreeEdge.
Definition: distributions.hpp:126
genesis::placement::SimulatorEdgeDistribution
Definition: distributions.hpp:49
genesis::placement::SimulatorExtraPlacementDistribution::operator=
SimulatorExtraPlacementDistribution & operator=(SimulatorExtraPlacementDistribution const &)=default
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::PlacementEdgeData
Data class for PlacementTreeEdges. Stores the branch length of the edge, and the edge_num,...
Definition: placement_tree.hpp:139
genesis::placement::SimulatorExtraPlacementDistribution::prepare
void prepare(Sample const &sample)
Prepare the distribution for usage. Needs to be called before generate().
Definition: distributions.cpp:56
genesis::placement::SimulatorEdgeDistribution::operator=
SimulatorEdgeDistribution & operator=(SimulatorEdgeDistribution const &)=default
genesis::placement::SimulatorProximalLengthDistribution::~SimulatorProximalLengthDistribution
~SimulatorProximalLengthDistribution()=default
genesis::placement::SimulatorExtraPlacementDistribution::~SimulatorExtraPlacementDistribution
~SimulatorExtraPlacementDistribution()=default
genesis::placement::SimulatorProximalLengthDistribution::operator=
SimulatorProximalLengthDistribution & operator=(SimulatorProximalLengthDistribution const &)=default
genesis::placement::SimulatorLikeWeightRatioDistribution::intervals
std::vector< double > intervals
Definition: distributions.hpp:225
genesis::placement::SimulatorExtraPlacementDistribution::SimulatorExtraPlacementDistribution
SimulatorExtraPlacementDistribution()=default
genesis::placement::SimulatorPendantLengthDistribution::min
double min
Definition: distributions.hpp:284
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::SimulatorLikeWeightRatioDistribution::weights
std::vector< double > weights
Definition: distributions.hpp:226
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::SimulatorExtraPlacementDistribution::EdgeProximity::candidates_per_level
std::vector< EdgeCandidates > candidates_per_level
Definition: distributions.hpp:137
options.hpp
genesis::placement::SimulatorLikeWeightRatioDistribution::~SimulatorLikeWeightRatioDistribution
~SimulatorLikeWeightRatioDistribution()=default
genesis::placement::SimulatorExtraPlacementDistribution::generate
std::vector< size_t > generate(PlacementTreeEdge const &edge)
Definition: distributions.cpp:119
genesis::placement::SimulatorEdgeDistribution::~SimulatorEdgeDistribution
~SimulatorEdgeDistribution()=default
genesis::placement::SimulatorPendantLengthDistribution
Definition: distributions.hpp:237
genesis::placement::SimulatorProximalLengthDistribution
Definition: distributions.hpp:296
genesis::placement::SimulatorProximalLengthDistribution::generate
double generate(PlacementTreeEdge const &edge)
Return a randomly chosen position on an edge.
Definition: distributions.hpp:329
genesis::placement::SimulatorPendantLengthDistribution::~SimulatorPendantLengthDistribution
~SimulatorPendantLengthDistribution()=default
genesis::utils::Options::get
static Options & get()
Returns a single instance of this class.
Definition: options.hpp:60
sample.hpp
genesis::tree::TreeEdge::data
EdgeDataType & data()
Definition: edge.hpp:217
genesis::placement::SimulatorExtraPlacementDistribution::placement_path_length_weights
std::vector< double > placement_path_length_weights
Definition: distributions.hpp:171
genesis::placement::SimulatorExtraPlacementDistribution::EdgeProximity::EdgeCandidates
std::vector< size_t > EdgeCandidates
Definition: distributions.hpp:136
genesis::placement::SimulatorPendantLengthDistribution::generate
double generate(PlacementTreeEdge const &edge)
Return a randomly chosen position on an edge.
Definition: distributions.hpp:270
genesis::placement::SimulatorExtraPlacementDistribution::placement_number_weights
std::vector< double > placement_number_weights
Definition: distributions.hpp:170
genesis::placement::SimulatorProximalLengthDistribution::SimulatorProximalLengthDistribution
SimulatorProximalLengthDistribution()=default
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::SimulatorExtraPlacementDistribution::edge_proximity_maxima
std::vector< size_t > edge_proximity_maxima() const
Definition: distributions.cpp:201