-
Notifications
You must be signed in to change notification settings - Fork 3
/
demography.py
255 lines (206 loc) · 7.81 KB
/
demography.py
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
import msprime
import math
def constant(args):
"""Single population model with pop size Ne and constant growth"""
genob, params, randomize, i, proposals = args
necessary_params = ["mu", "r", "Ne"]
for p in necessary_params:
if p not in list(params.keys()):
print("Invalid combination of parameters. Needed: mu | r | Ne")
if proposals:
mu, r, Ne = [
params[p].prop(i) if params[p].inferable else params[p].val
for p in necessary_params
]
else:
mu, r, Ne = [
params[p].rand() if randomize else params[p].val for p in necessary_params
]
return msprime.simulate(
sample_size=genob.num_samples,
Ne=Ne,
length=genob.seq_len,
mutation_rate=mu,
recombination_rate=r,
# random_seed=genob.seed,
)
def exponential_model(args, print=False):
"""Single population model with sudden population size increase from N1 to N2
at time T1 and exponential growth at time T2"""
params, randomize, i, proposals = args
necessary_params = ["mu", "r", "T1", "N1", "T2", "N2", "growth"]
for p in necessary_params:
if p not in list(params.keys()):
print(
"Invalid combination of parameters. "
"Needed: mu | r | T1 | N1 | T2 | N2 | growth"
)
if proposals:
mu, r, T1, N1, T2, N2, growth = [
params[p].prop(i) if params[p].inferable else params[p].val
for p in necessary_params
]
else:
mu, r, T1, N1, T2, N2, growth = [
params[p].rand() if randomize else params[p].val for p in necessary_params
]
N0 = N1 / math.exp(-growth * T1)
# Time is given in generations unit (t/25)
demographic_events = [
msprime.PopulationParametersChange(time=0, initial_size=N0, growth_rate=growth),
msprime.PopulationParametersChange(time=T1, initial_size=N1, growth_rate=0),
msprime.PopulationParametersChange(time=T2, initial_size=N2),
]
if print:
debugger = msprime.DemographyDebugger(Ne=N0, demographic_events=demographic_events)
debugger.print_history()
return demographic_events, mu, r
def exponential(args):
genob = args[0]
demographic_events, mu, r = exponential_model(args[1:])
return msprime.simulate(
sample_size=genob.num_samples,
demographic_events=demographic_events,
length=genob.seq_len,
mutation_rate=mu,
recombination_rate=r,
# random_seed=genob.seed,
)
def zigzag_model(args, print=False):
"""Derived model used by Schiffels and Durbin (2014) and Terhorst and
Terhorst, Kamm, and Song (2017) with periods of exponential growth and
decline in a single population. Here, growth rates are changed to pop sizes.
Schiffels and Durbin, 2014. https://doi.org/10.1038/ng.3015"""
params, randomize, i, proposals = args
necessary_params = [
"mu",
"r",
"T1",
"N1",
"T2",
"N2",
"T3",
"N3",
"T4",
"N4",
"T5",
"N5",
]
for p in necessary_params:
if p not in list(params.keys()):
print(
"Invalid combination of parameters. Needed: "
"mu | r | T1 | N1 | T2 | N2 | T3 | N3 | T4 | N4 | T5 | N5"
)
if proposals:
mu, r, T1, N1, T2, N2, T3, N3, T4, N4, T5, N5 = [
params[p].prop(i) if params[p].inferable else params[p].val
for p in necessary_params
]
else:
mu, r, T1, N1, T2, N2, T3, N3, T4, N4, T5, N5 = [
params[p].rand() if randomize else params[p].val for p in necessary_params
]
generation_time = 30
N0 = 71560
n_ancient = N0 / 10
t_ancient = 34133.318528
demographic_events = [
msprime.PopulationParametersChange(time=0, initial_size=N0, population_id=0),
msprime.PopulationParametersChange(time=T1, initial_size=N1, population_id=0),
msprime.PopulationParametersChange(time=T2, initial_size=N2, population_id=0),
msprime.PopulationParametersChange(time=T3, initial_size=N3, population_id=0),
msprime.PopulationParametersChange(time=T4, initial_size=N4, population_id=0),
msprime.PopulationParametersChange(time=T5, initial_size=N5, population_id=0),
msprime.PopulationParametersChange(
time=t_ancient, initial_size=n_ancient, population_id=0
),
]
if print:
debugger = msprime.DemographyDebugger(Ne=71560, demographic_events=demographic_events)
debugger.print_history()
return demographic_events, mu, r
def zigzag(args):
genob = args[0]
demographic_events, mu, r = zigzag_model(args[1:])
return msprime.simulate(
sample_size=genob.num_samples,
demographic_events=demographic_events,
length=genob.seq_len,
mutation_rate=mu,
recombination_rate=r,
# random_seed=genob.seed,
)
def bottleneck_model(args, print=False):
params, randomize, i, proposals = args
necessary_params = ["mu", "r", "N0", "T1", "N1", "T2", "N2"]
for p in necessary_params:
if p not in list(params.keys()):
print(
"Invalid combination of parameters. Needed: "
"mu | r | N0 | T1 | N1 | T2 | N2"
)
if proposals:
mu, r, N0, T1, N1, T2, N2 = [
params[p].prop(i) if params[p].inferable else params[p].val
for p in necessary_params
]
else:
mu, r, N0, T1, N1, T2, N2 = [
params[p].rand() if randomize else params[p].val for p in necessary_params
]
# Infer the 3 pop sizes, where N0 = N2
demographic_events = [
msprime.PopulationParametersChange(time=0, initial_size=N0),
msprime.PopulationParametersChange(time=T1, initial_size=N1),
msprime.PopulationParametersChange(time=T2, initial_size=N2),
]
if print:
debugger = msprime.DemographyDebugger(Ne=10000, demographic_events=demographic_events)
debugger.print_history()
return demographic_events, mu, r
def bottleneck(args):
genob = args[0]
demographic_events, mu, r = bottleneck_model(args[1:])
return msprime.simulate(
sample_size=genob.num_samples,
demographic_events=demographic_events,
length=genob.seq_len,
mutation_rate=mu,
recombination_rate=r,
# random_seed=genob.seed,
)
def ghost_migration(args):
"""Mass migration at time T1 from population 1 with pop size N2 to population
0 with pop size N1. Samples are collected only from population 0."""
genob, params, randomize, i, proposals = args
necessary_params = ["mu", "r", "T1", "N1", "N2", "mig"]
for p in necessary_params:
if p not in list(params.keys()):
print(
"Invalid combination of parameters. "
"Needed: mu | r | T1 | N1 | N2 | mig"
)
if proposals:
mu, r, T1, N1, N2, mig = [
params[p].prop(i) if params[p].inferable else params[p].val
for p in necessary_params
]
else:
mu, r, T1, N1, N2, mig = [
params[p].rand() if randomize else params[p].val for p in necessary_params
]
population_configurations = [
msprime.PopulationConfiguration(sample_size=genob.num_samples, initial_size=N1),
msprime.PopulationConfiguration(sample_size=0, initial_size=N2),
]
# migration from pop 1 into pop 0 (back in time)
mig_event = msprime.MassMigration(time=T1, source=1, destination=0, proportion=mig)
return msprime.simulate(
population_configurations=population_configurations,
demographic_events=[mig_event],
length=genob.seq_len,
mutation_rate=mu,
recombination_rate=r,
# random_seed=genob.seed,
)