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 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 .getAlignmentAndHiddenColumns(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.getAlignment().setHiddenColumns(
293 (HiddenColumns) alAndColsel[1]);
297 public void labels_itemStateChanged(ItemEvent itemEvent)
299 rc.showLabels(labels.getState());
302 Panel jPanel2 = new Panel();
304 Label jLabel1 = new Label();
306 Label jLabel2 = new Label();
308 Label jLabel3 = new Label();
310 protected Choice xCombobox = new Choice();
312 protected Choice yCombobox = new Choice();
314 protected Choice zCombobox = new Choice();
316 protected Button resetButton = new Button();
318 FlowLayout flowLayout1 = new FlowLayout();
320 BorderLayout borderLayout1 = new BorderLayout();
322 MenuBar menuBar1 = new MenuBar();
324 Menu menu1 = new Menu();
326 Menu menu2 = new Menu();
328 Menu calcSettings = new Menu();
330 protected CheckboxMenuItem labels = new CheckboxMenuItem();
332 protected CheckboxMenuItem protSetting = new CheckboxMenuItem();
334 protected CheckboxMenuItem nuclSetting = new CheckboxMenuItem();
336 MenuItem values = new MenuItem();
338 MenuItem inputData = new MenuItem();
340 private void jbInit() throws Exception
342 this.setLayout(borderLayout1);
343 jPanel2.setLayout(flowLayout1);
344 jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
345 jLabel1.setText("x=");
346 jLabel2.setFont(new java.awt.Font("Verdana", 0, 12));
347 jLabel2.setText("y=");
348 jLabel3.setFont(new java.awt.Font("Verdana", 0, 12));
349 jLabel3.setText("z=");
350 jPanel2.setBackground(Color.white);
351 zCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
352 zCombobox.addItemListener(this);
353 yCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
354 yCombobox.addItemListener(this);
355 xCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
356 xCombobox.addItemListener(this);
357 resetButton.setFont(new java.awt.Font("Verdana", 0, 12));
358 resetButton.setLabel(MessageManager.getString("action.reset"));
359 resetButton.addActionListener(this);
360 this.setMenuBar(menuBar1);
361 menu1.setLabel(MessageManager.getString("action.file"));
362 menu2.setLabel(MessageManager.getString("action.view"));
363 calcSettings.setLabel(MessageManager.getString("action.change_params"));
364 labels.setLabel(MessageManager.getString("label.labels"));
365 labels.addItemListener(this);
366 values.setLabel(MessageManager.getString("label.output_values"));
367 values.addActionListener(this);
368 inputData.setLabel(MessageManager.getString("label.input_data"));
369 nuclSetting.setLabel(MessageManager
370 .getString("label.nucleotide_matrix"));
371 nuclSetting.addItemListener(this);
372 protSetting.setLabel(MessageManager.getString("label.protein_matrix"));
373 protSetting.addItemListener(this);
374 this.add(jPanel2, BorderLayout.SOUTH);
375 jPanel2.add(jLabel1, null);
376 jPanel2.add(xCombobox, null);
377 jPanel2.add(jLabel2, null);
378 jPanel2.add(yCombobox, null);
379 jPanel2.add(jLabel3, null);
380 jPanel2.add(zCombobox, null);
381 jPanel2.add(resetButton, null);
384 menuBar1.add(calcSettings);
387 menu1.add(inputData);
388 calcSettings.add(nuclSetting);
389 calcSettings.add(protSetting);
390 inputData.addActionListener(this);