in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / MainFrameApplication.java
index ae302bd..287a690 100644 (file)
@@ -59,10 +59,10 @@ import javax.swing.event.ChangeListener;
 import javax.swing.filechooser.FileFilter;
 import javax.swing.plaf.synth.SynthLookAndFeel;
 
+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.archaeopteryx.Util.GraphicsExportType;
 import org.forester.archaeopteryx.tools.AncestralTaxonomyInferrer;
 import org.forester.archaeopteryx.tools.GoAnnotation;
 import org.forester.archaeopteryx.tools.PhyloInferenceDialog;
@@ -88,6 +88,7 @@ import org.forester.msa.MsaFormatException;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.PhylogenyNode;
+import org.forester.phylogeny.PhylogenyNodeI.NH_CONVERSION_SUPPORT_VALUE_STYLE;
 import org.forester.phylogeny.data.Confidence;
 import org.forester.phylogeny.data.Taxonomy;
 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
@@ -233,6 +234,13 @@ public final class MainFrameApplication extends MainFrame {
     JMenuItem                                _read_values_jmi;
 
     private MainFrameApplication( final Phylogeny[] phys, final Configuration config, final String title ) {
+        this( phys, config, title, null );
+    }
+
+    private MainFrameApplication( final Phylogeny[] phys,
+                                  final Configuration config,
+                                  final String title,
+                                  final File current_dir ) {
         _configuration = config;
         if ( _configuration == null ) {
             throw new IllegalArgumentException( "configuration is null" );
@@ -264,19 +272,22 @@ public final class MainFrameApplication extends MainFrame {
             //UIManager.setLookAndFeel( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" );
         }
         catch ( final UnsupportedLookAndFeelException e ) {
-            Util.dieWithSystemError( "unsupported look and feel: " + e.toString() );
+            AptxUtil.dieWithSystemError( "unsupported look and feel: " + e.toString() );
         }
         catch ( final ClassNotFoundException e ) {
-            Util.dieWithSystemError( "class not found exception: " + e.toString() );
+            AptxUtil.dieWithSystemError( "class not found exception: " + e.toString() );
         }
         catch ( final InstantiationException e ) {
-            Util.dieWithSystemError( "instantiation exception: " + e.toString() );
+            AptxUtil.dieWithSystemError( "instantiation exception: " + e.toString() );
         }
         catch ( final IllegalAccessException e ) {
-            Util.dieWithSystemError( "illegal access exception: " + e.toString() );
+            AptxUtil.dieWithSystemError( "illegal access exception: " + e.toString() );
         }
         catch ( final Exception e ) {
-            Util.dieWithSystemError( e.toString() );
+            AptxUtil.dieWithSystemError( e.toString() );
+        }
+        if ( ( current_dir != null ) && current_dir.canRead() && current_dir.isDirectory() ) {
+            setCurrentDir( current_dir );
         }
         // hide until everything is ready
         setVisible( false );
@@ -409,7 +420,7 @@ public final class MainFrameApplication extends MainFrame {
         // addKeyListener( this );
         setVisible( true );
         if ( ( phys != null ) && ( phys.length > 0 ) ) {
-            Util.addPhylogeniesToTabs( phys, title, null, _configuration, _mainpanel );
+            AptxUtil.addPhylogeniesToTabs( phys, title, null, _configuration, _mainpanel );
             validate();
             getMainPanel().getControlPanel().showWholeAll();
             getMainPanel().getControlPanel().showWhole();
@@ -420,9 +431,54 @@ public final class MainFrameApplication extends MainFrame {
         System.gc();
     }
 
+    private MainFrameApplication( final Phylogeny[] phys, final Configuration config ) {
+        _configuration = config;
+        if ( _configuration == null ) {
+            throw new IllegalArgumentException( "configuration is null" );
+        }
+        setVisible( false );
+        setOptions( Options.createInstance( _configuration ) );
+        _mainpanel = new MainPanel( _configuration, this );
+        _open_filechooser = null;
+        _open_filechooser_for_species_tree = null;
+        _save_filechooser = null;
+        _writetopdf_filechooser = null;
+        _writetographics_filechooser = null;
+        _msa_filechooser = null;
+        _seqs_filechooser = null;
+        _values_filechooser = null;
+        _jmenubar = new JMenuBar();
+        buildFileMenu();
+        buildTypeMenu();
+        _contentpane = getContentPane();
+        _contentpane.setLayout( new BorderLayout() );
+        _contentpane.add( _mainpanel, BorderLayout.CENTER );
+        // App is this big
+        setSize( MainFrameApplication.FRAME_X_SIZE, MainFrameApplication.FRAME_Y_SIZE );
+        // The window listener
+        setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );
+        addWindowListener( new WindowAdapter() {
+
+            @Override
+            public void windowClosing( final WindowEvent e ) {
+                exit();
+            }
+        } );
+        //   setVisible( true );
+        if ( ( phys != null ) && ( phys.length > 0 ) ) {
+            AptxUtil.addPhylogeniesToTabs( phys, "", null, _configuration, _mainpanel );
+            validate();
+            getMainPanel().getControlPanel().showWholeAll();
+            getMainPanel().getControlPanel().showWhole();
+        }
+        //activateSaveAllIfNeeded();
+        // ...and its children
+        _contentpane.repaint();
+    }
+
     private MainFrameApplication( final Phylogeny[] phys, final String config_file, final String title ) {
         // Reads the config file (false, false => not url, not applet):
-        this( phys, new Configuration( config_file, false, false ), title );
+        this( phys, new Configuration( config_file, false, false, true ), title );
     }
 
     @Override
@@ -590,10 +646,10 @@ public final class MainFrameApplication extends MainFrame {
             _contentpane.repaint();
         }
         catch ( final Exception ex ) {
-            Util.unexpectedException( ex );
+            AptxUtil.unexpectedException( ex );
         }
         catch ( final Error err ) {
-            Util.unexpectedError( err );
+            AptxUtil.unexpectedError( err );
         }
     }
 
@@ -660,15 +716,15 @@ public final class MainFrameApplication extends MainFrame {
         _save_all_item.setEnabled( false );
         _file_jmenu.addSeparator();
         _file_jmenu.add( _write_to_pdf_item = new JMenuItem( "Export to PDF file ..." ) );
-        if ( Util.canWriteFormat( "tif" ) || Util.canWriteFormat( "tiff" ) || Util.canWriteFormat( "TIF" ) ) {
+        if ( AptxUtil.canWriteFormat( "tif" ) || AptxUtil.canWriteFormat( "tiff" ) || AptxUtil.canWriteFormat( "TIF" ) ) {
             _file_jmenu.add( _write_to_tif_item = new JMenuItem( "Export to TIFF file..." ) );
         }
         _file_jmenu.add( _write_to_png_item = new JMenuItem( "Export to PNG file..." ) );
         _file_jmenu.add( _write_to_jpg_item = new JMenuItem( "Export to JPG file..." ) );
-        if ( Util.canWriteFormat( "gif" ) ) {
+        if ( AptxUtil.canWriteFormat( "gif" ) ) {
             _file_jmenu.add( _write_to_gif_item = new JMenuItem( "Export to GIF file..." ) );
         }
-        if ( Util.canWriteFormat( "bmp" ) ) {
+        if ( AptxUtil.canWriteFormat( "bmp" ) ) {
             _file_jmenu.add( _write_to_bmp_item = new JMenuItem( "Export to BMP file..." ) );
         }
         _file_jmenu.addSeparator();
@@ -739,16 +795,17 @@ public final class MainFrameApplication extends MainFrame {
         _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( SHOW_OVERVIEW_LABEL ) );
+        _options_jmenu.add( _show_scale_cbmi = new JCheckBoxMenuItem( DISPLAY_SCALE_LABEL ) );
+        _options_jmenu
+                .add( _show_branch_length_values_cbmi = new JCheckBoxMenuItem( DISPLAY_BRANCH_LENGTH_VALUES_LABEL ) );
+        _options_jmenu.add( _show_confidence_stddev_cbmi = new JCheckBoxMenuItem( SHOW_CONF_STDDEV_LABEL ) );
         _options_jmenu.add( _show_default_node_shapes_cbmi = new JCheckBoxMenuItem( DISPLAY_NODE_BOXES_LABEL ) );
         _options_jmenu
                 .add( _taxonomy_colorize_node_shapes_cbmi = new JCheckBoxMenuItem( MainFrame.TAXONOMY_COLORIZE_NODE_SHAPES_LABEL ) );
         _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( DISPLAY_SCALE_LABEL ) );
-        _options_jmenu
-                .add( _show_branch_length_values_cbmi = new JCheckBoxMenuItem( DISPLAY_BRANCH_LENGTH_VALUES_LABEL ) );
-        _options_jmenu.add( _show_overview_cbmi = new JCheckBoxMenuItem( SHOW_OVERVIEW_LABEL ) );
         _options_jmenu.add( _label_direction_cbmi = new JCheckBoxMenuItem( LABEL_DIRECTION_LABEL ) );
         _label_direction_cbmi.setToolTipText( LABEL_DIRECTION_TIP );
         _options_jmenu.add( _color_labels_same_as_parent_branch = new JCheckBoxMenuItem( COLOR_LABELS_LABEL ) );
@@ -782,13 +839,19 @@ public final class MainFrameApplication extends MainFrame {
         _options_jmenu.add( _print_size_mi = new JMenuItem( "" ) );
         _options_jmenu.add( _choose_pdf_width_mi = new JMenuItem( "" ) );
         _options_jmenu.addSeparator();
+        _options_jmenu.add( customizeMenuItemAsLabel( new JMenuItem( "Newick/NHX/Nexus Input:" ), getConfiguration() ) );
         _options_jmenu
-                .add( customizeMenuItemAsLabel( new JMenuItem( "Newick/NHX/Nexus Parsing:" ), getConfiguration() ) );
-        _options_jmenu
-                .add( _internal_number_are_confidence_for_nh_parsing_cbmi = new JCheckBoxMenuItem( "Internal Numbers Are Confidence Values" ) );
+                .add( _internal_number_are_confidence_for_nh_parsing_cbmi = new JCheckBoxMenuItem( "Internal Node Names are Confidence Values" ) );
         _options_jmenu.add( _replace_underscores_cbmi = new JCheckBoxMenuItem( "Replace Underscores with Spaces" ) );
         _options_jmenu
                 .add( _extract_pfam_style_tax_codes_cbmi = new JCheckBoxMenuItem( "Extract Taxonomy Codes from Pfam-style Labels" ) );
+        _options_jmenu.add( customizeMenuItemAsLabel( new JMenuItem( "Newick/Nexus Output:" ), getConfiguration() ) );
+        _options_jmenu
+                .add( _use_brackets_for_conf_in_nh_export_cbmi = new JCheckBoxMenuItem( USE_BRACKETS_FOR_CONF_IN_NH_LABEL ) );
+        _use_brackets_for_conf_in_nh_export_cbmi
+                .setToolTipText( "e.g. \"0.1[90]\" for a branch with support 90 and a length of 0.1" );
+        _options_jmenu
+                .add( _use_internal_names_for_conf_in_nh_export_cbmi = new JCheckBoxMenuItem( USE_INTERNAL_NAMES_FOR_CONF_IN_NH_LABEL ) );
         customizeJMenuItem( _choose_font_mi );
         customizeJMenuItem( _choose_minimal_confidence_mi );
         customizeJMenuItem( _switch_colors_mi );
@@ -830,6 +893,11 @@ public final class MainFrameApplication extends MainFrame {
         customizeCheckBoxMenuItem( _print_using_actual_size_cbmi, getOptions().isPrintUsingActualSize() );
         customizeCheckBoxMenuItem( _graphics_export_using_actual_size_cbmi, getOptions()
                 .isGraphicsExportUsingActualSize() );
+        customizeCheckBoxMenuItem( _show_confidence_stddev_cbmi, getOptions().isShowConfidenceStddev() );
+        customizeCheckBoxMenuItem( _use_brackets_for_conf_in_nh_export_cbmi, getOptions()
+                .getNhConversionSupportValueStyle() == NH_CONVERSION_SUPPORT_VALUE_STYLE.IN_SQUARE_BRACKETS );
+        customizeCheckBoxMenuItem( _use_internal_names_for_conf_in_nh_export_cbmi, getOptions()
+                .getNhConversionSupportValueStyle() == NH_CONVERSION_SUPPORT_VALUE_STYLE.AS_INTERNAL_NODE_NAMES );
         _jmenubar.add( _options_jmenu );
     }
 
@@ -852,10 +920,10 @@ public final class MainFrameApplication extends MainFrame {
         _tools_menu.add( _collapse_species_specific_subtrees = new JMenuItem( "Collapse Species-Specific Subtrees" ) );
         customizeJMenuItem( _collapse_species_specific_subtrees );
         _tools_menu
-                .add( _collapse_below_threshold = new JMenuItem( "Collapse Branches with Confidence Below Threshold" ) );
+                .add( _collapse_below_threshold = new JMenuItem( "Collapse Branches with Confidence Below Threshold into Multifurcations" ) );
         customizeJMenuItem( _collapse_below_threshold );
         _collapse_below_threshold
-                .setToolTipText( "To permanently collapse branches without at least one support value above a given threshold" );
+                .setToolTipText( "To collapse branches with confidence values below a threshold into multifurcations (in the case of multiple confidences per branch: without at least one confidence value above a threshold)" );
         _tools_menu.addSeparator();
         _tools_menu
                 .add( _move_node_names_to_tax_sn_jmi = new JMenuItem( "Transfer Node Names to Taxonomic Scientific Names" ) );
@@ -1045,8 +1113,14 @@ public final class MainFrameApplication extends MainFrame {
                 phy.hashIDs();
                 phy.recalculateNumberOfExternalDescendants( true );
                 getCurrentTreePanel().resetNodeIdToDistToLeafMap();
+                getCurrentTreePanel().updateSetOfCollapsedExternalNodes();
+                getCurrentTreePanel().calculateLongestExtNodeInfo();
+                getCurrentTreePanel().setNodeInPreorderToNull();
+                getCurrentTreePanel().recalculateMaxDistanceToRoot();
+                getCurrentTreePanel().resetPreferredSize();
                 getCurrentTreePanel().setEdited( true );
                 getCurrentTreePanel().repaint();
+                repaint();
             }
             if ( to_be_removed.size() > 0 ) {
                 JOptionPane.showMessageDialog( this, "Collapsed " + to_be_removed.size()
@@ -1293,6 +1367,13 @@ public final class MainFrameApplication extends MainFrame {
         System.exit( 0 );
     }
 
+    public void end() {
+        _mainpanel.terminate();
+        _contentpane.removeAll();
+        setVisible( false );
+        dispose();
+    }
+
     private void extractTaxCodeFromNodeNames() {
         if ( getCurrentTreePanel() != null ) {
             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
@@ -1414,7 +1495,7 @@ public final class MainFrameApplication extends MainFrame {
         phy.setRoot( node );
         phy.setRooted( true );
         phys[ 0 ] = phy;
-        Util.addPhylogeniesToTabs( phys, "", "", getConfiguration(), getMainPanel() );
+        AptxUtil.addPhylogeniesToTabs( phys, "", "", getConfiguration(), getMainPanel() );
         _mainpanel.getControlPanel().showWhole();
         _mainpanel.getCurrentTreePanel().setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
         _mainpanel.getOptions().setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
@@ -1773,11 +1854,11 @@ public final class MainFrameApplication extends MainFrame {
                                 }
                             }
                         }
-                        Util.addPhylogeniesToTabs( phys,
-                                                   file.getName(),
-                                                   file.getAbsolutePath(),
-                                                   getConfiguration(),
-                                                   getMainPanel() );
+                        AptxUtil.addPhylogeniesToTabs( phys,
+                                                       file.getName(),
+                                                       file.getAbsolutePath(),
+                                                       getConfiguration(),
+                                                       getMainPanel() );
                         _mainpanel.getControlPanel().showWhole();
                         if ( nhx_or_nexus && one_desc ) {
                             JOptionPane
@@ -2077,11 +2158,11 @@ public final class MainFrameApplication extends MainFrame {
                         PhylogenyMethods.transferInternalNodeNamesToConfidence( phy );
                     }
                 }
-                Util.addPhylogeniesToTabs( phys,
-                                           new File( url.getFile() ).getName(),
-                                           new File( url.getFile() ).toString(),
-                                           getConfiguration(),
-                                           getMainPanel() );
+                AptxUtil.addPhylogeniesToTabs( phys,
+                                               new File( url.getFile() ).getName(),
+                                               new File( url.getFile() ).toString(),
+                                               getConfiguration(),
+                                               getMainPanel() );
                 _mainpanel.getControlPanel().showWhole();
             }
         }
@@ -2260,7 +2341,7 @@ public final class MainFrameApplication extends MainFrame {
     private boolean writeAsNewHampshire( final Phylogeny t, boolean exception, final File file ) {
         try {
             final PhylogenyWriter writer = new PhylogenyWriter();
-            writer.toNewHampshire( t, false, true, file );
+            writer.toNewHampshire( t, false, true, getOptions().getNhConversionSupportValueStyle(), file );
         }
         catch ( final Exception e ) {
             exception = true;
@@ -2272,7 +2353,7 @@ public final class MainFrameApplication extends MainFrame {
     private boolean writeAsNexus( final Phylogeny t, boolean exception, final File file ) {
         try {
             final PhylogenyWriter writer = new PhylogenyWriter();
-            writer.toNexus( file, t );
+            writer.toNexus( file, t, getOptions().getNhConversionSupportValueStyle() );
         }
         catch ( final Exception e ) {
             exception = true;
@@ -2305,20 +2386,20 @@ public final class MainFrameApplication extends MainFrame {
         return exception;
     }
 
-    private void writePhylogenyToGraphicsFile( final String file_name, final GraphicsExportType type ) {
+    void writePhylogenyToGraphicsFile( final String file_name, final GraphicsExportType type ) {
         _mainpanel.getCurrentTreePanel().setParametersForPainting( _mainpanel.getCurrentTreePanel().getWidth(),
                                                                    _mainpanel.getCurrentTreePanel().getHeight(),
                                                                    true );
         String file_written_to = "";
         boolean error = false;
         try {
-            file_written_to = Util.writePhylogenyToGraphicsFile( file_name,
-                                                                 _mainpanel.getCurrentTreePanel().getWidth(),
-                                                                 _mainpanel.getCurrentTreePanel().getHeight(),
-                                                                 _mainpanel.getCurrentTreePanel(),
-                                                                 _mainpanel.getControlPanel(),
-                                                                 type,
-                                                                 getOptions() );
+            file_written_to = AptxUtil.writePhylogenyToGraphicsFile( file_name,
+                                                                     _mainpanel.getCurrentTreePanel().getWidth(),
+                                                                     _mainpanel.getCurrentTreePanel().getHeight(),
+                                                                     _mainpanel.getCurrentTreePanel(),
+                                                                     _mainpanel.getControlPanel(),
+                                                                     type,
+                                                                     getOptions() );
         }
         catch ( final IOException e ) {
             error = true;
@@ -2530,10 +2611,21 @@ public final class MainFrameApplication extends MainFrame {
         return new MainFrameApplication( phys, config, title );
     }
 
+    public static MainFrameApplication createInstance( final Phylogeny[] phys, final Configuration config ) {
+        return new MainFrameApplication( phys, config );
+    }
+
     static MainFrame createInstance( final Phylogeny[] phys, final String config_file_name, final String title ) {
         return new MainFrameApplication( phys, config_file_name, title );
     }
 
+    public static MainFrame createInstance( final Phylogeny[] phys,
+                                            final Configuration config,
+                                            final String title,
+                                            final File current_dir ) {
+        return new MainFrameApplication( phys, config, title, current_dir );
+    }
+
     static void setTextForGraphicsSizeChooserMenuItem( final JMenuItem mi, final Options o ) {
         mi.setText( "Enter Default Size for Graphics Export... (current: " + o.getPrintSizeX() + ", "
                 + o.getPrintSizeY() + ")" );