work on: add ability to perform GSDI in applets
[jalview.git] / forester / java / src / org / forester / archaeopteryx / TreePanel.java
index a309225..533093c 100644 (file)
@@ -100,6 +100,7 @@ import org.forester.archaeopteryx.phylogeny.data.RenderableVector;
 import org.forester.archaeopteryx.tools.Blast;
 import org.forester.archaeopteryx.tools.ImageLoader;
 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
+import org.forester.io.writers.SequenceWriter;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.PhylogenyMethods.DESCENDANT_SORT_PRIORITY;
@@ -477,8 +478,6 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
     /**
      * Set parameters for printing the displayed tree
      * 
-     * @param x
-     * @param y
      */
     final void calcParametersForPainting( final int x, final int y, final boolean recalc_longest_ext_node_info ) {
         // updateStyle(); not needed?
@@ -489,7 +488,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                 if ( getOptions().isAllowFontSizeChange() ) {
                     if ( ( getLongestExtNodeInfo() > ( x * 0.6 ) )
                             && ( getTreeFontSet().getLargeFont().getSize() > 2 + TreeFontSet.FONT_SIZE_CHANGE_STEP ) ) {
-                        while ( ( getLongestExtNodeInfo() > ( x * 0.6 ) )
+                        while ( ( getLongestExtNodeInfo() > ( x * 0.7 ) )
                                 && ( getTreeFontSet().getLargeFont().getSize() > 2 ) ) {
                             getMainPanel().getTreeFontSet().decreaseFontSize( getConfiguration().getMinBaseFontSize(),
                                                                               true );
@@ -497,7 +496,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                         }
                     }
                     else {
-                        while ( ( getLongestExtNodeInfo() < ( x * 0.5 ) )
+                        while ( ( getLongestExtNodeInfo() < ( x * 0.6 ) )
                                 && ( getTreeFontSet().getLargeFont().getSize() <= getTreeFontSet().getLargeFontMemory()
                                         .getSize() - TreeFontSet.FONT_SIZE_CHANGE_STEP ) ) {
                             getMainPanel().getTreeFontSet().increaseFontSize();
@@ -2911,25 +2910,9 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         return Blast.isContainsQueryForBlast( node );
     }
 
-    final private boolean isCanOpenSeqWeb( final PhylogenyNode node ) {
-        if ( node.getNodeData().isHasSequence()
-                && ( node.getNodeData().getSequence().getAccession() != null )
-                && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() )
-                && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() )
-                && getConfiguration().isHasWebLink( node.getNodeData().getSequence().getAccession().getSource()
-                        .toLowerCase() ) ) {
-            return true;
-        }
-        return false;
-    }
-
     final private boolean isCanOpenTaxWeb( final PhylogenyNode node ) {
         if ( node.getNodeData().isHasTaxonomy()
-                && ( ( ( node.getNodeData().getTaxonomy().getIdentifier() != null )
-                        && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getIdentifier().getProvider() )
-                        && !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getIdentifier().getValue() ) && getConfiguration()
-                        .isHasWebLink( node.getNodeData().getTaxonomy().getIdentifier().getProvider().toLowerCase() ) )
-                        || ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) )
+                && ( ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) )
                         || ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getTaxonomyCode() ) )
                         || ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getCommonName() ) ) || ( ( node
                         .getNodeData().getTaxonomy().getIdentifier() != null )
@@ -3154,7 +3137,14 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
             final String title = clickto_names.get( i );
             _node_popup_menu_items[ i ] = new JMenuItem( title );
             if ( title.equals( Configuration.clickto_options[ Configuration.open_seq_web ][ 0 ] ) ) {
-                _node_popup_menu_items[ i ].setEnabled( isCanOpenSeqWeb( node ) );
+                final String id = isCanOpenSeqWeb( node );
+                if ( !ForesterUtil.isEmpty( id ) ) {
+                    _node_popup_menu_items[ i ].setText( _node_popup_menu_items[ i ].getText() + " [" + id + "]" );
+                    _node_popup_menu_items[ i ].setEnabled( true );
+                }
+                else {
+                    _node_popup_menu_items[ i ].setEnabled( false );
+                }
             }
             else if ( title.equals( Configuration.clickto_options[ Configuration.open_tax_web ][ 0 ] ) ) {
                 _node_popup_menu_items[ i ].setEnabled( isCanOpenTaxWeb( node ) );
@@ -3229,6 +3219,8 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                 return "Sequence Symbols";
             case SEQUENCE_MOL_SEQ:
                 return "Molecular Sequences";
+            case SEQUENCE_MOL_SEQ_FASTA:
+                return "Molecular Sequences (Fasta)";
             case SEQUENCE_ACC:
                 return "Sequence Accessors";
             case TAXONOMY_SCIENTIFIC_NAME:
@@ -3243,36 +3235,32 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         }
     }
 
-    final private void openSeqWeb( final PhylogenyNode node ) {
-        if ( !isCanOpenSeqWeb( node ) ) {
-            cannotOpenBrowserWarningMessage( "sequence" );
-            return;
+    final private String isCanOpenSeqWeb( final PhylogenyNode node ) {
+        String v = ForesterUtil.extractUniProtKbProteinSeqIdentifier( node );
+        if ( ForesterUtil.isEmpty( v ) ) {
+            v = ForesterUtil.extractGenbankAccessor( node );
         }
-        String uri_str = null;
-        final Sequence seq = node.getNodeData().getSequence();
-        final String source = seq.getAccession().getSource().toLowerCase();
-        String url;
-        if ( source.toLowerCase().equals( "ncbi" ) ) {
-            url = Constants.NCBI_ALL_DATABASE_SEARCH;
-        }
-        else {
-            final WebLink weblink = getConfiguration().getWebLink( source );
-            url = weblink.getUrl().toString();
+        if ( ForesterUtil.isEmpty( v ) ) {
+            v = ForesterUtil.extractRefSeqAccessorAccessor( node );
         }
-        try {
-            uri_str = url + URLEncoder.encode( seq.getAccession().getValue(), ForesterConstants.UTF8 );
+        if ( ForesterUtil.isEmpty( v ) ) {
+            v = ForesterUtil.extractGInumber( node );
         }
-        catch ( final UnsupportedEncodingException e ) {
-            AptxUtil.showErrorMessage( this, e.toString() );
-            e.printStackTrace();
+        return v;
+    }
+
+    final private void openSeqWeb( final PhylogenyNode node ) {
+        if ( ForesterUtil.isEmpty( isCanOpenSeqWeb( node ) ) ) {
+            cannotOpenBrowserWarningMessage( "sequence" );
+            return;
         }
+        final String uri_str = AptxUtil.createUriForSeqWeb( node, getConfiguration(), this );
         if ( !ForesterUtil.isEmpty( uri_str ) ) {
             try {
-                JApplet applet = null;
-                if ( isApplet() ) {
-                    applet = obtainApplet();
-                }
-                AptxUtil.launchWebBrowser( new URI( uri_str ), isApplet(), applet, "_aptx_seq" );
+                AptxUtil.launchWebBrowser( new URI( uri_str ),
+                                           isApplet(),
+                                           isApplet() ? obtainApplet() : null,
+                                           "_aptx_seq" );
             }
             catch ( final IOException e ) {
                 AptxUtil.showErrorMessage( this, e.toString() );
@@ -3295,19 +3283,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         }
         String uri_str = null;
         final Taxonomy tax = node.getNodeData().getTaxonomy();
-        if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getProvider() )
-                && getConfiguration().isHasWebLink( tax.getIdentifier().getProvider().toLowerCase() ) ) {
-            final String type = tax.getIdentifier().getProvider().toLowerCase();
-            final WebLink weblink = getConfiguration().getWebLink( type );
-            try {
-                uri_str = weblink.getUrl() + URLEncoder.encode( tax.getIdentifier().getValue(), ForesterConstants.UTF8 );
-            }
-            catch ( final UnsupportedEncodingException e ) {
-                AptxUtil.showErrorMessage( this, e.toString() );
-                e.printStackTrace();
-            }
-        }
-        else if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getValue() )
+        if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getValue() )
                 && tax.getIdentifier().getValue().startsWith( "http://" ) ) {
             try {
                 uri_str = new URI( tax.getIdentifier().getValue() ).toString();
@@ -3320,7 +3296,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         }
         else if ( !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
             try {
-                uri_str = "http://www.eol.org/search?q="
+                uri_str = "http://www.uniprot.org/taxonomy/?query="
                         + URLEncoder.encode( tax.getScientificName(), ForesterConstants.UTF8 );
             }
             catch ( final UnsupportedEncodingException e ) {
@@ -3340,7 +3316,7 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         }
         else if ( !ForesterUtil.isEmpty( tax.getCommonName() ) ) {
             try {
-                uri_str = "http://www.eol.org/search?q="
+                uri_str = "http://www.uniprot.org/taxonomy/?query="
                         + URLEncoder.encode( tax.getCommonName(), ForesterConstants.UTF8 );
             }
             catch ( final UnsupportedEncodingException e ) {
@@ -3350,11 +3326,10 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
         }
         if ( !ForesterUtil.isEmpty( uri_str ) ) {
             try {
-                JApplet applet = null;
-                if ( isApplet() ) {
-                    applet = obtainApplet();
-                }
-                AptxUtil.launchWebBrowser( new URI( uri_str ), isApplet(), applet, "_aptx_tax" );
+                AptxUtil.launchWebBrowser( new URI( uri_str ),
+                                           isApplet(),
+                                           isApplet() ? obtainApplet() : null,
+                                           "_aptx_tax" );
             }
             catch ( final IOException e ) {
                 AptxUtil.showErrorMessage( this, e.toString() );
@@ -4532,10 +4507,10 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
     }
 
     final private void paintOvRectangle( final Graphics2D g ) {
-        final float w_ratio = ( float ) getWidth() / getVisibleRect().width;
-        final float h_ratio = ( float ) getHeight() / getVisibleRect().height;
-        final float x_ratio = ( float ) getWidth() / getVisibleRect().x;
-        final float y_ratio = ( float ) getHeight() / getVisibleRect().y;
+        final float w_ratio = ( ( float ) getWidth() ) / getVisibleRect().width;
+        final float h_ratio = ( ( float ) getHeight() ) / getVisibleRect().height;
+        final float x_ratio = ( ( float ) getWidth() ) / getVisibleRect().x;
+        final float y_ratio = ( ( float ) getHeight() ) / getVisibleRect().y;
         final float width = getOvMaxWidth() / w_ratio;
         final float height = getOvMaxHeight() / h_ratio;
         final float x = getVisibleRect().x + getOvXPosition() + ( getOvMaxWidth() / x_ratio );
@@ -5062,6 +5037,21 @@ public final class TreePanel extends JPanel implements ActionListener, MouseWhee
                         data.add( n.getNodeData().getSequence().getMolecularSequence() );
                     }
                     break;
+                case SEQUENCE_MOL_SEQ_FASTA:
+                    final StringBuilder sb = new StringBuilder();
+                    if ( n.getNodeData().isHasSequence()
+                            && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getMolecularSequence() ) ) {
+                        if ( !ForesterUtil.isEmpty( n.getNodeData().getSequence().getName() ) ) {
+                            sb.append( SequenceWriter.toFasta( n.getNodeData().getSequence().getName(), n.getNodeData()
+                                    .getSequence().getMolecularSequence(), 60 ) );
+                        }
+                        else {
+                            sb.append( SequenceWriter.toFasta( n.getName(), n.getNodeData().getSequence()
+                                    .getMolecularSequence(), 60 ) );
+                        }
+                        data.add( sb.toString() );
+                    }
+                    break;
                 case SEQUENCE_ACC:
                     if ( n.getNodeData().isHasSequence() && ( n.getNodeData().getSequence().getAccession() != null )
                             && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getAccession().toString() ) ) {