def test_annotate_individuals(self): for ts in self.get_msprime_examples(): slim_ts = pyslim.annotate_defaults(ts, model_type="nonWF", slim_generation=1) tables = slim_ts.tables metadata = list(pyslim.extract_individual_metadata(tables)) self.assertEqual(len(metadata), slim_ts.num_individuals) sexes = [ random.choice([ pyslim.INDIVIDUAL_TYPE_FEMALE, pyslim.INDIVIDUAL_TYPE_MALE ]) for _ in metadata ] for j in range(len(metadata)): metadata[j].sex = sexes[j] pyslim.annotate_individual_metadata(tables, metadata) new_ts = pyslim.load_tables(tables) for j, ind in enumerate(new_ts.individuals()): md = ind.metadata self.assertEqual(md.sex, sexes[j]) # try loading this into SLiM loaded_ts = self.run_msprime_restart(new_ts, sex="A") self.verify_annotated_tables(new_ts, slim_ts) self.verify_annotated_trees(new_ts, slim_ts) self.verify_haplotype_equality(new_ts, slim_ts)
def test_annotate_individuals(self): for ts in get_msprime_examples(): slim_ts = pyslim.annotate_defaults(ts, model_type="nonWF", slim_generation=1) tables = slim_ts.tables metadata = list(pyslim.extract_individual_metadata(tables)) self.assertEqual(len(metadata), slim_ts.num_individuals) sexes = [random.choice([pyslim.INDIVIDUAL_TYPE_FEMALE, pyslim.INDIVIDUAL_TYPE_MALE]) for _ in metadata] for j in range(len(metadata)): metadata[j].sex = sexes[j] pyslim.annotate_individual_metadata(tables, metadata) new_ts = pyslim.load_tables(tables) for j, ind in enumerate(new_ts.individuals()): md = pyslim.decode_individual(ind.metadata) self.assertEqual(md.sex, sexes[j])
def test_annotate_XY(self): for ts in self.get_msprime_examples(): for genome_type in ["X", "Y"]: slim_ts = pyslim.annotate_defaults(ts, model_type="nonWF", slim_generation=1) tables = slim_ts.tables metadata = list(pyslim.extract_individual_metadata(tables)) self.assertEqual(len(metadata), slim_ts.num_individuals) sexes = [ random.choice([ pyslim.INDIVIDUAL_TYPE_FEMALE, pyslim.INDIVIDUAL_TYPE_MALE ]) for _ in metadata ] for j in range(len(metadata)): metadata[j].sex = sexes[j] pyslim.annotate_individual_metadata(tables, metadata) node_metadata = list(pyslim.extract_node_metadata(tables)) self.assertEqual(len(node_metadata), slim_ts.num_nodes) for j in range(slim_ts.num_individuals): nodes = slim_ts.individual(j).nodes node_metadata[nodes[0]].genome_type = pyslim.GENOME_TYPE_X node_metadata[nodes[0]].is_null = (genome_type != "X") if sexes[j] == pyslim.INDIVIDUAL_TYPE_MALE: node_metadata[ nodes[1]].genome_type = pyslim.GENOME_TYPE_Y node_metadata[nodes[1]].is_null = (genome_type != "Y") else: node_metadata[ nodes[1]].genome_type = pyslim.GENOME_TYPE_X node_metadata[nodes[1]].is_null = (genome_type != "X") pyslim.annotate_node_metadata(tables, node_metadata) new_ts = pyslim.load_tables(tables) # try loading this into SLiM loaded_ts = self.run_msprime_restart(new_ts, sex=genome_type) self.verify_annotated_tables(new_ts, slim_ts) self.verify_annotated_trees(new_ts, slim_ts) self.verify_haplotype_equality(new_ts, slim_ts)
def verify_defaults(self, ts): ''' Verify the default values have been entered into metadata. ''' mut_md = pyslim.extract_mutation_metadata(ts.tables) for md in mut_md: self.assertEqual(md.mutation_type, 1) self.assertEqual(md.selection_coeff, 0.0) self.assertEqual(md.population, tskit.NULL) self.assertEqual(md.slim_time, 0) node_md = pyslim.extract_node_metadata(ts.tables) for md, node in zip(node_md, ts.nodes()): if not node.is_sample(): self.assertEqual(md, None) else: self.assertEqual(md.is_null, False) self.assertEqual(md.genome_type, pyslim.GENOME_TYPE_AUTOSOME) for ind in ts.individuals(): self.assertArrayEqual(ind.location, [0, 0, 0]) self.assertEqual(ind.flags, pyslim.INDIVIDUAL_ALIVE) ind_md = pyslim.extract_individual_metadata(ts.tables) for md in ind_md: self.assertEqual(md.sex, pyslim.INDIVIDUAL_TYPE_HERMAPHRODITE) self.assertEqual(md.flags, 0) pop_md = pyslim.extract_population_metadata(ts.tables) for md in pop_md: self.assertEqual(md.selfing_fraction, 0.0) self.assertEqual(md.female_cloning_fraction, 0.0) self.assertEqual(md.male_cloning_fraction, 0.0) self.assertEqual(md.sex_ratio, 0.5) self.assertEqual(md.bounds_x0, 0.0) self.assertEqual(md.bounds_x1, 0.0) self.assertEqual(md.bounds_y0, 0.0) self.assertEqual(md.bounds_y1, 0.0) self.assertEqual(md.bounds_z0, 0.0) self.assertEqual(md.bounds_z1, 0.0) self.assertEqual(len(md.migration_records), 0)
# Keywords: Python, nonWF, non-Wright-Fisher, tree-sequence recording, tree sequence recording import msprime, pyslim, random ts = msprime.simulate(sample_size=10000, Ne=5000, length=1e8, mutation_rate=0.0, recombination_rate=1e-8) tables = ts.dump_tables() pyslim.annotate_defaults_tables(tables, model_type="nonWF", slim_generation=1) individual_metadata = list(pyslim.extract_individual_metadata(tables)) for j in range(len(individual_metadata)): individual_metadata[j].sex = random.choice([pyslim.INDIVIDUAL_TYPE_FEMALE, pyslim.INDIVIDUAL_TYPE_MALE]) individual_metadata[j].age = random.choice([0, 1, 2, 3, 4]) pyslim.annotate_individual_metadata(tables, individual_metadata) slim_ts = pyslim.load_tables(tables) slim_ts.dump("recipe_16.9.trees")
# Keywords: Python, nonWF, non-Wright-Fisher, tree-sequence recording, tree sequence recording import msprime, pyslim, random ts = msprime.simulate(sample_size=10000, Ne=5000, length=1e8, mutation_rate=0.0, recombination_rate=1e-8) tables = ts.dump_tables() pyslim.annotate_defaults_tables(tables, model_type="nonWF", slim_generation=1) individual_metadata = list(pyslim.extract_individual_metadata(tables)) for j in range(len(individual_metadata)): individual_metadata[j].sex = random.choice([pyslim.INDIVIDUAL_TYPE_FEMALE, pyslim.INDIVIDUAL_TYPE_MALE]) individual_metadata[j].age = random.choice([0, 1, 2, 3, 4]) pyslim.annotate_individual_metadata(tables, individual_metadata) slim_ts = pyslim.load_tables(tables) slim_ts.dump("recipe_17.9.trees")