2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.analysis.AlignSeq;
24 import jalview.commands.CommandI;
25 import jalview.commands.EditCommand;
26 import jalview.commands.EditCommand.Action;
27 import jalview.datamodel.SequenceGroup;
28 import jalview.datamodel.SequenceI;
29 import jalview.jbgui.GSliderPanel;
30 import jalview.util.MessageManager;
32 import java.awt.event.ActionEvent;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Stack;
37 import java.util.Vector;
39 import javax.swing.JInternalFrame;
40 import javax.swing.JProgressBar;
41 import javax.swing.event.ChangeEvent;
42 import javax.swing.event.ChangeListener;
43 import javax.swing.event.InternalFrameAdapter;
44 import javax.swing.event.InternalFrameEvent;
52 public class RedundancyPanel extends GSliderPanel implements Runnable
58 Stack<CommandI> historyList = new Stack<CommandI>();
60 // simpler than synching with alignFrame.
64 SequenceI[] originalSequences;
70 private SequenceI[] redreps;
73 * Creates a new RedundancyPanel object.
80 public RedundancyPanel(final AlignmentPanel ap, AlignFrame af)
84 redundantSeqs = new Vector();
86 slider.addChangeListener(new ChangeListener()
88 public void stateChanged(ChangeEvent evt)
90 valueField.setText(slider.getValue() + "");
95 applyButton.setText(MessageManager.getString("action.hide"));
96 allGroupsCheck.setVisible(false);
98 slider.setMaximum(100);
101 Thread worker = new Thread(this);
104 frame = new JInternalFrame();
105 frame.setContentPane(this);
106 Desktop.addInternalFrame(frame, MessageManager
107 .getString("label.redundancy_threshold_selection"), 400, 100,
109 frame.addInternalFrameListener(new InternalFrameAdapter()
111 public void internalFrameClosing(InternalFrameEvent evt)
113 ap.getIdPanel().getIdCanvas().setHighlighted(null);
120 * This is a copy of remove redundancy in jalivew.datamodel.Alignment except
121 * we dont want to remove redundancy, just calculate once so we can use the
122 * slider to dynamically hide redundant sequences
129 * @return DOCUMENT ME!
133 JProgressBar progress = new JProgressBar();
134 progress.setIndeterminate(true);
135 southPanel.add(progress, java.awt.BorderLayout.SOUTH);
137 label.setText(MessageManager.getString("label.calculating"));
139 slider.setVisible(false);
140 applyButton.setEnabled(false);
141 valueField.setVisible(false);
145 String[] omitHidden = null;
147 SequenceGroup sg = ap.av.getSelectionGroup();
152 if ((sg != null) && (sg.getSize() >= 1))
154 originalSequences = sg.getSequencesInOrder(ap.av.getAlignment());
155 start = sg.getStartRes();
156 end = sg.getEndRes();
160 originalSequences = ap.av.getAlignment().getSequencesArray();
162 end = ap.av.getAlignment().getWidth();
165 height = originalSequences.length;
166 if (ap.av.hasHiddenColumns())
168 omitHidden = ap.av.getViewAsString(sg != null);
170 Object rr[] = AlignSeq.computeRedundancyMatrixWithRep(
172 omitHidden, start, end, false);
174 redundancy = (float[]) rr[0];
175 redreps = (SequenceI[]) rr[1];
177 progress.setIndeterminate(false);
178 progress.setVisible(false);
181 label.setText(MessageManager
182 .getString("label.enter_redundancy_thereshold"));
183 slider.setVisible(true);
184 applyButton.setEnabled(true);
185 valueField.setVisible(true);
188 sliderValueChanged();
189 // System.out.println((System.currentTimeMillis()-start));
192 void sliderValueChanged()
194 if (redundancy == null)
199 float value = slider.getValue();
200 List<SequenceI> redundantSequences = new ArrayList<SequenceI>();
201 for (int i = 0; i < redundancy.length; i++)
203 if (value <= redundancy[i])
205 redundantSequences.add(originalSequences[i]);
208 ap.getIdPanel().getIdCanvas().setHighlighted(redundantSequences);
217 public void applyButton_actionPerformed(ActionEvent e)
220 undoButton.setEnabled(true);
222 float value = slider.getValue();
223 SequenceGroup sg = ap.av.getSelectionGroup();
224 // delete_seqs(value, sg);
225 hide_seqs(value, sg);
227 private void hide_seqs(float value, SequenceGroup sg)
230 * hash to look up the representative for a sequence
232 HashMap<SequenceI, SequenceI> rep = new HashMap<SequenceI, SequenceI>();
234 * hash to collect lists of sequences represented by a sequence
236 HashMap<SequenceI, SequenceGroup> reps = new HashMap<SequenceI, SequenceGroup>();
237 for (int i = 0; i < redundancy.length; i++)
239 if (value <= redundancy[i])
241 // does this sequence represent other sequences ?
242 SequenceGroup repset;
243 // is the representative also redundant ?
244 SequenceI repForI = rep.get(redreps[i]);
246 // the representative is still in the alignment.
247 // is it representing anything already ?
248 repset = reps.get(redreps[i]);
251 repset = new SequenceGroup();
253 repset.addSequence(originalSequences[i], false);
254 rep.put(originalSequences[i], redreps[i]);
255 reps.put(redreps[i], repset);
256 // and save the representative sequence for originalSeq
257 repForI = redreps[i];
259 // already hidden the representative for this sequence, so look up its redundant peers
260 repset = reps.get(repForI);
263 throw new Error("Implementation failure for redundancy set creation");
265 // add the sequence to the peerset, and mark sequence's representative in hash
266 repset.addSequence(originalSequences[i], false);
267 rep.put(originalSequences[i], repForI);
269 // merge any sequences represented by this with its new containing group
270 SequenceGroup existingreps = reps.remove(originalSequences[i]);
271 if (existingreps!=null)
273 for (SequenceI sq:existingreps.getSequences())
275 rep.put(sq, repForI);
276 repset.addSequence(sq, false);
281 for (SequenceI repseq: reps.keySet())
283 sg = reps.get(repseq);
284 sg.addSequence(repseq, false);
285 sg.setSeqrep(repseq);
286 ap.av.hideRepSequences(repseq, sg);
290 private void delete_seqs(float value, SequenceGroup sg)
292 ArrayList<SequenceI> del = new ArrayList<SequenceI>();
294 for (int i = 0; i < redundancy.length; i++)
296 if (value <= redundancy[i])
298 del.add(originalSequences[i]);
301 // This has to be done before the restoreHistoryItem method of alignFrame
303 // actually restore these sequences.
306 SequenceI[] deleted = new SequenceI[del.size()];
309 for (int i = 0; i < del.size(); i++)
311 deleted[i] = del.get(i);
312 if (deleted[i].getLength() > width)
314 width = deleted[i].getLength();
318 EditCommand cut = new EditCommand(
319 MessageManager.getString("action.remove_redundancy"),
320 Action.CUT, deleted, 0, width, ap.av.getAlignment());
322 for (int i = 0; i < del.size(); i++)
324 ap.av.getAlignment().deleteSequence(deleted[i]);
327 sg.deleteSequence(deleted[i], false);
331 historyList.push(cut);
333 ap.alignFrame.addHistoryItem(cut);
335 PaintRefresher.Refresh(this, ap.av.getSequenceSetId(), true, true);
336 ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
348 public void undoButton_actionPerformed(ActionEvent e)
350 if (historyList == null || historyList.isEmpty())
352 undoButton.setEnabled(false);
356 CommandI command = historyList.pop();
357 if (ap.av.getHistoryList().contains(command))
359 command.undoCommand(af.getViewAlignments());
360 ap.av.getHistoryList().remove(command);
361 ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
363 af.updateEditMenuBar();
366 ap.paintAlignment(true);
368 if (historyList.size() == 0)
370 undoButton.setEnabled(false);
380 public void valueField_actionPerformed(ActionEvent e)
384 int i = Integer.parseInt(valueField.getText());
386 } catch (Exception ex)
388 valueField.setText(slider.getValue() + "");