2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.
25 import java.awt.event.*;
26 import java.awt.print.*;
30 import jalview.datamodel.*;
31 import jalview.jbgui.*;
32 import jalview.schemes.ResidueProperties;
33 import jalview.util.MessageManager;
34 import jalview.viewmodel.PCAModel;
42 public class PCAPanel extends GPCAPanel implements Runnable,
57 * Creates a new PCAPanel object.
64 public PCAPanel(AlignmentPanel ap)
69 boolean sameLength = true;
70 boolean selected = av.getSelectionGroup() != null
71 && av.getSelectionGroup().getSize() > 0;
72 AlignmentView seqstrings = av.getAlignmentView(selected);
73 boolean nucleotide = av.getAlignment().isNucleotide();
77 seqs = av.getAlignment().getSequencesArray();
81 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
83 SeqCigar sq[] = seqstrings.getSequences();
84 int length = sq[0].getWidth();
86 for (int i = 0; i < seqs.length; i++)
88 if (sq[i].getWidth() != length)
100 "The sequences must be aligned before calculating PCA.\n"
101 + "Try using the Pad function in the edit menu,\n"
102 + "or one of the multiple sequence alignment web services.",
103 "Sequences not aligned", JOptionPane.WARNING_MESSAGE);
107 pcaModel = new PCAModel(seqstrings, seqs, nucleotide);
108 PaintRefresher.Register(this, av.getSequenceSetId());
110 rc = new RotatableCanvas(ap);
111 this.getContentPane().add(rc, BorderLayout.CENTER);
112 Thread worker = new Thread(this);
116 protected void scoreMatrix_menuSelected()
118 scoreMatrixMenu.removeAll();
119 for (final String sm:ResidueProperties.scoreMatrices.keySet())
121 if (ResidueProperties.getScoreMatrix(sm) != null)
123 // create an entry for this score matrix for use in PCA
124 JCheckBoxMenuItem jm = new JCheckBoxMenuItem();
125 jm.setText(MessageManager
126 .getStringOrReturn("label.score_model", sm));
127 jm.setSelected(pcaModel.getScore_matrix().equals(sm));
128 if ((ResidueProperties.scoreMatrices.get(sm).isDNA() && ResidueProperties.scoreMatrices
129 .get(sm).isProtein())
130 || pcaModel.isNucleotide() == ResidueProperties.scoreMatrices
133 final PCAPanel us = this;
134 jm.addActionListener(new ActionListener()
137 public void actionPerformed(ActionEvent e)
139 if (!pcaModel.getScore_matrix().equals((String) sm))
141 pcaModel.setScore_matrix((String) sm);
142 Thread worker = new Thread(us);
147 scoreMatrixMenu.add(jm);
152 public void bgcolour_actionPerformed(ActionEvent e)
154 Color col = JColorChooser.showDialog(this, "Select Background Colour",
169 long progId = System.currentTimeMillis();
170 IProgressIndicator progress = this;
171 String message = "Recalculating PCA";
172 if (getParent() == null)
174 progress = ap.alignFrame;
175 message = "Calculating PCA";
177 progress.setProgressBar(message, progId);
180 calcSettings.setEnabled(false);
183 xCombobox.setSelectedIndex(0);
184 yCombobox.setSelectedIndex(1);
185 zCombobox.setSelectedIndex(2);
187 pcaModel.updateRc(rc);
189 nuclSetting.setSelected(pcaModel.isNucleotide());
190 protSetting.setSelected(!pcaModel.isNucleotide());
191 jvVersionSetting.setSelected(pcaModel.isJvCalcMode());
192 top = pcaModel.getTop();
194 } catch (OutOfMemoryError er)
196 new OOMWarning("calculating PCA", er);
200 progress.setProgressBar("", progId);
202 calcSettings.setEnabled(true);
204 if (getParent() == null)
207 Desktop.addInternalFrame(this, MessageManager.getString("label.principal_component_analysis"), 475,
213 protected void nuclSetting_actionPerfomed(ActionEvent arg0)
215 if (!pcaModel.isNucleotide())
217 pcaModel.setNucleotide(true);
218 pcaModel.setScore_matrix("DNA");
219 Thread worker = new Thread(this);
226 protected void protSetting_actionPerfomed(ActionEvent arg0)
229 if (pcaModel.isNucleotide())
231 pcaModel.setNucleotide(false);
232 pcaModel.setScore_matrix("BLOSUM62");
233 Thread worker = new Thread(this);
239 protected void jvVersionSetting_actionPerfomed(ActionEvent arg0)
241 pcaModel.setJvCalcMode(jvVersionSetting.isSelected());
242 Thread worker = new Thread(this);
249 void doDimensionChange()
256 int dim1 = top - xCombobox.getSelectedIndex();
257 int dim2 = top - yCombobox.getSelectedIndex();
258 int dim3 = top - zCombobox.getSelectedIndex();
259 pcaModel.updateRcView(dim1, dim2, dim3);
261 rc.rotmat.setIdentity();
263 rc.paint(rc.getGraphics());
272 protected void xCombobox_actionPerformed(ActionEvent e)
283 protected void yCombobox_actionPerformed(ActionEvent e)
294 protected void zCombobox_actionPerformed(ActionEvent e)
299 public void outputValues_actionPerformed(ActionEvent e)
301 CutAndPasteTransfer cap = new CutAndPasteTransfer();
304 cap.setText(pcaModel.getDetails());
305 Desktop.addInternalFrame(cap, MessageManager.getString("label.pca_details"), 500, 500);
306 } catch (OutOfMemoryError oom)
308 new OOMWarning("opening PCA details", oom);
313 public void showLabels_actionPerformed(ActionEvent e)
315 rc.showLabels(showLabels.getState());
318 public void print_actionPerformed(ActionEvent e)
320 PCAPrinter printer = new PCAPrinter();
324 public void originalSeqData_actionPerformed(ActionEvent e)
326 // this was cut'n'pasted from the equivalent TreePanel method - we should
327 // make this an abstract function of all jalview analysis windows
328 if (pcaModel.getSeqtrings() == null)
330 jalview.bin.Cache.log
331 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
334 // decide if av alignment is sufficiently different to original data to
335 // warrant a new window to be created
336 // create new alignmnt window with hidden regions (unhiding hidden regions
337 // yields unaligned seqs)
338 // or create a selection box around columns in alignment view
339 // test Alignment(SeqCigar[])
343 // we try to get the associated view's gap character
344 // but this may fail if the view was closed...
345 gc = av.getGapCharacter();
346 } catch (Exception ex)
350 Object[] alAndColsel = pcaModel.getSeqtrings()
351 .getAlignmentAndColumnSelection(gc);
353 if (alAndColsel != null && alAndColsel[0] != null)
355 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
357 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
358 Alignment dataset = (av != null && av.getAlignment() != null) ? av
359 .getAlignment().getDataset() : null;
362 al.setDataset(dataset);
368 AlignFrame af = new AlignFrame(al,
369 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
370 AlignFrame.DEFAULT_HEIGHT);
372 // >>>This is a fix for the moment, until a better solution is
374 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
376 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
379 Desktop.addInternalFrame(af, MessageManager.formatMessage("label.original_data_for_params", new String[]{this.title}),
380 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
384 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
385 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
386 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
389 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
393 class PCAPrinter extends Thread implements Printable
397 PrinterJob printJob = PrinterJob.getPrinterJob();
398 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
400 printJob.setPrintable(this, pf);
402 if (printJob.printDialog())
407 } catch (Exception PrintException)
409 PrintException.printStackTrace();
414 public int print(Graphics pg, PageFormat pf, int pi)
415 throws PrinterException
417 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
419 rc.drawBackground(pg, rc.bgColour);
421 if (rc.drawAxes == true)
428 return Printable.PAGE_EXISTS;
432 return Printable.NO_SUCH_PAGE;
443 public void eps_actionPerformed(ActionEvent e)
445 makePCAImage(jalview.util.ImageMaker.EPS);
454 public void png_actionPerformed(ActionEvent e)
456 makePCAImage(jalview.util.ImageMaker.PNG);
459 void makePCAImage(int type)
461 int width = rc.getWidth();
462 int height = rc.getHeight();
464 jalview.util.ImageMaker im;
466 if (type == jalview.util.ImageMaker.PNG)
468 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
469 "Make PNG image from PCA", width, height, null, null);
473 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
474 "Make EPS file from PCA", width, height, null,
478 if (im.getGraphics() != null)
480 rc.drawBackground(im.getGraphics(), Color.black);
481 rc.drawScene(im.getGraphics());
482 if (rc.drawAxes == true)
484 rc.drawAxes(im.getGraphics());
491 public void viewMenu_menuSelected()
493 buildAssociatedViewMenu();
496 void buildAssociatedViewMenu()
498 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
499 .getSequenceSetId());
500 if (aps.length == 1 && rc.av == aps[0].av)
502 associateViewsMenu.setVisible(false);
506 associateViewsMenu.setVisible(true);
508 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
510 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
513 associateViewsMenu.removeAll();
515 JRadioButtonMenuItem item;
516 ButtonGroup buttonGroup = new ButtonGroup();
517 int i, iSize = aps.length;
518 final PCAPanel thisPCAPanel = this;
519 for (i = 0; i < iSize; i++)
521 final AlignmentPanel ap = aps[i];
522 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
523 buttonGroup.add(item);
524 item.addActionListener(new ActionListener()
526 public void actionPerformed(ActionEvent evt)
528 rc.applyToAllViews = false;
531 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
535 associateViewsMenu.add(item);
538 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
540 buttonGroup.add(itemf);
542 itemf.setSelected(rc.applyToAllViews);
543 itemf.addActionListener(new ActionListener()
545 public void actionPerformed(ActionEvent evt)
547 rc.applyToAllViews = itemf.isSelected();
550 associateViewsMenu.add(itemf);
558 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
561 protected void outputPoints_actionPerformed(ActionEvent e)
563 CutAndPasteTransfer cap = new CutAndPasteTransfer();
566 cap.setText(pcaModel.getPointsasCsv(false,
567 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
568 zCombobox.getSelectedIndex()));
569 Desktop.addInternalFrame(cap, MessageManager.formatMessage("label.points_for_params", new String[]{this.getTitle()}), 500, 500);
570 } catch (OutOfMemoryError oom)
572 new OOMWarning("exporting PCA points", oom);
581 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
584 protected void outputProjPoints_actionPerformed(ActionEvent e)
586 CutAndPasteTransfer cap = new CutAndPasteTransfer();
589 cap.setText(pcaModel.getPointsasCsv(true,
590 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
591 zCombobox.getSelectedIndex()));
592 Desktop.addInternalFrame(cap, MessageManager.formatMessage("label.transformed_points_for_params", new String[]{this.getTitle()}),
594 } catch (OutOfMemoryError oom)
596 new OOMWarning("exporting transformed PCA points", oom);
601 // methods for implementing IProgressIndicator
602 // need to refactor to a reusable stub class
603 Hashtable progressBars, progressBarHandlers;
608 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
611 public void setProgressBar(String message, long id)
613 if (progressBars == null)
615 progressBars = new Hashtable();
616 progressBarHandlers = new Hashtable();
619 JPanel progressPanel;
620 Long lId = new Long(id);
621 GridLayout layout = (GridLayout) statusPanel.getLayout();
622 if (progressBars.get(lId) != null)
624 progressPanel = (JPanel) progressBars.get(new Long(id));
625 statusPanel.remove(progressPanel);
626 progressBars.remove(lId);
627 progressPanel = null;
630 statusBar.setText(message);
632 if (progressBarHandlers.contains(lId))
634 progressBarHandlers.remove(lId);
636 layout.setRows(layout.getRows() - 1);
640 progressPanel = new JPanel(new BorderLayout(10, 5));
642 JProgressBar progressBar = new JProgressBar();
643 progressBar.setIndeterminate(true);
645 progressPanel.add(new JLabel(message), BorderLayout.WEST);
646 progressPanel.add(progressBar, BorderLayout.CENTER);
648 layout.setRows(layout.getRows() + 1);
649 statusPanel.add(progressPanel);
651 progressBars.put(lId, progressPanel);
654 // setMenusForViewport();
659 public void registerHandler(final long id,
660 final IProgressIndicatorHandler handler)
662 if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
665 "call setProgressBar before registering the progress bar's handler.");
667 progressBarHandlers.put(new Long(id), handler);
668 final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
669 if (handler.canCancel())
671 JButton cancel = new JButton(MessageManager.getString("action.cancel"));
672 final IProgressIndicator us = this;
673 cancel.addActionListener(new ActionListener()
677 public void actionPerformed(ActionEvent e)
679 handler.cancelActivity(id);
682 + ((JLabel) progressPanel.getComponent(0))
686 progressPanel.add(cancel, BorderLayout.EAST);
692 * @return true if any progress bars are still active
695 public boolean operationInProgress()
697 if (progressBars != null && progressBars.size() > 0)
705 protected void resetButton_actionPerformed(ActionEvent e)
708 top = 0; // ugly - prevents dimensionChanged events from being processed
709 xCombobox.setSelectedIndex(0);
710 yCombobox.setSelectedIndex(1);
712 zCombobox.setSelectedIndex(2);