/
wright_fisher.py
185 lines (164 loc) · 7.15 KB
/
wright_fisher.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
#
# Copyright (C) 2017 Kevin Thornton <krthornt@uci.edu>
#
# This file is part of fwdpy11.
#
# fwdpy11 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.
#
# fwdpy11 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 fwdpy11. If not, see <http://www.gnu.org/licenses/>.
#
from .wfevolve import evolve_singlepop_regions_cpp
def quick_sim(ngens = None):
"""
A convenience function for rapidly creating a
:class:`fwdpy11.fwdpy11_types.Spop`
.. testcode::
import fwdpy11.wright_fisher
#This will simulate N=1e3 for 10N generations
pop = fwdpy11.wright_fisher.quick_sim()
print(pop.N)
print(pop.generation)
The output is:
.. testoutput::
1000
10000
.. note::
Implemented via a call to :func:`fwdpy11.wright_fisher.evolve`
"""
from .fwdpy11_types import GSLrng,Spop
rng = GSLrng(42)
pop=Spop(1000)
if ngens is None:
evolve(rng,pop)
else:
import numpy as np
nlist = np.array([pop.N]*ngens,dtype=np.uint32)
evolve(rng,pop,nlist)
return pop
def evolve(rng,pop,popsizes = None,mu_neutral=None,
mu_selected = None,recrate=None,sregions=None):
"""
.. testcode::
import fwdpy11 as fp11
import fwdpy11.wright_fisher as wf
import numpy as np
rng = fp11.GSLrng(42)
p = fp11.Spop(1000)
wf.evolve(rng,p)
print(p.generation)
nlist=np.array([5000]*323,dtype=np.uint32)
wf.evolve(rng,p,nlist)
print(p.N)
print(p.generation)
.. testoutput::
10000
5000
10323
"""
if popsizes is None:
import numpy as np
popsizes = np.array([pop.N]*10*pop.N,dtype=np.uint32)
if mu_neutral is None:
mu_neutral = 100./float(4*pop.N)
if mu_selected is None:
mu_selected = 10./float(4*pop.N)
if recrate is None:
recrate = 100./float(4*pop.N)
if sregions is None:
from .regions import ExpS
sregions = [ExpS(0,1,1,-0.1,1.0)]
from .regions import Region
nr=[Region(0,1,1)]
return evolve_regions(rng,pop,popsizes,mu_neutral,
mu_selected,recrate,nr,sregions,
nr)
def evolve_regions(rng,pop,popsizes,mu_neutral,
mu_selected,recrate,nregions,sregions,recregions,
selfing_rate = 0.):
"""
Evolve a single deme according to a Wright-Fisher life cycle
with arbitrary changes in population size and a temporal sampler.
:param rng: A :class:`fwdpy11.fwdpy11_types.GSLrng`
:param pop: A :class:`fwdpy11.fwdpy11_types.Spop`
:param popsizes: A 1d NumPy array representing population sizes over time.
:param mu_neutral: The neutral mutation rate (per gamete, per generation)
:param mu_selected: The selected mutation rate (per gamete, per generation)
:param recrate: The recombination reate (per diploid, per generation)
:param nregions: A list of :class:`fwdpy11.regions.Region`.
:param sregions: A list of :class:`fwdpy11.regions.Sregion`.
:param recregions: A list of :class:`fwdpy11.regions.Region`.
:param recorder: A callable to record data from the population.
:param selfing_rate: (default 0.0) The probability than an individual selfs.
.. note::
The fitness model will be :class:`fwdpy11.fitness.SpopAdditive` constructed
with a scaling of 2.0. This function calls
:func:`fwdpy11.wright_fisher.evolve_regions_sampler`, passing in a
:class:`fwdpy11.temporal_samplers.RecordNothing` object.
"""
from .temporal_samplers import RecordNothing
recorder=RecordNothing()
return evolve_regions_sampler(rng,pop,popsizes,mu_neutral,
mu_selected,recrate,nregions,sregions,recregions,
recorder,selfing_rate)
def evolve_regions_sampler(rng,pop,popsizes,mu_neutral,
mu_selected,recrate,nregions,sregions,recregions,
recorder,selfing_rate = 0.):
"""
Evolve a single deme according to a Wright-Fisher life cycle
with arbitrary changes in population size and a temporal sampler.
:param rng: A :class:`fwdpy11.fwdpy11_types.GSLrng`
:param pop: A :class:`fwdpy11.fwdpy11_types.Spop`
:param popsizes: A 1d NumPy array representing population sizes over time.
:param mu_neutral: The neutral mutation rate (per gamete, per generation)
:param mu_selected: The selected mutation rate (per gamete, per generation)
:param recrate: The recombination reate (per diploid, per generation)
:param nregions: A list of :class:`fwdpy11.regions.Region`.
:param sregions: A list of :class:`fwdpy11.regions.Sregion`.
:param recregions: A list of :class:`fwdpy11.regions.Region`.
:param recorder: A callable to record data from the population.
:param selfing_rate: (default 0.0) The probability than an individual selfs.
.. note::
The fitness model will be :class:`fwdpy11.fitness.SpopAdditive` constructed
with a scaling of 2.0.
"""
from .fitness import SpopAdditive
fitness = SpopAdditive(2.0)
return evolve_regions_sampler_fitness(rng,pop,popsizes,mu_neutral,
mu_selected,recrate,nregions,sregions,recregions,fitness,
recorder,selfing_rate)
def evolve_regions_sampler_fitness(rng,pop,popsizes,mu_neutral,
mu_selected,recrate,nregions,sregions,recregions,fitness,
recorder,selfing_rate = 0.,prune_all_fixations=True):
"""
Evolve a single deme according to a Wright-Fisher life cycle
with arbitrary changes in population size, a specified fitness model,
and a temporal sampler.
:param rng: A :class:`fwdpy11.fwdpy11_types.GSLrng`
:param pop: A :class:`fwdpy11.fwdpy11_types.Spop`
:param popsizes: A 1d NumPy array representing population sizes over time.
:param mu_neutral: The neutral mutation rate (per gamete, per generation)
:param mu_selected: The selected mutation rate (per gamete, per generation)
:param recrate: The recombination reate (per diploid, per generation)
:param nregions: A list of :class:`fwdpy11.regions.Region`.
:param sregions: A list of :class:`fwdpy11.regions.Sregion`.
:param recregions: A list of :class:`fwdpy11.regions.Region`.
:param fitness: A :class:`fwdpy11.fitness.SpopFitness`.
:param recorder: A callable to record data from the population.
:param selfing_rate: (default 0.0) The probability than an individual selfs.
:param prune_all_fixations: (True) Remove fixations affecting fitness from population.
"""
from .internal import makeMutationRegions,makeRecombinationRegions
mm=makeMutationRegions(nregions,sregions)
rm=makeRecombinationRegions(recregions)
evolve_singlepop_regions_cpp(rng,pop,popsizes,mu_neutral,
mu_selected,recrate,mm,rm,fitness,recorder,selfing_rate,
prune_all_fixations)