예제 #1
0
 def testBadStdDev(self):
     with self.assertRaises(ValueError):
         fwdpy11.GaussianS(0, 1, 1, np.nan)
     with self.assertRaises(ValueError):
         fwdpy11.GaussianS(0, 1, 1, 0)
     with self.assertRaises(ValueError):
         fwdpy11.GaussianS(0, 1, 1, -1e-3)
예제 #2
0
    def setUp(self):
        import fwdpy11
        import numpy as np
        self.N = 1000
        self.demography = np.array([self.N] * self.N, dtype=np.uint32)
        self.rho = 1.
        self.theta = 100.
        self.nreps = 500
        self.mu = self.theta / (4 * self.N)
        self.r = self.rho / (4 * self.N)

        self.GSS = fwdpy11.GSS(VS=1, opt=0)
        a = fwdpy11.Additive(2.0, self.GSS)
        self.p = {
            'nregions': [],
            'sregions': [fwdpy11.GaussianS(0, 1, 1, 0.25)],
            'recregions': [fwdpy11.Region(0, 1, 1)],
            'rates': (0.0, 0.025, self.r),
            'gvalue': a,
            'prune_selected': False,
            'demography': self.demography
        }
        self.params = fwdpy11.ModelParams(**self.p)
        self.rng = fwdpy11.GSLrng(101 * 45 * 110 * 210)
        self.pop = fwdpy11.DiploidPopulation(self.N, 1.0)
        fwdpy11.evolvets(self.rng, self.pop, self.params, 100)
예제 #3
0
def runsim(args):
    popsizes = np.array([args.popsize] * 20 * args.popsize, dtype=np.int32)
    pop = fwdpy11.DiploidPopulation(args.popsize, 1.0)
    sregions = [fwdpy11.GaussianS(0, 1, 1, args.sigma)]
    recregions = [fwdpy11.PoissonInterval(0, 1, args.recrate)]

    optima = fwdpy11.GSSmo([(0, 0, args.VS),
                            (10 * args.popsize, args.opt, args.VS)])
    p = {
        'nregions': [],  # No neutral mutations -- add them later!
        'gvalue': fwdpy11.Additive(2.0, optima),
        'sregions': sregions,
        'recregions': recregions,
        'rates': (0.0, args.mu, None),
        # Keep mutations at frequency 1 in the pop if they affect fitness.
        'prune_selected': False,
        'demography': np.array(popsizes, dtype=np.uint32)
    }
    params = fwdpy11.ModelParams(**p)
    rng = fwdpy11.GSLrng(args.seed)
    s = Recorder(args.popsize)
    fwdpy11.evolvets(rng,
                     pop,
                     params,
                     100,
                     s,
                     suppress_table_indexing=True,
                     track_mutation_counts=True)
    return pop
예제 #4
0
def runsim(args):
    popsizes = np.array([args.popsize] * 20 * args.popsize, dtype=np.int32)
    locus_boundaries = [(i, i + 11) for i in range(0, 10 * 11, 11)]
    sregions = [fwdpy11.GaussianS(i[0] + 5, i[0] + 6, 1, args.sigma)
                for i in locus_boundaries]
    recregions = [fwdpy11.PoissonInterval(
        *i, args.rho / (4 * args.popsize)) for i in locus_boundaries]
    recregions.extend([fwdpy11.BinomialPoint(i[1], 0.5)
                       for i in locus_boundaries[:-1]])
    pop = fwdpy11.DiploidPopulation(args.popsize, locus_boundaries[-1][1])

    optima = fwdpy11.GSSmo(
        [(0, 0, args.VS), (10 * args.popsize, args.opt, args.VS)])
    p = {'nregions': [],  # No neutral mutations -- add them later!
         'gvalue': fwdpy11.Additive(2.0, optima),
         'sregions': sregions,
         'recregions': recregions,
         'rates': (0.0, args.mu, None),
         # Keep mutations at frequency 1 in the pop if they affect fitness.
         'prune_selected': False,
         'demography':  np.array(popsizes, dtype=np.uint32)
         }
    params = fwdpy11.ModelParams(**p)
    rng = fwdpy11.GSLrng(args.seed)
    s = Recorder(args.popsize)
    fwdpy11.evolvets(rng, pop, params, 100, s,
                     suppress_table_indexing=True,
                     track_mutation_counts=True)
    return pop
예제 #5
0
 def setUp(self):
     self.N = 1000
     self.demography = np.array([self.N] * 100, dtype=np.uint32)
     self.rho = 1.
     self.theta = 100.
     self.nreps = 500
     self.mu = self.theta / (4 * self.N)
     self.r = self.rho / (4 * self.N)
     self.GSS = fwdpy11.GSS(VS=1, opt=0)
     a = fwdpy11.Additive(2.0, self.GSS)
     self.p = {
         'nregions': [],
         'sregions': [fwdpy11.GaussianS(0, 1, 1, 0.25)],
         'recregions': [fwdpy11.Region(0, 1, 1)],
         'rates': (0.0, 0.025, self.r),
         'gvalue': a,
         'prune_selected': False,
         'demography': self.demography
     }
     self.params = fwdpy11.ModelParams(**self.p)
     self.rng = fwdpy11.GSLrng(101 * 45 * 110 * 210)
     self.pop = fwdpy11.DiploidPopulation(self.N, 1.0)
     self.recorder = fwdpy11.RandomAncientSamples(
         seed=42, samplesize=10, timepoints=[i for i in range(1, 101)])
     fwdpy11.evolvets(self.rng, self.pop, self.params, 100, self.recorder)
예제 #6
0
    def setUpClass(self):
        # TODO add neutral variants
        self.N = 1000
        self.demography = np.array([self.N] * self.N, dtype=np.uint32)
        self.rho = 1.
        self.theta = 100.
        self.nreps = 500
        self.mu = self.theta / (4 * self.N)
        self.r = self.rho / (4 * self.N)

        self.GSS = fwdpy11.GSS(VS=1, opt=0)
        a = fwdpy11.Additive(2.0, self.GSS)
        self.p = {
            'nregions': [],
            'sregions': [fwdpy11.GaussianS(0, 1, 1, 0.25)],
            'recregions': [fwdpy11.Region(0, 1, 1)],
            'rates': (0.0, 0.025, self.r),
            'gvalue': a,
            'prune_selected': False,
            'demography': self.demography
        }
        self.params = fwdpy11.ModelParams(**self.p)
        self.rng = fwdpy11.GSLrng(101 * 45 * 110 * 210)
        self.pop = fwdpy11.DiploidPopulation(self.N, 1.0)
        self.all_samples = [i for i in range(2 * self.N)]
        fwdpy11.evolvets(self.rng, self.pop, self.params, 100)
        self.dm = fwdpy11.data_matrix_from_tables(self.pop.tables,
                                                  self.all_samples, True, True)
        self.neutral = np.array(self.dm.neutral)
        self.npos = np.array(self.dm.neutral.positions)
        self.selected = np.array(self.dm.selected)
        self.spos = np.array(self.dm.selected.positions)
예제 #7
0
def set_up_quant_trait_model():
    # 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)

    GSSmo = fwdpy11.GSSmo([(0, 0, 1), (N, 1, 1)])
    a = fwdpy11.Additive(2.0, GSSmo)
    p = {
        'nregions': [],
        'sregions': [fwdpy11.GaussianS(0, 1, 1, 0.25)],
        'recregions': [fwdpy11.Region(0, 1, 1)],
        'rates': (0.0, 0.025, r),
        'gvalue': a,
        'prune_selected': False,
        'demography': demography
    }
    params = fwdpy11.ModelParams(**p)
    rng = fwdpy11.GSLrng(101 * 45 * 110 * 210)
    pop = fwdpy11.DiploidPopulation(N, 1.0)
    return params, rng, pop
예제 #8
0
    def setUpClass(self):
        class CountSamplesPerTimePoint(object):
            def __init__(self):
                self.sample_timepoints = []
                self.sample_sizes = []
                self.timepoint_seen = {}

            def __call__(self, pop):
                assert len(pop.tables.preserved_nodes)//2 ==  \
                    len(pop.ancient_sample_metadata)
                # Get the most recent ancient samples
                # and record their number.  We do this
                # by a "brute-force" approach
                for t, n, m in pop.sample_timepoints(False):
                    if t not in self.timepoint_seen:
                        self.timepoint_seen[t] = 1
                    else:
                        self.timepoint_seen[t] += 1
                    if t not in self.sample_timepoints:
                        self.sample_timepoints.append(t)
                        self.sample_sizes.append(len(n) // 2)

                    # simplify to each time point
                    tables, idmap = fwdpy11.simplify_tables(pop.tables, n)
                    for ni in n:
                        assert idmap[ni] != fwdpy11.NULL_NODE
                        assert tables.nodes[idmap[ni]].time == t

        self.N = 1000
        self.demography = np.array([self.N] * 101, dtype=np.uint32)
        self.rho = 1.
        self.r = self.rho / (4 * self.N)

        self.GSS = fwdpy11.GSS(VS=1, opt=0)
        a = fwdpy11.Additive(2.0, self.GSS)
        self.p = {
            'nregions': [],
            'sregions': [fwdpy11.GaussianS(0, 1, 1, 0.25)],
            'recregions': [fwdpy11.Region(0, 1, 1)],
            'rates': (0.0, 0.025, self.r),
            'gvalue': a,
            'prune_selected': False,
            'demography': self.demography
        }
        self.params = fwdpy11.ModelParams(**self.p)
        self.rng = fwdpy11.GSLrng(101 * 45 * 110 * 210)
        self.pop = fwdpy11.DiploidPopulation(self.N, 1.0)
        self.all_samples = [i for i in range(2 * self.N)]
        self.ancient_sample_recorder = \
            fwdpy11.RandomAncientSamples(seed=42,
                                         samplesize=10,
                                         timepoints=[i for i in range(1, 101)])
        self.resetter = CountSamplesPerTimePoint()
        fwdpy11.evolvets(self.rng,
                         self.pop,
                         self.params,
                         5,
                         recorder=self.ancient_sample_recorder,
                         post_simplification_recorder=self.resetter)
예제 #9
0
def run_replicate(argtuple):
    args, repid, repseed = argtuple
    rng = fp11.GSLrng(repseed)
    NANC = args.N
    locus_boundaries = [(float(i + i * 11), float(i + i * 11 + 11))
                        for i in range(args.nloci)]
    nregions = [[
        fp11.Region(j[0], j[1], args.theta / (4. * float(NANC)), coupled=True)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]
    recregions = [[
        fp11.Region(j[0], j[1], args.rho / (4. * float(NANC)), coupled=True)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]
    sregions = [[
        fp11.GaussianS(j[0] + 5.,
                       j[0] + 6.,
                       args.mu,
                       args.sigmu,
                       coupled=False)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]
    interlocus_rec = fp11ml.binomial_rec(rng, [0.5] * (args.nloci - 1))
    nlist = np.array([NANC] * 20 * NANC, dtype=np.uint32)
    env = [(0, 0, 1), (10 * NANC, args.opt, 1)]
    pdict = {
        'nregions':
        nregions,
        'sregions':
        sregions,
        'recregions':
        recregions,
        'demography':
        nlist,
        'interlocus':
        interlocus_rec,
        'agg':
        fp11ml.AggAddTrait(),
        'gvalue':
        fp11ml.MultiLocusGeneticValue([fp11tv.SlocusAdditiveTrait(2.0)] *
                                      args.nloci),
        'trait2w':
        fp11qt.GSSmo(env),
        'mutrates_s': [args.mu / float(args.nloci)] * args.nloci,
        'mutrates_n': [10 * args.theta / float(4 * NANC)] * args.nloci,
        'recrates': [10 * args.rho / float(4 * NANC)] * args.nloci,
        'prune_selected':
        False
    }
    params = fp11.model_params.MlocusParamsQ(**pdict)
    ofilename = args.stub + '.rep' + str(repid) + '.pickle.lzma'
    recorder = Pickler(repid, NANC, ofilename)
    pop = fp11.MlocusPop(NANC, args.nloci, locus_boundaries)
    fp11qt.evolve(rng, pop, params, recorder)
    #Make sure last gen got pickled!
    if recorder.last_gen_recorded != pop.generation:
        with open(ofilename, "ab") as f:
            pickle.dump((repid, pop), f, -1)
    return ofilename
예제 #10
0
def run_replicate(argtuple):
    args, repid, repseed = argtuple
    rng = fp11.GSLrng(repseed)
    NANC = args.N
    locus_boundaries = [(float(i + i * 11), float(i + i * 11 + 11))
                        for i in range(args.nloci)]
    nregions = [[
        fp11.Region(j[0], j[1], args.theta / (4. * float(NANC)), coupled=True)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]
    recregions = [[
        fp11.Region(j[0], j[1], args.rho / (4. * float(NANC)), coupled=True)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]

    # Get the variance in effect sizes
    sigmu = args.sigmu  # default
    if args.plarge is not None:
        ghat = gamma_hat(1.0, args.mu)
        F = generate_gaussian_function_to_minimize(ghat, args.plarge)
        sigmu = get_gaussian_sigma(F)

    sregions = [[
        fp11.GaussianS(j[0] + 5., j[0] + 6., args.mu, sigmu, coupled=False)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]
    interlocus_rec = fp11ml.binomial_rec(rng, [0.5] * (args.nloci - 1))
    nlist = np.array([NANC] * 20 * NANC, dtype=np.uint32)
    env = [(0, 0, 1), (10 * NANC, args.opt, args.vsopt)]
    pdict = {
        'nregions':
        nregions,
        'sregions':
        sregions,
        'recregions':
        recregions,
        'demography':
        nlist,
        'interlocus':
        interlocus_rec,
        'agg':
        fp11ml.AggAddTrait(),
        'gvalue':
        fp11ml.MultiLocusGeneticValue([fp11tv.SlocusAdditiveTrait(2.0)] *
                                      args.nloci),
        'trait2w':
        fp11qt.GSSmo(env),
        'mutrates_s': [args.mu / float(args.nloci)] * args.nloci,
        'mutrates_n': [10 * args.theta / float(4 * NANC)] * args.nloci,
        'recrates': [10 * args.rho / float(4 * NANC)] * args.nloci,
        'prune_selected':
        False
    }
    params = fp11.model_params.MlocusParamsQ(**pdict)
    recorder = Recorder(repid, NANC, args.nsam)
    pop = fp11.MlocusPop(NANC, args.nloci, locus_boundaries)
    fp11qt.evolve(rng, pop, params, recorder)
    return recorder
예제 #11
0
def run_replicate(argtuple):
    args, repid, repseed = argtuple
    rng = fp11.GSLrng(repseed)
    NANC = args.N
    locus_boundaries = [(float(i + i * 11), float(i + i * 11 + 11))
                        for i in range(args.nloci)]
    nregions = [[
        fp11.Region(j[0], j[1], args.theta / (4. * float(NANC)), coupled=True)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]
    recregions = [[
        fp11.Region(j[0], j[1], args.rho / (4. * float(NANC)), coupled=True)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]
    sregions = [[
        fp11.GaussianS(j[0] + 5.,
                       j[0] + 6.,
                       args.mu,
                       args.sigmu,
                       coupled=False)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]
    interlocus_rec = fp11ml.binomial_rec([0.5] * (args.nloci - 1))
    nlist = np.array([NANC] * args.simlen * NANC, dtype=np.uint32)

    # the tuples are (time, z_o, VS)
    env = [(0, 0, 1), (10 * NANC, args.opt, 1)]
    gv = fwdpy11.genetic_values.MlocusAdditive(
        2.0, fwdpy11.genetic_values.GSSmo(env))
    mutrates_s = [args.mu / float(args.nloci)] * args.nloci
    mutrates_n = [10 * args.theta / float(4 * NANC)] * args.nloci
    recrates = [10 * args.rho / float(4 * NANC)] * args.nloci
    pdict = {
        'nregions': nregions,
        'sregions': sregions,
        'recregions': recregions,
        'demography': nlist,
        'interlocus_rec': interlocus_rec,
        'rates': (mutrates_n, mutrates_s, recrates),
        'gvalue': gv,
        'prune_selected': False
    }
    params = fp11.model_params.ModelParams(**pdict)
    recorder = HapStatSampler(repid, NANC, args.nsam)
    # sched prevents the TBB library
    # from over-subscribing a node
    # when running many sims in parallel
    sched = None
    if HAVESCHED is True:
        sched = Scheduler(1)
    pop = fp11.MlocusPop(NANC, locus_boundaries)
    assert pop.nloci == len(locus_boundaries), "nloci != len(locus_boundaries)"
    fwdpy11.wright_fisher.evolve(rng, pop, params, recorder)
    return repid, recorder.data, pop
def runsim(repargs):
    filename, popu, theta, rho, mutrate, seed ,repid = repargs
    pop = fp11.SlocusPop(popu)
    rng = fp11.GSLrng(seed)
    optima = fwdpy11.genetic_values.GSSmo([(0, 0, 1), (10*popu, 1, 1)])
    gv = fwdpy11.genetic_values.SlocusAdditive(2.0, optima)
    p = {'nregions': [fp11.Region(0,11,1.0)],
         'sregions': [fp11.GaussianS(5, 6, 1, 0.25)],
         'recregions': [fp11.Region(0, 11, 1)],
         # No neutral mutations in this example
         'rates':(theta/float(4*popu), mutrate, rho/float(4*popu)),
         'gvalue': gv,
         'prune_selected': False,
         'demography': np.array([popu]*20*popu, dtype=np.uint32)
         }
    timepoints = pop.generation
    sampler = Traj(timepoints, pop, repargs)
    params = fp11.model_params.ModelParams(**p)
    fwdpy11.wright_fisher.evolve(rng, pop, params, sampler)
    return repid, pop, sampler.data 
예제 #13
0
def runsim(args):
    mutrate, seed, repid = args
    rng = fp11.GSLrng(seed)
    t2f = fp11qt.GSSmo([(0, 0, 1), (10 * N, 1, 1)])
    pop = fp11.SlocusPop(N)
    p = {
        'nregions': [fp11.Region(0, 11, 1.0)],  # nregions must be a region
        'sregions': [fp11.GaussianS(5, 6, 1, 0.25)],
        'recregions': [fp11.Region(0, 11, 1)],
        'mutrate_n': THETA / float(4 * N),
        'mutrate_s': mutrate,
        'recrate': RHO / float(4 * N),
        'demography': np.array([N] * (10 * N + 100), dtype=np.uint32),
        'gvalue': fp11tv.SlocusAdditiveTrait(2.0),
        'trait_to_fitness': t2f,
        'prune_selected': False
    }

    params = fp11.model_params.SlocusParamsQ(**p)

    fp11qt.evolve(rng, pop, params)
    samples = take_sample(rng, pop)
    return repid, pop, samples
예제 #14
0
def runsim(args):
    """
    Run the simulation and deliver output to files.
    """
    pop = fwdpy11.DiploidPopulation(args.popsize, GENOME_LENGTH)

    rng = fwdpy11.GSLrng(args.seed)

    GSSmo = fwdpy11.GSSmo([(0, 0, args.VS),
                           (10 * args.popsize, args.opt, args.VS)])

    popsizes = [args.popsize
                ] * (10 * args.popsize + int(args.time * float(args.popsize)))
    p = {
        'nregions': [],  # No neutral mutations -- add them later!
        'gvalue': fwdpy11.Additive(2.0, GSSmo),
        'sregions': [fwdpy11.GaussianS(0, GENOME_LENGTH, 1, args.sigma)],
        'recregions': [fwdpy11.Region(0, GENOME_LENGTH, 1)],
        'rates': (0.0, args.mu, args.rho / float(4 * args.popsize)),
        # Keep mutations at frequency 1 in the pop if they affect fitness.
        'prune_selected': False,
        'demography': np.array(popsizes, dtype=np.uint32)
    }
    params = fwdpy11.ModelParams(**p)

    r = Recorder(args.record, args.preserve, args.num_ind)
    fwdpy11.evolvets(rng, pop, params, 100, r, suppress_table_indexing=True)

    with lzma.open(args.filename, 'wb') as f:
        pickle.dump(pop, f)

    if args.statfile is not None:
        stats = pd.DataFrame(r.data, columns=DATA._fields)
        # Write the statistics to an sqlite3 database,
        # which can be processed in R via dplyr.
        conn = sqlite3.connect(args.statfile)
        stats.to_sql('data', conn)
예제 #15
0
    def testQtraitSim(self):
        N = 1000
        demography = np.array([N] * 10 * N, dtype=np.uint32)
        rho = 1.
        r = rho / (4 * N)

        GSS = fwdpy11.GSS(VS=1, opt=1)
        a = fwdpy11.Additive(2.0, GSS)
        p = {
            'nregions': [],
            'sregions': [fwdpy11.GaussianS(0, 1, 1, 0.25)],
            'recregions': [fwdpy11.Region(0, 1, 1)],
            'rates': (0.0, 0.005, r),
            'gvalue': a,
            'prune_selected': False,
            '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), len(pop.fixations))

        # 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))
예제 #16
0
    def testQtraitSim(self):
        N = 1000
        demography = np.array([N] * 10 * N, dtype=np.uint32)
        rho = 1.
        r = rho / (4 * N)

        GSS = fwdpy11.GSS(VS=1, opt=1)
        a = fwdpy11.Additive(2.0, GSS)
        p = {
            'nregions': [],
            'sregions': [fwdpy11.GaussianS(0, 1, 1, 0.25)],
            'recregions': [fwdpy11.PoissonInterval(0, 1, r)],
            'rates': (0.0, 0.005, None),
            'gvalue': a,
            'prune_selected': False,
            'demography': demography
        }
        params = fwdpy11.ModelParams(**p)
        rng = fwdpy11.GSLrng(101 * 45 * 110 * 210)
        pop = fwdpy11.DiploidPopulation(N, 1.0)

        class Recorder(object):
            """ Records entire pop every 100 generations """
            def __call__(self, pop, recorder):
                if pop.generation % 100 == 0.0:
                    recorder.assign(np.arange(pop.N, dtype=np.int32))

        r = Recorder()
        fwdpy11.evolvets(rng, pop, params, 100, r)

        ancient_sample_metadata = np.array(pop.ancient_sample_metadata,
                                           copy=False)
        alive_sample_metadata = np.array(pop.diploid_metadata, copy=False)
        metadata = np.hstack((ancient_sample_metadata, alive_sample_metadata))

        nodes = np.array(pop.tables.nodes, copy=False)
        metadata_nodes = metadata['nodes'].flatten()
        metadata_node_times = nodes['time'][metadata_nodes]
        metadata_record_times = nodes['time'][metadata['nodes'][:, 0]]

        genetic_trait_values_from_sim = []
        genetic_values_from_ts = []
        for u in np.unique(metadata_node_times):
            samples_at_time_u = metadata_nodes[np.where(
                metadata_node_times == u)]
            vi = fwdpy11.VariantIterator(pop.tables, samples_at_time_u)
            sum_esizes = np.zeros(len(samples_at_time_u))
            for variant in vi:
                g = variant.genotypes
                r = variant.records[0]
                mutant = np.where(g == 1)[0]
                sum_esizes[mutant] += pop.mutations[r.key].s
            ind = int(len(samples_at_time_u) / 2)
            temp_gvalues = np.zeros(ind)
            temp_gvalues += sum_esizes[0::2]
            temp_gvalues += sum_esizes[1::2]
            genetic_values_from_ts.extend(temp_gvalues.tolist())
            genetic_trait_values_from_sim.extend(metadata['g'][np.where(
                metadata_record_times == u)[0]].tolist())

        for i, j in zip(genetic_trait_values_from_sim, genetic_values_from_ts):
            self.assertAlmostEqual(i, j)
import fwdpy11
print(fwdpy11.__file__)
import numpy as np
import FixedCrossoverInterval

N = 1000

pdict = {
    'demography': fwdpy11.DiscreteDemography(),
    'simlen': 100,
    'nregions': [],
    'sregions': [fwdpy11.GaussianS(0, 1, 1, 0.25)],
    'recregions': [FixedCrossoverInterval.FixedCrossoverInterval(0, 1, 2)],
    'rates': (0., 5e-3, None),
    'gvalue': fwdpy11.Additive(2., fwdpy11.GSS(VS=1, opt=1)),
    'prune_selected': False
}

params = fwdpy11.ModelParams(**pdict)

print(params.recregions)

pop = fwdpy11.DiploidPopulation(N, 1.0)

rng = fwdpy11.GSLrng(42)

fwdpy11.evolvets(rng, pop, params, 100)
예제 #18
0
def runsim(args):
    args, repid, repseed = args
    rng = fwdpy11.GSLrng(repseed)
    locus_boundaries = [(float(i + i * 11), float(i + i * 11 + 11))
                        for i in range(args.nloci)]
    NANC = args.N
    # In revision, change from using gamma_hat
    # to (2Ngamma^2)/(2VS) >= 100.
    # Keep the variable name to minimize
    # the refactoring...
    # ghat = gamma_hat(1.0, args.mu)
    ghat = np.sqrt(100. / 5000.)
    sregions = None
    if args.gamma is None:
        F = generate_gaussian_function_to_minimize(ghat, args.plarge)
        sigma_gamma = get_gaussian_sigma(F)
        sregions = [[
            fwdpy11.GaussianS(j[0] + 5.,
                              j[0] + 6.,
                              args.mu,
                              sigma_gamma,
                              coupled=False)
        ] for i, j in zip(range(args.nloci), locus_boundaries)]
    else:
        # Get a shape param for the gamma
        res = scipy.optimize.minimize_scalar(minimize_gamma_cdf,
                                             bounds=(0, 100),
                                             method='bounded',
                                             args=(args.gamma, ghat,
                                                   args.plarge))
        sregions = [[
            fwdpy11.GammaS(j[0] + 5.,
                           j[0] + 6.,
                           args.mu,
                           -1.0 * res.x,
                           args.gamma,
                           coupled=False),
            fwdpy11.GammaS(j[0] + 5.,
                           j[0] + 6.,
                           args.mu,
                           res.x,
                           args.gamma,
                           coupled=False)
        ] for i, j in zip(range(args.nloci), locus_boundaries)]
    print(sregions)
    nregions = [[
        fwdpy11.Region(j[0],
                       j[1],
                       args.theta / (4. * float(NANC)),
                       coupled=True)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]
    recregions = [[
        fwdpy11.Region(j[0], j[1], args.rho / (4. * float(NANC)), coupled=True)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]
    interlocus_rec = fwdpy11.multilocus.binomial_rec([0.5] * (args.nloci - 1))
    nlist = np.array([NANC] * 15 * NANC, dtype=np.uint32)
    env = [(0, 0, 1), (10 * NANC, args.opt, 1)]
    mutrates_s = [args.mu / float(args.nloci)] * args.nloci
    mutrates_n = [10 * args.theta / float(4 * NANC)] * args.nloci
    recrates = [10 * args.rho / float(4 * NANC)] * args.nloci
    gssmo = fwdpy11.genetic_values.GSSmo(env)
    gv = fwdpy11.genetic_values.MlocusAdditive(2.0, gssmo)
    pdict = {
        'nregions': nregions,
        'sregions': sregions,
        'recregions': recregions,
        'demography': nlist,
        'interlocus_rec': interlocus_rec,
        'gvalue': gv,
        'rates': (mutrates_n, mutrates_s, recrates),
        'prune_selected': False
    }
    params = fwdpy11.model_params.ModelParams(**pdict)
    pop = fwdpy11.MlocusPop(args.nloci, locus_boundaries)
    sampler = Sampler(rng, args.nsam)
    fwdpy11.wright_fisher.evolve(rng, pop, params, sampler)
    sampler.rng = None  # Cannot be pickled and is not needed
    return repid, pop, sampler
def run_replicate(argtuple):
    args, repid, repseed = argtuple
    rng = fp11.GSLrng(repseed)
    NANC = args.N
    locus_boundaries = [(float(i + i * 11), float(i + i * 11 + 11))
                        for i in range(args.nloci)]
    #nregions=[[fp11.Region(j[0],j[1],args.theta/(4.*float(NANC)),coupled=True)] for i,j in zip(range(args.nloci),locus_boundaries)]
    nregions = [[]] * args.nloci
    #Put the neutral variants in the selected regions
    nregions[0] = [
        fp11.Region(locus_boundaries[0][0] + 5.0, locus_boundaries[0][0] + 6.0,
                    1)
    ]
    #Put the neutral variants in middle
    nregions[int(args.nloci / 2)] = [
        fp11.Region(locus_boundaries[int(args.nloci / 2)][0] + 5.,
                    locus_boundaries[int(args.nloci / 2)][0] + 6., 1.0)
    ]
    #Put the neutral variants at far rigth end.
    nregions[args.nloci - 1] = [
        fp11.Region(locus_boundaries[args.nloci - 1][1] - 1.0,
                    locus_boundaries[args.nloci - 1][1], 1.0)
    ]
    recregions = [[
        fp11.Region(j[0], j[1], args.rho / (4. * float(NANC)), coupled=True)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]
    sregions = [[
        fp11.GaussianS(j[0] + 5.,
                       j[0] + 6.,
                       args.mu,
                       args.sigmu,
                       coupled=False)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]
    #The middle region has no sregions:
    sregions[int(args.nloci / 2)] = []
    mutrates_s = [args.mu / float(args.nloci - 1)] * args.nloci
    #No selected mutations in the middle locus
    mutrates_s[int(args.nloci / 2)] = 0.0
    mutrates_n = [0.0] * args.nloci
    for i in [0, int(args.nloci / 2), args.nloci - 1]:
        #The next line keeps scaling, etc., same as in other sims.
        mutrates_n[i] = (10.0 / 11.0) * (args.theta / float(4 * NANC))
    interlocus_rec = fp11ml.binomial_rec(rng, [0.5] * (args.nloci - 1))
    nlist = np.array([NANC] * 20 * NANC, dtype=np.uint32)
    env = [(0, 0, 1), (10 * NANC, args.opt, 1)]
    #print(mutrates_s)
    #print(mutrates_n)
    #print([10*args.rho/float(4*NANC)]*args.nloci)
    #for i in enumerate(nregions):
    #    if len(nregions[i[0]])>0:
    #        print("the type is",type(i[1]))
    #        print(i,i[1][0].b,i[1][0].e,i[1][0].w)
    #for i in enumerate(sregions):
    #    if len(sregions[i[0]])>0:
    #        print(i,i[1][0].b,i[1][0].e,i[1][0].w)
    #for i in enumerate(recregions):
    #    print(i,i[1][0].b,i[1][0].e)
    #sys.exit(0)
    pdict = {
        'nregions':
        nregions,
        'sregions':
        sregions,
        'recregions':
        recregions,
        'demography':
        nlist,
        'interlocus':
        interlocus_rec,
        'agg':
        fp11ml.AggAddTrait(),
        'gvalue':
        fp11ml.MultiLocusGeneticValue([fp11tv.SlocusAdditiveTrait(2.0)] *
                                      args.nloci),
        'trait2w':
        fp11qt.GSSmo(env),
        'mutrates_s':
        mutrates_s,
        'mutrates_n':
        mutrates_n,
        'recrates': [10 * args.rho / float(4 * NANC)] * args.nloci,
    }
    params = fp11.model_params.MlocusParamsQ(**pdict)
    ofilename = args.stub + '.rep' + str(repid) + '.pickle.lzma'
    recorder = Pickler(repid, NANC, ofilename)
    pop = fp11.MlocusPop(NANC, args.nloci, locus_boundaries)
    fp11qt.evolve(rng, pop, params, recorder)
    #Make sure last gen got pickled!
    if recorder.last_gen_recorded != pop.generation:
        with open(ofilename, "ab") as f:
            pickle.dump((repid, pop), f, -1)
    return ofilename
def run_replicate(argtuple):
    args, repid, repseed = argtuple
    rng = fp11.GSLrng(repseed)
    NANC = 7310
    locus_boundaries = [(float(i + i * 11), float(i + i * 11 + 11))
                        for i in range(args.nloci)]
    nregions = [[
        fp11.Region(j[0], j[1], args.theta / (4. * float(NANC)), coupled=True)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]
    recregions = [[
        fp11.Region(j[0], j[1], args.rho / (4. * float(NANC)), coupled=True)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]
    sregions = [[
        fp11.GaussianS(j[0] + 5.,
                       j[0] + 6.,
                       args.mu,
                       args.sigmu,
                       coupled=False)
    ] for i, j in zip(range(args.nloci), locus_boundaries)]
    interlocus_rec = fp11ml.binomial_rec(rng, [0.5] * (args.nloci - 1))
    nlist = get_nlist()
    env = [(0, 0, 1), (np.argmax(nlist == 1861), args.opt, 1)]
    pdict = {
        'nregions':
        nregions,
        'sregions':
        sregions,
        'recregions':
        recregions,
        'demography':
        nlist,
        'interlocus':
        interlocus_rec,
        'agg':
        fp11ml.AggAddTrait(),
        'gvalue':
        fp11ml.MultiLocusGeneticValue([fp11tv.SlocusAdditiveTrait(2.0)] *
                                      args.nloci),
        'trait2w':
        fp11qt.GSSmo(env),
        'mutrates_s': [args.mu] * args.nloci,
        'mutrates_n':
        [float(args.nloci) * args.theta / float(4 * NANC)] * args.nloci,
        'recrates':
        [float(args.nloci) * args.rho / float(4 * NANC)] * args.nloci,
    }
    #print(pdict['mutrates_n'])
    #for i in sregions:
    #    for j in i:
    #        print(str(j))
    #return
    params = fp11.model_params.MlocusParamsQ(**pdict)
    ofilename = args.stub + '.rep' + str(repid) + '.gz'
    recorder = Pickler(repid, args.nsam, 8 * NANC, np.argmax(nlist == 1861),
                       ofilename)
    pop = fp11.MlocusPop(NANC, args.nloci, locus_boundaries)
    fp11qt.evolve(rng, pop, params, recorder)
    #Make sure last gen got pickled!
    if recorder.last_gen_recorded != pop.generation:
        with open(ofilename, "ab") as f:
            with gzip.open(ofilename, "ab") as f:
                m = get_matrix(pop, args.nsam)
                pickle.dump(m, f, -1)
    pop.clear()
    del pop
    pop = None
    return ofilename
예제 #21
0
 def test_bad_input(self):
     with self.assertRaises(TypeError):
         fwdpy11.RecombinationRegions(1e-3,
                                      [fwdpy11.ExpS(0, 1, 1, -0.2),
                                       fwdpy11.GaussianS(1, 2, 1, 0.25)])
예제 #22
0
 def setUp(self):
     self.r = fwdpy11.GaussianS(BEG, END, WEIGHT,
                                0.5, DOM, COUPLED, LABEL)
예제 #23
0
 def testBadDominance(self):
     with self.assertRaises(ValueError):
         fwdpy11.GaussianS(0, 1, 1, 1e-3, h=np.nan)
#KRT: I changed theta and rho
#to 1100.  Thus, the value
#for each window is 100,
#which is what you will use for discoal.
theta = 1100.0  # float, not int
rho = 1100.0

rng = fp11.GSLrng(args.seed)

for rep in range(args.nreps):
    t2f = fp11qt.GSSmo([(0, 0, 1), (10 * N, 1, 1)])
    pop = fp11.SlocusPop(N)
    p = {
        'nregions': [fp11.Region(0, 11, 1.0)],  #nregions must be a region
        'sregions': [fp11.GaussianS(5, 6, 1, 0.25)],
        'recregions': [fp11.Region(0, 11, 1)],
        'mutrate_n': theta / float(4 * N),
        'mutrate_s':
        args.mutrate,  # Add this as you are using mutrate_n and recrate
        'recrate': rho / float(4 * N),
        # 'rates':(2.5e-4,1e-3,5e-3),  # Do not need this if using mutrate_n and recrate
        # Change to N instead of 10
        'demography': np.array([N] * (10 * N + 100), dtype=np.uint32),
        'gvalue': fp11tv.SlocusAdditiveTrait(2.0),
        'trait_to_fitness': t2f,
        # Do not remove selected fixations
        # from pop during simulation:
        'prune_selected': False
    }