diff --git a/lib/Biodiverse/Indices/Phylogenetic.pm b/lib/Biodiverse/Indices/Phylogenetic.pm index d2795f88e..321196ac5 100644 --- a/lib/Biodiverse/Indices/Phylogenetic.pm +++ b/lib/Biodiverse/Indices/Phylogenetic.pm @@ -1783,14 +1783,14 @@ sub get_inverse_range_weighted_path_lengths { my %args = @_; my $tree = $args{tree_ref}; - my $node_ranges = $args{node_range}; - + \my %node_ranges = $args{node_range}; + \my %node_length_hash = $tree->get_node_length_hash; + my %range_weighted; - - foreach my $node ($tree->get_node_refs) { - my $name = $node->get_name; - next if !$node_ranges->{$name}; - $range_weighted{$name} = $node->get_length / $node_ranges->{$name}; + + foreach my $name (keys %node_length_hash) { + next if !$node_ranges{$name}; + $range_weighted{$name} = $node_length_hash{$name} / $node_ranges{$name}; } my %results = (inverse_range_weighted_node_lengths => \%range_weighted); diff --git a/lib/Biodiverse/Indices/PhylogeneticRelative.pm b/lib/Biodiverse/Indices/PhylogeneticRelative.pm index 53b89dd85..31dd51712 100644 --- a/lib/Biodiverse/Indices/PhylogeneticRelative.pm +++ b/lib/Biodiverse/Indices/PhylogeneticRelative.pm @@ -656,21 +656,20 @@ sub get_metadata_get_trimmed_tree_eq_branch_lengths_node_length_hash { return $metadata_class->new(\%metadata); } -# should just be a wrapper around Tree::get_node_length_hash sub get_trimmed_tree_eq_branch_lengths_node_length_hash { my $self = shift; my %args = @_; my $tree_ref = $args{TREE_REF_EQUALISED_BRANCHES_TRIMMED} // croak 'Missing TREE_REF_EQUALISED_BRANCHES_TRIMMED arg'; - my $node_hash = $tree_ref->get_node_hash; - - my (%len_hash, $nonzero_length); - foreach my $node_name (keys %$node_hash) { - my $node_ref = $node_hash->{$node_name}; - my $length = $node_ref->get_length; - $len_hash{$node_name} = $length; - $nonzero_length ||= $length; + + \my %len_hash = $tree_ref->get_node_length_hash; + + my $nonzero_length; + + foreach my $len (values %len_hash) { + $nonzero_length ||= $len; + last if $len; } my %results = ( @@ -737,14 +736,14 @@ sub get_trimmed_tree_range_inverse_hash_nonzero_len { my %args = @_; my $tree = $args{trimmed_tree}; - my $node_ranges = $args{node_range}; + \my %node_ranges = $args{node_range}; + \my %length_hash = $tree->get_node_length_hash; my %range_weighted; - foreach my $name (keys %$node_ranges) { - my $range = $node_ranges->{$name} || next; - my $numerator = $tree->get_node_ref_aa($name)->get_length ? 1 : 0; - $range_weighted{$name} = $numerator / $range; + foreach my $name (keys %node_ranges) { + my $range = $node_ranges{$name} || next; + $range_weighted{$name} = ($length_hash{$name} ? 1 : 0) / $range; } my %results = (trimmed_tree_range_inverse_hash_nonzero_len => \%range_weighted); diff --git a/lib/Biodiverse/Tree.pm b/lib/Biodiverse/Tree.pm index 3d20b53ba..be6a47a69 100644 --- a/lib/Biodiverse/Tree.pm +++ b/lib/Biodiverse/Tree.pm @@ -3095,20 +3095,17 @@ sub clone_tree_with_equalised_branch_lengths { my $name = $args{name} // ( $self->get_param('NAME') . ' EQ' ); - my $non_zero_len = $args{node_length}; + my $non_zero_len = $args{node_length} + // ($self->get_total_tree_length / ( $self->get_nonzero_length_count || 1 )); - if ( !defined $non_zero_len ) { - # my $non_zero_node_count = grep { $_->get_length } $self->get_node_refs; - # this caches - my $non_zero_node_count = $self->get_nonzero_length_count; - $non_zero_len = - $self->get_total_tree_length / ( $non_zero_node_count || 1 ); - } + \my %orig_node_length_hash = $self->get_node_length_hash; my $new_tree = $self->clone_without_caches; + \my %new_node_hash = $new_tree->get_node_hash; - foreach my $node ( $new_tree->get_node_refs ) { - $node->set_length_aa ( $node->get_length ? $non_zero_len : 0 ); + foreach my $name ( keys %new_node_hash ) { + my $node = $new_node_hash{$name}; + $node->set_length_aa ( $orig_node_length_hash{$name} ? $non_zero_len : 0 ); } $new_tree->rename( new_name => $name );