From 1e856b97e14ea1b1d252fe342ec611bccebc4cb2 Mon Sep 17 00:00:00 2001 From: shawnlaffan Date: Tue, 19 Sep 2023 19:51:56 +1000 Subject: [PATCH] treenode to R phylo: handle root length properly --- lib/Biodiverse/TreeNode.pm | 5 +++-- t/13-Tree.t | 9 ++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/Biodiverse/TreeNode.pm b/lib/Biodiverse/TreeNode.pm index d3c9f3f19..4d2efb58f 100644 --- a/lib/Biodiverse/TreeNode.pm +++ b/lib/Biodiverse/TreeNode.pm @@ -2355,7 +2355,6 @@ sub to_R_phylo { my $length = $node->get_length; $length =~ s/,/./; # hack for issue #775 (another comma radix char) - push @length_arr, $length; if ($node->is_terminal_node) { push @tip_labels, $name; @@ -2367,6 +2366,8 @@ sub to_R_phylo { next NODE if $node->is_root_node; + push @length_arr, $length; + my $parent_id; my $parent = $node->get_parent; @@ -2389,7 +2390,7 @@ sub to_R_phylo { 'Nnode' => (scalar @internal_labels + 1), 'node.label' => \@internal_labels, 'tip.label' => \@tip_labels, - 'root.edge' => 0, + 'root.edge' => $self->get_length, ); return wantarray ? %str : \%str; diff --git a/t/13-Tree.t b/t/13-Tree.t index eea926f1c..7bd6822ae 100644 --- a/t/13-Tree.t +++ b/t/13-Tree.t @@ -947,8 +947,15 @@ sub test_export_Rphylo { my @trees = $rn->get_tree_array; my $tree1 = shift @trees; + # another one + $nwk = '(A:1, B:1):5.8'; + $rn = Biodiverse::ReadNexus->new; + $success = $rn->import_newick (data => $nwk); + @trees = $rn->get_tree_array; + my $tree3 = shift @trees; + my $i; - foreach my $tree ($tree1, $tree2) { + foreach my $tree ($tree1, $tree2, $tree3) { $i++; my $result = $tree->to_R_phylo; # round trip it