in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / ArchaeopteryxE.java
index 7dbed25..7ba34f4 100644 (file)
@@ -7,6 +7,7 @@ import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ComponentAdapter;
 import java.awt.event.ComponentEvent;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.net.URL;
@@ -25,11 +26,14 @@ import javax.swing.UnsupportedLookAndFeelException;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
 
+import org.apache.commons.codec.binary.Base64;
+import org.forester.archaeopteryx.AptxUtil.GraphicsExportType;
 import org.forester.archaeopteryx.Options.CLADOGRAM_TYPE;
 import org.forester.archaeopteryx.Options.NODE_LABEL_DIRECTION;
 import org.forester.archaeopteryx.Options.PHYLOGENY_GRAPHICS_TYPE;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.data.SequenceRelation;
+import org.forester.util.ForesterConstants;
 import org.forester.util.ForesterUtil;
 
 // Use like this:
@@ -113,6 +117,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
     private JMenuItem            _cycle_node_fill_mi;
     private JMenuItem            _choose_node_size_mi;
     private JCheckBoxMenuItem    _taxonomy_colorize_node_shapes_cbmi;
+    private JCheckBoxMenuItem    _show_confidence_stddev_cbmi;
 
     @Override
     public void actionPerformed( final ActionEvent e ) {
@@ -232,6 +237,9 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         else if ( o == _show_branch_length_values_cbmi ) {
             updateOptions( getOptions() );
         }
+        else if ( o == _show_confidence_stddev_cbmi ) {
+            updateOptions( getOptions() );
+        }
         else if ( o == _label_direction_cbmi ) {
             updateOptions( getOptions() );
         }
@@ -273,7 +281,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         }
         else if ( o == _website_item ) {
             try {
-                Util.openWebsite( Constants.APTX_WEB_SITE, true, this );
+                AptxUtil.openWebsite( Constants.APTX_WEB_SITE, true, this );
             }
             catch ( final IOException e1 ) {
                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
@@ -281,7 +289,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         }
         else if ( o == _phyloxml_website_item ) {
             try {
-                Util.openWebsite( Constants.PHYLOXML_WEB_SITE, true, this );
+                AptxUtil.openWebsite( Constants.PHYLOXML_WEB_SITE, true, this );
             }
             catch ( final IOException e1 ) {
                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
@@ -289,7 +297,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         }
         else if ( o == _aptx_ref_item ) {
             try {
-                Util.openWebsite( Constants.APTX_REFERENCE_URL, true, this );
+                AptxUtil.openWebsite( Constants.APTX_REFERENCE_URL, true, this );
             }
             catch ( final IOException e1 ) {
                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
@@ -297,7 +305,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         }
         else if ( o == _phyloxml_ref_item ) {
             try {
-                Util.openWebsite( Constants.PHYLOXML_REFERENCE_URL, true, this );
+                AptxUtil.openWebsite( Constants.PHYLOXML_REFERENCE_URL, true, this );
             }
             catch ( final IOException e1 ) {
                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
@@ -306,6 +314,68 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         repaint();
     }
 
+    /**
+     * This method returns the current phylogeny as a string in the chosen format
+     * 
+     * @param format must be NH, NHX, NEXUS or PHYLOXML
+     * @return the phylogeny string
+     * @author Herve Menager
+     */
+    public String getCurrentPhylogeny( final String format ) {
+        removeTextFrame();
+        if ( ( getMainPanel().getCurrentPhylogeny() == null ) || getMainPanel().getCurrentPhylogeny().isEmpty()
+                || ( getMainPanel().getCurrentPhylogeny().getNumberOfExternalNodes() > 10000 ) ) {
+            return new String();
+        }
+        switch ( ForesterConstants.PhylogeneticTreeFormats.valueOf( format ) ) {
+            case NH:
+                return getMainPanel().getCurrentPhylogeny().toNewHampshire();
+            case NHX:
+                return getMainPanel().getCurrentPhylogeny().toNewHampshireX();
+            case NEXUS:
+                return getMainPanel().getCurrentPhylogeny().toNexus();
+            case PHYLOXML:
+                return getMainPanel().getCurrentPhylogeny().toPhyloXML( -1 );
+            default:
+                break;
+        }
+        return new String();
+    }
+
+    /**
+     * This method returns a view of the current phylogeny in a chosen 
+     * graphics format, base64-encoded in a string so that in can be used
+     * from javascript.
+     * 
+     * @param format must be GraphicsExportType (gif, jpg, pdf, png, tif, bmp)
+     * @return the phylogeny string
+     * @author Herve Menager
+     */
+    public String getCurrentPhylogenyGraphicsAsBase64EncodedString( final String format ) {
+        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        try {
+            AptxUtil.writePhylogenyToGraphicsByteArrayOutputStream( baos,
+                                                                    _main_panel.getWidth(),
+                                                                    _main_panel.getHeight(),
+                                                                    getCurrentTreePanel(),
+                                                                    getCurrentTreePanel().getControlPanel(),
+                                                                    GraphicsExportType.valueOf( format ),
+                                                                    getOptions() );
+        }
+        catch ( final IOException ioe ) {
+            ForesterUtil.printErrorMessage( NAME, ioe.toString() );
+            ioe.printStackTrace();
+            JOptionPane.showMessageDialog( this,
+                                           NAME + ": Failed to generate graphics: " + "\nException: " + ioe,
+                                           "Failed to generate graphics",
+                                           JOptionPane.ERROR_MESSAGE );
+            return null;
+        }
+        final byte[] bytes = baos.toByteArray();
+        final String dataImg = Base64.encodeBase64String( bytes );
+        return dataImg;
+    }
+
     void buildFontSizeMenu() {
         _font_size_menu = MainFrame.createMenu( MainFrame.FONT_SIZE_MENU_LABEL, getConfiguration() );
         _font_size_menu.add( _super_tiny_fonts_mi = new JMenuItem( "Super tiny fonts" ) );
@@ -376,7 +446,11 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         _radio_group_1.add( _ext_node_dependent_cladogram_rbmi );
         _radio_group_1.add( _uniform_cladograms_rbmi );
         _radio_group_1.add( _non_lined_up_cladograms_rbmi );
-        //
+        _options_jmenu.add( _show_overview_cbmi = new JCheckBoxMenuItem( MainFrame.SHOW_OVERVIEW_LABEL ) );
+        _options_jmenu.add( _show_scale_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_SCALE_LABEL ) );
+        _options_jmenu
+                .add( _show_branch_length_values_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_BRANCH_LENGTH_VALUES_LABEL ) );
+        _options_jmenu.add( _show_confidence_stddev_cbmi = new JCheckBoxMenuItem( MainFrame.SHOW_CONF_STDDEV_LABEL ) );
         _options_jmenu
                 .add( _show_default_node_shapes_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_NODE_BOXES_LABEL ) );
         _options_jmenu
@@ -384,11 +458,6 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         _options_jmenu.add( _cycle_node_shape_mi = new JMenuItem( MainFrame.CYCLE_NODE_SHAPE_LABEL ) );
         _options_jmenu.add( _cycle_node_fill_mi = new JMenuItem( MainFrame.CYCLE_NODE_FILL_LABEL ) );
         _options_jmenu.add( _choose_node_size_mi = new JMenuItem( MainFrame.CHOOSE_NODE_SIZE_LABEL ) );
-        //
-        _options_jmenu.add( _show_scale_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_SCALE_LABEL ) );
-        _options_jmenu
-                .add( _show_branch_length_values_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_BRANCH_LENGTH_VALUES_LABEL ) );
-        _options_jmenu.add( _show_overview_cbmi = new JCheckBoxMenuItem( MainFrame.SHOW_OVERVIEW_LABEL ) );
         _options_jmenu.add( _label_direction_cbmi = new JCheckBoxMenuItem( MainFrame.LABEL_DIRECTION_LABEL ) );
         _options_jmenu
                 .add( _color_labels_same_as_parent_branch = new JCheckBoxMenuItem( MainFrame.COLOR_LABELS_LABEL ) );
@@ -440,6 +509,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         customizeCheckBoxMenuItem( _show_overview_cbmi, getOptions().isShowOverview() );
         customizeCheckBoxMenuItem( _search_whole_words_only_cbmi, getOptions().isMatchWholeTermsOnly() );
         customizeCheckBoxMenuItem( _inverse_search_result_cbmi, getOptions().isInverseSearchResult() );
+        customizeCheckBoxMenuItem( _show_confidence_stddev_cbmi, getOptions().isShowConfidenceStddev() );
         _jmenubar.add( _options_jmenu );
     }
 
@@ -574,7 +644,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
 
     @Override
     public void destroy() {
-        Util.printAppletMessage( NAME, "going to be destroyed " );
+        AptxUtil.printAppletMessage( NAME, "going to be destroyed " );
         removeTextFrame();
         if ( getMainPanel() != null ) {
             getMainPanel().terminate();
@@ -608,15 +678,15 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
     @Override
     public void init() {
         final String config_filename = getParameter( Constants.APPLET_PARAM_NAME_FOR_CONFIG_FILE_URL );
-        Util.printAppletMessage( NAME, "URL for configuration file is: " + config_filename );
-        final Configuration configuration = new Configuration( config_filename, true, true );
+        AptxUtil.printAppletMessage( NAME, "URL for configuration file is: " + config_filename );
+        final Configuration configuration = new Configuration( config_filename, true, true, true );
         setConfiguration( configuration );
         setOptions( Options.createInstance( configuration ) );
         setupUI();
         URL phys_url = null;
         Phylogeny[] phys = null;
         final String phys_url_string = getParameter( Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD );
-        Util.printAppletMessage( NAME, "URL for phylogenies is " + phys_url_string );
+        AptxUtil.printAppletMessage( NAME, "URL for phylogenies is " + phys_url_string );
         // Get URL to tree file
         if ( phys_url_string != null ) {
             try {
@@ -632,7 +702,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         // Load the tree from URL
         if ( phys_url != null ) {
             try {
-                phys = Util.readPhylogeniesFromUrl( phys_url, getConfiguration().isValidatePhyloXmlAgainstSchema() );
+                phys = AptxUtil.readPhylogeniesFromUrl( phys_url, getConfiguration().isValidatePhyloXmlAgainstSchema() );
             }
             catch ( final Exception e ) {
                 ForesterUtil.printErrorMessage( NAME, e.toString() );
@@ -652,7 +722,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
             return;
         }
         else {
-            Util.printAppletMessage( NAME, "loaded " + phys.length + " phylogenies from: " + phys_url );
+            AptxUtil.printAppletMessage( NAME, "loaded " + phys.length + " phylogenies from: " + phys_url );
         }
         setVisible( false );
         setMainPanel( new MainPanelApplets( getConfiguration(), this ) );
@@ -686,23 +756,23 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
             }
         } );
         if ( getConfiguration().isUseTabbedDisplay() ) {
-            Util.printAppletMessage( NAME, "using tabbed display" );
-            Util.addPhylogeniesToTabs( phys,
-                                       new File( phys_url.getFile() ).getName(),
-                                       phys_url.toString(),
-                                       getConfiguration(),
-                                       getMainPanel() );
+            AptxUtil.printAppletMessage( NAME, "using tabbed display" );
+            AptxUtil.addPhylogeniesToTabs( phys,
+                                           new File( phys_url.getFile() ).getName(),
+                                           phys_url.toString(),
+                                           getConfiguration(),
+                                           getMainPanel() );
         }
         else {
-            Util.printAppletMessage( NAME, "not using tabbed display" );
-            Util.addPhylogenyToPanel( phys, getConfiguration(), getMainPanel() );
+            AptxUtil.printAppletMessage( NAME, "not using tabbed display" );
+            AptxUtil.addPhylogenyToPanel( phys, getConfiguration(), getMainPanel() );
         }
         validate();
         setName( NAME );
         getMainPanel().getControlPanel().showWholeAll();
         getMainPanel().getControlPanel().showWhole();
         System.gc();
-        Util.printAppletMessage( NAME, "successfully initialized" );
+        AptxUtil.printAppletMessage( NAME, "successfully initialized" );
         /* GUILHEM_BEG */
         getCurrentTreePanel().getControlPanel().getSequenceRelationTypeBox().removeAllItems();
         for( final SequenceRelation.SEQUENCE_RELATION_TYPE type : getMainPanel().getCurrentPhylogeny()
@@ -762,7 +832,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
 
     private void removeBranchColors() {
         if ( getMainPanel().getCurrentPhylogeny() != null ) {
-            Util.removeBranchColors( getMainPanel().getCurrentPhylogeny() );
+            AptxUtil.removeBranchColors( getMainPanel().getCurrentPhylogeny() );
         }
     }
 
@@ -859,19 +929,19 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
             }
         }
         catch ( final UnsupportedLookAndFeelException e ) {
-            Util.dieWithSystemError( "UnsupportedLookAndFeelException: " + e.toString() );
+            AptxUtil.dieWithSystemError( "UnsupportedLookAndFeelException: " + e.toString() );
         }
         catch ( final ClassNotFoundException e ) {
-            Util.dieWithSystemError( "ClassNotFoundException: " + e.toString() );
+            AptxUtil.dieWithSystemError( "ClassNotFoundException: " + e.toString() );
         }
         catch ( final InstantiationException e ) {
-            Util.dieWithSystemError( "InstantiationException: " + e.toString() );
+            AptxUtil.dieWithSystemError( "InstantiationException: " + e.toString() );
         }
         catch ( final IllegalAccessException e ) {
-            Util.dieWithSystemError( "IllegalAccessException: " + e.toString() );
+            AptxUtil.dieWithSystemError( "IllegalAccessException: " + e.toString() );
         }
         catch ( final Exception e ) {
-            Util.dieWithSystemError( e.toString() );
+            AptxUtil.dieWithSystemError( e.toString() );
         }
     }
 
@@ -883,7 +953,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         requestFocus();
         requestFocusInWindow();
         requestFocus();
-        Util.printAppletMessage( NAME, "started" );
+        AptxUtil.printAppletMessage( NAME, "started" );
     }
 
     void switchColors() {
@@ -952,6 +1022,8 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
             }
         }
         options.setShowOverview( ( _show_overview_cbmi != null ) && _show_overview_cbmi.isSelected() );
+        options.setShowConfidenceStddev( ( _show_confidence_stddev_cbmi != null )
+                && _show_confidence_stddev_cbmi.isSelected() );
         if ( ( _show_branch_length_values_cbmi != null ) && _show_branch_length_values_cbmi.isEnabled() ) {
             options.setShowBranchLengthValues( _show_branch_length_values_cbmi.isSelected() );
         }
@@ -1005,7 +1077,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
                 || ( getMainPanel().getCurrentPhylogeny().getNumberOfExternalNodes() > 10000 ) ) {
             return;
         }
-        _textframe = TextFrame.instantiate( getMainPanel().getCurrentPhylogeny().toNewHampshire( false ) );
+        _textframe = TextFrame.instantiate( getMainPanel().getCurrentPhylogeny().toNewHampshire() );
     }
 
     void viewAsNHX() {