08481d01c88b5dd3ccb2d9fc6321bd999ace6786
[jalview.git] / src / jalview / gui / 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.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
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.getAlignment());
135       start = sg.getStartRes();
136       end = sg.getEndRes();
137     }
138     else
139     {
140       originalSequences = ap.av.getAlignment().getSequencesArray();
141       start = 0;
142       end = ap.av.getAlignment().getWidth();
143     }
144
145     height = originalSequences.length;
146     if (ap.av.hasHiddenColumns())
147     {
148       omitHidden = ap.av.getViewAsString(sg != null);
149     }
150     redundancy = AlignSeq.computeRedundancyMatrix(originalSequences,
151             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         if (sg != null)
238         {
239           sg.deleteSequence(deleted[i], false);
240         }
241       }
242
243       historyList.push(cut);
244
245       ap.alignFrame.addHistoryItem(cut);
246
247       PaintRefresher.Refresh(this, ap.av.getSequenceSetId(), true, true);
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 }