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 implements Runnable,
66 private IProgressIndicator progressBar;
76 private static final int MIN_WIDTH = 470;
78 private static final int MIN_HEIGHT = 250;
83 * Creates a new PCAPanel object using default score model and parameters
87 public PCAPanel(AlignmentPanel alignPanel)
89 this(alignPanel, ScoreModels.getInstance()
90 .getDefaultModel(!alignPanel.av.getAlignment().isNucleotide())
91 .getName(), SimilarityParams.SeqSpace);
95 * Constructor given sequence data, a similarity (or distance) score model
96 * name, and score calculation parameters
102 public PCAPanel(AlignmentPanel alignPanel, String modelName,
103 SimilarityParamsI params)
106 this.av = alignPanel.av;
107 this.ap = alignPanel;
108 boolean nucleotide = av.getAlignment().isNucleotide();
110 progressBar = new ProgressBar(statusPanel, statusBar);
112 addInternalFrameListener(new InternalFrameAdapter()
115 public void internalFrameClosed(InternalFrameEvent e)
117 close_actionPerformed();
121 boolean selected = av.getSelectionGroup() != null
122 && av.getSelectionGroup().getSize() > 0;
123 AlignmentView seqstrings = av.getAlignmentView(selected);
127 seqs = av.getAlignment().getSequencesArray();
131 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
134 ScoreModelI scoreModel = ScoreModels.getInstance().getScoreModel(
136 pcaModel = new PCAModel(seqstrings, seqs, nucleotide, scoreModel,
138 PaintRefresher.Register(this, av.getSequenceSetId());
140 rc = new RotatableCanvas(alignPanel);
141 this.getContentPane().add(rc, BorderLayout.CENTER);
142 Thread worker = new Thread(this);
147 * Ensure references to potentially very large objects (the PCA matrices) are
148 * nulled when the frame is closed
150 protected void close_actionPerformed()
156 * Repopulate the options and actions under the score model menu when it is
157 * selected. Options will depend on whether 'nucleotide' or 'peptide'
158 * modelling is selected (and also possibly on whether any additional score
159 * models have been added).
162 protected void scoreModel_menuSelected()
164 scoreModelMenu.removeAll();
165 for (final ScoreModelI sm : ScoreModels.getInstance().getModels())
167 final String name = sm.getName();
168 JCheckBoxMenuItem jm = new JCheckBoxMenuItem(name);
171 * if the score model doesn't provide a description, try to look one
172 * up in the text bundle, falling back on its name
174 String tooltip = sm.getDescription();
177 tooltip = MessageManager.getStringOrReturn("label.score_model_",
180 jm.setToolTipText(tooltip);
181 jm.setSelected(pcaModel.getScoreModelName().equals(name));
182 if ((pcaModel.isNucleotide() && sm.isDNA())
183 || (!pcaModel.isNucleotide() && sm.isProtein()))
185 jm.addActionListener(new ActionListener()
188 public void actionPerformed(ActionEvent e)
190 if (!pcaModel.getScoreModelName().equals(name))
192 ScoreModelI sm2 = ScoreModels.getInstance().getScoreModel(
194 pcaModel.setScoreModel(sm2);
195 Thread worker = new Thread(PCAPanel.this);
200 scoreModelMenu.add(jm);
206 public void bgcolour_actionPerformed(ActionEvent e)
208 Color col = JColorChooser.showDialog(this,
209 MessageManager.getString("label.select_background_colour"),
225 long progId = System.currentTimeMillis();
226 IProgressIndicator progress = this;
227 String message = MessageManager.getString("label.pca_recalculating");
228 if (getParent() == null)
230 progress = ap.alignFrame;
231 message = MessageManager.getString("label.pca_calculating");
233 progress.setProgressBar(message, progId);
236 calcSettings.setEnabled(false);
239 xCombobox.setSelectedIndex(0);
240 yCombobox.setSelectedIndex(1);
241 zCombobox.setSelectedIndex(2);
243 pcaModel.updateRc(rc);
245 nuclSetting.setSelected(pcaModel.isNucleotide());
246 protSetting.setSelected(!pcaModel.isNucleotide());
247 top = pcaModel.getTop();
249 } catch (OutOfMemoryError er)
251 new OOMWarning("calculating PCA", er);
255 progress.setProgressBar("", progId);
257 calcSettings.setEnabled(true);
259 if (getParent() == null)
262 Desktop.addInternalFrame(this, MessageManager
263 .getString("label.principal_component_analysis"), 475, 450);
264 this.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
269 protected void nuclSetting_actionPerfomed(ActionEvent arg0)
271 if (!pcaModel.isNucleotide())
273 pcaModel.setNucleotide(true);
274 pcaModel.setScoreModel(ScoreModels.getInstance().getDefaultModel(
276 Thread worker = new Thread(this);
283 protected void protSetting_actionPerfomed(ActionEvent arg0)
286 if (pcaModel.isNucleotide())
288 pcaModel.setNucleotide(false);
289 pcaModel.setScoreModel(ScoreModels.getInstance()
290 .getDefaultModel(true));
291 Thread worker = new Thread(this);
299 void doDimensionChange()
306 int dim1 = top - xCombobox.getSelectedIndex();
307 int dim2 = top - yCombobox.getSelectedIndex();
308 int dim3 = top - zCombobox.getSelectedIndex();
309 pcaModel.updateRcView(dim1, dim2, dim3);
311 rc.rotmat.setIdentity();
313 rc.paint(rc.getGraphics());
323 protected void xCombobox_actionPerformed(ActionEvent e)
335 protected void yCombobox_actionPerformed(ActionEvent e)
347 protected void zCombobox_actionPerformed(ActionEvent e)
353 public void outputValues_actionPerformed(ActionEvent e)
355 CutAndPasteTransfer cap = new CutAndPasteTransfer();
358 cap.setText(pcaModel.getDetails());
359 Desktop.addInternalFrame(cap,
360 MessageManager.getString("label.pca_details"), 500, 500);
361 } catch (OutOfMemoryError oom)
363 new OOMWarning("opening PCA details", oom);
369 public void showLabels_actionPerformed(ActionEvent e)
371 rc.showLabels(showLabels.getState());
375 public void print_actionPerformed(ActionEvent e)
377 PCAPrinter printer = new PCAPrinter();
382 public void originalSeqData_actionPerformed(ActionEvent e)
384 // this was cut'n'pasted from the equivalent TreePanel method - we should
385 // make this an abstract function of all jalview analysis windows
386 if (pcaModel.getSeqtrings() == null)
388 jalview.bin.Cache.log
389 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
392 // decide if av alignment is sufficiently different to original data to
393 // warrant a new window to be created
394 // create new alignmnt window with hidden regions (unhiding hidden regions
395 // yields unaligned seqs)
396 // or create a selection box around columns in alignment view
397 // test Alignment(SeqCigar[])
401 // we try to get the associated view's gap character
402 // but this may fail if the view was closed...
403 gc = av.getGapCharacter();
404 } catch (Exception ex)
408 Object[] alAndColsel = pcaModel.getSeqtrings()
409 .getAlignmentAndHiddenColumns(gc);
411 if (alAndColsel != null && alAndColsel[0] != null)
413 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
415 AlignmentI al = new Alignment((SequenceI[]) alAndColsel[0]);
416 AlignmentI dataset = (av != null && av.getAlignment() != null) ? av
417 .getAlignment().getDataset() : null;
420 al.setDataset(dataset);
426 AlignFrame af = new AlignFrame(al, (HiddenColumns) alAndColsel[1],
427 AlignFrame.DEFAULT_WIDTH,
428 AlignFrame.DEFAULT_HEIGHT);
430 // >>>This is a fix for the moment, until a better solution is
432 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
434 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
437 Desktop.addInternalFrame(af, MessageManager.formatMessage(
438 "label.original_data_for_params",
439 new String[] { this.title }), AlignFrame.DEFAULT_WIDTH,
440 AlignFrame.DEFAULT_HEIGHT);
444 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
445 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
446 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
449 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
453 class PCAPrinter extends Thread implements Printable
458 PrinterJob printJob = PrinterJob.getPrinterJob();
459 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
461 printJob.setPrintable(this, pf);
463 if (printJob.printDialog())
468 } catch (Exception PrintException)
470 PrintException.printStackTrace();
476 public int print(Graphics pg, PageFormat pf, int pi)
477 throws PrinterException
479 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
481 rc.drawBackground(pg, rc.bgColour);
483 if (rc.drawAxes == true)
490 return Printable.PAGE_EXISTS;
494 return Printable.NO_SUCH_PAGE;
506 public void eps_actionPerformed(ActionEvent e)
508 makePCAImage(jalview.util.ImageMaker.TYPE.EPS);
518 public void png_actionPerformed(ActionEvent e)
520 makePCAImage(jalview.util.ImageMaker.TYPE.PNG);
523 void makePCAImage(jalview.util.ImageMaker.TYPE type)
525 int width = rc.getWidth();
526 int height = rc.getHeight();
528 jalview.util.ImageMaker im;
530 if (type == jalview.util.ImageMaker.TYPE.PNG)
532 im = new jalview.util.ImageMaker(this,
533 jalview.util.ImageMaker.TYPE.PNG, "Make PNG image from PCA",
534 width, height, null, null, null, 0, false);
536 else if (type == jalview.util.ImageMaker.TYPE.EPS)
538 im = new jalview.util.ImageMaker(this,
539 jalview.util.ImageMaker.TYPE.EPS, "Make EPS file from PCA",
540 width, height, null, this.getTitle(), null, 0, false);
544 im = new jalview.util.ImageMaker(this,
545 jalview.util.ImageMaker.TYPE.SVG, "Make SVG file from PCA",
546 width, height, null, this.getTitle(), null, 0, false);
550 if (im.getGraphics() != null)
552 rc.drawBackground(im.getGraphics(), Color.black);
553 rc.drawScene(im.getGraphics());
554 if (rc.drawAxes == true)
556 rc.drawAxes(im.getGraphics());
563 public void viewMenu_menuSelected()
565 buildAssociatedViewMenu();
568 void buildAssociatedViewMenu()
570 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
571 .getSequenceSetId());
572 if (aps.length == 1 && rc.av == aps[0].av)
574 associateViewsMenu.setVisible(false);
578 associateViewsMenu.setVisible(true);
580 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
582 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
585 associateViewsMenu.removeAll();
587 JRadioButtonMenuItem item;
588 ButtonGroup buttonGroup = new ButtonGroup();
589 int i, iSize = aps.length;
590 final PCAPanel thisPCAPanel = this;
591 for (i = 0; i < iSize; i++)
593 final AlignmentPanel ap = aps[i];
594 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
595 buttonGroup.add(item);
596 item.addActionListener(new ActionListener()
599 public void actionPerformed(ActionEvent evt)
601 rc.applyToAllViews = false;
604 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
608 associateViewsMenu.add(item);
611 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
613 buttonGroup.add(itemf);
615 itemf.setSelected(rc.applyToAllViews);
616 itemf.addActionListener(new ActionListener()
619 public void actionPerformed(ActionEvent evt)
621 rc.applyToAllViews = itemf.isSelected();
624 associateViewsMenu.add(itemf);
632 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
636 protected void outputPoints_actionPerformed(ActionEvent e)
638 CutAndPasteTransfer cap = new CutAndPasteTransfer();
641 cap.setText(pcaModel.getPointsasCsv(false,
642 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
643 zCombobox.getSelectedIndex()));
644 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
645 "label.points_for_params", new String[] { this.getTitle() }),
647 } catch (OutOfMemoryError oom)
649 new OOMWarning("exporting PCA points", oom);
658 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
662 protected void outputProjPoints_actionPerformed(ActionEvent e)
664 CutAndPasteTransfer cap = new CutAndPasteTransfer();
667 cap.setText(pcaModel.getPointsasCsv(true,
668 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
669 zCombobox.getSelectedIndex()));
670 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
671 "label.transformed_points_for_params",
672 new String[] { this.getTitle() }), 500, 500);
673 } catch (OutOfMemoryError oom)
675 new OOMWarning("exporting transformed PCA points", oom);
683 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
686 public void setProgressBar(String message, long id)
688 progressBar.setProgressBar(message, id);
689 // if (progressBars == null)
691 // progressBars = new Hashtable();
692 // progressBarHandlers = new Hashtable();
695 // JPanel progressPanel;
696 // Long lId = new Long(id);
697 // GridLayout layout = (GridLayout) statusPanel.getLayout();
698 // if (progressBars.get(lId) != null)
700 // progressPanel = (JPanel) progressBars.get(new Long(id));
701 // statusPanel.remove(progressPanel);
702 // progressBars.remove(lId);
703 // progressPanel = null;
704 // if (message != null)
706 // statusBar.setText(message);
708 // if (progressBarHandlers.contains(lId))
710 // progressBarHandlers.remove(lId);
712 // layout.setRows(layout.getRows() - 1);
716 // progressPanel = new JPanel(new BorderLayout(10, 5));
718 // JProgressBar progressBar = new JProgressBar();
719 // progressBar.setIndeterminate(true);
721 // progressPanel.add(new JLabel(message), BorderLayout.WEST);
722 // progressPanel.add(progressBar, BorderLayout.CENTER);
724 // layout.setRows(layout.getRows() + 1);
725 // statusPanel.add(progressPanel);
727 // progressBars.put(lId, progressPanel);
730 // // setMenusForViewport();
735 public void registerHandler(final long id,
736 final IProgressIndicatorHandler handler)
738 progressBar.registerHandler(id, handler);
739 // if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
742 // Error(MessageManager.getString("error.call_setprogressbar_before_registering_handler"));
744 // progressBarHandlers.put(new Long(id), handler);
745 // final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
746 // if (handler.canCancel())
748 // JButton cancel = new JButton(
749 // MessageManager.getString("action.cancel"));
750 // final IProgressIndicator us = this;
751 // cancel.addActionListener(new ActionListener()
755 // public void actionPerformed(ActionEvent e)
757 // handler.cancelActivity(id);
758 // us.setProgressBar(MessageManager.formatMessage("label.cancelled_params",
759 // new String[]{((JLabel) progressPanel.getComponent(0)).getText()}), id);
762 // progressPanel.add(cancel, BorderLayout.EAST);
768 * @return true if any progress bars are still active
771 public boolean operationInProgress()
773 return progressBar.operationInProgress();
777 protected void resetButton_actionPerformed(ActionEvent e)
780 top = 0; // ugly - prevents dimensionChanged events from being processed
781 xCombobox.setSelectedIndex(0);
782 yCombobox.setSelectedIndex(1);
784 zCombobox.setSelectedIndex(2);