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.*;
28 public class PCAPanel extends EmbmenuFrame implements Runnable,
29 ActionListener, ItemListener
41 AlignmentView seqstrings;
43 public PCAPanel(AlignViewport av)
53 for (int i = 1; i < 8; i++)
55 xCombobox.addItem("dim " + i);
56 yCombobox.addItem("dim " + i);
57 zCombobox.addItem("dim " + i);
61 seqstrings = av.getAlignmentView(av.getSelectionGroup() != null);
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");
83 rc = new RotatableCanvas(av);
84 embedMenuIfNeeded(rc);
85 add(rc, BorderLayout.CENTER);
87 jalview.bin.JalviewLite.addFrame(this, "Principal component analysis",
90 Thread worker = new Thread(this);
99 pca = new PCA(seqstrings.getSequenceStrings(' '));
102 // Now find the component coordinates
105 while ((ii < seqs.length) && (seqs[ii] != null))
110 double[][] comps = new double[ii][ii];
112 for (int i = 0; i < ii; i++)
114 if (pca.getEigenvalue(i) > 1e-4)
116 comps[i] = pca.component(i);
125 top = pca.getM().rows - 1;
127 Vector points = new Vector();
128 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
130 for (int i = 0; i < pca.getM().rows; i++)
132 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
133 points.addElement(sp);
136 rc.setPoints(points, pca.getM().rows);
142 void doDimensionChange()
149 int dim1 = top - xCombobox.getSelectedIndex();
150 int dim2 = top - yCombobox.getSelectedIndex();
151 int dim3 = top - zCombobox.getSelectedIndex();
153 float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);
154 for (int i = 0; i < pca.getM().rows; i++)
156 ((SequencePoint) rc.points.elementAt(i)).coord = scores[i];
160 rc.rotmat.setIdentity();
162 rc.paint(rc.getGraphics());
165 public void actionPerformed(ActionEvent evt)
167 if (evt.getSource() == inputData)
173 values_actionPerformed();
177 public void itemStateChanged(ItemEvent evt)
179 if (evt.getSource() == xCombobox)
181 xCombobox_actionPerformed();
183 else if (evt.getSource() == yCombobox)
185 yCombobox_actionPerformed();
187 else if (evt.getSource() == zCombobox)
189 zCombobox_actionPerformed();
193 protected void xCombobox_actionPerformed()
198 protected void yCombobox_actionPerformed()
203 protected void zCombobox_actionPerformed()
208 public void values_actionPerformed()
211 CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
212 Frame frame = new Frame();
214 jalview.bin.JalviewLite.addFrame(frame, "PCA details", 500, 500);
216 cap.setText(pca.getDetails());
219 void showOriginalData()
221 // decide if av alignment is sufficiently different to original data to
222 // warrant a new window to be created
223 // create new alignmnt window with hidden regions (unhiding hidden regions
224 // yields unaligned seqs)
225 // or create a selection box around columns in alignment view
226 // test Alignment(SeqCigar[])
230 // we try to get the associated view's gap character
231 // but this may fail if the view was closed...
232 gc = av.getGapCharacter();
233 } catch (Exception ex)
237 Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(gc);
239 if (alAndColsel != null && alAndColsel[0] != null)
241 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
242 AlignFrame af = new AlignFrame(al, av.applet,
243 "Original Data for PCA", false);
245 af.viewport.setHiddenColumns((ColumnSelection) alAndColsel[1]);
249 public void labels_itemStateChanged(ItemEvent itemEvent)
251 rc.showLabels(labels.getState());
254 Panel jPanel2 = new Panel();
256 Label jLabel1 = new Label();
258 Label jLabel2 = new Label();
260 Label jLabel3 = new Label();
262 protected Choice xCombobox = new Choice();
264 protected Choice yCombobox = new Choice();
266 protected Choice zCombobox = new Choice();
268 FlowLayout flowLayout1 = new FlowLayout();
270 BorderLayout borderLayout1 = new BorderLayout();
272 MenuBar menuBar1 = new MenuBar();
274 Menu menu1 = new Menu();
276 Menu menu2 = new Menu();
278 protected CheckboxMenuItem labels = new CheckboxMenuItem();
280 MenuItem values = new MenuItem();
282 MenuItem inputData = new MenuItem();
284 private void jbInit() throws Exception
286 this.setLayout(borderLayout1);
287 jPanel2.setLayout(flowLayout1);
288 jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
289 jLabel1.setText("x=");
290 jLabel2.setFont(new java.awt.Font("Verdana", 0, 12));
291 jLabel2.setText("y=");
292 jLabel3.setFont(new java.awt.Font("Verdana", 0, 12));
293 jLabel3.setText("z=");
294 jPanel2.setBackground(Color.white);
295 zCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
296 zCombobox.addItemListener(this);
297 yCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
298 yCombobox.addItemListener(this);
299 xCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
300 xCombobox.addItemListener(this);
301 this.setMenuBar(menuBar1);
302 menu1.setLabel("File");
303 menu2.setLabel("View");
304 labels.setLabel("Labels");
305 labels.addItemListener(this);
306 values.setLabel("Output Values...");
307 values.addActionListener(this);
308 inputData.setLabel("Input Data...");
309 this.add(jPanel2, BorderLayout.SOUTH);
310 jPanel2.add(jLabel1, null);
311 jPanel2.add(xCombobox, null);
312 jPanel2.add(jLabel2, null);
313 jPanel2.add(yCombobox, null);
314 jPanel2.add(jLabel3, null);
315 jPanel2.add(zCombobox, null);
320 menu1.add(inputData);
321 inputData.addActionListener(this);