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