def clean_example(self): tables = tskit.TableCollection(sequence_length=100) tables.populations.add_row() tables.populations.add_row() tables.individuals.add_row() tables.nodes.add_row(time=0, flags=tskit.NODE_IS_SAMPLE, population=1, individual=0) tables.nodes.add_row(time=0, flags=tskit.NODE_IS_SAMPLE, population=1, individual=0) pyslim.annotate_defaults_tables(tables, model_type='nonWF', slim_generation=1) return tables
def test_inconsistent_nodes(self): clean_tables = self.clean_example() tables = clean_tables.copy() tables.nodes.clear() for j, n in enumerate(clean_tables.nodes): tables.nodes.add_row( time=n.time, flags=n.flags, population=j, individual=n.individual, metadata=n.metadata) with pytest.raises(ValueError): pyslim.annotate_defaults_tables(tables, model_type='nonWF', slim_generation=1) ts = tables.tree_sequence() with pytest.raises(ValueError): _ = pyslim.SlimTreeSequence(ts)
def test_dont_annotate_mutations(self, helper_functions): # Test the option to not overwrite mutation annotations ts = msprime.sim_ancestry(10) ts = msprime.sim_mutations(ts, rate=5, random_seed=3) assert ts.num_mutations > 0 tables = ts.dump_tables() pre_mutations = tables.mutations.copy() pyslim.annotate_defaults_tables(tables, model_type="WF", slim_generation=1, annotate_mutations=False) # this is necessary because b'' actually is decoded to # an empty mutation_list by the schema pre_mutations.metadata_schema = tables.mutations.metadata_schema assert tables.mutations.equals(pre_mutations)
# Keywords: Python, nonWF, non-Wright-Fisher, tree-sequence recording, tree sequence recording import msprime, pyslim, random import numpy as np 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) # add sexes and ages individual_metadata = [ind.metadata for ind in tables.individuals] for md in individual_metadata: md["sex"] = random.choice( [pyslim.INDIVIDUAL_TYPE_FEMALE, pyslim.INDIVIDUAL_TYPE_MALE]) md["age"] = random.choice([0, 1, 2, 3, 4]) ims = tables.individuals.metadata_schema tables.individuals.packset_metadata( [ims.validate_and_encode_row(md) for md in individual_metadata]) # add selected mutation mut_ind_id = random.choice(range(tables.individuals.num_rows)) mut_node_id = random.choice(np.where(tables.nodes.individual == mut_ind_id)[0]) mut_node = tables.nodes[mut_node_id] mut_metadata = {
# 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")