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);
117 protected void scoreMatrix_menuSelected()
119 scoreMatrixMenu.removeAll();
120 for (final String sm : ResidueProperties.scoreMatrices.keySet())
122 if (ResidueProperties.getScoreMatrix(sm) != null)
124 // create an entry for this score matrix for use in PCA
125 JCheckBoxMenuItem jm = new JCheckBoxMenuItem();
126 jm.setText(MessageManager
127 .getStringOrReturn("label.score_model", sm));
128 jm.setSelected(pcaModel.getScore_matrix().equals(sm));
129 if ((ResidueProperties.scoreMatrices.get(sm).isDNA() && ResidueProperties.scoreMatrices
130 .get(sm).isProtein())
131 || pcaModel.isNucleotide() == ResidueProperties.scoreMatrices
134 final PCAPanel us = this;
135 jm.addActionListener(new ActionListener()
138 public void actionPerformed(ActionEvent e)
140 if (!pcaModel.getScore_matrix().equals((String) sm))
142 pcaModel.setScore_matrix((String) sm);
143 Thread worker = new Thread(us);
148 scoreMatrixMenu.add(jm);
154 public void bgcolour_actionPerformed(ActionEvent e)
156 Color col = JColorChooser.showDialog(this, "Select Background Colour",
171 long progId = System.currentTimeMillis();
172 IProgressIndicator progress = this;
173 String message = "Recalculating PCA";
174 if (getParent() == null)
176 progress = ap.alignFrame;
177 message = "Calculating PCA";
179 progress.setProgressBar(message, progId);
182 calcSettings.setEnabled(false);
185 xCombobox.setSelectedIndex(0);
186 yCombobox.setSelectedIndex(1);
187 zCombobox.setSelectedIndex(2);
189 pcaModel.updateRc(rc);
191 nuclSetting.setSelected(pcaModel.isNucleotide());
192 protSetting.setSelected(!pcaModel.isNucleotide());
193 jvVersionSetting.setSelected(pcaModel.isJvCalcMode());
194 top = pcaModel.getTop();
196 } catch (OutOfMemoryError er)
198 new OOMWarning("calculating PCA", er);
202 progress.setProgressBar("", progId);
204 calcSettings.setEnabled(true);
206 if (getParent() == null)
209 Desktop.addInternalFrame(this, MessageManager
210 .getString("label.principal_component_analysis"), 475, 450);
215 protected void nuclSetting_actionPerfomed(ActionEvent arg0)
217 if (!pcaModel.isNucleotide())
219 pcaModel.setNucleotide(true);
220 pcaModel.setScore_matrix("DNA");
221 Thread worker = new Thread(this);
228 protected void protSetting_actionPerfomed(ActionEvent arg0)
231 if (pcaModel.isNucleotide())
233 pcaModel.setNucleotide(false);
234 pcaModel.setScore_matrix("BLOSUM62");
235 Thread worker = new Thread(this);
241 protected void jvVersionSetting_actionPerfomed(ActionEvent arg0)
243 pcaModel.setJvCalcMode(jvVersionSetting.isSelected());
244 Thread worker = new Thread(this);
251 void doDimensionChange()
258 int dim1 = top - xCombobox.getSelectedIndex();
259 int dim2 = top - yCombobox.getSelectedIndex();
260 int dim3 = top - zCombobox.getSelectedIndex();
261 pcaModel.updateRcView(dim1, dim2, dim3);
263 rc.rotmat.setIdentity();
265 rc.paint(rc.getGraphics());
274 protected void xCombobox_actionPerformed(ActionEvent e)
285 protected void yCombobox_actionPerformed(ActionEvent e)
296 protected void zCombobox_actionPerformed(ActionEvent e)
301 public void outputValues_actionPerformed(ActionEvent e)
303 CutAndPasteTransfer cap = new CutAndPasteTransfer();
306 cap.setText(pcaModel.getDetails());
307 Desktop.addInternalFrame(cap,
308 MessageManager.getString("label.pca_details"), 500, 500);
309 } catch (OutOfMemoryError oom)
311 new OOMWarning("opening PCA details", oom);
316 public void showLabels_actionPerformed(ActionEvent e)
318 rc.showLabels(showLabels.getState());
321 public void print_actionPerformed(ActionEvent e)
323 PCAPrinter printer = new PCAPrinter();
327 public void originalSeqData_actionPerformed(ActionEvent e)
329 // this was cut'n'pasted from the equivalent TreePanel method - we should
330 // make this an abstract function of all jalview analysis windows
331 if (pcaModel.getSeqtrings() == null)
333 jalview.bin.Cache.log
334 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
337 // decide if av alignment is sufficiently different to original data to
338 // warrant a new window to be created
339 // create new alignmnt window with hidden regions (unhiding hidden regions
340 // yields unaligned seqs)
341 // or create a selection box around columns in alignment view
342 // test Alignment(SeqCigar[])
346 // we try to get the associated view's gap character
347 // but this may fail if the view was closed...
348 gc = av.getGapCharacter();
349 } catch (Exception ex)
353 Object[] alAndColsel = pcaModel.getSeqtrings()
354 .getAlignmentAndColumnSelection(gc);
356 if (alAndColsel != null && alAndColsel[0] != null)
358 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
360 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
361 Alignment dataset = (av != null && av.getAlignment() != null) ? av
362 .getAlignment().getDataset() : null;
365 al.setDataset(dataset);
371 AlignFrame af = new AlignFrame(al,
372 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
373 AlignFrame.DEFAULT_HEIGHT);
375 // >>>This is a fix for the moment, until a better solution is
377 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
379 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
382 Desktop.addInternalFrame(af, MessageManager.formatMessage(
383 "label.original_data_for_params", new String[]
384 { this.title }), AlignFrame.DEFAULT_WIDTH,
385 AlignFrame.DEFAULT_HEIGHT);
389 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
390 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
391 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
394 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
398 class PCAPrinter extends Thread implements Printable
402 PrinterJob printJob = PrinterJob.getPrinterJob();
403 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
405 printJob.setPrintable(this, pf);
407 if (printJob.printDialog())
412 } catch (Exception PrintException)
414 PrintException.printStackTrace();
419 public int print(Graphics pg, PageFormat pf, int pi)
420 throws PrinterException
422 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
424 rc.drawBackground(pg, rc.bgColour);
426 if (rc.drawAxes == true)
433 return Printable.PAGE_EXISTS;
437 return Printable.NO_SUCH_PAGE;
448 public void eps_actionPerformed(ActionEvent e)
450 makePCAImage(jalview.util.ImageMaker.EPS);
459 public void png_actionPerformed(ActionEvent e)
461 makePCAImage(jalview.util.ImageMaker.PNG);
464 void makePCAImage(int type)
466 int width = rc.getWidth();
467 int height = rc.getHeight();
469 jalview.util.ImageMaker im;
471 if (type == jalview.util.ImageMaker.PNG)
473 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
474 "Make PNG image from PCA", width, height, null, null);
478 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
479 "Make EPS file from PCA", width, height, null,
483 if (im.getGraphics() != null)
485 rc.drawBackground(im.getGraphics(), Color.black);
486 rc.drawScene(im.getGraphics());
487 if (rc.drawAxes == true)
489 rc.drawAxes(im.getGraphics());
495 public void viewMenu_menuSelected()
497 buildAssociatedViewMenu();
500 void buildAssociatedViewMenu()
502 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
503 .getSequenceSetId());
504 if (aps.length == 1 && rc.av == aps[0].av)
506 associateViewsMenu.setVisible(false);
510 associateViewsMenu.setVisible(true);
512 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
514 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
517 associateViewsMenu.removeAll();
519 JRadioButtonMenuItem item;
520 ButtonGroup buttonGroup = new ButtonGroup();
521 int i, iSize = aps.length;
522 final PCAPanel thisPCAPanel = this;
523 for (i = 0; i < iSize; i++)
525 final AlignmentPanel ap = aps[i];
526 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
527 buttonGroup.add(item);
528 item.addActionListener(new ActionListener()
530 public void actionPerformed(ActionEvent evt)
532 rc.applyToAllViews = false;
535 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
539 associateViewsMenu.add(item);
542 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
544 buttonGroup.add(itemf);
546 itemf.setSelected(rc.applyToAllViews);
547 itemf.addActionListener(new ActionListener()
549 public void actionPerformed(ActionEvent evt)
551 rc.applyToAllViews = itemf.isSelected();
554 associateViewsMenu.add(itemf);
562 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
565 protected void outputPoints_actionPerformed(ActionEvent e)
567 CutAndPasteTransfer cap = new CutAndPasteTransfer();
570 cap.setText(pcaModel.getPointsasCsv(false,
571 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
572 zCombobox.getSelectedIndex()));
573 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
574 "label.points_for_params", new String[]
575 { this.getTitle() }), 500, 500);
576 } catch (OutOfMemoryError oom)
578 new OOMWarning("exporting PCA points", oom);
587 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
590 protected void outputProjPoints_actionPerformed(ActionEvent e)
592 CutAndPasteTransfer cap = new CutAndPasteTransfer();
595 cap.setText(pcaModel.getPointsasCsv(true,
596 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
597 zCombobox.getSelectedIndex()));
598 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
599 "label.transformed_points_for_params", new String[]
600 { this.getTitle() }), 500, 500);
601 } catch (OutOfMemoryError oom)
603 new OOMWarning("exporting transformed PCA points", oom);
608 // methods for implementing IProgressIndicator
609 // need to refactor to a reusable stub class
610 Hashtable progressBars, progressBarHandlers;
615 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
618 public void setProgressBar(String message, long id)
620 if (progressBars == null)
622 progressBars = new Hashtable();
623 progressBarHandlers = new Hashtable();
626 JPanel progressPanel;
627 Long lId = new Long(id);
628 GridLayout layout = (GridLayout) statusPanel.getLayout();
629 if (progressBars.get(lId) != null)
631 progressPanel = (JPanel) progressBars.get(new Long(id));
632 statusPanel.remove(progressPanel);
633 progressBars.remove(lId);
634 progressPanel = null;
637 statusBar.setText(message);
639 if (progressBarHandlers.contains(lId))
641 progressBarHandlers.remove(lId);
643 layout.setRows(layout.getRows() - 1);
647 progressPanel = new JPanel(new BorderLayout(10, 5));
649 JProgressBar progressBar = new JProgressBar();
650 progressBar.setIndeterminate(true);
652 progressPanel.add(new JLabel(message), BorderLayout.WEST);
653 progressPanel.add(progressBar, BorderLayout.CENTER);
655 layout.setRows(layout.getRows() + 1);
656 statusPanel.add(progressPanel);
658 progressBars.put(lId, progressPanel);
661 // setMenusForViewport();
666 public void registerHandler(final long id,
667 final IProgressIndicatorHandler handler)
669 if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
672 "call setProgressBar before registering the progress bar's handler.");
674 progressBarHandlers.put(new Long(id), handler);
675 final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
676 if (handler.canCancel())
678 JButton cancel = new JButton(
679 MessageManager.getString("action.cancel"));
680 final IProgressIndicator us = this;
681 cancel.addActionListener(new ActionListener()
685 public void actionPerformed(ActionEvent e)
687 handler.cancelActivity(id);
690 + ((JLabel) progressPanel.getComponent(0))
694 progressPanel.add(cancel, BorderLayout.EAST);
700 * @return true if any progress bars are still active
703 public boolean operationInProgress()
705 if (progressBars != null && progressBars.size() > 0)
713 protected void resetButton_actionPerformed(ActionEvent e)
716 top = 0; // ugly - prevents dimensionChanged events from being processed
717 xCombobox.setSelectedIndex(0);
718 yCombobox.setSelectedIndex(1);
720 zCombobox.setSelectedIndex(2);