Skip to content

Commit 586bc1c

Browse files
authored
Merge pull request #1046 from shawnlaffan/gui_range_convex_hull_union
This plots fewer lines and thus generates a less busy plot for large data sets.
2 parents 3138783 + 8ec0e9d commit 586bc1c

File tree

4 files changed

+102
-19
lines changed

4 files changed

+102
-19
lines changed

lib/Biodiverse/GUI/Tabs/Labels.pm

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ sub get_tree_menu_items {
399399
qw /plot_branches_by
400400
highlight_groups_on_map
401401
highlight_groups_on_map_convex_hull
402+
highlight_groups_on_map_convex_hull_union
402403
highlight_groups_on_map_circumcircle
403404
highlight_paths_on_tree
404405
separator
@@ -913,6 +914,25 @@ sub get_highlight_label_range_convex_hulls {
913914
$_[0]->{highlight_label_range_convex_hulls};
914915
}
915916

917+
sub set_highlight_label_range_convex_hull_union {
918+
my ($self, $value) = @_;
919+
920+
$self->{highlight_label_range_convex_hull_union} = !!$value;
921+
922+
return;
923+
}
924+
925+
sub toggle_highlight_label_range_convex_hull_union {
926+
my ($self, $value) = @_;
927+
928+
$self->{highlight_label_range_convex_hull_union}
929+
= !$self->{highlight_label_range_convex_hull_union};
930+
}
931+
932+
sub get_highlight_label_range_convex_hull_union {
933+
$_[0]->{highlight_label_range_convex_hull_union};
934+
}
935+
916936
sub set_highlight_label_range_circumcircles {
917937
my ($self, $value) = @_;
918938

@@ -963,6 +983,49 @@ sub highlight_label_range_convex_hulls {
963983
}
964984
}
965985
986+
sub highlight_label_range_convex_hull_union {
987+
my ($self, $node) = @_;
988+
989+
return if !$self->get_highlight_label_range_convex_hull_union;
990+
991+
my $terminal_elements = $node->get_terminal_elements;
992+
993+
my $bd = $self->get_base_ref;
994+
my $label_hash = $bd->get_labels_ref->get_element_hash;
995+
996+
# clear existing
997+
$self->{grid}->clear_range_convex_hulls;
998+
999+
my $cache = $bd->get_cached_value_dor_set_default_href('LABEL_RANGE_CONVEX_HULL_VERTICES');
1000+
1001+
# Cache on list of terminal names to avoid issues with trees
1002+
# that have similarly named nodes with different terminals.
1003+
my $cache_key = $node->is_terminal_node ? $node->get_name : $node->get_terminal_element_names_sha256;
1004+
1005+
my $hull_union = $cache->{$cache_key};
1006+
if (!$hull_union) {
1007+
# could climb up the tree if this takes too long
1008+
foreach my $label (keys %$terminal_elements) {
1009+
next LABEL if !exists $label_hash->{$label};
1010+
my $hull = $bd->get_label_range_convex_hull(label => $label);
1011+
$hull_union = $hull_union ? $hull_union->Union ($hull) : $hull;
1012+
}
1013+
$cache->{$cache_key} = $hull_union;
1014+
}
1015+
1016+
# avoid plotting empties
1017+
if (defined $hull_union) {
1018+
$self->{grid}->set_overlay(
1019+
type => 'polyline',
1020+
cb_target => 'range_convex_hulls',
1021+
plot_on_top => 1,
1022+
data => scalar $hull_union->GetPoints(0, 0),
1023+
colour => COLOUR_BLACK,
1024+
alpha => 0.5,
1025+
);
1026+
}
1027+
}
1028+
9661029
sub highlight_label_range_circumcircles {
9671030
my ($self, $node) = @_;
9681031
@@ -1583,11 +1646,11 @@ sub on_phylogeny_highlight {
15831646

15841647
$self->highlight_label_range_marks($node);
15851648
$self->highlight_label_range_convex_hulls($node);
1649+
$self->highlight_label_range_convex_hull_union($node);
15861650
$self->highlight_label_range_circumcircles($node);
15871651

1588-
if (defined $node) {
1589-
my $text = 'Node: ' . $node->get_name;
1590-
$self->get_xmlpage_object('label_VL_tree')->set_markup($text);
1652+
if (my $widget = $self->get_xmlpage_object('label_VL_tree')) {
1653+
$widget->set_markup('Node: ' . $node->get_name);
15911654
}
15921655

15931656
return;

lib/Biodiverse/GUI/Tabs/Spatial.pm

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ sub get_tree_menu_items {
418418
separator
419419
highlight_groups_on_map
420420
highlight_groups_on_map_convex_hull
421+
highlight_groups_on_map_convex_hull_union
421422
highlight_groups_on_map_circumcircle
422423
highlight_paths_on_tree
423424
separator
@@ -1198,22 +1199,6 @@ sub on_phylogeny_hover {
11981199
return;
11991200
}
12001201

1201-
# many other phylogeny methods are given in Labels.pm
1202-
# Called by dendrogram when user hovers over a node
1203-
sub on_phylogeny_highlight {
1204-
my ($self, $node) = @_;
1205-
1206-
return if !$node;
1207-
1208-
return if !$self->do_canvas_hover_flag;
1209-
1210-
$self->highlight_label_range_marks($node);
1211-
$self->highlight_label_range_convex_hulls($node);
1212-
$self->highlight_label_range_circumcircles($node);
1213-
1214-
return;
1215-
}
1216-
12171202
sub on_phylogeny_click {
12181203
my ($self, $node) = @_;
12191204

lib/Biodiverse/GUI/Tabs/Tab.pm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,21 @@ EOT
13321332
$self->get_highlight_label_range_convex_hulls;
13331333
},
13341334
},
1335+
highlight_groups_on_map_convex_hull_union => {
1336+
type => 'Gtk3::CheckMenuItem',
1337+
label => 'Highlight groups on map with range convex hull union',
1338+
tooltip => 'When hovering the mouse over a tree branch, '
1339+
. 'plot the union of the convex hulls of the range of each subtending label.',
1340+
event => 'toggled',
1341+
callback => sub {
1342+
my ($self, $widget) = @_;
1343+
$self->set_highlight_label_range_convex_hull_union($widget->get_active);
1344+
},
1345+
active => sub {
1346+
my ($self) = @_;
1347+
$self->get_highlight_label_range_convex_hull_union;
1348+
},
1349+
},
13351350
highlight_groups_on_map_circumcircle => {
13361351
type => 'Gtk3::CheckMenuItem',
13371352
label => 'Highlight groups on map with range circumcircles',

lib/Biodiverse/TreeNode.pm

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,26 @@ sub get_terminal_element_count {
10171017
return scalar keys %$hash;
10181018
}
10191019

1020+
sub get_terminal_element_names_sha256 {
1021+
my $self = shift;
1022+
state $cache_key = 'terminal_element_names_sha256';
1023+
1024+
if (my $cached = $self->get_cached_value ($cache_key)) {
1025+
return $cached;
1026+
}
1027+
1028+
my $terminals = $self->get_terminal_elements;
1029+
my @names = sort keys %$terminals;
1030+
1031+
use Digest::SHA qw/sha256_hex/;
1032+
# Join using ascii file separator control character,
1033+
# which is also the default value for $;
1034+
my $sha = sha256_hex join "\034", @names;
1035+
$self->set_cached_value ($cache_key => $sha);
1036+
1037+
return $sha;
1038+
}
1039+
10201040
sub get_all_named_descendants {
10211041
my $self = shift;
10221042
my %args = (cache => 1, @_); # cache unless told otherwise

0 commit comments

Comments
 (0)