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