Skip to content

Commit

Permalink
Tree plotting: use caching for range weighted and eq length plots
Browse files Browse the repository at this point in the history
Makes little difference for small trees
but will matter for large ones.
  • Loading branch information
shawnlaffan committed Jan 9, 2024
1 parent 49dca9b commit ff5266a
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions lib/Biodiverse/GUI/Dendrogram.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use Biodiverse::TreeNode;
use Biodiverse::Utilities qw/sort_list_with_tree_names_aa/;
use Sort::Key qw/rnkeysort/;

use parent qw/Biodiverse::Common::Caching/;

##########################################################
# Rendering constants
##########################################################
Expand Down Expand Up @@ -2112,20 +2114,30 @@ sub set_plot_mode {
$self->{dist_to_root_func} = sub {$_[0]->get_depth + 1};
}
elsif ($plot_mode =~ 'equal_length|range_weighted') {
# create a clone and wrap the methods
my $tree = $self->get_parent_tab->get_current_tree;
my $alt_tree = $tree; # can be proecssed in both if conditions below
if ($plot_mode =~ 'equal_length') {
$alt_tree = $alt_tree->clone_tree_with_equalised_branch_lengths;
}
if ($plot_mode =~ 'range_weighted') {
my $bd = $self->get_parent_tab->get_base_ref;
$alt_tree = $alt_tree->clone_without_caches;
NODE:
foreach my $node ( rnkeysort {$_->get_depth} $alt_tree->get_node_refs ) {
my $range = $node->get_node_range( basedata_ref => $bd );
$node->set_length_aa( $range ? $node->get_length / $range : 0 );
# Create an alternate tree with the chosen properties.
# Use a cache for speed.
# Basedata will not change for lifetime of object
# as GUI does not support in-place deletions.
my $cache_key = "tree_for_plot_mode_${plot_mode}";
my $alt_tree = $self->get_cached_value ($cache_key);

if (!defined $alt_tree) {
my $tree = $self->get_parent_tab->get_current_tree;
# alt_tree can be processed in both if-conditions below
$alt_tree = $tree;
if ($plot_mode =~ 'equal_length') {
$alt_tree = $alt_tree->clone_tree_with_equalised_branch_lengths;
}
if ($plot_mode =~ 'range_weighted') {
my $bd = $self->get_parent_tab->get_base_ref;
$alt_tree = $alt_tree->clone_without_caches;
NODE:
foreach my $node (rnkeysort {$_->get_depth} $alt_tree->get_node_refs) {
my $range = $node->get_node_range(basedata_ref => $bd);
$node->set_length_aa($range ? $node->get_length / $range : 0);
}
}
$self->set_cached_value ($cache_key => $alt_tree);
}
# We are passed nodes from the original tree, so use their names to
# look up the ref in the alt tree.
Expand Down

0 comments on commit ff5266a

Please sign in to comment.