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