2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.appletgui;
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentView;
25 import jalview.datamodel.ColumnSelection;
26 import jalview.datamodel.SeqCigar;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.MessageManager;
29 import jalview.viewmodel.PCAModel;
31 import java.awt.BorderLayout;
32 import java.awt.Button;
33 import java.awt.CheckboxMenuItem;
34 import java.awt.Choice;
35 import java.awt.Color;
36 import java.awt.FlowLayout;
37 import java.awt.Frame;
38 import java.awt.Label;
40 import java.awt.MenuBar;
41 import java.awt.MenuItem;
42 import java.awt.Panel;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.ActionListener;
45 import java.awt.event.ItemEvent;
46 import java.awt.event.ItemListener;
48 public class PCAPanel extends EmbmenuFrame implements Runnable,
49 ActionListener, ItemListener
59 public PCAPanel(AlignViewport av)
69 for (int i = 1; i < 8; i++)
71 xCombobox.addItem("dim " + i);
72 yCombobox.addItem("dim " + i);
73 zCombobox.addItem("dim " + i);
77 boolean selected = av.getSelectionGroup() != null
78 && av.getSelectionGroup().getSize() > 0;
79 AlignmentView seqstrings = av.getAlignmentView(selected);
80 boolean nucleotide = av.getAlignment().isNucleotide();
84 seqs = av.getAlignment().getSequencesArray();
88 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
90 SeqCigar sq[] = seqstrings.getSequences();
91 int length = sq[0].getWidth();
93 for (int i = 0; i < seqs.length; i++)
95 if (sq[i].getWidth() != length)
98 .println("Sequences must be equal length for PCA analysis");
102 pcaModel = new PCAModel(seqstrings, seqs, nucleotide);
104 rc = new RotatableCanvas(av);
105 embedMenuIfNeeded(rc);
106 add(rc, BorderLayout.CENTER);
108 jalview.bin.JalviewLite.addFrame(this,
109 MessageManager.getString("label.principal_component_analysis"),
112 Thread worker = new Thread(this);
121 // TODO progress indicator
122 calcSettings.setEnabled(false);
123 rc.setEnabled(false);
126 nuclSetting.setState(pcaModel.isNucleotide());
127 protSetting.setState(!pcaModel.isNucleotide());
134 pcaModel.updateRc(rc);
136 top = pcaModel.getTop();
137 } catch (OutOfMemoryError x)
139 System.err.println("Out of memory when calculating PCA.");
142 calcSettings.setEnabled(true);
144 // TODO revert progress indicator
150 void doDimensionChange()
157 int dim1 = top - xCombobox.getSelectedIndex();
158 int dim2 = top - yCombobox.getSelectedIndex();
159 int dim3 = top - zCombobox.getSelectedIndex();
160 pcaModel.updateRcView(dim1, dim2, dim3);
162 rc.rotmat.setIdentity();
164 rc.paint(rc.getGraphics());
167 public void actionPerformed(ActionEvent evt)
169 if (evt.getSource() == inputData)
173 if (evt.getSource() == resetButton)
180 if (evt.getSource() == values)
182 values_actionPerformed();
186 public void itemStateChanged(ItemEvent evt)
188 if (evt.getSource() == xCombobox)
190 xCombobox_actionPerformed();
192 else if (evt.getSource() == yCombobox)
194 yCombobox_actionPerformed();
196 else if (evt.getSource() == zCombobox)
198 zCombobox_actionPerformed();
200 else if (evt.getSource() == labels)
202 labels_itemStateChanged(evt);
204 else if (evt.getSource() == nuclSetting)
206 if (!pcaModel.isNucleotide())
208 pcaModel.setNucleotide(true);
209 new Thread(this).start();
212 else if (evt.getSource() == protSetting)
214 if (pcaModel.isNucleotide())
216 pcaModel.setNucleotide(false);
217 new Thread(this).start();
222 protected void xCombobox_actionPerformed()
227 protected void yCombobox_actionPerformed()
232 protected void zCombobox_actionPerformed()
237 public void values_actionPerformed()
240 CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
241 Frame frame = new Frame();
243 jalview.bin.JalviewLite.addFrame(frame,
244 MessageManager.getString("label.pca_details"), 500, 500);
246 cap.setText(pcaModel.getDetails());
249 void showOriginalData()
251 // decide if av alignment is sufficiently different to original data to
252 // warrant a new window to be created
253 // create new alignmnt window with hidden regions (unhiding hidden regions
254 // yields unaligned seqs)
255 // or create a selection box around columns in alignment view
256 // test Alignment(SeqCigar[])
260 // we try to get the associated view's gap character
261 // but this may fail if the view was closed...
262 gc = av.getGapCharacter();
263 } catch (Exception ex)
267 Object[] alAndColsel = pcaModel.getSeqtrings()
268 .getAlignmentAndColumnSelection(gc);
270 if (alAndColsel != null && alAndColsel[0] != null)
272 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
273 AlignFrame af = new AlignFrame(al, av.applet,
274 "Original Data for PCA", false);
276 af.viewport.setHiddenColumns((ColumnSelection) alAndColsel[1]);
280 public void labels_itemStateChanged(ItemEvent itemEvent)
282 rc.showLabels(labels.getState());
285 Panel jPanel2 = new Panel();
287 Label jLabel1 = new Label();
289 Label jLabel2 = new Label();
291 Label jLabel3 = new Label();
293 protected Choice xCombobox = new Choice();
295 protected Choice yCombobox = new Choice();
297 protected Choice zCombobox = new Choice();
299 protected Button resetButton = new Button();
301 FlowLayout flowLayout1 = new FlowLayout();
303 BorderLayout borderLayout1 = new BorderLayout();
305 MenuBar menuBar1 = new MenuBar();
307 Menu menu1 = new Menu();
309 Menu menu2 = new Menu();
311 Menu calcSettings = new Menu();
313 protected CheckboxMenuItem labels = new CheckboxMenuItem();
315 protected CheckboxMenuItem protSetting = new CheckboxMenuItem();
317 protected CheckboxMenuItem nuclSetting = new CheckboxMenuItem();
319 MenuItem values = new MenuItem();
321 MenuItem inputData = new MenuItem();
323 private void jbInit() throws Exception
325 this.setLayout(borderLayout1);
326 jPanel2.setLayout(flowLayout1);
327 jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
328 jLabel1.setText("x=");
329 jLabel2.setFont(new java.awt.Font("Verdana", 0, 12));
330 jLabel2.setText("y=");
331 jLabel3.setFont(new java.awt.Font("Verdana", 0, 12));
332 jLabel3.setText("z=");
333 jPanel2.setBackground(Color.white);
334 zCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
335 zCombobox.addItemListener(this);
336 yCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
337 yCombobox.addItemListener(this);
338 xCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
339 xCombobox.addItemListener(this);
340 resetButton.setFont(new java.awt.Font("Verdana", 0, 12));
341 resetButton.setLabel(MessageManager.getString("action.reset"));
342 resetButton.addActionListener(this);
343 this.setMenuBar(menuBar1);
344 menu1.setLabel(MessageManager.getString("action.file"));
345 menu2.setLabel(MessageManager.getString("action.view"));
346 calcSettings.setLabel(MessageManager.getString("action.change_params"));
347 labels.setLabel(MessageManager.getString("label.labels"));
348 labels.addItemListener(this);
349 values.setLabel(MessageManager.getString("label.output_values"));
350 values.addActionListener(this);
351 inputData.setLabel(MessageManager.getString("label.input_data"));
352 nuclSetting.setLabel(MessageManager
353 .getString("label.nucleotide_matrix"));
354 nuclSetting.addItemListener(this);
355 protSetting.setLabel(MessageManager.getString("label.protein_matrix"));
356 protSetting.addItemListener(this);
357 this.add(jPanel2, BorderLayout.SOUTH);
358 jPanel2.add(jLabel1, null);
359 jPanel2.add(xCombobox, null);
360 jPanel2.add(jLabel2, null);
361 jPanel2.add(yCombobox, null);
362 jPanel2.add(jLabel3, null);
363 jPanel2.add(zCombobox, null);
364 jPanel2.add(resetButton, null);
367 menuBar1.add(calcSettings);
370 menu1.add(inputData);
371 calcSettings.add(nuclSetting);
372 calcSettings.add(protSetting);
373 inputData.addActionListener(this);