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