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.ScoreMatrix;
24 import jalview.analysis.scoremodels.ScoreModels;
25 import jalview.api.analysis.ScoreModelI;
26 import jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.AlignmentView;
29 import jalview.datamodel.ColumnSelection;
30 import jalview.datamodel.SeqCigar;
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.Graphics;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.print.PageFormat;
43 import java.awt.print.Printable;
44 import java.awt.print.PrinterException;
45 import java.awt.print.PrinterJob;
47 import javax.swing.ButtonGroup;
48 import javax.swing.JCheckBoxMenuItem;
49 import javax.swing.JColorChooser;
50 import javax.swing.JMenuItem;
51 import javax.swing.JRadioButtonMenuItem;
52 import javax.swing.event.InternalFrameAdapter;
53 import javax.swing.event.InternalFrameEvent;
61 public class PCAPanel extends GPCAPanel implements Runnable,
65 private IProgressIndicator progressBar;
78 * Creates a new PCAPanel object.
85 public PCAPanel(AlignmentPanel ap)
91 progressBar = new ProgressBar(statusPanel, statusBar);
93 boolean sameLength = true;
94 boolean selected = av.getSelectionGroup() != null
95 && av.getSelectionGroup().getSize() > 0;
96 AlignmentView seqstrings = av.getAlignmentView(selected);
97 boolean nucleotide = av.getAlignment().isNucleotide();
101 seqs = av.getAlignment().getSequencesArray();
105 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
107 SeqCigar sq[] = seqstrings.getSequences();
108 int length = sq[0].getWidth();
110 for (int i = 0; i < seqs.length; i++)
112 if (sq[i].getWidth() != length)
121 JvOptionPane.showMessageDialog(Desktop.desktop,
122 MessageManager.getString("label.pca_sequences_not_aligned"),
123 MessageManager.getString("label.sequences_not_aligned"),
124 JvOptionPane.WARNING_MESSAGE);
129 addInternalFrameListener(new InternalFrameAdapter()
132 public void internalFrameClosed(InternalFrameEvent e)
134 close_actionPerformed();
138 pcaModel = new PCAModel(seqstrings, seqs, nucleotide);
139 PaintRefresher.Register(this, av.getSequenceSetId());
141 rc = new RotatableCanvas(ap);
142 this.getContentPane().add(rc, BorderLayout.CENTER);
143 Thread worker = new Thread(this);
148 * Ensure references to potentially very large objects (the PCA matrices) are
149 * nulled when the frame is closed
151 protected void close_actionPerformed()
157 protected void scoreMatrix_menuSelected()
159 scoreMatrixMenu.removeAll();
160 for (ScoreModelI sm : ScoreModels.getInstance().getModels())
162 if (sm instanceof ScoreMatrix)
164 final String name = sm.getName();
165 // create an entry for this score matrix for use in PCA
166 JCheckBoxMenuItem jm = new JCheckBoxMenuItem();
167 jm.setText(MessageManager.getStringOrReturn("label.score_model_",
169 jm.setSelected(pcaModel.getScore_matrix().equals(name));
170 if ((!pcaModel.isNucleotide() && !sm.isDNA())
171 || (pcaModel.isNucleotide() && sm.isDNA()))
173 jm.addActionListener(new ActionListener()
176 public void actionPerformed(ActionEvent e)
178 if (!pcaModel.getScore_matrix().equals(name))
180 pcaModel.setScore_matrix(name);
181 Thread worker = new Thread(PCAPanel.this);
186 scoreMatrixMenu.add(jm);
193 public void bgcolour_actionPerformed(ActionEvent e)
195 Color col = JColorChooser.showDialog(this,
196 MessageManager.getString("label.select_background_colour"),
212 long progId = System.currentTimeMillis();
213 IProgressIndicator progress = this;
214 String message = MessageManager.getString("label.pca_recalculating");
215 if (getParent() == null)
217 progress = ap.alignFrame;
218 message = MessageManager.getString("label.pca_calculating");
220 progress.setProgressBar(message, progId);
223 calcSettings.setEnabled(false);
226 xCombobox.setSelectedIndex(0);
227 yCombobox.setSelectedIndex(1);
228 zCombobox.setSelectedIndex(2);
230 pcaModel.updateRc(rc);
232 nuclSetting.setSelected(pcaModel.isNucleotide());
233 protSetting.setSelected(!pcaModel.isNucleotide());
234 jvVersionSetting.setSelected(pcaModel.isJvCalcMode());
235 top = pcaModel.getTop();
237 } catch (OutOfMemoryError er)
239 new OOMWarning("calculating PCA", er);
243 progress.setProgressBar("", progId);
245 calcSettings.setEnabled(true);
247 if (getParent() == null)
250 Desktop.addInternalFrame(this, MessageManager
251 .getString("label.principal_component_analysis"), 475, 450);
256 protected void nuclSetting_actionPerfomed(ActionEvent arg0)
258 if (!pcaModel.isNucleotide())
260 pcaModel.setNucleotide(true);
261 pcaModel.setScore_matrix("DNA");
262 Thread worker = new Thread(this);
269 protected void protSetting_actionPerfomed(ActionEvent arg0)
272 if (pcaModel.isNucleotide())
274 pcaModel.setNucleotide(false);
275 pcaModel.setScore_matrix("BLOSUM62");
276 Thread worker = new Thread(this);
282 protected void jvVersionSetting_actionPerfomed(ActionEvent arg0)
284 pcaModel.setJvCalcMode(jvVersionSetting.isSelected());
285 Thread worker = new Thread(this);
292 void doDimensionChange()
299 int dim1 = top - xCombobox.getSelectedIndex();
300 int dim2 = top - yCombobox.getSelectedIndex();
301 int dim3 = top - zCombobox.getSelectedIndex();
302 pcaModel.updateRcView(dim1, dim2, dim3);
304 rc.rotmat.setIdentity();
306 rc.paint(rc.getGraphics());
316 protected void xCombobox_actionPerformed(ActionEvent e)
328 protected void yCombobox_actionPerformed(ActionEvent e)
340 protected void zCombobox_actionPerformed(ActionEvent e)
346 public void outputValues_actionPerformed(ActionEvent e)
348 CutAndPasteTransfer cap = new CutAndPasteTransfer();
351 cap.setText(pcaModel.getDetails());
352 Desktop.addInternalFrame(cap,
353 MessageManager.getString("label.pca_details"), 500, 500);
354 } catch (OutOfMemoryError oom)
356 new OOMWarning("opening PCA details", oom);
362 public void showLabels_actionPerformed(ActionEvent e)
364 rc.showLabels(showLabels.getState());
368 public void print_actionPerformed(ActionEvent e)
370 PCAPrinter printer = new PCAPrinter();
375 public void originalSeqData_actionPerformed(ActionEvent e)
377 // this was cut'n'pasted from the equivalent TreePanel method - we should
378 // make this an abstract function of all jalview analysis windows
379 if (pcaModel.getSeqtrings() == null)
381 jalview.bin.Cache.log
382 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
385 // decide if av alignment is sufficiently different to original data to
386 // warrant a new window to be created
387 // create new alignmnt window with hidden regions (unhiding hidden regions
388 // yields unaligned seqs)
389 // or create a selection box around columns in alignment view
390 // test Alignment(SeqCigar[])
394 // we try to get the associated view's gap character
395 // but this may fail if the view was closed...
396 gc = av.getGapCharacter();
397 } catch (Exception ex)
401 Object[] alAndColsel = pcaModel.getSeqtrings()
402 .getAlignmentAndColumnSelection(gc);
404 if (alAndColsel != null && alAndColsel[0] != null)
406 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
408 AlignmentI al = new Alignment((SequenceI[]) alAndColsel[0]);
409 AlignmentI dataset = (av != null && av.getAlignment() != null) ? av
410 .getAlignment().getDataset() : null;
413 al.setDataset(dataset);
419 AlignFrame af = new AlignFrame(al,
420 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
421 AlignFrame.DEFAULT_HEIGHT);
423 // >>>This is a fix for the moment, until a better solution is
425 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
427 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
430 Desktop.addInternalFrame(af, MessageManager.formatMessage(
431 "label.original_data_for_params",
432 new String[] { this.title }), AlignFrame.DEFAULT_WIDTH,
433 AlignFrame.DEFAULT_HEIGHT);
437 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
438 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
439 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
442 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
446 class PCAPrinter extends Thread implements Printable
451 PrinterJob printJob = PrinterJob.getPrinterJob();
452 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
454 printJob.setPrintable(this, pf);
456 if (printJob.printDialog())
461 } catch (Exception PrintException)
463 PrintException.printStackTrace();
469 public int print(Graphics pg, PageFormat pf, int pi)
470 throws PrinterException
472 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
474 rc.drawBackground(pg, rc.bgColour);
476 if (rc.drawAxes == true)
483 return Printable.PAGE_EXISTS;
487 return Printable.NO_SUCH_PAGE;
499 public void eps_actionPerformed(ActionEvent e)
501 makePCAImage(jalview.util.ImageMaker.TYPE.EPS);
511 public void png_actionPerformed(ActionEvent e)
513 makePCAImage(jalview.util.ImageMaker.TYPE.PNG);
516 void makePCAImage(jalview.util.ImageMaker.TYPE type)
518 int width = rc.getWidth();
519 int height = rc.getHeight();
521 jalview.util.ImageMaker im;
523 if (type == jalview.util.ImageMaker.TYPE.PNG)
525 im = new jalview.util.ImageMaker(this,
526 jalview.util.ImageMaker.TYPE.PNG, "Make PNG image from PCA",
527 width, height, null, null, null, 0, false);
529 else if (type == jalview.util.ImageMaker.TYPE.EPS)
531 im = new jalview.util.ImageMaker(this,
532 jalview.util.ImageMaker.TYPE.EPS, "Make EPS file from PCA",
533 width, height, null, this.getTitle(), null, 0, false);
537 im = new jalview.util.ImageMaker(this,
538 jalview.util.ImageMaker.TYPE.SVG, "Make SVG file from PCA",
539 width, height, null, this.getTitle(), null, 0, false);
543 if (im.getGraphics() != null)
545 rc.drawBackground(im.getGraphics(), Color.black);
546 rc.drawScene(im.getGraphics());
547 if (rc.drawAxes == true)
549 rc.drawAxes(im.getGraphics());
556 public void viewMenu_menuSelected()
558 buildAssociatedViewMenu();
561 void buildAssociatedViewMenu()
563 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
564 .getSequenceSetId());
565 if (aps.length == 1 && rc.av == aps[0].av)
567 associateViewsMenu.setVisible(false);
571 associateViewsMenu.setVisible(true);
573 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
575 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
578 associateViewsMenu.removeAll();
580 JRadioButtonMenuItem item;
581 ButtonGroup buttonGroup = new ButtonGroup();
582 int i, iSize = aps.length;
583 final PCAPanel thisPCAPanel = this;
584 for (i = 0; i < iSize; i++)
586 final AlignmentPanel ap = aps[i];
587 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
588 buttonGroup.add(item);
589 item.addActionListener(new ActionListener()
592 public void actionPerformed(ActionEvent evt)
594 rc.applyToAllViews = false;
597 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
601 associateViewsMenu.add(item);
604 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
606 buttonGroup.add(itemf);
608 itemf.setSelected(rc.applyToAllViews);
609 itemf.addActionListener(new ActionListener()
612 public void actionPerformed(ActionEvent evt)
614 rc.applyToAllViews = itemf.isSelected();
617 associateViewsMenu.add(itemf);
625 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
629 protected void outputPoints_actionPerformed(ActionEvent e)
631 CutAndPasteTransfer cap = new CutAndPasteTransfer();
634 cap.setText(pcaModel.getPointsasCsv(false,
635 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
636 zCombobox.getSelectedIndex()));
637 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
638 "label.points_for_params", new String[] { this.getTitle() }),
640 } catch (OutOfMemoryError oom)
642 new OOMWarning("exporting PCA points", oom);
651 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
655 protected void outputProjPoints_actionPerformed(ActionEvent e)
657 CutAndPasteTransfer cap = new CutAndPasteTransfer();
660 cap.setText(pcaModel.getPointsasCsv(true,
661 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
662 zCombobox.getSelectedIndex()));
663 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
664 "label.transformed_points_for_params",
665 new String[] { this.getTitle() }), 500, 500);
666 } catch (OutOfMemoryError oom)
668 new OOMWarning("exporting transformed PCA points", oom);
676 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
679 public void setProgressBar(String message, long id)
681 progressBar.setProgressBar(message, id);
682 // if (progressBars == null)
684 // progressBars = new Hashtable();
685 // progressBarHandlers = new Hashtable();
688 // JPanel progressPanel;
689 // Long lId = new Long(id);
690 // GridLayout layout = (GridLayout) statusPanel.getLayout();
691 // if (progressBars.get(lId) != null)
693 // progressPanel = (JPanel) progressBars.get(new Long(id));
694 // statusPanel.remove(progressPanel);
695 // progressBars.remove(lId);
696 // progressPanel = null;
697 // if (message != null)
699 // statusBar.setText(message);
701 // if (progressBarHandlers.contains(lId))
703 // progressBarHandlers.remove(lId);
705 // layout.setRows(layout.getRows() - 1);
709 // progressPanel = new JPanel(new BorderLayout(10, 5));
711 // JProgressBar progressBar = new JProgressBar();
712 // progressBar.setIndeterminate(true);
714 // progressPanel.add(new JLabel(message), BorderLayout.WEST);
715 // progressPanel.add(progressBar, BorderLayout.CENTER);
717 // layout.setRows(layout.getRows() + 1);
718 // statusPanel.add(progressPanel);
720 // progressBars.put(lId, progressPanel);
723 // // setMenusForViewport();
728 public void registerHandler(final long id,
729 final IProgressIndicatorHandler handler)
731 progressBar.registerHandler(id, handler);
732 // if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
735 // Error(MessageManager.getString("error.call_setprogressbar_before_registering_handler"));
737 // progressBarHandlers.put(new Long(id), handler);
738 // final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
739 // if (handler.canCancel())
741 // JButton cancel = new JButton(
742 // MessageManager.getString("action.cancel"));
743 // final IProgressIndicator us = this;
744 // cancel.addActionListener(new ActionListener()
748 // public void actionPerformed(ActionEvent e)
750 // handler.cancelActivity(id);
751 // us.setProgressBar(MessageManager.formatMessage("label.cancelled_params",
752 // new String[]{((JLabel) progressPanel.getComponent(0)).getText()}), id);
755 // progressPanel.add(cancel, BorderLayout.EAST);
761 * @return true if any progress bars are still active
764 public boolean operationInProgress()
766 return progressBar.operationInProgress();
770 protected void resetButton_actionPerformed(ActionEvent e)
773 top = 0; // ugly - prevents dimensionChanged events from being processed
774 xCombobox.setSelectedIndex(0);
775 yCombobox.setSelectedIndex(1);
777 zCombobox.setSelectedIndex(2);