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.
23 import jalview.analysis.scoremodels.ScoreModels;
24 import jalview.api.analysis.ScoreModelI;
25 import jalview.api.analysis.SimilarityParamsI;
26 import jalview.bin.Cache;
27 import jalview.datamodel.Alignment;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.AlignmentView;
30 import jalview.datamodel.HiddenColumns;
31 import jalview.datamodel.SequenceI;
32 import jalview.jbgui.GPCAPanel;
33 import jalview.math.RotatableMatrix.Axis;
34 import jalview.util.ImageMaker;
35 import jalview.util.MessageManager;
36 import jalview.viewmodel.AlignmentViewport;
37 import jalview.viewmodel.PCAModel;
39 import java.awt.BorderLayout;
40 import java.awt.Color;
41 import java.awt.Dimension;
42 import java.awt.Graphics;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.ActionListener;
45 import java.awt.print.PageFormat;
46 import java.awt.print.Printable;
47 import java.awt.print.PrinterException;
48 import java.awt.print.PrinterJob;
50 import javax.swing.ButtonGroup;
51 import javax.swing.JColorChooser;
52 import javax.swing.JMenuItem;
53 import javax.swing.JRadioButtonMenuItem;
54 import javax.swing.event.InternalFrameAdapter;
55 import javax.swing.event.InternalFrameEvent;
58 * The panel holding the Principal Component Analysis 3-D visualisation
60 public class PCAPanel extends GPCAPanel
61 implements Runnable, IProgressIndicator
63 private static final int MIN_WIDTH = 470;
65 private static final int MIN_HEIGHT = 250;
77 private IProgressIndicator progressBar;
79 private boolean working;
82 * Constructor given sequence data, a similarity (or distance) score model
83 * name, and score calculation parameters
89 public PCAPanel(AlignmentPanel alignPanel, String modelName,
90 SimilarityParamsI params)
93 this.av = alignPanel.av;
95 boolean nucleotide = av.getAlignment().isNucleotide();
97 progressBar = new ProgressBar(statusPanel, statusBar);
99 addInternalFrameListener(new InternalFrameAdapter()
102 public void internalFrameClosed(InternalFrameEvent e)
104 close_actionPerformed();
108 boolean selected = av.getSelectionGroup() != null
109 && av.getSelectionGroup().getSize() > 0;
110 AlignmentView seqstrings = av.getAlignmentView(selected);
114 seqs = av.getAlignment().getSequencesArray();
118 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
121 ScoreModelI scoreModel = ScoreModels.getInstance()
122 .getScoreModel(modelName, ap);
123 pcaModel = new PCAModel(seqstrings, seqs, nucleotide, scoreModel,
125 PaintRefresher.Register(this, av.getSequenceSetId());
127 rc = new RotatableCanvas(alignPanel);
128 this.getContentPane().add(rc, BorderLayout.CENTER);
133 this.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
137 * Ensure references to potentially very large objects (the PCA matrices) are
138 * nulled when the frame is closed
140 protected void close_actionPerformed()
146 protected void bgcolour_actionPerformed()
148 Color col = JColorChooser.showDialog(this,
149 MessageManager.getString("label.select_background_colour"),
160 * Calculates the PCA and displays the results
165 long progId = System.currentTimeMillis();
166 IProgressIndicator progress = this;
167 String message = MessageManager.getString("label.pca_recalculating");
168 if (getParent() == null)
170 progress = ap.alignFrame;
171 message = MessageManager.getString("label.pca_calculating");
173 progress.setProgressBar(message, progId);
177 pcaModel.calculate();
179 xCombobox.setSelectedIndex(0);
180 yCombobox.setSelectedIndex(1);
181 zCombobox.setSelectedIndex(2);
183 pcaModel.updateRc(rc);
185 top = pcaModel.getTop();
187 } catch (OutOfMemoryError er)
189 new OOMWarning("calculating PCA", er);
194 progress.setProgressBar("", progId);
198 if (getParent() == null)
200 Desktop.addInternalFrame(this,
201 MessageManager.formatMessage("label.calc_title", "PCA",
202 pcaModel.getScoreModelName()),
209 * Updates the PCA display after a change of component to use for x, y or z
213 protected void doDimensionChange()
220 int dim1 = top - xCombobox.getSelectedIndex();
221 int dim2 = top - yCombobox.getSelectedIndex();
222 int dim3 = top - zCombobox.getSelectedIndex();
223 pcaModel.updateRcView(dim1, dim2, dim3);
228 * Sets the selected checkbox item index for PCA dimension (1, 2, 3...) for
229 * the given axis (X/Y/Z)
234 public void setSelectedDimensionIndex(int index, Axis axis)
239 xCombobox.setSelectedIndex(index);
242 yCombobox.setSelectedIndex(index);
245 zCombobox.setSelectedIndex(index);
252 protected void outputValues_actionPerformed()
254 CutAndPasteTransfer cap = new CutAndPasteTransfer();
257 cap.setText(pcaModel.getDetails());
258 Desktop.addInternalFrame(cap,
259 MessageManager.getString("label.pca_details"), 500, 500);
260 } catch (OutOfMemoryError oom)
262 new OOMWarning("opening PCA details", oom);
268 protected void showLabels_actionPerformed()
270 rc.showLabels(showLabels.getState());
274 protected void print_actionPerformed()
276 PCAPrinter printer = new PCAPrinter();
281 public void originalSeqData_actionPerformed()
283 // this was cut'n'pasted from the equivalent TreePanel method - we should
284 // make this an abstract function of all jalview analysis windows
285 if (pcaModel.getSeqtrings() == null)
288 "Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
291 // decide if av alignment is sufficiently different to original data to
292 // warrant a new window to be created
293 // create new alignmnt window with hidden regions (unhiding hidden regions
294 // yields unaligned seqs)
295 // or create a selection box around columns in alignment view
296 // test Alignment(SeqCigar[])
300 // we try to get the associated view's gap character
301 // but this may fail if the view was closed...
302 gc = av.getGapCharacter();
303 } catch (Exception ex)
307 Object[] alAndColsel = pcaModel.getSeqtrings()
308 .getAlignmentAndHiddenColumns(gc);
310 if (alAndColsel != null && alAndColsel[0] != null)
312 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
314 AlignmentI al = new Alignment((SequenceI[]) alAndColsel[0]);
315 AlignmentI dataset = (av != null && av.getAlignment() != null)
316 ? av.getAlignment().getDataset()
320 al.setDataset(dataset);
326 AlignFrame af = new AlignFrame(al, (HiddenColumns) alAndColsel[1],
327 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
329 // >>>This is a fix for the moment, until a better solution is
331 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
333 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
336 Desktop.addInternalFrame(af, MessageManager.formatMessage(
337 "label.original_data_for_params", new String[]
338 { this.title }), AlignFrame.DEFAULT_WIDTH,
339 AlignFrame.DEFAULT_HEIGHT);
343 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
344 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
345 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
348 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
352 class PCAPrinter extends Thread implements Printable
357 PrinterJob printJob = PrinterJob.getPrinterJob();
358 PageFormat defaultPage = printJob.defaultPage();
359 PageFormat pf = printJob.pageDialog(defaultPage);
361 if (defaultPage == pf)
369 printJob.setPrintable(this, pf);
371 if (printJob.printDialog())
376 } catch (Exception PrintException)
378 PrintException.printStackTrace();
384 public int print(Graphics pg, PageFormat pf, int pi)
385 throws PrinterException
387 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
389 rc.drawBackground(pg, rc.bgColour);
398 return Printable.PAGE_EXISTS;
402 return Printable.NO_SUCH_PAGE;
408 * Handler for 'Save as EPS' option
411 protected void eps_actionPerformed()
413 makePCAImage(ImageMaker.TYPE.EPS);
417 * Handler for 'Save as PNG' option
420 protected void png_actionPerformed()
422 makePCAImage(ImageMaker.TYPE.PNG);
425 void makePCAImage(ImageMaker.TYPE type)
427 int width = rc.getWidth();
428 int height = rc.getHeight();
432 if (type == ImageMaker.TYPE.PNG)
434 im = new ImageMaker(this, ImageMaker.TYPE.PNG,
435 "Make PNG image from PCA", width, height, null, null, null, 0,
438 else if (type == jalview.util.ImageMaker.TYPE.EPS)
440 im = new ImageMaker(this, ImageMaker.TYPE.EPS,
441 "Make EPS file from PCA", width, height, null,
442 this.getTitle(), null, 0, false);
446 im = new ImageMaker(this, ImageMaker.TYPE.SVG,
447 "Make SVG file from PCA", width, height, null,
448 this.getTitle(), null, 0, false);
451 if (im.getGraphics() != null)
453 rc.drawBackground(im.getGraphics(), Color.black);
454 rc.drawScene(im.getGraphics());
457 rc.drawAxes(im.getGraphics());
464 protected void viewMenu_menuSelected()
466 buildAssociatedViewMenu();
470 * Builds the menu showing the choice of possible views (for the associated
471 * sequence data) to which the PCA may be linked
473 void buildAssociatedViewMenu()
475 AlignmentPanel[] aps = PaintRefresher
476 .getAssociatedPanels(av.getSequenceSetId());
477 if (aps.length == 1 && rc.av == aps[0].av)
479 associateViewsMenu.setVisible(false);
483 associateViewsMenu.setVisible(true);
486 .getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
488 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
491 associateViewsMenu.removeAll();
493 JRadioButtonMenuItem item;
494 ButtonGroup buttonGroup = new ButtonGroup();
495 int iSize = aps.length;
497 for (int i = 0; i < iSize; i++)
499 final AlignmentPanel panel = aps[i];
500 item = new JRadioButtonMenuItem(panel.av.viewName, panel.av == rc.av);
501 buttonGroup.add(item);
502 item.addActionListener(new ActionListener()
505 public void actionPerformed(ActionEvent evt)
507 rc.applyToAllViews = false;
510 PaintRefresher.Register(PCAPanel.this,
511 panel.av.getSequenceSetId());
515 associateViewsMenu.add(item);
518 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem(
521 buttonGroup.add(itemf);
523 itemf.setSelected(rc.applyToAllViews);
524 itemf.addActionListener(new ActionListener()
527 public void actionPerformed(ActionEvent evt)
529 rc.applyToAllViews = itemf.isSelected();
532 associateViewsMenu.add(itemf);
540 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
544 protected void outputPoints_actionPerformed()
546 CutAndPasteTransfer cap = new CutAndPasteTransfer();
549 cap.setText(pcaModel.getPointsasCsv(false,
550 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
551 zCombobox.getSelectedIndex()));
552 Desktop.addInternalFrame(cap, MessageManager
553 .formatMessage("label.points_for_params", new String[]
554 { this.getTitle() }), 500, 500);
555 } catch (OutOfMemoryError oom)
557 new OOMWarning("exporting PCA points", oom);
566 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
570 protected void outputProjPoints_actionPerformed()
572 CutAndPasteTransfer cap = new CutAndPasteTransfer();
575 cap.setText(pcaModel.getPointsasCsv(true,
576 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
577 zCombobox.getSelectedIndex()));
578 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
579 "label.transformed_points_for_params", new String[]
580 { this.getTitle() }), 500, 500);
581 } catch (OutOfMemoryError oom)
583 new OOMWarning("exporting transformed PCA points", oom);
591 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
594 public void setProgressBar(String message, long id)
596 progressBar.setProgressBar(message, id);
597 // if (progressBars == null)
599 // progressBars = new Hashtable();
600 // progressBarHandlers = new Hashtable();
603 // JPanel progressPanel;
604 // Long lId = new Long(id);
605 // GridLayout layout = (GridLayout) statusPanel.getLayout();
606 // if (progressBars.get(lId) != null)
608 // progressPanel = (JPanel) progressBars.get(new Long(id));
609 // statusPanel.remove(progressPanel);
610 // progressBars.remove(lId);
611 // progressPanel = null;
612 // if (message != null)
614 // statusBar.setText(message);
616 // if (progressBarHandlers.contains(lId))
618 // progressBarHandlers.remove(lId);
620 // layout.setRows(layout.getRows() - 1);
624 // progressPanel = new JPanel(new BorderLayout(10, 5));
626 // JProgressBar progressBar = new JProgressBar();
627 // progressBar.setIndeterminate(true);
629 // progressPanel.add(new JLabel(message), BorderLayout.WEST);
630 // progressPanel.add(progressBar, BorderLayout.CENTER);
632 // layout.setRows(layout.getRows() + 1);
633 // statusPanel.add(progressPanel);
635 // progressBars.put(lId, progressPanel);
638 // // setMenusForViewport();
643 public void registerHandler(final long id,
644 final IProgressIndicatorHandler handler)
646 progressBar.registerHandler(id, handler);
647 // if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
650 // Error(MessageManager.getString("error.call_setprogressbar_before_registering_handler"));
652 // progressBarHandlers.put(new Long(id), handler);
653 // final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
654 // if (handler.canCancel())
656 // JButton cancel = new JButton(
657 // MessageManager.getString("action.cancel"));
658 // final IProgressIndicator us = this;
659 // cancel.addActionListener(new ActionListener()
663 // public void actionPerformed(ActionEvent e)
665 // handler.cancelActivity(id);
666 // us.setProgressBar(MessageManager.formatMessage("label.cancelled_params",
667 // new String[]{((JLabel) progressPanel.getComponent(0)).getText()}), id);
670 // progressPanel.add(cancel, BorderLayout.EAST);
676 * @return true if any progress bars are still active
679 public boolean operationInProgress()
681 return progressBar.operationInProgress();
685 protected void resetButton_actionPerformed()
688 top = 0; // ugly - prevents dimensionChanged events from being processed
689 xCombobox.setSelectedIndex(0);
690 yCombobox.setSelectedIndex(1);
692 zCombobox.setSelectedIndex(2);
696 * Answers true if PCA calculation is in progress, else false
700 public boolean isWorking()
706 * Answers the selected checkbox item index for PCA dimension for the X, Y or
707 * Z axis of the display
712 public int getSelectedDimensionIndex(Axis axis)
717 return xCombobox.getSelectedIndex();
719 return yCombobox.getSelectedIndex();
721 return zCombobox.getSelectedIndex();
725 public void setShowLabels(boolean show)
727 showLabels.setSelected(show);