2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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;
23 import java.awt.event.*;
25 import jalview.analysis.*;
26 import jalview.datamodel.*;
27 import jalview.viewmodel.PCAModel;
29 public class PCAPanel extends EmbmenuFrame implements Runnable,
30 ActionListener, ItemListener
40 public PCAPanel(AlignViewport av)
50 for (int i = 1; i < 8; i++)
52 xCombobox.addItem("dim " + i);
53 yCombobox.addItem("dim " + i);
54 zCombobox.addItem("dim " + i);
58 AlignmentView seqstrings = av
59 .getAlignmentView(av.getSelectionGroup() != null);
60 boolean nucleotide = av.getAlignment().isNucleotide();
62 if (av.getSelectionGroup() == null)
64 seqs = av.getAlignment().getSequencesArray();
68 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
70 SeqCigar sq[] = seqstrings.getSequences();
71 int length = sq[0].getWidth();
73 for (int i = 0; i < seqs.length; i++)
75 if (sq[i].getWidth() != length)
78 .println("Sequences must be equal length for PCA analysis");
82 pcaModel = new PCAModel(seqstrings, seqs, nucleotide);
84 rc = new RotatableCanvas(av);
85 embedMenuIfNeeded(rc);
86 add(rc, BorderLayout.CENTER);
88 jalview.bin.JalviewLite.addFrame(this, "Principal component analysis",
91 Thread worker = new Thread(this);
100 // TODO progress indicator
101 calcSettings.setEnabled(false);
102 rc.setEnabled(false);
105 nuclSetting.setState(pcaModel.isNucleotide());
106 protSetting.setState(!pcaModel.isNucleotide());
113 pcaModel.updateRc(rc);
115 top = pcaModel.getTop();
116 } catch (OutOfMemoryError x)
118 System.err.println("Out of memory when calculating PCA.");
121 calcSettings.setEnabled(true);
123 // TODO revert progress indicator
129 void doDimensionChange()
136 int dim1 = top - xCombobox.getSelectedIndex();
137 int dim2 = top - yCombobox.getSelectedIndex();
138 int dim3 = top - zCombobox.getSelectedIndex();
139 pcaModel.updateRcView(dim1, dim2, dim3);
141 rc.rotmat.setIdentity();
143 rc.paint(rc.getGraphics());
146 public void actionPerformed(ActionEvent evt)
148 if (evt.getSource() == inputData)
152 if (evt.getSource() == resetButton)
159 if (evt.getSource() == values)
161 values_actionPerformed();
165 public void itemStateChanged(ItemEvent evt)
167 if (evt.getSource() == xCombobox)
169 xCombobox_actionPerformed();
171 else if (evt.getSource() == yCombobox)
173 yCombobox_actionPerformed();
175 else if (evt.getSource() == zCombobox)
177 zCombobox_actionPerformed();
179 else if (evt.getSource() == nuclSetting)
181 if (!pcaModel.isNucleotide())
183 pcaModel.setNucleotide(true);
184 new Thread(this).start();
187 else if (evt.getSource() == protSetting)
189 if (pcaModel.isNucleotide())
191 pcaModel.setNucleotide(false);
192 new Thread(this).start();
197 protected void xCombobox_actionPerformed()
202 protected void yCombobox_actionPerformed()
207 protected void zCombobox_actionPerformed()
212 public void values_actionPerformed()
215 CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
216 Frame frame = new Frame();
218 jalview.bin.JalviewLite.addFrame(frame, "PCA details", 500, 500);
220 cap.setText(pcaModel.getDetails());
223 void showOriginalData()
225 // decide if av alignment is sufficiently different to original data to
226 // warrant a new window to be created
227 // create new alignmnt window with hidden regions (unhiding hidden regions
228 // yields unaligned seqs)
229 // or create a selection box around columns in alignment view
230 // test Alignment(SeqCigar[])
234 // we try to get the associated view's gap character
235 // but this may fail if the view was closed...
236 gc = av.getGapCharacter();
237 } catch (Exception ex)
241 Object[] alAndColsel = pcaModel.getSeqtrings()
242 .getAlignmentAndColumnSelection(gc);
244 if (alAndColsel != null && alAndColsel[0] != null)
246 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
247 AlignFrame af = new AlignFrame(al, av.applet,
248 "Original Data for PCA", false);
250 af.viewport.setHiddenColumns((ColumnSelection) alAndColsel[1]);
254 public void labels_itemStateChanged(ItemEvent itemEvent)
256 rc.showLabels(labels.getState());
259 Panel jPanel2 = new Panel();
261 Label jLabel1 = new Label();
263 Label jLabel2 = new Label();
265 Label jLabel3 = new Label();
267 protected Choice xCombobox = new Choice();
269 protected Choice yCombobox = new Choice();
271 protected Choice zCombobox = new Choice();
273 protected Button resetButton = new Button();
275 FlowLayout flowLayout1 = new FlowLayout();
277 BorderLayout borderLayout1 = new BorderLayout();
279 MenuBar menuBar1 = new MenuBar();
281 Menu menu1 = new Menu();
283 Menu menu2 = new Menu();
285 Menu calcSettings = new Menu();
287 protected CheckboxMenuItem labels = new CheckboxMenuItem();
289 protected CheckboxMenuItem protSetting = new CheckboxMenuItem();
291 protected CheckboxMenuItem nuclSetting = new CheckboxMenuItem();
293 MenuItem values = new MenuItem();
295 MenuItem inputData = new MenuItem();
297 private void jbInit() throws Exception
299 this.setLayout(borderLayout1);
300 jPanel2.setLayout(flowLayout1);
301 jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
302 jLabel1.setText("x=");
303 jLabel2.setFont(new java.awt.Font("Verdana", 0, 12));
304 jLabel2.setText("y=");
305 jLabel3.setFont(new java.awt.Font("Verdana", 0, 12));
306 jLabel3.setText("z=");
307 jPanel2.setBackground(Color.white);
308 zCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
309 zCombobox.addItemListener(this);
310 yCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
311 yCombobox.addItemListener(this);
312 xCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
313 xCombobox.addItemListener(this);
314 resetButton.setFont(new java.awt.Font("Verdana", 0, 12));
315 resetButton.setLabel("Reset");
316 resetButton.addActionListener(this);
317 this.setMenuBar(menuBar1);
318 menu1.setLabel("File");
319 menu2.setLabel("View");
320 calcSettings.setLabel("Change Parameters");
321 labels.setLabel("Labels");
322 labels.addItemListener(this);
323 values.setLabel("Output Values...");
324 values.addActionListener(this);
325 inputData.setLabel("Input Data...");
326 nuclSetting.setLabel("Nucleotide matrix");
327 nuclSetting.addItemListener(this);
328 protSetting.setLabel("Protein matrix");
329 protSetting.addItemListener(this);
330 this.add(jPanel2, BorderLayout.SOUTH);
331 jPanel2.add(jLabel1, null);
332 jPanel2.add(xCombobox, null);
333 jPanel2.add(jLabel2, null);
334 jPanel2.add(yCombobox, null);
335 jPanel2.add(jLabel3, null);
336 jPanel2.add(zCombobox, null);
337 jPanel2.add(resetButton, null);
340 menuBar1.add(calcSettings);
343 menu1.add(inputData);
344 calcSettings.add(nuclSetting);
345 calcSettings.add(protSetting);
346 inputData.addActionListener(this);