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