|
3 | 3 |
|
4 | 4 | import os |
5 | 5 | import os.path as op |
| 6 | +import tempfile |
6 | 7 |
|
| 8 | +import matplotlib |
7 | 9 |
|
8 | | -def disabled_test_tree_main(): |
| 10 | +matplotlib.use("Agg") |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | + |
| 15 | +def test_tree_main(tmp_path): |
| 16 | + """Render the built-in demo tree to a PNG and check the file is created.""" |
9 | 17 | from jcvi.apps.base import cleanup |
10 | 18 | from jcvi.graphics.tree import main as tree_main |
11 | 19 |
|
12 | | - demo = "demo.png" |
| 20 | + demo = str(tmp_path / "demo.png") |
13 | 21 | cleanup(demo) |
14 | 22 |
|
| 23 | + os.chdir(tmp_path) |
15 | 24 | tree_main(["demo", "--format", "png"]) |
16 | | - assert op.exists(demo) |
17 | | - cleanup(demo) |
| 25 | + assert op.exists(str(tmp_path / "demo.png")) |
| 26 | + |
| 27 | + |
| 28 | +def test_tree_parse(): |
| 29 | + """Test that the ete4 Tree can be created from a newick string.""" |
| 30 | + from ete4 import Tree |
| 31 | + |
| 32 | + newick = "(((A:0.1,B:0.2)90:0.3,(C:0.15,D:0.25)80:0.2)100:0.05,E:0.4);" |
| 33 | + t = Tree(newick) |
| 34 | + assert t is not None |
| 35 | + leaves = list(t.leaf_names()) |
| 36 | + assert sorted(leaves) == ["A", "B", "C", "D", "E"] |
| 37 | + |
| 38 | + |
| 39 | +def test_tree_parse_tree_function(): |
| 40 | + """Test parse_tree handles a plain newick file (no HPD).""" |
| 41 | + from jcvi.graphics.tree import parse_tree |
| 42 | + |
| 43 | + newick = "(((A:0.1,B:0.2)90:0.3,(C:0.15,D:0.25)80:0.2)100:0.05,E:0.4);" |
| 44 | + with tempfile.NamedTemporaryFile(mode="w", suffix=".nwk", delete=False) as f: |
| 45 | + f.write(newick) |
| 46 | + fname = f.name |
| 47 | + try: |
| 48 | + t, hpd = parse_tree(fname) |
| 49 | + assert hpd is None |
| 50 | + leaves = list(t.leaf_names()) |
| 51 | + assert sorted(leaves) == ["A", "B", "C", "D", "E"] |
| 52 | + finally: |
| 53 | + os.unlink(fname) |
| 54 | + |
| 55 | + |
| 56 | +def test_tree_truncate_name(): |
| 57 | + """Test the truncate_name helper function.""" |
| 58 | + from jcvi.graphics.tree import truncate_name |
| 59 | + |
| 60 | + assert truncate_name("ABCDEF", rule=None) == "ABCDEF" |
| 61 | + assert truncate_name("ABCDEF", rule="head3") == "DEF" |
| 62 | + assert truncate_name("ABCDEF", rule="ohead3") == "ABC" |
| 63 | + assert truncate_name("ABCDEF", rule="tail2") == "ABCD" |
| 64 | + assert truncate_name("ABCDEF", rule="otail2") == "EF" |
| 65 | + |
| 66 | + |
| 67 | +def test_tree_draw(tmp_path): |
| 68 | + """Test that draw_tree runs without error on a simple tree.""" |
| 69 | + import matplotlib.pyplot as plt |
| 70 | + from ete4 import Tree |
| 71 | + |
| 72 | + from jcvi.graphics.tree import draw_tree |
| 73 | + |
| 74 | + newick = "(((A:0.1,B:0.2)0.9:0.3,(C:0.15,D:0.25)0.8:0.2)1.0:0.05,E:0.4);" |
| 75 | + t = Tree(newick) |
| 76 | + |
| 77 | + fig = plt.figure(figsize=(8, 6)) |
| 78 | + ax = fig.add_axes([0, 0, 1, 1]) |
| 79 | + draw_tree(ax, t) |
| 80 | + out = str(tmp_path / "tree_draw.png") |
| 81 | + fig.savefig(out) |
| 82 | + plt.close(fig) |
| 83 | + assert op.exists(out) |
0 commit comments