JAL-1340 simpler and more efficient interactive highlighting of redundant sequences
[jalview.git] / src / jalview / gui / RedundancyPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.gui;
20
21 import java.util.*;
22
23 import java.awt.event.*;
24 import javax.swing.*;
25 import javax.swing.event.*;
26
27 import jalview.analysis.AlignSeq;
28 import jalview.commands.*;
29 import jalview.datamodel.*;
30 import jalview.jbgui.*;
31 import jalview.util.MessageManager;
32
33 /**
34  * DOCUMENT ME!
35  * 
36  * @author $author$
37  * @version $Revision$
38  */
39 public class RedundancyPanel extends GSliderPanel implements Runnable
40 {
41   AlignFrame af;
42
43   AlignmentPanel ap;
44
45   Stack historyList = new Stack(); // simpler than synching with alignFrame.
46
47   float[] redundancy;
48
49   SequenceI[] originalSequences;
50
51   JInternalFrame frame;
52
53   Vector redundantSeqs;
54
55   /**
56    * Creates a new RedundancyPanel object.
57    * 
58    * @param ap
59    *          DOCUMENT ME!
60    * @param af
61    *          DOCUMENT ME!
62    */
63   public RedundancyPanel(final AlignmentPanel ap, AlignFrame af)
64   {
65     this.ap = ap;
66     this.af = af;
67     redundantSeqs = new Vector();
68
69     slider.addChangeListener(new ChangeListener()
70     {
71       public void stateChanged(ChangeEvent evt)
72       {
73         valueField.setText(slider.getValue() + "");
74         sliderValueChanged();
75       }
76     });
77
78     applyButton.setText(MessageManager.getString("action.remove"));
79     allGroupsCheck.setVisible(false);
80     slider.setMinimum(0);
81     slider.setMaximum(100);
82     slider.setValue(100);
83
84     Thread worker = new Thread(this);
85     worker.start();
86
87     frame = new JInternalFrame();
88     frame.setContentPane(this);
89     Desktop.addInternalFrame(frame, MessageManager.getString("label.redundancy_threshold_selection"), 400,
90             100, false);
91     frame.addInternalFrameListener(new InternalFrameAdapter()
92     {
93       public void internalFrameClosing(InternalFrameEvent evt)
94       {
95         ap.idPanel.idCanvas.setHighlighted(null);
96       }
97     });
98
99   }
100
101   /**
102    * This is a copy of remove redundancy in jalivew.datamodel.Alignment except
103    * we dont want to remove redundancy, just calculate once so we can use the
104    * slider to dynamically hide redundant sequences
105    * 
106    * @param threshold
107    *          DOCUMENT ME!
108    * @param sel
109    *          DOCUMENT ME!
110    * 
111    * @return DOCUMENT ME!
112    */
113   public void run()
114   {
115     JProgressBar progress = new JProgressBar();
116     progress.setIndeterminate(true);
117     southPanel.add(progress, java.awt.BorderLayout.SOUTH);
118
119     label.setText(MessageManager.getString("label.calculating"));
120
121     slider.setVisible(false);
122     applyButton.setEnabled(false);
123     valueField.setVisible(false);
124
125     validate();
126
127     String[] omitHidden = null;
128
129     SequenceGroup sg = ap.av.getSelectionGroup();
130     int height;
131
132     int start, end;
133
134     if ((sg != null) && (sg.getSize() >= 1))
135     {
136       originalSequences = sg.getSequencesInOrder(ap.av.getAlignment());
137       start = sg.getStartRes();
138       end = sg.getEndRes();
139     }
140     else
141     {
142       originalSequences = ap.av.getAlignment().getSequencesArray();
143       start = 0;
144       end = ap.av.getAlignment().getWidth();
145     }
146
147     height = originalSequences.length;
148     if (ap.av.hasHiddenColumns())
149     {
150       omitHidden = ap.av.getViewAsString(sg != null);
151     }
152     redundancy = AlignSeq.computeRedundancyMatrix(originalSequences,
153             omitHidden, start, end, false);
154
155     progress.setIndeterminate(false);
156     progress.setVisible(false);
157     progress = null;
158
159     label.setText("Enter the redundancy threshold");
160     slider.setVisible(true);
161     applyButton.setEnabled(true);
162     valueField.setVisible(true);
163
164     validate();
165     // System.out.println((System.currentTimeMillis()-start));
166   }
167
168   void sliderValueChanged()
169   {
170     if (redundancy == null)
171     {
172       return;
173     }
174
175     float value = slider.getValue();
176     List<SequenceI> redundantSequences = new ArrayList<SequenceI>();
177     for (int i = 0; i < redundancy.length; i++)
178     {
179       if (value <= redundancy[i])
180       {
181         redundantSequences.add(originalSequences[i]);
182       }
183     }
184     ap.idPanel.idCanvas.setHighlighted(redundantSequences);
185   }
186
187   /**
188    * DOCUMENT ME!
189    * 
190    * @param e
191    *          DOCUMENT ME!
192    */
193   public void applyButton_actionPerformed(ActionEvent e)
194   {
195     Vector del = new Vector();
196
197     undoButton.setEnabled(true);
198
199     float value = slider.getValue();
200     SequenceGroup sg = ap.av.getSelectionGroup();
201
202     for (int i = 0; i < redundancy.length; i++)
203     {
204       if (value <= redundancy[i])
205       {
206         del.addElement(originalSequences[i]);
207       }
208     }
209
210     // This has to be done before the restoreHistoryItem method of alignFrame
211     // will
212     // actually restore these sequences.
213     if (del.size() > 0)
214     {
215       SequenceI[] deleted = new SequenceI[del.size()];
216
217       int width = 0;
218       for (int i = 0; i < del.size(); i++)
219       {
220         deleted[i] = (SequenceI) del.elementAt(i);
221         if (deleted[i].getLength() > width)
222         {
223           width = deleted[i].getLength();
224         }
225       }
226
227       EditCommand cut = new EditCommand("Remove Redundancy",
228               EditCommand.CUT, deleted, 0, width, ap.av.getAlignment());
229
230       for (int i = 0; i < del.size(); i++)
231       {
232         ap.av.getAlignment().deleteSequence(deleted[i]);
233         if (sg != null)
234         {
235           sg.deleteSequence(deleted[i], false);
236         }
237       }
238
239       historyList.push(cut);
240
241       ap.alignFrame.addHistoryItem(cut);
242
243       PaintRefresher.Refresh(this, ap.av.getSequenceSetId(), true, true);
244       // ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
245       // .getSequences());
246     }
247
248   }
249
250   /**
251    * DOCUMENT ME!
252    * 
253    * @param e
254    *          DOCUMENT ME!
255    */
256   public void undoButton_actionPerformed(ActionEvent e)
257   {
258     CommandI command = (CommandI) historyList.pop();
259     command.undoCommand(af.getViewAlignments());
260
261     if (ap.av.historyList.contains(command))
262     {
263       ap.av.historyList.remove(command);
264       af.updateEditMenuBar();
265     }
266
267     ap.paintAlignment(true);
268
269     if (historyList.size() == 0)
270     {
271       undoButton.setEnabled(false);
272     }
273   }
274
275   /**
276    * DOCUMENT ME!
277    * 
278    * @param e
279    *          DOCUMENT ME!
280    */
281   public void valueField_actionPerformed(ActionEvent e)
282   {
283     try
284     {
285       int i = Integer.parseInt(valueField.getText());
286       slider.setValue(i);
287     } catch (Exception ex)
288     {
289       valueField.setText(slider.getValue() + "");
290     }
291   }
292
293 }