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