2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.awt.event.*;
24 import java.awt.print.*;
28 import jalview.datamodel.*;
29 import jalview.jbgui.*;
30 import jalview.schemes.ResidueProperties;
31 import jalview.schemes.ScoreMatrix;
32 import jalview.util.MessageManager;
33 import jalview.viewmodel.PCAModel;
41 public class PCAPanel extends GPCAPanel implements Runnable,
56 * Creates a new PCAPanel object.
63 public PCAPanel(AlignmentPanel ap)
68 boolean sameLength = true;
69 boolean selected = av.getSelectionGroup() != null
70 && av.getSelectionGroup().getSize() > 0;
71 AlignmentView seqstrings = av.getAlignmentView(selected);
72 boolean nucleotide = av.getAlignment().isNucleotide();
76 seqs = av.getAlignment().getSequencesArray();
80 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
82 SeqCigar sq[] = seqstrings.getSequences();
83 int length = sq[0].getWidth();
85 for (int i = 0; i < seqs.length; i++)
87 if (sq[i].getWidth() != length)
99 "The sequences must be aligned before calculating PCA.\n"
100 + "Try using the Pad function in the edit menu,\n"
101 + "or one of the multiple sequence alignment web services.",
102 "Sequences not aligned", JOptionPane.WARNING_MESSAGE);
106 pcaModel = new PCAModel(seqstrings, seqs, nucleotide);
107 PaintRefresher.Register(this, av.getSequenceSetId());
109 rc = new RotatableCanvas(ap);
110 this.getContentPane().add(rc, BorderLayout.CENTER);
111 Thread worker = new Thread(this);
115 protected void scoreMatrix_menuSelected()
117 scoreMatrixMenu.removeAll();
118 for (final Object sm:ResidueProperties.scoreMatrices.keySet())
120 JMenuItem jm=new JMenuItem();
121 jm.setText((String)sm);
122 jm.setSelected(pcaModel.getScore_matrix().equals((String)sm));
123 final PCAPanel us = this;
124 jm.addActionListener(new ActionListener()
127 public void actionPerformed(ActionEvent e)
129 if (!pcaModel.getScore_matrix().equals((String)sm))
131 pcaModel.setScore_matrix((String) sm);
132 Thread worker = new Thread(us);
137 scoreMatrixMenu.add(jm);
140 public void bgcolour_actionPerformed(ActionEvent e)
142 Color col = JColorChooser.showDialog(this, "Select Background Colour",
157 long progId = System.currentTimeMillis();
158 IProgressIndicator progress = this;
159 String message = "Recalculating PCA";
160 if (getParent() == null)
162 progress = ap.alignFrame;
163 message = "Calculating PCA";
165 progress.setProgressBar(message, progId);
168 calcSettings.setEnabled(false);
171 xCombobox.setSelectedIndex(0);
172 yCombobox.setSelectedIndex(1);
173 zCombobox.setSelectedIndex(2);
175 pcaModel.updateRc(rc);
177 nuclSetting.setSelected(pcaModel.isNucleotide());
178 protSetting.setSelected(!pcaModel.isNucleotide());
179 jvVersionSetting.setSelected(pcaModel.isJvCalcMode());
180 top = pcaModel.getTop();
182 } catch (OutOfMemoryError er)
184 new OOMWarning("calculating PCA", er);
188 progress.setProgressBar("", progId);
190 calcSettings.setEnabled(true);
192 if (getParent() == null)
195 Desktop.addInternalFrame(this, MessageManager.getString("label.principal_component_analysis"), 475,
201 protected void nuclSetting_actionPerfomed(ActionEvent arg0)
203 if (!pcaModel.isNucleotide())
205 pcaModel.setNucleotide(true);
206 Thread worker = new Thread(this);
213 protected void protSetting_actionPerfomed(ActionEvent arg0)
216 if (pcaModel.isNucleotide())
218 pcaModel.setNucleotide(false);
219 Thread worker = new Thread(this);
225 protected void jvVersionSetting_actionPerfomed(ActionEvent arg0)
227 pcaModel.setJvCalcMode(jvVersionSetting.isSelected());
228 Thread worker = new Thread(this);
235 void doDimensionChange()
242 int dim1 = top - xCombobox.getSelectedIndex();
243 int dim2 = top - yCombobox.getSelectedIndex();
244 int dim3 = top - zCombobox.getSelectedIndex();
245 pcaModel.updateRcView(dim1, dim2, dim3);
247 rc.rotmat.setIdentity();
249 rc.paint(rc.getGraphics());
258 protected void xCombobox_actionPerformed(ActionEvent e)
269 protected void yCombobox_actionPerformed(ActionEvent e)
280 protected void zCombobox_actionPerformed(ActionEvent e)
285 public void outputValues_actionPerformed(ActionEvent e)
287 CutAndPasteTransfer cap = new CutAndPasteTransfer();
290 cap.setText(pcaModel.getDetails());
291 Desktop.addInternalFrame(cap, MessageManager.getString("label.pca_details"), 500, 500);
292 } catch (OutOfMemoryError oom)
294 new OOMWarning("opening PCA details", oom);
299 public void showLabels_actionPerformed(ActionEvent e)
301 rc.showLabels(showLabels.getState());
304 public void print_actionPerformed(ActionEvent e)
306 PCAPrinter printer = new PCAPrinter();
310 public void originalSeqData_actionPerformed(ActionEvent e)
312 // this was cut'n'pasted from the equivalent TreePanel method - we should
313 // make this an abstract function of all jalview analysis windows
314 if (pcaModel.getSeqtrings() == null)
316 jalview.bin.Cache.log
317 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
320 // decide if av alignment is sufficiently different to original data to
321 // warrant a new window to be created
322 // create new alignmnt window with hidden regions (unhiding hidden regions
323 // yields unaligned seqs)
324 // or create a selection box around columns in alignment view
325 // test Alignment(SeqCigar[])
329 // we try to get the associated view's gap character
330 // but this may fail if the view was closed...
331 gc = av.getGapCharacter();
332 } catch (Exception ex)
336 Object[] alAndColsel = pcaModel.getSeqtrings()
337 .getAlignmentAndColumnSelection(gc);
339 if (alAndColsel != null && alAndColsel[0] != null)
341 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
343 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
344 Alignment dataset = (av != null && av.getAlignment() != null) ? av
345 .getAlignment().getDataset() : null;
348 al.setDataset(dataset);
354 AlignFrame af = new AlignFrame(al,
355 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
356 AlignFrame.DEFAULT_HEIGHT);
358 // >>>This is a fix for the moment, until a better solution is
360 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
362 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
365 Desktop.addInternalFrame(af, MessageManager.formatMessage("label.original_data_for_params", new String[]{this.title}),
366 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
370 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
371 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
372 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
375 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
379 class PCAPrinter extends Thread implements Printable
383 PrinterJob printJob = PrinterJob.getPrinterJob();
384 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
386 printJob.setPrintable(this, pf);
388 if (printJob.printDialog())
393 } catch (Exception PrintException)
395 PrintException.printStackTrace();
400 public int print(Graphics pg, PageFormat pf, int pi)
401 throws PrinterException
403 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
405 rc.drawBackground(pg, rc.bgColour);
407 if (rc.drawAxes == true)
414 return Printable.PAGE_EXISTS;
418 return Printable.NO_SUCH_PAGE;
429 public void eps_actionPerformed(ActionEvent e)
431 makePCAImage(jalview.util.ImageMaker.EPS);
440 public void png_actionPerformed(ActionEvent e)
442 makePCAImage(jalview.util.ImageMaker.PNG);
445 void makePCAImage(int type)
447 int width = rc.getWidth();
448 int height = rc.getHeight();
450 jalview.util.ImageMaker im;
452 if (type == jalview.util.ImageMaker.PNG)
454 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
455 "Make PNG image from PCA", width, height, null, null);
459 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
460 "Make EPS file from PCA", width, height, null,
464 if (im.getGraphics() != null)
466 rc.drawBackground(im.getGraphics(), Color.black);
467 rc.drawScene(im.getGraphics());
468 if (rc.drawAxes == true)
470 rc.drawAxes(im.getGraphics());
477 public void viewMenu_menuSelected()
479 buildAssociatedViewMenu();
482 void buildAssociatedViewMenu()
484 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
485 .getSequenceSetId());
486 if (aps.length == 1 && rc.av == aps[0].av)
488 associateViewsMenu.setVisible(false);
492 associateViewsMenu.setVisible(true);
494 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
496 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
499 associateViewsMenu.removeAll();
501 JRadioButtonMenuItem item;
502 ButtonGroup buttonGroup = new ButtonGroup();
503 int i, iSize = aps.length;
504 final PCAPanel thisPCAPanel = this;
505 for (i = 0; i < iSize; i++)
507 final AlignmentPanel ap = aps[i];
508 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
509 buttonGroup.add(item);
510 item.addActionListener(new ActionListener()
512 public void actionPerformed(ActionEvent evt)
514 rc.applyToAllViews = false;
517 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
521 associateViewsMenu.add(item);
524 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
526 buttonGroup.add(itemf);
528 itemf.setSelected(rc.applyToAllViews);
529 itemf.addActionListener(new ActionListener()
531 public void actionPerformed(ActionEvent evt)
533 rc.applyToAllViews = itemf.isSelected();
536 associateViewsMenu.add(itemf);
544 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
547 protected void outputPoints_actionPerformed(ActionEvent e)
549 CutAndPasteTransfer cap = new CutAndPasteTransfer();
552 cap.setText(pcaModel.getPointsasCsv(false,
553 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
554 zCombobox.getSelectedIndex()));
555 Desktop.addInternalFrame(cap, MessageManager.formatMessage("label.points_for_params", new String[]{this.getTitle()}), 500, 500);
556 } catch (OutOfMemoryError oom)
558 new OOMWarning("exporting PCA points", oom);
567 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
570 protected void outputProjPoints_actionPerformed(ActionEvent e)
572 CutAndPasteTransfer cap = new CutAndPasteTransfer();
575 cap.setText(pcaModel.getPointsasCsv(true,
576 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
577 zCombobox.getSelectedIndex()));
578 Desktop.addInternalFrame(cap, MessageManager.formatMessage("label.transformed_points_for_params", new String[]{this.getTitle()}),
580 } catch (OutOfMemoryError oom)
582 new OOMWarning("exporting transformed PCA points", oom);
587 // methods for implementing IProgressIndicator
588 // need to refactor to a reusable stub class
589 Hashtable progressBars, progressBarHandlers;
594 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
597 public void setProgressBar(String message, long id)
599 if (progressBars == null)
601 progressBars = new Hashtable();
602 progressBarHandlers = new Hashtable();
605 JPanel progressPanel;
606 Long lId = new Long(id);
607 GridLayout layout = (GridLayout) statusPanel.getLayout();
608 if (progressBars.get(lId) != null)
610 progressPanel = (JPanel) progressBars.get(new Long(id));
611 statusPanel.remove(progressPanel);
612 progressBars.remove(lId);
613 progressPanel = null;
616 statusBar.setText(message);
618 if (progressBarHandlers.contains(lId))
620 progressBarHandlers.remove(lId);
622 layout.setRows(layout.getRows() - 1);
626 progressPanel = new JPanel(new BorderLayout(10, 5));
628 JProgressBar progressBar = new JProgressBar();
629 progressBar.setIndeterminate(true);
631 progressPanel.add(new JLabel(message), BorderLayout.WEST);
632 progressPanel.add(progressBar, BorderLayout.CENTER);
634 layout.setRows(layout.getRows() + 1);
635 statusPanel.add(progressPanel);
637 progressBars.put(lId, progressPanel);
640 // setMenusForViewport();
645 public void registerHandler(final long id,
646 final IProgressIndicatorHandler handler)
648 if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
651 "call setProgressBar before registering the progress bar's handler.");
653 progressBarHandlers.put(new Long(id), handler);
654 final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
655 if (handler.canCancel())
657 JButton cancel = new JButton(MessageManager.getString("action.cancel"));
658 final IProgressIndicator us = this;
659 cancel.addActionListener(new ActionListener()
663 public void actionPerformed(ActionEvent e)
665 handler.cancelActivity(id);
668 + ((JLabel) progressPanel.getComponent(0))
672 progressPanel.add(cancel, BorderLayout.EAST);
678 * @return true if any progress bars are still active
681 public boolean operationInProgress()
683 if (progressBars != null && progressBars.size() > 0)
691 protected void resetButton_actionPerformed(ActionEvent e)
694 top = 0; // ugly - prevents dimensionChanged events from being processed
695 xCombobox.setSelectedIndex(0);
696 yCombobox.setSelectedIndex(1);
698 zCombobox.setSelectedIndex(2);