예제 #1
0
 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
         top_md = tables.metadata
         top_md['SLiM']['separate_sexes'] = True
         tables.metadata = top_md
         metadata = [ind.metadata for ind in tables.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]
         ims = tables.individuals.metadata_schema
         tables.individuals.packset_metadata(
                 [ims.validate_and_encode_row(r) for r in metadata])
         pop_metadata = [p.metadata for p in tables.populations]
         for j, md in enumerate(pop_metadata):
             # nonWF models always have this
             md['sex_ratio'] = 0.0
         pms = tables.populations.metadata_schema
         tables.populations.packset_metadata(
                 [pms.validate_and_encode_row(r) for r in pop_metadata])
         new_ts = pyslim.load_tables(tables)
         for j, ind in enumerate(new_ts.individuals()):
             md = ind.metadata
             self.assertEqual(md["sex"], sexes[j])
         self.verify_annotated_tables(new_ts, slim_ts)
         self.verify_annotated_trees(new_ts, slim_ts)
         self.verify_haplotype_equality(new_ts, slim_ts)
         # try loading this into SLiM
         loaded_ts = self.run_msprime_restart(new_ts, sex="A")
         self.verify_trees_equal(new_ts, loaded_ts)
예제 #2
0
 def test_many_populations(self, helper_functions, tmp_path):
     # test we can add more than one population
     ts = msprime.sim_ancestry(5,
                               population_size=10,
                               sequence_length=100,
                               random_seed=455)
     t = ts.dump_tables()
     for k in range(5):
         md = pyslim.default_slim_metadata('population')
         md['name'] = f"new_pop_num_{k}"
         md['description'] = f"the {k}-th added pop"
         t.populations.add_row(metadata=md)
         i = t.individuals.add_row()
         for _ in range(2):
             t.nodes.add_row(flags=1, time=0.0, individual=i, population=k)
     ts = t.tree_sequence()
     ts = pyslim.annotate_defaults(ts, model_type='WF', slim_generation=1)
     for ind in ts.individuals():
         assert ind.flags == pyslim.INDIVIDUAL_ALIVE
     sts = helper_functions.run_slim_restart(
         ts,
         "restart_WF.slim",
         tmp_path,
         WF=True,
     )
예제 #3
0
 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)
예제 #4
0
 def test_basic_annotation(self, helper_functions, tmp_path):
     for ts in helper_functions.get_msprime_examples():
         for do_mutations in [False, True]:
             slim_gen = 4
             slim_ts = pyslim.annotate_defaults(
                 ts,
                 model_type="WF",
                 slim_generation=slim_gen,
                 annotate_mutations=do_mutations,
             )
             assert slim_ts.metadata['SLiM']['model_type'] == 'WF'
             assert slim_ts.metadata['SLiM']['generation'] == slim_gen
             assert slim_ts.metadata['SLiM'][
                 'file_version'] == pyslim.slim_file_version
             self.verify_annotated_tables(ts,
                                          slim_ts,
                                          check_alleles=(not do_mutations))
             self.verify_annotated_trees(ts, slim_ts)
             if not do_mutations:
                 self.verify_haplotype_equality(ts, slim_ts)
             self.verify_defaults(slim_ts)
             self.verify_provenance(slim_ts)
             # try loading this into SLiM
             loaded_ts = helper_functions.run_msprime_restart(slim_ts,
                                                              tmp_path,
                                                              WF=True)
             self.verify_annotated_tables(loaded_ts, slim_ts)
             self.verify_annotated_trees(loaded_ts, slim_ts)
             self.verify_haplotype_equality(loaded_ts, slim_ts)
예제 #5
0
 def test_generate_nucleotides_keep(self):
     ts = msprime.sim_ancestry(4, sequence_length=10, population_size=10)
     ts = pyslim.annotate_defaults(ts, model_type='nonWF', slim_generation=1)
     mts1 = msprime.sim_mutations(ts,
             model=msprime.SLiMMutationModel(type=1),
             rate=0.1,
             random_seed=23)
     mts1.dump("out.trees")
     nts1 = pyslim.generate_nucleotides(mts1, seed=10, keep=False)
     assert nts1.num_mutations > 0
     self.verify_generate_nucleotides(nts1, check_transitions=False)
     mts2 = msprime.sim_mutations(nts1,
             model=msprime.SLiMMutationModel(
                 type=2,
                 next_id=nts1.num_mutations,
             ),
             rate=0.1,
             random_seed=24,
     )
     # keep defaults to True
     nts2 = pyslim.generate_nucleotides(mts2, seed=12)
     assert nts2.num_mutations > nts1.num_mutations
     muts1 = {}
     for mut in nts1.mutations():
         for i, md in zip(mut.derived_state.split(","), mut.metadata['mutation_list']):
             muts1[i] = md['nucleotide']
     for mut in nts2.mutations():
         for i, md in zip(mut.derived_state.split(","), mut.metadata['mutation_list']):
             if md['mutation_type'] == 1:
                 assert i in muts1
                 assert muts1[i] == md['nucleotide']
             else:
                 assert md['nucleotide'] in [0, 1, 2, 3]
     nts3 = pyslim.generate_nucleotides(mts2, keep=False, seed=15)
     self.verify_generate_nucleotides(nts3, check_transitions=False)
예제 #6
0
 def test_annotate_refseq(self):
     ts = msprime.sim_ancestry(2, sequence_length=10, random_seed=77)
     refseq = "A" * int(ts.sequence_length)
     ats = pyslim.annotate_defaults(ts,
                                    model_type="nonWF",
                                    slim_generation=5,
                                    reference_sequence=refseq)
     assert ats.reference_sequence.data == refseq
예제 #7
0
 def test_annotate_errors(self, helper_functions):
     for ts in helper_functions.get_msprime_examples():
         with pytest.raises(ValueError):
             _ = pyslim.annotate_defaults(ts,
                                          model_type="WF",
                                          slim_generation=0)
         with pytest.raises(ValueError):
             _ = pyslim.annotate_defaults(ts,
                                          model_type="WF",
                                          slim_generation=4.4)
         with pytest.raises(ValueError):
             _ = pyslim.annotate_defaults(ts,
                                          model_type="foo",
                                          slim_generation=4)
         with pytest.raises(ValueError):
             _ = pyslim.annotate_defaults(ts,
                                          model_type=[],
                                          slim_generation=4)
     # odd number of samples
     ts = msprime.simulate(3)
     with pytest.raises(ValueError) as except_info:
         _ = pyslim.annotate_defaults(ts,
                                      model_type="WF",
                                      slim_generation=1)
     assert "diploid" in str(except_info)
     # inconsistent populations for diploids
     ts = msprime.simulate(population_configurations=[
         msprime.PopulationConfiguration(sample_size=3),
         msprime.PopulationConfiguration(sample_size=1)
     ],
                           migration_matrix=[[0.0, 1.0], [1.0, 0.0]])
     with pytest.raises(ValueError) as except_info:
         _ = pyslim.annotate_defaults(ts,
                                      model_type="WF",
                                      slim_generation=1)
     assert "more than one population" in str(except_info)
     # inconsistent times for diploids
     samples = [
         msprime.Sample(population=0, time=0),
         msprime.Sample(population=0, time=0),
         msprime.Sample(population=0, time=0),
         msprime.Sample(population=0, time=1),
     ]
     ts = msprime.simulate(samples=samples)
     with pytest.raises(ValueError) as except_info:
         _ = pyslim.annotate_defaults(ts,
                                      model_type="WF",
                                      slim_generation=1)
     assert "more than one time" in str(except_info)
     # non-integer positions
     ts = msprime.simulate(4, mutation_rate=10)
     with pytest.raises(ValueError) as except_info:
         _ = pyslim.annotate_defaults(ts,
                                      model_type="WF",
                                      slim_generation=1)
     assert "not at integer" in str(except_info)
예제 #8
0
 def test_annotate_XY(self):
     random.seed(8)
     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
             top_md = tables.metadata
             top_md['SLiM']['separate_sexes'] = True
             tables.metadata = top_md
             metadata = [ind.metadata for ind in tables.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]
             ims = tables.individuals.metadata_schema
             tables.individuals.packset_metadata(
                 [ims.validate_and_encode_row(r) for r in metadata])
             node_metadata = [n.metadata for n in tables.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")
             nms = tables.nodes.metadata_schema
             tables.nodes.packset_metadata(
                 [nms.validate_and_encode_row(r) for r in node_metadata])
             pop_metadata = [p.metadata for p in tables.populations]
             for j, md in enumerate(pop_metadata):
                 # nonWF models always have this
                 md['sex_ratio'] = 0.0
             pms = tables.populations.metadata_schema
             tables.populations.packset_metadata(
                 [pms.validate_and_encode_row(r) for r in pop_metadata])
             new_ts = pyslim.load_tables(tables)
             self.verify_annotated_tables(new_ts, slim_ts)
             self.verify_annotated_trees(new_ts, slim_ts)
             self.verify_haplotype_equality(new_ts, slim_ts)
             # try loading this into SLiM
             loaded_ts = self.run_msprime_restart(new_ts, sex=genome_type)
             self.verify_trees_equal(new_ts, loaded_ts)
예제 #9
0
 def test_basic_annotation(self):
     for ts in get_msprime_examples():
         slim_gen = 4
         slim_ts = pyslim.annotate_defaults(ts, model_type="WF",
                                            slim_generation=slim_gen)
         self.verify_annotated_tables(ts, slim_ts)
         self.verify_annotated_trees(ts, slim_ts)
         self.verify_haplotype_equality(ts, slim_ts)
         self.verify_defaults(slim_ts)
         self.verify_provenance(slim_ts)
예제 #10
0
 def test_annotate_mutations(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_mutation_metadata(tables))
         self.assertEqual(len(metadata), slim_ts.num_mutations)
         selcoefs = [random.uniform(0, 1) for _ in metadata]
         for j in range(len(metadata)):
             metadata[j].selection_coeff = selcoefs[j]
         pyslim.annotate_mutation_metadata(tables, metadata)
         new_ts = pyslim.load_tables(tables)
         for j, x in enumerate(new_ts.mutations()):
             md = pyslim.decode_mutation(x.metadata)
             self.assertEqual(md.selection_coeff, selcoefs[j])
예제 #11
0
 def test_just_simulate(self, helper_functions, tmp_path):
     ts = msprime.simulate(sample_size=4,
                           Ne=10,
                           length=10,
                           mutation_rate=0.0,
                           recombination_rate=0.01)
     ts = msprime.sim_mutations(ts, rate=0.1)
     slim_ts = pyslim.annotate_defaults(ts,
                                        model_type="WF",
                                        slim_generation=1)
     loaded_ts = helper_functions.run_msprime_restart(slim_ts,
                                                      tmp_path,
                                                      WF=True)
     self.verify_annotated_trees(ts, loaded_ts)
예제 #12
0
 def test_annotate_mutations(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 = [m.metadata for m in tables.mutations]
         selcoefs = [random.uniform(0, 1) for _ in metadata]
         for j in range(len(metadata)):
             metadata[j]['mutation_list'][0]["selection_coeff"] = selcoefs[j]
         ms = tables.mutations.metadata_schema
         tables.mutations.packset_metadata(
                 [ms.validate_and_encode_row(r) for r in metadata])
         new_ts = pyslim.load_tables(tables)
         for j, x in enumerate(new_ts.mutations()):
             md = x.metadata
             self.assertEqual(md['mutation_list'][0]["selection_coeff"], selcoefs[j])
예제 #13
0
 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])
예제 #14
0
 def test_unique_names(self):
     ts = msprime.sim_ancestry(4, sequence_length=10, random_seed=12)
     ts = pyslim.annotate_defaults(ts, model_type="nonWF", slim_generation=1)
     t = ts.dump_tables()
     md = t.populations[0].metadata
     md.update({"name": "ancestral"})
     t.populations[0] = t.populations[0].replace(metadata=md)
     md.update({"name": "ancestral_ancestral", "slim_id": 4})
     t.populations.add_row(metadata=md)
     ts = t.tree_sequence()
     rts = self.do_recapitate(ts, ancestral_Ne=10)
     names = [pop.metadata['name'] for pop in rts.populations()]
     assert len(set(names)) == len(names)
     assert "ancestral" in names
     assert "ancestral_ancestral" in names
예제 #15
0
 def test_basic_annotation(self):
     for ts in self.get_msprime_examples():
         slim_gen = 4
         slim_ts = pyslim.annotate_defaults(ts, model_type="WF",
                                            slim_generation=slim_gen)
         self.verify_annotated_tables(ts, slim_ts)
         self.verify_annotated_trees(ts, slim_ts)
         self.verify_haplotype_equality(ts, slim_ts)
         self.verify_defaults(slim_ts)
         self.verify_provenance(slim_ts)
         # try loading this into SLiM
         loaded_ts = self.run_msprime_restart(slim_ts, WF=True)
         self.verify_annotated_tables(loaded_ts, slim_ts)
         self.verify_annotated_trees(loaded_ts, slim_ts)
         self.verify_haplotype_equality(loaded_ts, slim_ts)
예제 #16
0
 def test_generate_nucleotides_refseq(self):
     ts = msprime.sim_ancestry(
             4,
             sequence_length=10,
             population_size=10,
             random_seed=10,
     )
     ts = pyslim.annotate_defaults(ts, model_type='nonWF', slim_generation=1)
     mts = msprime.sim_mutations(ts,
             model=msprime.SLiMMutationModel(type=1),
             rate=0.5,
             random_seed=23)
     refseq = "A" * int(mts.sequence_length)
     nts = pyslim.generate_nucleotides(mts, reference_sequence=refseq, seed=6)
     self.verify_generate_nucleotides(nts, check_transitions=True)
     assert nts.reference_sequence.data == refseq
예제 #17
0
 def test_annotate_nodes(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_node_metadata(tables))
         self.assertEqual(len(metadata), slim_ts.num_nodes)
         gtypes = [random.choice([pyslim.GENOME_TYPE_X, pyslim.GENOME_TYPE_Y])
                   for _ in metadata]
         for j in range(len(metadata)):
             if metadata[j] is not None:
                 metadata[j].genome_type = gtypes[j]
         pyslim.annotate_node_metadata(tables, metadata)
         new_ts = pyslim.load_tables(tables)
         for j, x in enumerate(new_ts.nodes()):
             md = pyslim.decode_node(x.metadata)
             if md is not None:
                 self.assertEqual(md.genome_type, gtypes[j])
예제 #18
0
 def test_annotate_nodes(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 = [n.metadata for n in tables.nodes]
         gtypes = [random.choice([pyslim.GENOME_TYPE_X, pyslim.GENOME_TYPE_Y])
                   for _ in metadata]
         for md, g in zip(metadata, gtypes):
             if md is not None:
                 md["genome_type"] = g
         nms = tables.nodes.metadata_schema
         tables.nodes.packset_metadata(
                 [nms.validate_and_encode_row(r) for r in metadata])
         new_ts = pyslim.load_tables(tables)
         for x, g in zip(new_ts.nodes(), gtypes):
             if x.metadata is not None:
                 self.assertEqual(x.metadata["genome_type"], g)
예제 #19
0
 def test_convert_alleles_errors(self):
     ts = msprime.sim_ancestry(4, sequence_length=10, population_size=10)
     with pytest.raises(ValueError, match="must have a valid reference sequence"):
         _ = pyslim.convert_alleles(ts)
     ts = pyslim.annotate_defaults(ts, model_type="nonWF", slim_generation=1)
     with pytest.raises(ValueError, match="must have a valid reference sequence"):
         _ = pyslim.convert_alleles(ts)
     mts = msprime.sim_mutations(ts,
             model=msprime.SLiMMutationModel(type=1),
             rate=0.1,
             random_seed=23)
     assert mts.num_mutations > 0
     mtt = mts.dump_tables()
     mtt.reference_sequence.data = 'A' * int(mts.sequence_length)
     mts = mtt.tree_sequence()
     with pytest.raises(ValueError, match="must be nucleotide mutations"):
         _ = pyslim.convert_alleles(mts)
예제 #20
0
 def test_annotate_errors(self):
     for ts in self.get_msprime_examples():
         with self.assertRaises(ValueError):
             _ = pyslim.annotate_defaults(ts,
                                          model_type="WF",
                                          slim_generation=0)
         with self.assertRaises(ValueError):
             _ = pyslim.annotate_defaults(ts,
                                          model_type="WF",
                                          slim_generation=4.4)
         with self.assertRaises(ValueError):
             _ = pyslim.annotate_defaults(ts,
                                          model_type="foo",
                                          slim_generation=4)
         with self.assertRaises(ValueError):
             _ = pyslim.annotate_defaults(ts,
                                          model_type=[],
                                          slim_generation=4)
     # odd number of samples
     ts = msprime.simulate(3)
     with self.assertRaisesRegex(ValueError, "diploid"):
         _ = pyslim.annotate_defaults(ts,
                                      model_type="WF",
                                      slim_generation=1)
     # inconsistent populations for diploids
     ts = msprime.simulate(population_configurations=[
         msprime.PopulationConfiguration(sample_size=3),
         msprime.PopulationConfiguration(sample_size=1)
     ],
                           migration_matrix=[[0.0, 1.0], [1.0, 0.0]])
     with self.assertRaisesRegex(ValueError, "more than one population"):
         _ = pyslim.annotate_defaults(ts,
                                      model_type="WF",
                                      slim_generation=1)
     # inconsistent times for diploids
     samples = [
         msprime.Sample(population=0, time=0),
         msprime.Sample(population=0, time=0),
         msprime.Sample(population=0, time=0),
         msprime.Sample(population=0, time=1),
     ]
     ts = msprime.simulate(samples=samples)
     with self.assertRaisesRegex(ValueError, "more than one time"):
         _ = pyslim.annotate_defaults(ts,
                                      model_type="WF",
                                      slim_generation=1)
예제 #21
0
 def test_empty_populations(self, helper_functions, tmp_path):
     # test SLiM doesn't error on having empty populations
     ts = msprime.sim_ancestry(5,
                               population_size=10,
                               sequence_length=100,
                               random_seed=455)
     ts = pyslim.annotate_defaults(ts, model_type='WF', slim_generation=1)
     t = ts.dump_tables()
     for k in range(5):
         md = pyslim.default_slim_metadata('population')
         md['name'] = f"new_pop_num_{k}"
         md['description'] = f"the {k}-th added pop"
         t.populations.add_row(metadata=md)
     ts = t.tree_sequence()
     sts = helper_functions.run_slim_restart(
         ts,
         "restart_WF.slim",
         tmp_path,
         WF=True,
     )
예제 #22
0
 def test_basic_annotation(self):
     for ts in self.get_msprime_examples():
         slim_gen = 4
         slim_ts = pyslim.annotate_defaults(ts,
                                            model_type="WF",
                                            slim_generation=slim_gen)
         self.assertEqual(slim_ts.metadata['SLiM']['model_type'], 'WF')
         self.assertEqual(slim_ts.metadata['SLiM']['generation'], slim_gen)
         self.assertEqual(slim_ts.metadata['SLiM']['file_version'],
                          pyslim.slim_file_version)
         self.verify_annotated_tables(ts, slim_ts)
         self.verify_annotated_trees(ts, slim_ts)
         self.verify_haplotype_equality(ts, slim_ts)
         self.verify_defaults(slim_ts)
         self.verify_provenance(slim_ts)
         # try loading this into SLiM
         loaded_ts = self.run_msprime_restart(slim_ts, WF=True)
         self.verify_annotated_tables(loaded_ts, slim_ts)
         self.verify_annotated_trees(loaded_ts, slim_ts)
         self.verify_haplotype_equality(loaded_ts, slim_ts)
예제 #23
0
 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)
예제 #24
0
 def test_annotate_XY(self, helper_functions, tmp_path):
     # This is Not Right because as written the null chromosomes have history;
     # we need to *only* simulate the non-null chromosomes.
     random.seed(8)
     for ts in helper_functions.get_msprime_examples():
         for genome_type in ["X", "Y"]:
             slim_ts = pyslim.annotate_defaults(ts,
                                                model_type="nonWF",
                                                slim_generation=1)
             tables = slim_ts.dump_tables()
             top_md = tables.metadata
             top_md['SLiM']['separate_sexes'] = True
             tables.metadata = top_md
             metadata = [ind.metadata for ind in tables.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]
             ims = tables.individuals.metadata_schema
             tables.individuals.packset_metadata(
                 [ims.validate_and_encode_row(r) for r in metadata])
             node_metadata = [n.metadata for n in tables.nodes]
             node_is_null = [False for _ in range(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")
                 for n in nodes:
                     node_is_null[n] = node_metadata[n]["is_null"]
             nms = tables.nodes.metadata_schema
             tables.nodes.packset_metadata(
                 [nms.validate_and_encode_row(r) for r in node_metadata])
             # update populations
             pop_metadata = [p.metadata for p in tables.populations]
             for j, md in enumerate(pop_metadata):
                 # nonWF models always have this
                 md['sex_ratio'] = 0.0
             pms = tables.populations.metadata_schema
             tables.populations.packset_metadata(
                 [pms.validate_and_encode_row(r) for r in pop_metadata])
             new_ts = tables.tree_sequence()
             self.verify_annotated_tables(new_ts, slim_ts)
             self.verify_annotated_trees(new_ts, slim_ts)
             self.verify_haplotype_equality(new_ts, slim_ts)
             # try loading this into SLiM
             loaded_ts = helper_functions.run_msprime_restart(
                 new_ts, tmp_path, sex=genome_type)
             self.verify_trees_equal(new_ts, loaded_ts)
예제 #25
0
    print(f"to {max(mut_map.values()):0.2e}.")
    return tables.tree_sequence()


# Snakes:
snake_demog = msprime.Demography()
snake_demog.add_population(name="p0", initial_size=10000)
snakes = msprime.sim_ancestry(
    samples={"p0": 300},  # number of individividuals sampled
    demography=snake_demog,
    recombination_rate=1e-8,
    sequence_length=sequence_length)

snakes = pyslim.annotate_defaults(
    snakes,
    model_type='nonWF',
    slim_generation=1,
)

# add mutations
snakes = add_mutations(snakes,
                       mut_type=2,
                       mu_rate=snake_mu_rate,
                       effect_sd=snake_mu_effect_sd)

# snakes should be in population 0 and have mutations of type 2
for n in snakes.nodes():
    assert n.population == 0

for m in snakes.mutations():
    for md in m.metadata['mutation_list']:
예제 #26
0
## this part of the code calls msprime to simulate an "unmutated" tree sequence.
if not args.af:
    simulation = msprime.simulate(args.Ne,
                                  recombination_rate=args.r,
                                  length=args.l,
                                  Ne=args.Ne)
else:
    population_configurations, demographic_events = afr_burnin()
    simulation = msprime.simulate(
        recombination_rate=args.r,
        length=args.l,
        population_configurations=population_configurations,
        demographic_events=demographic_events,
        Ne=2 * 14474)

ts = pyslim.annotate_defaults(simulation, model_type="WF", slim_generation=1)

# "throw" a standing variant onto the tree sequence;
# keep track of where it occurs and its frequency
mut_base, freq, mut_ts = throw_mut_on_tree(ts)

# save treeseq
mut_ts.dump("%s.trees" % (args.out))
basename = args.out

s = args.s

c = args.c
if not args.af:
    n = args.Ne
else:
예제 #27
0
# Keywords: Python, tree-sequence recording, tree sequence recording

import msprime, pyslim

ts = msprime.simulate(sample_size=10000,
                      Ne=5000,
                      length=1e8,
                      mutation_rate=0.0,
                      recombination_rate=1e-8)
slim_ts = pyslim.annotate_defaults(ts, model_type="WF", slim_generation=1)
slim_ts.dump("recipe_16.8.trees")
# Keywords: Python, tree-sequence recording, tree sequence recording

import msprime, pyslim

ts = msprime.simulate(sample_size=10000, Ne=5000, length=1e8,
    mutation_rate=0.0, recombination_rate=1e-8)
slim_ts = pyslim.annotate_defaults(ts, model_type="WF", slim_generation=1)
slim_ts.dump("recipe_17.8.trees")
예제 #29
0
        # Size changes to N_A at T_AF
        msprime.PopulationParametersChange(
            time=T_AF, initial_size=N_A, population_id=0)
    ]
    # Use the demography debugger to print out the demographic history
    # that we have just described.
    dd = msprime.DemographyDebugger(
        population_configurations=population_configurations,
        migration_matrix=migration_matrix,
        demographic_events=demographic_events)
    dd.print_history()

    # set mutation_rate to you need to genotypes
    return msprime.simulate(
        population_configurations=population_configurations,
        migration_matrix=migration_matrix,
        demographic_events=demographic_events,
        length=2.5e8,
        recombination_rate=1e-8,
        mutation_rate=0
    )

outofafrica_tree = out_of_africa(arg.sample_n_AF, arg.sample_n_EU, arg.sample_n_AS)
# If you want msprime tree sequence
#outofafrica_tree.dump(arg.out)

# If your analysis is followed by SLiM
new_outofafrica_tree = pyslim.annotate_defaults(outofafrica_tree, model_type="WF", slim_generation=1)
new_outofafrica_tree.dump(arg.out)