cleanup
[jalview.git] / forester / java / src / org / forester / archaeopteryx / TreePanel.java
index 79dfca7..e857426 100644 (file)
@@ -100,6 +100,7 @@ import org.forester.archaeopteryx.tools.ImageLoader;
 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
+import org.forester.phylogeny.PhylogenyMethods.DESCENDANT_SORT_PRIORITY;
 import org.forester.phylogeny.PhylogenyNode;
 import org.forester.phylogeny.data.Annotation;
 import org.forester.phylogeny.data.BranchColor;
@@ -345,10 +346,9 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
     }
 
     final public void actionPerformed( final ActionEvent e ) {
-        int index;
         boolean done = false;
         final JMenuItem node_popup_menu_item = ( JMenuItem ) e.getSource();
-        for( index = 0; ( index < _node_popup_menu_items.length ) && !done; index++ ) {
+        for( int index = 0; ( index < _node_popup_menu_items.length ) && !done; index++ ) {
             // NOTE: index corresponds to the indices of click-to options
             // in the control panel.
             if ( node_popup_menu_item == _node_popup_menu_items[ index ] ) {
@@ -464,25 +464,37 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             return;
         }
         if ( node.getNodeData().isHasSequence() ) {
-            String name = "";
-            if ( !ForesterUtil.isEmpty( node.getNodeData().getSequence().getName() ) ) {
-                name = node.getNodeData().getSequence().getName();
-            }
-            else if ( !ForesterUtil.isEmpty( node.getNodeData().getSequence().getSymbol() ) ) {
-                name = node.getNodeData().getSequence().getSymbol();
+            final String query = Blast.obtainQueryForBlast( node );
+            boolean nucleotide = false;
+            if ( !ForesterUtil.isEmpty( node.getNodeData().getSequence().getType() ) ) {
+                if ( !node.getNodeData().getSequence().getType().toLowerCase().equals( PhyloXmlUtil.SEQ_TYPE_PROTEIN ) ) {
+                    nucleotide = true;
+                }
             }
-            else if ( node.getNodeData().getSequence().getAccession() != null ) {
-                name = node.getNodeData().getSequence().getAccession().getValue();
+            else if ( !ForesterUtil.isEmpty( node.getNodeData().getSequence().getMolecularSequence() ) ) {
+                nucleotide = !ForesterUtil.seqIsLikelyToBeAa( node.getNodeData().getSequence().getMolecularSequence() );
             }
-            if ( !ForesterUtil.isEmpty( name ) ) {
+            if ( !ForesterUtil.isEmpty( query ) ) {
+                JApplet applet = null;
+                if ( isApplet() ) {
+                    applet = obtainApplet();
+                }
                 try {
-                    System.out.println( "trying: " + name );
-                    final Blast s = new Blast();
-                    s.go( name );
+                    Blast.openNcbiBlastWeb( query, nucleotide, applet, this );
                 }
                 catch ( final Exception e ) {
                     e.printStackTrace();
                 }
+                if ( Constants.ALLOW_DDBJ_BLAST ) {
+                    try {
+                        System.out.println( "trying: " + query );
+                        final Blast s = new Blast();
+                        s.ddbjBlast( query );
+                    }
+                    catch ( final Exception e ) {
+                        e.printStackTrace();
+                    }
+                }
             }
         }
     }
@@ -532,7 +544,12 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         if ( ( _phylogeny == null ) || _phylogeny.isEmpty() ) {
             return;
         }
-        int longest = 20;
+        int max_length = ForesterUtil.roundToInt( ( getSize().getWidth() - MOVE )
+                * Constants.EXT_NODE_INFO_LENGTH_MAX_RATIO );
+        if ( max_length < 40 ) {
+            max_length = 40;
+        }
+        int longest = 30;
         for( final PhylogenyNode node : _phylogeny.getExternalNodes() ) {
             int sum = 0;
             if ( node.isCollapse() ) {
@@ -562,6 +579,11 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                             .asSimpleText()
                             + " " );
                 }
+                if ( getControlPanel().isShowDomainArchitectures()
+                        && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
+                    sum += ( ( RenderableDomainArchitecture ) node.getNodeData().getSequence().getDomainArchitecture() )
+                            .getRenderingSize().getWidth();
+                }
             }
             if ( node.getNodeData().isHasTaxonomy() ) {
                 final Taxonomy tax = node.getNodeData().getTaxonomy();
@@ -583,21 +605,16 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                 sum += getTreeFontSet()._fm_large.stringWidth( node.getNodeData().getBinaryCharacters()
                         .getGainedCharactersAsStringBuffer().toString() );
             }
-            if ( getControlPanel().isShowDomainArchitectures() && node.getNodeData().isHasSequence()
-                    && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
-                sum += ( ( RenderableDomainArchitecture ) node.getNodeData().getSequence().getDomainArchitecture() )
-                        .getRenderingSize().getWidth();
-            }
-            if ( sum >= Constants.EXT_NODE_INFO_LENGTH_MAX ) {
-                setLongestExtNodeInfo( Constants.EXT_NODE_INFO_LENGTH_MAX );
+            if ( sum >= max_length ) {
+                setLongestExtNodeInfo( max_length );
                 return;
             }
             if ( sum > longest ) {
                 longest = sum;
             }
         }
-        if ( longest >= Constants.EXT_NODE_INFO_LENGTH_MAX ) {
-            setLongestExtNodeInfo( Constants.EXT_NODE_INFO_LENGTH_MAX );
+        if ( longest >= max_length ) {
+            setLongestExtNodeInfo( max_length );
         }
         else {
             setLongestExtNodeInfo( longest );
@@ -695,7 +712,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         if ( !node.isExternal() && !node.isRoot() ) {
             final boolean collapse = !node.isCollapse();
             AptxUtil.collapseSubtree( node, collapse );
-            updateSetOfCollapsedExternalNodes( _phylogeny );
+            updateSetOfCollapsedExternalNodes();
             _phylogeny.recalculateNumberOfExternalDescendants( true );
             resetNodeIdToDistToLeafMap();
             calculateLongestExtNodeInfo();
@@ -714,7 +731,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         }
         setWaitCursor();
         AptxUtil.collapseSpeciesSpecificSubtrees( _phylogeny );
-        updateSetOfCollapsedExternalNodes( _phylogeny );
+        updateSetOfCollapsedExternalNodes();
         _phylogeny.recalculateNumberOfExternalDescendants( true );
         resetNodeIdToDistToLeafMap();
         calculateLongestExtNodeInfo();
@@ -1317,6 +1334,9 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             case EDIT_NODE_DATA:
                 showNodeEditFrame( node );
                 break;
+            case SORT_DESCENDENTS:
+                sortDescendants( node );
+                break;
             default:
                 throw new IllegalArgumentException( "unknown action: " + action );
         }
@@ -1437,10 +1457,12 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
     }
 
     final private boolean isCanBlast( final PhylogenyNode node ) {
-        return ( node.getNodeData().isHasSequence() && ( ( ( node.getNodeData().getSequence().getAccession() != null ) && !ForesterUtil
-                .isEmpty( node.getNodeData().getSequence().getAccession().getValue() ) )
-                || !ForesterUtil.isEmpty( node.getNodeData().getSequence().getName() ) || !ForesterUtil.isEmpty( node
-                .getNodeData().getSequence().getSymbol() ) ) );
+        return ( node.getNodeData().isHasSequence()
+                && ( ( ( node.getNodeData().getSequence().getAccession() != null ) && !ForesterUtil.isEmpty( node
+                        .getNodeData().getSequence().getAccession().getValue() ) )
+                        || !ForesterUtil.isEmpty( node.getNodeData().getSequence().getName() ) || !ForesterUtil
+                        .isEmpty( node.getNodeData().getSequence().getMolecularSequence() ) ) && Blast
+                .isContainsQueryForBlast( node ) );
     }
 
     final boolean isCanCollapse() {
@@ -1739,9 +1761,6 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                 _node_popup_menu_items[ i ].setEnabled( isCanOpenTaxWeb( node ) );
             }
             else if ( title.equals( Configuration.clickto_options[ Configuration.blast ][ 0 ] ) ) {
-                if ( Constants.__RELEASE || Constants.__SNAPSHOT_RELEASE ) {
-                    continue;
-                }
                 _node_popup_menu_items[ i ].setEnabled( isCanBlast( node ) );
             }
             else if ( title.equals( Configuration.clickto_options[ Configuration.delete_subtree_or_node ][ 0 ] ) ) {
@@ -1782,7 +1801,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                 _node_popup_menu_items[ i ].setEnabled( isCanReroot() );
             }
             else if ( title.equals( Configuration.clickto_options[ Configuration.collapse_uncollapse ][ 0 ] ) ) {
-                _node_popup_menu_items[ i ].setEnabled( isCanCollapse() );
+                _node_popup_menu_items[ i ].setEnabled( ( isCanCollapse() && !node.isExternal() ) );
             }
             else if ( title.equals( Configuration.clickto_options[ Configuration.color_subtree ][ 0 ] ) ) {
                 _node_popup_menu_items[ i ].setEnabled( isCanColorSubtree() );
@@ -1790,6 +1809,12 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             else if ( title.equals( Configuration.clickto_options[ Configuration.subtree ][ 0 ] ) ) {
                 _node_popup_menu_items[ i ].setEnabled( isCanSubtree( node ) );
             }
+            else if ( title.equals( Configuration.clickto_options[ Configuration.swap ][ 0 ] ) ) {
+                _node_popup_menu_items[ i ].setEnabled( node.getNumberOfDescendants() == 2 );
+            }
+            else if ( title.equals( Configuration.clickto_options[ Configuration.sort_descendents ][ 0 ] ) ) {
+                _node_popup_menu_items[ i ].setEnabled( node.getNumberOfDescendants() > 1 );
+            }
             _node_popup_menu_items[ i ].addActionListener( this );
             _node_popup_menu.add( _node_popup_menu_items[ i ] );
         }
@@ -2512,17 +2537,21 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         paintNodeBox( _root.getXcoord(), _root.getYcoord(), _root, g, to_pdf, to_graphics_file, isInFoundNodes( _root ) );
     }
 
-    void updateSetOfCollapsedExternalNodes( final Phylogeny phy ) {
+    void updateSetOfCollapsedExternalNodes() {
+        final Phylogeny phy = getPhylogeny();
         _collapsed_external_nodeid_set.clear();
-        E: for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
-            final PhylogenyNode ext_node = it.next();
-            PhylogenyNode n = ext_node;
-            while ( !n.isRoot() ) {
-                if ( n.isCollapse() ) {
-                    _collapsed_external_nodeid_set.add( ext_node.getId() );
-                    continue E;
+        if ( phy != null ) {
+            E: for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
+                final PhylogenyNode ext_node = it.next();
+                PhylogenyNode n = ext_node;
+                while ( !n.isRoot() ) {
+                    if ( n.isCollapse() ) {
+                        _collapsed_external_nodeid_set.add( ext_node.getId() );
+                        ext_node.setCollapse( true );
+                        continue E;
+                    }
+                    n = n.getParent();
                 }
-                n = n.getParent();
             }
         }
     }
@@ -3641,6 +3670,9 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                                final int graphics_file_height,
                                final int graphics_file_x,
                                final int graphics_file_y ) {
+        if ( ( _phylogeny == null ) || _phylogeny.isEmpty() ) {
+            return;
+        }
         if ( _control_panel.isShowSequenceRelations() ) {
             _query_sequence = _control_panel.getSelectedQuerySequence();
         }
@@ -4514,7 +4546,9 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             }
         }
         if ( getMainPanel().getOptions().isAntialiasScreen() ) {
-            if ( getPhylogenyGraphicsType() == PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR ) {
+            if ( ( getPhylogenyGraphicsType() == PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR )
+                    && !getMainPanel().getOptions().isShowDefaultNodeShapes()
+                    && ( ( getControlPanel() != null ) && !getControlPanel().isShowDomainArchitectures() ) ) {
                 _rendering_hints.put( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF );
             }
             else {
@@ -4908,9 +4942,48 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
     }
 
     final void swap( final PhylogenyNode node ) {
+        if ( node.isExternal() || ( node.getNumberOfDescendants() < 2 ) ) {
+            return;
+        }
+        if ( node.getNumberOfDescendants() > 2 ) {
+            JOptionPane.showMessageDialog( this,
+                                           "Cannot swap descendants of nodes with more than 2 descendants",
+                                           "Cannot swap descendants",
+                                           JOptionPane.ERROR_MESSAGE );
+            return;
+        }
         if ( !node.isExternal() ) {
-            _phylogeny.swapChildren( node );
+            node.swapChildren();
             setNodeInPreorderToNull();
+            _phylogeny.externalNodesHaveChanged();
+            _phylogeny.hashIDs();
+            _phylogeny.recalculateNumberOfExternalDescendants( true );
+            resetNodeIdToDistToLeafMap();
+            setEdited( true );
+        }
+        repaint();
+    }
+
+    final void sortDescendants( final PhylogenyNode node ) {
+        if ( !node.isExternal() ) {
+            DESCENDANT_SORT_PRIORITY pri = DESCENDANT_SORT_PRIORITY.TAXONOMY;
+            if ( ( !getControlPanel().isShowTaxonomyScientificNames() && !getControlPanel().isShowTaxonomyCode() && !getControlPanel()
+                    .isShowTaxonomyCommonNames() ) ) {
+                if ( ( getControlPanel().isShowSequenceAcc() || getControlPanel().isShowGeneNames() || getControlPanel()
+                        .isShowGeneSymbols() ) ) {
+                    pri = DESCENDANT_SORT_PRIORITY.SEQUENCE;
+                }
+                else if ( getControlPanel().isShowNodeNames() ) {
+                    pri = DESCENDANT_SORT_PRIORITY.NODE_NAME;
+                }
+            }
+            PhylogenyMethods.sortNodeDescendents( node, pri );
+            setNodeInPreorderToNull();
+            _phylogeny.externalNodesHaveChanged();
+            _phylogeny.hashIDs();
+            _phylogeny.recalculateNumberOfExternalDescendants( true );
+            resetNodeIdToDistToLeafMap();
+            setEdited( true );
         }
         repaint();
     }