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