2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 import java.awt.event.*;
25 import javax.swing.event.*;
27 import jalview.commands.*;
28 import jalview.datamodel.*;
29 import jalview.jbgui.*;
30 import jalview.util.*;
38 public class RedundancyPanel
39 extends GSliderPanel implements Runnable
43 Stack historyList = new Stack(); // simpler than synching with alignFrame.
45 SequenceI[] originalSequences;
50 * Creates a new RedundancyPanel object.
52 * @param ap DOCUMENT ME!
53 * @param af DOCUMENT ME!
55 public RedundancyPanel(final AlignmentPanel ap, AlignFrame af)
59 redundantSeqs = new Vector();
61 slider.addChangeListener(new ChangeListener()
63 public void stateChanged(ChangeEvent evt)
65 valueField.setText(slider.getValue() + "");
70 applyButton.setText("Remove");
71 allGroupsCheck.setVisible(false);
73 slider.setMaximum(100);
76 Thread worker = new Thread(this);
79 frame = new JInternalFrame();
80 frame.setContentPane(this);
81 Desktop.addInternalFrame(frame, "Redundancy threshold selection", 400,
83 frame.addInternalFrameListener(new InternalFrameAdapter()
85 public void internalFrameClosing(InternalFrameEvent evt)
87 ap.idPanel.idCanvas.setHighlighted(null);
95 * This is a copy of remove redundancy in jalivew.datamodel.Alignment
96 * except we dont want to remove redundancy, just calculate once
97 * so we can use the slider to dynamically hide redundant sequences
99 * @param threshold DOCUMENT ME!
100 * @param sel DOCUMENT ME!
102 * @return DOCUMENT ME!
106 JProgressBar progress = new JProgressBar();
107 progress.setIndeterminate(true);
108 southPanel.add(progress, java.awt.BorderLayout.SOUTH);
110 label.setText("Calculating....");
112 slider.setVisible(false);
113 applyButton.setEnabled(false);
114 valueField.setVisible(false);
118 String[] omitHidden = null;
120 SequenceGroup sg = ap.av.getSelectionGroup();
125 if ( (sg != null) && (sg.getSize() >= 1))
127 originalSequences = sg.getSequencesInOrder(ap.av.alignment);
128 start = sg.getStartRes();
129 end = sg.getEndRes();
133 originalSequences = ap.av.alignment.getSequencesArray();
135 end = ap.av.alignment.getWidth();
138 height = originalSequences.length;
140 redundancy = new float[height];
141 for (int i = 0; i < height; i++)
146 if (ap.av.hasHiddenColumns)
148 omitHidden = ap.av.getViewAsString(sg != null);
151 // long start = System.currentTimeMillis();
155 for (int i = 0; i < height; i++)
158 for (int j = 0; j < i; j++)
165 if (omitHidden == null)
167 seqi = originalSequences[i].getSequenceAsString(start, end);
168 seqj = originalSequences[j].getSequenceAsString(start, end);
172 seqi = omitHidden[i];
173 seqj = omitHidden[j];
176 pid = Comparison.PID(seqi, seqj);
178 if (seqj.length() < seqi.length())
180 redundancy[j] = Math.max(pid, redundancy[j]);
184 redundancy[i] = Math.max(pid, redundancy[i]);
190 progress.setIndeterminate(false);
191 progress.setVisible(false);
194 label.setText("Enter the redundancy threshold");
195 slider.setVisible(true);
196 applyButton.setEnabled(true);
197 valueField.setVisible(true);
200 // System.out.println((System.currentTimeMillis()-start));
203 void sliderValueChanged()
205 if (redundancy == null)
210 float value = slider.getValue();
212 for (int i = 0; i < redundancy.length; i++)
214 if (value > redundancy[i])
216 redundantSeqs.remove(originalSequences[i]);
218 else if (!redundantSeqs.contains(originalSequences[i]))
220 redundantSeqs.add(originalSequences[i]);
225 ap.idPanel.idCanvas.setHighlighted(redundantSeqs);
231 * @param e DOCUMENT ME!
233 public void applyButton_actionPerformed(ActionEvent e)
235 Vector del = new Vector();
237 undoButton.setEnabled(true);
239 float value = slider.getValue();
240 SequenceGroup sg = ap.av.getSelectionGroup();
242 for (int i = 0; i < redundancy.length; i++)
244 if (value <= redundancy[i])
246 del.addElement(originalSequences[i]);
250 // This has to be done before the restoreHistoryItem method of alignFrame will
251 // actually restore these sequences.
254 SequenceI[] deleted = new SequenceI[del.size()];
257 for (int i = 0; i < del.size(); i++)
259 deleted[i] = (SequenceI) del.elementAt(i);
260 if (deleted[i].getLength() > width)
262 width = deleted[i].getLength();
266 EditCommand cut = new EditCommand("Remove Redundancy",
267 EditCommand.CUT, deleted, 0, width,
270 for (int i = 0; i < del.size(); i++)
272 ap.av.alignment.deleteSequence(deleted[i]);
273 PaintRefresher.Refresh(this,
274 ap.av.getSequenceSetId(),
279 sg.deleteSequence(deleted[i], false);
283 historyList.push(cut);
285 ap.alignFrame.addHistoryItem(cut);
287 ap.av.firePropertyChange("alignment", null,
288 ap.av.getAlignment().getSequences());
296 * @param e DOCUMENT ME!
298 public void undoButton_actionPerformed(ActionEvent e)
300 CommandI command = (CommandI) historyList.pop();
301 command.undoCommand(af.getViewAlignments());
303 if (ap.av.historyList.contains(command))
305 ap.av.historyList.remove(command);
306 af.updateEditMenuBar();
309 ap.paintAlignment(true);
311 if (historyList.size() == 0)
313 undoButton.setEnabled(false);
320 * @param e DOCUMENT ME!
322 public void valueField_actionPerformed(ActionEvent e)
326 int i = Integer.parseInt(valueField.getText());
331 valueField.setText(slider.getValue() + "");