####### Import data and generate fs #######
dd = dadi.Misc.make_data_dict("dadi_input_filtercov_PB_PG")
pop_ids, ns = ['PB', 'PG'], [80, 80] # Project down to 40 (half the chromosomes) for faster computation
fs = dadi.Spectrum.from_data_dict(dd, pop_ids, ns, polarized = False) # fs folded because the ancestral state of SNPs is unknown
# We can save our extracted spectrum to disk
fs.mask[0:5,:] = True # mask the first 5 alleles because low confidence in frequency calling
fs.mask[:,0:5] = True
fs.to_file('PG_PB.fs')
####### Define a custom demography model #######
def split_mig_bottlegrowth(params, ns, pts):
"""
Model of split with migration then instantaneous size change = bottleneck in both populations
Tb is a fixed parameter and theta is inferred with the other parameters using multinom = False during optimization
params list is
nu1: Size of population 1 after split.
nu2: Size of population 2 after split.
nu1B: Ratio of population size after instantanous change to ancient population size for pop1
nu1F: Ratio of contempoary to ancient population size pop1
nu2B: Ratio of population size after instantanous change to ancient population size for pop2
nu2F: Ratio of contempoary to ancient population size pop2
m12: The scaled migration rate from pop 2 (invaded) to pop 1 (refuge)
m21: The scaled migration rate from pop 2 to pop 1
Ts: The time between the split and bottleneck
Tb: The scaled time between the bottleneck and present.
ns = (n1,n2): Size of fs to generate.
pts: Number of points to use in grid for evaluation.
"""
nu1,nu2, nu1B, nu1F, nu2B, nu2F, mri, mir, Ts, theta = params
#RNG: Removed Tb from params list. It’s not a parameter you’re fitting in this function.
n1,n2 = ns
# RNG: Need to specify theta0 throughout function
# Define the grid we'll use
xx = yy = dadi.Numerics.default_grid(pts)
# phi for the equilibrium ancestral population
phi = dadi.PhiManip.phi_1D(xx, theta0=theta)
# phi for the ancestral population split
phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
# phi for the migration between pop 1 and pop 2 post split
phi = dadi.Integration.two_pops(phi, xx, Ts, nu1, nu2, m12=mri, m21=mir, theta0=theta)
# Define a function to describe the bottleneck then exponential growth in populations 1 and 2
# Use lambda for this function
# RNG: Changed Tb = to T = . The error message you were getting was just about the name of that argument.
Tb = (12*2*7.6e-09*7158678)/theta
nu1_func = lambda t: nu1B*(nu1F/nu1B)**(t/Tb)
nu2_func = lambda t: nu2B*(nu2F/nu2B)**(t/Tb)
phi = dadi.Integration.two_pops(phi, xx, T = Tb, nu1=nu1_func, nu2=nu2_func,
m12=mri, m21=mir, theta0 = theta)
# Finally, calculate the spectrum.
sfs = dadi.Spectrum.from_phi(phi, (n1,n2), (xx,yy))
return sfs