From 6ebad4e2283f33540c6ed4a9b1c8efc839d62637 Mon Sep 17 00:00:00 2001 From: cmzmasek Date: Wed, 30 Jul 2014 19:29:37 +0000 Subject: [PATCH] in progress --- .../src/org/forester/archaeopteryx/MainFrame.java | 108 +++++++++++++++++++- .../archaeopteryx/MainFrameApplication.java | 5 + .../src/org/forester/archaeopteryx/TreePanel.java | 2 +- 3 files changed, 111 insertions(+), 4 deletions(-) diff --git a/forester/java/src/org/forester/archaeopteryx/MainFrame.java b/forester/java/src/org/forester/archaeopteryx/MainFrame.java index 377c999..8bbedf1 100644 --- a/forester/java/src/org/forester/archaeopteryx/MainFrame.java +++ b/forester/java/src/org/forester/archaeopteryx/MainFrame.java @@ -32,6 +32,8 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; +import java.util.ArrayList; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Locale; @@ -67,6 +69,7 @@ import org.forester.phylogeny.PhylogenyNode.NH_CONVERSION_SUPPORT_VALUE_STYLE; import org.forester.phylogeny.data.Annotation; import org.forester.phylogeny.data.NodeVisualData.NodeFill; import org.forester.phylogeny.data.NodeVisualData.NodeShape; +import org.forester.phylogeny.iterators.PhylogenyNodeIterator; import org.forester.sdi.GSDI; import org.forester.sdi.GSDIR; import org.forester.sdi.SDIException; @@ -162,6 +165,8 @@ public abstract class MainFrame extends JFrame implements ActionListener { JMenuItem _annotate_item; JMenuItem _remove_branch_color_item; JMenuItem _remove_visual_styles_item; + JMenuItem _deleted_selected_nodes_item; + JMenuItem _deleted_not_selected_nodes_item; // font size menu: JMenuItem _super_tiny_fonts_item; JMenuItem _tiny_fonts_item; @@ -320,6 +325,18 @@ public abstract class MainFrame extends JFrame implements ActionListener { } midpointRoot(); } + else if ( o == _deleted_selected_nodes_item ) { + if ( isSubtreeDisplayed() ) { + return; + } + deleteSelectedNodes( true ); + } + else if ( o == _deleted_not_selected_nodes_item ) { + if ( isSubtreeDisplayed() ) { + return; + } + deleteSelectedNodes( false ); + } else if ( o == _annotate_item ) { annotateSequences(); } @@ -526,6 +543,88 @@ public abstract class MainFrame extends JFrame implements ActionListener { _contentpane.repaint(); } + private void deleteSelectedNodes( boolean delete ) { + final Phylogeny phy = getMainPanel().getCurrentPhylogeny(); + if ( phy == null || phy.getNumberOfExternalNodes() < 2 ) { + return; + } + List nodes = null; + if ( ( getCurrentTreePanel().getFoundNodes0() != null ) || ( getCurrentTreePanel().getFoundNodes1() != null ) ) { + nodes = getCurrentTreePanel().getFoundNodesAsListOfPhylogenyNodes(); + } + String function = "Retain"; + if ( delete ) { + function = "Delete"; + } + if ( ( nodes == null ) || nodes.isEmpty() ) { + JOptionPane + .showMessageDialog( this, + "Need to select external nodes, either via direct selection or via the \"Search\" function", + "No external nodes selected to " + function.toLowerCase(), + JOptionPane.ERROR_MESSAGE ); + return; + } + final int todo = nodes.size(); + final int ext = phy.getNumberOfExternalNodes(); + int res = todo; + if ( delete ) { + res = ext - todo; + } + + if ( res < 1 ) { + JOptionPane.showMessageDialog( this, + "Cannot delete all nodes", + "Attempt to delete all nodes ", + JOptionPane.ERROR_MESSAGE ); + return; + } + final int result = JOptionPane.showConfirmDialog( null, "OK to " + function.toLowerCase() + " " + todo + + " external node(s), from a total of " + ext + " external nodes," + "\nresulting in tree with " + res + + " nodes", function + " external nodes", JOptionPane.OK_CANCEL_OPTION ); + if ( result == JOptionPane.OK_OPTION ) { + if ( !delete ) { + final List to_delete = new ArrayList(); + for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) { + final PhylogenyNode n = it.next(); + if ( !nodes.contains( n ) ) { + to_delete.add( n ); + } + } + for( final PhylogenyNode n : to_delete ) { + phy.deleteSubtree( n, true ); + } + } + else { + for( final PhylogenyNode n : nodes ) { + phy.deleteSubtree( n, true ); + } + } + resetSearch(); + getCurrentTreePanel().setNodeInPreorderToNull(); + phy.externalNodesHaveChanged(); + phy.clearHashIdToNodeMap(); + phy.recalculateNumberOfExternalDescendants( true ); + getCurrentTreePanel().resetNodeIdToDistToLeafMap(); + getCurrentTreePanel().setEdited( true ); + repaint(); + } + } + + void resetSearch() { + getMainPanel().getCurrentTreePanel().setFoundNodes0( null ); + getMainPanel().getCurrentTreePanel().setFoundNodes1( null ); + getMainPanel().getControlPanel().setSearchFoundCountsOnLabel0( 0 ); + getMainPanel().getControlPanel().getSearchFoundCountsLabel0().setVisible( false ); + getMainPanel().getControlPanel().getSearchTextField0().setText( "" ); + getMainPanel().getControlPanel().getSearchResetButton0().setEnabled( false ); + getMainPanel().getControlPanel().getSearchResetButton0().setVisible( false ); + getMainPanel().getControlPanel().setSearchFoundCountsOnLabel1( 0 ); + getMainPanel().getControlPanel().getSearchFoundCountsLabel1().setVisible( false ); + getMainPanel().getControlPanel().getSearchTextField1().setText( "" ); + getMainPanel().getControlPanel().getSearchResetButton1().setEnabled( false ); + getMainPanel().getControlPanel().getSearchResetButton1().setVisible( false ); + } + public Configuration getConfiguration() { return _configuration; } @@ -1426,7 +1525,11 @@ public abstract class MainFrame extends JFrame implements ActionListener { private void annotateSequences() { if ( getCurrentTreePanel() != null ) { - final Set nodes = getCurrentTreePanel().getFoundNodes0(); + List nodes = null; + if ( ( getCurrentTreePanel().getFoundNodes0() != null ) + || ( getCurrentTreePanel().getFoundNodes1() != null ) ) { + nodes = getCurrentTreePanel().getFoundNodesAsListOfPhylogenyNodes(); + } if ( ( nodes == null ) || nodes.isEmpty() ) { JOptionPane .showMessageDialog( this, @@ -1475,8 +1578,7 @@ public abstract class MainFrame extends JFrame implements ActionListener { desc = desc.replaceAll( "\\s+", " " ); } if ( !ForesterUtil.isEmpty( ref ) || !ForesterUtil.isEmpty( desc ) ) { - for( final Long id : nodes ) { - final PhylogenyNode n = phy.getNode( id ); + for( final PhylogenyNode n : nodes ) { ForesterUtil.ensurePresenceOfSequence( n ); final Annotation ann = ForesterUtil.isEmpty( ref ) ? new Annotation() : new Annotation( ref ); diff --git a/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java b/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java index f37cb7c..b688722 100644 --- a/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java +++ b/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java @@ -1077,6 +1077,11 @@ public final class MainFrameApplication extends MainFrame { _tools_menu.add( _midpoint_root_item = new JMenuItem( "Midpoint-Root" ) ); customizeJMenuItem( _midpoint_root_item ); _tools_menu.addSeparator(); + _tools_menu.add( _deleted_selected_nodes_item = new JMenuItem( "Delete Selected Nodes" ) ); + customizeJMenuItem( _deleted_selected_nodes_item ); + _tools_menu.add( _deleted_not_selected_nodes_item = new JMenuItem( "Retain Selected Nodes" ) ); + customizeJMenuItem( _deleted_not_selected_nodes_item ); + _tools_menu.addSeparator(); _tools_menu.add( _collapse_species_specific_subtrees = new JMenuItem( "Collapse Species-Specific Subtrees" ) ); customizeJMenuItem( _collapse_species_specific_subtrees ); _tools_menu diff --git a/forester/java/src/org/forester/archaeopteryx/TreePanel.java b/forester/java/src/org/forester/archaeopteryx/TreePanel.java index d3a1c38..3b0abfe 100644 --- a/forester/java/src/org/forester/archaeopteryx/TreePanel.java +++ b/forester/java/src/org/forester/archaeopteryx/TreePanel.java @@ -2820,7 +2820,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee return getTreeFontSet().getFontMetricsLarge(); } - private List getFoundNodesAsListOfPhylogenyNodes() { + List getFoundNodesAsListOfPhylogenyNodes() { final List additional_nodes = new ArrayList(); if ( getFoundNodes0() != null ) { for( final Long id : getFoundNodes0() ) { -- 1.7.10.2