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 MessageManager.getString("label.pca_sequences_not_aligned"),
101 MessageManager.getString("label.sequences_not_aligned"), JOptionPane.WARNING_MESSAGE);
105 pcaModel = new PCAModel(seqstrings, seqs, nucleotide);
106 PaintRefresher.Register(this, av.getSequenceSetId());
108 rc = new RotatableCanvas(ap);
109 this.getContentPane().add(rc, BorderLayout.CENTER);
110 Thread worker = new Thread(this);
115 protected void scoreMatrix_menuSelected()
117 scoreMatrixMenu.removeAll();
118 for (final String sm : ResidueProperties.scoreMatrices.keySet())
120 if (ResidueProperties.getScoreMatrix(sm) != null)
122 // create an entry for this score matrix for use in PCA
123 JCheckBoxMenuItem jm = new JCheckBoxMenuItem();
124 jm.setText(MessageManager
125 .getStringOrReturn("label.score_model", sm));
126 jm.setSelected(pcaModel.getScore_matrix().equals(sm));
127 if ((ResidueProperties.scoreMatrices.get(sm).isDNA() && ResidueProperties.scoreMatrices
128 .get(sm).isProtein())
129 || pcaModel.isNucleotide() == ResidueProperties.scoreMatrices
132 final PCAPanel us = this;
133 jm.addActionListener(new ActionListener()
136 public void actionPerformed(ActionEvent e)
138 if (!pcaModel.getScore_matrix().equals((String) sm))
140 pcaModel.setScore_matrix((String) sm);
141 Thread worker = new Thread(us);
146 scoreMatrixMenu.add(jm);
152 public void bgcolour_actionPerformed(ActionEvent e)
154 Color col = JColorChooser.showDialog(this, MessageManager.getString("label.select_backgroud_colour"),
169 long progId = System.currentTimeMillis();
170 IProgressIndicator progress = this;
171 String message = MessageManager.getString("label.pca_recalculating");
172 if (getParent() == null)
174 progress = ap.alignFrame;
175 message = MessageManager.getString("label.pca_calculating");
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
208 .getString("label.principal_component_analysis"), 475, 450);
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,
306 MessageManager.getString("label.pca_details"), 500, 500);
307 } catch (OutOfMemoryError oom)
309 new OOMWarning("opening PCA details", oom);
314 public void showLabels_actionPerformed(ActionEvent e)
316 rc.showLabels(showLabels.getState());
319 public void print_actionPerformed(ActionEvent e)
321 PCAPrinter printer = new PCAPrinter();
325 public void originalSeqData_actionPerformed(ActionEvent e)
327 // this was cut'n'pasted from the equivalent TreePanel method - we should
328 // make this an abstract function of all jalview analysis windows
329 if (pcaModel.getSeqtrings() == null)
331 jalview.bin.Cache.log
332 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
335 // decide if av alignment is sufficiently different to original data to
336 // warrant a new window to be created
337 // create new alignmnt window with hidden regions (unhiding hidden regions
338 // yields unaligned seqs)
339 // or create a selection box around columns in alignment view
340 // test Alignment(SeqCigar[])
344 // we try to get the associated view's gap character
345 // but this may fail if the view was closed...
346 gc = av.getGapCharacter();
347 } catch (Exception ex)
351 Object[] alAndColsel = pcaModel.getSeqtrings()
352 .getAlignmentAndColumnSelection(gc);
354 if (alAndColsel != null && alAndColsel[0] != null)
356 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
358 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
359 Alignment dataset = (av != null && av.getAlignment() != null) ? av
360 .getAlignment().getDataset() : null;
363 al.setDataset(dataset);
369 AlignFrame af = new AlignFrame(al,
370 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
371 AlignFrame.DEFAULT_HEIGHT);
373 // >>>This is a fix for the moment, until a better solution is
375 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
377 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
380 Desktop.addInternalFrame(af, MessageManager.formatMessage(
381 "label.original_data_for_params", new String[]
382 { this.title }), AlignFrame.DEFAULT_WIDTH,
383 AlignFrame.DEFAULT_HEIGHT);
387 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
388 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
389 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
392 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
396 class PCAPrinter extends Thread implements Printable
400 PrinterJob printJob = PrinterJob.getPrinterJob();
401 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
403 printJob.setPrintable(this, pf);
405 if (printJob.printDialog())
410 } catch (Exception PrintException)
412 PrintException.printStackTrace();
417 public int print(Graphics pg, PageFormat pf, int pi)
418 throws PrinterException
420 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
422 rc.drawBackground(pg, rc.bgColour);
424 if (rc.drawAxes == true)
431 return Printable.PAGE_EXISTS;
435 return Printable.NO_SUCH_PAGE;
446 public void eps_actionPerformed(ActionEvent e)
448 makePCAImage(jalview.util.ImageMaker.EPS);
457 public void png_actionPerformed(ActionEvent e)
459 makePCAImage(jalview.util.ImageMaker.PNG);
462 void makePCAImage(int type)
464 int width = rc.getWidth();
465 int height = rc.getHeight();
467 jalview.util.ImageMaker im;
469 if (type == jalview.util.ImageMaker.PNG)
471 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
472 "Make PNG image from PCA", width, height, null, null);
476 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
477 "Make EPS file from PCA", width, height, null,
481 if (im.getGraphics() != null)
483 rc.drawBackground(im.getGraphics(), Color.black);
484 rc.drawScene(im.getGraphics());
485 if (rc.drawAxes == true)
487 rc.drawAxes(im.getGraphics());
493 public void viewMenu_menuSelected()
495 buildAssociatedViewMenu();
498 void buildAssociatedViewMenu()
500 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
501 .getSequenceSetId());
502 if (aps.length == 1 && rc.av == aps[0].av)
504 associateViewsMenu.setVisible(false);
508 associateViewsMenu.setVisible(true);
510 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
512 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
515 associateViewsMenu.removeAll();
517 JRadioButtonMenuItem item;
518 ButtonGroup buttonGroup = new ButtonGroup();
519 int i, iSize = aps.length;
520 final PCAPanel thisPCAPanel = this;
521 for (i = 0; i < iSize; i++)
523 final AlignmentPanel ap = aps[i];
524 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
525 buttonGroup.add(item);
526 item.addActionListener(new ActionListener()
528 public void actionPerformed(ActionEvent evt)
530 rc.applyToAllViews = false;
533 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
537 associateViewsMenu.add(item);
540 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
542 buttonGroup.add(itemf);
544 itemf.setSelected(rc.applyToAllViews);
545 itemf.addActionListener(new ActionListener()
547 public void actionPerformed(ActionEvent evt)
549 rc.applyToAllViews = itemf.isSelected();
552 associateViewsMenu.add(itemf);
560 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
563 protected void outputPoints_actionPerformed(ActionEvent e)
565 CutAndPasteTransfer cap = new CutAndPasteTransfer();
568 cap.setText(pcaModel.getPointsasCsv(false,
569 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
570 zCombobox.getSelectedIndex()));
571 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
572 "label.points_for_params", new String[]
573 { this.getTitle() }), 500, 500);
574 } catch (OutOfMemoryError oom)
576 new OOMWarning("exporting PCA points", oom);
585 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
588 protected void outputProjPoints_actionPerformed(ActionEvent e)
590 CutAndPasteTransfer cap = new CutAndPasteTransfer();
593 cap.setText(pcaModel.getPointsasCsv(true,
594 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
595 zCombobox.getSelectedIndex()));
596 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
597 "label.transformed_points_for_params", new String[]
598 { this.getTitle() }), 500, 500);
599 } catch (OutOfMemoryError oom)
601 new OOMWarning("exporting transformed PCA points", oom);
606 // methods for implementing IProgressIndicator
607 // need to refactor to a reusable stub class
608 Hashtable progressBars, progressBarHandlers;
613 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
616 public void setProgressBar(String message, long id)
618 if (progressBars == null)
620 progressBars = new Hashtable();
621 progressBarHandlers = new Hashtable();
624 JPanel progressPanel;
625 Long lId = new Long(id);
626 GridLayout layout = (GridLayout) statusPanel.getLayout();
627 if (progressBars.get(lId) != null)
629 progressPanel = (JPanel) progressBars.get(new Long(id));
630 statusPanel.remove(progressPanel);
631 progressBars.remove(lId);
632 progressPanel = null;
635 statusBar.setText(message);
637 if (progressBarHandlers.contains(lId))
639 progressBarHandlers.remove(lId);
641 layout.setRows(layout.getRows() - 1);
645 progressPanel = new JPanel(new BorderLayout(10, 5));
647 JProgressBar progressBar = new JProgressBar();
648 progressBar.setIndeterminate(true);
650 progressPanel.add(new JLabel(message), BorderLayout.WEST);
651 progressPanel.add(progressBar, BorderLayout.CENTER);
653 layout.setRows(layout.getRows() + 1);
654 statusPanel.add(progressPanel);
656 progressBars.put(lId, progressPanel);
659 // setMenusForViewport();
664 public void registerHandler(final long id,
665 final IProgressIndicatorHandler handler)
667 if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
669 throw new Error(MessageManager.getString("error.call_setprogressbar_before_registering_handler"));
671 progressBarHandlers.put(new Long(id), handler);
672 final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
673 if (handler.canCancel())
675 JButton cancel = new JButton(
676 MessageManager.getString("action.cancel"));
677 final IProgressIndicator us = this;
678 cancel.addActionListener(new ActionListener()
682 public void actionPerformed(ActionEvent e)
684 handler.cancelActivity(id);
685 us.setProgressBar(MessageManager.formatMessage("label.cancelled_params", new String[]{((JLabel) progressPanel.getComponent(0)).getText()}), id);
688 progressPanel.add(cancel, BorderLayout.EAST);
694 * @return true if any progress bars are still active
697 public boolean operationInProgress()
699 if (progressBars != null && progressBars.size() > 0)
707 protected void resetButton_actionPerformed(ActionEvent e)
710 top = 0; // ugly - prevents dimensionChanged events from being processed
711 xCombobox.setSelectedIndex(0);
712 yCombobox.setSelectedIndex(1);
714 zCombobox.setSelectedIndex(2);