def test_VariantIteratorBeginEnd(self): for i in np.arange(0, self.pop.tables.genome_length, 0.1): vi = fwdpy11.VariantIterator(self.pop.tables, self.pop.mutations, [i for i in range(2 * self.pop.N)], i, i + 0.1) nm = len([ j for j in self.pop.tables.mutations if self.pop.mutations[j.key].pos >= i and self.pop.mutations[j.key].pos < i + 0.1 ]) nseen = 0 for v in vi: r = v.record self.assertTrue(self.pop.mutations[r.key].pos >= i) self.assertTrue(self.pop.mutations[r.key].pos < i + 0.1) nseen += 1 self.assertEqual(nm, nseen) # test bad start/stop with self.assertRaises(ValueError): vi = fwdpy11.VariantIterator(self.pop.tables, self.pop.mutations, [i for i in range(2 * self.pop.N)], begin=0.5, end=0.25)
def test_mcounts_from_ancient_samples(self): vi = fwdpy11.VariantIterator(self.pop.tables, self.pop.tables.preserved_nodes) for v in vi: k = v.records[0] self.assertEqual(self.pop.mcounts_ancient_samples[k.key], v.genotypes.sum())
def test_VariantIteratorFromPreservedSamples(self): n = np.array(self.pop.tables.nodes) pn = np.array(self.pop.tables.preserved_nodes) at = n['time'][pn] for u in np.unique(at): n = pn[np.where(at == u)[0]] vi = fwdpy11.VariantIterator(self.pop.tables, n) for variant in vi: k = variant.records[0] self.assertNotEqual(k.node, fwdpy11.NULL_NODE) self.assertNotEqual(k.key, np.iinfo(np.uint64).max)
def test_VariantIteratorFromPopulation(self): dm = fwdpy11.data_matrix_from_tables( self.pop.tables, [i for i in range(2 * self.pop.N)], False, True, True) sa = np.array(dm.selected) cs = np.sum(sa, axis=1) i = 0 vi = fwdpy11.VariantIterator(self.pop) for v in vi: c = self.pop.mcounts[self.pop.tables.mutations[i].key] self.assertEqual(c, cs[i]) self.assertEqual(c, v.genotypes.sum()) i += 1 mc = np.array(self.pop.mcounts) self.assertEqual(i, len(np.where(mc > 0)[0])) self.assertEqual(i, len(self.pop.tables.mutations))
def test_VariantIterator(self): """ Test VariantIterator by asserting that sum of genotypes equal values in the corresponding DataMatrix and those in pop.mcounts """ dm = fwdpy11.data_matrix_from_tables( self.pop.tables, [i for i in range(2 * self.pop.N)], False, True, True) sa = np.array(dm.selected) cs = np.sum(sa, axis=1) i = 0 vi = fwdpy11.VariantIterator(self.pop.tables, [i for i in range(2 * self.pop.N)]) for v in vi: c = self.pop.mcounts[self.pop.tables.mutations[i].key] self.assertEqual(c, cs[i]) self.assertEqual(c, v.genotypes.sum()) i += 1 mc = np.array(self.pop.mcounts) self.assertEqual(i, len(np.where(mc > 0)[0])) self.assertEqual(i, len(self.pop.tables.mutations))
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 pickle import lzma import sys import numpy as np import seaborn as sns import fwdpy11 with lzma.open(sys.argv[1], 'rb') as f: pop = pickle.load(f) genetic_trait_values_from_sim = [] genetic_values_from_ts = [] idx = 0 for n, s, m in pop.sample_timepoints(): vi = fwdpy11.VariantIterator(pop.tables, s) sum_esizes = np.zeros(len(s)) 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(s) / 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(m['g'].tolist()) # plot the time vs trait value xyplot = sns.scatterplot(genetic_trait_values_from_sim, genetic_values_from_ts)