5adfae98bf14a30531d587ae0e84e99a9d1988a8
[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
92             .getString("label.redundancy_threshold_selection"), 400, 100,
93             false);
94     frame.addInternalFrameListener(new InternalFrameAdapter()
95     {
96       public void internalFrameClosing(InternalFrameEvent evt)
97       {
98         ap.idPanel.idCanvas.setHighlighted(null);
99       }
100     });
101
102   }
103
104   /**
105    * This is a copy of remove redundancy in jalivew.datamodel.Alignment except
106    * we dont want to remove redundancy, just calculate once so we can use the
107    * slider to dynamically hide redundant sequences
108    * 
109    * @param threshold
110    *          DOCUMENT ME!
111    * @param sel
112    *          DOCUMENT ME!
113    * 
114    * @return DOCUMENT ME!
115    */
116   public void run()
117   {
118     JProgressBar progress = new JProgressBar();
119     progress.setIndeterminate(true);
120     southPanel.add(progress, java.awt.BorderLayout.SOUTH);
121
122     label.setText(MessageManager.getString("label.calculating"));
123
124     slider.setVisible(false);
125     applyButton.setEnabled(false);
126     valueField.setVisible(false);
127
128     validate();
129
130     String[] omitHidden = null;
131
132     SequenceGroup sg = ap.av.getSelectionGroup();
133     int height;
134
135     int start, end;
136
137     if ((sg != null) && (sg.getSize() >= 1))
138     {
139       originalSequences = sg.getSequencesInOrder(ap.av.getAlignment());
140       start = sg.getStartRes();
141       end = sg.getEndRes();
142     }
143     else
144     {
145       originalSequences = ap.av.getAlignment().getSequencesArray();
146       start = 0;
147       end = ap.av.getAlignment().getWidth();
148     }
149
150     height = originalSequences.length;
151     if (ap.av.hasHiddenColumns())
152     {
153       omitHidden = ap.av.getViewAsString(sg != null);
154     }
155     redundancy = AlignSeq.computeRedundancyMatrix(originalSequences,
156             omitHidden, start, end, false);
157
158     progress.setIndeterminate(false);
159     progress.setVisible(false);
160     progress = null;
161
162     label.setText("Enter the redundancy threshold");
163     slider.setVisible(true);
164     applyButton.setEnabled(true);
165     valueField.setVisible(true);
166
167     validate();
168     sliderValueChanged();
169     // System.out.println((System.currentTimeMillis()-start));
170   }
171
172   void sliderValueChanged()
173   {
174     if (redundancy == null)
175     {
176       return;
177     }
178
179     float value = slider.getValue();
180     List<SequenceI> redundantSequences = new ArrayList<SequenceI>();
181     for (int i = 0; i < redundancy.length; i++)
182     {
183       if (value <= redundancy[i])
184       {
185         redundantSequences.add(originalSequences[i]);
186       }
187     }
188     ap.idPanel.idCanvas.setHighlighted(redundantSequences);
189   }
190
191   /**
192    * DOCUMENT ME!
193    * 
194    * @param e
195    *          DOCUMENT ME!
196    */
197   public void applyButton_actionPerformed(ActionEvent e)
198   {
199     Vector del = new Vector();
200
201     undoButton.setEnabled(true);
202
203     float value = slider.getValue();
204     SequenceGroup sg = ap.av.getSelectionGroup();
205
206     for (int i = 0; i < redundancy.length; i++)
207     {
208       if (value <= redundancy[i])
209       {
210         del.addElement(originalSequences[i]);
211       }
212     }
213
214     // This has to be done before the restoreHistoryItem method of alignFrame
215     // will
216     // actually restore these sequences.
217     if (del.size() > 0)
218     {
219       SequenceI[] deleted = new SequenceI[del.size()];
220
221       int width = 0;
222       for (int i = 0; i < del.size(); i++)
223       {
224         deleted[i] = (SequenceI) del.elementAt(i);
225         if (deleted[i].getLength() > width)
226         {
227           width = deleted[i].getLength();
228         }
229       }
230
231       EditCommand cut = new EditCommand("Remove Redundancy",
232               EditCommand.CUT, deleted, 0, width, ap.av.getAlignment());
233
234       for (int i = 0; i < del.size(); i++)
235       {
236         ap.av.getAlignment().deleteSequence(deleted[i]);
237         if (sg != null)
238         {
239           sg.deleteSequence(deleted[i], false);
240         }
241       }
242
243       historyList.push(cut);
244
245       ap.alignFrame.addHistoryItem(cut);
246
247       PaintRefresher.Refresh(this, ap.av.getSequenceSetId(), true, true);
248       // ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
249       // .getSequences());
250     }
251
252   }
253
254   /**
255    * DOCUMENT ME!
256    * 
257    * @param e
258    *          DOCUMENT ME!
259    */
260   public void undoButton_actionPerformed(ActionEvent e)
261   {
262     CommandI command = (CommandI) historyList.pop();
263     command.undoCommand(af.getViewAlignments());
264
265     if (ap.av.historyList.contains(command))
266     {
267       ap.av.historyList.remove(command);
268       af.updateEditMenuBar();
269     }
270
271     ap.paintAlignment(true);
272
273     if (historyList.size() == 0)
274     {
275       undoButton.setEnabled(false);
276     }
277   }
278
279   /**
280    * DOCUMENT ME!
281    * 
282    * @param e
283    *          DOCUMENT ME!
284    */
285   public void valueField_actionPerformed(ActionEvent e)
286   {
287     try
288     {
289       int i = Integer.parseInt(valueField.getText());
290       slider.setValue(i);
291     } catch (Exception ex)
292     {
293       valueField.setText(slider.getValue() + "");
294     }
295   }
296
297 }