def test_is_symmetrical(self): unlabelled_leaf = RankTree(children=[]) assert unlabelled_leaf.is_symmetrical() three_leaf_asym = RankTree(children=[ unlabelled_leaf, RankTree(children=[unlabelled_leaf, unlabelled_leaf]), ]) assert not three_leaf_asym.is_symmetrical() six_leaf_sym = RankTree(children=[three_leaf_asym, three_leaf_asym]) assert six_leaf_sym.is_symmetrical()
def test_is_symmetrical(self): unlabelled_leaf = RankTree(children=[]) self.assertTrue(unlabelled_leaf.is_symmetrical()) three_leaf_asym = RankTree(children=[ unlabelled_leaf, RankTree(children=[unlabelled_leaf, unlabelled_leaf]), ]) self.assertFalse(three_leaf_asym.is_symmetrical()) six_leaf_sym = RankTree(children=[three_leaf_asym, three_leaf_asym]) self.assertTrue(six_leaf_sym.is_symmetrical())
def test_add_sibling_topologies_simple(self): a = RankTree(children=[], label="A") b = RankTree(children=[], label="B") ab = RankTree(children=[a, b]) a_counter = comb.TopologyCounter() a_counter["A"][a.rank()] = 1 self.assertEqual(a_counter, comb.TopologyCounter.from_sample("A")) b_counter = comb.TopologyCounter() b_counter["B"][b.rank()] = 1 self.assertEqual(b_counter, comb.TopologyCounter.from_sample("B")) partial_counter = comb.PartialTopologyCounter() partial_counter.add_sibling_topologies(a_counter) partial_counter.add_sibling_topologies(b_counter) expected = comb.TopologyCounter() expected["A"][a.rank()] = 1 expected["B"][b.rank()] = 1 expected["A", "B"][ab.rank()] = 1 joined_counter = partial_counter.join_all_combinations() self.assertEqual(joined_counter, expected)
def test_join_topologies(self): a = RankTree(children=[], label="A") b = RankTree(children=[], label="B") c = RankTree(children=[], label="C") a_tuple = (("A"), a.rank()) b_tuple = (("B"), b.rank()) c_tuple = (("C"), c.rank()) ab_tuple = (("A", "B"), RankTree(children=[a, b]).rank()) ac_tuple = (("A", "C"), RankTree(children=[a, c]).rank()) bc_tuple = (("B", "C"), RankTree(children=[b, c]).rank()) self.verify_join_topologies((a_tuple, b_tuple), (0, 0)) self.verify_join_topologies((b_tuple, a_tuple), (0, 0)) self.verify_join_topologies((b_tuple, c_tuple), (0, 0)) self.verify_join_topologies((a_tuple, b_tuple, c_tuple), (0, 0)) self.verify_join_topologies((a_tuple, bc_tuple), (1, 0)) self.verify_join_topologies((b_tuple, ac_tuple), (1, 1)) self.verify_join_topologies((c_tuple, ab_tuple), (1, 2))
def test_equal(self): unlabelled_leaf = RankTree(children=[]) self.assertEqual(unlabelled_leaf, unlabelled_leaf) self.assertTrue(unlabelled_leaf.shape_equal(unlabelled_leaf)) leaf_zero = RankTree(children=[], label=0) leaf_one = RankTree(children=[], label=1) leaf_two = RankTree(children=[], label=2) self.assertEqual(leaf_zero, leaf_zero) self.assertNotEqual(leaf_zero, leaf_one) self.assertTrue(leaf_zero.shape_equal(leaf_one)) tree1 = RankTree(children=[leaf_zero, leaf_one]) self.assertEqual(tree1, tree1) self.assertNotEqual(tree1, unlabelled_leaf) self.assertFalse(tree1.shape_equal(unlabelled_leaf)) tree2 = RankTree(children=[leaf_two, leaf_one]) self.assertNotEqual(tree1, tree2) self.assertTrue(tree1.shape_equal(tree2))
def test_equal(self): unlabelled_leaf = RankTree(children=[]) assert unlabelled_leaf == unlabelled_leaf assert unlabelled_leaf.shape_equal(unlabelled_leaf) leaf_zero = RankTree(children=[], label=0) leaf_one = RankTree(children=[], label=1) leaf_two = RankTree(children=[], label=2) assert leaf_zero == leaf_zero assert leaf_zero != leaf_one assert leaf_zero.shape_equal(leaf_one) tree1 = RankTree(children=[leaf_zero, leaf_one]) assert tree1 == tree1 assert tree1 != unlabelled_leaf assert not tree1.shape_equal(unlabelled_leaf) tree2 = RankTree(children=[leaf_two, leaf_one]) assert tree1 != tree2 assert tree1.shape_equal(tree2)
def test_add_sibling_topologies_polytomy(self): """ Goes through the topology-merging step at the root of this tree: | | +----+-----+----+ | | | | | | | | | | | +---+ | | | | | | | | | | A A B A C """ partial_counter = comb.PartialTopologyCounter() a = RankTree(children=[], label="A") c = RankTree(children=[], label="C") ac = RankTree(children=[a, c]) expected = collections.defaultdict(collections.Counter) a_counter = comb.TopologyCounter.from_sample("A") b_counter = comb.TopologyCounter.from_sample("B") ac_counter = comb.TopologyCounter() ac_counter["A"][a.rank()] = 1 ac_counter["C"][c.rank()] = 1 ac_counter["A", "C"][ac.rank()] = 1 partial_counter.add_sibling_topologies(a_counter) expected[("A",)] = collections.Counter({((("A",), (0, 0)),): 1}) self.assertEqual(partial_counter.partials, expected) partial_counter.add_sibling_topologies(a_counter) expected[("A",)][((("A",), (0, 0)),)] += 1 self.assertEqual(partial_counter.partials, expected) partial_counter.add_sibling_topologies(b_counter) expected[("B",)][((("B",), (0, 0)),)] = 1 expected[("A", "B")][((("A",), (0, 0)), (("B",), (0, 0)))] = 2 self.assertEqual(partial_counter.partials, expected) partial_counter.add_sibling_topologies(ac_counter) expected[("A",)][((("A",), (0, 0)),)] += 1 expected[("C",)][((("C",), (0, 0)),)] = 1 expected[("A", "B")][((("A",), (0, 0)), (("B",), (0, 0)))] += 1 expected[("A", "C")][((("A",), (0, 0)), (("C",), (0, 0)))] = 2 expected[("A", "C")][((("A", "C"), (0, 0)),)] = 1 expected[("B", "C")][((("B",), (0, 0)), (("C",), (0, 0)))] = 1 expected[("A", "B", "C")][ ((("A",), (0, 0)), (("B",), (0, 0)), (("C",), (0, 0))) ] = 2 expected[("A", "B", "C")][((("A", "C"), (0, 0)), (("B",), (0, 0)))] = 1 self.assertEqual(partial_counter.partials, expected) expected_topologies = comb.TopologyCounter() expected_topologies["A"][(0, 0)] = 3 expected_topologies["B"][(0, 0)] = 1 expected_topologies["C"][(0, 0)] = 1 expected_topologies["A", "B"][(0, 0)] = 3 expected_topologies["A", "C"][(0, 0)] = 3 expected_topologies["B", "C"][(0, 0)] = 1 expected_topologies["A", "B", "C"][(0, 0)] = 2 expected_topologies["A", "B", "C"][(1, 1)] = 1 joined_topologies = partial_counter.join_all_combinations() self.assertEqual(joined_topologies, expected_topologies)
def test_is_canonical(self): for n in range(7): for tree in RankTree.all_labelled_trees(n): self.assertTrue(tree.is_canonical()) shape_not_canonical = RankTree( children=[ RankTree(children=[], label=0), RankTree( children=[ RankTree( children=[ RankTree(children=[], label=1), RankTree(children=[], label=2), ] ), RankTree(children=[], label=3), ] ), ] ) self.assertFalse(shape_not_canonical.is_canonical()) labels_not_canonical = RankTree( children=[ RankTree(children=[], label=0), RankTree( children=[ RankTree( children=[ RankTree(children=[], label=2), RankTree(children=[], label=3), ] ), RankTree( children=[ RankTree(children=[], label=1), RankTree(children=[], label=4), ] ), ] ), ] ) self.assertFalse(labels_not_canonical.is_canonical())
def test_is_canonical(self): for n in range(7): for tree in RankTree.all_labelled_trees(n): assert tree.is_canonical() shape_not_canonical = RankTree(children=[ RankTree(children=[], label=0), RankTree(children=[ RankTree(children=[ RankTree(children=[], label=1), RankTree(children=[], label=2), ]), RankTree(children=[], label=3), ]), ]) assert not shape_not_canonical.is_canonical() labels_not_canonical = RankTree(children=[ RankTree(children=[], label=0), RankTree(children=[ RankTree(children=[ RankTree(children=[], label=2), RankTree(children=[], label=3), ]), RankTree(children=[ RankTree(children=[], label=1), RankTree(children=[], label=4), ]), ]), ]) assert not labels_not_canonical.is_canonical()