forked from tbrown91/SimBac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecomb_event.h
265 lines (258 loc) · 8.97 KB
/
recomb_event.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//Copyright (C) 2015 Thomas Brown, Xavier Didelot, Daniel J. Wilson, Nicola De Maio
//
// This file is part of SimBac.
//
// SimBac is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SimBac is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SimBac. If not, see <http://www.gnu.org/licenses/>.
#ifndef RECOMB_EVENT
#define RECOMB_EVENT
#include <math.h>
void split_ancestries(list<int> &starts_1, list<int> &ends_1, list<int> &starts_2, list<int> &ends_2, const int beg, const int end){
//Split the ancestry of the recombinant node into two, extracting the recombinant interval
list<int>::iterator itStart1=starts_1.begin(), itEnd1=ends_1.begin();
if (beg < end){
//Recombinant break does not wrap around the end of the genome
while (itStart1!=starts_1.end()){
//Find the interval containing the start and end points of the recombinant interval
if (end < *itStart1) return; //No more intervals affected by recombinant interval
else if (beg > *itEnd1){
//Ancestral interval is not in recombinant break
++itStart1;
++itEnd1;
}else if ((beg <= *itStart1) && (end > *itEnd1)){
//Recombinant break takes entire ancestral interval
starts_2.push_back(*itStart1);
ends_2.push_back(*itEnd1);
itStart1 = starts_1.erase(itStart1);
itEnd1 = ends_1.erase(itEnd1);
}else if ((beg <= *itStart1) && (end > *itStart1)){
//Break falls at start of ancestral interval
starts_2.push_back(*itStart1);
ends_2.push_back(end-1);
*itStart1 = end;
return; //No more ancestral inervals to check
}else if ((beg <= *itEnd1) && (end > *itEnd1)){
//Break falls at end of ancestral interval
starts_2.push_back(beg);
ends_2.push_back(*itEnd1);
*itEnd1 = beg - 1;
++itStart1;
++itEnd1;
}else{
//Break falls inside ancestral interval
starts_2.push_back(beg);
ends_2.push_back(end-1);
ends_1.insert(itEnd1,beg-1);
++itStart1;
starts_1.insert(itStart1,end);
return;//No more ancestral inervals to check
}
}
}else{
//Recombinant break wraps around the end of the genome
if (beg == end){
//Recombinant interval takes entire genome
starts_2 = starts_1;
ends_2 = ends_1;
starts_1.clear();
ends_1.clear();
}else{
while (itStart1 != starts_1.end()){
if ((end > *itEnd1) || (beg <= *itStart1)){
//Break covers the ancestral interval
starts_2.push_back(*itStart1);
ends_2.push_back(*itEnd1);
itStart1 = starts_1.erase(itStart1);
itEnd1 = ends_1.erase(itEnd1);
}else if ((end > *itStart1) && (beg <= *itEnd1)){
//Break covers start and end of interval
starts_2.push_back(*itStart1);
ends_2.push_back(end-1);
starts_2.push_back(beg);
ends_2.push_back(*itEnd1);
*itStart1 = end;
*itEnd1 = beg-1;
++itStart1;
++itEnd1;
}else if ((end > *itStart1) && (end < *itEnd1)){
//Break falls across an interval at the start
starts_2.push_back(*itStart1);
ends_2.push_back(end-1);
*itStart1 = end;
++itStart1;
++itEnd1;
}else if ((beg <= *itEnd1) && (beg > *itStart1)){
//Break falls across an interval at the end
starts_2.push_back(beg);
ends_2.push_back(*itEnd1);
*itEnd1 = beg - 1;
++itStart1;
++itEnd1;
}else{
++itEnd1;
++itStart1;
}
}
}
}
}
void choose_nonClonalRecomb(const vector<double> &prob, const int G, const list<int> &starts, const list<int> &ends, int &beg, int &end, const double noStop, const int totMaterial, const double recombRate){
//Choose a recombination interval for the chosen lineage which is non-clonal
int b=starts.size();
double r_1 = (gsl_rng_uniform(rng)*recombRate);
int index = 0;
list<int>::const_iterator itStart = starts.begin(), itEnd = ends.begin();
while (r_1 > prob[index]){
r_1 -= prob[index];
++index;
if (index == b) break;
}
if (index == b){
//Choose a start point within the ancestral intervals
beg = (int)floor(gsl_rng_uniform(rng)*(totMaterial-b));
beg += *itStart+1;
++itStart;
for (itEnd=ends.begin();itEnd!=ends.end();++itEnd){
if (beg > *itEnd){
beg = beg - *itEnd + *itStart;
}else break;
++itStart;
}
double r_2 = gsl_rng_uniform(rng);
int len = (int)ceil(log(1-r_2*(1-pow(noStop,G-1)))/log(noStop));
end = (beg + len) % G;
}else{
//Start of recombination occurs at beginning of an ancestral interval
if (index == 0){
if ((starts.front() == 0) && (ends.back() == G-1)){
//Start site is at beginning of genome inside an ancestral interval
beg = 0;
double r_2 = gsl_rng_uniform(rng);
int len = (int)ceil(log(1-r_2*(1-pow(noStop,G-1)))/log(noStop));
end = (beg + len) % G;
}else{
//Interval starts at first interval
beg = starts.front();
double r_2 = gsl_rng_uniform(rng);
int len = (int)ceil(log(1-r_2*(1-pow(noStop,ends.back()-starts.front())))/log(noStop));
end = (beg + len) % G;
}
}else{
itStart = starts.begin();
itEnd = ends.begin();
advance(itEnd,index-1);
advance(itStart,index);
beg = *itStart;
//Simulate recombinant break length via a truncated geometric distribution
double r_2 = gsl_rng_uniform(rng);
int len = (int)ceil(log(1-r_2*(1-pow(noStop,G+(*itEnd)-(*itStart))))/log(noStop));
end = (beg + len) % G;
}
}
itStart = starts.begin();
itEnd = ends.begin();
if ((starts.front() == 0) && (ends.back() == G-1)){
//Ancestral material wraps around end of genome
if (b>1){
//Check if end of recombinant interval falls between ancestral intervals
++itStart;
while (itStart!=starts.end()){
if ((end > *itEnd) && (end <= *itStart)){
end = *itEnd+1;
break;
}else if (end <= *itEnd){break;}
++itEnd;
++itStart;
}
}
}else{
//Check if end of recombinant interval falls between ancestral intervals
if ((end <= starts.front()) || (end > ends.back())) end = ends.back()+1;
else{
++itStart;
while(itStart!=starts.end()){
if ((end > *itEnd) && (end <= *itStart)){
end = *itEnd+1;
break;
}else if (end <= *itEnd){break;}
++itEnd;
++itStart;
}
}
}
}
void choose_clonalRecomb(const vector<double> &prob, const int G, const list<int> &starts, const list<int> &ends, int &beg, int &end, const double delta, const int totMaterial, const double recombRate){
//Choose a recombination interval for the chosen lineage which is non-clonal
int b=starts.size();
double r_1 = (gsl_rng_uniform(rng)*recombRate);
int index = 0;
list<int>::const_iterator itStart = starts.begin(), itEnd = ends.begin();
while (r_1 >= prob[index]){
r_1 -= prob[index];
++index;
if (index == b) break;
}
if (index == b){
//Choose a start point within the ancestral intervals
beg = (int)floor(gsl_rng_uniform(rng)*(totMaterial-b));
beg += starts.front()+1;
itStart = starts.begin();
++itStart;
for (itEnd=ends.begin();itEnd!=ends.end();++itEnd){
if (beg > *itEnd){
beg = beg - *itEnd + *itStart;
}else break;
++itStart;
}
}else{
advance(itStart,index);
beg = *itStart;
}
int len = gsl_ran_geometric(rng,1.0/delta);
if (len > G){
len = G;
}
end = (beg + len) % G;
itStart = starts.begin();
itEnd = ends.begin();
if ((starts.front() == 0) && (ends.back() == G-1)){
//Ancestral material wraps around end of genome
if (b>1){
//Check if end of recombinant interval falls between ancestral intervals
++itStart;
while (itStart != starts.end()){
if ((end > *itEnd) && (end <= *itStart)){
end = *itEnd+1;
break;
}else if (end <= *itEnd){break;}
++itEnd;
++itStart;
}
}
}else{
//Check if end of recombinant interval falls between ancestral intervals
if ((end <= starts.front()) || (end > ends.back())) end = ends.back()+1;
else{
++itStart;
while (itStart != starts.end()){
if ((end > *itEnd) && (end <= *itStart)){
end = *itEnd+1;
break;
}else if (end <= *itEnd){break;}
++itEnd;
++itStart;
}
}
}
}
#endif