def test_neighbor_joining(): """ Compare the results of `neighbor_join()` with a known tree. """ dist = np.array([ [0, 5, 4, 7, 6, 8], [5, 0, 7, 10, 9, 11], [4, 7, 0, 7, 6, 8], [7, 10, 7, 0, 5, 9], [6, 9, 6, 5, 0, 8], [8, 11, 8, 9, 8, 0], ]) ref_tree = phylo.Tree( phylo.TreeNode([ phylo.TreeNode([ phylo.TreeNode([ phylo.TreeNode(index=0), phylo.TreeNode(index=1), ], [1, 4]), phylo.TreeNode(index=2), ], [1, 2]), phylo.TreeNode([ phylo.TreeNode(index=3), phylo.TreeNode(index=4), ], [3, 2]), phylo.TreeNode(index=5), ], [1, 1, 5])) test_tree = phylo.neighbor_joining(dist) assert test_tree == ref_tree
def test_clustalo_tree(sequences): leaves = [phylo.TreeNode(index=i) for i in range(len(sequences))] inter1 = phylo.TreeNode([leaves[0], leaves[1]], [1.0, 1.0]) inter2 = phylo.TreeNode([leaves[2], leaves[3]], [2.5, 2.5]) root = phylo.TreeNode([inter1, inter2], [3.5, 2]) tree = phylo.Tree(root) # You cannot simultaneously set and get a tree in ClustalOmega # -> Test whether both is possible in separate calls app = ClustalOmegaApp(sequences) app.set_guide_tree(tree) app.start() app.join() app = ClustalOmegaApp(sequences) app.start() app.join() assert app.get_guide_tree() is not None
def test_equality(tree): """ Assert that equal trees equal each other, and non-equal trees do not equal each other. """ assert tree == tree.copy() # Order of children is not important assert tree == phylo.Tree( phylo.TreeNode( [tree.root.children[1].copy(), tree.root.children[0].copy()], [tree.root.children[1].distance, tree.root.children[0].distance])) # Different distance -> Unequal tree assert tree != phylo.Tree( phylo.TreeNode( [tree.root.children[0].copy(), tree.root.children[1].copy()], [tree.root.children[0].distance, 42])) # Additional node -> Unequal tree assert tree != phylo.Tree( phylo.TreeNode([ tree.root.children[0].copy(), tree.root.children[1].copy(), phylo.TreeNode(index=len(tree)) ], [ tree.root.children[0].distance, tree.root.children[1].distance, 42 ]))
# When a :class:`TreeNode` is created, you have to provide either child # nodes and their distances to this node (intermediate node) or a # reference index (leaf node). # This reference index is dependent on the context and can refer to # anything: sequences, organisms, etc. # # The childs and the reference index cannot be changed after object # creation. # Also the parent can only be set once - when the node is used as child # in the creation of a new node. import biotite.sequence.phylo as phylo # The reference objects fruits = ["Apple", "Pear", "Orange", "Lemon", "Banana"] # Create nodes apple = phylo.TreeNode(index=fruits.index("Apple")) pear = phylo.TreeNode(index=fruits.index("Pear")) orange = phylo.TreeNode(index=fruits.index("Orange")) lemon = phylo.TreeNode(index=fruits.index("Lemon")) banana = phylo.TreeNode(index=fruits.index("Banana")) intermediate1 = phylo.TreeNode(children=(apple, pear), distances=(2.0, 2.0)) intermediate2 = phylo.TreeNode((orange, lemon), (1.0, 1.0)) intermediate3 = phylo.TreeNode((intermediate2, banana), (2.0, 3.0)) root = phylo.TreeNode((intermediate1, intermediate3), (2.0, 1.0)) # Create tree from root node tree = phylo.Tree(root=root) # Trees can be converted into Newick notation print("Tree:", tree.to_newick(labels=fruits)) # Distances can be omitted print("Tree w/o distances:", tree.to_newick(labels=fruits, include_distance=False))
def test_immutability(): node = phylo.TreeNode(index=0) # Attributes are not writable with pytest.raises(AttributeError): node.children = None with pytest.raises(AttributeError): node.parent = None with pytest.raises(AttributeError): node.index = None # A root node cannot be child node1 = phylo.TreeNode(index=0) node2 = phylo.TreeNode(index=1) node1.as_root() with pytest.raises(phylo.TreeError): phylo.TreeNode([node1, node2], [0, 0]) # A child node cannot be root node1 = phylo.TreeNode(index=0) node2 = phylo.TreeNode(index=1) phylo.TreeNode([node1, node2], [0, 0]) with pytest.raises(phylo.TreeError): node1.as_root() # A node cannot be child of a two nodes node1 = phylo.TreeNode(index=0) node2 = phylo.TreeNode(index=1) phylo.TreeNode([node1, node2], [0, 0]) with pytest.raises(phylo.TreeError): phylo.TreeNode([node1, node2], [0, 0]) # Tree cannot be constructed from child nodes node1 = phylo.TreeNode(index=0) node2 = phylo.TreeNode(index=0) # node1 and node2 have now a parent phylo.TreeNode([node1, node2], [0, 0]) with pytest.raises(phylo.TreeError): phylo.Tree(node1)