Skip to content

demographics

Equilibrium, two epoch, and three epoch (bottleneck) model with selection (sig1, sig2) on the two derived alleles

bottlegrowth(params, ns, pts, sig1=0.0, sig2=0.0, theta1=1.0, theta2=1.0, misid=0.0, dt=0.005, folded=False)

Three epoch demography - two instantaneous population size changes in the past Args: params (list[float]): [nu1,nu2,T1,T2] - nuB,nuF - relative poplulation size changes to ancestral population size (nu1 occurs before nu2, historically)

    - T - time for which population had relative sizes nuB, nuF (scaled by 2N generations)
sig1 (float): population scaled selection coefficient for the first derived allele.
sig2 (float): population scaled selection coefficients for the second derived allele.
theta1 (float): population scaled mutation rates for the first derived allele.
theta2 (float): population scaled mutation rates for the second derived allele.
misid(float): ancestral misidentification parameter
dt (float): time step to use for integration
folded (bool): If True, fold the frequency spectrum (if we assume we don't know the order that derived alleles appeared).
Source code in dadi/Triallele/demographics.py
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
def bottlegrowth(params, ns, pts, sig1 = 0.0, sig2 = 0.0, theta1 = 1.0, theta2 = 1.0, misid = 0.0, dt = 0.005, folded = False):
    """
    Three epoch demography - two instantaneous population size changes in the past
    Args:
        params (list[float]): [nu1,nu2,T1,T2]
            - nuB,nuF - relative poplulation size changes to ancestral population size (nu1 occurs before nu2, historically)

            - T - time for which population had relative sizes nuB, nuF (scaled by 2N generations)
        sig1 (float): population scaled selection coefficient for the first derived allele.
        sig2 (float): population scaled selection coefficients for the second derived allele.
        theta1 (float): population scaled mutation rates for the first derived allele.
        theta2 (float): population scaled mutation rates for the second derived allele.
        misid(float): ancestral misidentification parameter
        dt (float): time step to use for integration
        folded (bool): If True, fold the frequency spectrum (if we assume we don't know the order that derived alleles appeared).
    """
    nuB,nuF,T = params
    if nuB == nuF:
        nu = nuB
    else:
        nu = lambda t: nuB*np.exp(np.log(nuF/nuB) * t/T)

    x = np.linspace(0,1,pts+1)
    sig1,sig2 = np.float(sig1),np.float(sig2)

    y1 = dadi.PhiManip.phi_1D(x,gamma=sig1)
    y2 = dadi.PhiManip.phi_1D(x,gamma=sig2)
    phi = np.zeros((len(x),len(x)))

    # integrate to equilibrium first
    if sig1 == sig2 == 0.0 and theta1 == theta2 == 1:
        phi = integration.equilibrium_neutral_exact(x)
    else:
        phi = integration.equilibrium_neutral_exact(x)
        phi,y1,y2 = integration.advance(phi, x, 2, y1, y2, nu=1., sig1=sig1, sig2=sig2, theta1=theta1, theta2=theta2, dt=dt)

    phi,y1,y2 = integration.advance(phi, x, T, y1, y2, nu, sig1, sig2, theta1, theta2, dt=dt)

    dx = numerics.grid_dx(x)

    try:
        ns = int(ns)
    except TypeError:
        ns = ns[0]

    fs = numerics.sample(phi, ns, x)
    fs.extrap_t = dt

    if folded == True:
        fs = fs.fold_major()

    if misid > 0.0:
        fs = numerics.misidentification(fs,misid)

    return fs

equilibrium(params, ns, pts, sig1=0.0, sig2=0.0, theta1=1.0, theta2=1.0, misid=0.0, dt=0.005, folded=False)

Integrate the density function to equilibrium Args: params (list): unused sig1 (float): population scaled selection coefficient for the first derived allele. sig2 (float): population scaled selection coefficients for the second derived allele. theta1 (float): population scaled mutation rates for the first derived allele. theta2 (float): population scaled mutation rates for the second derived allele. misid(float): ancestral misidentification parameter dt (float): time step to use for integration folded (bool): If True, fold the frequency spectrum (if we assume we don't know the order that derived alleles appeared).

Source code in dadi/Triallele/demographics.py
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
def equilibrium(params, ns, pts, sig1 = 0.0, sig2 = 0.0, theta1 = 1.0, theta2 = 1.0, misid = 0.0, dt = 0.005, folded = False):
    """
    Integrate the density function to equilibrium
    Args:
        params (list): unused
        sig1 (float): population scaled selection coefficient for the first derived allele.
        sig2 (float): population scaled selection coefficients for the second derived allele.
        theta1 (float): population scaled mutation rates for the first derived allele.
        theta2 (float): population scaled mutation rates for the second derived allele.
        misid(float): ancestral misidentification parameter
        dt (float): time step to use for integration
        folded (bool): If True, fold the frequency spectrum (if we assume we don't know the order that derived alleles appeared).
    """
    x = np.linspace(0,1,pts+1)
    sig1,sig2 = np.float(sig1),np.float(sig2)

    y1 = dadi.PhiManip.phi_1D(x,gamma=sig1)
    y2 = dadi.PhiManip.phi_1D(x,gamma=sig2)
    phi = np.zeros((len(x),len(x)))
    if sig1 == sig2 == 0.0 and theta1 == theta2 == 1:
        phi = integration.equilibrium_neutral_exact(x)
    else:
        phi = integration.equilibrium_neutral_exact(x)
        phi,y1,y2 = integration.advance(phi, x, 2, y1, y2, nu=1., sig1=sig1, sig2=sig2, theta1=theta1, theta2=theta2, dt=dt)

    dx = numerics.grid_dx(x)

    try:
        ns = int(ns)
    except TypeError:
        ns = ns[0]

    fs = numerics.sample(phi, ns, x)
    fs.extrap_t = dt

    if folded == True:
        fs = fs.fold_major()

    if misid > 0.0:
        fs = numerics.misidentification(fs,misid)

    return fs

three_epoch(params, ns, pts, sig1=0.0, sig2=0.0, theta1=1.0, theta2=1.0, misid=0.0, dt=0.005, folded=False)

Three epoch demography - two instantaneous population size changes in the past Args: params (list[float]): [nu1,nu2,T1,T2] - nu1,nu2 - relative poplulation size changes to ancestral population size (nu1 occurs before nu2, historically)

    - T1,T2 - time for which population had relative sizes nu1, nu2 (scaled by 2N generations)
sig1 (float): population scaled selection coefficient for the first derived allele.
sig2 (float): population scaled selection coefficients for the second derived allele.
theta1 (float): population scaled mutation rates for the first derived allele.
theta2 (float): population scaled mutation rates for the second derived allele.
misid(float): ancestral misidentification parameter
dt (float): time step to use for integration
folded (bool): If True, fold the frequency spectrum (if we assume we don't know the order that derived alleles appeared).
Source code in dadi/Triallele/demographics.py
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
def three_epoch(params, ns, pts, sig1 = 0.0, sig2 = 0.0, theta1 = 1.0, theta2 = 1.0, misid = 0.0, dt = 0.005, folded = False):
    """
    Three epoch demography - two instantaneous population size changes in the past
    Args:
        params (list[float]): [nu1,nu2,T1,T2]
            - nu1,nu2 - relative poplulation size changes to ancestral population size (nu1 occurs before nu2, historically)

            - T1,T2 - time for which population had relative sizes nu1, nu2 (scaled by 2N generations)
        sig1 (float): population scaled selection coefficient for the first derived allele.
        sig2 (float): population scaled selection coefficients for the second derived allele.
        theta1 (float): population scaled mutation rates for the first derived allele.
        theta2 (float): population scaled mutation rates for the second derived allele.
        misid(float): ancestral misidentification parameter
        dt (float): time step to use for integration
        folded (bool): If True, fold the frequency spectrum (if we assume we don't know the order that derived alleles appeared).
    """
    nu1,nu2,T1,T2 = params
    x = np.linspace(0,1,pts+1)
    sig1,sig2 = np.float(sig1),np.float(sig2)

    y1 = dadi.PhiManip.phi_1D(x,gamma=sig1)
    y2 = dadi.PhiManip.phi_1D(x,gamma=sig2)
    phi = np.zeros((len(x),len(x)))

    # integrate to equilibrium first
    if sig1 == sig2 == 0.0 and theta1 == theta2 == 1:
        phi = integration.equilibrium_neutral_exact(x)
    else:
        phi = integration.equilibrium_neutral_exact(x)
        phi,y1,y2 = integration.advance(phi, x, 2, y1, y2, nu=1., sig1=sig1, sig2=sig2, theta1=theta1, theta2=theta2, dt=dt)

    phi,y1,y2 = integration.advance(phi, x, T1, y1, y2, nu1, sig1, sig2, theta1, theta2, dt=dt)
    phi,y1,y2 = integration.advance(phi, x, T2, y1, y2, nu2, sig1, sig2, theta1, theta2, dt=dt)

    dx = numerics.grid_dx(x)

    try:
        ns = int(ns)
    except TypeError:
        ns = ns[0]

    fs = numerics.sample(phi, ns, x)
    fs.extrap_t = dt

    if folded == True:
        fs = fs.fold_major()

    if misid > 0.0:
        fs = numerics.misidentification(fs,misid)

    return fs

two_epoch(params, ns, pts, sig1=0.0, sig2=0.0, theta1=1.0, theta2=1.0, misid=0.0, dt=0.005, folded=False)

Two epoch demography - a single population size change at some point in the past Args: params (list[float]): [nu,T,sig1,sig2,theta1,theta2,misid,dt] - nu - relative poplulation size change to ancestral population size

    - T - time in past that size change occured (scaled by 2N generations)
sig1 (float): population scaled selection coefficient for the first derived allele.
sig2 (float): population scaled selection coefficients for the second derived allele.
theta1 (float): population scaled mutation rates for the first derived allele.
theta2 (float): population scaled mutation rates for the second derived allele.
misid(float): ancestral misidentification parameter
dt (float): time step to use for integration
folded (bool): If True, fold the frequency spectrum (if we assume we don't know the order that derived alleles appeared).
Source code in dadi/Triallele/demographics.py
 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
def two_epoch(params, ns, pts, sig1 = 0.0, sig2 = 0.0, theta1 = 1.0, theta2 = 1.0, misid = 0.0, dt = 0.005, folded = False):
    """
    Two epoch demography - a single population size change at some point in the past
    Args:
        params (list[float]): [nu,T,sig1,sig2,theta1,theta2,misid,dt]
            - nu - relative poplulation size change to ancestral population size

            - T - time in past that size change occured (scaled by 2N generations)
        sig1 (float): population scaled selection coefficient for the first derived allele.
        sig2 (float): population scaled selection coefficients for the second derived allele.
        theta1 (float): population scaled mutation rates for the first derived allele.
        theta2 (float): population scaled mutation rates for the second derived allele.
        misid(float): ancestral misidentification parameter
        dt (float): time step to use for integration
        folded (bool): If True, fold the frequency spectrum (if we assume we don't know the order that derived alleles appeared).
    """
    nu,T = params

    x = np.linspace(0,1,pts+1)
    sig1,sig2 = np.float(sig1),np.float(sig2)

    y1 = dadi.PhiManip.phi_1D(x,gamma=sig1)
    y2 = dadi.PhiManip.phi_1D(x,gamma=sig2)
    phi = np.zeros((len(x),len(x)))

    # integrate to equilibrium first
    if sig1 == sig2 == 0.0 and theta1 == theta2 == 1:
        phi = integration.equilibrium_neutral_exact(x)
    else:
        phi = integration.equilibrium_neutral_exact(x)
        phi,y1,y2 = integration.advance(phi, x, 2, y1, y2, nu=1., sig1=sig1, sig2=sig2, theta1=theta1, theta2=theta2, dt=dt)

    phi,y1,y2 = integration.advance(phi, x, T, y1, y2, nu=nu, sig1=sig1, sig2=sig2, theta1=theta1, theta2=theta2, dt=dt)

    dx = numerics.grid_dx(x)

    try:
        ns = int(ns)
    except TypeError:
        ns = ns[0]

    fs = numerics.sample(phi, ns, x)
    fs.extrap_t = dt

    if folded == True:
        fs = fs.fold_major()

    if misid > 0.0:
        fs = numerics.misidentification(fs,misid)

    return fs