A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
std::vector< size_t > generate(PlacementTreeEdge const &edge)
Data class for PlacementTreeEdges. Stores the branch length of the edge, and the edge_num, as defined in the jplace standard.
PlacementTree & tree()
Get the PlacementTree of this Sample.
Definition: sample.cpp:119
void prepare(Sample const &sample)
Prepare the distribution for usage. Needs to be called before generate().
SimulatorProximalLengthDistribution & operator=(SimulatorProximalLengthDistribution const &)=default
SimulatorExtraPlacementDistribution & operator=(SimulatorExtraPlacementDistribution const &)=default
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().
void prepare(Sample const &sample)
Prepare the distribution for usage. Needs to be called before generate().
size_t edge_count() const
Return the number of TreeEdges of the Tree.
Definition: tree/tree.cpp:358
Generate a certain number of additional PqueryPlacements around a given PlacementTreeEdge.
Manage a set of Pqueries along with the PlacementTree where the PqueryPlacements are placed on...
Definition: sample.hpp:68
SimulatorPendantLengthDistribution & operator=(SimulatorPendantLengthDistribution const &)=default
void prepare(Sample const &sample)
Prepare the distribution for usage. Needs to be called before generate().
SimulatorEdgeDistribution & operator=(SimulatorEdgeDistribution const &)=default
double generate()
Return a randomly chosen like weight ratio.
SimulatorLikeWeightRatioDistribution & operator=(SimulatorLikeWeightRatioDistribution const &)=default
double generate(PlacementTreeEdge const &edge)
Return a randomly chosen position on an edge.
EdgeDataType & data()
Definition: edge.hpp:118
static Options & get()
Returns a single instance of this class.
Definition: options.hpp:59