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