Skip to content

Commit

Permalink
GUI: Add menu option to merge tree knuckle nodes
Browse files Browse the repository at this point in the history
Updates #814
  • Loading branch information
shawnlaffan committed Sep 12, 2022
1 parent 2baf4e8 commit aefb514
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
9 changes: 9 additions & 0 deletions bin/ui/wndMain.ui
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,14 @@ Assumes a dissimilarity matrix, so take care.</property>
<signal handler="on_trim_tree_to_lca" name="activate"/>
</object>
</child>
<child>
<object class="GtkAction" id="menu_merge_tree_knuckle_nodes">
<property name="name">menu_merge_tree_knuckle_nodes</property>
<property name="tooltip" translatable="yes">Merge single-child (knuckle) nodes with their children</property>
<property name="label" translatable="yes">Merge knuckle nodes</property>
<signal handler="on_tree_merge_knuckle_nodes" name="activate"/>
</object>
</child>
<child>
<object class="GtkAction" id="menu_range_weight_tree_branches">
<property name="name">menu_range_weight_tree_branches</property>
Expand Down Expand Up @@ -808,6 +816,7 @@ This cannot be undone. </property>
<menuitem action="menu_convert_phylogeny_to_matrix"/>
<menuitem action="menu_trim_tree_to_basedata"/>
<menuitem action="menu_trim_tree_to_lca"/>
<menuitem action="menu_merge_tree_knuckle_nodes"/>
<menuitem action="menu_range_weight_tree_branches"/>
<menuitem action="menu_equalise_tree_branches"/>
<menuitem action="menu_rescale_tree_branches2"/>
Expand Down
3 changes: 3 additions & 0 deletions lib/Biodiverse/GUI/Callbacks.pm
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ my %data_funcs = (
on_trim_tree_to_lca => {
METHOD => 'do_trim_tree_to_lca',
},
on_tree_merge_knuckle_nodes => {
METHOD => 'do_tree_merge_knuckle_nodes',
},
on_trim_matrix_to_basedata => {
METHOD => 'do_trim_matrix_to_basedata',
},
Expand Down
69 changes: 69 additions & 0 deletions lib/Biodiverse/GUI/GUIManager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1944,6 +1944,75 @@ sub do_trim_tree_to_lca {
}


# too much duplicated code in here
sub do_tree_merge_knuckle_nodes {
my $self = shift;
my %args = @_;

my $phylogeny = $self->{project}->get_selected_phylogeny;

if ( !defined $phylogeny ) {
Biodiverse::GUI::YesNoCancel->run(
{
header => 'no tree selected',
hide_yes => 1,
hide_no => 1,
hide_cancel => 1,
}
);

return 0;
}

# Show the Get Name dialog
my ( $dlgxml, $dlg ) = $self->get_dlg_duplicate();
$dlg->set_transient_for( $self->get_object('wndMain') );

my $txt_name = $dlgxml->get_object('txtName');
my $name = $phylogeny->get_param('NAME');

my $suffix = $args{suffix} || '_noknuckles';

# If ends with $suffix followed by a number then increment it
if ( $name =~ /(.*_$suffix)([0-9]+)$/ ) {
$name = $1 . ( $2 + 1 );
}
else {
$name .= "_${suffix}1";
}
$txt_name->set_text($name);

my $response = $dlg->run();
my $chosen_name = $txt_name->get_text;

$dlg->destroy;

return if $response ne 'ok'; # they chickened out

# could be more efficient?
my $new_tree = $phylogeny->clone;
$new_tree->delete_cached_values;
$new_tree->merge_knuckle_nodes;

$new_tree->set_param( NAME => $chosen_name );

# now we add it if it is not already in the list
# otherwise we select it
my $phylogenies = $self->{project}->get_phylogeny_list;

my $in_list = grep { $_ eq $new_tree } @$phylogenies;

if ($in_list) {
$self->{project}->select_phylogeny($new_tree);
}
else {
$self->{project}->add_phylogeny( $new_tree, 0 );
}

return;
}


sub do_tree_equalise_branch_lengths {
my $self = shift;
my %args = @_;
Expand Down

0 comments on commit aefb514

Please sign in to comment.