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());
137 pcaModel.calculate();
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.paint(rc.getGraphics());
175 public void actionPerformed(ActionEvent evt)
177 if (evt.getSource() == inputData)
181 if (evt.getSource() == resetButton)
188 if (evt.getSource() == values)
190 values_actionPerformed();
195 public void itemStateChanged(ItemEvent evt)
197 if (evt.getSource() == xCombobox)
199 xCombobox_actionPerformed();
201 else if (evt.getSource() == yCombobox)
203 yCombobox_actionPerformed();
205 else if (evt.getSource() == zCombobox)
207 zCombobox_actionPerformed();
209 else if (evt.getSource() == labels)
211 labels_itemStateChanged(evt);
213 else if (evt.getSource() == nuclSetting)
215 if (!pcaModel.isNucleotide())
217 pcaModel.setNucleotide(true);
218 ScoreModelI scoreModel = ScoreModels.getInstance()
219 .getDefaultModel(false);
220 pcaModel.setScoreModel(scoreModel);
221 new Thread(this).start();
224 else if (evt.getSource() == protSetting)
226 if (pcaModel.isNucleotide())
228 pcaModel.setNucleotide(false);
229 ScoreModelI scoreModel = ScoreModels.getInstance()
230 .getDefaultModel(true);
231 pcaModel.setScoreModel(scoreModel);
232 new Thread(this).start();
237 protected void xCombobox_actionPerformed()
242 protected void yCombobox_actionPerformed()
247 protected void zCombobox_actionPerformed()
252 public void values_actionPerformed()
255 CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
256 Frame frame = new Frame();
258 jalview.bin.JalviewLite.addFrame(frame,
259 MessageManager.getString("label.pca_details"), 500, 500);
261 cap.setText(pcaModel.getDetails());
264 void showOriginalData()
266 // decide if av alignment is sufficiently different to original data to
267 // warrant a new window to be created
268 // create new alignmnt window with hidden regions (unhiding hidden regions
269 // yields unaligned seqs)
270 // or create a selection box around columns in alignment view
271 // test Alignment(SeqCigar[])
275 // we try to get the associated view's gap character
276 // but this may fail if the view was closed...
277 gc = av.getGapCharacter();
278 } catch (Exception ex)
282 Object[] alAndColsel = pcaModel.getSeqtrings()
283 .getAlignmentAndHiddenColumns(gc);
285 if (alAndColsel != null && alAndColsel[0] != null)
287 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
288 AlignFrame af = new AlignFrame(al, av.applet, "Original Data for PCA",
291 af.viewport.getAlignment()
292 .setHiddenColumns((HiddenColumns) 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"));
369 .setLabel(MessageManager.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);