JAL-1340 simpler and more efficient interactive highlighting of redundant sequences
[jalview.git] / src / jalview / appletgui / 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.appletgui;
20
21 import java.util.*;
22 import java.util.List;
23 import java.awt.*;
24 import java.awt.event.*;
25
26 import jalview.analysis.AlignSeq;
27 import jalview.commands.*;
28 import jalview.datamodel.*;
29 import jalview.util.MessageManager;
30
31 public class RedundancyPanel extends SliderPanel implements Runnable,
32         WindowListener
33 {
34   Stack historyList = new Stack(); // simpler than synching with alignFrame.
35
36   float[] redundancy;
37
38   SequenceI[] originalSequences;
39
40   Frame frame;
41
42   Vector redundantSeqs;
43
44   public RedundancyPanel(AlignmentPanel ap)
45   {
46     super(ap, 0, false, null);
47
48     redundantSeqs = new Vector();
49     this.ap = ap;
50     undoButton.setVisible(true);
51     applyButton.setVisible(true);
52     allGroupsCheck.setVisible(false);
53
54     label.setText(MessageManager.getString("label.enter_redundancy_threshold"));
55     valueField.setText("100");
56
57     slider.setVisibleAmount(1);
58     slider.setMinimum(0);
59     slider.setMaximum(100 + slider.getVisibleAmount());
60     slider.setValue(100);
61
62     slider.addAdjustmentListener(new AdjustmentListener()
63     {
64       public void adjustmentValueChanged(AdjustmentEvent evt)
65       {
66         valueField.setText(slider.getValue() + "");
67         sliderValueChanged();
68       }
69     });
70
71     frame = new Frame();
72     frame.add(this);
73     jalview.bin.JalviewLite.addFrame(frame,
74             MessageManager.getString("label.redundancy_threshold_selection"), 400, 100);
75
76     frame.addWindowListener(this);
77
78     Thread worker = new Thread(this);
79     worker.start();
80   }
81
82   /**
83    * This is a copy of remove redundancy in jalivew.datamodel.Alignment except
84    * we dont want to remove redundancy, just calculate once so we can use the
85    * slider to dynamically hide redundant sequences
86    * 
87    * @param threshold
88    *          DOCUMENT ME!
89    * @param sel
90    *          DOCUMENT ME!
91    * 
92    * @return DOCUMENT ME!
93    */
94   public void run()
95   {
96     label.setText(MessageManager.getString("label.calculating"));
97
98     slider.setVisible(false);
99     applyButton.setEnabled(false);
100     valueField.setVisible(false);
101
102     validate();
103
104     String[] omitHidden = null;
105
106     SequenceGroup sg = ap.av.getSelectionGroup();
107     int height;
108
109     int start, end;
110
111     if ((sg != null) && (sg.getSize() >= 1))
112     {
113       originalSequences = sg.getSequencesInOrder(ap.av.getAlignment());
114       start = sg.getStartRes();
115       end = sg.getEndRes();
116     }
117     else
118     {
119       originalSequences = ap.av.getAlignment().getSequencesArray();
120       start = 0;
121       end = ap.av.getAlignment().getWidth();
122     }
123
124     height = originalSequences.length;
125
126     redundancy = AlignSeq.computeRedundancyMatrix(originalSequences,
127             omitHidden, start, end, false);
128     label.setText(MessageManager.getString("label.enter_redundancy_threshold"));
129     slider.setVisible(true);
130     applyButton.setEnabled(true);
131     valueField.setVisible(true);
132
133     validate();
134     // System.out.println("blob done "+ (System.currentTimeMillis()-start));
135   }
136
137   void sliderValueChanged()
138   {
139     if (redundancy == null)
140     {
141       return;
142     }
143
144     float value = slider.getValue();
145
146     List<SequenceI> redundantSequences = new ArrayList<SequenceI>();
147     for (int i = 0; i < redundancy.length; i++)
148     {
149       if (value <= redundancy[i])
150       {
151         redundantSequences.add(originalSequences[i]);
152       }
153     }
154
155     ap.idPanel.idCanvas.setHighlighted(redundantSequences);
156     PaintRefresher.Refresh(this, ap.av.getSequenceSetId(), true, true);
157
158   }
159
160   public void applyButton_actionPerformed()
161   {
162     Vector del = new Vector();
163
164     undoButton.setEnabled(true);
165
166     float value = slider.getValue();
167     SequenceGroup sg = ap.av.getSelectionGroup();
168
169     for (int i = 0; i < redundancy.length; i++)
170     {
171       if (value <= redundancy[i])
172       {
173         del.addElement(originalSequences[i]);
174       }
175     }
176
177     // This has to be done before the restoreHistoryItem method of alignFrame
178     // will
179     // actually restore these sequences.
180     if (del.size() > 0)
181     {
182       SequenceI[] deleted = new SequenceI[del.size()];
183
184       int width = 0;
185       for (int i = 0; i < del.size(); i++)
186       {
187         deleted[i] = (SequenceI) del.elementAt(i);
188         if (deleted[i].getLength() > width)
189         {
190           width = deleted[i].getLength();
191         }
192       }
193
194       EditCommand cut = new EditCommand("Remove Redundancy",
195               EditCommand.CUT, deleted, 0, width, ap.av.getAlignment());
196       AlignmentI alignment = ap.av.getAlignment();
197       for (int i = 0; i < del.size(); i++)
198       {
199         alignment.deleteSequence(deleted[i]);
200         if (sg != null)
201         {
202           sg.deleteSequence(deleted[i], false);
203         }
204       }
205
206       historyList.push(cut);
207
208       ap.alignFrame.addHistoryItem(cut);
209
210       PaintRefresher.Refresh(this, ap.av.getSequenceSetId(), true, true);
211       // ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
212       // .getSequences());
213     }
214
215   }
216
217   public void undoButton_actionPerformed()
218   {
219     CommandI command = (CommandI) historyList.pop();
220     command.undoCommand(null);
221
222     if (ap.av.historyList.contains(command))
223     {
224       ap.av.historyList.removeElement(command);
225       ap.alignFrame.updateEditMenuBar();
226     }
227
228     ap.paintAlignment(true);
229
230     if (historyList.size() == 0)
231     {
232       undoButton.setEnabled(false);
233     }
234   }
235
236   public void valueField_actionPerformed(ActionEvent e)
237   {
238     try
239     {
240       int i = Integer.parseInt(valueField.getText());
241       slider.setValue(i);
242     } catch (Exception ex)
243     {
244       valueField.setText(slider.getValue() + "");
245     }
246   }
247
248   public void windowOpened(WindowEvent evt)
249   {
250   }
251
252   public void windowClosing(WindowEvent evt)
253   {
254     ap.idPanel.idCanvas.setHighlighted(null);
255   }
256
257   public void windowClosed(WindowEvent evt)
258   {
259   }
260
261   public void windowActivated(WindowEvent evt)
262   {
263   }
264
265   public void windowDeactivated(WindowEvent evt)
266   {
267   }
268
269   public void windowIconified(WindowEvent evt)
270   {
271   }
272
273   public void windowDeiconified(WindowEvent evt)
274   {
275   }
276 }