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