+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
package jalview.gui;
-import jalview.api.analysis.AnnotationFilterParameter;
-import jalview.api.analysis.AnnotationFilterParameter.SearchableAnnotationField;
import jalview.datamodel.AlignmentAnnotation;
-import jalview.datamodel.Annotation;
-import jalview.datamodel.ColumnSelection;
import jalview.datamodel.GraphLine;
-import jalview.datamodel.SequenceGroup;
import jalview.schemes.AnnotationColourGradient;
import jalview.util.MessageManager;
+import java.awt.Color;
+import java.awt.Dimension;
import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.FocusAdapter;
+import java.awt.event.FocusEvent;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
-import java.util.List;
+import java.math.BigDecimal;
+import java.math.MathContext;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Vector;
+import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
-import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
@SuppressWarnings("serial")
public abstract class AnnotationRowFilter extends JPanel
{
+ private static final String TWO_DP = "%.2f";
+
+ private final static MathContext FOUR_SIG_FIG = new MathContext(4);
+
protected AlignViewport av;
protected AlignmentPanel ap;
protected int[] annmap;
- protected boolean enableSeqAss = false;
-
- private jalview.datamodel.AlignmentAnnotation currentAnnotation;
-
protected boolean adjusting = false;
- protected JCheckBox currentColours = new JCheckBox();
-
- protected JPanel minColour = new JPanel();
-
- protected JPanel maxColour = new JPanel();
-
protected JCheckBox seqAssociated = new JCheckBox();
- protected JCheckBox thresholdIsMin = new JCheckBox();
+ protected JCheckBox percentThreshold = new JCheckBox();
- protected JSlider slider = new JSlider();
+ protected Slider slider;
protected JTextField thresholdValue = new JTextField(20);
protected JInternalFrame frame;
+
+ protected JButton ok = new JButton();
+
+ protected JButton cancel = new JButton();
+
/**
* enabled if the user is dragging the slider - try to keep updates to a
* minimun
*/
protected boolean sliderDragging = false;
+ protected JComboBox<String> threshold = new JComboBox<>();
+
+ protected JComboBox<String> annotations;
+
+ /*
+ * map from annotation to its menu item display label
+ * - so we know which item to pre-select on restore
+ */
+ private Map<AlignmentAnnotation, String> annotationLabels;
+
+ private AlignmentAnnotation currentAnnotation;
+
+ /**
+ * Constructor
+ *
+ * @param viewport
+ * @param alignPanel
+ */
+ public AnnotationRowFilter(AlignViewport viewport,
+ final AlignmentPanel alignPanel)
+ {
+ this.av = viewport;
+ this.ap = alignPanel;
+ this.slider = new Slider(0f, 100f, 50f);
+
+ thresholdValue.addFocusListener(new FocusAdapter()
+ {
+ @Override
+ public void focusLost(FocusEvent e)
+ {
+ thresholdValue_actionPerformed();
+ }
+ });
+ }
+
protected void addSliderChangeListener()
{
{
if (!adjusting)
{
- thresholdValue.setText((slider.getValue() / 1000f) + "");
+ setThresholdValueText();
valueChanged(!sliderDragging);
}
}
});
}
+ /**
+ * update the text field from the threshold slider. preserves state of
+ * 'adjusting' so safe to call in init.
+ */
+ protected void setThresholdValueText()
+ {
+ boolean oldadj = adjusting;
+ adjusting = true;
+ if (percentThreshold.isSelected())
+ {
+ thresholdValue
+ .setText(String.format(TWO_DP, getSliderPercentageValue()));
+ }
+ else
+ {
+ /*
+ * round to 4 significant digits without trailing zeroes
+ */
+ float f = getSliderValue();
+ BigDecimal formatted = new BigDecimal(f).round(FOUR_SIG_FIG)
+ .stripTrailingZeros();
+ thresholdValue.setText(formatted.toPlainString());
+ }
+ adjusting = oldadj;
+ }
+
+ /**
+ * Answers the value of the slider position (descaled to 'true' value)
+ *
+ * @return
+ */
+ protected float getSliderValue()
+ {
+ return slider.getSliderValue();
+ }
+
+ /**
+ * Sets the slider value (scaled from the true value to the slider range)
+ *
+ * @param value
+ */
+ protected void setSliderValue(float value)
+ {
+ slider.setSliderValue(value);
+ }
+
+ /**
+ * Answers the value of the slider position as a percentage between minimum
+ * and maximum of its range
+ *
+ * @return
+ */
+ protected float getSliderPercentageValue()
+ {
+ return slider.getSliderPercentageValue();
+ }
+
+ /**
+ * Sets the slider position for a given percentage value of its min-max range
+ *
+ * @param pct
+ */
+ protected void setSliderPercentageValue(float pct)
+ {
+ slider.setSliderPercentageValue(pct);
+ }
+
protected void addSliderMouseListeners()
{
@Override
public void mouseReleased(MouseEvent evt)
{
- if (sliderDragging)
- {
- sliderDragging = false;
- valueChanged(true);
- }
- ap.paintAlignment(true);
+ sliderDragReleased();
}
});
}
-
- public AnnotationRowFilter(AlignViewport av, final AlignmentPanel ap)
- {
- this.av = av;
- this.ap = ap;
- }
-
- public AnnotationRowFilter()
- {
-
- }
-
+ /**
+ * Builds and returns a list of menu items (display text) for choice of
+ * annotation. Also builds maps between annotations, their positions in the
+ * list, and their display labels in the list.
+ *
+ * @param isSeqAssociated
+ * @return
+ */
public Vector<String> getAnnotationItems(boolean isSeqAssociated)
{
- Vector<String> list = new Vector<String>();
+ annotationLabels = new HashMap<>();
+
+ Vector<String> list = new Vector<>();
int index = 1;
- int[] anmap = new int[av.getAlignment().getAlignmentAnnotation().length];
- for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
+ int[] anmap = new int[av.getAlignment()
+ .getAlignmentAnnotation().length];
+ seqAssociated.setEnabled(false);
+ for (int i = 0; i < av.getAlignment()
+ .getAlignmentAnnotation().length; i++)
{
- if (av.getAlignment().getAlignmentAnnotation()[i].sequenceRef == null)
+ AlignmentAnnotation annotation = av.getAlignment()
+ .getAlignmentAnnotation()[i];
+ if (annotation.sequenceRef == null)
{
if (isSeqAssociated)
{
}
else
{
- enableSeqAss = true;
+ seqAssociated.setEnabled(true);
}
- String label = av.getAlignment().getAlignmentAnnotation()[i].label;
+ String label = annotation.label;
+ // add associated sequence ID if available
+ if (!isSeqAssociated && annotation.sequenceRef != null)
+ {
+ label = label + "_" + annotation.sequenceRef.getName();
+ }
+ // make label unique
if (!list.contains(label))
{
anmap[list.size()] = i;
list.add(label);
-
+ annotationLabels.put(annotation, label);
}
else
{
if (!isSeqAssociated)
{
anmap[list.size()] = i;
- list.add(label + "_" + (index++));
+ label = label + "_" + (index++);
+ list.add(label);
+ annotationLabels.put(annotation, label);
}
}
}
return selectedThresholdItem;
}
- public void modelChanged()
+ public void ok_actionPerformed()
{
- seqAssociated.setEnabled(enableSeqAss);
- }
-
- public void ok_actionPerformed(ActionEvent e)
- {
- updateView();
try
{
frame.setClosed(true);
}
}
- public void cancel_actionPerformed(ActionEvent e)
+ public void cancel_actionPerformed()
{
reset();
- ap.paintAlignment(true);
+ ap.paintAlignment(true, true);
try
{
frame.setClosed(true);
}
}
- public void thresholdCheck_actionPerformed(ActionEvent e)
+ protected void thresholdCheck_actionPerformed()
{
updateView();
}
- public void annotations_actionPerformed(ActionEvent e)
+ protected void selectedAnnotationChanged()
{
updateView();
}
- public void threshold_actionPerformed(ActionEvent e)
+ protected void threshold_actionPerformed()
{
updateView();
}
- public void thresholdValue_actionPerformed(ActionEvent e)
+ /**
+ * Updates the slider position, and the display, for an update in the slider's
+ * text input field
+ */
+ protected void thresholdValue_actionPerformed()
{
try
{
float f = Float.parseFloat(thresholdValue.getText());
- slider.setValue((int) (f * 1000));
+ if (percentThreshold.isSelected())
+ {
+ setSliderPercentageValue(f);
+ }
+ else
+ {
+ setSliderValue(f);
+ }
updateView();
} catch (NumberFormatException ex)
{
}
}
- public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
+ protected void percentageValue_actionPerformed()
+ {
+ setThresholdValueText();
+ }
+
+ protected void thresholdIsMin_actionPerformed()
{
updateView();
}
- protected void populateThresholdComboBox(JComboBox<String> threshold)
+ protected void populateThresholdComboBox(JComboBox<String> thresh)
{
- threshold.addItem(MessageManager
- .getString("label.threshold_feature_no_thereshold"));
- threshold.addItem(MessageManager
- .getString("label.threshold_feature_above_thereshold"));
- threshold.addItem(MessageManager
- .getString("label.threshold_feature_below_thereshold"));
+ thresh.addItem(MessageManager
+ .getString("label.threshold_feature_no_threshold"));
+ thresh.addItem(MessageManager
+ .getString("label.threshold_feature_above_threshold"));
+ thresh.addItem(MessageManager
+ .getString("label.threshold_feature_below_threshold"));
}
- protected void seqAssociated_actionPerformed(ActionEvent arg0,
- JComboBox<String> annotations, JCheckBox seqAssociated)
+ /**
+ * Rebuilds the drop-down list of annotations to choose from when the 'per
+ * sequence only' checkbox is checked or unchecked.
+ *
+ * @param anns
+ */
+ protected void seqAssociated_actionPerformed(JComboBox<String> anns)
{
adjusting = true;
- String cursel = (String) annotations.getSelectedItem();
- boolean isvalid = false, isseqs = seqAssociated.isSelected();
- annotations.removeAllItems();
+ String cursel = (String) anns.getSelectedItem();
+ boolean isvalid = false;
+ boolean isseqs = seqAssociated.isSelected();
+ anns.removeAllItems();
for (String anitem : getAnnotationItems(seqAssociated.isSelected()))
{
if (anitem.equals(cursel) || (isseqs && cursel.startsWith(anitem)))
isvalid = true;
cursel = anitem;
}
- annotations.addItem(anitem);
+ anns.addItem(anitem);
}
- adjusting = false;
if (isvalid)
{
- annotations.setSelectedItem(cursel);
+ anns.setSelectedItem(cursel);
}
else
{
- if (annotations.getItemCount() > 0)
+ if (anns.getItemCount() > 0)
{
- annotations.setSelectedIndex(0);
+ anns.setSelectedIndex(0);
}
}
+ adjusting = false;
+
+ updateView();
}
protected void propagateSeqAssociatedThreshold(boolean allAnnotation,
}
float thr = annotation.threshold.value;
- for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
+ for (int i = 0; i < av.getAlignment()
+ .getAlignmentAnnotation().length; i++)
{
- AlignmentAnnotation aa = av.getAlignment().getAlignmentAnnotation()[i];
+ AlignmentAnnotation aa = av.getAlignment()
+ .getAlignmentAnnotation()[i];
if (aa.label.equals(annotation.label)
&& (annotation.getCalcId() == null ? aa.getCalcId() == null
: annotation.getCalcId().equals(aa.getCalcId())))
}
}
- protected boolean colorAlignmContaining(
- AlignmentAnnotation currentAnnotation, int selectedThresholdItem)
+ public AlignmentAnnotation getCurrentAnnotation()
{
+ return currentAnnotation;
+ }
- AnnotationColourGradient acg = null;
- if (currentColours.isSelected())
- {
- acg = new AnnotationColourGradient(currentAnnotation,
- av.getGlobalColourScheme(), selectedThresholdItem);
- }
- else
- {
- acg = new AnnotationColourGradient(currentAnnotation,
- minColour.getBackground(), maxColour.getBackground(),
- selectedThresholdItem);
- }
- acg.setSeqAssociated(seqAssociated.isSelected());
-
- if (currentAnnotation.graphMin == 0f
- && currentAnnotation.graphMax == 0f)
- {
- acg.setPredefinedColours(true);
- }
-
- acg.thresholdIsMinMax = thresholdIsMin.isSelected();
-
- av.setGlobalColourScheme(acg);
-
- if (av.getAlignment().getGroups() != null)
- {
+ protected void setCurrentAnnotation(AlignmentAnnotation annotation)
+ {
+ this.currentAnnotation = annotation;
+ }
- for (SequenceGroup sg : ap.av.getAlignment().getGroups())
- {
- if (sg.cs == null)
- {
- continue;
- }
+ /**
+ * update associated view model and trigger any necessary repaints.
+ *
+ * @param updateAllAnnotation
+ */
+ protected abstract void valueChanged(boolean updateAllAnnotation);
- if (currentColours.isSelected())
- {
- sg.cs = new AnnotationColourGradient(currentAnnotation, sg.cs,
- selectedThresholdItem);
- ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
- .isSelected());
+ protected abstract void updateView();
- }
- else
- {
- sg.cs = new AnnotationColourGradient(currentAnnotation,
- minColour.getBackground(), maxColour.getBackground(),
- selectedThresholdItem);
- ((AnnotationColourGradient) sg.cs).setSeqAssociated(seqAssociated
- .isSelected());
- }
+ protected abstract void reset();
- }
- }
- return false;
+ protected String getAnnotationMenuLabel(AlignmentAnnotation ann)
+ {
+ return annotationLabels.get(ann);
}
- protected boolean filterAnnotations(Annotation[] annotations,
- AnnotationFilterParameter filterParams, ColumnSelection cs)
+ protected void jbInit()
{
- av.showAllHiddenColumns();
- cs.clear();
- int count = 0;
- do
+ ok.setOpaque(false);
+ ok.setText(MessageManager.getString("action.ok"));
+ ok.addActionListener(new ActionListener()
{
- if (annotations[count] != null)
+ @Override
+ public void actionPerformed(ActionEvent e)
{
+ ok_actionPerformed();
+ }
+ });
- boolean itemMatched = false;
+ cancel.setOpaque(false);
+ cancel.setText(MessageManager.getString("action.cancel"));
+ cancel.addActionListener(new ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ cancel_actionPerformed();
+ }
+ });
- if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.ABOVE_THRESHOLD
- && annotations[count].value > currentAnnotation.threshold.value)
- {
- itemMatched = true;
- }
- if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.BELOW_THRESHOLD
- && annotations[count].value < currentAnnotation.threshold.value)
- {
- itemMatched = true;
- }
+ annotations.addItemListener(new ItemListener()
+ {
+ @Override
+ public void itemStateChanged(ItemEvent e)
+ {
+ selectedAnnotationChanged();
+ }
+ });
+ annotations.setToolTipText(
+ MessageManager.getString("info.select_annotation_row"));
- if (filterParams.isFilterAlphaHelix()
- && annotations[count].secondaryStructure == 'H')
- {
- itemMatched = true;
- }
+ threshold.addActionListener(new ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ threshold_actionPerformed();
+ }
+ });
- if (filterParams.isFilterBetaSheet()
- && annotations[count].secondaryStructure == 'E')
- {
- itemMatched = true;
- }
+ thresholdValue.setEnabled(false);
+ thresholdValue.setColumns(7);
+ thresholdValue.addActionListener(new ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ thresholdValue_actionPerformed();
+ }
+ });
- if (filterParams.isFilterTurn()
- && annotations[count].secondaryStructure == 'S')
+ percentThreshold
+ .setText(MessageManager.getString("label.as_percentage"));
+ percentThreshold.addActionListener(new ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ if (!adjusting)
{
- itemMatched = true;
+ percentageValue_actionPerformed();
}
+ }
+ });
+ slider.setPaintLabels(false);
+ slider.setPaintTicks(true);
+ slider.setBackground(Color.white);
+ slider.setEnabled(false);
+ slider.setOpaque(false);
+ slider.setPreferredSize(new Dimension(100, 32));
+ }
- String regexSearchString = filterParams.getRegexString();
- if (regexSearchString != null
- && !filterParams.getRegexSearchFields().isEmpty())
- {
- List<SearchableAnnotationField> fields = filterParams
- .getRegexSearchFields();
- try
- {
- if (fields.contains(SearchableAnnotationField.DISPLAY_STRING)
- && annotations[count].displayCharacter
- .matches(regexSearchString))
- {
- itemMatched = true;
- }
- } catch (java.util.regex.PatternSyntaxException pse)
- {
- if (annotations[count].displayCharacter
- .equals(regexSearchString))
- {
- itemMatched = true;
- }
- }
- if (fields.contains(SearchableAnnotationField.DESCRIPTION)
- && annotations[count].description != null
- && annotations[count].description
- .matches(regexSearchString))
- {
- itemMatched = true;
- }
- }
+ public JComboBox<String> getThreshold()
+ {
+ return threshold;
+ }
- if (itemMatched)
- {
- cs.addElement(count);
- }
- }
- count++;
- } while (count < annotations.length);
- return false;
+ public void setThreshold(JComboBox<String> thresh)
+ {
+ this.threshold = thresh;
}
- public jalview.datamodel.AlignmentAnnotation getCurrentAnnotation()
+ public JComboBox<String> getAnnotations()
{
- return currentAnnotation;
+ return annotations;
}
- public void setCurrentAnnotation(
- jalview.datamodel.AlignmentAnnotation currentAnnotation)
+ public void setAnnotations(JComboBox<String> anns)
{
- this.currentAnnotation = currentAnnotation;
+ this.annotations = anns;
}
- public abstract void valueChanged(boolean updateAllAnnotation);
+ protected void sliderDragReleased()
+ {
+ if (sliderDragging)
+ {
+ sliderDragging = false;
+ valueChanged(true);
+ }
+ }
- public abstract void updateView();
+ /**
+ * Sets the min-max range and current value of the slider, with rescaling from
+ * true values to slider range as required
+ *
+ * @param min
+ * @param max
+ * @param value
+ */
+ protected void setSliderModel(float min, float max, float value)
+ {
+ slider.setSliderModel(min, max, value);
- public abstract void reset();
+ /*
+ * tick mark every 10th position
+ */
+ slider.setMajorTickSpacing(
+ (slider.getMaximum() - slider.getMinimum()) / 10);
+ }
}