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