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