From df5eb80050c711f2dd6643bc395d26a931eff789 Mon Sep 17 00:00:00 2001 From: Yan Wong Date: Thu, 14 Nov 2024 10:14:59 +0000 Subject: [PATCH] Add textual mouseover descriptions of collapsed nodes I realised that the numbers can be somewhat cryptic, and it's trivial to add helpful text that appears on mouseover, e.g. "This polytomy has 3 additional branches, leading to a total of 14 descendant samples" --- python/tests/data/svg/tree_poly_tracked.svg | 20 ++++++++-- .../data/svg/tree_poly_tracked_collapse.svg | 25 ++++++++++--- .../tests/data/svg/tree_simple_collapsed.svg | 10 ++++- .../data/svg/tree_subtrees_with_collapsed.svg | 5 ++- python/tskit/drawing.py | 37 ++++++++++++------- 5 files changed, 71 insertions(+), 26 deletions(-) diff --git a/python/tests/data/svg/tree_poly_tracked.svg b/python/tests/data/svg/tree_poly_tracked.svg index 517d508e21..fdf6b8712c 100644 --- a/python/tests/data/svg/tree_poly_tracked.svg +++ b/python/tests/data/svg/tree_poly_tracked.svg @@ -15,7 +15,10 @@ - +3/𝟑 + + +3/𝟑 + This polytomy has 3 additional branches, leading to a total of 3 descendant samples + @@ -23,7 +26,10 @@ - +3/𝟑 + + +3/𝟑 + This polytomy has 3 additional branches, leading to a total of 3 descendant samples + @@ -34,7 +40,10 @@ - +2 + + +2 + A collapsed non-sample node with 2 descendant samples in this tree + 36 @@ -93,7 +102,10 @@ - +14/𝟐 + + +14/𝟐 + This polytomy has 2 additional branches, leading to a total of 14 descendant samples + 41 diff --git a/python/tests/data/svg/tree_poly_tracked_collapse.svg b/python/tests/data/svg/tree_poly_tracked_collapse.svg index 437c2c355b..895daa2ac1 100644 --- a/python/tests/data/svg/tree_poly_tracked_collapse.svg +++ b/python/tests/data/svg/tree_poly_tracked_collapse.svg @@ -15,7 +15,10 @@ - +3/𝟑 + + +3/𝟑 + This polytomy has 3 additional branches, leading to a total of 3 descendant samples + @@ -23,7 +26,10 @@ - +3/𝟑 + + +3/𝟑 + This polytomy has 3 additional branches, leading to a total of 3 descendant samples + @@ -34,7 +40,10 @@ - +2 + + +2 + A collapsed non-sample node with 2 descendant samples in this tree + 36 @@ -71,7 +80,10 @@ - +3 + + +3 + A collapsed non-sample node with 3 descendant samples in this tree + 39 @@ -80,7 +92,10 @@ - +14/𝟐 + + +14/𝟐 + This polytomy has 2 additional branches, leading to a total of 14 descendant samples + 41 diff --git a/python/tests/data/svg/tree_simple_collapsed.svg b/python/tests/data/svg/tree_simple_collapsed.svg index 67446bb6dd..7bc5d31f7f 100644 --- a/python/tests/data/svg/tree_simple_collapsed.svg +++ b/python/tests/data/svg/tree_simple_collapsed.svg @@ -11,7 +11,10 @@ - +2 + + +2 + A collapsed non-sample node with 2 descendant samples in this tree + 8 @@ -37,7 +40,10 @@ - +4 + + +4 + A collapsed non-sample node with 4 descendant samples in this tree + 13 diff --git a/python/tests/data/svg/tree_subtrees_with_collapsed.svg b/python/tests/data/svg/tree_subtrees_with_collapsed.svg index 5bd9d76b74..afcae706b7 100644 --- a/python/tests/data/svg/tree_subtrees_with_collapsed.svg +++ b/python/tests/data/svg/tree_subtrees_with_collapsed.svg @@ -11,7 +11,10 @@ - +2 + + +2 + A collapsed non-sample node with 2 descendant samples in this tree + 16 diff --git a/python/tskit/drawing.py b/python/tskit/drawing.py index 7c6c591bfa..381029161a 100644 --- a/python/tskit/drawing.py +++ b/python/tskit/drawing.py @@ -1990,15 +1990,20 @@ def draw_tree(self): end=(x2, 0), ) ) - poly.add( - dwg.text( - f"+{info.num_samples}/{bold_integer(info.num_branches)}", - font_style="italic", - x=[rnd(x2)], - dy=[rnd(-self.text_height / 10)], # make the plus sign line up - text_anchor="end", + label = dwg.text( + f"+{info.num_samples}/{bold_integer(info.num_branches)}", + font_style="italic", + x=[rnd(x2)], + dy=[rnd(-self.text_height / 10)], # make the plus sign line up + text_anchor="end", + ) + label.set_desc( + title=( + f"This polytomy has {info.num_branches} additional branches, " + f"leading to a total of {info.num_samples} descendant samples" ) ) + poly.add(label) curr_svg_group.add(poly) # Add edge above node first => on layer underneath anything else @@ -2092,14 +2097,18 @@ def draw_tree(self): node_lab_attr["transform"] = self.text_transform("above") else: if multi_samples is not None: - curr_svg_group.add( - dwg.text( - text=f"+{multi_samples}", - transform=self.text_transform("below", dy=1), - font_style="italic", - class_="lab summary", - ) + label = dwg.text( + text=f"+{multi_samples}", + transform=self.text_transform("below", dy=1), + font_style="italic", + class_="lab summary", + ) + title = ( + f"A collapsed {'sample' if tree.is_sample(u) else 'non-sample'} " + f"node with {multi_samples} descendant samples in this tree" ) + label.set_desc(title=title) + curr_svg_group.add(label) if u == left_child[tree.parent(u)]: add_class(node_lab_attr, "lft") node_lab_attr["transform"] = self.text_transform("above_left")