2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 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
31 public class PCAPanel
\r
32 extends Frame implements Runnable, ActionListener, ItemListener
\r
41 public PCAPanel(AlignViewport av)
\r
49 e.printStackTrace();
\r
52 for (int i = 1; i < 8; i++)
\r
54 xCombobox.addItem("dim " + i);
\r
55 yCombobox.addItem("dim " + i);
\r
56 zCombobox.addItem("dim " + i);
\r
60 if (av.getSelectionGroup()!=null && av.getSelectionGroup().getSize(false) > 3)
\r
62 seqs = new Sequence[av.getSelectionGroup().getSize(false)];
\r
63 for (int i = 0; i < av.getSelectionGroup().getSize(false); i++)
\r
65 seqs[i] = av.getSelectionGroup().getSequenceAt(i);
\r
70 seqs = new Sequence[av.getAlignment().getHeight()];
\r
71 for (int i = 0; i < av.getAlignment().getHeight(); i++)
\r
73 seqs[i] = av.getAlignment().getSequenceAt(i);
\r
77 rc = new RotatableCanvas(av);
\r
78 add(rc, BorderLayout.CENTER);
\r
80 jalview.bin.JalviewLite.addFrame(this, "Principal component analysis",
\r
84 Thread worker = new Thread(this);
\r
93 pca = new PCA(seqs);
\r
96 // Now find the component coordinates
\r
99 while ((ii < seqs.length) && (seqs[ii] != null))
\r
104 double[][] comps = new double[ii][ii];
\r
106 for (int i = 0; i < ii; i++)
\r
108 if (pca.getEigenvalue(i) > 1e-4)
\r
110 comps[i] = pca.component(i);
\r
115 xCombobox.select(0);
\r
116 yCombobox.select(1);
\r
117 zCombobox.select(2);
\r
119 top = pca.getM().rows - 1;
\r
121 Vector points = new Vector();
\r
122 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
\r
124 for (int i = 0; i < pca.getM().rows; i++)
\r
126 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
\r
127 points.addElement(sp);
\r
130 rc.setPoints(points, pca.getM().rows);
\r
136 void doDimensionChange()
\r
143 int dim1 = top - xCombobox.getSelectedIndex();
\r
144 int dim2 = top - yCombobox.getSelectedIndex();
\r
145 int dim3 = top - zCombobox.getSelectedIndex();
\r
147 float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);
\r
148 for (int i = 0; i < pca.getM().rows; i++)
\r
150 ( (SequencePoint) rc.points.elementAt(i)).coord = scores[i];
\r
154 rc.rotmat.setIdentity();
\r
156 rc.paint(rc.getGraphics());
\r
159 public void actionPerformed(ActionEvent evt)
\r
161 values_actionPerformed();
\r
164 public void itemStateChanged(ItemEvent evt)
\r
166 if(evt.getSource()==xCombobox)
\r
167 xCombobox_actionPerformed();
\r
168 else if(evt.getSource()==yCombobox)
\r
169 yCombobox_actionPerformed();
\r
170 else if(evt.getSource()==zCombobox)
\r
171 zCombobox_actionPerformed();
\r
175 protected void xCombobox_actionPerformed()
\r
177 doDimensionChange();
\r
180 protected void yCombobox_actionPerformed()
\r
182 doDimensionChange();
\r
185 protected void zCombobox_actionPerformed()
\r
187 doDimensionChange();
\r
190 public void values_actionPerformed()
\r
193 CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
\r
194 Frame frame = new Frame();
\r
196 jalview.bin.JalviewLite.addFrame(frame, "PCA details", 500, 500);
\r
198 cap.setText(pca.getDetails());
\r
201 public void labels_itemStateChanged(ItemEvent itemEvent)
\r
203 rc.showLabels( labels.getState() );
\r
205 Panel jPanel2 = new Panel();
\r
206 Label jLabel1 = new Label();
\r
207 Label jLabel2 = new Label();
\r
208 Label jLabel3 = new Label();
\r
209 protected Choice xCombobox = new Choice();
\r
210 protected Choice yCombobox = new Choice();
\r
211 protected Choice zCombobox = new Choice();
\r
212 FlowLayout flowLayout1 = new FlowLayout();
\r
213 BorderLayout borderLayout1 = new BorderLayout();
\r
214 MenuBar menuBar1 = new MenuBar();
\r
215 Menu menu1 = new Menu();
\r
216 Menu menu2 = new Menu();
\r
217 protected CheckboxMenuItem labels = new CheckboxMenuItem();
\r
218 MenuItem values = new MenuItem();
\r
220 private void jbInit()
\r
223 this.setLayout(borderLayout1);
\r
224 jPanel2.setLayout(flowLayout1);
\r
225 jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
\r
226 jLabel1.setText("x=");
\r
227 jLabel2.setFont(new java.awt.Font("Verdana", 0, 12));
\r
228 jLabel2.setText("y=");
\r
229 jLabel3.setFont(new java.awt.Font("Verdana", 0, 12));
\r
230 jLabel3.setText("z=");
\r
231 jPanel2.setBackground(Color.white);
\r
232 zCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
\r
233 zCombobox.addItemListener(this);
\r
234 yCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
\r
235 yCombobox.addItemListener(this);
\r
236 xCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
\r
237 xCombobox.addItemListener(this);
\r
238 this.setMenuBar(menuBar1);
\r
239 menu1.setLabel("File");
\r
240 menu2.setLabel("View");
\r
241 labels.setLabel("Labels");
\r
242 labels.addItemListener(this);
\r
243 values.setLabel("Output Values...");
\r
244 values.addActionListener(this);
\r
245 this.add(jPanel2, BorderLayout.SOUTH);
\r
246 jPanel2.add(jLabel1, null);
\r
247 jPanel2.add(xCombobox, null);
\r
248 jPanel2.add(jLabel2, null);
\r
249 jPanel2.add(yCombobox, null);
\r
250 jPanel2.add(jLabel3, null);
\r
251 jPanel2.add(zCombobox, null);
\r
252 menuBar1.add(menu1);
\r
253 menuBar1.add(menu2);
\r