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.ColumnSelection;
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 implements Runnable,
52 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().getSequencesInOrder(viewport.getAlignment());
93 SeqCigar sq[] = seqstrings.getSequences();
94 int length = sq[0].getWidth();
96 for (int i = 0; i < seqs.length; i++)
98 if (sq[i].getWidth() != length)
101 .println("Sequences must be equal length for PCA analysis");
106 ScoreModelI scoreModel = ScoreModels.getInstance().getDefaultModel(
108 pcaModel = new PCAModel(seqstrings, seqs, nucleotide, scoreModel,
109 SimilarityParams.SeqSpace);
111 rc = new RotatableCanvas(viewport);
112 embedMenuIfNeeded(rc);
113 add(rc, BorderLayout.CENTER);
115 jalview.bin.JalviewLite.addFrame(this,
116 MessageManager.getString("label.principal_component_analysis"),
119 Thread worker = new Thread(this);
129 // TODO progress indicator
130 calcSettings.setEnabled(false);
131 rc.setEnabled(false);
134 nuclSetting.setState(pcaModel.isNucleotide());
135 protSetting.setState(!pcaModel.isNucleotide());
142 pcaModel.updateRc(rc);
144 top = pcaModel.getTop();
145 } catch (OutOfMemoryError x)
147 System.err.println("Out of memory when calculating PCA.");
150 calcSettings.setEnabled(true);
152 // TODO revert progress indicator
158 void doDimensionChange()
165 int dim1 = top - xCombobox.getSelectedIndex();
166 int dim2 = top - yCombobox.getSelectedIndex();
167 int dim3 = top - zCombobox.getSelectedIndex();
168 pcaModel.updateRcView(dim1, dim2, dim3);
170 rc.rotmat.setIdentity();
172 rc.paint(rc.getGraphics());
176 public void actionPerformed(ActionEvent evt)
178 if (evt.getSource() == inputData)
182 if (evt.getSource() == resetButton)
189 if (evt.getSource() == values)
191 values_actionPerformed();
196 public void itemStateChanged(ItemEvent evt)
198 if (evt.getSource() == xCombobox)
200 xCombobox_actionPerformed();
202 else if (evt.getSource() == yCombobox)
204 yCombobox_actionPerformed();
206 else if (evt.getSource() == zCombobox)
208 zCombobox_actionPerformed();
210 else if (evt.getSource() == labels)
212 labels_itemStateChanged(evt);
214 else if (evt.getSource() == nuclSetting)
216 if (!pcaModel.isNucleotide())
218 pcaModel.setNucleotide(true);
219 ScoreModelI scoreModel = ScoreModels.getInstance().getDefaultModel(
221 pcaModel.setScoreModel(scoreModel);
222 new Thread(this).start();
225 else if (evt.getSource() == protSetting)
227 if (pcaModel.isNucleotide())
229 pcaModel.setNucleotide(false);
230 ScoreModelI scoreModel = ScoreModels.getInstance().getDefaultModel(
232 pcaModel.setScoreModel(scoreModel);
233 new Thread(this).start();
238 protected void xCombobox_actionPerformed()
243 protected void yCombobox_actionPerformed()
248 protected void zCombobox_actionPerformed()
253 public void values_actionPerformed()
256 CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
257 Frame frame = new Frame();
259 jalview.bin.JalviewLite.addFrame(frame,
260 MessageManager.getString("label.pca_details"), 500, 500);
262 cap.setText(pcaModel.getDetails());
265 void showOriginalData()
267 // decide if av alignment is sufficiently different to original data to
268 // warrant a new window to be created
269 // create new alignmnt window with hidden regions (unhiding hidden regions
270 // yields unaligned seqs)
271 // or create a selection box around columns in alignment view
272 // test Alignment(SeqCigar[])
276 // we try to get the associated view's gap character
277 // but this may fail if the view was closed...
278 gc = av.getGapCharacter();
279 } catch (Exception ex)
283 Object[] alAndColsel = pcaModel.getSeqtrings()
284 .getAlignmentAndColumnSelection(gc);
286 if (alAndColsel != null && alAndColsel[0] != null)
288 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
289 AlignFrame af = new AlignFrame(al, av.applet,
290 "Original Data for PCA", false);
292 af.viewport.setHiddenColumns((ColumnSelection) alAndColsel[1]);
296 public void labels_itemStateChanged(ItemEvent itemEvent)
298 rc.showLabels(labels.getState());
301 Panel jPanel2 = new Panel();
303 Label jLabel1 = new Label();
305 Label jLabel2 = new Label();
307 Label jLabel3 = new Label();
309 protected Choice xCombobox = new Choice();
311 protected Choice yCombobox = new Choice();
313 protected Choice zCombobox = new Choice();
315 protected Button resetButton = new Button();
317 FlowLayout flowLayout1 = new FlowLayout();
319 BorderLayout borderLayout1 = new BorderLayout();
321 MenuBar menuBar1 = new MenuBar();
323 Menu menu1 = new Menu();
325 Menu menu2 = new Menu();
327 Menu calcSettings = new Menu();
329 protected CheckboxMenuItem labels = new CheckboxMenuItem();
331 protected CheckboxMenuItem protSetting = new CheckboxMenuItem();
333 protected CheckboxMenuItem nuclSetting = new CheckboxMenuItem();
335 MenuItem values = new MenuItem();
337 MenuItem inputData = new MenuItem();
339 private void jbInit() throws Exception
341 this.setLayout(borderLayout1);
342 jPanel2.setLayout(flowLayout1);
343 jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
344 jLabel1.setText("x=");
345 jLabel2.setFont(new java.awt.Font("Verdana", 0, 12));
346 jLabel2.setText("y=");
347 jLabel3.setFont(new java.awt.Font("Verdana", 0, 12));
348 jLabel3.setText("z=");
349 jPanel2.setBackground(Color.white);
350 zCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
351 zCombobox.addItemListener(this);
352 yCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
353 yCombobox.addItemListener(this);
354 xCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
355 xCombobox.addItemListener(this);
356 resetButton.setFont(new java.awt.Font("Verdana", 0, 12));
357 resetButton.setLabel(MessageManager.getString("action.reset"));
358 resetButton.addActionListener(this);
359 this.setMenuBar(menuBar1);
360 menu1.setLabel(MessageManager.getString("action.file"));
361 menu2.setLabel(MessageManager.getString("action.view"));
362 calcSettings.setLabel(MessageManager.getString("action.change_params"));
363 labels.setLabel(MessageManager.getString("label.labels"));
364 labels.addItemListener(this);
365 values.setLabel(MessageManager.getString("label.output_values"));
366 values.addActionListener(this);
367 inputData.setLabel(MessageManager.getString("label.input_data"));
368 nuclSetting.setLabel(MessageManager
369 .getString("label.nucleotide_matrix"));
370 nuclSetting.addItemListener(this);
371 protSetting.setLabel(MessageManager.getString("label.protein_matrix"));
372 protSetting.addItemListener(this);
373 this.add(jPanel2, BorderLayout.SOUTH);
374 jPanel2.add(jLabel1, null);
375 jPanel2.add(xCombobox, null);
376 jPanel2.add(jLabel2, null);
377 jPanel2.add(yCombobox, null);
378 jPanel2.add(jLabel3, null);
379 jPanel2.add(zCombobox, null);
380 jPanel2.add(resetButton, null);
383 menuBar1.add(calcSettings);
386 menu1.add(inputData);
387 calcSettings.add(nuclSetting);
388 calcSettings.add(protSetting);
389 inputData.addActionListener(this);