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