def setUp(self): GN = fwdpy11.GaussianNoise self.w = fwdpy11.Multiplicative(2.0) self.t = fwdpy11.Multiplicative( 2.0, fwdpy11.GSS(0.0, 1.0)) self.tn = fwdpy11.Multiplicative(1.0, fwdpy11.GSS( 0.0, 1.0), GN(mean=0.1, sd=2.0))
def testPopGenSim(self): N = 1000 demography = np.array([N] * 10 * N, dtype=np.uint32) rho = 1. r = rho / (4 * N) a = fwdpy11.Multiplicative(2.0) p = { 'nregions': [], 'sregions': [fwdpy11.ExpS(0, 1, 1, 0.01)], 'recregions': [fwdpy11.Region(0, 1, 1)], 'rates': (0.0, 0.00005, r), 'gvalue': a, 'prune_selected': True, 'demography': demography } params = fwdpy11.ModelParams(**p) rng = fwdpy11.GSLrng(101 * 45 * 110 * 210) pop = fwdpy11.DiploidPopulation(N, 1.0) fwdpy11.evolvets(rng, pop, params, 100, track_mutation_counts=True) mc = fwdpy11.count_mutations(pop.tables, pop.mutations, [i for i in range(2 * pop.N)]) assert len(pop.fixations) > 0, "Test is meaningless without fixations" fixations = np.where(mc == 2 * pop.N)[0] self.assertEqual(len(fixations), 0) # Brute-force calculation of fixations brute_force = np.zeros(len(pop.mutations), dtype=np.int32) for g in pop.haploid_genomes: if g.n > 0: for k in g.smutations: brute_force[k] += g.n self.assertTrue(np.array_equal(brute_force, mc)) self.assertTrue(np.array_equal(brute_force, pop.mcounts))
def runsim(args): rng = fwdpy11.GSLrng(args.seed) pdict = {'gvalue': fwdpy11.Multiplicative(FITNESS_SCALING), 'rates': (0., args.mu, None), 'nregions': [], 'sregions': [fwdpy11.GammaS(0., GENOME_LENGTH, 1.0-args.proportion, args.mean, args.shape, FITNESS_SCALING/2.0, scaling=2*args.popsize, label=1), fwdpy11.ConstantS(0, GENOME_LENGTH, args.proportion, TWONS, FITNESS_SCALING/2.0, label=2, scaling=2*args.popsize)], 'recregions': [fwdpy11.PoissonInterval(0, GENOME_LENGTH, args.recrate)], 'demography': np.array([args.popsize]*SIMLEN*args.popsize, dtype=np.uint32), # This could easily be True for these sims: 'prune_selected': False } params = fwdpy11.ModelParams(**pdict) pop = fwdpy11.DiploidPopulation(args.popsize, GENOME_LENGTH) sampler = fwdpy11.RandomAncientSamples( args.seed, args.popsize, [i for i in range(10*pop.N, SIMLEN*pop.N)]) # With a lot of ancient samples: # 1. RAM use already skyrockets # 2. Simplification slows down # So, we should do it a little less often: fwdpy11.evolvets(rng, pop, params, 1000, sampler, suppress_table_indexing=True) return pop
def build_parameters_dict(args): """ Returns sim params and the final sizes in each deme """ demog, simlen, finalNs = build_demography(args) nregions = [] sregions = [] recregions = [fwdpy11.PoissonInterval(0, 1, args.rho / (4.0 * args.Nref))] rates = (0, 0, None) if args.gamma is not None: sregions = [ fwdpy11.ConstantS(0, 1, 1, args.gamma, args.H, scaling=2 * args.Nref) ] rates = (0, args.theta / (4.0 * args.Nref), None) pdict = { "nregions": nregions, "sregions": sregions, "recregions": recregions, "rates": rates, "gvalue": fwdpy11.Multiplicative(2.0), "demography": demog, "simlen": simlen, "prune_selected": True, } return pdict, simlen, finalNs
def runsim(argtuple): seed = argtuple rng = fwdpy11.GSLrng(seed) pdict = { 'gvalue': fwdpy11.Multiplicative(2.), 'rates': (0., U / 2., R), # The U/2. is from their eqn. 2. 'nregions': [], 'sregions': [ fwdpy11.ConstantS(0, 1. / 3., 1, -0.02, 1.), fwdpy11.ConstantS(2. / 3., 1., 1, -0.02, 1.) ], 'recregions': [fwdpy11.Region(0, 1. / 3., 1), fwdpy11.Region(2. / 3., 1., 1)], 'demography': np.array([N] * 20 * N, dtype=np.uint32) } params = fwdpy11.ModelParams(**pdict) pop = fwdpy11.DiploidPopulation(N, GENOME_LENGTH) fwdpy11.evolvets(rng, pop, params, 100, suppress_table_indexing=True) rdips = np.random.choice(N, NSAM, replace=False) md = np.array(pop.diploid_metadata, copy=False) rdip_nodes = md['nodes'][rdips].flatten() nodes = np.array(pop.tables.nodes, copy=False) # Only visit trees spanning the # mutation-free segment of the genome tv = fwdpy11.TreeIterator(pop.tables, rdip_nodes, begin=1. / 3., end=2. / 3.) plist = np.zeros(len(nodes), dtype=np.int8) sum_pairwise_tmrca = 0 for t in tv: for i in range(len(rdip_nodes) - 1): u = rdip_nodes[i] while u != fwdpy11.NULL_NODE: plist[u] = 1 u = t.parent(u) for j in range(i + 1, len(rdip_nodes)): u = rdip_nodes[j] while u != fwdpy11.NULL_NODE: if plist[u] == 1: sum_pairwise_tmrca += 2 * \ (pop.generation-nodes['time'][u]) u = fwdpy11.NULL_NODE else: u = t.parent(u) plist.fill(0) return 2 * sum_pairwise_tmrca / (len(rdip_nodes) * (len(rdip_nodes) - 1))
def runsim(args, simseed, npseed): dg = demes.load(args.yaml) final_demes = get_final_demes(dg) demog = fwdpy11.discrete_demography.from_demes(dg, burnin=args.burnin) final_deme_ids = sorted([ i for i in demog.metadata["deme_labels"] if demog.metadata["deme_labels"][i] in final_demes ]) initial_sizes = [ demog.metadata["initial_sizes"][i] for i in sorted(demog.metadata["initial_sizes"].keys()) ] recrate = RHO / (4.0 * initial_sizes[0]) pdict = { "nregions": [], "sregions": [], "recregions": [fwdpy11.PoissonInterval(0, 1, recrate)], "gvalue": fwdpy11.Multiplicative(2.0), "rates": (0.0, 0.0, None), "simlen": demog.metadata["total_simulation_length"], "demography": demog, } params = fwdpy11.ModelParams(**pdict) pop = fwdpy11.DiploidPopulation(initial_sizes, 1.0) # FIXME: need seed as input argument to this fxn rng = fwdpy11.GSLrng(simseed) np.random.seed(npseed) fwdpy11.evolvets(rng, pop, params, 100) nmuts = fwdpy11.infinite_sites(rng, pop, THETA / (4.0 * initial_sizes[0])) md = np.array(pop.diploid_metadata, copy=False) sample_nodes = [] for i in final_deme_ids: w = np.where(md["deme"] == i) s = np.random.choice(w[0], args.nsam, replace=False) sample_nodes.append(md["nodes"][s].flatten()) fs = pop.tables.fs(sample_nodes) return fs
def setUp(self): import numpy as np N = 1000 demography = np.array([N] * 10 * N, dtype=np.uint32) rho = 1. r = rho / (4 * N) a = fwdpy11.Multiplicative(2.0) self.p = { 'nregions': [], 'sregions': [fwdpy11.ExpS(0, 1, 1, 0.01)], 'recregions': [fwdpy11.Region(0, 1, 1)], 'rates': (0.0, 0.00005, r), 'gvalue': a, 'demography': demography } self.pop = fwdpy11.DiploidPopulation(N) self.rng = fwdpy11.GSLrng(101 * 45 * 110 * 210)
def mslike(pop, **kwargs): """ Function to establish default parameters for a single-locus simulation for standard pop-gen modeling scenarios. :params pop: An instance of :class:`fwdpy11.DiploidPopulation` :params kwargs: Keyword arguments. """ import fwdpy11 if isinstance(pop, fwdpy11.DiploidPopulation) is False: raise ValueError("incorrect pop type: " + str(type(pop))) defaults = { 'simlen': 10 * pop.N, 'beg': 0.0, 'end': 1.0, 'theta': 100.0, 'pneutral': 1.0, 'rho': 100.0, 'dfe': None } for key, value in kwargs.items(): if key in defaults: defaults[key] = value import numpy as np params = { 'demography': np.array([pop.N] * defaults['simlen'], dtype=np.uint32), 'nregions': [fwdpy11.Region(defaults['beg'], defaults['end'], 1.0)], 'recregions': [fwdpy11.Region(defaults['beg'], defaults['end'], 1.0)], 'rates': ((defaults['pneutral'] * defaults['theta']) / (4.0 * pop.N), ((1.0 - defaults['pneutral']) * defaults['theta']) / (4.0 * pop.N), defaults['rho'] / (4.0 * float(pop.N))), 'gvalue': fwdpy11.Multiplicative(2.0) } if defaults['dfe'] is None: params['sregions'] = [] else: params['sregions'] = [defaults['dfe']] return params
def set_up_standard_pop_gen_model(): """ For this sort of model, when mutations fix, they are removed from the simulation, INCLUDING THE TREE SEQUENCES. The fact of their existence gets recorded in pop.fixations and pop.fixation_times """ # TODO add neutral variants N = 1000 demography = np.array([N] * 10 * N, dtype=np.uint32) rho = 1. # theta = 100. # nreps = 500 # mu = theta/(4*N) r = rho / (4 * N) a = fwdpy11.Multiplicative(2.0) pselected = 1e-3 p = { 'nregions': [], 'sregions': [ fwdpy11.GammaS(0, 1, 1. - pselected, mean=-5, shape=1, scaling=2 * N), fwdpy11.ConstantS(0, 1, pselected, 1000, scaling=2 * N) ], 'recregions': [fwdpy11.Region(0, 1, 1)], 'rates': (0.0, 0.001, r), 'gvalue': a, 'prune_selected': True, 'demography': demography } params = fwdpy11.ModelParams(**p) rng = fwdpy11.GSLrng(666**2) pop = fwdpy11.DiploidPopulation(N, 1.0) return params, rng, pop
import sys import fwdpy11 N = int(sys.argv[1]) seed = int(sys.argv[2]) pdict = { "nregions": [], "sregions": [], "recregions": [], "rates": [0, 0, 0], "gvalue": fwdpy11.Multiplicative(2.0), "simlen": 5 * N, } mp = fwdpy11.ModelParams(**pdict) pop = fwdpy11.DiploidPopulation(N, 1.0) rng = fwdpy11.GSLrng(seed) fwdpy11.evolvets(rng, pop, mp, 100, suppress_table_indexing=True) ts = pop.dump_tables_to_tskit() # Copies all data from C++ to Python ts.dump("fwdpy11.trees")