コード例 #1
0
def evolve_track_wrapper(popsize=1000,
                         rho=10000.0,
                         mu=1e-2,
                         seed=42,
                         gc_interval=10,
                         dfe=fwdpy11.ConstantS(0, 1, 1, -0.025, 1.0)):
    if isinstance(dfe, fwdpy11.Sregion) is False:
        raise TypeError("dfe must be a fwdpy11.Sregion")

    if dfe.b != 0.0 or dfe.e != 1.0:
        raise ValueError("DFE beg/end must be 0.0/1.0, repsectively")

    pop = fwdpy11.SlocusPop(popsize)
    recrate = float(rho) / (4.0 * float(popsize))

    pdict = {
        'rates': (0.0, mu, recrate),
        'nregions': [],
        'sregions': [dfe],
        'recregions': [fwdpy11.Region(0, 1, 1)],
        'gvalue': fwdpy11.fitness.SlocusMult(2.0),
        'demography': np.array([popsize] * 10 * popsize, dtype=np.uint32)
    }

    params = fwdpy11.model_params.SlocusParams(**pdict)
    rng = fwdpy11.GSLrng(seed)
    ancestry = evolve_track(rng, pop, params, gc_interval)
    return (pop, ancestry)
コード例 #2
0
def set_up_quant_trait_model():
    N = 1000
    demography = np.array([N] * 10 * N, dtype=np.uint32)
    rho = 1000.
    r = rho / (4 * N)

    timepoints = np.array([0], dtype=np.uint32)
    ntraits = 4
    optima = np.array(np.zeros(len(timepoints) * ntraits))
    optima = optima.reshape((len(timepoints), ntraits))
    GSSmo = fwdpy11.MultivariateGSSmo(timepoints, optima, 1)
    cmat = np.identity(ntraits)
    np.fill_diagonal(cmat, 0.1)
    a = fwdpy11.StrictAdditiveMultivariateEffects(ntraits, 0, GSSmo)
    p = {
        'nregions': [],
        'sregions': [fwdpy11.MultivariateGaussianEffects(0, 1, 1, cmat)],
        'recregions': [fwdpy11.Region(0, 1, 1)],
        'rates': (0.0, 0.001, 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, ntraits
コード例 #3
0
ファイル: simulate.py プロジェクト: molpopgen/qtrait_paper
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 evolve_and_return(args):
    """
    This function runs our simulation.
    The input arguments come in a tuple,
    which is required by many of Python's
    functions for execution in separate processes.

    For this function, the arguments are the population
    size and a random number seed.
    """
    from fwdpy11 import Multiplicative
    N, seed = args
    # Construct as single-deme object
    # with N diploids
    pop = fp11.DiploidPopulation(N)
    theta = 100.0
    # Initialize a random number generator
    rng = fp11.GSLrng(seed)
    p = fp11ez.mslike(pop,
                      simlen=100,
                      rates=(theta / float(4 * pop.N), 1e-3,
                             theta / float(4 * pop.N)))
    p['gvalue'] = Multiplicative(2.)
    params = fp11.ModelParams(**p)
    fp11.evolve_genomes(rng, pop, params)
    # The population is picklable, and so
    # we can return it from another process
    return pop
コード例 #5
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))
コード例 #6
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
コード例 #7
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)
コード例 #8
0
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
コード例 #9
0
 def __init__(self,
              set_gen,
              final,
              Nstart,
              nwindows=55,
              beginning=50,
              replicate=1):
     self.nwindows = nwindows
     self.val_per_window = 4
     self.beginning = beginning
     self.pi = [['N'] + [
         i for i in range((self.nwindows - self.beginning) *
                          self.val_per_window)
     ]]
     self.singleton = [['N'] + [
         i for i in range((self.nwindows - self.beginning) *
                          self.val_per_window)
     ]]
     self.tajimasD = [['N'] + [
         i for i in range((self.nwindows - self.beginning) *
                          self.val_per_window)
     ]]
     self.counter = 1
     self.final = final
     self.set_gen = set_gen
     self.Nstart = Nstart
     self.__rng = fp11.GSLrng(np.random.randint(42 + float(replicate)))
コード例 #10
0
 def test_mutation_counts_with_indexing_suppressed_no_neutral_muts_in_genomes(
         self):
     """
     A sim w/ and w/o putting neutral variants in genomes
     should give the same mutation counts.
     """
     pop2 = copy.deepcopy(self.pop)
     rng = fwdpy11.GSLrng(101 * 45 * 110 * 210)  # Use same seed!!!
     self.params.prune_selected = False
     params = copy.deepcopy(self.params)
     fwdpy11.evolvets(rng, pop2, params, 100, suppress_table_indexing=True)
     fwdpy11.evolvets(self.rng,
                      self.pop,
                      self.params,
                      100,
                      suppress_table_indexing=True)
     ti = fwdpy11.TreeIterator(self.pop.tables,
                               [i for i in range(2 * self.pop.N)])
     mc = _count_mutations_from_diploids(self.pop)
     for t in ti:
         for m in t.mutations():
             # Have to skip neutral mutations b/c they won't
             # end up in mc b/c it is obtained from genomes
             if pop2.mutations[m.key].neutral is False:
                 self.assertEqual(mc[m.key], self.pop.mcounts[m.key])
             self.assertEqual(t.leaf_counts(m.node), pop2.mcounts[m.key])
コード例 #11
0
def evolve_snowdrift(args):
    """
    We write the function taking a tuple
    out of habit, simplifying later
    integration with multiprocessing or
    concurrent.futures.
    """
    N, seed = args
    # Construct as single-deme object
    # with N diploids
    pop = fp11.DiploidPopulation(N)
    # Initialize a random number generator
    rng = fp11.GSLrng(seed)
    p = {
        'sregions': [fp11.ExpS(0, 1, 1, -0.1, 1.0)],
        'recregions': [fp11.Region(0, 1, 1)],
        'nregions': [],
        'gvalue': snowdrift.DiploidSnowdrift(0.2, -0.2, 1, -2),
        # evolve for 100 generations so that unit tests are
        # fast
        'demography': np.array([N] * 100, dtype=np.uint32),
        'rates': (0.0, 0.0025, 0.001),
        'prune_selected': False
    }
    params = fwdpy11.ModelParams(**p)
    sampler = SamplePhenotypes(params.gvalue)
    fp11.evolve_genomes(rng, pop, params, sampler)
    # return our pop
    return pop
コード例 #12
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)
コード例 #13
0
ファイル: test_pickling.py プロジェクト: pythseq/fwdpy11
    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)
コード例 #14
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
コード例 #15
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)
コード例 #16
0
 def setUp(self):
     self.pop = fwdpy11.DiploidPopulation(1000)
     self.pdict = fwdpy11.ezparams.mslike(self.pop,
                                          dfe=fwdpy11.ExpS(0, 1, 1, -0.05),
                                          pneutral=0.95,
                                          simlen=10)
     self.pdict['gvalue'] = ca.additive()
     self.rng = fwdpy11.GSLrng(42)
     self.params = fwdpy11.ModelParams(**self.pdict)
コード例 #17
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
コード例 #18
0
 def setUp(self):
     self.pop = fwdpy11.DiploidPopulation(1000)
     self.pdict = fwdpy11.ezparams.mslike(self.pop,
                                          dfe=fwdpy11.ConstantS(
                                              0, 1, 1, -0.05, 0.05),
                                          pneutral=0.95,
                                          simlen=10)
     self.pdict['gvalue'] = general.GeneralW()
     self.rng = fwdpy11.GSLrng(42)
     self.params = fwdpy11.ModelParams(**self.pdict)
コード例 #19
0
 def setUp(self):
     from fwdpy11.ezparams import mslike
     from fwdpy11 import Multiplicative
     from fwdpy11 import ModelParams
     self.sampler = DiploidTypeSampler()
     self.pop = fwdpy11.DiploidPopulation(1000)
     self.params_dict = mslike(self.pop, simlen=10)
     self.params_dict['gvalue'] = Multiplicative(2.)
     self.params = ModelParams(**self.params_dict)
     self.rng = fwdpy11.GSLrng(42)
コード例 #20
0
ファイル: bgs.py プロジェクト: pythseq/fwdpy11
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))
コード例 #21
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
コード例 #22
0
    def test_stop(self):
        rng = fwdpy11.GSLrng(42)

        def generation(pop, simplified):
            if pop.generation >= 50:
                return True
            return False

        fwdpy11.evolvets(
            rng, self.pop, self.params, 100,
            stopping_criterion=generation)
        self.assertEqual(self.pop.generation, 50)
コード例 #23
0
 def setUpClass(self):
     self.params, self.rng, self.pop = set_up_quant_trait_model()
     self.params.demography = np.array([self.pop.N] * 200, dtype=np.uint32)
     self.pop2 = copy.deepcopy(self.pop)
     self.rng2 = fwdpy11.GSLrng(101 * 45 * 110 * 210)
     params2 = copy.deepcopy(self.params)
     fwdpy11.evolvets(self.rng, self.pop, self.params, 100)
     fwdpy11.evolvets(self.rng2,
                      self.pop2,
                      params2,
                      100,
                      remove_extinct_variants=False)
コード例 #24
0
 def setUpClass(self):
     from fwdpy11 import ModelParams
     from fwdpy11 import Multiplicative
     self.pop = fp11.DiploidPopulation(1000)
     self.rng = fp11.GSLrng(42)
     self.recorder = GenerationRecorder()
     self.p = ModelParams()
     self.p.rates = (1e-3, 1e-3, 1e-3)
     self.p.demography = np.array([1000] * 100, dtype=np.uint32)
     self.p.nregions = [fp11.Region(0, 1, 1)]
     self.p.sregions = [fp11.ExpS(0, 1, 1, -1e-2)]
     self.p.recregions = self.p.nregions
     self.p.gvalue = Multiplicative(2.0)
コード例 #25
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
コード例 #26
0
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
コード例 #27
0
    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)
コード例 #28
0
def evolve(N, rho, simlen=20, seed=42):
    """
    Evolve without selection and without simplifying.

    :param (int) N: Number of diploids
    :param (float) rho: 4Nr
    :param (int) simlen: (Default: 10) Evolve for simlen*N generations.
    :param (int) seed: (Default: 42) RNG seed.

    :rtype: fwdpy11_arg_example.argsimplifier.ArgSimplifier

    :return: Unsimplified data
    """
    pop = fp11.SlocusPop(N)

    # Set up parameters with defaults
    recrate = rho / (4.0 * float(N))
    mutrate_n = 0.0
    mutrate_s = 0.0

    pdict = {'rates': (mutrate_n, mutrate_s, recrate),
            'nregions': [],
             'sregions': [],
             'recregions': [fp11.Region(0, 1, 1)],
             'gvalue': fp11w.SlocusMult(1.0),
             'demography': np.array([N] * simlen * N, dtype=np.uint32)
             }
    params = fp11mp.SlocusParams(**pdict)

    # Set the gc_interval to be so long that it never happens
    gc_interval = 2 * simlen * N
    simplifier = ArgSimplifier(gc_interval, None)
    atracker = AncestryTracker(pop.N, False, 2*pop.N)
    mm = makeMutationRegions(params.nregions, params.sregions)
    rm = makeRecombinationRegions(params.recregions)
    rng = fp11.GSLrng(seed)
    tsim = evolve_singlepop_regions_track_ancestry(rng, pop, atracker, simplifier,
                                                   params.demography,
                                                   params.mutrate_s,
                                                   params.recrate, mm, rm,
                                                   params.gvalue, params.pself)
    # Reverse time:
    atracker.prep_for_gc()
    return atracker
コード例 #29
0
def process_replicate(argtuple):
    filename, repid, seed = argtuple

    with gzip.open(filename, 'rb') as f:
        pop = fwdpy11.SlocusPop.load_from_pickle_file(f)

    rng = fwdpy11.GSLrng(seed)
    np.random.seed(seed)

    # Add neutral mutations
    nm = fwdpy11.ts.infinite_sites(rng, pop,
                                   float(NLOCI) * args.theta / (4 * pop.N))

    # Get the node table for the pop and the
    # metadata for ancient samples
    nodes = np.array(pop.tables.nodes, copy=False)
    amd = np.array(pop.ancient_sample_metadata, copy=False)
    # These are the times for each ancient sample
    amt = nodes['time'][amd['nodes'][:, 0]]

    genome_scan = []
    qtraits = []
    ld = []
    for t in np.unique(amt):
        # Get the indexes of the metadata corresponding
        # to this time point
        sample_indexes_at_time = np.where(amt == t)[0]

        # Calc some simple stats about the overall pop'n
        mean_trait_value = amd['g'][sample_indexes_at_time].mean()
        vg = amd['g'][sample_indexes_at_time].var()
        wbar = amd['w'][sample_indexes_at_time].mean()
        qtraits.append(QtraitRecord(t, mean_trait_value, wbar, vg))

        samples = amd['nodes'][sample_indexes_at_time].flatten()
        tables, idmap = fwdpy11.ts.simplify(pop, samples)

        ld.append(pairwise_LD(t, pop, tables, samples, idmap))
        genome_scan.extend(
            genome_scan_stats(t, pop.mutations, tables, idmap, amd,
                              sample_indexes_at_time))

    return repid, genome_scan, qtraits, ld
コード例 #30
0
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