Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
Hudson.hpp
2{
3 private:
4 // the set of extant ancestors
5 std::set<...> P;
6 // store the coalescence record
7 std::set<...> C;
8 // counts the number of extant segments intersecting with a given interval
9 std::map<..., ...> S;
10 // track the cumulative number of links subtended by each extant segment
11 BinaryIndexedTree<...> L;
12 // next available node
13 ... w;
14 // clock
15 ... t;
16
17 public:
25 {
26 // for each individual in the sample
27 for (int j = 1; j <= n; ++j)
28 {
29 // allocate a segment x covering the interval [0, m) that points to node j
30 auto x = Segment(0, m, j);
31 // record that this segment subtends m−1 links
32 this->L[k] = m - 1;
33 // insert it into the set of ancestors P
34 this->P.insert(x);
35 }
36
37 // initialise the map S stating that the number of extant segments in the interval [0, m) is n
38 this->S[0] = n;
39 this->S[m] -= 1;
40 // set the next available node w to n + 1
41 this->w = n + 1;
42 // set the clock to zero
43 this->t = 0;
44 } // end constructor
45
52 void event()
53 {
54 // find the total number of links subtended by all segments:
55 total = L.total();
56 // calculate the current rate of recombination
57 lambda_r = r(t) * total;
58 // calculate the current rate of common ancestor event
59 lambda = lambda_r + P.size() * (P.size() - 1);
60
61 // simulate timing and type of next event
62 std::exponential_distribution<> exp_dis(lambda);
63 auto delta_t = exp_dis(generator);
64 t = t + delta_t;
65 std::uniform_real_distribution<> unif_dis(0.0, 1.0) if (unif_dis(gen) < lambda_r / lambda)
66 {
67 // invoke algorithm R
69 }
70 else
71 {
72 // invoke algorithm C
73 common_ancestor_event();
74 }
75 }
76
84 {
85 R1_choose_link();
86 R2_break_between_segments();
87 R3_break_within_segments();
88 R4_update_population();
89 }
90
91 private:
92 auto R1_choose_link()
93 {
94 // pick a link h from the total(L) that are currently being tracked
95 std::uniform_real_distribution<> unif_dis(1, L.total());
96 auto h = unif_dis(generator);
97 // ind the segment y that subtends this link
98 auto y = L.find(h);
99 // calculate the corresponding breakpoint k
100 auto k = right(y) - L.total(y) + h - 1 x = prev(y)
101 // does link h falls within y or between y and its predecessor x?
102 if (left(y) < k)
103 {
104 R3_break_within_segments();
105 }
106 else
107 {
108 R2_break_between_segments();
109 }
110 }
111
112 auto R2_break_between_segments()
113 {
114 // big_lambda is equivalent to null
115 // simply break the forward and reverse links in the segment chain between them
116 next(x) = big_lambda;
117 prev(y) = big_lambda;
118 // independent segment chain starting with z
119 // which represents the new individual to be added to the set of ancestor
120 auto z = y;
121 R4_update_population();
122 }
123
124 // @brief Split the segment such that the ancestral material from left(y) to k
125 // remains assigned to the current individual and the remainder
126 // is assigned to the new individual z.
127 auto R3_break_within_segments()
128 {
129 // independent segment chain starting with z, which represents the new individual
130 // to be added to the set of ancestor
131 z = Segment(k, right(y), node(y), big_lambda, next(y));
132 if (next(y) != big_lambda)
133 {
134 prev(next(y)) = z;
135 }
136 next(y) = big_lambda;
137 right(y) = k;
138 // update the number of links subtended by the segment y, which has
139 // right(z) − k fewer links as a result of this operation
140 L[y] += k - right(z);
141 }
142
143 auto R4_update_population()
144 {
145 // update the information about the number of links subtended by this segment
146 L[z] = right(z) - left(z) - 1;
147 // Insert the segment z into the set of ancestors
148 this->P.insert(z)
149 }
150
151}; // end class HudsonAlgorithm
Definition Hudson.hpp:2
HudsonAlgorithm()
Constructor.
Definition Hudson.hpp:24
void event()
?
Definition Hudson.hpp:52
void recombination_event()
Choose a link uniformly and break it, resulting in one extra individual in the set of extant ancestor...
Definition Hudson.hpp:83