2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
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.
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.
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
19 package jalview.appletgui;
24 import java.awt.event.*;
26 import jalview.analysis.*;
27 import jalview.datamodel.*;
29 public class PCAPanel extends EmbmenuFrame implements Runnable,
30 ActionListener, ItemListener
42 AlignmentView seqstrings;
44 public PCAPanel(AlignViewport av)
54 for (int i = 1; i < 8; i++)
56 xCombobox.addItem("dim " + i);
57 yCombobox.addItem("dim " + i);
58 zCombobox.addItem("dim " + i);
62 seqstrings = av.getAlignmentView(av.getSelectionGroup() != null);
63 if (av.getSelectionGroup() == null)
65 seqs = av.alignment.getSequencesArray();
69 seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment);
71 SeqCigar sq[] = seqstrings.getSequences();
72 int length = sq[0].getWidth();
74 for (int i = 0; i < seqs.length; i++)
76 if (sq[i].getWidth() != length)
79 .println("Sequences must be equal length for PCA analysis");
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 pca = new PCA(seqstrings.getSequenceStrings(' '));
103 // Now find the component coordinates
106 while ((ii < seqs.length) && (seqs[ii] != null))
111 double[][] comps = new double[ii][ii];
113 for (int i = 0; i < ii; i++)
115 if (pca.getEigenvalue(i) > 1e-4)
117 comps[i] = pca.component(i);
126 top = pca.getM().rows - 1;
128 Vector points = new Vector();
129 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
131 for (int i = 0; i < pca.getM().rows; i++)
133 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
134 points.addElement(sp);
137 rc.setPoints(points, pca.getM().rows);
143 void doDimensionChange()
150 int dim1 = top - xCombobox.getSelectedIndex();
151 int dim2 = top - yCombobox.getSelectedIndex();
152 int dim3 = top - zCombobox.getSelectedIndex();
154 float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);
155 for (int i = 0; i < pca.getM().rows; i++)
157 ((SequencePoint) rc.points.elementAt(i)).coord = scores[i];
161 rc.rotmat.setIdentity();
163 rc.paint(rc.getGraphics());
166 public void actionPerformed(ActionEvent evt)
168 if (evt.getSource() == inputData)
174 values_actionPerformed();
178 public void itemStateChanged(ItemEvent evt)
180 if (evt.getSource() == xCombobox)
182 xCombobox_actionPerformed();
184 else if (evt.getSource() == yCombobox)
186 yCombobox_actionPerformed();
188 else if (evt.getSource() == zCombobox)
190 zCombobox_actionPerformed();
194 protected void xCombobox_actionPerformed()
199 protected void yCombobox_actionPerformed()
204 protected void zCombobox_actionPerformed()
209 public void values_actionPerformed()
212 CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
213 Frame frame = new Frame();
215 jalview.bin.JalviewLite.addFrame(frame, "PCA details", 500, 500);
217 cap.setText(pca.getDetails());
220 void showOriginalData()
222 // decide if av alignment is sufficiently different to original data to
223 // warrant a new window to be created
224 // create new alignmnt window with hidden regions (unhiding hidden regions
225 // yields unaligned seqs)
226 // or create a selection box around columns in alignment view
227 // test Alignment(SeqCigar[])
231 // we try to get the associated view's gap character
232 // but this may fail if the view was closed...
233 gc = av.getGapCharacter();
234 } catch (Exception ex)
238 Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(gc);
240 if (alAndColsel != null && alAndColsel[0] != null)
242 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
243 AlignFrame af = new AlignFrame(al, av.applet,
244 "Original Data for PCA", false);
246 af.viewport.setHiddenColumns((ColumnSelection) alAndColsel[1]);
250 public void labels_itemStateChanged(ItemEvent itemEvent)
252 rc.showLabels(labels.getState());
255 Panel jPanel2 = new Panel();
257 Label jLabel1 = new Label();
259 Label jLabel2 = new Label();
261 Label jLabel3 = new Label();
263 protected Choice xCombobox = new Choice();
265 protected Choice yCombobox = new Choice();
267 protected Choice zCombobox = new Choice();
269 FlowLayout flowLayout1 = new FlowLayout();
271 BorderLayout borderLayout1 = new BorderLayout();
273 MenuBar menuBar1 = new MenuBar();
275 Menu menu1 = new Menu();
277 Menu menu2 = new Menu();
279 protected CheckboxMenuItem labels = new CheckboxMenuItem();
281 MenuItem values = new MenuItem();
283 MenuItem inputData = new MenuItem();
285 private void jbInit() throws Exception
287 this.setLayout(borderLayout1);
288 jPanel2.setLayout(flowLayout1);
289 jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
290 jLabel1.setText("x=");
291 jLabel2.setFont(new java.awt.Font("Verdana", 0, 12));
292 jLabel2.setText("y=");
293 jLabel3.setFont(new java.awt.Font("Verdana", 0, 12));
294 jLabel3.setText("z=");
295 jPanel2.setBackground(Color.white);
296 zCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
297 zCombobox.addItemListener(this);
298 yCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
299 yCombobox.addItemListener(this);
300 xCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
301 xCombobox.addItemListener(this);
302 this.setMenuBar(menuBar1);
303 menu1.setLabel("File");
304 menu2.setLabel("View");
305 labels.setLabel("Labels");
306 labels.addItemListener(this);
307 values.setLabel("Output Values...");
308 values.addActionListener(this);
309 inputData.setLabel("Input Data...");
310 this.add(jPanel2, BorderLayout.SOUTH);
311 jPanel2.add(jLabel1, null);
312 jPanel2.add(xCombobox, null);
313 jPanel2.add(jLabel2, null);
314 jPanel2.add(yCombobox, null);
315 jPanel2.add(jLabel3, null);
316 jPanel2.add(zCombobox, null);
321 menu1.add(inputData);
322 inputData.addActionListener(this);