/*
* 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.NJTree;
import jalview.analysis.scoremodels.PIDModel;
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.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyVetoException;
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;
/**
* A dialog to allow a user to select and action Tree calculation options
*/
public class TreeChooser extends JPanel
{
private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
AlignFrame af;
JRadioButton pca;
JRadioButton tree;
JRadioButton neighbourJoining;
JRadioButton averageDistance;
JComboBox modelNames;
private JInternalFrame frame;
private ButtonGroup treeTypes;
private JCheckBox includeGaps;
private JCheckBox matchGaps;
private JCheckBox includeGappedColumns;
private JCheckBox shorterSequence;
/**
* Constructor
*
* @param af
*/
public TreeChooser(AlignFrame alignFrame)
{
this.af = alignFrame;
init();
}
/**
* Lays out the panel and adds it to the desktop
*/
void init()
{
frame = new JInternalFrame();
frame.setContentPane(this);
this.setBackground(Color.white);
/*
* Layout consists of 4 or 5 panels:
* - first with choice of Tree or PCA
* - second with choice of tree method NJ or AV
* - third with choice of score model
* - fourth with score model parameter options [suppressed]
* - fifth with OK and Cancel
*/
tree = new JRadioButton(MessageManager.getString("label.tree"));
tree.setOpaque(false);
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"));
ItemListener listener = new ItemListener()
{
@Override
public void itemStateChanged(ItemEvent e)
{
neighbourJoining.setEnabled(tree.isSelected());
averageDistance.setEnabled(tree.isSelected());
}
};
pca.addItemListener(listener);
tree.addItemListener(listener);
ButtonGroup calcTypes = new ButtonGroup();
calcTypes.add(pca);
calcTypes.add(tree);
JPanel calcChoicePanel = new JPanel();
calcChoicePanel.setOpaque(false);
tree.setSelected(true);
calcChoicePanel.add(tree);
calcChoicePanel.add(pca);
neighbourJoining.setOpaque(false);
treeTypes = new ButtonGroup();
treeTypes.add(neighbourJoining);
treeTypes.add(averageDistance);
neighbourJoining.setSelected(true);
JPanel treeChoicePanel = new JPanel();
treeChoicePanel.setOpaque(false);
treeChoicePanel.add(neighbourJoining);
treeChoicePanel.add(averageDistance);
/*
* score model drop-down
*/
modelNames = new JComboBox();
ScoreModels scoreModels = ScoreModels.getInstance();
for (ScoreModelI sm : scoreModels.getModels())
{
boolean nucleotide = af.getViewport().getAlignment().isNucleotide();
if (sm.isDNA() && nucleotide || sm.isProtein() && !nucleotide)
{
modelNames.addItem(sm.getName());
}
}
JPanel scoreModelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
scoreModelPanel.setOpaque(false);
scoreModelPanel.add(modelNames, FlowLayout.LEFT);
/*
* 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);
this.add(treeChoicePanel);
this.add(scoreModelPanel);
if (includeParams)
{
this.add(paramsPanel);
}
this.add(actionPanel);
int width = 380;
int height = includeParams ? 400 : 220;
Desktop.addInternalFrame(frame,
MessageManager.getString("label.choose_tree"), width, height,
false);
frame.setLayer(JLayeredPane.PALETTE_LAYER);
}
/**
* Open and calculate the selected tree on 'OK'
*/
protected void ok_actionPerformed()
{
boolean doPCA = pca.isSelected();
ScoreModelI sm = ScoreModels.getInstance().forName(
modelNames.getSelectedItem().toString());
SimilarityParamsI params = getSimilarityParameters(doPCA, sm);
if (doPCA)
{
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, sm, params);
}
else
{
String treeType = neighbourJoining.isSelected() ? NJTree.NEIGHBOUR_JOINING
: NJTree.AVERAGE_DISTANCE;
af.newTreePanel(treeType, sm, params);
}
// closeFrame();
}
/**
*
*/
protected void closeFrame()
{
try
{
frame.setClosed(true);
} catch (PropertyVetoException ex)
{
}
}
/**
* Returns a data bean holding parameters for similarity (or distance) model
* calculation
*
* @param doPCA
* @param sm
* @return
*/
protected SimilarityParamsI getSimilarityParameters(boolean doPCA,
ScoreModelI sm)
{
// commented out: parameter choices read from gui widgets
// SimilarityParamsI params = new SimilarityParams(
// includeGappedColumns.isSelected(), matchGaps.isSelected(),
// includeGaps.isSelected(), shorterSequence.isSelected());
/*
* for now we want
* includeGappedColumns = true
* includeGaps = true
* matchOnShortestSequence = false
* matchGaps = true except false for PCA by PID (to match SeqSpace)
*/
boolean matchGap = true;
if (doPCA && (sm instanceof PIDModel))
{
matchGap = false;
}
return new SimilarityParams(true, matchGap, true, false);
}
/**
* Closes dialog on cancel
*
* @param e
*/
protected void cancel_actionPerformed(ActionEvent e)
{
try
{
frame.setClosed(true);
} catch (Exception ex)
{
}
}
}