From 4375ffa65b125b63ba3c36a17a41458b4f74b8db Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Sat, 7 Mar 2015 02:16:37 +0000 Subject: [PATCH] in progress --- .../src/org/forester/archaeopteryx/MainFrame.java | 2185 ++++++++++++-------- .../forester/archaeopteryx/MainFrameApplet.java | 97 +- .../archaeopteryx/MainFrameApplication.java | 2001 +++++++----------- 3 files changed, 2145 insertions(+), 2138 deletions(-) diff --git a/forester/java/src/org/forester/archaeopteryx/MainFrame.java b/forester/java/src/org/forester/archaeopteryx/MainFrame.java index e8d28e0..85d2d68 100644 --- a/forester/java/src/org/forester/archaeopteryx/MainFrame.java +++ b/forester/java/src/org/forester/archaeopteryx/MainFrame.java @@ -52,16 +52,17 @@ import javax.swing.JPanel; import javax.swing.JRadioButtonMenuItem; import javax.swing.JTextField; import javax.swing.SwingUtilities; +import javax.swing.filechooser.FileFilter; +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.tools.InferenceManager; import org.forester.archaeopteryx.tools.ProcessPool; import org.forester.archaeopteryx.tools.ProcessRunning; -import org.forester.archaeopteryx.webservices.PhylogeniesWebserviceClient; -import org.forester.archaeopteryx.webservices.WebservicesManager; import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION; +import org.forester.io.writers.PhylogenyWriter; import org.forester.phylogeny.Phylogeny; import org.forester.phylogeny.PhylogenyMethods; import org.forester.phylogeny.PhylogenyMethods.DESCENDANT_SORT_PRIORITY; @@ -79,208 +80,528 @@ import org.forester.util.ForesterConstants; import org.forester.util.ForesterUtil; import org.forester.util.WindowsUtils; +class DefaultFilter extends FileFilter { + + @Override + public boolean accept( final File f ) { + final String file_name = f.getName().trim().toLowerCase(); + return file_name.endsWith( ".nh" ) || file_name.endsWith( ".newick" ) || file_name.endsWith( ".phy" ) + || file_name.endsWith( ".nwk" ) || file_name.endsWith( ".phb" ) || file_name.endsWith( ".ph" ) + || file_name.endsWith( ".tr" ) || file_name.endsWith( ".dnd" ) || file_name.endsWith( ".tree" ) + || file_name.endsWith( ".nhx" ) || file_name.endsWith( ".xml" ) || file_name.endsWith( ".phyloxml" ) + || file_name.endsWith( "phylo.xml" ) || file_name.endsWith( ".pxml" ) || file_name.endsWith( ".nexus" ) + || file_name.endsWith( ".nx" ) || file_name.endsWith( ".nex" ) || file_name.endsWith( ".tre" ) + || file_name.endsWith( ".zip" ) || file_name.endsWith( ".tol" ) || file_name.endsWith( ".tolxml" ) + || file_name.endsWith( ".con" ) || f.isDirectory(); + } + + @Override + public String getDescription() { + return "All supported files (*.xml, *.phyloxml, *phylo.xml, *.nhx, *.nh, *.newick, *.nex, *.nexus, *.phy, *.tre, *.tree, *.tol, ...)"; + } +} + +class GraphicsFileFilter extends FileFilter { + + @Override + public boolean accept( final File f ) { + final String file_name = f.getName().trim().toLowerCase(); + return file_name.endsWith( ".jpg" ) || file_name.endsWith( ".jpeg" ) || file_name.endsWith( ".png" ) + || file_name.endsWith( ".gif" ) || file_name.endsWith( ".bmp" ) || f.isDirectory(); + } + + @Override + public String getDescription() { + return "Image files (*.jpg, *.jpeg, *.png, *.gif, *.bmp)"; + } +} + public abstract class MainFrame extends JFrame implements ActionListener { - static final String USE_MOUSEWHEEL_SHIFT_TO_ROTATE = "In this display type, use mousewheel + Shift to rotate [or A and S]"; - static final String PHYLOXML_REF_TOOL_TIP = Constants.PHYLOXML_REFERENCE; //TODO //FIXME - static final String APTX_REF_TOOL_TIP = Constants.APTX_REFERENCE; - private static final long serialVersionUID = 3655000897845508358L; - final static Font menu_font = new Font( Configuration.getDefaultFontFamilyName(), - Font.PLAIN, - 10 ); - static final String TYPE_MENU_HEADER = "Type"; - static final String RECTANGULAR_TYPE_CBMI_LABEL = "Rectangular"; - static final String EURO_TYPE_CBMI_LABEL = "Euro Type"; - static final String CURVED_TYPE_CBMI_LABEL = "Curved"; - static final String TRIANGULAR_TYPE_CBMI_LABEL = "Triangular"; - static final String CONVEX_TYPE_CBMI_LABEL = "Convex"; - static final String ROUNDED_TYPE_CBMI_LABEL = "Rounded"; - static final String UNROOTED_TYPE_CBMI_LABEL = "Unrooted (alpha)"; //TODO - static final String CIRCULAR_TYPE_CBMI_LABEL = "Circular (alpha)"; //TODO - static final String OPTIONS_HEADER = "Options"; - static final String SEARCH_SUBHEADER = "Search:"; - static final String DISPLAY_SUBHEADER = "Display:"; - static final String SEARCH_TERMS_ONLY_LABEL = "Match Complete Terms Only"; - static final String SEARCH_REGEX_LABEL = "Search with Regular Expressions"; - static final String SEARCH_CASE_SENSITIVE_LABEL = "Case Sensitive"; - static final String INVERSE_SEARCH_RESULT_LABEL = "Negate Result"; - static final String COLOR_BY_TAXONOMIC_GROUP = "Colorize by Taxonomic Group"; - static final String DISPLAY_SCALE_LABEL = "Scale"; - static final String NON_LINED_UP_CLADOGRAMS_LABEL = "Non-Lined Up Cladograms"; - static final String UNIFORM_CLADOGRAMS_LABEL = "Total Node Sum Dependent Cladograms"; - static final String LABEL_DIRECTION_LABEL = "Radial Labels"; - static final String LABEL_DIRECTION_TIP = "To use radial node labels in radial and unrooted display types"; - static final String SEARCH_WITH_REGEX_TIP = "To search using regular expressions (~Java/Perl syntax). For example, use \"^B.+\\d{2,}$\" to search for everything starting with a B and ending with at least two digits."; - static final String SCREEN_ANTIALIAS_LABEL = "Antialias"; - static final String COLOR_LABELS_LABEL = "Colorize Labels Same as Parent Branch"; - static final String BG_GRAD_LABEL = "Background Color Gradient"; - static final String DISPLAY_NODE_BOXES_LABEL_EXT = "Shapes for External Nodes"; - static final String DISPLAY_NODE_BOXES_LABEL_INT = "Shapes for Internal Nodes"; - static final String DISPLAY_NODE_BOXES_LABEL_MARKED = "Shapes for Nodes with Visual Data"; - static final String SHOW_OVERVIEW_LABEL = "Overview"; - static final String FONT_SIZE_MENU_LABEL = "Font Size"; - static final String NONUNIFORM_CLADOGRAMS_LABEL = "External Node Sum Dependent Cladograms"; - static final String SHOW_DOMAIN_LABELS_LABEL = "Domain Labels"; - static final String SHOW_ANN_REF_SOURCE_LABEL = "Seq Annotation Ref Sources"; - static final String COLOR_LABELS_TIP = "To use parent branch colors for node labels as well, need to turn off taxonomy dependent colorization and turn on branch colorization for this to become apparent"; - static final String ABBREV_SN_LABEL = "Abbreviate Scientific Taxonomic Names"; - static final String TAXONOMY_COLORIZE_NODE_SHAPES_LABEL = "Colorize Node Shapes According to Taxonomy"; - static final String CYCLE_NODE_SHAPE_LABEL = "Cycle Node Shapes"; - static final String CYCLE_NODE_FILL_LABEL = "Cycle Node Fill Type"; - static final String CHOOSE_NODE_SIZE_LABEL = "Choose Node Shape Size"; - static final String SHOW_CONF_STDDEV_LABEL = "Confidence Standard Deviations"; - static final String USE_BRACKETS_FOR_CONF_IN_NH_LABEL = "Use Brackets for Confidence Values"; - static final String USE_INTERNAL_NAMES_FOR_CONF_IN_NH_LABEL = "Use Internal Node Names for Confidence Values"; - static final String SHOW_BASIC_TREE_INFORMATION_LABEL = "Basic Tree Information"; - static final String RIGHT_LINE_UP_DOMAINS = "Right-align Domain Architectures"; - static final String LINE_UP_RENDERABLE_DATA = "Line Up Diagrams (such as Domain Architectures)"; - JMenuBar _jmenubar; - JMenu _file_jmenu; - JMenu _tools_menu; - JMenu _view_jmenu; - JMenu _options_jmenu; - JMenu _font_size_menu; - JMenu _help_jmenu; - JMenuItem[] _load_phylogeny_from_webservice_menu_items; + /** + * Display the about box. + */ + static void about() { + final StringBuffer about = new StringBuffer( "Archaeopteryx\nVersion " + Constants.VERSION + "\n" ); + about.append( "Copyright (C) 2014 Christian M Zmasek\n" ); + about.append( "All Rights Reserved\n" ); + about.append( "License: GNU Lesser General Public License (LGPL)\n" ); + about.append( "Last modified: " + Constants.PRG_DATE + "\n" ); + about.append( "Based on: " + ForesterUtil.getForesterLibraryInformation() + "\n" ); + about.append( "phyloXML version : " + ForesterConstants.PHYLO_XML_VERSION + "\n" ); + about.append( "phyloXML location: " + ForesterConstants.PHYLO_XML_LOCATION + "\n" ); + if ( !ForesterUtil.isEmpty( ForesterUtil.JAVA_VERSION ) && !ForesterUtil.isEmpty( ForesterUtil.JAVA_VENDOR ) ) { + about.append( "[your Java version: " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]\n" ); + } + if ( !ForesterUtil.isEmpty( ForesterUtil.OS_NAME ) && !ForesterUtil.isEmpty( ForesterUtil.OS_ARCH ) + && !ForesterUtil.isEmpty( ForesterUtil.OS_VERSION ) ) { + about.append( "[your OS: " + ForesterUtil.OS_NAME + " " + ForesterUtil.OS_ARCH + " " + + ForesterUtil.OS_VERSION + "]\n" ); + } + final Runtime rt = java.lang.Runtime.getRuntime(); + final long free_memory = rt.freeMemory() / 1000000; + final long total_memory = rt.totalMemory() / 1000000; + about.append( "[free memory: " + free_memory + "MB, total memory: " + total_memory + "MB]\n" ); + about.append( "[locale: " + Locale.getDefault() + "]\n" ); + about.append( "References:\n" ); + about.append( Constants.PHYLOXML_REFERENCE_SHORT + "\n" ); + about.append( "For more information & download:\n" ); + about.append( Constants.APTX_WEB_SITE + "\n" ); + about.append( "Documentation:\n" ); + about.append( Constants.APTX_DOC_SITE + "\n" ); + about.append( "Comments: " + Constants.AUTHOR_EMAIL ); + JOptionPane.showMessageDialog( null, about, Constants.PRG_NAME, JOptionPane.PLAIN_MESSAGE ); + } + + static void chooseNodeSize( final Options options, final Component parent ) { + final String s = ( String ) JOptionPane.showInputDialog( parent, + "Please enter the default size for node shapes.\n" + + "[current value: " + + options.getDefaultNodeShapeSize() + "]\n", + "Node Shape Size", + JOptionPane.QUESTION_MESSAGE, + null, + null, + options.getDefaultNodeShapeSize() ); + if ( !ForesterUtil.isEmpty( s ) ) { + boolean success = true; + double m = 0.0; + final String m_str = s.trim(); + if ( !ForesterUtil.isEmpty( m_str ) ) { + try { + m = Double.parseDouble( m_str ); + } + catch ( final Exception ex ) { + success = false; + } + } + else { + success = false; + } + if ( success && ( m >= 0.0 ) ) { + final short size = ForesterUtil.roundToShort( m ); + if ( size >= 0.0 ) { + options.setDefaultNodeShapeSize( size ); + } + } + } + } + + static String createCurrentFontDesc( final TreeFontSet tree_font_set ) { + return tree_font_set.getLargeFont().getFamily() + " " + tree_font_set.getLargeFont().getSize(); + } + + static JMenu createMenu( final String title, final Configuration conf ) { + final JMenu jmenu = new JMenu( title ); + if ( !conf.isUseNativeUI() ) { + jmenu.setFont( MainFrame.menu_font ); + jmenu.setBackground( conf.getGuiMenuBackgroundColor() ); + jmenu.setForeground( conf.getGuiMenuTextColor() ); + } + return jmenu; + } + + static JMenuItem customizeMenuItemAsLabel( final JMenuItem label, final Configuration configuration ) { + label.setFont( MainFrame.menu_font.deriveFont( Font.BOLD ) ); + if ( !configuration.isUseNativeUI() ) { + label.setBackground( configuration.getGuiMenuBackgroundColor() ); + label.setForeground( configuration.getGuiMenuTextColor() ); + label.setOpaque( true ); + } + label.setSelected( false ); + label.setEnabled( false ); + return label; + } + + private static void cycleNodeDataReturn( final Options op, final Configuration conf ) { + switch ( op.getExtDescNodeDataToReturn() ) { + case UNKNOWN: + op.setExtDescNodeDataToReturn( NodeDataField.DOMAINS_ALL ); + break; + case DOMAINS_ALL: + op.setExtDescNodeDataToReturn( NodeDataField.DOMAINS_COLLAPSED_PER_PROTEIN ); + break; + case DOMAINS_COLLAPSED_PER_PROTEIN: + op.setExtDescNodeDataToReturn( NodeDataField.SEQ_ANNOTATIONS ); + break; + case SEQ_ANNOTATIONS: + op.setExtDescNodeDataToReturn( NodeDataField.GO_TERM_IDS ); + break; + case GO_TERM_IDS: + op.setExtDescNodeDataToReturn( NodeDataField.SEQUENCE_MOL_SEQ_FASTA ); + break; + case SEQUENCE_MOL_SEQ_FASTA: + if ( ( conf != null ) && ( conf.getExtDescNodeDataToReturn() != null ) + && ( conf.getExtDescNodeDataToReturn() != NodeDataField.DOMAINS_ALL ) + && ( conf.getExtDescNodeDataToReturn() != NodeDataField.DOMAINS_COLLAPSED_PER_PROTEIN ) + && ( conf.getExtDescNodeDataToReturn() != NodeDataField.SEQ_ANNOTATIONS ) + && ( conf.getExtDescNodeDataToReturn() != NodeDataField.GO_TERM_IDS ) + && ( conf.getExtDescNodeDataToReturn() != NodeDataField.SEQUENCE_MOL_SEQ_FASTA ) ) { + op.setExtDescNodeDataToReturn( conf.getExtDescNodeDataToReturn() ); + } + else { + op.setExtDescNodeDataToReturn( NodeDataField.UNKNOWN ); + } + break; + default: + op.setExtDescNodeDataToReturn( NodeDataField.UNKNOWN ); + } + } + + static void cycleNodeFill( final Options op ) { + switch ( op.getDefaultNodeFill() ) { + case GRADIENT: + op.setDefaultNodeFill( NodeFill.SOLID ); + break; + case NONE: + op.setDefaultNodeFill( NodeFill.GRADIENT ); + break; + case SOLID: + op.setDefaultNodeFill( NodeFill.NONE ); + break; + default: + throw new RuntimeException( "unknown fill: " + op.getDefaultNodeFill() ); + } + } + + static void cycleNodeShape( final Options op ) { + switch ( op.getDefaultNodeShape() ) { + case CIRCLE: + op.setDefaultNodeShape( NodeShape.RECTANGLE ); + break; + case RECTANGLE: + op.setDefaultNodeShape( NodeShape.CIRCLE ); + break; + default: + throw new RuntimeException( "unknown shape: " + op.getDefaultNodeShape() ); + } + } + + static void cycleOverview( final Options op, final TreePanel tree_panel ) { + switch ( op.getOvPlacement() ) { + case LOWER_LEFT: + op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.UPPER_LEFT ); + break; + case LOWER_RIGHT: + op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.LOWER_LEFT ); + break; + case UPPER_LEFT: + op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.UPPER_RIGHT ); + break; + case UPPER_RIGHT: + op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.LOWER_RIGHT ); + break; + default: + throw new RuntimeException( "unknown placement: " + op.getOvPlacement() ); + } + if ( tree_panel != null ) { + tree_panel.updateOvSettings(); + } + } + + static void setCycleDataReturnMenuItem( final JMenuItem mi, final Options options ) { + if ( ( options != null ) && ( options.getExtDescNodeDataToReturn() != null ) ) { + mi.setText( "Cycle Node Return Data... (current: " + options.getExtDescNodeDataToReturn().toString() + ")" ); + } + else { + mi.setText( "Cycle Node Return Data..." ); + } + } + + static void setCycleNodeFillMenuItem( final JMenuItem mi, final Options options ) { + if ( ( options != null ) && ( options.getDefaultNodeFill() != null ) ) { + mi.setText( "Cycle Node Shape Fill Type... (current: " + + options.getDefaultNodeFill().toString().toLowerCase() + ")" ); + } + else { + mi.setText( "Cycle Node Shape Fill Type..." ); + } + } + + static void setCycleNodeShapeMenuItem( final JMenuItem mi, final Options options ) { + if ( ( options != null ) && ( options.getDefaultNodeShape() != null ) ) { + mi.setText( "Cycle Node Shape Fill Type... (current: " + + options.getDefaultNodeShape().toString().toLowerCase() + ")" ); + } + else { + mi.setText( "Cycle Node Shape Fill Type..." ); + } + } + + static void setOvPlacementColorChooseMenuItem( final JMenuItem mi, final Options options ) { + if ( ( options != null ) && ( options.getOvPlacement() != null ) ) { + mi.setText( "Cycle Overview Placement... (current: " + options.getOvPlacement() + ")" ); + } + else { + mi.setText( "Cycle Overview Placement..." ); + } + } + + static void setTextColorChooseMenuItem( final JMenuItem mi, final TreePanel tree_panel ) { + if ( ( tree_panel != null ) && ( tree_panel.getTreeColorSet() != null ) ) { + mi.setText( "Select Color Scheme... (current: " + tree_panel.getTreeColorSet().getCurrentColorSchemeName() + + ")" ); + } + else { + mi.setText( "Select Color Scheme..." ); + } + } + + static void setTextForFontChooserMenuItem( final JMenuItem mi, final String font_desc ) { + mi.setText( "Select Default Font... (current: " + font_desc + ")" ); + } + + static void setTextForGraphicsSizeChooserMenuItem( final JMenuItem mi, final Options o ) { + mi.setText( "Enter Default Size for Graphics Export... (current: " + o.getPrintSizeX() + ", " + + o.getPrintSizeY() + ")" ); + } + + static void setTextForPdfLineWidthChooserMenuItem( final JMenuItem mi, final Options o ) { + mi.setText( "Enter Default Line Width for PDF Export... (current: " + o.getPrintLineWidth() + ")" ); + } + + static void setTextMinSupportMenuItem( final JMenuItem mi, final Options options, final TreePanel current_tree_panel ) { + if ( ( current_tree_panel == null ) || ( current_tree_panel.getPhylogeny() == null ) ) { + mi.setEnabled( true ); + } + else if ( AptxUtil.isHasAtLeastOneBranchWithSupportValues( current_tree_panel.getPhylogeny() ) ) { + mi.setEnabled( true ); + } + else { + mi.setEnabled( false ); + } + mi.setText( "Enter Min Confidence Value... (current: " + options.getMinConfidenceValue() + ")" ); + } + + static void setTextNodeSizeMenuItem( final JMenuItem mi, final Options options ) { + mi.setText( "Enter Default Node Shape Size... (current: " + options.getDefaultNodeShapeSize() + ")" ); + } + + static void updateScreenTextAntialias( final List treepanels ) { + for( final TreePanel tree_panel : treepanels ) { + tree_panel.setTextAntialias(); + } + } + final static NHFilter nhfilter = new NHFilter(); + final static NHXFilter nhxfilter = new NHXFilter(); + final static XMLFilter xmlfilter = new XMLFilter(); + final static TolFilter tolfilter = new TolFilter(); + final static NexusFilter nexusfilter = new NexusFilter(); + final static PdfFilter pdffilter = new PdfFilter(); + final static GraphicsFileFilter graphicsfilefilter = new GraphicsFileFilter(); + final static MsaFileFilter msafilter = new MsaFileFilter(); + final static SequencesFileFilter seqsfilter = new SequencesFileFilter(); + final static DefaultFilter defaultfilter = new DefaultFilter(); + static final String USE_MOUSEWHEEL_SHIFT_TO_ROTATE = "In this display type, use mousewheel + Shift to rotate [or A and S]"; + static final String PHYLOXML_REF_TOOL_TIP = Constants.PHYLOXML_REFERENCE; //TODO //FIXME + static final String APTX_REF_TOOL_TIP = Constants.APTX_REFERENCE; + private static final long serialVersionUID = 3655000897845508358L; + final static Font menu_font = new Font( Configuration.getDefaultFontFamilyName(), + Font.PLAIN, + 10 ); + static final String TYPE_MENU_HEADER = "Type"; + static final String RECTANGULAR_TYPE_CBMI_LABEL = "Rectangular"; + static final String EURO_TYPE_CBMI_LABEL = "Euro Type"; + static final String CURVED_TYPE_CBMI_LABEL = "Curved"; + static final String TRIANGULAR_TYPE_CBMI_LABEL = "Triangular"; + static final String CONVEX_TYPE_CBMI_LABEL = "Convex"; + static final String ROUNDED_TYPE_CBMI_LABEL = "Rounded"; + static final String UNROOTED_TYPE_CBMI_LABEL = "Unrooted (alpha)"; //TODO + static final String CIRCULAR_TYPE_CBMI_LABEL = "Circular (alpha)"; //TODO + static final String OPTIONS_HEADER = "Options"; + static final String SEARCH_SUBHEADER = "Search:"; + static final String DISPLAY_SUBHEADER = "Display:"; + static final String SEARCH_TERMS_ONLY_LABEL = "Match Complete Terms Only"; + static final String SEARCH_REGEX_LABEL = "Search with Regular Expressions"; + static final String SEARCH_CASE_SENSITIVE_LABEL = "Case Sensitive"; + static final String INVERSE_SEARCH_RESULT_LABEL = "Negate Result"; + static final String COLOR_BY_TAXONOMIC_GROUP = "Colorize by Taxonomic Group"; + static final String DISPLAY_SCALE_LABEL = "Scale"; + static final String NON_LINED_UP_CLADOGRAMS_LABEL = "Non-Lined Up Cladograms"; + static final String UNIFORM_CLADOGRAMS_LABEL = "Total Node Sum Dependent Cladograms"; + static final String LABEL_DIRECTION_LABEL = "Radial Labels"; + static final String LABEL_DIRECTION_TIP = "To use radial node labels in radial and unrooted display types"; + static final String SEARCH_WITH_REGEX_TIP = "To search using regular expressions (~Java/Perl syntax). For example, use \"^B.+\\d{2,}$\" to search for everything starting with a B and ending with at least two digits."; + static final String SCREEN_ANTIALIAS_LABEL = "Antialias"; + static final String COLOR_LABELS_LABEL = "Colorize Labels Same as Parent Branch"; + static final String BG_GRAD_LABEL = "Background Color Gradient"; + static final String DISPLAY_NODE_BOXES_LABEL_EXT = "Shapes for External Nodes"; + static final String DISPLAY_NODE_BOXES_LABEL_INT = "Shapes for Internal Nodes"; + static final String DISPLAY_NODE_BOXES_LABEL_MARKED = "Shapes for Nodes with Visual Data"; + static final String SHOW_OVERVIEW_LABEL = "Overview"; + static final String FONT_SIZE_MENU_LABEL = "Font Size"; + static final String NONUNIFORM_CLADOGRAMS_LABEL = "External Node Sum Dependent Cladograms"; + static final String SHOW_DOMAIN_LABELS_LABEL = "Domain Labels"; + static final String SHOW_ANN_REF_SOURCE_LABEL = "Seq Annotation Ref Sources"; + static final String COLOR_LABELS_TIP = "To use parent branch colors for node labels as well, need to turn off taxonomy dependent colorization and turn on branch colorization for this to become apparent"; + static final String ABBREV_SN_LABEL = "Abbreviate Scientific Taxonomic Names"; + static final String TAXONOMY_COLORIZE_NODE_SHAPES_LABEL = "Colorize Node Shapes According to Taxonomy"; + static final String CYCLE_NODE_SHAPE_LABEL = "Cycle Node Shapes"; + static final String CYCLE_NODE_FILL_LABEL = "Cycle Node Fill Type"; + static final String CHOOSE_NODE_SIZE_LABEL = "Choose Node Shape Size"; + static final String SHOW_CONF_STDDEV_LABEL = "Confidence Standard Deviations"; + static final String USE_BRACKETS_FOR_CONF_IN_NH_LABEL = "Use Brackets for Confidence Values"; + static final String USE_INTERNAL_NAMES_FOR_CONF_IN_NH_LABEL = "Use Internal Node Names for Confidence Values"; + static final String SHOW_BASIC_TREE_INFORMATION_LABEL = "Basic Tree Information"; + static final String RIGHT_LINE_UP_DOMAINS = "Right-align Domain Architectures"; + static final String LINE_UP_RENDERABLE_DATA = "Line Up Diagrams (such as Domain Architectures)"; + JMenuBar _jmenubar; + JMenu _file_jmenu; + JMenu _tools_menu; + JMenu _view_jmenu; + JMenu _options_jmenu; + JMenu _font_size_menu; + JMenu _help_jmenu; + JMenuItem[] _load_phylogeny_from_webservice_menu_items; // Analysis menu - JMenu _analysis_menu; - JMenuItem _load_species_tree_item; - JMenuItem _gsdi_item; - JMenuItem _gsdir_item; - JMenuItem _lineage_inference; + JMenu _analysis_menu; + JMenuItem _load_species_tree_item; + JMenuItem _gsdi_item; + JMenuItem _gsdir_item; + JMenuItem _lineage_inference; // file menu: - JMenuItem _open_item; - JMenuItem _open_url_item; - JMenuItem _save_item; - JMenuItem _save_all_item; - JMenuItem _close_item; - JMenuItem _exit_item; - JMenuItem _new_item; - JMenuItem _print_item; + JMenuItem _open_item; + JMenuItem _open_url_item; + JMenuItem _save_item; + JMenuItem _save_all_item; + JMenuItem _close_item; + JMenuItem _exit_item; + JMenuItem _new_item; + JMenuItem _print_item; JMenuItem _write_to_pdf_item; JMenuItem _write_to_jpg_item; JMenuItem _write_to_gif_item; JMenuItem _write_to_tif_item; - JMenuItem _write_to_png_item; + JMenuItem _write_to_png_item; JMenuItem _write_to_bmp_item; // tools menu: - JMenuItem _midpoint_root_item; - JMenuItem _taxcolor_item; - JMenuItem _confcolor_item; - JMenuItem _color_rank_jmi; - JMenuItem _collapse_species_specific_subtrees; - JMenuItem _obtain_detailed_taxonomic_information_jmi; - JMenuItem _obtain_detailed_taxonomic_information_deleting_jmi; - JMenuItem _obtain_seq_information_jmi; - JMenuItem _move_node_names_to_tax_sn_jmi; - JMenuItem _move_node_names_to_seq_names_jmi; - JMenuItem _extract_tax_code_from_node_names_jmi; - JMenuItem _annotate_item; - JMenuItem _remove_branch_color_item; - JMenuItem _remove_visual_styles_item; - JMenuItem _delete_selected_nodes_item; - JMenuItem _delete_not_selected_nodes_item; + JMenuItem _midpoint_root_item; + JMenuItem _taxcolor_item; + JMenuItem _confcolor_item; + JMenuItem _color_rank_jmi; + JMenuItem _collapse_species_specific_subtrees; + JMenuItem _obtain_detailed_taxonomic_information_jmi; + JMenuItem _obtain_detailed_taxonomic_information_deleting_jmi; + JMenuItem _obtain_seq_information_jmi; + JMenuItem _move_node_names_to_tax_sn_jmi; + JMenuItem _move_node_names_to_seq_names_jmi; + JMenuItem _extract_tax_code_from_node_names_jmi; + JMenuItem _annotate_item; + JMenuItem _remove_branch_color_item; + JMenuItem _remove_visual_styles_item; + JMenuItem _delete_selected_nodes_item; + JMenuItem _delete_not_selected_nodes_item; // font size menu: - JMenuItem _super_tiny_fonts_item; - JMenuItem _tiny_fonts_item; - JMenuItem _small_fonts_item; - JMenuItem _medium_fonts_item; - JMenuItem _large_fonts_item; + JMenuItem _super_tiny_fonts_item; + JMenuItem _tiny_fonts_item; + JMenuItem _small_fonts_item; + JMenuItem _medium_fonts_item; + JMenuItem _large_fonts_item; // options menu: // _ screen and print - JMenuItem _choose_font_mi; - JMenuItem _switch_colors_mi; - JCheckBoxMenuItem _label_direction_cbmi; + JMenuItem _choose_font_mi; + JMenuItem _switch_colors_mi; + JCheckBoxMenuItem _label_direction_cbmi; // _ screen display - JCheckBoxMenuItem _screen_antialias_cbmi; - JCheckBoxMenuItem _background_gradient_cbmi; - JRadioButtonMenuItem _non_lined_up_cladograms_rbmi; - JRadioButtonMenuItem _uniform_cladograms_rbmi; - JRadioButtonMenuItem _ext_node_dependent_cladogram_rbmi; - JCheckBoxMenuItem _color_by_taxonomic_group_cbmi; - JCheckBoxMenuItem _show_scale_cbmi; //TODO fix me - JCheckBoxMenuItem _show_overview_cbmi; - JCheckBoxMenuItem _show_domain_labels; - JCheckBoxMenuItem _show_annotation_ref_source; - JCheckBoxMenuItem _abbreviate_scientific_names; - JCheckBoxMenuItem _color_labels_same_as_parent_branch; - JMenuItem _overview_placment_mi; - JMenuItem _choose_minimal_confidence_mi; - JCheckBoxMenuItem _show_default_node_shapes_internal_cbmi; - JCheckBoxMenuItem _show_default_node_shapes_external_cbmi; - JCheckBoxMenuItem _show_default_node_shapes_for_marked_cbmi; - JMenuItem _cycle_node_shape_mi; - JMenuItem _cycle_node_fill_mi; - JMenuItem _choose_node_size_mi; - JMenuItem _cycle_data_return; - JCheckBoxMenuItem _show_confidence_stddev_cbmi; - JCheckBoxMenuItem _right_line_up_domains_cbmi; - JCheckBoxMenuItem _line_up_renderable_data_cbmi; + JCheckBoxMenuItem _screen_antialias_cbmi; + JCheckBoxMenuItem _background_gradient_cbmi; + JRadioButtonMenuItem _non_lined_up_cladograms_rbmi; + JRadioButtonMenuItem _uniform_cladograms_rbmi; + JRadioButtonMenuItem _ext_node_dependent_cladogram_rbmi; + JCheckBoxMenuItem _color_by_taxonomic_group_cbmi; + JCheckBoxMenuItem _show_scale_cbmi; //TODO fix me + JCheckBoxMenuItem _show_overview_cbmi; + JCheckBoxMenuItem _show_domain_labels; + JCheckBoxMenuItem _show_annotation_ref_source; + JCheckBoxMenuItem _abbreviate_scientific_names; + JCheckBoxMenuItem _color_labels_same_as_parent_branch; + JMenuItem _overview_placment_mi; + JMenuItem _choose_minimal_confidence_mi; + JCheckBoxMenuItem _show_default_node_shapes_internal_cbmi; + JCheckBoxMenuItem _show_default_node_shapes_external_cbmi; + JCheckBoxMenuItem _show_default_node_shapes_for_marked_cbmi; + JMenuItem _cycle_node_shape_mi; + JMenuItem _cycle_node_fill_mi; + JMenuItem _choose_node_size_mi; + JMenuItem _cycle_data_return; + JCheckBoxMenuItem _show_confidence_stddev_cbmi; + JCheckBoxMenuItem _right_line_up_domains_cbmi; + JCheckBoxMenuItem _line_up_renderable_data_cbmi; // _ print - JCheckBoxMenuItem _graphics_export_visible_only_cbmi; - JCheckBoxMenuItem _antialias_print_cbmi; - JCheckBoxMenuItem _print_black_and_white_cbmi; - JCheckBoxMenuItem _print_using_actual_size_cbmi; - JCheckBoxMenuItem _graphics_export_using_actual_size_cbmi; - JMenuItem _print_size_mi; - JMenuItem _choose_pdf_width_mi; + JCheckBoxMenuItem _graphics_export_visible_only_cbmi; + JCheckBoxMenuItem _antialias_print_cbmi; + JCheckBoxMenuItem _print_black_and_white_cbmi; + JCheckBoxMenuItem _print_using_actual_size_cbmi; + JCheckBoxMenuItem _graphics_export_using_actual_size_cbmi; + JMenuItem _print_size_mi; + JMenuItem _choose_pdf_width_mi; // _ parsing - JCheckBoxMenuItem _internal_number_are_confidence_for_nh_parsing_cbmi; - JRadioButtonMenuItem _extract_taxonomy_no_rbmi; - JRadioButtonMenuItem _extract_taxonomy_agressive_rbmi; - JRadioButtonMenuItem _extract_taxonomy_pfam_strict_rbmi; - JRadioButtonMenuItem _extract_taxonomy_pfam_relaxed_rbmi; - JCheckBoxMenuItem _replace_underscores_cbmi; - JCheckBoxMenuItem _allow_errors_in_distance_to_parent_cbmi; - JCheckBoxMenuItem _use_brackets_for_conf_in_nh_export_cbmi; - JCheckBoxMenuItem _use_internal_names_for_conf_in_nh_export_cbmi; + JCheckBoxMenuItem _internal_number_are_confidence_for_nh_parsing_cbmi; + JRadioButtonMenuItem _extract_taxonomy_no_rbmi; + JRadioButtonMenuItem _extract_taxonomy_agressive_rbmi; + JRadioButtonMenuItem _extract_taxonomy_pfam_strict_rbmi; + JRadioButtonMenuItem _extract_taxonomy_pfam_relaxed_rbmi; + JCheckBoxMenuItem _replace_underscores_cbmi; + JCheckBoxMenuItem _allow_errors_in_distance_to_parent_cbmi; + JCheckBoxMenuItem _use_brackets_for_conf_in_nh_export_cbmi; + JCheckBoxMenuItem _use_internal_names_for_conf_in_nh_export_cbmi; // _ search - JCheckBoxMenuItem _search_case_senstive_cbmi; - JCheckBoxMenuItem _search_whole_words_only_cbmi; - JCheckBoxMenuItem _inverse_search_result_cbmi; - JCheckBoxMenuItem _search_with_regex_cbmi; + JCheckBoxMenuItem _search_case_senstive_cbmi; + JCheckBoxMenuItem _search_whole_words_only_cbmi; + JCheckBoxMenuItem _inverse_search_result_cbmi; + JCheckBoxMenuItem _search_with_regex_cbmi; // type menu: - JMenu _type_menu; - JCheckBoxMenuItem _rectangular_type_cbmi; - JCheckBoxMenuItem _triangular_type_cbmi; - JCheckBoxMenuItem _curved_type_cbmi; - JCheckBoxMenuItem _convex_type_cbmi; - JCheckBoxMenuItem _euro_type_cbmi; - JCheckBoxMenuItem _rounded_type_cbmi; - JCheckBoxMenuItem _unrooted_type_cbmi; - JCheckBoxMenuItem _circular_type_cbmi; + JMenu _type_menu; + JCheckBoxMenuItem _rectangular_type_cbmi; + JCheckBoxMenuItem _triangular_type_cbmi; + JCheckBoxMenuItem _curved_type_cbmi; + JCheckBoxMenuItem _convex_type_cbmi; + JCheckBoxMenuItem _euro_type_cbmi; + JCheckBoxMenuItem _rounded_type_cbmi; ; + JCheckBoxMenuItem _unrooted_type_cbmi; + JCheckBoxMenuItem _circular_type_cbmi; // view as text menu: - JMenuItem _view_as_NH_item; - JMenuItem _view_as_XML_item; - JMenuItem _view_as_nexus_item; - JMenuItem _display_basic_information_item; + JMenuItem _view_as_NH_item; + JMenuItem _view_as_XML_item; + JMenuItem _view_as_nexus_item; + JMenuItem _display_basic_information_item; // help menu: - JMenuItem _about_item; - JMenuItem _help_item; - JMenuItem _website_item; - JMenuItem _phyloxml_website_item; - JMenuItem _phyloxml_ref_item; - JMenuItem _aptx_ref_item; + JMenuItem _about_item; + JMenuItem _help_item; + JMenuItem _website_item; + JMenuItem _phyloxml_website_item; + JMenuItem _phyloxml_ref_item; + JMenuItem _aptx_ref_item; // - JFileChooser _writetopdf_filechooser; + JFileChooser _writetopdf_filechooser; File _current_dir; - + JFileChooser _save_filechooser; + JFileChooser _writetographics_filechooser; // process menu: - JMenu _process_menu; + JMenu _process_menu; // Handy pointers to child components: - MainPanel _mainpanel; - Container _contentpane; - final LinkedList _textframes = new LinkedList(); ; - Configuration _configuration; - Options _options; - private Phylogeny _species_tree; - InferenceManager _inference_manager; - final ProcessPool _process_pool; - private String _previous_node_annotation_ref; + MainPanel _mainpanel; + Container _contentpane; + final LinkedList _textframes = new LinkedList(); + Configuration _configuration; + Options _options; + private Phylogeny _species_tree; + InferenceManager _inference_manager; + final ProcessPool _process_pool; + private String _previous_node_annotation_ref; MainFrame() { _process_pool = ProcessPool.createInstance(); _writetopdf_filechooser = new JFileChooser(); + _save_filechooser = new JFileChooser(); + _save_filechooser.setCurrentDirectory( new File( "." ) ); + _save_filechooser.setMultiSelectionEnabled( false ); + _save_filechooser.setFileFilter( MainFrame.xmlfilter ); + _save_filechooser.addChoosableFileFilter( nhfilter ); + _save_filechooser.addChoosableFileFilter( nexusfilter ); + _save_filechooser.addChoosableFileFilter( _save_filechooser.getAcceptAllFileFilter() ); + _writetographics_filechooser = new JFileChooser(); + _writetographics_filechooser.addChoosableFileFilter( MainFrame.graphicsfilefilter ); } /** @@ -583,288 +904,54 @@ public abstract class MainFrame extends JFrame implements ActionListener { } else if ( o == _phyloxml_ref_item ) { try { - AptxUtil.openWebsite( Constants.PHYLOXML_REFERENCE_URL, is_applet, applet ); - } - catch ( final IOException e1 ) { - ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() ); - } - } - else if ( o == _write_to_pdf_item ) { - writeToPdf( _mainpanel.getCurrentPhylogeny() ); - } - else { - if ( _load_phylogeny_from_webservice_menu_items != null ) { - for( int i = 0; i < _load_phylogeny_from_webservice_menu_items.length; ++i ) { - if ( o == _load_phylogeny_from_webservice_menu_items[ i ] ) { - readPhylogeniesFromWebservice( i ); - } - } - } - } - _contentpane.repaint(); - } - - - void setCurrentDir( final File current_dir ) { - _current_dir = current_dir; - } - - void writeToPdf( final Phylogeny t ) { - if ( ( t == null ) || t.isEmpty() ) { - return; - } - String initial_filename = ""; - if ( getMainPanel().getCurrentTreePanel().getTreeFile() != null ) { - initial_filename = getMainPanel().getCurrentTreePanel().getTreeFile().toString(); - } - if ( initial_filename.indexOf( '.' ) > 0 ) { - initial_filename = initial_filename.substring( 0, initial_filename.lastIndexOf( '.' ) ); - } - initial_filename = initial_filename + ".pdf"; - _writetopdf_filechooser.setSelectedFile( new File( initial_filename ) ); - final File my_dir = getCurrentDir(); - if ( my_dir != null ) { - _writetopdf_filechooser.setCurrentDirectory( my_dir ); - } - final int result = _writetopdf_filechooser.showSaveDialog( _contentpane ); - File file = _writetopdf_filechooser.getSelectedFile(); - setCurrentDir( _writetopdf_filechooser.getCurrentDirectory() ); - if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) { - if ( !file.toString().toLowerCase().endsWith( ".pdf" ) ) { - file = new File( file.toString() + ".pdf" ); - } - if ( file.exists() ) { - final int i = JOptionPane.showConfirmDialog( this, - file + " already exists. Overwrite?", - "WARNING", - JOptionPane.OK_CANCEL_OPTION, - JOptionPane.WARNING_MESSAGE ); - if ( i != JOptionPane.OK_OPTION ) { - return; - } + AptxUtil.openWebsite( Constants.PHYLOXML_REFERENCE_URL, is_applet, applet ); + } + catch ( final IOException e1 ) { + ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() ); } - printPhylogenyToPdf( file.toString() ); } - } - - - void printPhylogenyToPdf( final String file_name ) { - if ( !getOptions().isPrintUsingActualSize() ) { - getCurrentTreePanel() - .calcParametersForPainting( getOptions().getPrintSizeX(), getOptions().getPrintSizeY() ); - getCurrentTreePanel().resetPreferredSize(); - getCurrentTreePanel().repaint(); - } - String pdf_written_to = ""; - boolean error = false; - try { - if ( getOptions().isPrintUsingActualSize() ) { - pdf_written_to = PdfExporter.writePhylogenyToPdf( file_name, - getCurrentTreePanel(), - getCurrentTreePanel().getWidth(), - getCurrentTreePanel().getHeight() ); - } - else { - pdf_written_to = PdfExporter.writePhylogenyToPdf( file_name, getCurrentTreePanel(), getOptions() - .getPrintSizeX(), getOptions().getPrintSizeY() ); - } - } - catch ( final IOException e ) { - error = true; - JOptionPane.showMessageDialog( this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE ); - } - if ( !error ) { - if ( !ForesterUtil.isEmpty( pdf_written_to ) ) { - JOptionPane.showMessageDialog( this, - "Wrote PDF to: " + pdf_written_to, - "Information", - JOptionPane.INFORMATION_MESSAGE ); - } - else { - JOptionPane.showMessageDialog( this, - "There was an unknown problem when attempting to write to PDF file: \"" - + file_name + "\"", - "Error", - JOptionPane.ERROR_MESSAGE ); - } - } - if ( !getOptions().isPrintUsingActualSize() ) { - getControlPanel().showWhole(); - } - } - - ControlPanel getControlPanel() { - return getMainPanel().getControlPanel(); - } - - - File getCurrentDir() { - if ( ( _current_dir == null ) || !_current_dir.canRead() ) { - if ( ForesterUtil.isWindows() ) { - try { - _current_dir = new File( WindowsUtils.getCurrentUserDesktopPath() ); - } - catch ( final Exception e ) { - _current_dir = null; - } - } - } - if ( ( _current_dir == null ) || !_current_dir.canRead() ) { - if ( System.getProperty( "user.home" ) != null ) { - _current_dir = new File( System.getProperty( "user.home" ) ); - } - else if ( System.getProperty( "user.dir" ) != null ) { - _current_dir = new File( System.getProperty( "user.dir" ) ); - } - } - return _current_dir; - } - - private void deleteSelectedNodes( final boolean delete ) { - final Phylogeny phy = getMainPanel().getCurrentPhylogeny(); - if ( ( phy == null ) || ( phy.getNumberOfExternalNodes() < 2 ) ) { - return; + else if ( o == _write_to_pdf_item ) { + writeToPdf( _mainpanel.getCurrentPhylogeny() ); } - final List nodes = new ArrayList(); - if ( ( getCurrentTreePanel().getFoundNodes0() != null ) || ( getCurrentTreePanel().getFoundNodes1() != null ) ) { - final List all_selected_nodes = getCurrentTreePanel().getFoundNodesAsListOfPhylogenyNodes(); - for( final PhylogenyNode n : all_selected_nodes ) { - if ( n.isExternal() ) { - nodes.add( n ); - } - } + else if ( o == _save_item ) { + writeToFile( _mainpanel.getCurrentPhylogeny() ); + // If subtree currently displayed, save it, instead of complete + // tree. } - String function = "Retain"; - if ( delete ) { - function = "Delete"; + else if ( o == _write_to_png_item ) { + writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.PNG ); } - if ( ( nodes == null ) || nodes.isEmpty() ) { - JOptionPane - .showMessageDialog( this, - "Need to select external nodes, either via direct selection or via the \"Search\" function", - "No external nodes selected to " + function.toLowerCase(), - JOptionPane.ERROR_MESSAGE ); - return; + else if ( o == _graphics_export_visible_only_cbmi ) { + updateOptions( getOptions() ); } - final int todo = nodes.size(); - final int ext = phy.getNumberOfExternalNodes(); - int res = todo; - if ( delete ) { - res = ext - todo; + else if ( o == _antialias_print_cbmi ) { + updateOptions( getOptions() ); } - if ( res < 1 ) { - JOptionPane.showMessageDialog( this, - "Cannot delete all nodes", - "Attempt to delete all nodes ", - JOptionPane.ERROR_MESSAGE ); - return; + else if ( o == _print_black_and_white_cbmi ) { + updateOptions( getOptions() ); } - final int result = JOptionPane.showConfirmDialog( null, function + " " + todo - + " external node(s), from a total of " + ext + " external nodes," + "\nresulting in tree with " + res - + " nodes?", function + " external nodes", JOptionPane.OK_CANCEL_OPTION ); - if ( result == JOptionPane.OK_OPTION ) { - if ( !delete ) { - final List to_delete = new ArrayList(); - for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) { - final PhylogenyNode n = it.next(); - if ( !nodes.contains( n ) ) { - to_delete.add( n ); + else if ( o == _print_using_actual_size_cbmi ) { + updateOptions( getOptions() ); + } + else if ( o == _graphics_export_using_actual_size_cbmi ) { + updateOptions( getOptions() ); + } + else if ( o == _print_size_mi ) { + choosePrintSize(); + } + else if ( o == _choose_pdf_width_mi ) { + choosePdfWidth(); + } + else { + if ( _load_phylogeny_from_webservice_menu_items != null ) { + for( int i = 0; i < _load_phylogeny_from_webservice_menu_items.length; ++i ) { + if ( o == _load_phylogeny_from_webservice_menu_items[ i ] ) { + readPhylogeniesFromWebservice( i ); } } - for( final PhylogenyNode n : to_delete ) { - phy.deleteSubtree( n, true ); - } - } - else { - for( final PhylogenyNode n : nodes ) { - phy.deleteSubtree( n, true ); - } } - resetSearch(); - getCurrentTreePanel().setNodeInPreorderToNull(); - phy.externalNodesHaveChanged(); - phy.clearHashIdToNodeMap(); - phy.recalculateNumberOfExternalDescendants( true ); - getCurrentTreePanel().resetNodeIdToDistToLeafMap(); - getCurrentTreePanel().setEdited( true ); - repaint(); } - } - - void resetSearch() { - getMainPanel().getCurrentTreePanel().setFoundNodes0( null ); - getMainPanel().getCurrentTreePanel().setFoundNodes1( null ); - getMainPanel().getControlPanel().setSearchFoundCountsOnLabel0( 0 ); - getMainPanel().getControlPanel().getSearchFoundCountsLabel0().setVisible( false ); - getMainPanel().getControlPanel().getSearchTextField0().setText( "" ); - getMainPanel().getControlPanel().getSearchResetButton0().setEnabled( false ); - getMainPanel().getControlPanel().getSearchResetButton0().setVisible( false ); - getMainPanel().getControlPanel().setSearchFoundCountsOnLabel1( 0 ); - getMainPanel().getControlPanel().getSearchFoundCountsLabel1().setVisible( false ); - getMainPanel().getControlPanel().getSearchTextField1().setText( "" ); - getMainPanel().getControlPanel().getSearchResetButton1().setEnabled( false ); - getMainPanel().getControlPanel().getSearchResetButton1().setVisible( false ); - } - - public Configuration getConfiguration() { - return _configuration; - } - - /** - * This method returns the current external node data which - * has been selected by the user by clicking the "Return ..." - * menu item. This method is expected to be called from Javascript or - * something like it. - * - * @return current external node data as String - */ - public String getCurrentExternalNodesDataBuffer() { - return getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString(); - } - - public int getCurrentExternalNodesDataBufferChangeCounter() { - return getCurrentTreePanel().getCurrentExternalNodesDataBufferChangeCounter(); - } - - public int getCurrentExternalNodesDataBufferLength() { - return getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString().length(); - } - - public InferenceManager getInferenceManager() { - return _inference_manager; - } - - public MainPanel getMainPanel() { - return _mainpanel; - } - - public Options getOptions() { - return _options; - } - - public ProcessPool getProcessPool() { - return _process_pool; - } - - public void showTextFrame( final String s, final String title ) { - checkTextFrames(); - _textframes.addLast( TextFrame.instantiate( s, title, _textframes ) ); - } - - public void showWhole() { - _mainpanel.getControlPanel().showWhole(); - } - - public void updateProcessMenu() { - // In general Swing is not thread safe. - // See "Swing's Threading Policy". - SwingUtilities.invokeLater( new Runnable() { - - @Override - public void run() { - doUpdateProcessMenu(); - } - } ); + _contentpane.repaint(); } void activateSaveAllIfNeeded() { @@ -876,50 +963,116 @@ public abstract class MainFrame extends JFrame implements ActionListener { } } -// void buildFileMenu() { -// _file_jmenu = createMenu( "File", getConfiguration() ); -// _file_jmenu.add( _exit_item = new JMenuItem( "Exit" ) ); -// customizeJMenuItem( _exit_item ); -// _jmenubar.add( _file_jmenu ); -// } - + private void annotateSequences() { + if ( getCurrentTreePanel() != null ) { + List nodes = null; + if ( ( getCurrentTreePanel().getFoundNodes0() != null ) + || ( getCurrentTreePanel().getFoundNodes1() != null ) ) { + nodes = getCurrentTreePanel().getFoundNodesAsListOfPhylogenyNodes(); + } + if ( ( nodes == null ) || nodes.isEmpty() ) { + JOptionPane + .showMessageDialog( this, + "Need to select nodes, either via direct selection or via the \"Search\" function", + "No nodes selected for annotation", + JOptionPane.ERROR_MESSAGE ); + return; + } + final Phylogeny phy = getMainPanel().getCurrentPhylogeny(); + if ( ( phy != null ) && !phy.isEmpty() ) { + final JTextField ref_field = new JTextField( 10 ); + final JTextField desc_filed = new JTextField( 20 ); + ref_field.setText( ForesterUtil.isEmpty( getPreviousNodeAnnotationReference() ) ? "" + : getPreviousNodeAnnotationReference() ); + final JPanel my_panel = new JPanel(); + my_panel.add( new JLabel( "Reference " ) ); + my_panel.add( ref_field ); + my_panel.add( Box.createHorizontalStrut( 15 ) ); + my_panel.add( new JLabel( "Description " ) ); + my_panel.add( desc_filed ); + final int result = JOptionPane.showConfirmDialog( null, + my_panel, + "Enter the sequence annotation(s) for the " + + nodes.size() + " selected nodes", + JOptionPane.OK_CANCEL_OPTION ); + if ( result == JOptionPane.OK_OPTION ) { + String ref = ref_field.getText(); + String desc = desc_filed.getText(); + if ( !ForesterUtil.isEmpty( ref ) ) { + ref = ref.trim(); + ref = ref.replaceAll( "\\s+", " " ); + if ( ( ref.indexOf( ':' ) < 1 ) || ( ref.indexOf( ':' ) > ( ref.length() - 2 ) ) + || ( ref.length() < 3 ) ) { + JOptionPane.showMessageDialog( this, + "Reference needs to be in the form of \"GO:1234567\"", + "Illegal Format for Annotation Reference", + JOptionPane.ERROR_MESSAGE ); + return; + } + } + if ( ref != null ) { + setPreviousNodeAnnotationReference( ref ); + } + if ( desc != null ) { + desc = desc.trim(); + desc = desc.replaceAll( "\\s+", " " ); + } + if ( !ForesterUtil.isEmpty( ref ) || !ForesterUtil.isEmpty( desc ) ) { + for( final PhylogenyNode n : nodes ) { + ForesterUtil.ensurePresenceOfSequence( n ); + final Annotation ann = ForesterUtil.isEmpty( ref ) ? new Annotation() + : new Annotation( ref ); + if ( !ForesterUtil.isEmpty( desc ) ) { + ann.setDesc( desc ); + } + n.getNodeData().getSequence().addAnnotation( ann ); + } + } + getMainPanel().getControlPanel().showAnnotations(); + } + } + } + } + + // void buildFileMenu() { + // _file_jmenu = createMenu( "File", getConfiguration() ); + // _file_jmenu.add( _exit_item = new JMenuItem( "Exit" ) ); + // customizeJMenuItem( _exit_item ); + // _jmenubar.add( _file_jmenu ); + // } void buildFileMenu() { _file_jmenu = MainFrame.createMenu( "File", getConfiguration() ); - - // _file_jmenu.add( _save_item = new JMenuItem( "Save Tree As..." ) ); - // _file_jmenu.add( _save_all_item = new JMenuItem( "Save All Trees As..." ) ); - // _save_all_item.setToolTipText( "Write all phylogenies to one file." ); - // _save_all_item.setEnabled( false ); - // _file_jmenu.addSeparator(); + _file_jmenu.add( _save_item = new JMenuItem( "Save Tree As..." ) ); + // _file_jmenu.add( _save_all_item = new JMenuItem( "Save All Trees As..." ) ); + // _save_all_item.setToolTipText( "Write all phylogenies to one file." ); + // _save_all_item.setEnabled( false ); + // _file_jmenu.addSeparator(); _file_jmenu.add( _write_to_pdf_item = new JMenuItem( "Export to PDF file ..." ) ); -// 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 ( AptxUtil.canWriteFormat( "gif" ) ) { -// _file_jmenu.add( _write_to_gif_item = new JMenuItem( "Export to GIF file..." ) ); -// } -// if ( AptxUtil.canWriteFormat( "bmp" ) ) { -// _file_jmenu.add( _write_to_bmp_item = new JMenuItem( "Export to BMP file..." ) ); -// } - // _file_jmenu.addSeparator(); - // _file_jmenu.add( _print_item = new JMenuItem( "Print..." ) ); + // 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 ( AptxUtil.canWriteFormat( "gif" ) ) { + // _file_jmenu.add( _write_to_gif_item = new JMenuItem( "Export to GIF file..." ) ); + // } + // if ( AptxUtil.canWriteFormat( "bmp" ) ) { + // _file_jmenu.add( _write_to_bmp_item = new JMenuItem( "Export to BMP file..." ) ); + // } + // _file_jmenu.addSeparator(); + // _file_jmenu.add( _print_item = new JMenuItem( "Print..." ) ); _file_jmenu.addSeparator(); - _file_jmenu.add( _exit_item = new JMenuItem( "Exit" ) ); - - customizeJMenuItem( _save_item ); - -// customizeJMenuItem( _close_item ); - //customizeJMenuItem( _save_all_item ); + customizeJMenuItem( _save_item ); + // customizeJMenuItem( _close_item ); + //customizeJMenuItem( _save_all_item ); customizeJMenuItem( _write_to_pdf_item ); -// customizeJMenuItem( _write_to_png_item ); -// customizeJMenuItem( _write_to_jpg_item ); -// customizeJMenuItem( _write_to_gif_item ); -// customizeJMenuItem( _write_to_tif_item ); -// customizeJMenuItem( _write_to_bmp_item ); -// customizeJMenuItem( _print_item ); + customizeJMenuItem( _write_to_png_item ); + // customizeJMenuItem( _write_to_jpg_item ); + // customizeJMenuItem( _write_to_gif_item ); + // customizeJMenuItem( _write_to_tif_item ); + // customizeJMenuItem( _write_to_bmp_item ); + // customizeJMenuItem( _print_item ); customizeJMenuItem( _exit_item ); _jmenubar.add( _file_jmenu ); } @@ -998,18 +1151,134 @@ public abstract class MainFrame extends JFrame implements ActionListener { _jmenubar.add( _view_jmenu ); } - void checkTextFrames() { - if ( _textframes.size() > 5 ) { - try { - if ( _textframes.getFirst() != null ) { - _textframes.getFirst().removeMe(); + void checkTextFrames() { + if ( _textframes.size() > 5 ) { + try { + if ( _textframes.getFirst() != null ) { + _textframes.getFirst().removeMe(); + } + else { + _textframes.removeFirst(); + } + } + catch ( final NoSuchElementException e ) { + // Ignore. + } + } + } + + private void chooseFont() { + final FontChooser fc = new FontChooser(); + fc.setFont( getMainPanel().getTreeFontSet().getLargeFont() ); + fc.showDialog( this, "Select the Base Font" ); + getMainPanel().getTreeFontSet().setBaseFont( fc.getFont() ); + } + + private void chooseMinimalConfidence() { + final String s = ( String ) JOptionPane + .showInputDialog( this, + "Please enter the minimum for confidence values to be displayed.\n" + + "[current value: " + getOptions().getMinConfidenceValue() + "]\n", + "Minimal Confidence Value", + JOptionPane.QUESTION_MESSAGE, + null, + null, + getOptions().getMinConfidenceValue() ); + if ( !ForesterUtil.isEmpty( s ) ) { + boolean success = true; + double m = 0.0; + final String m_str = s.trim(); + if ( !ForesterUtil.isEmpty( m_str ) ) { + try { + m = Double.parseDouble( m_str ); + } + catch ( final Exception ex ) { + success = false; + } + } + else { + success = false; + } + if ( success && ( m >= 0.0 ) ) { + getOptions().setMinConfidenceValue( m ); + } + } + } + + void choosePdfWidth() { + final String s = ( String ) JOptionPane.showInputDialog( this, + "Please enter the default line width for PDF export.\n" + + "[current value: " + + getOptions().getPrintLineWidth() + "]\n", + "Line Width for PDF Export", + JOptionPane.QUESTION_MESSAGE, + null, + null, + getOptions().getPrintLineWidth() ); + if ( !ForesterUtil.isEmpty( s ) ) { + boolean success = true; + float f = 0.0f; + final String m_str = s.trim(); + if ( !ForesterUtil.isEmpty( m_str ) ) { + try { + f = Float.parseFloat( m_str ); + } + catch ( final Exception ex ) { + success = false; + } + } + else { + success = false; + } + if ( success && ( f > 0.0 ) ) { + getOptions().setPrintLineWidth( f ); + } + } + } + + void choosePrintSize() { + final String s = ( String ) JOptionPane.showInputDialog( this, + "Please enter values for width and height,\nseparated by a comma.\n" + + "[current values: " + + getOptions().getPrintSizeX() + ", " + + getOptions().getPrintSizeY() + "]\n" + + "[A4: " + Constants.A4_SIZE_X + ", " + + Constants.A4_SIZE_Y + "]\n" + "[US Letter: " + + Constants.US_LETTER_SIZE_X + ", " + + Constants.US_LETTER_SIZE_Y + "]", + "Default Size for Graphics Export", + JOptionPane.QUESTION_MESSAGE, + null, + null, + getOptions().getPrintSizeX() + ", " + + getOptions().getPrintSizeY() ); + if ( !ForesterUtil.isEmpty( s ) && ( s.indexOf( ',' ) > 0 ) ) { + boolean success = true; + int x = 0; + int y = 0; + final String[] str_ary = s.split( "," ); + if ( str_ary.length == 2 ) { + final String x_str = str_ary[ 0 ].trim(); + final String y_str = str_ary[ 1 ].trim(); + if ( !ForesterUtil.isEmpty( x_str ) && !ForesterUtil.isEmpty( y_str ) ) { + try { + x = Integer.parseInt( x_str ); + y = Integer.parseInt( y_str ); + } + catch ( final Exception ex ) { + success = false; + } } else { - _textframes.removeFirst(); + success = false; } } - catch ( final NoSuchElementException e ) { - // Ignore. + else { + success = false; + } + if ( success && ( x > 1 ) && ( y > 1 ) ) { + getOptions().setPrintSizeX( x ); + getOptions().setPrintSizeY( y ); } } } @@ -1085,6 +1354,77 @@ public abstract class MainFrame extends JFrame implements ActionListener { } } + private void deleteSelectedNodes( final boolean delete ) { + final Phylogeny phy = getMainPanel().getCurrentPhylogeny(); + if ( ( phy == null ) || ( phy.getNumberOfExternalNodes() < 2 ) ) { + return; + } + final List nodes = new ArrayList(); + if ( ( getCurrentTreePanel().getFoundNodes0() != null ) || ( getCurrentTreePanel().getFoundNodes1() != null ) ) { + final List all_selected_nodes = getCurrentTreePanel().getFoundNodesAsListOfPhylogenyNodes(); + for( final PhylogenyNode n : all_selected_nodes ) { + if ( n.isExternal() ) { + nodes.add( n ); + } + } + } + String function = "Retain"; + if ( delete ) { + function = "Delete"; + } + if ( ( nodes == null ) || nodes.isEmpty() ) { + JOptionPane + .showMessageDialog( this, + "Need to select external nodes, either via direct selection or via the \"Search\" function", + "No external nodes selected to " + function.toLowerCase(), + JOptionPane.ERROR_MESSAGE ); + return; + } + final int todo = nodes.size(); + final int ext = phy.getNumberOfExternalNodes(); + int res = todo; + if ( delete ) { + res = ext - todo; + } + if ( res < 1 ) { + JOptionPane.showMessageDialog( this, + "Cannot delete all nodes", + "Attempt to delete all nodes ", + JOptionPane.ERROR_MESSAGE ); + return; + } + final int result = JOptionPane.showConfirmDialog( null, function + " " + todo + + " external node(s), from a total of " + ext + " external nodes," + "\nresulting in tree with " + res + + " nodes?", function + " external nodes", JOptionPane.OK_CANCEL_OPTION ); + if ( result == JOptionPane.OK_OPTION ) { + if ( !delete ) { + final List to_delete = new ArrayList(); + for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) { + final PhylogenyNode n = it.next(); + if ( !nodes.contains( n ) ) { + to_delete.add( n ); + } + } + for( final PhylogenyNode n : to_delete ) { + phy.deleteSubtree( n, true ); + } + } + else { + for( final PhylogenyNode n : nodes ) { + phy.deleteSubtree( n, true ); + } + } + resetSearch(); + getCurrentTreePanel().setNodeInPreorderToNull(); + phy.externalNodesHaveChanged(); + phy.clearHashIdToNodeMap(); + phy.recalculateNumberOfExternalDescendants( true ); + getCurrentTreePanel().resetNodeIdToDistToLeafMap(); + getCurrentTreePanel().setEdited( true ); + repaint(); + } + } + void displayBasicInformation( final File treefile ) { if ( ( _mainpanel.getCurrentPhylogeny() != null ) && !_mainpanel.getCurrentPhylogeny().isEmpty() ) { String title = "Basic Information"; @@ -1095,6 +1435,32 @@ public abstract class MainFrame extends JFrame implements ActionListener { } } + private void doUpdateProcessMenu() { + if ( _process_pool.size() > 0 ) { + if ( _process_menu == null ) { + _process_menu = createMenu( "", getConfiguration() ); + _process_menu.setForeground( Color.RED ); + } + _process_menu.removeAll(); + final String text = "processes running: " + _process_pool.size(); + _process_menu.setText( text ); + _jmenubar.add( _process_menu ); + for( int i = 0; i < _process_pool.size(); ++i ) { + final ProcessRunning p = _process_pool.getProcessByIndex( i ); + _process_menu.add( customizeJMenuItem( new JMenuItem( p.getName() + " [" + p.getStart() + "]" ) ) ); + } + } + else { + if ( _process_menu != null ) { + _process_menu.removeAll(); + _jmenubar.remove( _process_menu ); + } + } + _jmenubar.validate(); + _jmenubar.repaint(); + repaint(); + } + void exceptionOccuredDuringOpenFile( final Exception e ) { try { _mainpanel.getCurrentTreePanel().setArrowCursor(); @@ -1280,6 +1646,56 @@ public abstract class MainFrame extends JFrame implements ActionListener { } } + public Configuration getConfiguration() { + return _configuration; + } + + ControlPanel getControlPanel() { + return getMainPanel().getControlPanel(); + } + + File getCurrentDir() { + if ( ( _current_dir == null ) || !_current_dir.canRead() ) { + if ( ForesterUtil.isWindows() ) { + try { + _current_dir = new File( WindowsUtils.getCurrentUserDesktopPath() ); + } + catch ( final Exception e ) { + _current_dir = null; + } + } + } + if ( ( _current_dir == null ) || !_current_dir.canRead() ) { + if ( System.getProperty( "user.home" ) != null ) { + _current_dir = new File( System.getProperty( "user.home" ) ); + } + else if ( System.getProperty( "user.dir" ) != null ) { + _current_dir = new File( System.getProperty( "user.dir" ) ); + } + } + return _current_dir; + } + + /** + * This method returns the current external node data which + * has been selected by the user by clicking the "Return ..." + * menu item. This method is expected to be called from Javascript or + * something like it. + * + * @return current external node data as String + */ + public String getCurrentExternalNodesDataBuffer() { + return getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString(); + } + + public int getCurrentExternalNodesDataBufferChangeCounter() { + return getCurrentTreePanel().getCurrentExternalNodesDataBufferChangeCounter(); + } + + public int getCurrentExternalNodesDataBufferLength() { + return getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString().length(); + } + TreePanel getCurrentTreePanel() { return getMainPanel().getCurrentTreePanel(); } @@ -1288,14 +1704,34 @@ public abstract class MainFrame extends JFrame implements ActionListener { return _help_jmenu; } + public InferenceManager getInferenceManager() { + return _inference_manager; + } + JCheckBoxMenuItem getlabelDirectionCbmi() { return _label_direction_cbmi; } + public MainPanel getMainPanel() { + return _mainpanel; + } + JMenuBar getMenuBarOfMainFrame() { return _jmenubar; } + public Options getOptions() { + return _options; + } + + private String getPreviousNodeAnnotationReference() { + return _previous_node_annotation_ref; + } + + public ProcessPool getProcessPool() { + return _process_pool; + } + final Phylogeny getSpeciesTree() { return _species_tree; } @@ -1380,6 +1816,51 @@ public abstract class MainFrame extends JFrame implements ActionListener { } } + void printPhylogenyToPdf( final String file_name ) { + if ( !getOptions().isPrintUsingActualSize() ) { + getCurrentTreePanel() + .calcParametersForPainting( getOptions().getPrintSizeX(), getOptions().getPrintSizeY() ); + getCurrentTreePanel().resetPreferredSize(); + getCurrentTreePanel().repaint(); + } + String pdf_written_to = ""; + boolean error = false; + try { + if ( getOptions().isPrintUsingActualSize() ) { + pdf_written_to = PdfExporter.writePhylogenyToPdf( file_name, + getCurrentTreePanel(), + getCurrentTreePanel().getWidth(), + getCurrentTreePanel().getHeight() ); + } + else { + pdf_written_to = PdfExporter.writePhylogenyToPdf( file_name, getCurrentTreePanel(), getOptions() + .getPrintSizeX(), getOptions().getPrintSizeY() ); + } + } + catch ( final IOException e ) { + error = true; + JOptionPane.showMessageDialog( this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE ); + } + if ( !error ) { + if ( !ForesterUtil.isEmpty( pdf_written_to ) ) { + JOptionPane.showMessageDialog( this, + "Wrote PDF to: " + pdf_written_to, + "Information", + JOptionPane.INFORMATION_MESSAGE ); + } + else { + JOptionPane.showMessageDialog( this, + "There was an unknown problem when attempting to write to PDF file: \"" + + file_name + "\"", + "Error", + JOptionPane.ERROR_MESSAGE ); + } + } + if ( !getOptions().isPrintUsingActualSize() ) { + getControlPanel().showWhole(); + } + } + void readPhylogeniesFromWebservice( final int i ) { final UrlTreeReader reader = new UrlTreeReader( this, i ); new Thread( reader ).start(); @@ -1394,10 +1875,41 @@ public abstract class MainFrame extends JFrame implements ActionListener { _textframes.clear(); } + private void removeBranchColors() { + if ( getMainPanel().getCurrentPhylogeny() != null ) { + AptxUtil.removeBranchColors( getMainPanel().getCurrentPhylogeny() ); + } + } + + private void removeVisualStyles() { + if ( getMainPanel().getCurrentPhylogeny() != null ) { + AptxUtil.removeVisualStyles( getMainPanel().getCurrentPhylogeny() ); + } + } + + void resetSearch() { + getMainPanel().getCurrentTreePanel().setFoundNodes0( null ); + getMainPanel().getCurrentTreePanel().setFoundNodes1( null ); + getMainPanel().getControlPanel().setSearchFoundCountsOnLabel0( 0 ); + getMainPanel().getControlPanel().getSearchFoundCountsLabel0().setVisible( false ); + getMainPanel().getControlPanel().getSearchTextField0().setText( "" ); + getMainPanel().getControlPanel().getSearchResetButton0().setEnabled( false ); + getMainPanel().getControlPanel().getSearchResetButton0().setVisible( false ); + getMainPanel().getControlPanel().setSearchFoundCountsOnLabel1( 0 ); + getMainPanel().getControlPanel().getSearchFoundCountsLabel1().setVisible( false ); + getMainPanel().getControlPanel().getSearchTextField1().setText( "" ); + getMainPanel().getControlPanel().getSearchResetButton1().setEnabled( false ); + getMainPanel().getControlPanel().getSearchResetButton1().setVisible( false ); + } + void setConfiguration( final Configuration configuration ) { _configuration = configuration; } + void setCurrentDir( final File current_dir ) { + _current_dir = current_dir; + } + void setInferenceManager( final InferenceManager i ) { _inference_manager = i; } @@ -1406,6 +1918,10 @@ public abstract class MainFrame extends JFrame implements ActionListener { _options = options; } + private void setPreviousNodeAnnotationReference( final String previous_node_annotation_ref ) { + _previous_node_annotation_ref = previous_node_annotation_ref; + } + void setSelectedTypeInTypeMenu( final PHYLOGENY_GRAPHICS_TYPE type ) { setTypeMenuToAllUnselected(); switch ( type ) { @@ -1453,6 +1969,15 @@ public abstract class MainFrame extends JFrame implements ActionListener { _circular_type_cbmi.setSelected( false ); } + public void showTextFrame( final String s, final String title ) { + checkTextFrames(); + _textframes.addLast( TextFrame.instantiate( s, title, _textframes ) ); + } + + public void showWhole() { + _mainpanel.getControlPanel().showWhole(); + } + void switchColors() { final TreeColorSet colorset = _mainpanel.getTreeColorSet(); final ColorSchemeChooser csc = new ColorSchemeChooser( getMainPanel(), colorset ); @@ -1624,6 +2149,18 @@ public abstract class MainFrame extends JFrame implements ActionListener { } } + public void updateProcessMenu() { + // In general Swing is not thread safe. + // See "Swing's Threading Policy". + SwingUtilities.invokeLater( new Runnable() { + + @Override + public void run() { + doUpdateProcessMenu(); + } + } ); + } + void updateTypeCheckboxes( final Options options, final Object o ) { setTypeMenuToAllUnselected(); ( ( JCheckBoxMenuItem ) o ).setSelected( true ); @@ -1662,416 +2199,372 @@ public abstract class MainFrame extends JFrame implements ActionListener { } } - private void annotateSequences() { - if ( getCurrentTreePanel() != null ) { - List nodes = null; - if ( ( getCurrentTreePanel().getFoundNodes0() != null ) - || ( getCurrentTreePanel().getFoundNodes1() != null ) ) { - nodes = getCurrentTreePanel().getFoundNodesAsListOfPhylogenyNodes(); + boolean writeAsNewHampshire( final Phylogeny t, boolean exception, final File file ) { + try { + final PhylogenyWriter writer = new PhylogenyWriter(); + writer.toNewHampshire( t, true, getOptions().getNhConversionSupportValueStyle(), file ); + } + catch ( final Exception e ) { + exception = true; + exceptionOccuredDuringSaveAs( e ); + } + return exception; + } + + boolean writeAsNexus( final Phylogeny t, boolean exception, final File file ) { + try { + final PhylogenyWriter writer = new PhylogenyWriter(); + writer.toNexus( file, t, getOptions().getNhConversionSupportValueStyle() ); + } + catch ( final Exception e ) { + exception = true; + exceptionOccuredDuringSaveAs( e ); + } + return exception; + } + + boolean writeAsPhyloXml( final Phylogeny t, boolean exception, final File file ) { + try { + final PhylogenyWriter writer = new PhylogenyWriter(); + writer.toPhyloXML( file, t, 0 ); + } + catch ( final Exception e ) { + exception = true; + exceptionOccuredDuringSaveAs( e ); + } + return exception; + } + + void writePhylogenyToGraphicsFile( final String file_name, final GraphicsExportType type ) { + _mainpanel.getCurrentTreePanel().calcParametersForPainting( _mainpanel.getCurrentTreePanel().getWidth(), + _mainpanel.getCurrentTreePanel().getHeight() ); + String file_written_to = ""; + boolean error = false; + try { + 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; + JOptionPane.showMessageDialog( this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE ); + } + if ( !error ) { + if ( ( file_written_to != null ) && ( file_written_to.length() > 0 ) ) { + JOptionPane.showMessageDialog( this, + "Wrote image to: " + file_written_to, + "Graphics Export", + JOptionPane.INFORMATION_MESSAGE ); } - if ( ( nodes == null ) || nodes.isEmpty() ) { - JOptionPane - .showMessageDialog( this, - "Need to select nodes, either via direct selection or via the \"Search\" function", - "No nodes selected for annotation", - JOptionPane.ERROR_MESSAGE ); - return; + else { + JOptionPane.showMessageDialog( this, + "There was an unknown problem when attempting to write to an image file: \"" + + file_name + "\"", + "Error", + JOptionPane.ERROR_MESSAGE ); } - final Phylogeny phy = getMainPanel().getCurrentPhylogeny(); - if ( ( phy != null ) && !phy.isEmpty() ) { - final JTextField ref_field = new JTextField( 10 ); - final JTextField desc_filed = new JTextField( 20 ); - ref_field.setText( ForesterUtil.isEmpty( getPreviousNodeAnnotationReference() ) ? "" - : getPreviousNodeAnnotationReference() ); - final JPanel my_panel = new JPanel(); - my_panel.add( new JLabel( "Reference " ) ); - my_panel.add( ref_field ); - my_panel.add( Box.createHorizontalStrut( 15 ) ); - my_panel.add( new JLabel( "Description " ) ); - my_panel.add( desc_filed ); - final int result = JOptionPane.showConfirmDialog( null, - my_panel, - "Enter the sequence annotation(s) for the " - + nodes.size() + " selected nodes", - JOptionPane.OK_CANCEL_OPTION ); - if ( result == JOptionPane.OK_OPTION ) { - String ref = ref_field.getText(); - String desc = desc_filed.getText(); - if ( !ForesterUtil.isEmpty( ref ) ) { - ref = ref.trim(); - ref = ref.replaceAll( "\\s+", " " ); - if ( ( ref.indexOf( ':' ) < 1 ) || ( ref.indexOf( ':' ) > ( ref.length() - 2 ) ) - || ( ref.length() < 3 ) ) { - JOptionPane.showMessageDialog( this, - "Reference needs to be in the form of \"GO:1234567\"", - "Illegal Format for Annotation Reference", - JOptionPane.ERROR_MESSAGE ); - return; - } + } + _contentpane.repaint(); + } + + void writeToFile( final Phylogeny t ) { + if ( t == null ) { + return; + } + String initial_filename = null; + if ( getMainPanel().getCurrentTreePanel().getTreeFile() != null ) { + try { + initial_filename = getMainPanel().getCurrentTreePanel().getTreeFile().getCanonicalPath(); + } + catch ( final IOException e ) { + initial_filename = null; + } + } + if ( !ForesterUtil.isEmpty( initial_filename ) ) { + _save_filechooser.setSelectedFile( new File( initial_filename ) ); + } + else { + _save_filechooser.setSelectedFile( new File( "" ) ); + } + final File my_dir = getCurrentDir(); + if ( my_dir != null ) { + _save_filechooser.setCurrentDirectory( my_dir ); + } + final int result = _save_filechooser.showSaveDialog( _contentpane ); + final File file = _save_filechooser.getSelectedFile(); + setCurrentDir( _save_filechooser.getCurrentDirectory() ); + boolean exception = false; + if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) { + if ( file.exists() ) { + final int i = JOptionPane.showConfirmDialog( this, + file + " already exists.\nOverwrite?", + "Overwrite?", + JOptionPane.OK_CANCEL_OPTION, + JOptionPane.QUESTION_MESSAGE ); + if ( i != JOptionPane.OK_OPTION ) { + return; + } + else { + final File to = new File( file.getAbsoluteFile().toString() + Constants.BACKUP_FILE_SUFFIX ); + try { + ForesterUtil.copyFile( file, to ); } - if ( ref != null ) { - setPreviousNodeAnnotationReference( ref ); + catch ( final Exception e ) { + JOptionPane.showMessageDialog( this, + "Failed to create backup copy " + to, + "Failed to Create Backup Copy", + JOptionPane.WARNING_MESSAGE ); } - if ( desc != null ) { - desc = desc.trim(); - desc = desc.replaceAll( "\\s+", " " ); + try { + file.delete(); } - if ( !ForesterUtil.isEmpty( ref ) || !ForesterUtil.isEmpty( desc ) ) { - for( final PhylogenyNode n : nodes ) { - ForesterUtil.ensurePresenceOfSequence( n ); - final Annotation ann = ForesterUtil.isEmpty( ref ) ? new Annotation() - : new Annotation( ref ); - if ( !ForesterUtil.isEmpty( desc ) ) { - ann.setDesc( desc ); - } - n.getNodeData().getSequence().addAnnotation( ann ); - } + catch ( final Exception e ) { + JOptionPane.showMessageDialog( this, + "Failed to delete: " + file, + "Failed to Delete", + JOptionPane.WARNING_MESSAGE ); } - getMainPanel().getControlPanel().showAnnotations(); } } - } - } - - private void chooseFont() { - final FontChooser fc = new FontChooser(); - fc.setFont( getMainPanel().getTreeFontSet().getLargeFont() ); - fc.showDialog( this, "Select the Base Font" ); - getMainPanel().getTreeFontSet().setBaseFont( fc.getFont() ); - } - - private void chooseMinimalConfidence() { - final String s = ( String ) JOptionPane - .showInputDialog( this, - "Please enter the minimum for confidence values to be displayed.\n" - + "[current value: " + getOptions().getMinConfidenceValue() + "]\n", - "Minimal Confidence Value", - JOptionPane.QUESTION_MESSAGE, - null, - null, - getOptions().getMinConfidenceValue() ); - if ( !ForesterUtil.isEmpty( s ) ) { - boolean success = true; - double m = 0.0; - final String m_str = s.trim(); - if ( !ForesterUtil.isEmpty( m_str ) ) { - try { - m = Double.parseDouble( m_str ); - } - catch ( final Exception ex ) { - success = false; - } + if ( _save_filechooser.getFileFilter() == MainFrame.nhfilter ) { + exception = writeAsNewHampshire( t, exception, file ); + } + else if ( _save_filechooser.getFileFilter() == MainFrame.xmlfilter ) { + exception = writeAsPhyloXml( t, exception, file ); } + else if ( _save_filechooser.getFileFilter() == MainFrame.nexusfilter ) { + exception = writeAsNexus( t, exception, file ); + } + // "*.*": else { - success = false; + final String file_name = file.getName().trim().toLowerCase(); + if ( file_name.endsWith( ".nh" ) || file_name.endsWith( ".newick" ) || file_name.endsWith( ".phy" ) + || file_name.endsWith( ".tree" ) ) { + exception = writeAsNewHampshire( t, exception, file ); + } + else if ( file_name.endsWith( ".nex" ) || file_name.endsWith( ".nexus" ) ) { + exception = writeAsNexus( t, exception, file ); + } + // XML is default: + else { + exception = writeAsPhyloXml( t, exception, file ); + } } - if ( success && ( m >= 0.0 ) ) { - getOptions().setMinConfidenceValue( m ); + if ( !exception ) { + getMainPanel().setTitleOfSelectedTab( file.getName() ); + getMainPanel().getCurrentTreePanel().setTreeFile( file ); + getMainPanel().getCurrentTreePanel().setEdited( false ); } } } - private void doUpdateProcessMenu() { - if ( _process_pool.size() > 0 ) { - if ( _process_menu == null ) { - _process_menu = createMenu( "", getConfiguration() ); - _process_menu.setForeground( Color.RED ); + void writeToGraphicsFile( final Phylogeny t, final GraphicsExportType type ) { + if ( ( t == null ) || t.isEmpty() ) { + return; + } + String initial_filename = ""; + if ( getMainPanel().getCurrentTreePanel().getTreeFile() != null ) { + initial_filename = getMainPanel().getCurrentTreePanel().getTreeFile().toString(); + } + if ( initial_filename.indexOf( '.' ) > 0 ) { + initial_filename = initial_filename.substring( 0, initial_filename.lastIndexOf( '.' ) ); + } + initial_filename = initial_filename + "." + type; + _writetographics_filechooser.setSelectedFile( new File( initial_filename ) ); + final File my_dir = getCurrentDir(); + if ( my_dir != null ) { + _writetographics_filechooser.setCurrentDirectory( my_dir ); + } + final int result = _writetographics_filechooser.showSaveDialog( _contentpane ); + File file = _writetographics_filechooser.getSelectedFile(); + setCurrentDir( _writetographics_filechooser.getCurrentDirectory() ); + if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) { + if ( !file.toString().toLowerCase().endsWith( type.toString() ) ) { + file = new File( file.toString() + "." + type ); } - _process_menu.removeAll(); - final String text = "processes running: " + _process_pool.size(); - _process_menu.setText( text ); - _jmenubar.add( _process_menu ); - for( int i = 0; i < _process_pool.size(); ++i ) { - final ProcessRunning p = _process_pool.getProcessByIndex( i ); - _process_menu.add( customizeJMenuItem( new JMenuItem( p.getName() + " [" + p.getStart() + "]" ) ) ); + if ( file.exists() ) { + final int i = JOptionPane.showConfirmDialog( this, + file + " already exists. Overwrite?", + "Warning", + JOptionPane.OK_CANCEL_OPTION, + JOptionPane.WARNING_MESSAGE ); + if ( i != JOptionPane.OK_OPTION ) { + return; + } + else { + try { + file.delete(); + } + catch ( final Exception e ) { + JOptionPane.showMessageDialog( this, + "Failed to delete: " + file, + "Error", + JOptionPane.WARNING_MESSAGE ); + } + } } + writePhylogenyToGraphicsFile( file.toString(), type ); } - else { - if ( _process_menu != null ) { - _process_menu.removeAll(); - _jmenubar.remove( _process_menu ); + } + + void writeToPdf( final Phylogeny t ) { + if ( ( t == null ) || t.isEmpty() ) { + return; + } + String initial_filename = ""; + if ( getMainPanel().getCurrentTreePanel().getTreeFile() != null ) { + initial_filename = getMainPanel().getCurrentTreePanel().getTreeFile().toString(); + } + if ( initial_filename.indexOf( '.' ) > 0 ) { + initial_filename = initial_filename.substring( 0, initial_filename.lastIndexOf( '.' ) ); + } + initial_filename = initial_filename + ".pdf"; + _writetopdf_filechooser.setSelectedFile( new File( initial_filename ) ); + final File my_dir = getCurrentDir(); + if ( my_dir != null ) { + _writetopdf_filechooser.setCurrentDirectory( my_dir ); + } + final int result = _writetopdf_filechooser.showSaveDialog( _contentpane ); + File file = _writetopdf_filechooser.getSelectedFile(); + setCurrentDir( _writetopdf_filechooser.getCurrentDirectory() ); + if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) { + if ( !file.toString().toLowerCase().endsWith( ".pdf" ) ) { + file = new File( file.toString() + ".pdf" ); + } + if ( file.exists() ) { + final int i = JOptionPane.showConfirmDialog( this, + file + " already exists. Overwrite?", + "WARNING", + JOptionPane.OK_CANCEL_OPTION, + JOptionPane.WARNING_MESSAGE ); + if ( i != JOptionPane.OK_OPTION ) { + return; + } } + printPhylogenyToPdf( file.toString() ); } - _jmenubar.validate(); - _jmenubar.repaint(); - repaint(); } +} - private String getPreviousNodeAnnotationReference() { - return _previous_node_annotation_ref; - } +class MsaFileFilter extends FileFilter { - private void removeBranchColors() { - if ( getMainPanel().getCurrentPhylogeny() != null ) { - AptxUtil.removeBranchColors( getMainPanel().getCurrentPhylogeny() ); - } + @Override + public boolean accept( final File f ) { + final String file_name = f.getName().trim().toLowerCase(); + return file_name.endsWith( ".msa" ) || file_name.endsWith( ".aln" ) || file_name.endsWith( ".fasta" ) + || file_name.endsWith( ".fas" ) || file_name.endsWith( ".fa" ) || f.isDirectory(); } - private void removeVisualStyles() { - if ( getMainPanel().getCurrentPhylogeny() != null ) { - AptxUtil.removeVisualStyles( getMainPanel().getCurrentPhylogeny() ); - } + @Override + public String getDescription() { + return "Multiple sequence alignment files (*.msa, *.aln, *.fasta, *.fa, *.fas)"; } +} - private void setPreviousNodeAnnotationReference( final String previous_node_annotation_ref ) { - _previous_node_annotation_ref = previous_node_annotation_ref; - } +class NexusFilter extends FileFilter { - /** - * Display the about box. - */ - static void about() { - final StringBuffer about = new StringBuffer( "Archaeopteryx\nVersion " + Constants.VERSION + "\n" ); - about.append( "Copyright (C) 2014 Christian M Zmasek\n" ); - about.append( "All Rights Reserved\n" ); - about.append( "License: GNU Lesser General Public License (LGPL)\n" ); - about.append( "Last modified: " + Constants.PRG_DATE + "\n" ); - about.append( "Based on: " + ForesterUtil.getForesterLibraryInformation() + "\n" ); - about.append( "phyloXML version : " + ForesterConstants.PHYLO_XML_VERSION + "\n" ); - about.append( "phyloXML location: " + ForesterConstants.PHYLO_XML_LOCATION + "\n" ); - if ( !ForesterUtil.isEmpty( ForesterUtil.JAVA_VERSION ) && !ForesterUtil.isEmpty( ForesterUtil.JAVA_VENDOR ) ) { - about.append( "[your Java version: " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]\n" ); - } - if ( !ForesterUtil.isEmpty( ForesterUtil.OS_NAME ) && !ForesterUtil.isEmpty( ForesterUtil.OS_ARCH ) - && !ForesterUtil.isEmpty( ForesterUtil.OS_VERSION ) ) { - about.append( "[your OS: " + ForesterUtil.OS_NAME + " " + ForesterUtil.OS_ARCH + " " - + ForesterUtil.OS_VERSION + "]\n" ); - } - final Runtime rt = java.lang.Runtime.getRuntime(); - final long free_memory = rt.freeMemory() / 1000000; - final long total_memory = rt.totalMemory() / 1000000; - about.append( "[free memory: " + free_memory + "MB, total memory: " + total_memory + "MB]\n" ); - about.append( "[locale: " + Locale.getDefault() + "]\n" ); - about.append( "References:\n" ); - about.append( Constants.PHYLOXML_REFERENCE_SHORT + "\n" ); - about.append( "For more information & download:\n" ); - about.append( Constants.APTX_WEB_SITE + "\n" ); - about.append( "Documentation:\n" ); - about.append( Constants.APTX_DOC_SITE + "\n" ); - about.append( "Comments: " + Constants.AUTHOR_EMAIL ); - JOptionPane.showMessageDialog( null, about, Constants.PRG_NAME, JOptionPane.PLAIN_MESSAGE ); + @Override + public boolean accept( final File f ) { + final String file_name = f.getName().trim().toLowerCase(); + return file_name.endsWith( ".nex" ) || file_name.endsWith( ".nexus" ) || file_name.endsWith( ".nx" ) + || file_name.endsWith( ".tre" ) || f.isDirectory(); } - static void chooseNodeSize( final Options options, final Component parent ) { - final String s = ( String ) JOptionPane.showInputDialog( parent, - "Please enter the default size for node shapes.\n" - + "[current value: " - + options.getDefaultNodeShapeSize() + "]\n", - "Node Shape Size", - JOptionPane.QUESTION_MESSAGE, - null, - null, - options.getDefaultNodeShapeSize() ); - if ( !ForesterUtil.isEmpty( s ) ) { - boolean success = true; - double m = 0.0; - final String m_str = s.trim(); - if ( !ForesterUtil.isEmpty( m_str ) ) { - try { - m = Double.parseDouble( m_str ); - } - catch ( final Exception ex ) { - success = false; - } - } - else { - success = false; - } - if ( success && ( m >= 0.0 ) ) { - final short size = ForesterUtil.roundToShort( m ); - if ( size >= 0.0 ) { - options.setDefaultNodeShapeSize( size ); - } - } - } + @Override + public String getDescription() { + return "Nexus files (*.nex, *.nexus, *.nx, *.tre)"; } +} // NexusFilter - static String createCurrentFontDesc( final TreeFontSet tree_font_set ) { - return tree_font_set.getLargeFont().getFamily() + " " + tree_font_set.getLargeFont().getSize(); - } +class NHFilter extends FileFilter { - static JMenu createMenu( final String title, final Configuration conf ) { - final JMenu jmenu = new JMenu( title ); - if ( !conf.isUseNativeUI() ) { - jmenu.setFont( MainFrame.menu_font ); - jmenu.setBackground( conf.getGuiMenuBackgroundColor() ); - jmenu.setForeground( conf.getGuiMenuTextColor() ); - } - return jmenu; + @Override + public boolean accept( final File f ) { + final String file_name = f.getName().trim().toLowerCase(); + return file_name.endsWith( ".nh" ) || file_name.endsWith( ".newick" ) || file_name.endsWith( ".phy" ) + || file_name.endsWith( ".tr" ) || file_name.endsWith( ".tree" ) || file_name.endsWith( ".dnd" ) + || file_name.endsWith( ".ph" ) || file_name.endsWith( ".phb" ) || file_name.endsWith( ".nwk" ) + || f.isDirectory(); } - static JMenuItem customizeMenuItemAsLabel( final JMenuItem label, final Configuration configuration ) { - label.setFont( MainFrame.menu_font.deriveFont( Font.BOLD ) ); - if ( !configuration.isUseNativeUI() ) { - label.setBackground( configuration.getGuiMenuBackgroundColor() ); - label.setForeground( configuration.getGuiMenuTextColor() ); - label.setOpaque( true ); - } - label.setSelected( false ); - label.setEnabled( false ); - return label; + @Override + public String getDescription() { + return "New Hampshire - Newick files (*.nh, *.newick, *.phy, *.tree, *.dnd, *.tr, *.ph, *.phb, *.nwk)"; } +} // NHFilter - static void cycleNodeFill( final Options op ) { - switch ( op.getDefaultNodeFill() ) { - case GRADIENT: - op.setDefaultNodeFill( NodeFill.SOLID ); - break; - case NONE: - op.setDefaultNodeFill( NodeFill.GRADIENT ); - break; - case SOLID: - op.setDefaultNodeFill( NodeFill.NONE ); - break; - default: - throw new RuntimeException( "unknown fill: " + op.getDefaultNodeFill() ); - } - } +class NHXFilter extends FileFilter { - static void cycleNodeShape( final Options op ) { - switch ( op.getDefaultNodeShape() ) { - case CIRCLE: - op.setDefaultNodeShape( NodeShape.RECTANGLE ); - break; - case RECTANGLE: - op.setDefaultNodeShape( NodeShape.CIRCLE ); - break; - default: - throw new RuntimeException( "unknown shape: " + op.getDefaultNodeShape() ); - } + @Override + public boolean accept( final File f ) { + final String file_name = f.getName().trim().toLowerCase(); + return file_name.endsWith( ".nhx" ) || f.isDirectory(); } - private static void cycleNodeDataReturn( final Options op, Configuration conf ) { - switch ( op.getExtDescNodeDataToReturn() ) { - case UNKNOWN: - op.setExtDescNodeDataToReturn( NodeDataField.DOMAINS_ALL ); - break; - case DOMAINS_ALL: - op.setExtDescNodeDataToReturn( NodeDataField.DOMAINS_COLLAPSED_PER_PROTEIN ); - break; - case DOMAINS_COLLAPSED_PER_PROTEIN: - op.setExtDescNodeDataToReturn( NodeDataField.SEQ_ANNOTATIONS ); - break; - case SEQ_ANNOTATIONS: - op.setExtDescNodeDataToReturn( NodeDataField.GO_TERM_IDS ); - break; - case GO_TERM_IDS: - op.setExtDescNodeDataToReturn( NodeDataField.SEQUENCE_MOL_SEQ_FASTA ); - break; - case SEQUENCE_MOL_SEQ_FASTA: - if ( conf != null && conf.getExtDescNodeDataToReturn() != null - && conf.getExtDescNodeDataToReturn() != NodeDataField.DOMAINS_ALL - && conf.getExtDescNodeDataToReturn() != NodeDataField.DOMAINS_COLLAPSED_PER_PROTEIN - && conf.getExtDescNodeDataToReturn() != NodeDataField.SEQ_ANNOTATIONS - && conf.getExtDescNodeDataToReturn() != NodeDataField.GO_TERM_IDS - && conf.getExtDescNodeDataToReturn() != NodeDataField.SEQUENCE_MOL_SEQ_FASTA - ) { - op.setExtDescNodeDataToReturn( conf.getExtDescNodeDataToReturn() ); - } - else { - op.setExtDescNodeDataToReturn( NodeDataField.UNKNOWN ); - } - break; - default: - op.setExtDescNodeDataToReturn( NodeDataField.UNKNOWN ); - } + @Override + public String getDescription() { + return "NHX files (*.nhx) [deprecated]"; } +} - static void cycleOverview( final Options op, final TreePanel tree_panel ) { - switch ( op.getOvPlacement() ) { - case LOWER_LEFT: - op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.UPPER_LEFT ); - break; - case LOWER_RIGHT: - op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.LOWER_LEFT ); - break; - case UPPER_LEFT: - op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.UPPER_RIGHT ); - break; - case UPPER_RIGHT: - op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.LOWER_RIGHT ); - break; - default: - throw new RuntimeException( "unknown placement: " + op.getOvPlacement() ); - } - if ( tree_panel != null ) { - tree_panel.updateOvSettings(); - } - } +class PdfFilter extends FileFilter { - static void setCycleNodeFillMenuItem( final JMenuItem mi, final Options options ) { - if ( ( options != null ) && ( options.getDefaultNodeFill() != null ) ) { - mi.setText( "Cycle Node Shape Fill Type... (current: " - + options.getDefaultNodeFill().toString().toLowerCase() + ")" ); - } - else { - mi.setText( "Cycle Node Shape Fill Type..." ); - } + @Override + public boolean accept( final File f ) { + return f.getName().trim().toLowerCase().endsWith( ".pdf" ) || f.isDirectory(); } - static void setCycleNodeShapeMenuItem( final JMenuItem mi, final Options options ) { - if ( ( options != null ) && ( options.getDefaultNodeShape() != null ) ) { - mi.setText( "Cycle Node Shape Fill Type... (current: " - + options.getDefaultNodeShape().toString().toLowerCase() + ")" ); - } - else { - mi.setText( "Cycle Node Shape Fill Type..." ); - } + @Override + public String getDescription() { + return "PDF files (*.pdf)"; } +} // PdfFilter - static void setCycleDataReturnMenuItem( final JMenuItem mi, final Options options ) { - if ( ( options != null ) && ( options.getExtDescNodeDataToReturn() != null ) ) { - mi.setText( "Cycle Node Return Data... (current: " - + options.getExtDescNodeDataToReturn().toString() + ")" ); - } - else { - mi.setText( "Cycle Node Return Data..." ); - } - } +class SequencesFileFilter extends FileFilter { - static void setOvPlacementColorChooseMenuItem( final JMenuItem mi, final Options options ) { - if ( ( options != null ) && ( options.getOvPlacement() != null ) ) { - mi.setText( "Cycle Overview Placement... (current: " + options.getOvPlacement() + ")" ); - } - else { - mi.setText( "Cycle Overview Placement..." ); - } + @Override + public boolean accept( final File f ) { + final String file_name = f.getName().trim().toLowerCase(); + return file_name.endsWith( ".fasta" ) || file_name.endsWith( ".fa" ) || file_name.endsWith( ".fas" ) + || file_name.endsWith( ".seqs" ) || f.isDirectory(); } - static void setTextColorChooseMenuItem( final JMenuItem mi, final TreePanel tree_panel ) { - if ( ( tree_panel != null ) && ( tree_panel.getTreeColorSet() != null ) ) { - mi.setText( "Select Color Scheme... (current: " + tree_panel.getTreeColorSet().getCurrentColorSchemeName() - + ")" ); - } - else { - mi.setText( "Select Color Scheme..." ); - } + @Override + public String getDescription() { + return "Sequences files (*.fasta, *.fa, *.fas, *.seqs )"; } +} - static void setTextForFontChooserMenuItem( final JMenuItem mi, final String font_desc ) { - mi.setText( "Select Default Font... (current: " + font_desc + ")" ); +class TolFilter extends FileFilter { + + @Override + public boolean accept( final File f ) { + final String file_name = f.getName().trim().toLowerCase(); + return ( file_name.endsWith( ".tol" ) || file_name.endsWith( ".tolxml" ) || file_name.endsWith( ".zip" ) || f + .isDirectory() ) && ( !file_name.endsWith( ".xml.zip" ) ); } - static void setTextMinSupportMenuItem( final JMenuItem mi, final Options options, final TreePanel current_tree_panel ) { - if ( ( current_tree_panel == null ) || ( current_tree_panel.getPhylogeny() == null ) ) { - mi.setEnabled( true ); - } - else if ( AptxUtil.isHasAtLeastOneBranchWithSupportValues( current_tree_panel.getPhylogeny() ) ) { - mi.setEnabled( true ); - } - else { - mi.setEnabled( false ); - } - mi.setText( "Enter Min Confidence Value... (current: " + options.getMinConfidenceValue() + ")" ); + @Override + public String getDescription() { + return "Tree of Life files (*.tol, *.tolxml)"; } +} // TolFilter - static void setTextNodeSizeMenuItem( final JMenuItem mi, final Options options ) { - mi.setText( "Enter Default Node Shape Size... (current: " + options.getDefaultNodeShapeSize() + ")" ); +class XMLFilter extends FileFilter { + + @Override + public boolean accept( final File f ) { + final String file_name = f.getName().trim().toLowerCase(); + return file_name.endsWith( ".xml" ) || file_name.endsWith( ".phyloxml" ) || file_name.endsWith( "phylo.xml" ) + || file_name.endsWith( ".pxml" ) || file_name.endsWith( ".zip" ) || f.isDirectory(); } - static void updateScreenTextAntialias( final List treepanels ) { - for( final TreePanel tree_panel : treepanels ) { - tree_panel.setTextAntialias(); - } + @Override + public String getDescription() { + return "phyloXML files (*.xml, *.phyloxml, *phylo.xml, *.pxml, *.zip)"; } -} +} // XMLFilter diff --git a/forester/java/src/org/forester/archaeopteryx/MainFrameApplet.java b/forester/java/src/org/forester/archaeopteryx/MainFrameApplet.java index f5bfd5c..d742229 100644 --- a/forester/java/src/org/forester/archaeopteryx/MainFrameApplet.java +++ b/forester/java/src/org/forester/archaeopteryx/MainFrameApplet.java @@ -126,41 +126,6 @@ public final class MainFrameApplet extends MainFrame { System.gc(); } - private void readSpeciesTree( final Configuration configuration, final String species_tree_url_str ) - throws MalformedURLException, FileNotFoundException, IOException { - final URL species_tree_url = new URL( species_tree_url_str ); - final Phylogeny[] species_trees = AptxUtil.readPhylogeniesFromUrl( species_tree_url, - configuration - .isValidatePhyloXmlAgainstSchema(), - configuration - .isReplaceUnderscoresInNhParsing(), - false, - TAXONOMY_EXTRACTION.NO, - false ); - if ( ( species_trees != null ) && ( species_trees.length > 0 ) ) { - AptxUtil.printAppletMessage( ArchaeopteryxA.NAME, "successfully read species tree" ); - if ( species_trees[ 0 ].isEmpty() ) { - ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, "species tree is empty" ); - } - else if ( !species_trees[ 0 ].isRooted() ) { - ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, "species tree is not rooted" ); - } - else { - setSpeciesTree( species_trees[ 0 ] ); - AptxUtil.printAppletMessage( ArchaeopteryxA.NAME, "species tree OK" ); - } - } - else { - ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, "failed to read species tree from " - + species_tree_url_str ); - } - } - - @Override - public MainPanel getMainPanel() { - return _mainpanel; - } - void buildAnalysisMenu() { _analysis_menu = MainFrame.createMenu( "Analysis", getConfiguration() ); _analysis_menu.add( _gsdi_item = new JMenuItem( "GSDI (Generalized Speciation Duplication Inference)" ) ); @@ -186,10 +151,11 @@ public final class MainFrameApplet extends MainFrame { .setTextMinSupportMenuItem( _choose_minimal_confidence_mi, getOptions(), getCurrentTreePanel() ); MainFrame.setTextForFontChooserMenuItem( _choose_font_mi, createCurrentFontDesc( getMainPanel() .getTreeFontSet() ) ); + setTextForGraphicsSizeChooserMenuItem( _print_size_mi, getOptions() ); + setTextForPdfLineWidthChooserMenuItem( _choose_pdf_width_mi, getOptions() ); MainFrame.setCycleNodeFillMenuItem( _cycle_node_fill_mi, getOptions() ); MainFrame.setCycleNodeShapeMenuItem( _cycle_node_shape_mi, getOptions() ); MainFrame.setTextNodeSizeMenuItem( _choose_node_size_mi, getOptions() ); - try { getMainPanel().getControlPanel().setVisibilityOfDomainStrucureCB(); getMainPanel().getControlPanel().setVisibilityOfX(); @@ -250,6 +216,30 @@ public final class MainFrameApplet extends MainFrame { _options_jmenu.add( _search_with_regex_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_REGEX_LABEL ) ); _search_with_regex_cbmi.setToolTipText( MainFrame.SEARCH_WITH_REGEX_TIP ); _options_jmenu.add( _inverse_search_result_cbmi = new JCheckBoxMenuItem( INVERSE_SEARCH_RESULT_LABEL ) ); + // + _options_jmenu.addSeparator(); + _options_jmenu.add( customizeMenuItemAsLabel( new JMenuItem( "Graphics Export & Printing:" ), + getConfiguration() ) ); + _options_jmenu.add( _antialias_print_cbmi = new JCheckBoxMenuItem( "Antialias" ) ); + _options_jmenu.add( _print_black_and_white_cbmi = new JCheckBoxMenuItem( "Export in Black and White" ) ); + _options_jmenu + .add( _print_using_actual_size_cbmi = new JCheckBoxMenuItem( "Use Current Image Size for PDF export and Printing" ) ); + _options_jmenu + .add( _graphics_export_using_actual_size_cbmi = new JCheckBoxMenuItem( "Use Current Image Size for PNG, JPG, and GIF export" ) ); + _options_jmenu + .add( _graphics_export_visible_only_cbmi = new JCheckBoxMenuItem( "Limit to Visible ('Screenshot') for PNG, JPG, and GIF export" ) ); + _options_jmenu.add( _print_size_mi = new JMenuItem( "" ) ); + _options_jmenu.add( _choose_pdf_width_mi = new JMenuItem( "" ) ); + // + customizeCheckBoxMenuItem( _antialias_print_cbmi, getOptions().isAntialiasPrint() ); + customizeCheckBoxMenuItem( _print_black_and_white_cbmi, getOptions().isPrintBlackAndWhite() ); + customizeCheckBoxMenuItem( _graphics_export_visible_only_cbmi, getOptions().isGraphicsExportVisibleOnly() ); + customizeCheckBoxMenuItem( _print_using_actual_size_cbmi, getOptions().isPrintUsingActualSize() ); + customizeCheckBoxMenuItem( _graphics_export_using_actual_size_cbmi, getOptions() + .isGraphicsExportUsingActualSize() ); + customizeJMenuItem( _print_size_mi ); + customizeJMenuItem( _choose_pdf_width_mi ); + // customizeJMenuItem( _choose_font_mi ); customizeJMenuItem( _switch_colors_mi ); customizeJMenuItem( _choose_minimal_confidence_mi ); @@ -316,4 +306,39 @@ public final class MainFrameApplet extends MainFrame { JApplet getApplet() { return _applet; } + + @Override + public MainPanel getMainPanel() { + return _mainpanel; + } + + private void readSpeciesTree( final Configuration configuration, final String species_tree_url_str ) + throws MalformedURLException, FileNotFoundException, IOException { + final URL species_tree_url = new URL( species_tree_url_str ); + final Phylogeny[] species_trees = AptxUtil.readPhylogeniesFromUrl( species_tree_url, + configuration + .isValidatePhyloXmlAgainstSchema(), + configuration + .isReplaceUnderscoresInNhParsing(), + false, + TAXONOMY_EXTRACTION.NO, + false ); + if ( ( species_trees != null ) && ( species_trees.length > 0 ) ) { + AptxUtil.printAppletMessage( ArchaeopteryxA.NAME, "successfully read species tree" ); + if ( species_trees[ 0 ].isEmpty() ) { + ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, "species tree is empty" ); + } + else if ( !species_trees[ 0 ].isRooted() ) { + ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, "species tree is not rooted" ); + } + else { + setSpeciesTree( species_trees[ 0 ] ); + AptxUtil.printAppletMessage( ArchaeopteryxA.NAME, "species tree OK" ); + } + } + else { + ForesterUtil.printErrorMessage( ArchaeopteryxA.NAME, "failed to read species tree from " + + species_tree_url_str ); + } + } } diff --git a/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java b/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java index dcf7818..29a0a57 100644 --- a/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java +++ b/forester/java/src/org/forester/archaeopteryx/MainFrameApplication.java @@ -57,7 +57,6 @@ import javax.swing.UnsupportedLookAndFeelException; import javax.swing.WindowConstants; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; -import javax.swing.filechooser.FileFilter; import javax.swing.plaf.synth.SynthLookAndFeel; import org.forester.analysis.TaxonomyDataManager; @@ -105,58 +104,72 @@ import org.forester.util.BasicTable; import org.forester.util.BasicTableParser; import org.forester.util.DescriptiveStatistics; import org.forester.util.ForesterUtil; -import org.forester.util.WindowsUtils; public final class MainFrameApplication extends MainFrame { - static final String INFER_ANCESTOR_TAXONOMIES = "Infer Ancestor Taxonomies"; - static final String OBTAIN_DETAILED_TAXONOMIC_INFORMATION = "Obtain Detailed Taxonomic Information"; - private final static int FRAME_X_SIZE = 800; - private final static int FRAME_Y_SIZE = 800; + public static MainFrameApplication createInstance( final Phylogeny[] phys, final Configuration config ) { + return new MainFrameApplication( phys, config ); + } + + static MainFrame createInstance( final Phylogeny[] phys, final Configuration config, final String title ) { + return new MainFrameApplication( phys, config, 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 MainFrame createInstance( final Phylogeny[] phys, final String config_file_name, final String title ) { + return new MainFrameApplication( phys, config_file_name, title ); + } + + static void warnIfNotPhyloXmlValidation( final Configuration c ) { + if ( !c.isValidatePhyloXmlAgainstSchema() ) { + JOptionPane + .showMessageDialog( null, + ForesterUtil + .wordWrap( "phyloXML XSD-based validation is turned off [enable with line 'validate_against_phyloxml_xsd_schem: true' in configuration file]", + 80 ), + "Warning", + JOptionPane.WARNING_MESSAGE ); + } + } + static final String INFER_ANCESTOR_TAXONOMIES = "Infer Ancestor Taxonomies"; + static final String OBTAIN_DETAILED_TAXONOMIC_INFORMATION = "Obtain Detailed Taxonomic Information"; + private final static int FRAME_X_SIZE = 800; + private final static int FRAME_Y_SIZE = 800; // Filters for the file-open dialog (classes defined in this file) - private final static NHFilter nhfilter = new NHFilter(); - private final static NHXFilter nhxfilter = new NHXFilter(); - private final static XMLFilter xmlfilter = new XMLFilter(); - private final static TolFilter tolfilter = new TolFilter(); - private final static NexusFilter nexusfilter = new NexusFilter(); - private final static PdfFilter pdffilter = new PdfFilter(); - private final static GraphicsFileFilter graphicsfilefilter = new GraphicsFileFilter(); - private final static MsaFileFilter msafilter = new MsaFileFilter(); - private final static SequencesFileFilter seqsfilter = new SequencesFileFilter(); - private final static DefaultFilter defaultfilter = new DefaultFilter(); - private static final long serialVersionUID = -799735726778865234L; - private static final boolean PREPROCESS_TREES = false; - private final JFileChooser _values_filechooser; - private final JFileChooser _sequences_filechooser; - private final JFileChooser _open_filechooser; - private final JFileChooser _msa_filechooser; - private final JFileChooser _seqs_pi_filechooser; - private final JFileChooser _open_filechooser_for_species_tree; - private final JFileChooser _save_filechooser; - - private final JFileChooser _writetographics_filechooser; + private static final long serialVersionUID = -799735726778865234L; + private static final boolean PREPROCESS_TREES = false; + private final JFileChooser _values_filechooser; + private final JFileChooser _sequences_filechooser; + private final JFileChooser _open_filechooser; + private final JFileChooser _msa_filechooser; + private final JFileChooser _seqs_pi_filechooser; + private final JFileChooser _open_filechooser_for_species_tree; // Application-only print menu items - - private JMenuItem _collapse_below_threshold; - private JMenuItem _collapse_below_branch_length; - - private ButtonGroup _radio_group_1; - private ButtonGroup _radio_group_2; + private JMenuItem _collapse_below_threshold; + private JMenuItem _collapse_below_branch_length; + private ButtonGroup _radio_group_1; + private ButtonGroup _radio_group_2; // Others: - double _min_not_collapse = Constants.MIN_NOT_COLLAPSE_DEFAULT; - double _min_not_collapse_bl = 0.001; + double _min_not_collapse = Constants.MIN_NOT_COLLAPSE_DEFAULT; + double _min_not_collapse_bl = 0.001; // Phylogeny Inference menu - private JMenu _inference_menu; - private JMenuItem _inference_from_msa_item; - private JMenuItem _inference_from_seqs_item; + private JMenu _inference_menu; + private JMenuItem _inference_from_msa_item; + private JMenuItem _inference_from_seqs_item; // Phylogeny Inference - private PhylogeneticInferenceOptions _phylogenetic_inference_options = null; - private Msa _msa = null; - private File _msa_file = null; - private List _seqs = null; - private File _seqs_file = null; - JMenuItem _read_values_jmi; - JMenuItem _read_seqs_jmi; + private PhylogeneticInferenceOptions _phylogenetic_inference_options = null; + private Msa _msa = null; + private File _msa_file = null; + private List _seqs = null; + private File _seqs_file = null; + JMenuItem _read_values_jmi; + JMenuItem _read_seqs_jmi; private MainFrameApplication( final Phylogeny[] phys, final Configuration config ) { _configuration = config; @@ -271,44 +284,44 @@ public final class MainFrameApplication extends MainFrame { _open_filechooser = new JFileChooser(); _open_filechooser.setCurrentDirectory( new File( "." ) ); _open_filechooser.setMultiSelectionEnabled( false ); - _open_filechooser.addChoosableFileFilter( MainFrameApplication.xmlfilter ); - _open_filechooser.addChoosableFileFilter( MainFrameApplication.nhxfilter ); - _open_filechooser.addChoosableFileFilter( MainFrameApplication.nhfilter ); - _open_filechooser.addChoosableFileFilter( MainFrameApplication.nexusfilter ); - _open_filechooser.addChoosableFileFilter( MainFrameApplication.tolfilter ); + _open_filechooser.addChoosableFileFilter( MainFrame.xmlfilter ); + _open_filechooser.addChoosableFileFilter( MainFrame.nhxfilter ); + _open_filechooser.addChoosableFileFilter( MainFrame.nhfilter ); + _open_filechooser.addChoosableFileFilter( MainFrame.nexusfilter ); + _open_filechooser.addChoosableFileFilter( MainFrame.tolfilter ); _open_filechooser.addChoosableFileFilter( _open_filechooser.getAcceptAllFileFilter() ); - _open_filechooser.setFileFilter( MainFrameApplication.defaultfilter ); + _open_filechooser.setFileFilter( MainFrame.defaultfilter ); _open_filechooser_for_species_tree = new JFileChooser(); _open_filechooser_for_species_tree.setCurrentDirectory( new File( "." ) ); _open_filechooser_for_species_tree.setMultiSelectionEnabled( false ); - _open_filechooser_for_species_tree.addChoosableFileFilter( MainFrameApplication.xmlfilter ); - _open_filechooser_for_species_tree.addChoosableFileFilter( MainFrameApplication.tolfilter ); - _open_filechooser_for_species_tree.setFileFilter( MainFrameApplication.xmlfilter ); + _open_filechooser_for_species_tree.addChoosableFileFilter( MainFrame.xmlfilter ); + _open_filechooser_for_species_tree.addChoosableFileFilter( MainFrame.tolfilter ); + _open_filechooser_for_species_tree.setFileFilter( MainFrame.xmlfilter ); _save_filechooser = new JFileChooser(); _save_filechooser.setCurrentDirectory( new File( "." ) ); _save_filechooser.setMultiSelectionEnabled( false ); - _save_filechooser.setFileFilter( MainFrameApplication.xmlfilter ); - _save_filechooser.addChoosableFileFilter( MainFrameApplication.nhfilter ); - _save_filechooser.addChoosableFileFilter( MainFrameApplication.nexusfilter ); + _save_filechooser.setFileFilter( MainFrame.xmlfilter ); + _save_filechooser.addChoosableFileFilter( MainFrame.nhfilter ); + _save_filechooser.addChoosableFileFilter( MainFrame.nexusfilter ); _save_filechooser.addChoosableFileFilter( _save_filechooser.getAcceptAllFileFilter() ); _writetopdf_filechooser = new JFileChooser(); - _writetopdf_filechooser.addChoosableFileFilter( MainFrameApplication.pdffilter ); + _writetopdf_filechooser.addChoosableFileFilter( MainFrame.pdffilter ); _writetographics_filechooser = new JFileChooser(); - _writetographics_filechooser.addChoosableFileFilter( MainFrameApplication.graphicsfilefilter ); + _writetographics_filechooser.addChoosableFileFilter( MainFrame.graphicsfilefilter ); // Msa: _msa_filechooser = new JFileChooser(); _msa_filechooser.setName( "Read Multiple Sequence Alignment File" ); _msa_filechooser.setCurrentDirectory( new File( "." ) ); _msa_filechooser.setMultiSelectionEnabled( false ); _msa_filechooser.addChoosableFileFilter( _msa_filechooser.getAcceptAllFileFilter() ); - _msa_filechooser.addChoosableFileFilter( MainFrameApplication.msafilter ); + _msa_filechooser.addChoosableFileFilter( MainFrame.msafilter ); // Seqs: _seqs_pi_filechooser = new JFileChooser(); _seqs_pi_filechooser.setName( "Read Sequences File" ); _seqs_pi_filechooser.setCurrentDirectory( new File( "." ) ); _seqs_pi_filechooser.setMultiSelectionEnabled( false ); _seqs_pi_filechooser.addChoosableFileFilter( _seqs_pi_filechooser.getAcceptAllFileFilter() ); - _seqs_pi_filechooser.addChoosableFileFilter( MainFrameApplication.seqsfilter ); + _seqs_pi_filechooser.addChoosableFileFilter( MainFrame.seqsfilter ); // Expression _values_filechooser = new JFileChooser(); _values_filechooser.setCurrentDirectory( new File( "." ) ); @@ -419,11 +432,6 @@ public final class MainFrameApplication extends MainFrame { if ( o == _open_url_item ) { readPhylogeniesFromURL(); } - else if ( o == _save_item ) { - writeToFile( _mainpanel.getCurrentPhylogeny() ); - // If subtree currently displayed, save it, instead of complete - // tree. - } else if ( o == _new_item ) { newTree(); } @@ -436,9 +444,6 @@ public final class MainFrameApplication extends MainFrame { else if ( o == _write_to_jpg_item ) { writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.JPG ); } - else if ( o == _write_to_png_item ) { - writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.PNG ); - } else if ( o == _write_to_gif_item ) { writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.GIF ); } @@ -500,27 +505,6 @@ public final class MainFrameApplication extends MainFrame { else if ( o == _extract_tax_code_from_node_names_jmi ) { extractTaxDataFromNodeNames(); } - else if ( o == _graphics_export_visible_only_cbmi ) { - updateOptions( getOptions() ); - } - else if ( o == _antialias_print_cbmi ) { - updateOptions( getOptions() ); - } - else if ( o == _print_black_and_white_cbmi ) { - updateOptions( getOptions() ); - } - else if ( o == _print_using_actual_size_cbmi ) { - updateOptions( getOptions() ); - } - else if ( o == _graphics_export_using_actual_size_cbmi ) { - updateOptions( getOptions() ); - } - else if ( o == _print_size_mi ) { - choosePrintSize(); - } - else if ( o == _choose_pdf_width_mi ) { - choosePdfWidth(); - } else if ( o == _internal_number_are_confidence_for_nh_parsing_cbmi ) { updateOptions( getOptions() ); } @@ -571,144 +555,144 @@ public final class MainFrameApplication extends MainFrame { } } - public void end() { - _mainpanel.terminate(); - _contentpane.removeAll(); - setVisible( false ); - dispose(); - } - - @Override - public MainPanel getMainPanel() { - return _mainpanel; - } - - public Msa getMsa() { - return _msa; - } - - public File getMsaFile() { - return _msa_file; - } - - public List getSeqs() { - return _seqs; - } - - public File getSeqsFile() { - return _seqs_file; - } - - public void readMsaFromFile() { - // Set an initial directory if none set yet + private void addExpressionValuesFromFile() { + if ( ( getCurrentTreePanel() == null ) || ( getCurrentTreePanel().getPhylogeny() == null ) ) { + JOptionPane.showMessageDialog( this, + "Need to load evolutionary tree first", + "Can Not Read Expression Values", + JOptionPane.WARNING_MESSAGE ); + return; + } final File my_dir = getCurrentDir(); - _msa_filechooser.setMultiSelectionEnabled( false ); - // Open file-open dialog and set current directory if ( my_dir != null ) { - _msa_filechooser.setCurrentDirectory( my_dir ); + _values_filechooser.setCurrentDirectory( my_dir ); } - final int result = _msa_filechooser.showOpenDialog( _contentpane ); - // All done: get the msa - final File file = _msa_filechooser.getSelectedFile(); - setCurrentDir( _msa_filechooser.getCurrentDirectory() ); - if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) { - setMsaFile( null ); - setMsa( null ); - Msa msa = null; + final int result = _values_filechooser.showOpenDialog( _contentpane ); + final File file = _values_filechooser.getSelectedFile(); + if ( ( file != null ) && ( file.length() > 0 ) && ( result == JFileChooser.APPROVE_OPTION ) ) { + BasicTable t = null; try { - final InputStream is = new FileInputStream( file ); - if ( FastaParser.isLikelyFasta( file ) ) { - msa = FastaParser.parseMsa( is ); + t = BasicTableParser.parse( file, '\t' ); + if ( t.getNumberOfColumns() < 2 ) { + t = BasicTableParser.parse( file, ',' ); } - else { - msa = GeneralMsaParser.parse( is ); + if ( t.getNumberOfColumns() < 2 ) { + t = BasicTableParser.parse( file, ' ' ); } } - catch ( final MsaFormatException e ) { - setArrowCursor(); - JOptionPane.showMessageDialog( this, - e.getLocalizedMessage(), - "Multiple sequence alignment format error", - JOptionPane.ERROR_MESSAGE ); - return; - } catch ( final IOException e ) { - setArrowCursor(); JOptionPane.showMessageDialog( this, - e.getLocalizedMessage(), - "Failed to read multiple sequence alignment", + e.getMessage(), + "Could Not Read Expression Value Table", JOptionPane.ERROR_MESSAGE ); return; } - catch ( final IllegalArgumentException e ) { - setArrowCursor(); + if ( t.getNumberOfColumns() < 2 ) { JOptionPane.showMessageDialog( this, - e.getLocalizedMessage(), - "Unexpected error during reading of multiple sequence alignment", + "Table contains " + t.getNumberOfColumns() + " column(s)", + "Problem with Expression Value Table", JOptionPane.ERROR_MESSAGE ); return; } - catch ( final Exception e ) { - setArrowCursor(); - e.printStackTrace(); + if ( t.getNumberOfRows() < 1 ) { JOptionPane.showMessageDialog( this, - e.getLocalizedMessage(), - "Unexpected error during reading of multiple sequence alignment", + "Table contains zero rows", + "Problem with Expression Value Table", JOptionPane.ERROR_MESSAGE ); return; } - if ( ( msa == null ) || ( msa.getNumberOfSequences() < 1 ) ) { + final Phylogeny phy = getCurrentTreePanel().getPhylogeny(); + if ( t.getNumberOfRows() != phy.getNumberOfExternalNodes() ) { JOptionPane.showMessageDialog( this, - "Multiple sequence alignment is empty", - "Illegal Multiple Sequence Alignment", - JOptionPane.ERROR_MESSAGE ); - return; + "Table contains " + t.getNumberOfRows() + " rows, but tree contains " + + phy.getNumberOfExternalNodes() + " external nodes", + "Warning", + JOptionPane.WARNING_MESSAGE ); } - if ( msa.getNumberOfSequences() < 4 ) { - JOptionPane.showMessageDialog( this, - "Multiple sequence alignment needs to contain at least 3 sequences", - "Illegal multiple sequence alignment", - JOptionPane.ERROR_MESSAGE ); - return; + final DescriptiveStatistics stats = new BasicDescriptiveStatistics(); + int not_found = 0; + for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) { + final PhylogenyNode node = iter.next(); + final String node_name = node.getName(); + if ( !ForesterUtil.isEmpty( node_name ) ) { + int row = -1; + try { + row = t.findRow( node_name ); + } + catch ( final IllegalArgumentException e ) { + JOptionPane + .showMessageDialog( this, + e.getMessage(), + "Error Mapping Node Identifiers to Expression Value Identifiers", + JOptionPane.ERROR_MESSAGE ); + return; + } + if ( row < 0 ) { + if ( node.isExternal() ) { + not_found++; + } + continue; + } + final List l = new ArrayList(); + for( int col = 1; col < t.getNumberOfColumns(); ++col ) { + double d = -100; + try { + d = Double.parseDouble( t.getValueAsString( col, row ) ); + } + catch ( final NumberFormatException e ) { + JOptionPane.showMessageDialog( this, + "Could not parse \"" + t.getValueAsString( col, row ) + + "\" into a decimal value", + "Issue with Expression Value Table", + JOptionPane.ERROR_MESSAGE ); + return; + } + stats.addValue( d ); + l.add( d ); + } + if ( !l.isEmpty() ) { + if ( node.getNodeData().getProperties() != null ) { + node.getNodeData().getProperties() + .removePropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ); + } + node.getNodeData().setVector( l ); + } + } } - if ( msa.getLength() < 2 ) { - JOptionPane.showMessageDialog( this, - "Multiple sequence alignment needs to contain at least 2 residues", - "Illegal multiple sequence alignment", - JOptionPane.ERROR_MESSAGE ); - return; + if ( not_found > 0 ) { + JOptionPane.showMessageDialog( this, "Could not fine expression values for " + not_found + + " external node(s)", "Warning", JOptionPane.WARNING_MESSAGE ); } - System.gc(); - setMsaFile( _msa_filechooser.getSelectedFile() ); - setMsa( msa ); + getCurrentTreePanel().setStatisticsForExpressionValues( stats ); } } - public void readSeqsFromFileforPI() { - // Set an initial directory if none set yet + private void addSequencesFromFile() { + if ( ( getCurrentTreePanel() == null ) || ( getCurrentTreePanel().getPhylogeny() == null ) ) { + JOptionPane.showMessageDialog( this, + "Need to load evolutionary tree first", + "Can Not Read Sequences", + JOptionPane.WARNING_MESSAGE ); + return; + } final File my_dir = getCurrentDir(); - _seqs_pi_filechooser.setMultiSelectionEnabled( false ); - // Open file-open dialog and set current directory if ( my_dir != null ) { - _seqs_pi_filechooser.setCurrentDirectory( my_dir ); + _sequences_filechooser.setCurrentDirectory( my_dir ); } - final int result = _seqs_pi_filechooser.showOpenDialog( _contentpane ); - // All done: get the seqs - final File file = _seqs_pi_filechooser.getSelectedFile(); - setCurrentDir( _seqs_pi_filechooser.getCurrentDirectory() ); + final int result = _sequences_filechooser.showOpenDialog( _contentpane ); + final File file = _sequences_filechooser.getSelectedFile(); + List seqs = null; if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) { - setSeqsFile( null ); - setSeqs( null ); - List seqs = null; try { if ( FastaParser.isLikelyFasta( new FileInputStream( file ) ) ) { seqs = FastaParser.parse( new FileInputStream( file ) ); - for( final MolecularSequence seq : seqs ) { - System.out.println( SequenceWriter.toFasta( seq, 60 ) ); - } } else { - //TODO error + JOptionPane.showMessageDialog( this, + "Format does not appear to be Fasta", + "Multiple sequence file format error", + JOptionPane.ERROR_MESSAGE ); + return; } } catch ( final MsaFormatException e ) { @@ -727,14 +711,6 @@ public final class MainFrameApplication extends MainFrame { JOptionPane.ERROR_MESSAGE ); return; } - catch ( final IllegalArgumentException e ) { - setArrowCursor(); - JOptionPane.showMessageDialog( this, - e.getLocalizedMessage(), - "Unexpected error during reading of multiple sequence file", - JOptionPane.ERROR_MESSAGE ); - return; - } catch ( final Exception e ) { setArrowCursor(); e.printStackTrace(); @@ -747,27 +723,106 @@ public final class MainFrameApplication extends MainFrame { if ( ( seqs == null ) || ( seqs.size() < 1 ) ) { JOptionPane.showMessageDialog( this, "Multiple sequence file is empty", - "Illegal multiple sequence file", + "Empty multiple sequence file", JOptionPane.ERROR_MESSAGE ); + setArrowCursor(); return; } - if ( seqs.size() < 4 ) { - JOptionPane.showMessageDialog( this, - "Multiple sequence file needs to contain at least 3 sequences", - "Illegal multiple sequence file", - JOptionPane.ERROR_MESSAGE ); - return; + } + if ( seqs != null ) { + for( final MolecularSequence seq : seqs ) { + System.out.println( seq.getIdentifier() ); + } + final Phylogeny phy = getCurrentTreePanel().getPhylogeny(); + int total_counter = 0; + int attached_counter = 0; + for( final MolecularSequence seq : seqs ) { + ++total_counter; + final String seq_name = seq.getIdentifier(); + if ( !ForesterUtil.isEmpty( seq_name ) ) { + List nodes = phy.getNodesViaSequenceName( seq_name ); + if ( nodes.isEmpty() ) { + nodes = phy.getNodesViaSequenceSymbol( seq_name ); + } + if ( nodes.isEmpty() ) { + nodes = phy.getNodesViaGeneName( seq_name ); + } + if ( nodes.isEmpty() ) { + nodes = phy.getNodes( seq_name ); + } + if ( nodes.size() > 1 ) { + JOptionPane.showMessageDialog( this, + "Sequence name \"" + seq_name + "\" is not unique", + "Sequence name not unique", + JOptionPane.ERROR_MESSAGE ); + setArrowCursor(); + return; + } + final String[] a = seq_name.split( "\\s" ); + if ( nodes.isEmpty() && ( a.length > 1 ) ) { + final String seq_name_split = a[ 0 ]; + nodes = phy.getNodesViaSequenceName( seq_name_split ); + if ( nodes.isEmpty() ) { + nodes = phy.getNodesViaSequenceSymbol( seq_name_split ); + } + if ( nodes.isEmpty() ) { + nodes = phy.getNodes( seq_name_split ); + } + if ( nodes.size() > 1 ) { + JOptionPane.showMessageDialog( this, "Split sequence name \"" + seq_name_split + + "\" is not unique", "Sequence name not unique", JOptionPane.ERROR_MESSAGE ); + setArrowCursor(); + return; + } + } + if ( nodes.size() == 1 ) { + ++attached_counter; + final PhylogenyNode n = nodes.get( 0 ); + if ( !n.getNodeData().isHasSequence() ) { + n.getNodeData().addSequence( new org.forester.phylogeny.data.Sequence() ); + } + n.getNodeData().getSequence().setMolecularSequence( seq.getMolecularSequenceAsString() ); + if ( ForesterUtil.isEmpty( n.getNodeData().getSequence().getName() ) ) { + n.getNodeData().getSequence().setName( seq_name ); + } + } + } + } + if ( attached_counter > 0 ) { + int ext_nodes = 0; + int ext_nodes_with_seq = 0; + for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) { + ++ext_nodes; + final PhylogenyNode n = iter.next(); + if ( n.getNodeData().isHasSequence() + && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getMolecularSequence() ) ) { + ++ext_nodes_with_seq; + } + } + final String s; + if ( ext_nodes == ext_nodes_with_seq ) { + s = "All " + ext_nodes_with_seq + " external nodes now have a molecular sequence attached to them."; + } + else { + s = ext_nodes_with_seq + " out of " + ext_nodes + + " external nodes now have a molecular sequence attached to them."; + } + if ( ( attached_counter == total_counter ) && ( ext_nodes == ext_nodes_with_seq ) ) { + JOptionPane.showMessageDialog( this, + "Attached all " + total_counter + " sequences to tree nodes.\n" + s, + "All sequences attached", + JOptionPane.INFORMATION_MESSAGE ); + } + else { + JOptionPane.showMessageDialog( this, "Attached " + attached_counter + + " sequences out of a total of " + total_counter + " sequences.\n" + s, attached_counter + + " sequences attached", JOptionPane.WARNING_MESSAGE ); + } + } + else { + JOptionPane.showMessageDialog( this, "No maching tree node for any of the " + total_counter + + " sequences", "Could not attach any sequences", JOptionPane.ERROR_MESSAGE ); } - // if ( msa.getLength() < 2 ) { - // JOptionPane.showMessageDialog( this, - // "Multiple sequence alignment needs to contain at least 2 residues", - // "Illegal multiple sequence file", - // JOptionPane.ERROR_MESSAGE ); - // return; - // } - System.gc(); - setSeqsFile( _seqs_pi_filechooser.getSelectedFile() ); - setSeqs( seqs ); } } @@ -868,11 +923,10 @@ public final class MainFrameApplication extends MainFrame { .setTextMinSupportMenuItem( _choose_minimal_confidence_mi, getOptions(), getCurrentTreePanel() ); MainFrame.setTextForFontChooserMenuItem( _choose_font_mi, MainFrame .createCurrentFontDesc( getMainPanel().getTreeFontSet() ) ); - setTextForGraphicsSizeChooserMenuItem( _print_size_mi, getOptions() ); - setTextForPdfLineWidthChooserMenuItem( _choose_pdf_width_mi, getOptions() ); + MainFrame.setTextForGraphicsSizeChooserMenuItem( _print_size_mi, getOptions() ); + MainFrame.setTextForPdfLineWidthChooserMenuItem( _choose_pdf_width_mi, getOptions() ); MainFrame.setCycleNodeFillMenuItem( _cycle_node_fill_mi, getOptions() ); MainFrame.setCycleNodeShapeMenuItem( _cycle_node_shape_mi, getOptions() ); - MainFrame.setCycleDataReturnMenuItem( _cycle_data_return, getOptions() ); MainFrame.setTextNodeSizeMenuItem( _choose_node_size_mi, getOptions() ); try { @@ -992,8 +1046,7 @@ public final class MainFrameApplication extends MainFrame { customizeJMenuItem( _cycle_node_shape_mi ); customizeJMenuItem( _cycle_node_fill_mi ); customizeJMenuItem( _choose_node_size_mi ); - customizeJMenuItem( _cycle_data_return); - + customizeJMenuItem( _cycle_data_return ); customizeCheckBoxMenuItem( _color_labels_same_as_parent_branch, getOptions().isColorLabelsSameAsParentBranch() ); customizeCheckBoxMenuItem( _color_by_taxonomic_group_cbmi, getOptions().isColorByTaxonomicGroup() ); customizeCheckBoxMenuItem( _screen_antialias_cbmi, getOptions().isAntialiasScreen() ); @@ -1164,527 +1217,152 @@ public final class MainFrameApplication extends MainFrame { exit(); } - void executeLineageInference() { - if ( ( _mainpanel.getCurrentPhylogeny() == null ) || ( _mainpanel.getCurrentPhylogeny().isEmpty() ) ) { - return; - } - if ( !_mainpanel.getCurrentPhylogeny().isRooted() ) { - JOptionPane.showMessageDialog( this, - "Phylogeny is not rooted.", - "Cannot infer ancestral taxonomies", - JOptionPane.ERROR_MESSAGE ); - return; + private void closeCurrentPane() { + if ( getMainPanel().getCurrentTreePanel() != null ) { + if ( getMainPanel().getCurrentTreePanel().isEdited() ) { + final int r = JOptionPane.showConfirmDialog( this, + "Close tab despite potentially unsaved changes?", + "Close Tab?", + JOptionPane.YES_NO_OPTION ); + if ( r != JOptionPane.YES_OPTION ) { + return; + } + } + getMainPanel().closeCurrentPane(); + activateSaveAllIfNeeded(); } - final AncestralTaxonomyInferrer inferrer = new AncestralTaxonomyInferrer( this, - _mainpanel.getCurrentTreePanel(), - _mainpanel.getCurrentPhylogeny() - .copy() ); - new Thread( inferrer ).start(); - } - - void exit() { - removeAllTextFrames(); - _mainpanel.terminate(); - _contentpane.removeAll(); - setVisible( false ); - dispose(); - // System.exit( 0 ); //TODO reconfirm that this is OK, then remove. } - void readPhylogeniesFromURL() { - URL url = null; - Phylogeny[] phys = null; - final String message = "Please enter a complete URL, for example \"http://purl.org/phylo/treebase/phylows/study/TB2:S15480?format=nexus\""; - final String url_string = JOptionPane.showInputDialog( this, - message, - "Use URL/webservice to obtain a phylogeny", - JOptionPane.QUESTION_MESSAGE ); - boolean nhx_or_nexus = false; - if ( ( url_string != null ) && ( url_string.length() > 0 ) ) { - try { - url = new URL( url_string ); - PhylogenyParser parser = null; - if ( url.getHost().toLowerCase().indexOf( "tolweb" ) >= 0 ) { - parser = new TolParser(); - } - else { - parser = ParserUtils.createParserDependingOnUrlContents( url, getConfiguration() - .isValidatePhyloXmlAgainstSchema() ); - } - if ( parser instanceof NexusPhylogeniesParser ) { - nhx_or_nexus = true; - } - else if ( parser instanceof NHXParser ) { - nhx_or_nexus = true; - } - if ( _mainpanel.getCurrentTreePanel() != null ) { - _mainpanel.getCurrentTreePanel().setWaitCursor(); - } - else { - _mainpanel.setWaitCursor(); + private void collapse( final Phylogeny phy ) { + final PhylogenyNodeIterator it = phy.iteratorPostorder(); + final List to_be_removed = new ArrayList(); + double min_support = Double.MAX_VALUE; + boolean conf_present = false; + while ( it.hasNext() ) { + final PhylogenyNode n = it.next(); + if ( !n.isExternal() && !n.isRoot() ) { + final List c = n.getBranchData().getConfidences(); + if ( ( c != null ) && ( c.size() > 0 ) ) { + conf_present = true; + double max = 0; + for( final Confidence confidence : c ) { + if ( confidence.getValue() > max ) { + max = confidence.getValue(); + } + } + if ( max < getMinNotCollapseConfidenceValue() ) { + to_be_removed.add( n ); + } + if ( max < min_support ) { + min_support = max; + } } - final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); - phys = factory.create( url.openStream(), parser ); } - catch ( final MalformedURLException e ) { - JOptionPane.showMessageDialog( this, - "Malformed URL: " + url + "\n" + e.getLocalizedMessage(), - "Malformed URL", - JOptionPane.ERROR_MESSAGE ); + } + if ( conf_present ) { + for( final PhylogenyNode node : to_be_removed ) { + PhylogenyMethods.removeNode( node, phy ); } - catch ( final IOException e ) { - JOptionPane.showMessageDialog( this, - "Could not read from " + url + "\n" - + ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ), - "Failed to read URL", - JOptionPane.ERROR_MESSAGE ); + if ( to_be_removed.size() > 0 ) { + phy.externalNodesHaveChanged(); + phy.clearHashIdToNodeMap(); + phy.recalculateNumberOfExternalDescendants( true ); + getCurrentTreePanel().resetNodeIdToDistToLeafMap(); + getCurrentTreePanel().updateSetOfCollapsedExternalNodes(); + getCurrentTreePanel().calculateLongestExtNodeInfo(); + getCurrentTreePanel().setNodeInPreorderToNull(); + getCurrentTreePanel().recalculateMaxDistanceToRoot(); + getCurrentTreePanel().resetPreferredSize(); + getCurrentTreePanel().setEdited( true ); + getCurrentTreePanel().repaint(); + repaint(); } - catch ( final Exception e ) { - JOptionPane.showMessageDialog( this, - ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ), - "Unexpected Exception", - JOptionPane.ERROR_MESSAGE ); + if ( to_be_removed.size() > 0 ) { + JOptionPane.showMessageDialog( this, "Collapsed " + to_be_removed.size() + + " branches with\nconfidence values below " + getMinNotCollapseConfidenceValue(), "Collapsed " + + to_be_removed.size() + " branches", JOptionPane.INFORMATION_MESSAGE ); } - finally { - if ( _mainpanel.getCurrentTreePanel() != null ) { - _mainpanel.getCurrentTreePanel().setArrowCursor(); - } - else { - _mainpanel.setArrowCursor(); - } - } - if ( ( phys != null ) && ( phys.length > 0 ) ) { - if ( nhx_or_nexus && getOptions().isInternalNumberAreConfidenceForNhParsing() ) { - for( final Phylogeny phy : phys ) { - PhylogenyMethods.transferInternalNodeNamesToConfidence( phy, "" ); - } - } - AptxUtil.addPhylogeniesToTabs( phys, - new File( url.getFile() ).getName(), - new File( url.getFile() ).toString(), - getConfiguration(), - getMainPanel() ); - _mainpanel.getControlPanel().showWhole(); + else { + JOptionPane.showMessageDialog( this, "No branch collapsed,\nminimum confidence value per branch is " + + min_support, "No branch collapsed", JOptionPane.INFORMATION_MESSAGE ); } } - activateSaveAllIfNeeded(); - System.gc(); - } - - void setMsa( final Msa msa ) { - _msa = msa; - } - - void setMsaFile( final File msa_file ) { - _msa_file = msa_file; - } - - void setSeqs( final List seqs ) { - _seqs = seqs; - } - - void setSeqsFile( final File seqs_file ) { - _seqs_file = seqs_file; - } - - void writePhylogenyToGraphicsFile( final String file_name, final GraphicsExportType type ) { - _mainpanel.getCurrentTreePanel().calcParametersForPainting( _mainpanel.getCurrentTreePanel().getWidth(), - _mainpanel.getCurrentTreePanel().getHeight() ); - String file_written_to = ""; - boolean error = false; - try { - 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; - JOptionPane.showMessageDialog( this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE ); - } - if ( !error ) { - if ( ( file_written_to != null ) && ( file_written_to.length() > 0 ) ) { - JOptionPane.showMessageDialog( this, - "Wrote image to: " + file_written_to, - "Graphics Export", - JOptionPane.INFORMATION_MESSAGE ); - } - else { - JOptionPane.showMessageDialog( this, - "There was an unknown problem when attempting to write to an image file: \"" - + file_name + "\"", - "Error", - JOptionPane.ERROR_MESSAGE ); - } - } - _contentpane.repaint(); - } - - private void addExpressionValuesFromFile() { - if ( ( getCurrentTreePanel() == null ) || ( getCurrentTreePanel().getPhylogeny() == null ) ) { + else { JOptionPane.showMessageDialog( this, - "Need to load evolutionary tree first", - "Can Not Read Expression Values", - JOptionPane.WARNING_MESSAGE ); - return; - } - final File my_dir = getCurrentDir(); - if ( my_dir != null ) { - _values_filechooser.setCurrentDirectory( my_dir ); - } - final int result = _values_filechooser.showOpenDialog( _contentpane ); - final File file = _values_filechooser.getSelectedFile(); - if ( ( file != null ) && ( file.length() > 0 ) && ( result == JFileChooser.APPROVE_OPTION ) ) { - BasicTable t = null; - try { - t = BasicTableParser.parse( file, '\t' ); - if ( t.getNumberOfColumns() < 2 ) { - t = BasicTableParser.parse( file, ',' ); - } - if ( t.getNumberOfColumns() < 2 ) { - t = BasicTableParser.parse( file, ' ' ); - } - } - catch ( final IOException e ) { - JOptionPane.showMessageDialog( this, - e.getMessage(), - "Could Not Read Expression Value Table", - JOptionPane.ERROR_MESSAGE ); - return; - } - if ( t.getNumberOfColumns() < 2 ) { - JOptionPane.showMessageDialog( this, - "Table contains " + t.getNumberOfColumns() + " column(s)", - "Problem with Expression Value Table", - JOptionPane.ERROR_MESSAGE ); - return; - } - if ( t.getNumberOfRows() < 1 ) { - JOptionPane.showMessageDialog( this, - "Table contains zero rows", - "Problem with Expression Value Table", - JOptionPane.ERROR_MESSAGE ); - return; - } - final Phylogeny phy = getCurrentTreePanel().getPhylogeny(); - if ( t.getNumberOfRows() != phy.getNumberOfExternalNodes() ) { - JOptionPane.showMessageDialog( this, - "Table contains " + t.getNumberOfRows() + " rows, but tree contains " - + phy.getNumberOfExternalNodes() + " external nodes", - "Warning", - JOptionPane.WARNING_MESSAGE ); - } - final DescriptiveStatistics stats = new BasicDescriptiveStatistics(); - int not_found = 0; - for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) { - final PhylogenyNode node = iter.next(); - final String node_name = node.getName(); - if ( !ForesterUtil.isEmpty( node_name ) ) { - int row = -1; - try { - row = t.findRow( node_name ); - } - catch ( final IllegalArgumentException e ) { - JOptionPane - .showMessageDialog( this, - e.getMessage(), - "Error Mapping Node Identifiers to Expression Value Identifiers", - JOptionPane.ERROR_MESSAGE ); - return; - } - if ( row < 0 ) { - if ( node.isExternal() ) { - not_found++; - } - continue; - } - final List l = new ArrayList(); - for( int col = 1; col < t.getNumberOfColumns(); ++col ) { - double d = -100; - try { - d = Double.parseDouble( t.getValueAsString( col, row ) ); - } - catch ( final NumberFormatException e ) { - JOptionPane.showMessageDialog( this, - "Could not parse \"" + t.getValueAsString( col, row ) - + "\" into a decimal value", - "Issue with Expression Value Table", - JOptionPane.ERROR_MESSAGE ); - return; - } - stats.addValue( d ); - l.add( d ); - } - if ( !l.isEmpty() ) { - if ( node.getNodeData().getProperties() != null ) { - node.getNodeData().getProperties() - .removePropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ); - } - node.getNodeData().setVector( l ); - } - } - } - if ( not_found > 0 ) { - JOptionPane.showMessageDialog( this, "Could not fine expression values for " + not_found - + " external node(s)", "Warning", JOptionPane.WARNING_MESSAGE ); - } - getCurrentTreePanel().setStatisticsForExpressionValues( stats ); + "No branch collapsed because no confidence values present", + "No confidence values present", + JOptionPane.INFORMATION_MESSAGE ); } } - private void addSequencesFromFile() { - if ( ( getCurrentTreePanel() == null ) || ( getCurrentTreePanel().getPhylogeny() == null ) ) { - JOptionPane.showMessageDialog( this, - "Need to load evolutionary tree first", - "Can Not Read Sequences", - JOptionPane.WARNING_MESSAGE ); - return; - } - final File my_dir = getCurrentDir(); - if ( my_dir != null ) { - _sequences_filechooser.setCurrentDirectory( my_dir ); - } - final int result = _sequences_filechooser.showOpenDialog( _contentpane ); - final File file = _sequences_filechooser.getSelectedFile(); - List seqs = null; - if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) { - try { - if ( FastaParser.isLikelyFasta( new FileInputStream( file ) ) ) { - seqs = FastaParser.parse( new FileInputStream( file ) ); - } - else { - JOptionPane.showMessageDialog( this, - "Format does not appear to be Fasta", - "Multiple sequence file format error", - JOptionPane.ERROR_MESSAGE ); - return; - } - } - catch ( final MsaFormatException e ) { - setArrowCursor(); - JOptionPane.showMessageDialog( this, - e.getLocalizedMessage(), - "Multiple sequence file format error", - JOptionPane.ERROR_MESSAGE ); - return; - } - catch ( final IOException e ) { - setArrowCursor(); - JOptionPane.showMessageDialog( this, - e.getLocalizedMessage(), - "Failed to read multiple sequence file", - JOptionPane.ERROR_MESSAGE ); - return; - } - catch ( final Exception e ) { - setArrowCursor(); - e.printStackTrace(); - JOptionPane.showMessageDialog( this, - e.getLocalizedMessage(), - "Unexpected error during reading of multiple sequence file", - JOptionPane.ERROR_MESSAGE ); - return; - } - if ( ( seqs == null ) || ( seqs.size() < 1 ) ) { - JOptionPane.showMessageDialog( this, - "Multiple sequence file is empty", - "Empty multiple sequence file", - JOptionPane.ERROR_MESSAGE ); - setArrowCursor(); - return; - } - } - if ( seqs != null ) { - for( final MolecularSequence seq : seqs ) { - System.out.println( seq.getIdentifier() ); - } + private void collapseBelowBranchLengthThreshold() { + if ( getCurrentTreePanel() != null ) { final Phylogeny phy = getCurrentTreePanel().getPhylogeny(); - int total_counter = 0; - int attached_counter = 0; - for( final MolecularSequence seq : seqs ) { - ++total_counter; - final String seq_name = seq.getIdentifier(); - if ( !ForesterUtil.isEmpty( seq_name ) ) { - List nodes = phy.getNodesViaSequenceName( seq_name ); - if ( nodes.isEmpty() ) { - nodes = phy.getNodesViaSequenceSymbol( seq_name ); - } - if ( nodes.isEmpty() ) { - nodes = phy.getNodesViaGeneName( seq_name ); - } - if ( nodes.isEmpty() ) { - nodes = phy.getNodes( seq_name ); - } - if ( nodes.size() > 1 ) { - JOptionPane.showMessageDialog( this, - "Sequence name \"" + seq_name + "\" is not unique", - "Sequence name not unique", - JOptionPane.ERROR_MESSAGE ); - setArrowCursor(); - return; - } - final String[] a = seq_name.split( "\\s" ); - if ( nodes.isEmpty() && ( a.length > 1 ) ) { - final String seq_name_split = a[ 0 ]; - nodes = phy.getNodesViaSequenceName( seq_name_split ); - if ( nodes.isEmpty() ) { - nodes = phy.getNodesViaSequenceSymbol( seq_name_split ); - } - if ( nodes.isEmpty() ) { - nodes = phy.getNodes( seq_name_split ); - } - if ( nodes.size() > 1 ) { - JOptionPane.showMessageDialog( this, "Split sequence name \"" + seq_name_split - + "\" is not unique", "Sequence name not unique", JOptionPane.ERROR_MESSAGE ); - setArrowCursor(); - return; - } - } - if ( nodes.size() == 1 ) { - ++attached_counter; - final PhylogenyNode n = nodes.get( 0 ); - if ( !n.getNodeData().isHasSequence() ) { - n.getNodeData().addSequence( new org.forester.phylogeny.data.Sequence() ); - } - n.getNodeData().getSequence().setMolecularSequence( seq.getMolecularSequenceAsString() ); - if ( ForesterUtil.isEmpty( n.getNodeData().getSequence().getName() ) ) { - n.getNodeData().getSequence().setName( seq_name ); - } - } - } - } - if ( attached_counter > 0 ) { - int ext_nodes = 0; - int ext_nodes_with_seq = 0; - for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) { - ++ext_nodes; - final PhylogenyNode n = iter.next(); - if ( n.getNodeData().isHasSequence() - && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getMolecularSequence() ) ) { - ++ext_nodes_with_seq; - } - } - final String s; - if ( ext_nodes == ext_nodes_with_seq ) { - s = "All " + ext_nodes_with_seq + " external nodes now have a molecular sequence attached to them."; - } - else { - s = ext_nodes_with_seq + " out of " + ext_nodes - + " external nodes now have a molecular sequence attached to them."; - } - if ( ( attached_counter == total_counter ) && ( ext_nodes == ext_nodes_with_seq ) ) { - JOptionPane.showMessageDialog( this, - "Attached all " + total_counter + " sequences to tree nodes.\n" + s, - "All sequences attached", - JOptionPane.INFORMATION_MESSAGE ); - } - else { - JOptionPane.showMessageDialog( this, "Attached " + attached_counter - + " sequences out of a total of " + total_counter + " sequences.\n" + s, attached_counter - + " sequences attached", JOptionPane.WARNING_MESSAGE ); - } - } - else { - JOptionPane.showMessageDialog( this, "No maching tree node for any of the " + total_counter - + " sequences", "Could not attach any sequences", JOptionPane.ERROR_MESSAGE ); - } - } - } - - private void choosePdfWidth() { - final String s = ( String ) JOptionPane.showInputDialog( this, - "Please enter the default line width for PDF export.\n" - + "[current value: " - + getOptions().getPrintLineWidth() + "]\n", - "Line Width for PDF Export", - JOptionPane.QUESTION_MESSAGE, - null, - null, - getOptions().getPrintLineWidth() ); - if ( !ForesterUtil.isEmpty( s ) ) { - boolean success = true; - float f = 0.0f; - final String m_str = s.trim(); - if ( !ForesterUtil.isEmpty( m_str ) ) { - try { - f = Float.parseFloat( m_str ); - } - catch ( final Exception ex ) { - success = false; - } - } - else { - success = false; - } - if ( success && ( f > 0.0 ) ) { - getOptions().setPrintLineWidth( f ); - } - } - } - - private void choosePrintSize() { - final String s = ( String ) JOptionPane.showInputDialog( this, - "Please enter values for width and height,\nseparated by a comma.\n" - + "[current values: " - + getOptions().getPrintSizeX() + ", " - + getOptions().getPrintSizeY() + "]\n" - + "[A4: " + Constants.A4_SIZE_X + ", " - + Constants.A4_SIZE_Y + "]\n" + "[US Letter: " - + Constants.US_LETTER_SIZE_X + ", " - + Constants.US_LETTER_SIZE_Y + "]", - "Default Size for Graphics Export", - JOptionPane.QUESTION_MESSAGE, - null, - null, - getOptions().getPrintSizeX() + ", " - + getOptions().getPrintSizeY() ); - if ( !ForesterUtil.isEmpty( s ) && ( s.indexOf( ',' ) > 0 ) ) { - boolean success = true; - int x = 0; - int y = 0; - final String[] str_ary = s.split( "," ); - if ( str_ary.length == 2 ) { - final String x_str = str_ary[ 0 ].trim(); - final String y_str = str_ary[ 1 ].trim(); - if ( !ForesterUtil.isEmpty( x_str ) && !ForesterUtil.isEmpty( y_str ) ) { - try { - x = Integer.parseInt( x_str ); - y = Integer.parseInt( y_str ); + if ( ( phy != null ) && !phy.isEmpty() ) { + final String s = ( String ) JOptionPane + .showInputDialog( this, + "Please enter the minimum branch length value\n", + "Minimal Branch Length Value", + JOptionPane.QUESTION_MESSAGE, + null, + null, + getMinNotCollapseBlValue() ); + if ( !ForesterUtil.isEmpty( s ) ) { + boolean success = true; + double m = 0.0; + final String m_str = s.trim(); + if ( !ForesterUtil.isEmpty( m_str ) ) { + try { + m = Double.parseDouble( m_str ); + } + catch ( final Exception ex ) { + success = false; + } } - catch ( final Exception ex ) { + else { success = false; } + if ( success && ( m >= 0.0 ) ) { + setMinNotCollapseBlValue( m ); + collapseBl( phy ); + } } - else { - success = false; - } - } - else { - success = false; - } - if ( success && ( x > 1 ) && ( y > 1 ) ) { - getOptions().setPrintSizeX( x ); - getOptions().setPrintSizeY( y ); } } } - private void closeCurrentPane() { - if ( getMainPanel().getCurrentTreePanel() != null ) { - if ( getMainPanel().getCurrentTreePanel().isEdited() ) { - final int r = JOptionPane.showConfirmDialog( this, - "Close tab despite potentially unsaved changes?", - "Close Tab?", - JOptionPane.YES_NO_OPTION ); - if ( r != JOptionPane.YES_OPTION ) { - return; + private void collapseBelowThreshold() { + if ( getCurrentTreePanel() != null ) { + final Phylogeny phy = getCurrentTreePanel().getPhylogeny(); + if ( ( phy != null ) && !phy.isEmpty() ) { + final String s = ( String ) JOptionPane.showInputDialog( this, + "Please enter the minimum confidence value\n", + "Minimal Confidence Value", + JOptionPane.QUESTION_MESSAGE, + null, + null, + getMinNotCollapseConfidenceValue() ); + if ( !ForesterUtil.isEmpty( s ) ) { + boolean success = true; + double m = 0.0; + final String m_str = s.trim(); + if ( !ForesterUtil.isEmpty( m_str ) ) { + try { + m = Double.parseDouble( m_str ); + } + catch ( final Exception ex ) { + success = false; + } + } + else { + success = false; + } + if ( success && ( m >= 0.0 ) ) { + setMinNotCollapseConfidenceValue( m ); + collapse( phy ); + } } } - getMainPanel().closeCurrentPane(); - activateSaveAllIfNeeded(); } } @@ -1746,139 +1424,6 @@ public final class MainFrameApplication extends MainFrame { } } - private void collapse( final Phylogeny phy ) { - final PhylogenyNodeIterator it = phy.iteratorPostorder(); - final List to_be_removed = new ArrayList(); - double min_support = Double.MAX_VALUE; - boolean conf_present = false; - while ( it.hasNext() ) { - final PhylogenyNode n = it.next(); - if ( !n.isExternal() && !n.isRoot() ) { - final List c = n.getBranchData().getConfidences(); - if ( ( c != null ) && ( c.size() > 0 ) ) { - conf_present = true; - double max = 0; - for( final Confidence confidence : c ) { - if ( confidence.getValue() > max ) { - max = confidence.getValue(); - } - } - if ( max < getMinNotCollapseConfidenceValue() ) { - to_be_removed.add( n ); - } - if ( max < min_support ) { - min_support = max; - } - } - } - } - if ( conf_present ) { - for( final PhylogenyNode node : to_be_removed ) { - PhylogenyMethods.removeNode( node, phy ); - } - if ( to_be_removed.size() > 0 ) { - phy.externalNodesHaveChanged(); - phy.clearHashIdToNodeMap(); - 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() - + " branches with\nconfidence values below " + getMinNotCollapseConfidenceValue(), "Collapsed " - + to_be_removed.size() + " branches", JOptionPane.INFORMATION_MESSAGE ); - } - else { - JOptionPane.showMessageDialog( this, "No branch collapsed,\nminimum confidence value per branch is " - + min_support, "No branch collapsed", JOptionPane.INFORMATION_MESSAGE ); - } - } - else { - JOptionPane.showMessageDialog( this, - "No branch collapsed because no confidence values present", - "No confidence values present", - JOptionPane.INFORMATION_MESSAGE ); - } - } - - private void collapseBelowThreshold() { - if ( getCurrentTreePanel() != null ) { - final Phylogeny phy = getCurrentTreePanel().getPhylogeny(); - if ( ( phy != null ) && !phy.isEmpty() ) { - final String s = ( String ) JOptionPane.showInputDialog( this, - "Please enter the minimum confidence value\n", - "Minimal Confidence Value", - JOptionPane.QUESTION_MESSAGE, - null, - null, - getMinNotCollapseConfidenceValue() ); - if ( !ForesterUtil.isEmpty( s ) ) { - boolean success = true; - double m = 0.0; - final String m_str = s.trim(); - if ( !ForesterUtil.isEmpty( m_str ) ) { - try { - m = Double.parseDouble( m_str ); - } - catch ( final Exception ex ) { - success = false; - } - } - else { - success = false; - } - if ( success && ( m >= 0.0 ) ) { - setMinNotCollapseConfidenceValue( m ); - collapse( phy ); - } - } - } - } - } - - private void collapseBelowBranchLengthThreshold() { - if ( getCurrentTreePanel() != null ) { - final Phylogeny phy = getCurrentTreePanel().getPhylogeny(); - if ( ( phy != null ) && !phy.isEmpty() ) { - final String s = ( String ) JOptionPane - .showInputDialog( this, - "Please enter the minimum branch length value\n", - "Minimal Branch Length Value", - JOptionPane.QUESTION_MESSAGE, - null, - null, - getMinNotCollapseBlValue() ); - if ( !ForesterUtil.isEmpty( s ) ) { - boolean success = true; - double m = 0.0; - final String m_str = s.trim(); - if ( !ForesterUtil.isEmpty( m_str ) ) { - try { - m = Double.parseDouble( m_str ); - } - catch ( final Exception ex ) { - success = false; - } - } - else { - success = false; - } - if ( success && ( m >= 0.0 ) ) { - setMinNotCollapseBlValue( m ); - collapseBl( phy ); - } - } - } - } - } - private PhyloXmlParser createPhyloXmlParser() { PhyloXmlParser xml_parser = null; if ( getConfiguration().isValidatePhyloXmlAgainstSchema() ) { @@ -1898,6 +1443,31 @@ public final class MainFrameApplication extends MainFrame { return xml_parser; } + public void end() { + _mainpanel.terminate(); + _contentpane.removeAll(); + setVisible( false ); + dispose(); + } + + void executeLineageInference() { + if ( ( _mainpanel.getCurrentPhylogeny() == null ) || ( _mainpanel.getCurrentPhylogeny().isEmpty() ) ) { + return; + } + if ( !_mainpanel.getCurrentPhylogeny().isRooted() ) { + JOptionPane.showMessageDialog( this, + "Phylogeny is not rooted.", + "Cannot infer ancestral taxonomies", + JOptionPane.ERROR_MESSAGE ); + return; + } + final AncestralTaxonomyInferrer inferrer = new AncestralTaxonomyInferrer( this, + _mainpanel.getCurrentTreePanel(), + _mainpanel.getCurrentPhylogeny() + .copy() ); + new Thread( inferrer ).start(); + } + private void executePhyleneticInference( final boolean from_unaligned_seqs ) { final PhyloInferenceDialog dialog = new PhyloInferenceDialog( this, getPhylogeneticInferenceOptions(), @@ -1935,6 +1505,15 @@ public final class MainFrameApplication extends MainFrame { } } + void exit() { + removeAllTextFrames(); + _mainpanel.terminate(); + _contentpane.removeAll(); + setVisible( false ); + dispose(); + // System.exit( 0 ); //TODO reconfirm that this is OK, then remove. + } + private void extractTaxDataFromNodeNames() throws PhyloXmlDataFormatException { final StringBuilder sb = new StringBuilder(); final StringBuilder sb_failed = new StringBuilder(); @@ -1998,15 +1577,25 @@ public final class MainFrameApplication extends MainFrame { } } - - + @Override + public MainPanel getMainPanel() { + return _mainpanel; + } + + private double getMinNotCollapseBlValue() { + return _min_not_collapse_bl; + } private double getMinNotCollapseConfidenceValue() { return _min_not_collapse; } - private double getMinNotCollapseBlValue() { - return _min_not_collapse_bl; + public Msa getMsa() { + return _msa; + } + + public File getMsaFile() { + return _msa_file; } private PhylogeneticInferenceOptions getPhylogeneticInferenceOptions() { @@ -2016,6 +1605,14 @@ public final class MainFrameApplication extends MainFrame { return _phylogenetic_inference_options; } + public List getSeqs() { + return _seqs; + } + + public File getSeqsFile() { + return _seqs_file; + } + private boolean isUnsavedDataPresent() { final List tps = getMainPanel().getTreePanels(); for( final TreePanel tp : tps ) { @@ -2110,6 +1707,30 @@ public final class MainFrameApplication extends MainFrame { } } + private void preProcessTreesUponReading( final Phylogeny[] phys ) { + for( final Phylogeny phy : phys ) { + if ( ( phy != null ) && !phy.isEmpty() ) { + for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) { + final PhylogenyNode n = it.next(); + if ( n.isExternal() ) { + if ( n.getNodeData().isHasSequence() ) { + final Sequence s = n.getNodeData().getSequence(); + if ( ForesterUtil.isEmpty( s.getGeneName() ) || s.getGeneName().startsWith( "LOC" ) ) { + if ( ( s.getAccession() != null ) + && !ForesterUtil.isEmpty( s.getAccession().getValue() ) ) { + s.setGeneName( s.getAccession().getValue() ); + } + else if ( !ForesterUtil.isEmpty( n.getName() ) ) { + s.setGeneName( n.getName() ); + } + } + } + } + } + } + } + } + private void print() { if ( ( getCurrentTreePanel() == null ) || ( getCurrentTreePanel().getPhylogeny() == null ) || getCurrentTreePanel().getPhylogeny().isEmpty() ) { @@ -2143,7 +1764,90 @@ public final class MainFrameApplication extends MainFrame { } } - + public void readMsaFromFile() { + // Set an initial directory if none set yet + final File my_dir = getCurrentDir(); + _msa_filechooser.setMultiSelectionEnabled( false ); + // Open file-open dialog and set current directory + if ( my_dir != null ) { + _msa_filechooser.setCurrentDirectory( my_dir ); + } + final int result = _msa_filechooser.showOpenDialog( _contentpane ); + // All done: get the msa + final File file = _msa_filechooser.getSelectedFile(); + setCurrentDir( _msa_filechooser.getCurrentDirectory() ); + if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) { + setMsaFile( null ); + setMsa( null ); + Msa msa = null; + try { + final InputStream is = new FileInputStream( file ); + if ( FastaParser.isLikelyFasta( file ) ) { + msa = FastaParser.parseMsa( is ); + } + else { + msa = GeneralMsaParser.parse( is ); + } + } + catch ( final MsaFormatException e ) { + setArrowCursor(); + JOptionPane.showMessageDialog( this, + e.getLocalizedMessage(), + "Multiple sequence alignment format error", + JOptionPane.ERROR_MESSAGE ); + return; + } + catch ( final IOException e ) { + setArrowCursor(); + JOptionPane.showMessageDialog( this, + e.getLocalizedMessage(), + "Failed to read multiple sequence alignment", + JOptionPane.ERROR_MESSAGE ); + return; + } + catch ( final IllegalArgumentException e ) { + setArrowCursor(); + JOptionPane.showMessageDialog( this, + e.getLocalizedMessage(), + "Unexpected error during reading of multiple sequence alignment", + JOptionPane.ERROR_MESSAGE ); + return; + } + catch ( final Exception e ) { + setArrowCursor(); + e.printStackTrace(); + JOptionPane.showMessageDialog( this, + e.getLocalizedMessage(), + "Unexpected error during reading of multiple sequence alignment", + JOptionPane.ERROR_MESSAGE ); + return; + } + if ( ( msa == null ) || ( msa.getNumberOfSequences() < 1 ) ) { + JOptionPane.showMessageDialog( this, + "Multiple sequence alignment is empty", + "Illegal Multiple Sequence Alignment", + JOptionPane.ERROR_MESSAGE ); + return; + } + if ( msa.getNumberOfSequences() < 4 ) { + JOptionPane.showMessageDialog( this, + "Multiple sequence alignment needs to contain at least 3 sequences", + "Illegal multiple sequence alignment", + JOptionPane.ERROR_MESSAGE ); + return; + } + if ( msa.getLength() < 2 ) { + JOptionPane.showMessageDialog( this, + "Multiple sequence alignment needs to contain at least 2 residues", + "Illegal multiple sequence alignment", + JOptionPane.ERROR_MESSAGE ); + return; + } + System.gc(); + setMsaFile( _msa_filechooser.getSelectedFile() ); + setMsa( msa ); + } + } private void readPhylogeniesFromFile() { boolean exception = false; @@ -2169,8 +1873,8 @@ public final class MainFrameApplication extends MainFrame { else { _mainpanel.setWaitCursor(); } - if ( ( _open_filechooser.getFileFilter() == MainFrameApplication.nhfilter ) - || ( _open_filechooser.getFileFilter() == MainFrameApplication.nhxfilter ) ) { + if ( ( _open_filechooser.getFileFilter() == MainFrame.nhfilter ) + || ( _open_filechooser.getFileFilter() == MainFrame.nhxfilter ) ) { try { final NHXParser nhx = new NHXParser(); setSpecialOptionsForNhxParser( nhx ); @@ -2182,7 +1886,7 @@ public final class MainFrameApplication extends MainFrame { exceptionOccuredDuringOpenFile( e ); } } - else if ( _open_filechooser.getFileFilter() == MainFrameApplication.xmlfilter ) { + else if ( _open_filechooser.getFileFilter() == MainFrame.xmlfilter ) { warnIfNotPhyloXmlValidation( getConfiguration() ); try { final PhyloXmlParser xml_parser = createPhyloXmlParser(); @@ -2193,7 +1897,7 @@ public final class MainFrameApplication extends MainFrame { exceptionOccuredDuringOpenFile( e ); } } - else if ( _open_filechooser.getFileFilter() == MainFrameApplication.tolfilter ) { + else if ( _open_filechooser.getFileFilter() == MainFrame.tolfilter ) { try { phys = PhylogenyMethods.readPhylogenies( new TolParser(), file ); } @@ -2202,7 +1906,7 @@ public final class MainFrameApplication extends MainFrame { exceptionOccuredDuringOpenFile( e ); } } - else if ( _open_filechooser.getFileFilter() == MainFrameApplication.nexusfilter ) { + else if ( _open_filechooser.getFileFilter() == MainFrame.nexusfilter ) { try { final NexusPhylogeniesParser nex = new NexusPhylogeniesParser(); setSpecialOptionsForNexParser( nex ); @@ -2285,27 +1989,170 @@ public final class MainFrameApplication extends MainFrame { System.gc(); } - private void preProcessTreesUponReading( final Phylogeny[] phys ) { - for( final Phylogeny phy : phys ) { - if ( ( phy != null ) && !phy.isEmpty() ) { - for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) { - final PhylogenyNode n = it.next(); - if ( n.isExternal() ) { - if ( n.getNodeData().isHasSequence() ) { - final Sequence s = n.getNodeData().getSequence(); - if ( ForesterUtil.isEmpty( s.getGeneName() ) || s.getGeneName().startsWith( "LOC" ) ) { - if ( ( s.getAccession() != null ) - && !ForesterUtil.isEmpty( s.getAccession().getValue() ) ) { - s.setGeneName( s.getAccession().getValue() ); - } - else if ( !ForesterUtil.isEmpty( n.getName() ) ) { - s.setGeneName( n.getName() ); - } - } - } + void readPhylogeniesFromURL() { + URL url = null; + Phylogeny[] phys = null; + final String message = "Please enter a complete URL, for example \"http://purl.org/phylo/treebase/phylows/study/TB2:S15480?format=nexus\""; + final String url_string = JOptionPane.showInputDialog( this, + message, + "Use URL/webservice to obtain a phylogeny", + JOptionPane.QUESTION_MESSAGE ); + boolean nhx_or_nexus = false; + if ( ( url_string != null ) && ( url_string.length() > 0 ) ) { + try { + url = new URL( url_string ); + PhylogenyParser parser = null; + if ( url.getHost().toLowerCase().indexOf( "tolweb" ) >= 0 ) { + parser = new TolParser(); + } + else { + parser = ParserUtils.createParserDependingOnUrlContents( url, getConfiguration() + .isValidatePhyloXmlAgainstSchema() ); + } + if ( parser instanceof NexusPhylogeniesParser ) { + nhx_or_nexus = true; + } + else if ( parser instanceof NHXParser ) { + nhx_or_nexus = true; + } + if ( _mainpanel.getCurrentTreePanel() != null ) { + _mainpanel.getCurrentTreePanel().setWaitCursor(); + } + else { + _mainpanel.setWaitCursor(); + } + final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); + phys = factory.create( url.openStream(), parser ); + } + catch ( final MalformedURLException e ) { + JOptionPane.showMessageDialog( this, + "Malformed URL: " + url + "\n" + e.getLocalizedMessage(), + "Malformed URL", + JOptionPane.ERROR_MESSAGE ); + } + catch ( final IOException e ) { + JOptionPane.showMessageDialog( this, + "Could not read from " + url + "\n" + + ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ), + "Failed to read URL", + JOptionPane.ERROR_MESSAGE ); + } + catch ( final Exception e ) { + JOptionPane.showMessageDialog( this, + ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ), + "Unexpected Exception", + JOptionPane.ERROR_MESSAGE ); + } + finally { + if ( _mainpanel.getCurrentTreePanel() != null ) { + _mainpanel.getCurrentTreePanel().setArrowCursor(); + } + else { + _mainpanel.setArrowCursor(); + } + } + if ( ( phys != null ) && ( phys.length > 0 ) ) { + if ( nhx_or_nexus && getOptions().isInternalNumberAreConfidenceForNhParsing() ) { + for( final Phylogeny phy : phys ) { + PhylogenyMethods.transferInternalNodeNamesToConfidence( phy, "" ); + } + } + AptxUtil.addPhylogeniesToTabs( phys, + new File( url.getFile() ).getName(), + new File( url.getFile() ).toString(), + getConfiguration(), + getMainPanel() ); + _mainpanel.getControlPanel().showWhole(); + } + } + activateSaveAllIfNeeded(); + System.gc(); + } + + public void readSeqsFromFileforPI() { + // Set an initial directory if none set yet + final File my_dir = getCurrentDir(); + _seqs_pi_filechooser.setMultiSelectionEnabled( false ); + // Open file-open dialog and set current directory + if ( my_dir != null ) { + _seqs_pi_filechooser.setCurrentDirectory( my_dir ); + } + final int result = _seqs_pi_filechooser.showOpenDialog( _contentpane ); + // All done: get the seqs + final File file = _seqs_pi_filechooser.getSelectedFile(); + setCurrentDir( _seqs_pi_filechooser.getCurrentDirectory() ); + if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) { + setSeqsFile( null ); + setSeqs( null ); + List seqs = null; + try { + if ( FastaParser.isLikelyFasta( new FileInputStream( file ) ) ) { + seqs = FastaParser.parse( new FileInputStream( file ) ); + for( final MolecularSequence seq : seqs ) { + System.out.println( SequenceWriter.toFasta( seq, 60 ) ); } } + else { + //TODO error + } + } + catch ( final MsaFormatException e ) { + setArrowCursor(); + JOptionPane.showMessageDialog( this, + e.getLocalizedMessage(), + "Multiple sequence file format error", + JOptionPane.ERROR_MESSAGE ); + return; + } + catch ( final IOException e ) { + setArrowCursor(); + JOptionPane.showMessageDialog( this, + e.getLocalizedMessage(), + "Failed to read multiple sequence file", + JOptionPane.ERROR_MESSAGE ); + return; + } + catch ( final IllegalArgumentException e ) { + setArrowCursor(); + JOptionPane.showMessageDialog( this, + e.getLocalizedMessage(), + "Unexpected error during reading of multiple sequence file", + JOptionPane.ERROR_MESSAGE ); + return; + } + catch ( final Exception e ) { + setArrowCursor(); + e.printStackTrace(); + JOptionPane.showMessageDialog( this, + e.getLocalizedMessage(), + "Unexpected error during reading of multiple sequence file", + JOptionPane.ERROR_MESSAGE ); + return; + } + if ( ( seqs == null ) || ( seqs.size() < 1 ) ) { + JOptionPane.showMessageDialog( this, + "Multiple sequence file is empty", + "Illegal multiple sequence file", + JOptionPane.ERROR_MESSAGE ); + return; + } + if ( seqs.size() < 4 ) { + JOptionPane.showMessageDialog( this, + "Multiple sequence file needs to contain at least 3 sequences", + "Illegal multiple sequence file", + JOptionPane.ERROR_MESSAGE ); + return; } + // if ( msa.getLength() < 2 ) { + // JOptionPane.showMessageDialog( this, + // "Multiple sequence alignment needs to contain at least 2 residues", + // "Illegal multiple sequence file", + // JOptionPane.ERROR_MESSAGE ); + // return; + // } + System.gc(); + setSeqsFile( _seqs_pi_filechooser.getSelectedFile() ); + setSeqs( seqs ); } } @@ -2320,7 +2167,7 @@ public final class MainFrameApplication extends MainFrame { final int result = _open_filechooser_for_species_tree.showOpenDialog( _contentpane ); final File file = _open_filechooser_for_species_tree.getSelectedFile(); if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) { - if ( _open_filechooser_for_species_tree.getFileFilter() == MainFrameApplication.xmlfilter ) { + if ( _open_filechooser_for_species_tree.getFileFilter() == MainFrame.xmlfilter ) { try { final Phylogeny[] trees = PhylogenyMethods.readPhylogenies( PhyloXmlParser .createPhyloXmlParserXsdValidating(), file ); @@ -2331,7 +2178,7 @@ public final class MainFrameApplication extends MainFrame { exceptionOccuredDuringOpenFile( e ); } } - else if ( _open_filechooser_for_species_tree.getFileFilter() == MainFrameApplication.tolfilter ) { + else if ( _open_filechooser_for_species_tree.getFileFilter() == MainFrame.tolfilter ) { try { final Phylogeny[] trees = PhylogenyMethods.readPhylogenies( new TolParser(), file ); t = trees[ 0 ]; @@ -2414,20 +2261,34 @@ public final class MainFrameApplication extends MainFrame { } } - + private void setMinNotCollapseBlValue( final double min_not_collapse_bl ) { + _min_not_collapse_bl = min_not_collapse_bl; + } private void setMinNotCollapseConfidenceValue( final double min_not_collapse ) { _min_not_collapse = min_not_collapse; } - private void setMinNotCollapseBlValue( final double min_not_collapse_bl ) { - _min_not_collapse_bl = min_not_collapse_bl; + void setMsa( final Msa msa ) { + _msa = msa; + } + + void setMsaFile( final File msa_file ) { + _msa_file = msa_file; } private void setPhylogeneticInferenceOptions( final PhylogeneticInferenceOptions phylogenetic_inference_options ) { _phylogenetic_inference_options = phylogenetic_inference_options; } + void setSeqs( final List seqs ) { + _seqs = seqs; + } + + void setSeqsFile( final File seqs_file ) { + _seqs_file = seqs_file; + } + private void setSpecialOptionsForNexParser( final NexusPhylogeniesParser nex ) { nex.setReplaceUnderscores( getOptions().isReplaceUnderscoresInNhParsing() ); nex.setTaxonomyExtraction( getOptions().getTaxonomyExtraction() ); @@ -2496,376 +2357,4 @@ 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, true, getOptions().getNhConversionSupportValueStyle(), file ); - } - catch ( final Exception e ) { - exception = true; - exceptionOccuredDuringSaveAs( e ); - } - return exception; - } - - private boolean writeAsNexus( final Phylogeny t, boolean exception, final File file ) { - try { - final PhylogenyWriter writer = new PhylogenyWriter(); - writer.toNexus( file, t, getOptions().getNhConversionSupportValueStyle() ); - } - catch ( final Exception e ) { - exception = true; - exceptionOccuredDuringSaveAs( e ); - } - return exception; - } - - private boolean writeAsPhyloXml( final Phylogeny t, boolean exception, final File file ) { - try { - final PhylogenyWriter writer = new PhylogenyWriter(); - writer.toPhyloXML( file, t, 0 ); - } - catch ( final Exception e ) { - exception = true; - exceptionOccuredDuringSaveAs( e ); - } - return exception; - } - - private void writeToFile( final Phylogeny t ) { - if ( t == null ) { - return; - } - String initial_filename = null; - if ( getMainPanel().getCurrentTreePanel().getTreeFile() != null ) { - try { - initial_filename = getMainPanel().getCurrentTreePanel().getTreeFile().getCanonicalPath(); - } - catch ( final IOException e ) { - initial_filename = null; - } - } - if ( !ForesterUtil.isEmpty( initial_filename ) ) { - _save_filechooser.setSelectedFile( new File( initial_filename ) ); - } - else { - _save_filechooser.setSelectedFile( new File( "" ) ); - } - final File my_dir = getCurrentDir(); - if ( my_dir != null ) { - _save_filechooser.setCurrentDirectory( my_dir ); - } - final int result = _save_filechooser.showSaveDialog( _contentpane ); - final File file = _save_filechooser.getSelectedFile(); - setCurrentDir( _save_filechooser.getCurrentDirectory() ); - boolean exception = false; - if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) { - if ( file.exists() ) { - final int i = JOptionPane.showConfirmDialog( this, - file + " already exists.\nOverwrite?", - "Overwrite?", - JOptionPane.OK_CANCEL_OPTION, - JOptionPane.QUESTION_MESSAGE ); - if ( i != JOptionPane.OK_OPTION ) { - return; - } - else { - final File to = new File( file.getAbsoluteFile().toString() + Constants.BACKUP_FILE_SUFFIX ); - try { - ForesterUtil.copyFile( file, to ); - } - catch ( final Exception e ) { - JOptionPane.showMessageDialog( this, - "Failed to create backup copy " + to, - "Failed to Create Backup Copy", - JOptionPane.WARNING_MESSAGE ); - } - try { - file.delete(); - } - catch ( final Exception e ) { - JOptionPane.showMessageDialog( this, - "Failed to delete: " + file, - "Failed to Delete", - JOptionPane.WARNING_MESSAGE ); - } - } - } - if ( _save_filechooser.getFileFilter() == MainFrameApplication.nhfilter ) { - exception = writeAsNewHampshire( t, exception, file ); - } - else if ( _save_filechooser.getFileFilter() == MainFrameApplication.xmlfilter ) { - exception = writeAsPhyloXml( t, exception, file ); - } - else if ( _save_filechooser.getFileFilter() == MainFrameApplication.nexusfilter ) { - exception = writeAsNexus( t, exception, file ); - } - // "*.*": - else { - final String file_name = file.getName().trim().toLowerCase(); - if ( file_name.endsWith( ".nh" ) || file_name.endsWith( ".newick" ) || file_name.endsWith( ".phy" ) - || file_name.endsWith( ".tree" ) ) { - exception = writeAsNewHampshire( t, exception, file ); - } - else if ( file_name.endsWith( ".nex" ) || file_name.endsWith( ".nexus" ) ) { - exception = writeAsNexus( t, exception, file ); - } - // XML is default: - else { - exception = writeAsPhyloXml( t, exception, file ); - } - } - if ( !exception ) { - getMainPanel().setTitleOfSelectedTab( file.getName() ); - getMainPanel().getCurrentTreePanel().setTreeFile( file ); - getMainPanel().getCurrentTreePanel().setEdited( false ); - } - } - } - - private void writeToGraphicsFile( final Phylogeny t, final GraphicsExportType type ) { - if ( ( t == null ) || t.isEmpty() ) { - return; - } - String initial_filename = ""; - if ( getMainPanel().getCurrentTreePanel().getTreeFile() != null ) { - initial_filename = getMainPanel().getCurrentTreePanel().getTreeFile().toString(); - } - if ( initial_filename.indexOf( '.' ) > 0 ) { - initial_filename = initial_filename.substring( 0, initial_filename.lastIndexOf( '.' ) ); - } - initial_filename = initial_filename + "." + type; - _writetographics_filechooser.setSelectedFile( new File( initial_filename ) ); - final File my_dir = getCurrentDir(); - if ( my_dir != null ) { - _writetographics_filechooser.setCurrentDirectory( my_dir ); - } - final int result = _writetographics_filechooser.showSaveDialog( _contentpane ); - File file = _writetographics_filechooser.getSelectedFile(); - setCurrentDir( _writetographics_filechooser.getCurrentDirectory() ); - if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) { - if ( !file.toString().toLowerCase().endsWith( type.toString() ) ) { - file = new File( file.toString() + "." + type ); - } - if ( file.exists() ) { - final int i = JOptionPane.showConfirmDialog( this, - file + " already exists. Overwrite?", - "Warning", - JOptionPane.OK_CANCEL_OPTION, - JOptionPane.WARNING_MESSAGE ); - if ( i != JOptionPane.OK_OPTION ) { - return; - } - else { - try { - file.delete(); - } - catch ( final Exception e ) { - JOptionPane.showMessageDialog( this, - "Failed to delete: " + file, - "Error", - JOptionPane.WARNING_MESSAGE ); - } - } - } - writePhylogenyToGraphicsFile( file.toString(), type ); - } - } - - - public static MainFrameApplication createInstance( final Phylogeny[] phys, final Configuration config ) { - return new MainFrameApplication( phys, config ); - } - - 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 MainFrame createInstance( final Phylogeny[] phys, final Configuration config, final String title ) { - return new MainFrameApplication( phys, config, title ); - } - - static MainFrame createInstance( final Phylogeny[] phys, final String config_file_name, final String title ) { - return new MainFrameApplication( phys, config_file_name, title ); - } - - static void setTextForGraphicsSizeChooserMenuItem( final JMenuItem mi, final Options o ) { - mi.setText( "Enter Default Size for Graphics Export... (current: " + o.getPrintSizeX() + ", " - + o.getPrintSizeY() + ")" ); - } - - static void setTextForPdfLineWidthChooserMenuItem( final JMenuItem mi, final Options o ) { - mi.setText( "Enter Default Line Width for PDF Export... (current: " + o.getPrintLineWidth() + ")" ); - } - - static void warnIfNotPhyloXmlValidation( final Configuration c ) { - if ( !c.isValidatePhyloXmlAgainstSchema() ) { - JOptionPane - .showMessageDialog( null, - ForesterUtil - .wordWrap( "phyloXML XSD-based validation is turned off [enable with line 'validate_against_phyloxml_xsd_schem: true' in configuration file]", - 80 ), - "Warning", - JOptionPane.WARNING_MESSAGE ); - } - } } // MainFrameApplication. - -class DefaultFilter extends FileFilter { - - @Override - public boolean accept( final File f ) { - final String file_name = f.getName().trim().toLowerCase(); - return file_name.endsWith( ".nh" ) || file_name.endsWith( ".newick" ) || file_name.endsWith( ".phy" ) - || file_name.endsWith( ".nwk" ) || file_name.endsWith( ".phb" ) || file_name.endsWith( ".ph" ) - || file_name.endsWith( ".tr" ) || file_name.endsWith( ".dnd" ) || file_name.endsWith( ".tree" ) - || file_name.endsWith( ".nhx" ) || file_name.endsWith( ".xml" ) || file_name.endsWith( ".phyloxml" ) - || file_name.endsWith( "phylo.xml" ) || file_name.endsWith( ".pxml" ) || file_name.endsWith( ".nexus" ) - || file_name.endsWith( ".nx" ) || file_name.endsWith( ".nex" ) || file_name.endsWith( ".tre" ) - || file_name.endsWith( ".zip" ) || file_name.endsWith( ".tol" ) || file_name.endsWith( ".tolxml" ) - || file_name.endsWith( ".con" ) || f.isDirectory(); - } - - @Override - public String getDescription() { - return "All supported files (*.xml, *.phyloxml, *phylo.xml, *.nhx, *.nh, *.newick, *.nex, *.nexus, *.phy, *.tre, *.tree, *.tol, ...)"; - } -} - -class GraphicsFileFilter extends FileFilter { - - @Override - public boolean accept( final File f ) { - final String file_name = f.getName().trim().toLowerCase(); - return file_name.endsWith( ".jpg" ) || file_name.endsWith( ".jpeg" ) || file_name.endsWith( ".png" ) - || file_name.endsWith( ".gif" ) || file_name.endsWith( ".bmp" ) || f.isDirectory(); - } - - @Override - public String getDescription() { - return "Image files (*.jpg, *.jpeg, *.png, *.gif, *.bmp)"; - } -} - -class MsaFileFilter extends FileFilter { - - @Override - public boolean accept( final File f ) { - final String file_name = f.getName().trim().toLowerCase(); - return file_name.endsWith( ".msa" ) || file_name.endsWith( ".aln" ) || file_name.endsWith( ".fasta" ) - || file_name.endsWith( ".fas" ) || file_name.endsWith( ".fa" ) || f.isDirectory(); - } - - @Override - public String getDescription() { - return "Multiple sequence alignment files (*.msa, *.aln, *.fasta, *.fa, *.fas)"; - } -} - -class NexusFilter extends FileFilter { - - @Override - public boolean accept( final File f ) { - final String file_name = f.getName().trim().toLowerCase(); - return file_name.endsWith( ".nex" ) || file_name.endsWith( ".nexus" ) || file_name.endsWith( ".nx" ) - || file_name.endsWith( ".tre" ) || f.isDirectory(); - } - - @Override - public String getDescription() { - return "Nexus files (*.nex, *.nexus, *.nx, *.tre)"; - } -} // NexusFilter - -class NHFilter extends FileFilter { - - @Override - public boolean accept( final File f ) { - final String file_name = f.getName().trim().toLowerCase(); - return file_name.endsWith( ".nh" ) || file_name.endsWith( ".newick" ) || file_name.endsWith( ".phy" ) - || file_name.endsWith( ".tr" ) || file_name.endsWith( ".tree" ) || file_name.endsWith( ".dnd" ) - || file_name.endsWith( ".ph" ) || file_name.endsWith( ".phb" ) || file_name.endsWith( ".nwk" ) - || f.isDirectory(); - } - - @Override - public String getDescription() { - return "New Hampshire - Newick files (*.nh, *.newick, *.phy, *.tree, *.dnd, *.tr, *.ph, *.phb, *.nwk)"; - } -} // NHFilter - -class NHXFilter extends FileFilter { - - @Override - public boolean accept( final File f ) { - final String file_name = f.getName().trim().toLowerCase(); - return file_name.endsWith( ".nhx" ) || f.isDirectory(); - } - - @Override - public String getDescription() { - return "NHX files (*.nhx) [deprecated]"; - } -} - -class PdfFilter extends FileFilter { - - @Override - public boolean accept( final File f ) { - return f.getName().trim().toLowerCase().endsWith( ".pdf" ) || f.isDirectory(); - } - - @Override - public String getDescription() { - return "PDF files (*.pdf)"; - } -} // PdfFilter - -class SequencesFileFilter extends FileFilter { - - @Override - public boolean accept( final File f ) { - final String file_name = f.getName().trim().toLowerCase(); - return file_name.endsWith( ".fasta" ) || file_name.endsWith( ".fa" ) || file_name.endsWith( ".fas" ) - || file_name.endsWith( ".seqs" ) || f.isDirectory(); - } - - @Override - public String getDescription() { - return "Sequences files (*.fasta, *.fa, *.fas, *.seqs )"; - } -} - -class TolFilter extends FileFilter { - - @Override - public boolean accept( final File f ) { - final String file_name = f.getName().trim().toLowerCase(); - return ( file_name.endsWith( ".tol" ) || file_name.endsWith( ".tolxml" ) || file_name.endsWith( ".zip" ) || f - .isDirectory() ) && ( !file_name.endsWith( ".xml.zip" ) ); - } - - @Override - public String getDescription() { - return "Tree of Life files (*.tol, *.tolxml)"; - } -} // TolFilter - -class XMLFilter extends FileFilter { - - @Override - public boolean accept( final File f ) { - final String file_name = f.getName().trim().toLowerCase(); - return file_name.endsWith( ".xml" ) || file_name.endsWith( ".phyloxml" ) || file_name.endsWith( "phylo.xml" ) - || file_name.endsWith( ".pxml" ) || file_name.endsWith( ".zip" ) || f.isDirectory(); - } - - @Override - public String getDescription() { - return "phyloXML files (*.xml, *.phyloxml, *phylo.xml, *.pxml, *.zip)"; - } -} // XMLFilter -- 1.7.10.2