2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
20 package jalview.appletgui;
\r
25 import java.awt.event.*;
\r
27 import jalview.analysis.*;
\r
28 import jalview.datamodel.*;
\r
30 public class PCAPanel
\r
31 extends Frame implements Runnable, ActionListener, ItemListener
\r
38 AlignmentView seqstrings;
\r
40 public PCAPanel(AlignViewport av)
\r
48 e.printStackTrace();
\r
51 for (int i = 1; i < 8; i++)
\r
53 xCombobox.addItem("dim " + i);
\r
54 yCombobox.addItem("dim " + i);
\r
55 zCombobox.addItem("dim " + i);
\r
59 seqstrings = av.getAlignmentView(av.getSelectionGroup() != null);
\r
60 if (av.getSelectionGroup() == null)
\r
62 seqs = av.alignment.getSequencesArray();
\r
66 seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment);
\r
68 SeqCigar sq[] = seqstrings.getSequences();
\r
69 int length = sq[0].getWidth();
\r
71 for (int i = 0; i < seqs.length; i++)
\r
73 if (sq[i].getWidth() != length)
\r
75 System.out.println("Sequences must be equal length for PCA analysis");
\r
80 rc = new RotatableCanvas(av);
\r
81 add(rc, BorderLayout.CENTER);
\r
83 jalview.bin.JalviewLite.addFrame(this, "Principal component analysis",
\r
86 Thread worker = new Thread(this);
\r
95 pca = new PCA(seqstrings.getSequenceStrings(' '));
\r
98 // Now find the component coordinates
\r
101 while ( (ii < seqs.length) && (seqs[ii] != null))
\r
106 double[][] comps = new double[ii][ii];
\r
108 for (int i = 0; i < ii; i++)
\r
110 if (pca.getEigenvalue(i) > 1e-4)
\r
112 comps[i] = pca.component(i);
\r
117 xCombobox.select(0);
\r
118 yCombobox.select(1);
\r
119 zCombobox.select(2);
\r
121 top = pca.getM().rows - 1;
\r
123 Vector points = new Vector();
\r
124 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
\r
126 for (int i = 0; i < pca.getM().rows; i++)
\r
128 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
\r
129 points.addElement(sp);
\r
132 rc.setPoints(points, pca.getM().rows);
\r
138 void doDimensionChange()
\r
145 int dim1 = top - xCombobox.getSelectedIndex();
\r
146 int dim2 = top - yCombobox.getSelectedIndex();
\r
147 int dim3 = top - zCombobox.getSelectedIndex();
\r
149 float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);
\r
150 for (int i = 0; i < pca.getM().rows; i++)
\r
152 ( (SequencePoint) rc.points.elementAt(i)).coord = scores[i];
\r
156 rc.rotmat.setIdentity();
\r
158 rc.paint(rc.getGraphics());
\r
161 public void actionPerformed(ActionEvent evt)
\r
163 if (evt.getSource() == inputData)
\r
165 showOriginalData();
\r
169 values_actionPerformed();
\r
173 public void itemStateChanged(ItemEvent evt)
\r
175 if (evt.getSource() == xCombobox)
\r
177 xCombobox_actionPerformed();
\r
179 else if (evt.getSource() == yCombobox)
\r
181 yCombobox_actionPerformed();
\r
183 else if (evt.getSource() == zCombobox)
\r
185 zCombobox_actionPerformed();
\r
189 protected void xCombobox_actionPerformed()
\r
191 doDimensionChange();
\r
194 protected void yCombobox_actionPerformed()
\r
196 doDimensionChange();
\r
199 protected void zCombobox_actionPerformed()
\r
201 doDimensionChange();
\r
204 public void values_actionPerformed()
\r
207 CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
\r
208 Frame frame = new Frame();
\r
210 jalview.bin.JalviewLite.addFrame(frame, "PCA details", 500, 500);
\r
212 cap.setText(pca.getDetails());
\r
215 void showOriginalData()
\r
217 // decide if av alignment is sufficiently different to original data to warrant a new window to be created
\r
218 // create new alignmnt window with hidden regions (unhiding hidden regions yields unaligned seqs)
\r
219 // or create a selection box around columns in alignment view
\r
220 // test Alignment(SeqCigar[])
\r
221 Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(av.
\r
222 getGapCharacter());
\r
224 if (alAndColsel != null && alAndColsel[0] != null)
\r
226 Alignment al = new Alignment( (SequenceI[]) alAndColsel[0]);
\r
227 AlignFrame af = new AlignFrame(al,
\r
229 "Original Data for PCA",
\r
232 af.viewport.setHiddenColumns( (ColumnSelection) alAndColsel[1]);
\r
236 public void labels_itemStateChanged(ItemEvent itemEvent)
\r
238 rc.showLabels(labels.getState());
\r
241 Panel jPanel2 = new Panel();
\r
242 Label jLabel1 = new Label();
\r
243 Label jLabel2 = new Label();
\r
244 Label jLabel3 = new Label();
\r
245 protected Choice xCombobox = new Choice();
\r
246 protected Choice yCombobox = new Choice();
\r
247 protected Choice zCombobox = new Choice();
\r
248 FlowLayout flowLayout1 = new FlowLayout();
\r
249 BorderLayout borderLayout1 = new BorderLayout();
\r
250 MenuBar menuBar1 = new MenuBar();
\r
251 Menu menu1 = new Menu();
\r
252 Menu menu2 = new Menu();
\r
253 protected CheckboxMenuItem labels = new CheckboxMenuItem();
\r
254 MenuItem values = new MenuItem();
\r
255 MenuItem inputData = new MenuItem();
\r
257 private void jbInit()
\r
260 this.setLayout(borderLayout1);
\r
261 jPanel2.setLayout(flowLayout1);
\r
262 jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
\r
263 jLabel1.setText("x=");
\r
264 jLabel2.setFont(new java.awt.Font("Verdana", 0, 12));
\r
265 jLabel2.setText("y=");
\r
266 jLabel3.setFont(new java.awt.Font("Verdana", 0, 12));
\r
267 jLabel3.setText("z=");
\r
268 jPanel2.setBackground(Color.white);
\r
269 zCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
\r
270 zCombobox.addItemListener(this);
\r
271 yCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
\r
272 yCombobox.addItemListener(this);
\r
273 xCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
\r
274 xCombobox.addItemListener(this);
\r
275 this.setMenuBar(menuBar1);
\r
276 menu1.setLabel("File");
\r
277 menu2.setLabel("View");
\r
278 labels.setLabel("Labels");
\r
279 labels.addItemListener(this);
\r
280 values.setLabel("Output Values...");
\r
281 values.addActionListener(this);
\r
282 inputData.setLabel("Input Data...");
\r
283 this.add(jPanel2, BorderLayout.SOUTH);
\r
284 jPanel2.add(jLabel1, null);
\r
285 jPanel2.add(xCombobox, null);
\r
286 jPanel2.add(jLabel2, null);
\r
287 jPanel2.add(yCombobox, null);
\r
288 jPanel2.add(jLabel3, null);
\r
289 jPanel2.add(zCombobox, null);
\r
290 menuBar1.add(menu1);
\r
291 menuBar1.add(menu2);
\r
294 menu1.add(inputData);
\r
295 inputData.addActionListener(this);
\r