def parse_slim_organization(lines, mtypes, elements, mutrate): start = lines.index("#CHROMOSOME ORGANIZATION") end = get_next_block_starts(lines, start) #need to get sum of all weights per mutation type ttlweights = {} for key1 in elements: ttlweights[key1] = 0. for key2 in elements[key1]: ttlweights[key1] = ttlweights[key1] + elements[key1][key2] nregions = [] sregions = [] mun = 0 mus = 0 for i in lines[start + 1:start + 1 + end]: t = i.split() ebeg = float(t[1]) eend = float(t[2]) for key in elements[t[0]]: mt = mtypes[key] weight = elements[t[0]][key] / ttlweights[t[0]] ##In this block, we halve s or mean s, ##and double h, to convert from SLiM's ##fitness model of 1,1+hs,1+s to the ##1,1+sh,1+2s used here. if mt[1] == 'f': if mt[2] == 0.: #is a neutral mutation mun = mun + mutrate * weight * (eend - ebeg + 1.) nregions.append( fwdpy.Region(ebeg - 1., eend, mutrate * weight)) else: mus = mus + mutrate * weight * (eend - ebeg + 1.) sregions.append( fwdpy.ConstantS(ebeg - 1., eend, mutrate * weight, 0.5 * mt[2], 2 * mt[0])) elif mt[1] == 'e': mus = mus + mutrate * weight * (eend - ebeg + 1.) sregions.append( fwdpy.ExpS(ebeg - 1., eend, mutrate * weight, 0.5 * mt[2], 2 * mt[0])) elif mt[1] == 'g': mus = mus + mutrate * weight * (eend - ebeg + 1.) sregions.append( fwdpy.GammaS(ebeg - 1., eend, mutrate * weight, 0.5 * mt[2], mt[3], 2 * mt[0])) else: raise RuntimeError("invalid DFE encountered") return { 'nregions': nregions, 'sregions': sregions, 'mu_neutral': mun, 'mu_selected': mus }
import unittest import fwdpy import numpy as np nregions = [fwdpy.Region(0, 1, 1), fwdpy.Region(2, 3, 1)] sregions = [fwdpy.ExpS(1, 2, 1, -0.1), fwdpy.ExpS(1, 2, 0.01, 0.001)] rregions = [fwdpy.Region(0, 3, 1)] rng = fwdpy.GSLrng(100) N = 1000 NGENS = 100 popsizes = np.array([N], dtype=np.uint32) popsizes = np.tile(popsizes, NGENS) pops = fwdpy.evolve_regions(rng, 1, N, popsizes[0:], 0.001, 0.0001, 0.001, nregions, sregions, rregions) #The sum of the gamete counts must be 2*(deme size): #mpops = fwdpy.evolve_regions_split(rng,pops,popsizes[0:],popsizes[0:],0.001,0.0001,0.001,nregions,sregions,rregions,[0]*2) class test_singlepop_views(unittest.TestCase): def testNumGametes(self): gams = fwdpy.view_gametes(pops[0]) nsingle = 0 for i in gams: nsingle += i['n'] self.assertEqual(nsingle, 2000) def testDipsize(self): dips_single = fwdpy.view_diploids(pops[0], [0, 1, 2]) self.assertEqual(len(dips_single), 3)
#The next three lines process and compile our custom fitness module: import pyximport pyximport.install() import test_fwdpy_extensions.test_custom_fitness as tfp #import fwdpy and numpy as usual import fwdpy as fp import numpy as np rng = fp.GSLrng(101) rngs = fp.GSLrng(202) p = fp.SpopVec(3, 1000) s = fp.NothingSampler(len(p)) n = np.array([1000] * 1000, dtype=np.uint32) nr = [fp.Region(0, 1, 1)] sr = [fp.ExpS(0, 1, 1, 0.1)] #Now, let's do some evolution with our 'custom' fitness functions: fitness = tfp.AdditiveFitnessTesting() fp.evolve_regions_sampler_fitness(rng, p, s, fitness, n, 0.001, 0.001, 0.001, nr, sr, nr, 1) fitness = tfp.AaOnlyTesting() fp.evolve_regions_sampler_fitness(rng, p, s, fitness, n, 0.001, 0.001, 0.001, nr, sr, nr, 1) fitness = tfp.GBRFitness() fp.evolve_regions_sampler_fitness(rng, p, s, fitness, n, 0.001, 0.001, 0.001, nr, sr, nr, 1)
import time # In[26]: ##Info dt = datetime.datetime.now() print("This example was processed using ", fp.pkg_version(), "on", dt.month, "/", dt.day, "/", dt.year) print("The dependency versions are", fp.pkg_dependencies()) # In[27]: #set up our sim rng = fp.GSLrng(101) nregions = [fp.Region(0, 1, 1), fp.Region(2, 3, 1)] sregions = [fp.ExpS(1, 2, 1, -0.1), fp.ExpS(1, 2, 0.1, 0.001)] rregions = [fp.Region(0, 3, 1)] popsizes = np.array([1000] * 10000, dtype=np.uint32) # In[28]: #Run the sim pops = fp.evolve_regions(rng, 4, 1000, popsizes[0:], 0.001, 0.0001, 0.001, nregions, sregions, rregions) # In[29]: #Take samples from the simulation samples = [fp.get_samples(rng, i, 20) for i in pops] # ## Calculating sliding windows