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.analysis.scoremodels.ScoreModels;
24 import jalview.analysis.scoremodels.SimilarityParams;
25 import jalview.api.analysis.ScoreModelI;
26 import jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentView;
28 import jalview.datamodel.HiddenColumns;
29 import jalview.datamodel.SeqCigar;
30 import jalview.datamodel.SequenceI;
31 import jalview.util.MessageManager;
32 import jalview.viewmodel.PCAModel;
34 import java.awt.BorderLayout;
35 import java.awt.Button;
36 import java.awt.CheckboxMenuItem;
37 import java.awt.Choice;
38 import java.awt.Color;
39 import java.awt.FlowLayout;
40 import java.awt.Frame;
41 import java.awt.Label;
43 import java.awt.MenuBar;
44 import java.awt.MenuItem;
45 import java.awt.Panel;
46 import java.awt.event.ActionEvent;
47 import java.awt.event.ActionListener;
48 import java.awt.event.ItemEvent;
49 import java.awt.event.ItemListener;
51 public class PCAPanel extends EmbmenuFrame
52 implements Runnable, ActionListener, ItemListener
62 public PCAPanel(AlignViewport viewport)
72 for (int i = 1; i < 8; i++)
74 xCombobox.addItem("dim " + i);
75 yCombobox.addItem("dim " + i);
76 zCombobox.addItem("dim " + i);
80 boolean selected = viewport.getSelectionGroup() != null
81 && viewport.getSelectionGroup().getSize() > 0;
82 AlignmentView seqstrings = viewport.getAlignmentView(selected);
83 boolean nucleotide = viewport.getAlignment().isNucleotide();
87 seqs = viewport.getAlignment().getSequencesArray();
91 seqs = viewport.getSelectionGroup()
92 .getSequencesInOrder(viewport.getAlignment());
94 SeqCigar sq[] = seqstrings.getSequences();
95 int length = sq[0].getWidth();
97 for (int i = 0; i < seqs.length; i++)
99 if (sq[i].getWidth() != length)
102 .println("Sequences must be equal length for PCA analysis");
107 ScoreModelI scoreModel = ScoreModels.getInstance()
108 .getDefaultModel(!nucleotide);
109 pcaModel = new PCAModel(seqstrings, seqs, nucleotide, scoreModel,
110 SimilarityParams.SeqSpace);
112 rc = new RotatableCanvas(viewport);
113 embedMenuIfNeeded(rc);
114 add(rc, BorderLayout.CENTER);
116 jalview.bin.JalviewLite.addFrame(this,
117 MessageManager.getString("label.principal_component_analysis"),
120 Thread worker = new Thread(this);
130 // TODO progress indicator
131 calcSettings.setEnabled(false);
132 rc.setEnabled(false);
135 nuclSetting.setState(pcaModel.isNucleotide());
136 protSetting.setState(!pcaModel.isNucleotide());
143 pcaModel.updateRc(rc);
145 top = pcaModel.getTop();
146 } catch (OutOfMemoryError x)
148 System.err.println("Out of memory when calculating PCA.");
151 calcSettings.setEnabled(true);
153 // TODO revert progress indicator
159 void doDimensionChange()
166 int dim1 = top - xCombobox.getSelectedIndex();
167 int dim2 = top - yCombobox.getSelectedIndex();
168 int dim3 = top - zCombobox.getSelectedIndex();
169 pcaModel.updateRcView(dim1, dim2, dim3);
171 rc.rotmat.setIdentity();
173 rc.paint(rc.getGraphics());
177 public void actionPerformed(ActionEvent evt)
179 if (evt.getSource() == inputData)
183 if (evt.getSource() == resetButton)
190 if (evt.getSource() == values)
192 values_actionPerformed();
197 public void itemStateChanged(ItemEvent evt)
199 if (evt.getSource() == xCombobox)
201 xCombobox_actionPerformed();
203 else if (evt.getSource() == yCombobox)
205 yCombobox_actionPerformed();
207 else if (evt.getSource() == zCombobox)
209 zCombobox_actionPerformed();
211 else if (evt.getSource() == labels)
213 labels_itemStateChanged(evt);
215 else if (evt.getSource() == nuclSetting)
217 if (!pcaModel.isNucleotide())
219 pcaModel.setNucleotide(true);
220 ScoreModelI scoreModel = ScoreModels.getInstance()
221 .getDefaultModel(false);
222 pcaModel.setScoreModel(scoreModel);
223 new Thread(this).start();
226 else if (evt.getSource() == protSetting)
228 if (pcaModel.isNucleotide())
230 pcaModel.setNucleotide(false);
231 ScoreModelI scoreModel = ScoreModels.getInstance()
232 .getDefaultModel(true);
233 pcaModel.setScoreModel(scoreModel);
234 new Thread(this).start();
239 protected void xCombobox_actionPerformed()
244 protected void yCombobox_actionPerformed()
249 protected void zCombobox_actionPerformed()
254 public void values_actionPerformed()
257 CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
258 Frame frame = new Frame();
260 jalview.bin.JalviewLite.addFrame(frame,
261 MessageManager.getString("label.pca_details"), 500, 500);
263 cap.setText(pcaModel.getDetails());
266 void showOriginalData()
268 // decide if av alignment is sufficiently different to original data to
269 // warrant a new window to be created
270 // create new alignmnt window with hidden regions (unhiding hidden regions
271 // yields unaligned seqs)
272 // or create a selection box around columns in alignment view
273 // test Alignment(SeqCigar[])
277 // we try to get the associated view's gap character
278 // but this may fail if the view was closed...
279 gc = av.getGapCharacter();
280 } catch (Exception ex)
284 Object[] alAndColsel = pcaModel.getSeqtrings()
285 .getAlignmentAndHiddenColumns(gc);
287 if (alAndColsel != null && alAndColsel[0] != null)
289 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
290 AlignFrame af = new AlignFrame(al, av.applet, "Original Data for PCA",
293 af.viewport.getAlignment()
294 .setHiddenColumns((HiddenColumns) alAndColsel[1]);
298 public void labels_itemStateChanged(ItemEvent itemEvent)
300 rc.showLabels(labels.getState());
303 Panel jPanel2 = new Panel();
305 Label jLabel1 = new Label();
307 Label jLabel2 = new Label();
309 Label jLabel3 = new Label();
311 protected Choice xCombobox = new Choice();
313 protected Choice yCombobox = new Choice();
315 protected Choice zCombobox = new Choice();
317 protected Button resetButton = new Button();
319 FlowLayout flowLayout1 = new FlowLayout();
321 BorderLayout borderLayout1 = new BorderLayout();
323 MenuBar menuBar1 = new MenuBar();
325 Menu menu1 = new Menu();
327 Menu menu2 = new Menu();
329 Menu calcSettings = new Menu();
331 protected CheckboxMenuItem labels = new CheckboxMenuItem();
333 protected CheckboxMenuItem protSetting = new CheckboxMenuItem();
335 protected CheckboxMenuItem nuclSetting = new CheckboxMenuItem();
337 MenuItem values = new MenuItem();
339 MenuItem inputData = new MenuItem();
341 private void jbInit() throws Exception
343 this.setLayout(borderLayout1);
344 jPanel2.setLayout(flowLayout1);
345 jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
346 jLabel1.setText("x=");
347 jLabel2.setFont(new java.awt.Font("Verdana", 0, 12));
348 jLabel2.setText("y=");
349 jLabel3.setFont(new java.awt.Font("Verdana", 0, 12));
350 jLabel3.setText("z=");
351 jPanel2.setBackground(Color.white);
352 zCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
353 zCombobox.addItemListener(this);
354 yCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
355 yCombobox.addItemListener(this);
356 xCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
357 xCombobox.addItemListener(this);
358 resetButton.setFont(new java.awt.Font("Verdana", 0, 12));
359 resetButton.setLabel(MessageManager.getString("action.reset"));
360 resetButton.addActionListener(this);
361 this.setMenuBar(menuBar1);
362 menu1.setLabel(MessageManager.getString("action.file"));
363 menu2.setLabel(MessageManager.getString("action.view"));
364 calcSettings.setLabel(MessageManager.getString("action.change_params"));
365 labels.setLabel(MessageManager.getString("label.labels"));
366 labels.addItemListener(this);
367 values.setLabel(MessageManager.getString("label.output_values"));
368 values.addActionListener(this);
369 inputData.setLabel(MessageManager.getString("label.input_data"));
371 .setLabel(MessageManager.getString("label.nucleotide_matrix"));
372 nuclSetting.addItemListener(this);
373 protSetting.setLabel(MessageManager.getString("label.protein_matrix"));
374 protSetting.addItemListener(this);
375 this.add(jPanel2, BorderLayout.SOUTH);
376 jPanel2.add(jLabel1, null);
377 jPanel2.add(xCombobox, null);
378 jPanel2.add(jLabel2, null);
379 jPanel2.add(yCombobox, null);
380 jPanel2.add(jLabel3, null);
381 jPanel2.add(zCombobox, null);
382 jPanel2.add(resetButton, null);
385 menuBar1.add(calcSettings);
388 menu1.add(inputData);
389 calcSettings.add(nuclSetting);
390 calcSettings.add(protSetting);
391 inputData.addActionListener(this);