2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
23 import java.awt.event.*;
24 import java.awt.print.*;
27 import jalview.analysis.*;
28 import jalview.datamodel.*;
29 import jalview.jbgui.*;
30 import jalview.viewmodel.PCAModel;
38 public class PCAPanel extends GPCAPanel implements Runnable,
53 * Creates a new PCAPanel object.
60 public PCAPanel(AlignmentPanel ap)
65 boolean sameLength = true;
66 boolean selected = av.getSelectionGroup() != null
67 && av.getSelectionGroup().getSize() > 0;
68 AlignmentView seqstrings = av.getAlignmentView(selected);
69 boolean nucleotide = av.getAlignment().isNucleotide();
73 seqs = av.getAlignment().getSequencesArray();
77 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
79 SeqCigar sq[] = seqstrings.getSequences();
80 int length = sq[0].getWidth();
82 for (int i = 0; i < seqs.length; i++)
84 if (sq[i].getWidth() != length)
96 "The sequences must be aligned before calculating PCA.\n"
97 + "Try using the Pad function in the edit menu,\n"
98 + "or one of the multiple sequence alignment web services.",
99 "Sequences not aligned", JOptionPane.WARNING_MESSAGE);
103 pcaModel = new PCAModel(seqstrings, seqs, nucleotide);
104 PaintRefresher.Register(this, av.getSequenceSetId());
106 rc = new RotatableCanvas(ap);
107 this.getContentPane().add(rc, BorderLayout.CENTER);
108 Thread worker = new Thread(this);
112 public void bgcolour_actionPerformed(ActionEvent e)
114 Color col = JColorChooser.showDialog(this, "Select Background Colour",
129 long progId = System.currentTimeMillis();
130 IProgressIndicator progress = this;
131 String message = "Recalculating PCA";
132 if (getParent() == null)
134 progress = ap.alignFrame;
135 message = "Calculating PCA";
137 progress.setProgressBar(message, progId);
140 calcSettings.setEnabled(false);
143 xCombobox.setSelectedIndex(0);
144 yCombobox.setSelectedIndex(1);
145 zCombobox.setSelectedIndex(2);
147 pcaModel.updateRc(rc);
149 nuclSetting.setSelected(pcaModel.isNucleotide());
150 protSetting.setSelected(!pcaModel.isNucleotide());
151 jvVersionSetting.setSelected(pcaModel.isJvCalcMode());
152 top = pcaModel.getTop();
154 } catch (OutOfMemoryError er)
156 new OOMWarning("calculating PCA", er);
160 progress.setProgressBar("", progId);
162 calcSettings.setEnabled(true);
164 if (getParent() == null)
167 Desktop.addInternalFrame(this, "Principal component analysis", 475,
173 protected void nuclSetting_actionPerfomed(ActionEvent arg0)
175 if (!pcaModel.isNucleotide())
177 pcaModel.setNucleotide(true);
178 Thread worker = new Thread(this);
185 protected void protSetting_actionPerfomed(ActionEvent arg0)
188 if (pcaModel.isNucleotide())
190 pcaModel.setNucleotide(false);
191 Thread worker = new Thread(this);
197 protected void jvVersionSetting_actionPerfomed(ActionEvent arg0)
199 pcaModel.setJvCalcMode(jvVersionSetting.isSelected());
200 Thread worker = new Thread(this);
207 void doDimensionChange()
214 int dim1 = top - xCombobox.getSelectedIndex();
215 int dim2 = top - yCombobox.getSelectedIndex();
216 int dim3 = top - zCombobox.getSelectedIndex();
217 pcaModel.updateRcView(dim1, dim2, dim3);
219 rc.rotmat.setIdentity();
221 rc.paint(rc.getGraphics());
230 protected void xCombobox_actionPerformed(ActionEvent e)
241 protected void yCombobox_actionPerformed(ActionEvent e)
252 protected void zCombobox_actionPerformed(ActionEvent e)
257 public void outputValues_actionPerformed(ActionEvent e)
259 CutAndPasteTransfer cap = new CutAndPasteTransfer();
262 cap.setText(pcaModel.getDetails());
263 Desktop.addInternalFrame(cap, "PCA details", 500, 500);
264 } catch (OutOfMemoryError oom)
266 new OOMWarning("opening PCA details", oom);
271 public void showLabels_actionPerformed(ActionEvent e)
273 rc.showLabels(showLabels.getState());
276 public void print_actionPerformed(ActionEvent e)
278 PCAPrinter printer = new PCAPrinter();
282 public void originalSeqData_actionPerformed(ActionEvent e)
284 // this was cut'n'pasted from the equivalent TreePanel method - we should
285 // make this an abstract function of all jalview analysis windows
286 if (pcaModel.getSeqtrings() == null)
288 jalview.bin.Cache.log
289 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
292 // decide if av alignment is sufficiently different to original data to
293 // warrant a new window to be created
294 // create new alignmnt window with hidden regions (unhiding hidden regions
295 // yields unaligned seqs)
296 // or create a selection box around columns in alignment view
297 // test Alignment(SeqCigar[])
301 // we try to get the associated view's gap character
302 // but this may fail if the view was closed...
303 gc = av.getGapCharacter();
304 } catch (Exception ex)
308 Object[] alAndColsel = pcaModel.getSeqtrings()
309 .getAlignmentAndColumnSelection(gc);
311 if (alAndColsel != null && alAndColsel[0] != null)
313 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
315 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
316 Alignment dataset = (av != null && av.getAlignment() != null) ? av
317 .getAlignment().getDataset() : null;
320 al.setDataset(dataset);
326 AlignFrame af = new AlignFrame(al,
327 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
328 AlignFrame.DEFAULT_HEIGHT);
330 // >>>This is a fix for the moment, until a better solution is
332 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
334 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
337 Desktop.addInternalFrame(af, "Original Data for " + this.title,
338 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
342 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
343 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
344 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
347 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
351 class PCAPrinter extends Thread implements Printable
355 PrinterJob printJob = PrinterJob.getPrinterJob();
356 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
358 printJob.setPrintable(this, pf);
360 if (printJob.printDialog())
365 } catch (Exception PrintException)
367 PrintException.printStackTrace();
372 public int print(Graphics pg, PageFormat pf, int pi)
373 throws PrinterException
375 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
377 rc.drawBackground(pg, rc.bgColour);
379 if (rc.drawAxes == true)
386 return Printable.PAGE_EXISTS;
390 return Printable.NO_SUCH_PAGE;
401 public void eps_actionPerformed(ActionEvent e)
403 makePCAImage(jalview.util.ImageMaker.EPS);
412 public void png_actionPerformed(ActionEvent e)
414 makePCAImage(jalview.util.ImageMaker.PNG);
417 void makePCAImage(int type)
419 int width = rc.getWidth();
420 int height = rc.getHeight();
422 jalview.util.ImageMaker im;
424 if (type == jalview.util.ImageMaker.PNG)
426 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
427 "Make PNG image from PCA", width, height, null, null);
431 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
432 "Make EPS file from PCA", width, height, null,
436 if (im.getGraphics() != null)
438 rc.drawBackground(im.getGraphics(), Color.black);
439 rc.drawScene(im.getGraphics());
440 if (rc.drawAxes == true)
442 rc.drawAxes(im.getGraphics());
448 public void viewMenu_menuSelected()
450 buildAssociatedViewMenu();
453 void buildAssociatedViewMenu()
455 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
456 .getSequenceSetId());
457 if (aps.length == 1 && rc.av == aps[0].av)
459 associateViewsMenu.setVisible(false);
463 associateViewsMenu.setVisible(true);
465 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
467 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
470 associateViewsMenu.removeAll();
472 JRadioButtonMenuItem item;
473 ButtonGroup buttonGroup = new ButtonGroup();
474 int i, iSize = aps.length;
475 final PCAPanel thisPCAPanel = this;
476 for (i = 0; i < iSize; i++)
478 final AlignmentPanel ap = aps[i];
479 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
480 buttonGroup.add(item);
481 item.addActionListener(new ActionListener()
483 public void actionPerformed(ActionEvent evt)
485 rc.applyToAllViews = false;
488 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
492 associateViewsMenu.add(item);
495 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
497 buttonGroup.add(itemf);
499 itemf.setSelected(rc.applyToAllViews);
500 itemf.addActionListener(new ActionListener()
502 public void actionPerformed(ActionEvent evt)
504 rc.applyToAllViews = itemf.isSelected();
507 associateViewsMenu.add(itemf);
515 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
518 protected void outputPoints_actionPerformed(ActionEvent e)
520 CutAndPasteTransfer cap = new CutAndPasteTransfer();
523 cap.setText(pcaModel.getPointsasCsv(false,
524 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
525 zCombobox.getSelectedIndex()));
526 Desktop.addInternalFrame(cap, "Points for " + getTitle(), 500, 500);
527 } catch (OutOfMemoryError oom)
529 new OOMWarning("exporting PCA points", oom);
538 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
541 protected void outputProjPoints_actionPerformed(ActionEvent e)
543 CutAndPasteTransfer cap = new CutAndPasteTransfer();
546 cap.setText(pcaModel.getPointsasCsv(true,
547 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
548 zCombobox.getSelectedIndex()));
549 Desktop.addInternalFrame(cap, "Transformed points for " + getTitle(),
551 } catch (OutOfMemoryError oom)
553 new OOMWarning("exporting transformed PCA points", oom);
558 // methods for implementing IProgressIndicator
559 // need to refactor to a reusable stub class
560 Hashtable progressBars, progressBarHandlers;
565 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
568 public void setProgressBar(String message, long id)
570 if (progressBars == null)
572 progressBars = new Hashtable();
573 progressBarHandlers = new Hashtable();
576 JPanel progressPanel;
577 Long lId = new Long(id);
578 GridLayout layout = (GridLayout) statusPanel.getLayout();
579 if (progressBars.get(lId) != null)
581 progressPanel = (JPanel) progressBars.get(new Long(id));
582 statusPanel.remove(progressPanel);
583 progressBars.remove(lId);
584 progressPanel = null;
587 statusBar.setText(message);
589 if (progressBarHandlers.contains(lId))
591 progressBarHandlers.remove(lId);
593 layout.setRows(layout.getRows() - 1);
597 progressPanel = new JPanel(new BorderLayout(10, 5));
599 JProgressBar progressBar = new JProgressBar();
600 progressBar.setIndeterminate(true);
602 progressPanel.add(new JLabel(message), BorderLayout.WEST);
603 progressPanel.add(progressBar, BorderLayout.CENTER);
605 layout.setRows(layout.getRows() + 1);
606 statusPanel.add(progressPanel);
608 progressBars.put(lId, progressPanel);
611 // setMenusForViewport();
616 public void registerHandler(final long id,
617 final IProgressIndicatorHandler handler)
619 if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
622 "call setProgressBar before registering the progress bar's handler.");
624 progressBarHandlers.put(new Long(id), handler);
625 final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
626 if (handler.canCancel())
628 JButton cancel = new JButton("Cancel");
629 final IProgressIndicator us = this;
630 cancel.addActionListener(new ActionListener()
634 public void actionPerformed(ActionEvent e)
636 handler.cancelActivity(id);
639 + ((JLabel) progressPanel.getComponent(0))
643 progressPanel.add(cancel, BorderLayout.EAST);
649 * @return true if any progress bars are still active
652 public boolean operationInProgress()
654 if (progressBars != null && progressBars.size() > 0)
662 protected void resetButton_actionPerformed(ActionEvent e)
665 top = 0; // ugly - prevents dimensionChanged events from being processed
666 xCombobox.setSelectedIndex(0);
667 yCombobox.setSelectedIndex(1);
669 zCombobox.setSelectedIndex(2);