2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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.
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.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.appletgui;
21 import java.awt.event.*;
23 import jalview.datamodel.*;
24 import jalview.viewmodel.PCAModel;
26 public class PCAPanel extends EmbmenuFrame implements Runnable,
27 ActionListener, ItemListener
37 public PCAPanel(AlignViewport av)
47 for (int i = 1; i < 8; i++)
49 xCombobox.addItem("dim " + i);
50 yCombobox.addItem("dim " + i);
51 zCombobox.addItem("dim " + i);
55 boolean selected = av.getSelectionGroup() != null
56 && av.getSelectionGroup().getSize() > 0;
57 AlignmentView seqstrings = av.getAlignmentView(selected);
58 boolean nucleotide = av.getAlignment().isNucleotide();
62 seqs = av.getAlignment().getSequencesArray();
66 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
68 SeqCigar sq[] = seqstrings.getSequences();
69 int length = sq[0].getWidth();
71 for (int i = 0; i < seqs.length; i++)
73 if (sq[i].getWidth() != length)
76 .println("Sequences must be equal length for PCA analysis");
80 pcaModel = new PCAModel(seqstrings, seqs, nucleotide);
82 rc = new RotatableCanvas(av);
83 embedMenuIfNeeded(rc);
84 add(rc, BorderLayout.CENTER);
86 jalview.bin.JalviewLite.addFrame(this, "Principal component analysis",
89 Thread worker = new Thread(this);
98 // TODO progress indicator
99 calcSettings.setEnabled(false);
100 rc.setEnabled(false);
103 nuclSetting.setState(pcaModel.isNucleotide());
104 protSetting.setState(!pcaModel.isNucleotide());
111 pcaModel.updateRc(rc);
113 top = pcaModel.getTop();
114 } catch (OutOfMemoryError x)
116 System.err.println("Out of memory when calculating PCA.");
119 calcSettings.setEnabled(true);
121 // TODO revert progress indicator
127 void doDimensionChange()
134 int dim1 = top - xCombobox.getSelectedIndex();
135 int dim2 = top - yCombobox.getSelectedIndex();
136 int dim3 = top - zCombobox.getSelectedIndex();
137 pcaModel.updateRcView(dim1, dim2, dim3);
139 rc.rotmat.setIdentity();
141 rc.paint(rc.getGraphics());
144 public void actionPerformed(ActionEvent evt)
146 if (evt.getSource() == inputData)
150 if (evt.getSource() == resetButton)
157 if (evt.getSource() == values)
159 values_actionPerformed();
163 public void itemStateChanged(ItemEvent evt)
165 if (evt.getSource() == xCombobox)
167 xCombobox_actionPerformed();
169 else if (evt.getSource() == yCombobox)
171 yCombobox_actionPerformed();
173 else if (evt.getSource() == zCombobox)
175 zCombobox_actionPerformed();
177 else if (evt.getSource() == labels)
179 labels_itemStateChanged(evt);
181 else if (evt.getSource() == nuclSetting)
183 if (!pcaModel.isNucleotide())
185 pcaModel.setNucleotide(true);
186 new Thread(this).start();
189 else if (evt.getSource() == protSetting)
191 if (pcaModel.isNucleotide())
193 pcaModel.setNucleotide(false);
194 new Thread(this).start();
199 protected void xCombobox_actionPerformed()
204 protected void yCombobox_actionPerformed()
209 protected void zCombobox_actionPerformed()
214 public void values_actionPerformed()
217 CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
218 Frame frame = new Frame();
220 jalview.bin.JalviewLite.addFrame(frame, "PCA details", 500, 500);
222 cap.setText(pcaModel.getDetails());
225 void showOriginalData()
227 // decide if av alignment is sufficiently different to original data to
228 // warrant a new window to be created
229 // create new alignmnt window with hidden regions (unhiding hidden regions
230 // yields unaligned seqs)
231 // or create a selection box around columns in alignment view
232 // test Alignment(SeqCigar[])
236 // we try to get the associated view's gap character
237 // but this may fail if the view was closed...
238 gc = av.getGapCharacter();
239 } catch (Exception ex)
243 Object[] alAndColsel = pcaModel.getSeqtrings()
244 .getAlignmentAndColumnSelection(gc);
246 if (alAndColsel != null && alAndColsel[0] != null)
248 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
249 AlignFrame af = new AlignFrame(al, av.applet,
250 "Original Data for PCA", false);
252 af.viewport.setHiddenColumns((ColumnSelection) alAndColsel[1]);
256 public void labels_itemStateChanged(ItemEvent itemEvent)
258 rc.showLabels(labels.getState());
261 Panel jPanel2 = new Panel();
263 Label jLabel1 = new Label();
265 Label jLabel2 = new Label();
267 Label jLabel3 = new Label();
269 protected Choice xCombobox = new Choice();
271 protected Choice yCombobox = new Choice();
273 protected Choice zCombobox = new Choice();
275 protected Button resetButton = new Button();
277 FlowLayout flowLayout1 = new FlowLayout();
279 BorderLayout borderLayout1 = new BorderLayout();
281 MenuBar menuBar1 = new MenuBar();
283 Menu menu1 = new Menu();
285 Menu menu2 = new Menu();
287 Menu calcSettings = new Menu();
289 protected CheckboxMenuItem labels = new CheckboxMenuItem();
291 protected CheckboxMenuItem protSetting = new CheckboxMenuItem();
293 protected CheckboxMenuItem nuclSetting = new CheckboxMenuItem();
295 MenuItem values = new MenuItem();
297 MenuItem inputData = new MenuItem();
299 private void jbInit() throws Exception
301 this.setLayout(borderLayout1);
302 jPanel2.setLayout(flowLayout1);
303 jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
304 jLabel1.setText("x=");
305 jLabel2.setFont(new java.awt.Font("Verdana", 0, 12));
306 jLabel2.setText("y=");
307 jLabel3.setFont(new java.awt.Font("Verdana", 0, 12));
308 jLabel3.setText("z=");
309 jPanel2.setBackground(Color.white);
310 zCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
311 zCombobox.addItemListener(this);
312 yCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
313 yCombobox.addItemListener(this);
314 xCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
315 xCombobox.addItemListener(this);
316 resetButton.setFont(new java.awt.Font("Verdana", 0, 12));
317 resetButton.setLabel("Reset");
318 resetButton.addActionListener(this);
319 this.setMenuBar(menuBar1);
320 menu1.setLabel("File");
321 menu2.setLabel("View");
322 calcSettings.setLabel("Change Parameters");
323 labels.setLabel("Labels");
324 labels.addItemListener(this);
325 values.setLabel("Output Values...");
326 values.addActionListener(this);
327 inputData.setLabel("Input Data...");
328 nuclSetting.setLabel("Nucleotide matrix");
329 nuclSetting.addItemListener(this);
330 protSetting.setLabel("Protein matrix");
331 protSetting.addItemListener(this);
332 this.add(jPanel2, BorderLayout.SOUTH);
333 jPanel2.add(jLabel1, null);
334 jPanel2.add(xCombobox, null);
335 jPanel2.add(jLabel2, null);
336 jPanel2.add(yCombobox, null);
337 jPanel2.add(jLabel3, null);
338 jPanel2.add(zCombobox, null);
339 jPanel2.add(resetButton, null);
342 menuBar1.add(calcSettings);
345 menu1.add(inputData);
346 calcSettings.add(nuclSetting);
347 calcSettings.add(protSetting);
348 inputData.addActionListener(this);