Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
HistoryBase.hpp
1// Copyright 2021 Arnaud Becheler <abechele@umich.edu>
2
3/*********************************************************************** * This program is free software; you can
4 *redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free
5 *Software Foundation; either version 2 of the License, or * (at your option) any later version. *
6 * *
7 ***************************************************************************/
8
9#ifndef __BASE_HISTORY_H_INCLUDED__
10#define __BASE_HISTORY_H_INCLUDED__
11
12#include "../utils/random/DiscreteDistribution.hpp" // BackwardKernel
13
14#include <functional> // std::cref
15#include <memory> // unique_ptr
16#include <vector>
17
18namespace quetzal
19{
20namespace demography
21{
39template <typename Space, typename DemographicPolicy, typename MemoryPolicy> class HistoryBase
40{
41 public:
43 using coord_type = Space;
45 using time_type = unsigned int;
47 using memory_policy = MemoryPolicy;
49 using demographic_policy = DemographicPolicy;
51 using value_type = typename demographic_policy::value_type;
53 using flow_type = typename memory_policy::template flow_type<coord_type, time_type, value_type>;
55 using pop_sizes_type = typename memory_policy::template pop_sizes_type<coord_type, time_type, value_type>;
65 HistoryBase(coord_type const &x, value_type N, unsigned int nb_generations)
66 : m_nb_generations(nb_generations), m_sizes(std::make_unique<pop_sizes_type>()),
67 m_flows(std::make_unique<flow_type>())
68 {
69 m_sizes->set(x, 0, N);
70 }
74 auto distribution_area(time_type t) const
75 {
76 return m_sizes->definition_space(t);
77 }
83 auto &pop_size(coord_type const &x, time_type const &t)
84 {
85 return m_sizes->operator()(x, t);
86 }
90 auto get_functor_N() const noexcept
91 {
92 auto N_ref = std::cref(this->m_sizes);
93 return [N_ref](coord_type const &x, time_type t) { return N_ref.get()->get(x, t); };
94 }
98 auto get_pop_size(coord_type const &x, time_type const &t)
99 {
100 return m_sizes->get(x, t);
101 }
105 void set_pop_size(coord_type const &x, time_type const &t, value_type N)
106 {
107 m_sizes->set(x, t, N);
108 }
112 unsigned int const &nb_generations() const
113 {
114 return m_nb_generations;
115 }
124 template <typename Generator> coord_type backward_kernel(coord_type const &x, time_type t, Generator &gen) const
125 {
126 --t;
127 assert(m_flows->flow_to_is_defined(x, t));
128 auto k = make_backward_distribution(x, t);
129 return k(gen);
130 }
131
132 protected:
133 // Need to be accessed by the expand method
134 std::unique_ptr<flow_type> m_flows;
135
136 private:
137 unsigned int m_nb_generations;
138 std::unique_ptr<pop_sizes_type> m_sizes;
142 auto make_backward_distribution(coord_type const &x, time_type const &t) const
143 {
144 std::vector<double> weights;
145 std::vector<coord_type> support;
146 weights.reserve(m_flows->flow_to(x, t).size());
147 support.reserve(m_flows->flow_to(x, t).size());
148 for (auto const &it : m_flows->flow_to(x, t))
149 {
150 support.push_back(it.first);
151 weights.push_back(static_cast<double>(it.second));
152 }
153 return discrete_distribution_type(std::move(support), std::move(weights));
154 }
155}; // end HistoryBase
156} // namespace demography
157} // namespace quetzal
158
159#endif
Base class for simulating and recording spatial population history, encpasulating backward migration ...
Definition HistoryBase.hpp:40
auto & pop_size(coord_type const &x, time_type const &t)
Population size at deme x at time t.
Definition HistoryBase.hpp:83
auto get_functor_N() const noexcept
Read-only functor.
Definition HistoryBase.hpp:90
void set_pop_size(coord_type const &x, time_type const &t, value_type N)
Used in derived class History to increment migrants.
Definition HistoryBase.hpp:105
HistoryBase(coord_type const &x, value_type N, unsigned int nb_generations)
Constructor initializing the demographic database.
Definition HistoryBase.hpp:65
coord_type backward_kernel(coord_type const &x, time_type t, Generator &gen) const
Samples a coordinate from the backward-in-time transition matrix.
Definition HistoryBase.hpp:124
auto distribution_area(time_type t) const
Retrieve demes where N > 0 at time t.
Definition HistoryBase.hpp:74
auto get_pop_size(coord_type const &x, time_type const &t)
Read-only.
Definition HistoryBase.hpp:98
unsigned int const & nb_generations() const
Last time recorded in the foward-in-time database history.
Definition HistoryBase.hpp:112
Sampling (non) arithmetic values in discrete probability distribution.
Definition DiscreteDistribution.hpp:29
Simulation of coalescence-based models of molecular evolution.
Definition coalescence.hpp:21