merge from 2_4_Release branch
[jalview.git] / src / jalview / gui / RedundancyPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
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.
9  * 
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.
14  * 
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
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.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.alignment);
136       start = sg.getStartRes();
137       end = sg.getEndRes();
138     }
139     else
140     {
141       originalSequences = ap.av.alignment.getSequencesArray();
142       start = 0;
143       end = ap.av.alignment.getWidth();
144     }
145
146     height = originalSequences.length;
147
148     redundancy = new float[height];
149     for (int i = 0; i < height; i++)
150     {
151       redundancy[i] = 0f;
152     }
153
154     if (ap.av.hasHiddenColumns)
155     {
156       omitHidden = ap.av.getViewAsString(sg != null);
157     }
158
159     // long start = System.currentTimeMillis();
160
161     float pid;
162     String seqi, seqj;
163     for (int i = 0; i < height; i++)
164     {
165
166       for (int j = 0; j < i; j++)
167       {
168         if (i == j)
169         {
170           continue;
171         }
172
173         if (omitHidden == null)
174         {
175           seqi = originalSequences[i].getSequenceAsString(start, end);
176           seqj = originalSequences[j].getSequenceAsString(start, end);
177         }
178         else
179         {
180           seqi = omitHidden[i];
181           seqj = omitHidden[j];
182         }
183
184         pid = Comparison.PID(seqi, seqj);
185
186         if (seqj.length() < seqi.length())
187         {
188           redundancy[j] = Math.max(pid, redundancy[j]);
189         }
190         else
191         {
192           redundancy[i] = Math.max(pid, redundancy[i]);
193         }
194
195       }
196     }
197
198     progress.setIndeterminate(false);
199     progress.setVisible(false);
200     progress = null;
201
202     label.setText("Enter the redundancy threshold");
203     slider.setVisible(true);
204     applyButton.setEnabled(true);
205     valueField.setVisible(true);
206
207     validate();
208     // System.out.println((System.currentTimeMillis()-start));
209   }
210
211   void sliderValueChanged()
212   {
213     if (redundancy == null)
214     {
215       return;
216     }
217
218     float value = slider.getValue();
219
220     for (int i = 0; i < redundancy.length; i++)
221     {
222       if (value > redundancy[i])
223       {
224         redundantSeqs.remove(originalSequences[i]);
225       }
226       else if (!redundantSeqs.contains(originalSequences[i]))
227       {
228         redundantSeqs.add(originalSequences[i]);
229       }
230
231     }
232
233     ap.idPanel.idCanvas.setHighlighted(redundantSeqs);
234   }
235
236   /**
237    * DOCUMENT ME!
238    * 
239    * @param e
240    *                DOCUMENT ME!
241    */
242   public void applyButton_actionPerformed(ActionEvent e)
243   {
244     Vector del = new Vector();
245
246     undoButton.setEnabled(true);
247
248     float value = slider.getValue();
249     SequenceGroup sg = ap.av.getSelectionGroup();
250
251     for (int i = 0; i < redundancy.length; i++)
252     {
253       if (value <= redundancy[i])
254       {
255         del.addElement(originalSequences[i]);
256       }
257     }
258
259     // This has to be done before the restoreHistoryItem method of alignFrame
260     // will
261     // actually restore these sequences.
262     if (del.size() > 0)
263     {
264       SequenceI[] deleted = new SequenceI[del.size()];
265
266       int width = 0;
267       for (int i = 0; i < del.size(); i++)
268       {
269         deleted[i] = (SequenceI) del.elementAt(i);
270         if (deleted[i].getLength() > width)
271         {
272           width = deleted[i].getLength();
273         }
274       }
275
276       EditCommand cut = new EditCommand("Remove Redundancy",
277               EditCommand.CUT, deleted, 0, width, ap.av.alignment);
278
279       for (int i = 0; i < del.size(); i++)
280       {
281         ap.av.alignment.deleteSequence(deleted[i]);
282         PaintRefresher.Refresh(this, ap.av.getSequenceSetId(), true, true);
283         if (sg != null)
284         {
285           sg.deleteSequence(deleted[i], false);
286         }
287       }
288
289       historyList.push(cut);
290
291       ap.alignFrame.addHistoryItem(cut);
292
293       ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
294               .getSequences());
295     }
296
297   }
298
299   /**
300    * DOCUMENT ME!
301    * 
302    * @param e
303    *                DOCUMENT ME!
304    */
305   public void undoButton_actionPerformed(ActionEvent e)
306   {
307     CommandI command = (CommandI) historyList.pop();
308     command.undoCommand(af.getViewAlignments());
309
310     if (ap.av.historyList.contains(command))
311     {
312       ap.av.historyList.remove(command);
313       af.updateEditMenuBar();
314     }
315
316     ap.paintAlignment(true);
317
318     if (historyList.size() == 0)
319     {
320       undoButton.setEnabled(false);
321     }
322   }
323
324   /**
325    * DOCUMENT ME!
326    * 
327    * @param e
328    *                DOCUMENT ME!
329    */
330   public void valueField_actionPerformed(ActionEvent e)
331   {
332     try
333     {
334       int i = Integer.parseInt(valueField.getText());
335       slider.setValue(i);
336     } catch (Exception ex)
337     {
338       valueField.setText(slider.getValue() + "");
339     }
340   }
341
342 }