def test_bad_tree(self): bad_trees = [] tree_file = "tests/data/species_trees/101g_nucl_conc_unconst.combined.nwk.tre" with open(tree_file) as f: bad_trees.append(f.read()) good_nexus = "#NEXUS\n\n" good_nexus += "Begin taxa;\n" good_nexus += " Dimensions ntax=3;\n" good_nexus += " Taxlabels\n" good_nexus += " spc01\n" good_nexus += " spc02\n" good_nexus += " spc03\n" good_nexus += " ;\n" good_nexus += "End;\n" good_nexus += "Begin trees;\n" good_nexus += " Translate\n" good_nexus += " 1 spc01,\n" good_nexus += " 2 spc02,\n" good_nexus += " 3 spc03\n" good_nexus += " ;\n" good_nwk = "tree TREE1 = ((1[&dmv={0.1}]:1,2[&dmv={0.2}]:1)[&dmv={0.3}]" good_nexus += "End;\n" bad_trees.append(good_nexus.replace("#NEXUS", "#NEXU")) bad_trees.append(good_nexus.replace("#NEXUS", "NEXUS")) bad_trees.append(good_nexus.replace("tree TREE1", "tre TREE1")) bad_trees.append(good_nexus.replace("End;", "")) bad_trees.append(good_nexus.replace("Translate", "T")) bad_trees.append(good_nexus.replace("2 spc02,", "2 spc02")) bad_trees.append(good_nexus.replace("2 spc02,", "2 spc02 asdf,")) bad_trees.append(good_nexus.replace("2 spc02,", "2 spc03,")) bad_trees.append(good_nexus.replace("2 spc02,", "spc02 2,")) bad_trees.append(good_nexus.replace("2 spc02,", "asdf2 spc02,")) bad_trees.append(good_nexus.replace("2 spc02,", "spc02; 2,")) bad_trees.append(good_nexus.replace(";\n", "")) bad_trees.append(good_nexus.replace("Taxlabels", "Begin trees;")) bad_trees.append(good_nexus.replace("dmv", "emv")) bad_trees.append(good_nexus.replace("[", "")) bad_trees.append(good_nexus.replace("[", "").replace("]", "")) bad_trees.append(good_nexus.replace("=", "")) bad_trees.append(good_nexus.replace("Begin taxa", "Begin trees")) bad_trees.append(good_nexus.replace("Begin trees", "Begin taxa")) bad_trees.append(good_nexus.replace("[&dmv={0.5}]", "")) bad_trees.append(good_nexus.replace("[&dmv={0.1}]", "")) bad_trees.append(good_nexus.replace("[&dmv={0.1}]", "[&dmv={asdf}]")) bad_trees.append(good_nexus.replace(":1,2[&dmv", ":1, 2[&dmv")) bad_trees.append(good_nexus.replace(good_nwk, good_nwk + good_nwk)) good_generation_time = 5 for bad_tree in bad_trees: with self.assertRaises(ValueError): msprime.parse_starbeast(tree=bad_tree, generation_time=good_generation_time)
def test_12_species(self): with open("tests/data/species_trees/91genes_species_rev.tre") as f: good_tree = f.read() good_branch_length_units = "myr" good_generation_time = 5 parsed_tuple = msprime.parse_starbeast( tree=good_tree, branch_length_units=good_branch_length_units, generation_time=good_generation_time, ) self.assertEqual(len(parsed_tuple), 2) self.assertIs(type(parsed_tuple[0]), list) self.assertEqual(len(parsed_tuple[0]), 12) for pc in parsed_tuple[0]: species_name = pc.metadata["species_name"] self.assertTrue(species_name.startswith("spc")) self.assertTrue(species_name[3:].isnumeric()) self.assertIsInstance( pc, msprime.simulations.PopulationConfiguration) self.assertIsInstance(parsed_tuple[1], list) self.assertEqual(len(parsed_tuple[1]), 22) event_types = [msprime.simulations.MassMigration] event_types.append(msprime.simulations.PopulationParametersChange) for mm in parsed_tuple[1]: self.assertIn(type(mm), event_types)
def verify( self, tree, pop_size_map, nexus=None, branch_length_units="yr", generation_time=1, ): if nexus is None: nexus = make_nexus(tree, pop_size_map) population_configurations, demographic_events = msprime.parse_starbeast( nexus, generation_time, branch_length_units) self.assertEqual(len(population_configurations), tree.num_samples()) for pop_config in population_configurations: self.assertEqual(pop_config.growth_rate, 0) self.assertIn("species_name", pop_config.metadata) # Population IDs are mapped to leaves as they are encountered in a postorder # traversal. pop_id_map = {} k = 0 for u in tree.nodes(order="postorder"): if tree.is_leaf(u): pop_id_map[u] = k k += 1 else: pop_id_map[u] = pop_id_map[tree.left_child(u)] for u in tree.leaves(): pop_config = population_configurations[pop_id_map[u]] self.assertEqual(pop_config.initial_size, pop_size_map[u]) self.assertEqual(pop_config.growth_rate, 0) self.assertEqual(pop_config.metadata["species_name"], f"spc{u}") # We should have demographic events for every non-unary internal node, and # events should be output in increasing time order. j = 0 for node in [u for u in tree.nodes(order="timeasc")]: children = tree.children(node) if len(children) > 1: dest = pop_id_map[node] for child in children[1:]: event = demographic_events[j] j += 1 self.assertIsInstance(event, msprime.MassMigration) self.assertAlmostEqual(event.time, tree.time(node)) source = pop_id_map[child] self.assertEqual(event.source, source) self.assertEqual(event.dest, dest) event = demographic_events[j] j += 1 self.assertIsInstance(event, msprime.PopulationParametersChange) self.assertAlmostEqual(event.time, tree.time(node)) self.assertEqual(event.population, dest) self.assertEqual(event.growth_rate, None) self.assertEqual(event.initial_size, pop_size_map[node]) self.assertEqual(j, len(demographic_events))
def verify( self, tree, pop_size_map, nexus=None, branch_length_units="yr", generation_time=1, ): if nexus is None: nexus = make_nexus(tree, pop_size_map) spec = msprime.parse_starbeast(nexus, generation_time, branch_length_units) assert spec.num_populations == tree.num_samples() for pop in spec.populations: assert pop.growth_rate == 0 # Population IDs are mapped to leaves as they are encountered in a postorder # traversal. pop_id_map = {} k = 0 for u in tree.nodes(order="postorder"): if tree.is_leaf(u): pop_id_map[u] = k k += 1 else: pop_id_map[u] = pop_id_map[tree.left_child(u)] for u in tree.leaves(): pop = spec.populations[pop_id_map[u]] assert pop.initial_size == pop_size_map[u] assert pop.growth_rate == 0 assert pop.name == f"spc{u}" # We should have demographic events for every non-unary internal node, and # events should be output in increasing time order. j = 0 for node in [u for u in tree.nodes(order="timeasc")]: children = tree.children(node) if len(children) > 1: dest = pop_id_map[node] for child in children[1:]: event = spec.events[j] j += 1 assert isinstance(event, msprime.MassMigration) self.assertAlmostEqual(event.time, tree.time(node)) source = pop_id_map[child] assert event.source == source assert event.dest == dest event = spec.events[j] j += 1 assert isinstance(event, msprime.PopulationParametersChange) self.assertAlmostEqual(event.time, tree.time(node)) assert event.population == dest assert event.growth_rate is None assert event.initial_size == pop_size_map[node] assert j == len(spec.events)
def test_bad_parameter(self): with open("tests/data/species_trees/91genes_species_rev.tre") as f: good_tree = f.read() good_branch_length_units = "myr" for bad_branch_length_units in [-3, "asdf", ["myr"], "gen"]: with self.assertRaises(ValueError): msprime.parse_starbeast( tree=f.read(), branch_length_units=bad_branch_length_units, generation_time=5, ) for bad_generation_time in [-3, "sdf"]: with self.assertRaises(ValueError): msprime.parse_starbeast( tree=good_tree, branch_length_units=good_branch_length_units, generation_time=bad_generation_time, ) for bad_generation_time in [None, {}]: with self.assertRaises(TypeError): msprime.parse_starbeast( tree=good_tree, branch_length_units=good_branch_length_units, generation_time=bad_generation_time, )
def test_12_species(self): with open("tests/data/species_trees/91genes_species_rev.tre") as f: good_tree = f.read() good_branch_length_units = "myr" good_generation_time = 5 spec = msprime.parse_starbeast( tree=good_tree, branch_length_units=good_branch_length_units, generation_time=good_generation_time, ) assert len(spec.populations) == 12 for pop in spec.populations: species_name = pop.name assert species_name.startswith("spc") assert species_name[3:].isnumeric() assert len(spec.events) == 22 event_types = [msprime.demography.MassMigration] event_types.append(msprime.demography.PopulationParametersChange) for mm in spec.events: assert type(mm) in event_types