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.analysis.scoremodels.SimilarityParams;
25 import jalview.api.analysis.ScoreModelI;
26 import jalview.api.analysis.SimilarityParamsI;
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.util.MessageManager;
34 import jalview.viewmodel.AlignmentViewport;
35 import jalview.viewmodel.PCAModel;
37 import java.awt.BorderLayout;
38 import java.awt.Color;
39 import java.awt.Dimension;
40 import java.awt.Graphics;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.print.PageFormat;
44 import java.awt.print.Printable;
45 import java.awt.print.PrinterException;
46 import java.awt.print.PrinterJob;
48 import javax.swing.ButtonGroup;
49 import javax.swing.JCheckBoxMenuItem;
50 import javax.swing.JColorChooser;
51 import javax.swing.JMenuItem;
52 import javax.swing.JRadioButtonMenuItem;
53 import javax.swing.event.InternalFrameAdapter;
54 import javax.swing.event.InternalFrameEvent;
62 public class PCAPanel extends GPCAPanel
63 implements Runnable, IProgressIndicator
66 private IProgressIndicator progressBar;
76 private static final int MIN_WIDTH = 470;
78 private static final int MIN_HEIGHT = 250;
82 private boolean working;
85 * Creates a new PCAPanel object using default score model and parameters
89 public PCAPanel(AlignmentPanel alignPanel)
92 ScoreModels.getInstance()
94 !alignPanel.av.getAlignment().isNucleotide())
96 SimilarityParams.SeqSpace);
100 * Constructor given sequence data, a similarity (or distance) score model
101 * name, and score calculation parameters
107 public PCAPanel(AlignmentPanel alignPanel, String modelName,
108 SimilarityParamsI params)
111 this.av = alignPanel.av;
112 this.ap = alignPanel;
113 boolean nucleotide = av.getAlignment().isNucleotide();
115 progressBar = new ProgressBar(statusPanel, statusBar);
117 addInternalFrameListener(new InternalFrameAdapter()
120 public void internalFrameClosed(InternalFrameEvent e)
122 close_actionPerformed();
126 boolean selected = av.getSelectionGroup() != null
127 && av.getSelectionGroup().getSize() > 0;
128 AlignmentView seqstrings = av.getAlignmentView(selected);
132 seqs = av.getAlignment().getSequencesArray();
136 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
139 ScoreModelI scoreModel = ScoreModels.getInstance()
140 .getScoreModel(modelName, ap);
141 pcaModel = new PCAModel(seqstrings, seqs, nucleotide, scoreModel,
143 PaintRefresher.Register(this, av.getSequenceSetId());
145 rc = new RotatableCanvas(alignPanel);
146 this.getContentPane().add(rc, BorderLayout.CENTER);
147 Thread worker = new Thread(this);
152 * Ensure references to potentially very large objects (the PCA matrices) are
153 * nulled when the frame is closed
155 protected void close_actionPerformed()
161 * Repopulate the options and actions under the score model menu when it is
162 * selected. Options will depend on whether 'nucleotide' or 'peptide'
163 * modelling is selected (and also possibly on whether any additional score
164 * models have been added).
167 protected void scoreModel_menuSelected()
169 scoreModelMenu.removeAll();
170 for (final ScoreModelI sm : ScoreModels.getInstance().getModels())
172 final String name = sm.getName();
173 JCheckBoxMenuItem jm = new JCheckBoxMenuItem(name);
176 * if the score model doesn't provide a description, try to look one
177 * up in the text bundle, falling back on its name
179 String tooltip = sm.getDescription();
182 tooltip = MessageManager.getStringOrReturn("label.score_model_",
185 jm.setToolTipText(tooltip);
186 jm.setSelected(pcaModel.getScoreModelName().equals(name));
187 if ((pcaModel.isNucleotide() && sm.isDNA())
188 || (!pcaModel.isNucleotide() && sm.isProtein()))
190 jm.addActionListener(new ActionListener()
193 public void actionPerformed(ActionEvent e)
195 if (!pcaModel.getScoreModelName().equals(name))
197 ScoreModelI sm2 = ScoreModels.getInstance()
198 .getScoreModel(name, ap);
199 pcaModel.setScoreModel(sm2);
200 Thread worker = new Thread(PCAPanel.this);
205 scoreModelMenu.add(jm);
211 public void bgcolour_actionPerformed(ActionEvent e)
213 Color col = JColorChooser.showDialog(this,
214 MessageManager.getString("label.select_background_colour"),
230 long progId = System.currentTimeMillis();
231 IProgressIndicator progress = this;
232 String message = MessageManager.getString("label.pca_recalculating");
233 if (getParent() == null)
235 progress = ap.alignFrame;
236 message = MessageManager.getString("label.pca_calculating");
238 progress.setProgressBar(message, progId);
242 calcSettings.setEnabled(false);
245 xCombobox.setSelectedIndex(0);
246 yCombobox.setSelectedIndex(1);
247 zCombobox.setSelectedIndex(2);
249 pcaModel.updateRc(rc);
251 nuclSetting.setSelected(pcaModel.isNucleotide());
252 protSetting.setSelected(!pcaModel.isNucleotide());
253 top = pcaModel.getTop();
255 } catch (OutOfMemoryError er)
257 new OOMWarning("calculating PCA", er);
262 progress.setProgressBar("", progId);
264 calcSettings.setEnabled(true);
266 if (getParent() == null)
269 Desktop.addInternalFrame(this, MessageManager
270 .getString("label.principal_component_analysis"), 475, 450);
271 this.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
277 protected void nuclSetting_actionPerfomed(ActionEvent arg0)
279 if (!pcaModel.isNucleotide())
281 pcaModel.setNucleotide(true);
282 pcaModel.setScoreModel(
283 ScoreModels.getInstance().getDefaultModel(false));
284 Thread worker = new Thread(this);
291 protected void protSetting_actionPerfomed(ActionEvent arg0)
294 if (pcaModel.isNucleotide())
296 pcaModel.setNucleotide(false);
297 pcaModel.setScoreModel(
298 ScoreModels.getInstance().getDefaultModel(true));
299 Thread worker = new Thread(this);
307 void doDimensionChange()
314 int dim1 = top - xCombobox.getSelectedIndex();
315 int dim2 = top - yCombobox.getSelectedIndex();
316 int dim3 = top - zCombobox.getSelectedIndex();
317 pcaModel.updateRcView(dim1, dim2, dim3);
319 rc.rotmat.setIdentity();
321 rc.paint(rc.getGraphics());
331 protected void xCombobox_actionPerformed(ActionEvent e)
343 protected void yCombobox_actionPerformed(ActionEvent e)
355 protected void zCombobox_actionPerformed(ActionEvent e)
361 public void outputValues_actionPerformed(ActionEvent e)
363 CutAndPasteTransfer cap = new CutAndPasteTransfer();
366 cap.setText(pcaModel.getDetails());
367 Desktop.addInternalFrame(cap,
368 MessageManager.getString("label.pca_details"), 500, 500);
369 } catch (OutOfMemoryError oom)
371 new OOMWarning("opening PCA details", oom);
377 public void showLabels_actionPerformed(ActionEvent e)
379 rc.showLabels(showLabels.getState());
383 public void print_actionPerformed(ActionEvent e)
385 PCAPrinter printer = new PCAPrinter();
390 public void originalSeqData_actionPerformed(ActionEvent e)
392 // this was cut'n'pasted from the equivalent TreePanel method - we should
393 // make this an abstract function of all jalview analysis windows
394 if (pcaModel.getSeqtrings() == null)
396 jalview.bin.Cache.log.info(
397 "Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
400 // decide if av alignment is sufficiently different to original data to
401 // warrant a new window to be created
402 // create new alignmnt window with hidden regions (unhiding hidden regions
403 // yields unaligned seqs)
404 // or create a selection box around columns in alignment view
405 // test Alignment(SeqCigar[])
409 // we try to get the associated view's gap character
410 // but this may fail if the view was closed...
411 gc = av.getGapCharacter();
412 } catch (Exception ex)
416 Object[] alAndColsel = pcaModel.getSeqtrings()
417 .getAlignmentAndHiddenColumns(gc);
419 if (alAndColsel != null && alAndColsel[0] != null)
421 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
423 AlignmentI al = new Alignment((SequenceI[]) alAndColsel[0]);
424 AlignmentI dataset = (av != null && av.getAlignment() != null)
425 ? av.getAlignment().getDataset()
429 al.setDataset(dataset);
435 AlignFrame af = new AlignFrame(al, (HiddenColumns) alAndColsel[1],
436 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
438 // >>>This is a fix for the moment, until a better solution is
440 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
442 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
445 Desktop.addInternalFrame(af, MessageManager.formatMessage(
446 "label.original_data_for_params", new String[]
447 { this.title }), AlignFrame.DEFAULT_WIDTH,
448 AlignFrame.DEFAULT_HEIGHT);
452 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
453 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
454 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
457 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
461 class PCAPrinter extends Thread implements Printable
466 PrinterJob printJob = PrinterJob.getPrinterJob();
467 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
469 printJob.setPrintable(this, pf);
471 if (printJob.printDialog())
476 } catch (Exception PrintException)
478 PrintException.printStackTrace();
484 public int print(Graphics pg, PageFormat pf, int pi)
485 throws PrinterException
487 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
489 rc.drawBackground(pg, rc.bgColour);
491 if (rc.drawAxes == true)
498 return Printable.PAGE_EXISTS;
502 return Printable.NO_SUCH_PAGE;
514 public void eps_actionPerformed(ActionEvent e)
516 makePCAImage(jalview.util.ImageMaker.TYPE.EPS);
526 public void png_actionPerformed(ActionEvent e)
528 makePCAImage(jalview.util.ImageMaker.TYPE.PNG);
531 void makePCAImage(jalview.util.ImageMaker.TYPE type)
533 int width = rc.getWidth();
534 int height = rc.getHeight();
536 jalview.util.ImageMaker im;
538 if (type == jalview.util.ImageMaker.TYPE.PNG)
540 im = new jalview.util.ImageMaker(this,
541 jalview.util.ImageMaker.TYPE.PNG, "Make PNG image from PCA",
542 width, height, null, null, null, 0, false);
544 else if (type == jalview.util.ImageMaker.TYPE.EPS)
546 im = new jalview.util.ImageMaker(this,
547 jalview.util.ImageMaker.TYPE.EPS, "Make EPS file from PCA",
548 width, height, null, this.getTitle(), null, 0, false);
552 im = new jalview.util.ImageMaker(this,
553 jalview.util.ImageMaker.TYPE.SVG, "Make SVG file from PCA",
554 width, height, null, this.getTitle(), null, 0, false);
558 if (im.getGraphics() != null)
560 rc.drawBackground(im.getGraphics(), Color.black);
561 rc.drawScene(im.getGraphics());
562 if (rc.drawAxes == true)
564 rc.drawAxes(im.getGraphics());
571 public void viewMenu_menuSelected()
573 buildAssociatedViewMenu();
576 void buildAssociatedViewMenu()
578 AlignmentPanel[] aps = PaintRefresher
579 .getAssociatedPanels(av.getSequenceSetId());
580 if (aps.length == 1 && rc.av == aps[0].av)
582 associateViewsMenu.setVisible(false);
586 associateViewsMenu.setVisible(true);
589 .getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
591 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
594 associateViewsMenu.removeAll();
596 JRadioButtonMenuItem item;
597 ButtonGroup buttonGroup = new ButtonGroup();
598 int i, iSize = aps.length;
599 final PCAPanel thisPCAPanel = this;
600 for (i = 0; i < iSize; i++)
602 final AlignmentPanel ap = aps[i];
603 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
604 buttonGroup.add(item);
605 item.addActionListener(new ActionListener()
608 public void actionPerformed(ActionEvent evt)
610 rc.applyToAllViews = false;
613 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
617 associateViewsMenu.add(item);
620 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem(
623 buttonGroup.add(itemf);
625 itemf.setSelected(rc.applyToAllViews);
626 itemf.addActionListener(new ActionListener()
629 public void actionPerformed(ActionEvent evt)
631 rc.applyToAllViews = itemf.isSelected();
634 associateViewsMenu.add(itemf);
642 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
646 protected void outputPoints_actionPerformed(ActionEvent e)
648 CutAndPasteTransfer cap = new CutAndPasteTransfer();
651 cap.setText(pcaModel.getPointsasCsv(false,
652 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
653 zCombobox.getSelectedIndex()));
654 Desktop.addInternalFrame(cap, MessageManager
655 .formatMessage("label.points_for_params", new String[]
656 { this.getTitle() }), 500, 500);
657 } catch (OutOfMemoryError oom)
659 new OOMWarning("exporting PCA points", oom);
668 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
672 protected void outputProjPoints_actionPerformed(ActionEvent e)
674 CutAndPasteTransfer cap = new CutAndPasteTransfer();
677 cap.setText(pcaModel.getPointsasCsv(true,
678 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
679 zCombobox.getSelectedIndex()));
680 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
681 "label.transformed_points_for_params", new String[]
682 { this.getTitle() }), 500, 500);
683 } catch (OutOfMemoryError oom)
685 new OOMWarning("exporting transformed PCA points", oom);
693 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
696 public void setProgressBar(String message, long id)
698 progressBar.setProgressBar(message, id);
699 // if (progressBars == null)
701 // progressBars = new Hashtable();
702 // progressBarHandlers = new Hashtable();
705 // JPanel progressPanel;
706 // Long lId = new Long(id);
707 // GridLayout layout = (GridLayout) statusPanel.getLayout();
708 // if (progressBars.get(lId) != null)
710 // progressPanel = (JPanel) progressBars.get(new Long(id));
711 // statusPanel.remove(progressPanel);
712 // progressBars.remove(lId);
713 // progressPanel = null;
714 // if (message != null)
716 // statusBar.setText(message);
718 // if (progressBarHandlers.contains(lId))
720 // progressBarHandlers.remove(lId);
722 // layout.setRows(layout.getRows() - 1);
726 // progressPanel = new JPanel(new BorderLayout(10, 5));
728 // JProgressBar progressBar = new JProgressBar();
729 // progressBar.setIndeterminate(true);
731 // progressPanel.add(new JLabel(message), BorderLayout.WEST);
732 // progressPanel.add(progressBar, BorderLayout.CENTER);
734 // layout.setRows(layout.getRows() + 1);
735 // statusPanel.add(progressPanel);
737 // progressBars.put(lId, progressPanel);
740 // // setMenusForViewport();
745 public void registerHandler(final long id,
746 final IProgressIndicatorHandler handler)
748 progressBar.registerHandler(id, handler);
749 // if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
752 // Error(MessageManager.getString("error.call_setprogressbar_before_registering_handler"));
754 // progressBarHandlers.put(new Long(id), handler);
755 // final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
756 // if (handler.canCancel())
758 // JButton cancel = new JButton(
759 // MessageManager.getString("action.cancel"));
760 // final IProgressIndicator us = this;
761 // cancel.addActionListener(new ActionListener()
765 // public void actionPerformed(ActionEvent e)
767 // handler.cancelActivity(id);
768 // us.setProgressBar(MessageManager.formatMessage("label.cancelled_params",
769 // new String[]{((JLabel) progressPanel.getComponent(0)).getText()}), id);
772 // progressPanel.add(cancel, BorderLayout.EAST);
778 * @return true if any progress bars are still active
781 public boolean operationInProgress()
783 return progressBar.operationInProgress();
787 protected void resetButton_actionPerformed(ActionEvent e)
790 top = 0; // ugly - prevents dimensionChanged events from being processed
791 xCombobox.setSelectedIndex(0);
792 yCombobox.setSelectedIndex(1);
794 zCombobox.setSelectedIndex(2);
798 * Answers true if PCA calculation is in progress, else false
802 public boolean isWorking()