cleanup
[jalview.git] / forester / java / src / org / forester / archaeopteryx / TreePanel.java
index 3ad3935..88c4d66 100644 (file)
@@ -76,11 +76,8 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.List;
-import java.util.Map.Entry;
 import java.util.Set;
-import java.util.SortedMap;
 import java.util.SortedSet;
-import java.util.TreeMap;
 
 import javax.swing.BorderFactory;
 import javax.swing.JApplet;
@@ -109,6 +106,7 @@ 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.Accession;
 import org.forester.phylogeny.data.Annotation;
 import org.forester.phylogeny.data.BranchColor;
 import org.forester.phylogeny.data.Confidence;
@@ -130,7 +128,7 @@ import org.forester.util.BasicDescriptiveStatistics;
 import org.forester.util.DescriptiveStatistics;
 import org.forester.util.ForesterConstants;
 import org.forester.util.ForesterUtil;
-import org.forester.util.SequenceIdParser;
+import org.forester.util.SequenceAccessionTools;
 
 public final class TreePanel extends JPanel implements ActionListener, MouseWheelListener, Printable {
 
@@ -610,13 +608,18 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                             .getValue()
                             + " " );
                 }
-                if ( getControlPanel().isShowGeneNames() && ( node.getNodeData().getSequence().getName().length() > 0 ) ) {
+                if ( getControlPanel().isShowSeqNames() && ( node.getNodeData().getSequence().getName().length() > 0 ) ) {
                     sum += getTreeFontSet()._fm_large.stringWidth( node.getNodeData().getSequence().getName() + " " );
                 }
-                if ( getControlPanel().isShowGeneSymbols()
+                if ( getControlPanel().isShowSeqSymbols()
                         && ( node.getNodeData().getSequence().getSymbol().length() > 0 ) ) {
                     sum += getTreeFontSet()._fm_large.stringWidth( node.getNodeData().getSequence().getSymbol() + " " );
                 }
+                if ( getControlPanel().isShowGeneNames()
+                        && ( node.getNodeData().getSequence().getGeneName().length() > 0 ) ) {
+                    sum += getTreeFontSet()._fm_large
+                            .stringWidth( node.getNodeData().getSequence().getGeneName() + " " );
+                }
                 if ( getControlPanel().isShowAnnotation()
                         && ( node.getNodeData().getSequence().getAnnotations() != null )
                         && !node.getNodeData().getSequence().getAnnotations().isEmpty() ) {
@@ -1928,8 +1931,8 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             DESCENDANT_SORT_PRIORITY pri = DESCENDANT_SORT_PRIORITY.TAXONOMY;
             if ( ( !getControlPanel().isShowTaxonomyScientificNames() && !getControlPanel().isShowTaxonomyCode() && !getControlPanel()
                     .isShowTaxonomyCommonNames() ) ) {
-                if ( ( getControlPanel().isShowSequenceAcc() || getControlPanel().isShowGeneNames() || getControlPanel()
-                        .isShowGeneSymbols() ) ) {
+                if ( ( getControlPanel().isShowSequenceAcc() || getControlPanel().isShowSeqNames() || getControlPanel()
+                        .isShowSeqSymbols() ) ) {
                     pri = DESCENDANT_SORT_PRIORITY.SEQUENCE;
                 }
                 else if ( getControlPanel().isShowNodeNames() ) {
@@ -2270,7 +2273,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                     }
                 }
                 if ( type == '?' ) {
-                    if ( SequenceIdParser.isProtein( query ) ) {
+                    if ( SequenceAccessionTools.isProteinDbQuery( query ) ) {
                         type = 'p';
                     }
                     else {
@@ -2404,10 +2407,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         _color_chooser.setPreviewPanel( new JPanel() );
         SubtreeColorizationActionListener al;
         if ( ( getFoundNodes() != null ) && !getFoundNodes().isEmpty() ) {
-            final List<PhylogenyNode> additional_nodes = new ArrayList<PhylogenyNode>();
-            for( final Long id : getFoundNodes() ) {
-                additional_nodes.add( _phylogeny.getNode( id ) );
-            }
+            final List<PhylogenyNode> additional_nodes = getFoundNodesAsListOfPhylogenyNodes();
             al = new SubtreeColorizationActionListener( _color_chooser, node, additional_nodes );
         }
         else {
@@ -2418,6 +2418,14 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         dialog.setVisible( true );
     }
 
+    private List<PhylogenyNode> getFoundNodesAsListOfPhylogenyNodes() {
+        final List<PhylogenyNode> additional_nodes = new ArrayList<PhylogenyNode>();
+        for( final Long id : getFoundNodes() ) {
+            additional_nodes.add( _phylogeny.getNode( id ) );
+        }
+        return additional_nodes;
+    }
+
     final private void copySubtree( final PhylogenyNode node ) {
         if ( getPhylogenyGraphicsType() == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) {
             errorMessageNoCutCopyPasteInUnrootedDisplay();
@@ -2907,17 +2915,11 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
     }
 
     final private String isCanOpenSeqWeb( final PhylogenyNode node ) {
-        String v = ForesterUtil.extractUniProtKbProteinSeqIdentifier( node );
-        if ( ForesterUtil.isEmpty( v ) ) {
-            v = ForesterUtil.extractGenbankAccessor( node );
-        }
-        if ( ForesterUtil.isEmpty( v ) ) {
-            v = ForesterUtil.extractRefSeqAccessorAccessor( node );
-        }
-        if ( ForesterUtil.isEmpty( v ) ) {
-            v = ForesterUtil.extractGInumber( node );
+        final Accession a = SequenceAccessionTools.obtainAccessorFromDataFields( node );
+        if ( a != null ) {
+            return a.getValue();
         }
-        return v;
+        return null;
     }
 
     final private boolean isCanOpenTaxWeb( final PhylogenyNode node ) {
@@ -3222,6 +3224,8 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         switch ( getOptions().getExtDescNodeDataToReturn() ) {
             case NODE_NAME:
                 return "Node Names";
+            case GENE_NAME:
+                return "Gene Names";
             case SEQUENCE_NAME:
                 return "Sequence Names";
             case SEQUENCE_SYMBOL:
@@ -3982,13 +3986,19 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             _sb.append( node.getName() );
         }
         if ( node.getNodeData().isHasSequence() ) {
-            if ( getControlPanel().isShowGeneSymbols() && ( node.getNodeData().getSequence().getSymbol().length() > 0 ) ) {
+            if ( getControlPanel().isShowSeqSymbols() && ( node.getNodeData().getSequence().getSymbol().length() > 0 ) ) {
                 if ( _sb.length() > 0 ) {
                     _sb.append( " " );
                 }
                 _sb.append( node.getNodeData().getSequence().getSymbol() );
             }
-            if ( getControlPanel().isShowGeneNames() && ( node.getNodeData().getSequence().getName().length() > 0 ) ) {
+            if ( getControlPanel().isShowGeneNames() && ( node.getNodeData().getSequence().getGeneName().length() > 0 ) ) {
+                if ( _sb.length() > 0 ) {
+                    _sb.append( " " );
+                }
+                _sb.append( node.getNodeData().getSequence().getGeneName() );
+            }
+            if ( getControlPanel().isShowSeqNames() && ( node.getNodeData().getSequence().getName().length() > 0 ) ) {
                 if ( _sb.length() > 0 ) {
                     _sb.append( " " );
                 }
@@ -4232,7 +4242,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                 }
                 _sb.append( node.getNodeData().getSequence().getAccession().getValue() );
             }
-            if ( getControlPanel().isShowGeneNames() && ( node.getNodeData().getSequence().getName().length() > 0 ) ) {
+            if ( getControlPanel().isShowSeqNames() && ( node.getNodeData().getSequence().getName().length() > 0 ) ) {
                 if ( _sb.length() > 0 ) {
                     _sb.append( " " );
                 }
@@ -4475,16 +4485,21 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                         }
                     }
                     if ( node.getNodeData().isHasSequence() ) {
-                        if ( getControlPanel().isShowGeneNames()
+                        if ( getControlPanel().isShowSeqNames()
                                 && ( !ForesterUtil.isEmpty( node.getNodeData().getSequence().getName() ) ) ) {
                             x += getTreeFontSet()._fm_large.stringWidth( node.getNodeData().getSequence().getName()
                                     + " " );
                         }
-                        if ( getControlPanel().isShowGeneSymbols()
+                        if ( getControlPanel().isShowSeqSymbols()
                                 && ( !ForesterUtil.isEmpty( node.getNodeData().getSequence().getSymbol() ) ) ) {
                             x += getTreeFontSet()._fm_large.stringWidth( node.getNodeData().getSequence().getSymbol()
                                     + " " );
                         }
+                        if ( getControlPanel().isShowGeneNames()
+                                && ( !ForesterUtil.isEmpty( node.getNodeData().getSequence().getGeneName() ) ) ) {
+                            x += getTreeFontSet()._fm_large.stringWidth( node.getNodeData().getSequence().getGeneName()
+                                    + " " );
+                        }
                         if ( getControlPanel().isShowSequenceAcc()
                                 && ( node.getNodeData().getSequence().getAccession() != null ) ) {
                             x += getTreeFontSet()._fm_large.stringWidth( node.getNodeData().getSequence()
@@ -5070,7 +5085,15 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
 
     private void showExtDescNodeData( final PhylogenyNode node ) {
         final List<String> data = new ArrayList<String>();
-        for( final PhylogenyNode n : node.getAllExternalDescendants() ) {
+        final List<PhylogenyNode> nodes = node.getAllExternalDescendants();
+        if ( ( getFoundNodes() != null ) && !getFoundNodes().isEmpty() ) {
+            for( final PhylogenyNode n : getFoundNodesAsListOfPhylogenyNodes() ) {
+                if ( !nodes.contains( n ) ) {
+                    nodes.add( n );
+                }
+            }
+        }
+        for( final PhylogenyNode n : nodes ) {
             switch ( getOptions().getExtDescNodeDataToReturn() ) {
                 case NODE_NAME:
                     if ( !ForesterUtil.isEmpty( n.getName() ) ) {
@@ -5083,6 +5106,12 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                         data.add( n.getNodeData().getSequence().getName() );
                     }
                     break;
+                case GENE_NAME:
+                    if ( n.getNodeData().isHasSequence()
+                            && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getGeneName() ) ) {
+                        data.add( n.getNodeData().getSequence().getGeneName() );
+                    }
+                    break;
                 case SEQUENCE_SYMBOL:
                     if ( n.getNodeData().isHasSequence()
                             && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getSymbol() ) ) {
@@ -5114,6 +5143,11 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                             ann.append( n.getNodeData().getSequence().getName() );
                             ann.append( "|" );
                         }
+                        if ( !ForesterUtil.isEmpty( n.getNodeData().getSequence().getGeneName() ) ) {
+                            ann.append( "GN=" );
+                            ann.append( n.getNodeData().getSequence().getGeneName() );
+                            ann.append( "|" );
+                        }
                         if ( n.getNodeData().getSequence().getAccession() != null ) {
                             ann.append( "ACC=" );
                             ann.append( n.getNodeData().getSequence().getAccession().asText() );
@@ -5131,9 +5165,12 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                                 ann.append( "|" );
                             }
                         }
-                        String ann_str = ann.toString().trim();
-                        if ( ann_str.endsWith( "|" ) ) {
-                            ann_str = ann_str.substring( 0, ann_str.length() - 1 );
+                        String ann_str;
+                        if ( ann.charAt( ann.length() - 1 ) == '|' ) {
+                            ann_str = ann.substring( 0, ann.length() - 1 );
+                        }
+                        else {
+                            ann_str = ann.toString();
                         }
                         sb.append( SequenceWriter.toFasta( ann_str, n.getNodeData().getSequence()
                                 .getMolecularSequence(), 60 ) );
@@ -5173,7 +5210,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             }
         } // for loop
         final StringBuilder sb = new StringBuilder();
-        final int size = makeSB( data, getOptions(), sb );
+        final int size = TreePanelUtil.makeSB( data, getOptions(), sb );
         if ( ( getConfiguration().getExtNodeDataReturnOn() == EXT_NODE_DATA_RETURN_ON.CONSOLE )
                 || ( getConfiguration().getExtNodeDataReturnOn() == EXT_NODE_DATA_RETURN_ON.BUFFER_ONLY ) ) {
             if ( getConfiguration().getExtNodeDataReturnOn() == EXT_NODE_DATA_RETURN_ON.CONSOLE ) {
@@ -5194,15 +5231,26 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             }
             else {
                 setCurrentExternalNodesDataBuffer( sb );
-                final String title = ( getOptions().getExtDescNodeDataToReturn() == NODE_DATA.UNKNOWN ? "Data"
-                        : obtainTitleForExtDescNodeData() )
-                        + " for "
-                        + data.size()
-                        + "/"
-                        + node.getNumberOfExternalNodes()
-                        + " external descendats of node "
-                        + node
-                        + ", unique entries: " + size;
+                String title;
+                if ( ( getFoundNodes() != null ) && !getFoundNodes().isEmpty() ) {
+                    title = ( getOptions().getExtDescNodeDataToReturn() == NODE_DATA.UNKNOWN ? "Data"
+                            : obtainTitleForExtDescNodeData() )
+                            + " for "
+                            + data.size()
+                            + " nodes, unique entries: "
+                            + size;
+                }
+                else {
+                    title = ( getOptions().getExtDescNodeDataToReturn() == NODE_DATA.UNKNOWN ? "Data"
+                            : obtainTitleForExtDescNodeData() )
+                            + " for "
+                            + data.size()
+                            + "/"
+                            + node.getNumberOfExternalNodes()
+                            + " external descendats of node "
+                            + node
+                            + ", unique entries: " + size;
+                }
                 final String s = sb.toString().trim();
                 if ( getMainPanel().getMainFrame() == null ) {
                     // Must be "E" applet version.
@@ -5216,46 +5264,6 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         }
     }
 
-    private int makeSB( final List<String> data, final Options optz, final StringBuilder sb ) {
-        final SortedMap<String, Integer> map = new TreeMap<String, Integer>();
-        if ( ( optz.getExtDescNodeDataToReturn() != NODE_DATA.SEQUENCE_MOL_SEQ )
-                && ( optz.getExtDescNodeDataToReturn() != NODE_DATA.SEQUENCE_MOL_SEQ_FASTA ) ) {
-            for( final String d : data ) {
-                if ( !ForesterUtil.isEmpty( d ) ) {
-                    if ( map.containsKey( d ) ) {
-                        map.put( d, map.get( d ) + 1 );
-                    }
-                    else {
-                        map.put( d, 1 );
-                    }
-                }
-            }
-        }
-        int size = 0;
-        if ( ( optz.getExtDescNodeDataToReturn() != NODE_DATA.SEQUENCE_MOL_SEQ )
-                && ( optz.getExtDescNodeDataToReturn() != NODE_DATA.SEQUENCE_MOL_SEQ_FASTA ) ) {
-            for( final Entry<String, Integer> e : map.entrySet() ) {
-                final String v = e.getKey();
-                final Object c = e.getValue();
-                sb.append( v );
-                sb.append( "\t" );
-                sb.append( c );
-                sb.append( ForesterUtil.LINE_SEPARATOR );
-            }
-            size = map.size();
-        }
-        else {
-            for( final String d : data ) {
-                if ( !ForesterUtil.isEmpty( d ) ) {
-                    sb.append( d );
-                    sb.append( ForesterUtil.LINE_SEPARATOR );
-                }
-            }
-            size = data.size();
-        }
-        return size;
-    }
-
     final private void showNodeDataPopup( final MouseEvent e, final PhylogenyNode node ) {
         try {
             if ( ( node.getName().length() > 0 )
@@ -5382,6 +5390,17 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                         _popup_buffer.append( "]" );
                         enc_data = true;
                     }
+                    if ( !ForesterUtil.isEmpty( seq.getGeneName() ) ) {
+                        if ( enc_data ) {
+                            _popup_buffer.append( " [" );
+                        }
+                        else {
+                            _popup_buffer.append( "[" );
+                        }
+                        _popup_buffer.append( seq.getGeneName() );
+                        _popup_buffer.append( "]" );
+                        enc_data = true;
+                    }
                     if ( !ForesterUtil.isEmpty( seq.getName() ) ) {
                         if ( enc_data ) {
                             _popup_buffer.append( " " );