def test_sampleContext(self): self.assertEqual( alg.sampleContext( alg.factorByField( sp.p_tree.parse("(B|a,(A|b,C|b,E|b),D|c);").tree, field=2), keep=[], maxTips=1, ), sp.Node(kids=[ sp.Node(label="B|a", factor="a"), sp.Node(label="A|b", factor="b"), sp.Node(label="D|c", factor="c"), ]), ) self.assertEqual( alg.sampleContext( alg.factorByField( sp.p_tree.parse("(B|a,(A|b,C|b,E|b),D|c);").tree, field=2), keep=[], maxTips=2, ), sp.Node(kids=[ sp.Node(label="B|a", factor="a"), sp.Node(kids=[ sp.Node(label="A|b", factor="b"), sp.Node(label="C|b", factor="b"), ]), sp.Node(label="D|c", factor="c"), ]), )
def test_node(self): self.assertEqual( sp.p_node.parse("(A[foo=boo,bar=baz]:0.42)Root"), sp.Node( kids=[ sp.Node(label="A", form={ "foo": "boo", "bar": "baz" }, length=0.42) ], label="Root", ), )
def test_newick(self): self.assertEqual( sp.p_tree.parse("(A[foo=boo]:0.42)Root;").tree, sp.Node( kids=[sp.Node(label="A", form={"foo": "boo"}, length=0.42)], label="Root", ), ) self.assertEqual( sp.p_tree.parse("(B,(A,C,E),D);").tree, sp.Node(kids=[ sp.Node(label="B"), sp.Node(kids=[ sp.Node(label="A"), sp.Node(label="C"), sp.Node(label="E"), ]), sp.Node(label="D"), ]), )
def test_treemap(self): def _lower(x): if x.label: x.label = x.label.lower() return x self.assertEqual( alg.treemap(sp.p_tree.parse("(B,(A,C,E),D);").tree, _lower), sp.Node(kids=[ sp.Node(label="b"), sp.Node(kids=[ sp.Node(label="a"), sp.Node(label="c"), sp.Node(label="e"), ]), sp.Node(label="d"), ]), )
def test_factorByLabel(self): def _fun(name): try: return name.split("|")[1] except: return None self.assertEqual( alg.factorByLabel( sp.p_tree.parse("(B|a,(A|b,C|b,E|b),D|c);").tree, _fun), sp.Node(kids=[ sp.Node(label="B|a", factor="a"), sp.Node(kids=[ sp.Node(label="A|b", factor="b"), sp.Node(label="C|b", factor="b"), sp.Node(label="E|b", factor="b"), ]), sp.Node(label="D|c", factor="c"), ]), )
def test_leaf(self): self.assertEqual( sp.p_leaf.parse("A[foo=boo]:0.42"), sp.Node(label="A", form={"foo": "boo"}, length=0.42), )