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.datamodel.Alignment;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.AlignmentView;
26 import jalview.datamodel.ColumnSelection;
27 import jalview.datamodel.SeqCigar;
28 import jalview.datamodel.SequenceI;
29 import jalview.jbgui.GPCAPanel;
30 import jalview.schemes.ResidueProperties;
31 import jalview.util.MessageManager;
32 import jalview.viewmodel.AlignmentViewport;
33 import jalview.viewmodel.PCAModel;
35 import java.awt.BorderLayout;
36 import java.awt.Color;
37 import java.awt.Graphics;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.print.PageFormat;
41 import java.awt.print.Printable;
42 import java.awt.print.PrinterException;
43 import java.awt.print.PrinterJob;
45 import javax.swing.ButtonGroup;
46 import javax.swing.JCheckBoxMenuItem;
47 import javax.swing.JColorChooser;
48 import javax.swing.JMenuItem;
49 import javax.swing.JOptionPane;
50 import javax.swing.JRadioButtonMenuItem;
58 public class PCAPanel extends GPCAPanel implements Runnable,
62 private IProgressIndicator progressBar;
75 * Creates a new PCAPanel object.
82 public PCAPanel(AlignmentPanel ap)
87 progressBar = new ProgressBar(statusPanel, statusBar);
89 boolean sameLength = true;
90 boolean selected = av.getSelectionGroup() != null
91 && av.getSelectionGroup().getSize() > 0;
92 AlignmentView seqstrings = av.getAlignmentView(selected);
93 boolean nucleotide = av.getAlignment().isNucleotide();
97 seqs = av.getAlignment().getSequencesArray();
101 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
103 SeqCigar sq[] = seqstrings.getSequences();
104 int length = sq[0].getWidth();
106 for (int i = 0; i < seqs.length; i++)
108 if (sq[i].getWidth() != length)
117 JOptionPane.showMessageDialog(Desktop.desktop,
118 MessageManager.getString("label.pca_sequences_not_aligned"),
119 MessageManager.getString("label.sequences_not_aligned"),
120 JOptionPane.WARNING_MESSAGE);
124 pcaModel = new PCAModel(seqstrings, seqs, nucleotide);
125 PaintRefresher.Register(this, av.getSequenceSetId());
127 rc = new RotatableCanvas(ap);
128 this.getContentPane().add(rc, BorderLayout.CENTER);
129 Thread worker = new Thread(this);
134 protected void scoreMatrix_menuSelected()
136 scoreMatrixMenu.removeAll();
137 for (final String sm : ResidueProperties.scoreMatrices.keySet())
139 if (ResidueProperties.getScoreMatrix(sm) != null)
141 // create an entry for this score matrix for use in PCA
142 JCheckBoxMenuItem jm = new JCheckBoxMenuItem();
143 jm.setText(MessageManager.getStringOrReturn("label.score_model_",
145 jm.setSelected(pcaModel.getScore_matrix().equals(sm));
146 if ((ResidueProperties.scoreMatrices.get(sm).isDNA() && ResidueProperties.scoreMatrices
147 .get(sm).isProtein())
148 || pcaModel.isNucleotide() == ResidueProperties.scoreMatrices
151 final PCAPanel us = this;
152 jm.addActionListener(new ActionListener()
155 public void actionPerformed(ActionEvent e)
157 if (!pcaModel.getScore_matrix().equals(sm))
159 pcaModel.setScore_matrix(sm);
160 Thread worker = new Thread(us);
165 scoreMatrixMenu.add(jm);
172 public void bgcolour_actionPerformed(ActionEvent e)
174 Color col = JColorChooser.showDialog(this,
175 MessageManager.getString("label.select_backgroud_colour"),
191 long progId = System.currentTimeMillis();
192 IProgressIndicator progress = this;
193 String message = MessageManager.getString("label.pca_recalculating");
194 if (getParent() == null)
196 progress = ap.alignFrame;
197 message = MessageManager.getString("label.pca_calculating");
199 progress.setProgressBar(message, progId);
202 calcSettings.setEnabled(false);
205 xCombobox.setSelectedIndex(0);
206 yCombobox.setSelectedIndex(1);
207 zCombobox.setSelectedIndex(2);
209 pcaModel.updateRc(rc);
211 nuclSetting.setSelected(pcaModel.isNucleotide());
212 protSetting.setSelected(!pcaModel.isNucleotide());
213 jvVersionSetting.setSelected(pcaModel.isJvCalcMode());
214 top = pcaModel.getTop();
216 } catch (OutOfMemoryError er)
218 new OOMWarning("calculating PCA", er);
222 progress.setProgressBar("", progId);
224 calcSettings.setEnabled(true);
226 if (getParent() == null)
229 Desktop.addInternalFrame(this, MessageManager
230 .getString("label.principal_component_analysis"), 475, 450);
235 protected void nuclSetting_actionPerfomed(ActionEvent arg0)
237 if (!pcaModel.isNucleotide())
239 pcaModel.setNucleotide(true);
240 pcaModel.setScore_matrix("DNA");
241 Thread worker = new Thread(this);
248 protected void protSetting_actionPerfomed(ActionEvent arg0)
251 if (pcaModel.isNucleotide())
253 pcaModel.setNucleotide(false);
254 pcaModel.setScore_matrix("BLOSUM62");
255 Thread worker = new Thread(this);
261 protected void jvVersionSetting_actionPerfomed(ActionEvent arg0)
263 pcaModel.setJvCalcMode(jvVersionSetting.isSelected());
264 Thread worker = new Thread(this);
271 void doDimensionChange()
278 int dim1 = top - xCombobox.getSelectedIndex();
279 int dim2 = top - yCombobox.getSelectedIndex();
280 int dim3 = top - zCombobox.getSelectedIndex();
281 pcaModel.updateRcView(dim1, dim2, dim3);
283 rc.rotmat.setIdentity();
285 rc.paint(rc.getGraphics());
295 protected void xCombobox_actionPerformed(ActionEvent e)
307 protected void yCombobox_actionPerformed(ActionEvent e)
319 protected void zCombobox_actionPerformed(ActionEvent e)
325 public void outputValues_actionPerformed(ActionEvent e)
327 CutAndPasteTransfer cap = new CutAndPasteTransfer();
330 cap.setText(pcaModel.getDetails());
331 Desktop.addInternalFrame(cap,
332 MessageManager.getString("label.pca_details"), 500, 500);
333 } catch (OutOfMemoryError oom)
335 new OOMWarning("opening PCA details", oom);
341 public void showLabels_actionPerformed(ActionEvent e)
343 rc.showLabels(showLabels.getState());
347 public void print_actionPerformed(ActionEvent e)
349 PCAPrinter printer = new PCAPrinter();
354 public void originalSeqData_actionPerformed(ActionEvent e)
356 // this was cut'n'pasted from the equivalent TreePanel method - we should
357 // make this an abstract function of all jalview analysis windows
358 if (pcaModel.getSeqtrings() == null)
360 jalview.bin.Cache.log
361 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
364 // decide if av alignment is sufficiently different to original data to
365 // warrant a new window to be created
366 // create new alignmnt window with hidden regions (unhiding hidden regions
367 // yields unaligned seqs)
368 // or create a selection box around columns in alignment view
369 // test Alignment(SeqCigar[])
373 // we try to get the associated view's gap character
374 // but this may fail if the view was closed...
375 gc = av.getGapCharacter();
376 } catch (Exception ex)
380 Object[] alAndColsel = pcaModel.getSeqtrings()
381 .getAlignmentAndColumnSelection(gc);
383 if (alAndColsel != null && alAndColsel[0] != null)
385 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
387 AlignmentI al = new Alignment((SequenceI[]) alAndColsel[0]);
388 AlignmentI dataset = (av != null && av.getAlignment() != null) ? av
389 .getAlignment().getDataset() : null;
392 al.setDataset(dataset);
398 AlignFrame af = new AlignFrame(al,
399 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
400 AlignFrame.DEFAULT_HEIGHT);
402 // >>>This is a fix for the moment, until a better solution is
404 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
406 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
409 Desktop.addInternalFrame(af, MessageManager.formatMessage(
410 "label.original_data_for_params",
411 new String[] { this.title }), AlignFrame.DEFAULT_WIDTH,
412 AlignFrame.DEFAULT_HEIGHT);
416 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
417 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
418 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
421 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
425 class PCAPrinter extends Thread implements Printable
430 PrinterJob printJob = PrinterJob.getPrinterJob();
431 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
433 printJob.setPrintable(this, pf);
435 if (printJob.printDialog())
440 } catch (Exception PrintException)
442 PrintException.printStackTrace();
448 public int print(Graphics pg, PageFormat pf, int pi)
449 throws PrinterException
451 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
453 rc.drawBackground(pg, rc.bgColour);
455 if (rc.drawAxes == true)
462 return Printable.PAGE_EXISTS;
466 return Printable.NO_SUCH_PAGE;
478 public void eps_actionPerformed(ActionEvent e)
480 makePCAImage(jalview.util.ImageMaker.TYPE.EPS);
490 public void png_actionPerformed(ActionEvent e)
492 makePCAImage(jalview.util.ImageMaker.TYPE.PNG);
495 void makePCAImage(jalview.util.ImageMaker.TYPE type)
497 int width = rc.getWidth();
498 int height = rc.getHeight();
500 jalview.util.ImageMaker im;
502 if (type == jalview.util.ImageMaker.TYPE.PNG)
504 im = new jalview.util.ImageMaker(this,
505 jalview.util.ImageMaker.TYPE.PNG, "Make PNG image from PCA",
506 width, height, null, null, null, 0, false);
508 else if (type == jalview.util.ImageMaker.TYPE.EPS)
510 im = new jalview.util.ImageMaker(this,
511 jalview.util.ImageMaker.TYPE.EPS, "Make EPS file from PCA",
512 width, height, null, this.getTitle(), null, 0, false);
516 im = new jalview.util.ImageMaker(this,
517 jalview.util.ImageMaker.TYPE.SVG, "Make SVG file from PCA",
518 width, height, null, this.getTitle(), null, 0, false);
522 if (im.getGraphics() != null)
524 rc.drawBackground(im.getGraphics(), Color.black);
525 rc.drawScene(im.getGraphics());
526 if (rc.drawAxes == true)
528 rc.drawAxes(im.getGraphics());
535 public void viewMenu_menuSelected()
537 buildAssociatedViewMenu();
540 void buildAssociatedViewMenu()
542 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
543 .getSequenceSetId());
544 if (aps.length == 1 && rc.av == aps[0].av)
546 associateViewsMenu.setVisible(false);
550 associateViewsMenu.setVisible(true);
552 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
554 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
557 associateViewsMenu.removeAll();
559 JRadioButtonMenuItem item;
560 ButtonGroup buttonGroup = new ButtonGroup();
561 int i, iSize = aps.length;
562 final PCAPanel thisPCAPanel = this;
563 for (i = 0; i < iSize; i++)
565 final AlignmentPanel ap = aps[i];
566 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
567 buttonGroup.add(item);
568 item.addActionListener(new ActionListener()
571 public void actionPerformed(ActionEvent evt)
573 rc.applyToAllViews = false;
576 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
580 associateViewsMenu.add(item);
583 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
585 buttonGroup.add(itemf);
587 itemf.setSelected(rc.applyToAllViews);
588 itemf.addActionListener(new ActionListener()
591 public void actionPerformed(ActionEvent evt)
593 rc.applyToAllViews = itemf.isSelected();
596 associateViewsMenu.add(itemf);
604 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
608 protected void outputPoints_actionPerformed(ActionEvent e)
610 CutAndPasteTransfer cap = new CutAndPasteTransfer();
613 cap.setText(pcaModel.getPointsasCsv(false,
614 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
615 zCombobox.getSelectedIndex()));
616 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
617 "label.points_for_params", new String[] { this.getTitle() }),
619 } catch (OutOfMemoryError oom)
621 new OOMWarning("exporting PCA points", oom);
630 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
634 protected void outputProjPoints_actionPerformed(ActionEvent e)
636 CutAndPasteTransfer cap = new CutAndPasteTransfer();
639 cap.setText(pcaModel.getPointsasCsv(true,
640 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
641 zCombobox.getSelectedIndex()));
642 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
643 "label.transformed_points_for_params",
644 new String[] { this.getTitle() }), 500, 500);
645 } catch (OutOfMemoryError oom)
647 new OOMWarning("exporting transformed PCA points", oom);
655 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
658 public void setProgressBar(String message, long id)
660 progressBar.setProgressBar(message, id);
661 // if (progressBars == null)
663 // progressBars = new Hashtable();
664 // progressBarHandlers = new Hashtable();
667 // JPanel progressPanel;
668 // Long lId = new Long(id);
669 // GridLayout layout = (GridLayout) statusPanel.getLayout();
670 // if (progressBars.get(lId) != null)
672 // progressPanel = (JPanel) progressBars.get(new Long(id));
673 // statusPanel.remove(progressPanel);
674 // progressBars.remove(lId);
675 // progressPanel = null;
676 // if (message != null)
678 // statusBar.setText(message);
680 // if (progressBarHandlers.contains(lId))
682 // progressBarHandlers.remove(lId);
684 // layout.setRows(layout.getRows() - 1);
688 // progressPanel = new JPanel(new BorderLayout(10, 5));
690 // JProgressBar progressBar = new JProgressBar();
691 // progressBar.setIndeterminate(true);
693 // progressPanel.add(new JLabel(message), BorderLayout.WEST);
694 // progressPanel.add(progressBar, BorderLayout.CENTER);
696 // layout.setRows(layout.getRows() + 1);
697 // statusPanel.add(progressPanel);
699 // progressBars.put(lId, progressPanel);
702 // // setMenusForViewport();
707 public void registerHandler(final long id,
708 final IProgressIndicatorHandler handler)
710 progressBar.registerHandler(id, handler);
711 // if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
714 // Error(MessageManager.getString("error.call_setprogressbar_before_registering_handler"));
716 // progressBarHandlers.put(new Long(id), handler);
717 // final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
718 // if (handler.canCancel())
720 // JButton cancel = new JButton(
721 // MessageManager.getString("action.cancel"));
722 // final IProgressIndicator us = this;
723 // cancel.addActionListener(new ActionListener()
727 // public void actionPerformed(ActionEvent e)
729 // handler.cancelActivity(id);
730 // us.setProgressBar(MessageManager.formatMessage("label.cancelled_params",
731 // new String[]{((JLabel) progressPanel.getComponent(0)).getText()}), id);
734 // progressPanel.add(cancel, BorderLayout.EAST);
740 * @return true if any progress bars are still active
743 public boolean operationInProgress()
745 return progressBar.operationInProgress();
749 protected void resetButton_actionPerformed(ActionEvent e)
752 top = 0; // ugly - prevents dimensionChanged events from being processed
753 xCombobox.setSelectedIndex(0);
754 yCombobox.setSelectedIndex(1);
756 zCombobox.setSelectedIndex(2);