/* * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * * Jalview is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * Jalview is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jalview. If not, see . * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.gui; import jalview.analysis.TreeBuilder; import jalview.analysis.scoremodels.ScoreModels; import jalview.analysis.scoremodels.SimilarityParams; import jalview.api.analysis.ScoreModelI; import jalview.api.analysis.SimilarityParamsI; import jalview.util.MessageManager; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.beans.PropertyVetoException; import java.util.ArrayList; import java.util.List; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JInternalFrame; import javax.swing.JLabel; import javax.swing.JLayeredPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.event.InternalFrameAdapter; import javax.swing.event.InternalFrameEvent; /** * A dialog where a user can choose and action Tree or PCA calculation options */ public class CalculationChooser extends JPanel { /* * flag for whether gap matches residue in the PID calculation for a Tree * - true gives Jalview 2.10.1 behaviour * - set to false (using Groovy) for a more correct tree * (JAL-374) */ private static boolean treeMatchGaps = true; private static final Font VERDANA_11PT = new Font("Verdana", 0, 11); AlignFrame af; JRadioButton pca; JRadioButton neighbourJoining; JRadioButton averageDistance; JComboBox modelNames; JButton ok; private JInternalFrame frame; private JCheckBox includeGaps; private JCheckBox matchGaps; private JCheckBox includeGappedColumns; private JCheckBox shorterSequence; /** * Constructor * * @param af */ public CalculationChooser(AlignFrame alignFrame) { this.af = alignFrame; init(); af.alignPanel.setCalculationDialog(this); } /** * Lays out the panel and adds it to the desktop */ void init() { setLayout(new BorderLayout()); frame = new JInternalFrame(); frame.setContentPane(this); this.setBackground(Color.white); /* * Layout consists of 3 or 4 panels: * - first with choice of PCA or tree method NJ or AV * - second with choice of score model * - third with score model parameter options [suppressed] * - fourth with OK and Cancel */ pca = new JRadioButton( MessageManager.getString("label.principal_component_analysis")); pca.setOpaque(false); neighbourJoining = new JRadioButton( MessageManager.getString("label.tree_calc_nj")); averageDistance = new JRadioButton( MessageManager.getString("label.tree_calc_av")); neighbourJoining.setOpaque(false); JPanel calcChoicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); calcChoicePanel.setOpaque(false); // first create the Tree calculation's border panel JPanel treePanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); treePanel.setOpaque(false); treePanel.setBorder(BorderFactory.createTitledBorder(MessageManager .getString("label.tree"))); // then copy the inset dimensions for the border-less PCA panel JPanel pcaBorderless = new JPanel(new FlowLayout(FlowLayout.LEFT)); Insets b = treePanel.getBorder().getBorderInsets(treePanel); pcaBorderless.setBorder(BorderFactory.createEmptyBorder(2, b.left, 2, b.right)); pcaBorderless.setOpaque(false); pcaBorderless.add(pca, FlowLayout.LEFT); calcChoicePanel.add(pcaBorderless, FlowLayout.LEFT); treePanel.add(neighbourJoining); treePanel.add(averageDistance); calcChoicePanel.add(treePanel); ButtonGroup calcTypes = new ButtonGroup(); calcTypes.add(pca); calcTypes.add(neighbourJoining); calcTypes.add(averageDistance); { @Override public void itemStateChanged(ItemEvent e) { neighbourJoining.setEnabled(tree.isSelected()); averageDistance.setEnabled(tree.isSelected()); } }; /* * score models drop-down - with added tooltips! */ modelNames = buildModelOptionsList(); JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); scoreModelPanel.setOpaque(false); scoreModelPanel.add(modelNames); /* * score model parameters */ JPanel paramsPanel = new JPanel(new GridLayout(5, 1)); paramsPanel.setOpaque(false); includeGaps = new JCheckBox("Include gaps"); matchGaps = new JCheckBox("Match gaps"); includeGappedColumns = new JCheckBox("Include gapped columns"); shorterSequence = new JCheckBox("Match on shorter sequence"); paramsPanel.add(new JLabel("Pairwise sequence scoring options")); paramsPanel.add(includeGaps); paramsPanel.add(matchGaps); paramsPanel.add(includeGappedColumns); paramsPanel.add(shorterSequence); /* * OK / Cancel buttons */ JButton ok = new JButton(MessageManager.getString("action.ok")); ok.setFont(VERDANA_11PT); ok.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(ActionEvent e) { ok_actionPerformed(); } }); JButton cancel = new JButton(MessageManager.getString("action.cancel")); cancel.setFont(VERDANA_11PT); cancel.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(ActionEvent e) { cancel_actionPerformed(e); } }); JPanel actionPanel = new JPanel(); actionPanel.setOpaque(false); actionPanel.add(ok); actionPanel.add(cancel); boolean includeParams = false; this.add(calcChoicePanel, BorderLayout.CENTER); calcChoicePanel.add(scoreModelPanel); if (includeParams) { scoreModelPanel.add(paramsPanel); } this.add(actionPanel, BorderLayout.SOUTH); int width = 350; int height = includeParams ? 420 : 240; setMinimumSize(new Dimension(325, height - 10)); String title = MessageManager.getString("label.choose_calculation"); if (af.getViewport().viewName != null) { title = title + " (" + af.getViewport().viewName + ")"; } Desktop.addInternalFrame(frame, title, width, height, false); calcChoicePanel.doLayout(); revalidate(); /* * null the AlignmentPanel's reference to the dialog when it is closed */ frame.addInternalFrameListener(new InternalFrameAdapter() { @Override public void internalFrameClosed(InternalFrameEvent evt) { af.alignPanel.setCalculationDialog(null); }; }); frame.setLayer(JLayeredPane.PALETTE_LAYER); } /** * A rather elaborate helper method (blame Swing, not me) that builds a * drop-down list of score models (by name) with descriptions as tooltips. * There is also a tooltip shown for the currently selected item when hovering * over it (without opening the list). */ protected JComboBox buildModelOptionsList() { final JComboBox comboBox = new JComboBox(); ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer(); comboBox.setRenderer(renderer); final List tips = new ArrayList(); /* * show tooltip on mouse over the combobox * note the listener has to be on the components that make up * the combobox, doesn't work if just on the combobox */ MouseAdapter mouseListener = new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { comboBox.setToolTipText(tips.get(comboBox.getSelectedIndex())); } @Override public void mouseExited(MouseEvent e) { comboBox.setToolTipText(null); } }; for (Component c : comboBox.getComponents()) { c.addMouseListener(mouseListener); } /* * now we can actually add entries to the combobox, * remembering their descriptions for tooltips */ ScoreModels scoreModels = ScoreModels.getInstance(); for (ScoreModelI sm : scoreModels.getModels()) { boolean nucleotide = af.getViewport().getAlignment().isNucleotide(); if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide) { comboBox.addItem(sm.getName()); /* * tooltip is description if provided, else text lookup with * fallback on the model name */ String tooltip = sm.getDescription(); if (tooltip == null) { tooltip = MessageManager.getStringOrReturn("label.score_model_", sm.getName()); } tips.add(tooltip); } /* * set the list of tooltips on the combobox's renderer */ renderer.setTooltips(tips); } return comboBox; } /** * Open and calculate the selected tree or PCA on 'OK' */ protected void ok_actionPerformed() { boolean doPCA = pca.isSelected(); String modelName = modelNames.getSelectedItem().toString(); SimilarityParamsI params = getSimilarityParameters(doPCA); if (doPCA) { openPcaPanel(modelName, params); } else { openTreePanel(modelName, params); } // closeFrame(); } /** * Open a new Tree panel on the desktop * * @param modelName * @param params */ protected void openTreePanel(String modelName, SimilarityParamsI params) { String treeType = neighbourJoining.isSelected() ? TreeBuilder.NEIGHBOUR_JOINING : TreeBuilder.AVERAGE_DISTANCE; af.newTreePanel(treeType, modelName, params); } /** * Open a new PCA panel on the desktop * * @param modelName * @param params */ protected void openPcaPanel(String modelName, SimilarityParamsI params) { AlignViewport viewport = af.getViewport(); if (((viewport.getSelectionGroup() != null) && (viewport.getSelectionGroup().getSize() < 4) && (viewport .getSelectionGroup().getSize() > 0)) || (viewport.getAlignment().getHeight() < 4)) { JvOptionPane .showInternalMessageDialog( this, MessageManager .getString("label.principal_component_analysis_must_take_least_four_input_sequences"), MessageManager .getString("label.sequence_selection_insufficient"), JvOptionPane.WARNING_MESSAGE); return; } new PCAPanel(af.alignPanel, modelName, params); } /** * */ protected void closeFrame() { try { frame.setClosed(true); } catch (PropertyVetoException ex) { } } /** * Returns a data bean holding parameters for similarity (or distance) model * calculation * * @param doPCA * @return */ protected SimilarityParamsI getSimilarityParameters(boolean doPCA) { // commented out: parameter choices read from gui widgets // SimilarityParamsI params = new SimilarityParams( // includeGappedColumns.isSelected(), matchGaps.isSelected(), // includeGaps.isSelected(), shorterSequence.isSelected()); boolean includeGapGap = true; boolean includeGapResidue = true; boolean matchOnShortestLength = false; /* * 'matchGaps' flag is only used in the PID calculation * - set to false for PCA so that PCA using PID reproduces SeqSpace PCA * - set to true for Tree to reproduce Jalview 2.10.1 calculation * - set to false for Tree for a more correct calculation (JAL-374) */ boolean matchGap = doPCA ? false : treeMatchGaps; return new SimilarityParams(includeGapGap, matchGap, includeGapResidue, matchOnShortestLength); } /** * Closes dialog on cancel * * @param e */ protected void cancel_actionPerformed(ActionEvent e) { try { frame.setClosed(true); } catch (Exception ex) { } } }