Ejemplo n.º 1
0
 def setUp(self):
     GN = fwdpy11.GaussianNoise
     self.w = fwdpy11.Additive(2.0)
     self.t = fwdpy11.Additive(
         2.0, fwdpy11.GSS(0.0, 1.0))
     self.tn = fwdpy11.Additive(1.0,
                                fwdpy11.GSS(
                                    0.0, 1.0),
                                GN(mean=0.1, sd=2.0))
Ejemplo n.º 2
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
Ejemplo n.º 3
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)
Ejemplo n.º 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
Ejemplo n.º 5
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)
Ejemplo n.º 6
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
Ejemplo n.º 7
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)
Ejemplo n.º 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)
Ejemplo n.º 9
0
 def setUp(self):
     self.pop = fwdpy11.DiploidPopulation(1000, 1.0)
     p = {'nregions': [],  # No neutral mutations -- add them later!
          'gvalue': fwdpy11.Additive(2.0),
          'sregions': [fwdpy11.ExpS(0, 1, 1, -0.1)],
          'recregions': [fwdpy11.Region(0, 1, 1)],
          'rates': (0.0, 1e-3, 1e-3),
          # Keep mutations at frequency 1 in the pop if they affect fitness.
          'prune_selected': False,
          'demography':  np.array([1000]*10000, dtype=np.uint32)
          }
     self.params = fwdpy11.ModelParams(**p)
Ejemplo n.º 10
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)
Ejemplo n.º 11
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))
Ejemplo n.º 12
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)
Ejemplo n.º 13
0
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)
Ejemplo n.º 14
0
 def setUp(self):
     self.a = fwdpy11.Additive(
         2.0, fwdpy11.GSS(0.0, 1.0))
     self.b = fwdpy11.Additive(
         2.0, fwdpy11.GSSmo([(0, 0.0, 1.0)]))
     self.pop = fwdpy11.DiploidPopulation(1000)