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 java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Graphics;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.awt.print.PageFormat;
29 import java.awt.print.Printable;
30 import java.awt.print.PrinterException;
31 import java.awt.print.PrinterJob;
33 import javax.swing.ButtonGroup;
34 import javax.swing.JCheckBoxMenuItem;
35 import javax.swing.JColorChooser;
36 import javax.swing.JMenuItem;
37 import javax.swing.JOptionPane;
38 import javax.swing.JRadioButtonMenuItem;
40 import jalview.datamodel.Alignment;
41 import jalview.datamodel.AlignmentView;
42 import jalview.datamodel.ColumnSelection;
43 import jalview.datamodel.SeqCigar;
44 import jalview.datamodel.SequenceI;
45 import jalview.jbgui.GPCAPanel;
46 import jalview.schemes.ResidueProperties;
47 import jalview.util.MessageManager;
48 import jalview.viewmodel.AlignmentViewport;
49 import jalview.viewmodel.PCAModel;
57 public class PCAPanel extends GPCAPanel implements Runnable,
61 private IProgressIndicator progressBar;
74 * Creates a new PCAPanel object.
81 public PCAPanel(AlignmentPanel ap)
86 progressBar = new ProgressBar(statusPanel, statusBar);
88 boolean sameLength = true;
89 boolean selected = av.getSelectionGroup() != null
90 && av.getSelectionGroup().getSize() > 0;
91 AlignmentView seqstrings = av.getAlignmentView(selected);
92 boolean nucleotide = av.getAlignment().isNucleotide();
96 seqs = av.getAlignment().getSequencesArray();
100 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
102 SeqCigar sq[] = seqstrings.getSequences();
103 int length = sq[0].getWidth();
105 for (int i = 0; i < seqs.length; i++)
107 if (sq[i].getWidth() != length)
119 MessageManager.getString("label.pca_sequences_not_aligned"),
120 MessageManager.getString("label.sequences_not_aligned"), 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
144 .getStringOrReturn("label.score_model", sm));
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);
171 public void bgcolour_actionPerformed(ActionEvent e)
173 Color col = JColorChooser.showDialog(this, MessageManager.getString("label.select_backgroud_colour"),
188 long progId = System.currentTimeMillis();
189 IProgressIndicator progress = this;
190 String message = MessageManager.getString("label.pca_recalculating");
191 if (getParent() == null)
193 progress = ap.alignFrame;
194 message = MessageManager.getString("label.pca_calculating");
196 progress.setProgressBar(message, progId);
199 calcSettings.setEnabled(false);
202 xCombobox.setSelectedIndex(0);
203 yCombobox.setSelectedIndex(1);
204 zCombobox.setSelectedIndex(2);
206 pcaModel.updateRc(rc);
208 nuclSetting.setSelected(pcaModel.isNucleotide());
209 protSetting.setSelected(!pcaModel.isNucleotide());
210 jvVersionSetting.setSelected(pcaModel.isJvCalcMode());
211 top = pcaModel.getTop();
213 } catch (OutOfMemoryError er)
215 new OOMWarning("calculating PCA", er);
219 progress.setProgressBar("", progId);
221 calcSettings.setEnabled(true);
223 if (getParent() == null)
226 Desktop.addInternalFrame(this, MessageManager
227 .getString("label.principal_component_analysis"), 475, 450);
232 protected void nuclSetting_actionPerfomed(ActionEvent arg0)
234 if (!pcaModel.isNucleotide())
236 pcaModel.setNucleotide(true);
237 pcaModel.setScore_matrix("DNA");
238 Thread worker = new Thread(this);
245 protected void protSetting_actionPerfomed(ActionEvent arg0)
248 if (pcaModel.isNucleotide())
250 pcaModel.setNucleotide(false);
251 pcaModel.setScore_matrix("BLOSUM62");
252 Thread worker = new Thread(this);
258 protected void jvVersionSetting_actionPerfomed(ActionEvent arg0)
260 pcaModel.setJvCalcMode(jvVersionSetting.isSelected());
261 Thread worker = new Thread(this);
268 void doDimensionChange()
275 int dim1 = top - xCombobox.getSelectedIndex();
276 int dim2 = top - yCombobox.getSelectedIndex();
277 int dim3 = top - zCombobox.getSelectedIndex();
278 pcaModel.updateRcView(dim1, dim2, dim3);
280 rc.rotmat.setIdentity();
282 rc.paint(rc.getGraphics());
291 protected void xCombobox_actionPerformed(ActionEvent e)
302 protected void yCombobox_actionPerformed(ActionEvent e)
313 protected void zCombobox_actionPerformed(ActionEvent e)
318 public void outputValues_actionPerformed(ActionEvent e)
320 CutAndPasteTransfer cap = new CutAndPasteTransfer();
323 cap.setText(pcaModel.getDetails());
324 Desktop.addInternalFrame(cap,
325 MessageManager.getString("label.pca_details"), 500, 500);
326 } catch (OutOfMemoryError oom)
328 new OOMWarning("opening PCA details", oom);
333 public void showLabels_actionPerformed(ActionEvent e)
335 rc.showLabels(showLabels.getState());
338 public void print_actionPerformed(ActionEvent e)
340 PCAPrinter printer = new PCAPrinter();
344 public void originalSeqData_actionPerformed(ActionEvent e)
346 // this was cut'n'pasted from the equivalent TreePanel method - we should
347 // make this an abstract function of all jalview analysis windows
348 if (pcaModel.getSeqtrings() == null)
350 jalview.bin.Cache.log
351 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
354 // decide if av alignment is sufficiently different to original data to
355 // warrant a new window to be created
356 // create new alignmnt window with hidden regions (unhiding hidden regions
357 // yields unaligned seqs)
358 // or create a selection box around columns in alignment view
359 // test Alignment(SeqCigar[])
363 // we try to get the associated view's gap character
364 // but this may fail if the view was closed...
365 gc = av.getGapCharacter();
366 } catch (Exception ex)
370 Object[] alAndColsel = pcaModel.getSeqtrings()
371 .getAlignmentAndColumnSelection(gc);
373 if (alAndColsel != null && alAndColsel[0] != null)
375 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
377 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
378 Alignment dataset = (av != null && av.getAlignment() != null) ? av
379 .getAlignment().getDataset() : null;
382 al.setDataset(dataset);
388 AlignFrame af = new AlignFrame(al,
389 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
390 AlignFrame.DEFAULT_HEIGHT);
392 // >>>This is a fix for the moment, until a better solution is
394 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
396 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
399 Desktop.addInternalFrame(af, MessageManager.formatMessage(
400 "label.original_data_for_params", new String[]
401 { this.title }), AlignFrame.DEFAULT_WIDTH,
402 AlignFrame.DEFAULT_HEIGHT);
406 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
407 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
408 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
411 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
415 class PCAPrinter extends Thread implements Printable
419 PrinterJob printJob = PrinterJob.getPrinterJob();
420 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
422 printJob.setPrintable(this, pf);
424 if (printJob.printDialog())
429 } catch (Exception PrintException)
431 PrintException.printStackTrace();
436 public int print(Graphics pg, PageFormat pf, int pi)
437 throws PrinterException
439 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
441 rc.drawBackground(pg, rc.bgColour);
443 if (rc.drawAxes == true)
450 return Printable.PAGE_EXISTS;
454 return Printable.NO_SUCH_PAGE;
465 public void eps_actionPerformed(ActionEvent e)
467 makePCAImage(jalview.util.ImageMaker.TYPE.EPS);
476 public void png_actionPerformed(ActionEvent e)
478 makePCAImage(jalview.util.ImageMaker.TYPE.PNG);
481 void makePCAImage(jalview.util.ImageMaker.TYPE type)
483 int width = rc.getWidth();
484 int height = rc.getHeight();
486 jalview.util.ImageMaker im;
488 if (type == jalview.util.ImageMaker.TYPE.PNG)
490 im = new jalview.util.ImageMaker(this,
491 jalview.util.ImageMaker.TYPE.PNG,
492 "Make PNG image from PCA", width, height, null, null);
494 else if (type == jalview.util.ImageMaker.TYPE.EPS)
496 im = new jalview.util.ImageMaker(this,
497 jalview.util.ImageMaker.TYPE.EPS,
498 "Make EPS file from PCA", width, height, null,
503 im = new jalview.util.ImageMaker(this,
504 jalview.util.ImageMaker.TYPE.SVG, "Make SVG file from PCA",
505 width, height, null, this.getTitle());
509 if (im.getGraphics() != null)
511 rc.drawBackground(im.getGraphics(), Color.black);
512 rc.drawScene(im.getGraphics());
513 if (rc.drawAxes == true)
515 rc.drawAxes(im.getGraphics());
521 public void viewMenu_menuSelected()
523 buildAssociatedViewMenu();
526 void buildAssociatedViewMenu()
528 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
529 .getSequenceSetId());
530 if (aps.length == 1 && rc.av == aps[0].av)
532 associateViewsMenu.setVisible(false);
536 associateViewsMenu.setVisible(true);
538 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
540 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
543 associateViewsMenu.removeAll();
545 JRadioButtonMenuItem item;
546 ButtonGroup buttonGroup = new ButtonGroup();
547 int i, iSize = aps.length;
548 final PCAPanel thisPCAPanel = this;
549 for (i = 0; i < iSize; i++)
551 final AlignmentPanel ap = aps[i];
552 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
553 buttonGroup.add(item);
554 item.addActionListener(new ActionListener()
556 public void actionPerformed(ActionEvent evt)
558 rc.applyToAllViews = false;
561 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
565 associateViewsMenu.add(item);
568 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
570 buttonGroup.add(itemf);
572 itemf.setSelected(rc.applyToAllViews);
573 itemf.addActionListener(new ActionListener()
575 public void actionPerformed(ActionEvent evt)
577 rc.applyToAllViews = itemf.isSelected();
580 associateViewsMenu.add(itemf);
588 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
591 protected void outputPoints_actionPerformed(ActionEvent e)
593 CutAndPasteTransfer cap = new CutAndPasteTransfer();
596 cap.setText(pcaModel.getPointsasCsv(false,
597 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
598 zCombobox.getSelectedIndex()));
599 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
600 "label.points_for_params", new String[]
601 { this.getTitle() }), 500, 500);
602 } catch (OutOfMemoryError oom)
604 new OOMWarning("exporting PCA points", oom);
613 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
616 protected void outputProjPoints_actionPerformed(ActionEvent e)
618 CutAndPasteTransfer cap = new CutAndPasteTransfer();
621 cap.setText(pcaModel.getPointsasCsv(true,
622 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
623 zCombobox.getSelectedIndex()));
624 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
625 "label.transformed_points_for_params", new String[]
626 { this.getTitle() }), 500, 500);
627 } catch (OutOfMemoryError oom)
629 new OOMWarning("exporting transformed PCA points", oom);
636 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
639 public void setProgressBar(String message, long id)
641 progressBar.setProgressBar(message, id);
642 // if (progressBars == null)
644 // progressBars = new Hashtable();
645 // progressBarHandlers = new Hashtable();
648 // JPanel progressPanel;
649 // Long lId = new Long(id);
650 // GridLayout layout = (GridLayout) statusPanel.getLayout();
651 // if (progressBars.get(lId) != null)
653 // progressPanel = (JPanel) progressBars.get(new Long(id));
654 // statusPanel.remove(progressPanel);
655 // progressBars.remove(lId);
656 // progressPanel = null;
657 // if (message != null)
659 // statusBar.setText(message);
661 // if (progressBarHandlers.contains(lId))
663 // progressBarHandlers.remove(lId);
665 // layout.setRows(layout.getRows() - 1);
669 // progressPanel = new JPanel(new BorderLayout(10, 5));
671 // JProgressBar progressBar = new JProgressBar();
672 // progressBar.setIndeterminate(true);
674 // progressPanel.add(new JLabel(message), BorderLayout.WEST);
675 // progressPanel.add(progressBar, BorderLayout.CENTER);
677 // layout.setRows(layout.getRows() + 1);
678 // statusPanel.add(progressPanel);
680 // progressBars.put(lId, progressPanel);
683 // // setMenusForViewport();
688 public void registerHandler(final long id,
689 final IProgressIndicatorHandler handler)
691 progressBar.registerHandler(id, handler);
692 // if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
695 // Error(MessageManager.getString("error.call_setprogressbar_before_registering_handler"));
697 // progressBarHandlers.put(new Long(id), handler);
698 // final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
699 // if (handler.canCancel())
701 // JButton cancel = new JButton(
702 // MessageManager.getString("action.cancel"));
703 // final IProgressIndicator us = this;
704 // cancel.addActionListener(new ActionListener()
708 // public void actionPerformed(ActionEvent e)
710 // handler.cancelActivity(id);
711 // us.setProgressBar(MessageManager.formatMessage("label.cancelled_params",
712 // new String[]{((JLabel) progressPanel.getComponent(0)).getText()}), id);
715 // progressPanel.add(cancel, BorderLayout.EAST);
721 * @return true if any progress bars are still active
724 public boolean operationInProgress()
726 return progressBar.operationInProgress();
730 protected void resetButton_actionPerformed(ActionEvent e)
733 top = 0; // ugly - prevents dimensionChanged events from being processed
734 xCombobox.setSelectedIndex(0);
735 yCombobox.setSelectedIndex(1);
737 zCombobox.setSelectedIndex(2);