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