Exemple #1
0
 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 pytest.raises(ValueError):
             species_trees.parse_starbeast(
                 tree=bad_tree, generation_time=good_generation_time)
    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)
        demography = species_trees.parse_starbeast(nexus, generation_time,
                                                   branch_length_units)
        assert demography.num_populations == tree.num_nodes
        for pop in demography.populations:
            assert pop.growth_rate == 0

        # Population IDs are mapped to leaves first, and then to the internal nodes
        # in postorder
        pop_id_map = {}
        k = 0
        for u in tree.leaves():
            pop_id_map[u] = k
            k += 1

        for u in tree.nodes(order="postorder"):
            if tree.is_internal(u):
                pop_id_map[u] = k
                k += 1

        for u in tree.nodes():
            pop = demography.populations[pop_id_map[u]]
            assert pop.growth_rate == 0
            assert pop.initial_size == pop_size_map[u]
            if tree.is_leaf(u):
                # Note: we're assuming the default newick here in tskit that labels
                # nodes as their id + 1.
                assert pop.name == f"spc{u}"
            else:
                assert pop.name == f"pop_{pop_id_map[u]}"

        # We should have demographic events for every internal node, and
        # events should be output in increasing time order.
        j = 0
        for node in tree.nodes(order="timeasc"):
            children = tree.children(node)
            dest = pop_id_map[node]
            for child in children:
                event = demography.events[j]
                j += 1
                assert isinstance(event, msprime.MassMigration)
                assert event.time == pytest.approx(tree.time(node))
                source = pop_id_map[child]
                assert event.source == source
                assert event.dest == dest

        assert j == len(demography.events)
Exemple #3
0
 def test_bad_parameter(self):
     with open("tests/data/species_trees/91genes_species_rev.tre") as f:
         good_tree = f.read()
         good_time_units = "myr"
         for bad_time_units in [-3, "asdf", ["myr"], "gen"]:
             with pytest.raises(ValueError):
                 species_trees.parse_starbeast(
                     tree=f.read(),
                     time_units=bad_time_units,
                     generation_time=5,
                 )
         for bad_generation_time in [-3, "sdf"]:
             with pytest.raises(ValueError):
                 species_trees.parse_starbeast(
                     tree=good_tree,
                     time_units=good_time_units,
                     generation_time=bad_generation_time,
                 )
         for bad_generation_time in [None, {}]:
             with pytest.raises(TypeError):
                 species_trees.parse_starbeast(
                     tree=good_tree,
                     time_units=good_time_units,
                     generation_time=bad_generation_time,
                 )
Exemple #4
0
 def test_12_species(self):
     with open("tests/data/species_trees/91genes_species_rev.tre") as f:
         good_tree = f.read()
         good_time_units = "myr"
         good_generation_time = 5
         spec = species_trees.parse_starbeast(
             tree=good_tree,
             time_units=good_time_units,
             generation_time=good_generation_time,
         )
         assert len(spec.populations) == 23
         for pop in spec.populations[:12]:
             species_name = pop.name
             assert species_name.startswith("spc")
             assert species_name[3:].isnumeric()
         assert len(spec.events) == 11
         for mm in spec.events:
             assert isinstance(mm, msprime.PopulationSplit)
Exemple #5
0
 def verify_non_ultrametric(self, tree, pop_size_map):
     nexus = make_nexus(tree, pop_size_map)
     with pytest.raises(ValueError):
         species_trees.parse_starbeast(nexus, 10)