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