JAL-1503 update version in GPL header
[jalview.git] / src / jalview / gui / RedundancyPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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     sliderValueChanged();
166     // System.out.println((System.currentTimeMillis()-start));
167   }
168
169   void sliderValueChanged()
170   {
171     if (redundancy == null)
172     {
173       return;
174     }
175
176     float value = slider.getValue();
177     List<SequenceI> redundantSequences = new ArrayList<SequenceI>();
178     for (int i = 0; i < redundancy.length; i++)
179     {
180       if (value <= redundancy[i])
181       {
182         redundantSequences.add(originalSequences[i]);
183       }
184     }
185     ap.idPanel.idCanvas.setHighlighted(redundantSequences);
186   }
187
188   /**
189    * DOCUMENT ME!
190    * 
191    * @param e
192    *          DOCUMENT ME!
193    */
194   public void applyButton_actionPerformed(ActionEvent e)
195   {
196     Vector del = new Vector();
197
198     undoButton.setEnabled(true);
199
200     float value = slider.getValue();
201     SequenceGroup sg = ap.av.getSelectionGroup();
202
203     for (int i = 0; i < redundancy.length; i++)
204     {
205       if (value <= redundancy[i])
206       {
207         del.addElement(originalSequences[i]);
208       }
209     }
210
211     // This has to be done before the restoreHistoryItem method of alignFrame
212     // will
213     // actually restore these sequences.
214     if (del.size() > 0)
215     {
216       SequenceI[] deleted = new SequenceI[del.size()];
217
218       int width = 0;
219       for (int i = 0; i < del.size(); i++)
220       {
221         deleted[i] = (SequenceI) del.elementAt(i);
222         if (deleted[i].getLength() > width)
223         {
224           width = deleted[i].getLength();
225         }
226       }
227
228       EditCommand cut = new EditCommand("Remove Redundancy",
229               EditCommand.CUT, deleted, 0, width, ap.av.getAlignment());
230
231       for (int i = 0; i < del.size(); i++)
232       {
233         ap.av.getAlignment().deleteSequence(deleted[i]);
234         if (sg != null)
235         {
236           sg.deleteSequence(deleted[i], false);
237         }
238       }
239
240       historyList.push(cut);
241
242       ap.alignFrame.addHistoryItem(cut);
243
244       PaintRefresher.Refresh(this, ap.av.getSequenceSetId(), true, true);
245       // ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
246       // .getSequences());
247     }
248
249   }
250
251   /**
252    * DOCUMENT ME!
253    * 
254    * @param e
255    *          DOCUMENT ME!
256    */
257   public void undoButton_actionPerformed(ActionEvent e)
258   {
259     CommandI command = (CommandI) historyList.pop();
260     command.undoCommand(af.getViewAlignments());
261
262     if (ap.av.historyList.contains(command))
263     {
264       ap.av.historyList.remove(command);
265       af.updateEditMenuBar();
266     }
267
268     ap.paintAlignment(true);
269
270     if (historyList.size() == 0)
271     {
272       undoButton.setEnabled(false);
273     }
274   }
275
276   /**
277    * DOCUMENT ME!
278    * 
279    * @param e
280    *          DOCUMENT ME!
281    */
282   public void valueField_actionPerformed(ActionEvent e)
283   {
284     try
285     {
286       int i = Integer.parseInt(valueField.getText());
287       slider.setValue(i);
288     } catch (Exception ex)
289     {
290       valueField.setText(slider.getValue() + "");
291     }
292   }
293
294 }