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.
24 import java.awt.event.*;
25 import java.awt.print.*;
28 import jalview.datamodel.*;
29 import jalview.jbgui.*;
30 import jalview.util.MessageManager;
31 import jalview.viewmodel.PCAModel;
39 public class PCAPanel extends GPCAPanel implements Runnable,
54 * Creates a new PCAPanel object.
61 public PCAPanel(AlignmentPanel ap)
66 boolean sameLength = true;
67 boolean selected = av.getSelectionGroup() != null
68 && av.getSelectionGroup().getSize() > 0;
69 AlignmentView seqstrings = av.getAlignmentView(selected);
70 boolean nucleotide = av.getAlignment().isNucleotide();
74 seqs = av.getAlignment().getSequencesArray();
78 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
80 SeqCigar sq[] = seqstrings.getSequences();
81 int length = sq[0].getWidth();
83 for (int i = 0; i < seqs.length; i++)
85 if (sq[i].getWidth() != length)
97 "The sequences must be aligned before calculating PCA.\n"
98 + "Try using the Pad function in the edit menu,\n"
99 + "or one of the multiple sequence alignment web services.",
100 "Sequences not aligned", JOptionPane.WARNING_MESSAGE);
104 pcaModel = new PCAModel(seqstrings, seqs, nucleotide);
105 PaintRefresher.Register(this, av.getSequenceSetId());
107 rc = new RotatableCanvas(ap);
108 this.getContentPane().add(rc, BorderLayout.CENTER);
109 Thread worker = new Thread(this);
113 public void bgcolour_actionPerformed(ActionEvent e)
115 Color col = JColorChooser.showDialog(this, "Select Background Colour",
130 long progId = System.currentTimeMillis();
131 IProgressIndicator progress = this;
132 String message = "Recalculating PCA";
133 if (getParent() == null)
135 progress = ap.alignFrame;
136 message = "Calculating PCA";
138 progress.setProgressBar(message, progId);
141 calcSettings.setEnabled(false);
144 xCombobox.setSelectedIndex(0);
145 yCombobox.setSelectedIndex(1);
146 zCombobox.setSelectedIndex(2);
148 pcaModel.updateRc(rc);
150 nuclSetting.setSelected(pcaModel.isNucleotide());
151 protSetting.setSelected(!pcaModel.isNucleotide());
152 jvVersionSetting.setSelected(pcaModel.isJvCalcMode());
153 top = pcaModel.getTop();
155 } catch (OutOfMemoryError er)
157 new OOMWarning("calculating PCA", er);
161 progress.setProgressBar("", progId);
163 calcSettings.setEnabled(true);
165 if (getParent() == null)
168 Desktop.addInternalFrame(this, MessageManager.getString("label.principal_component_analysis"), 475,
174 protected void nuclSetting_actionPerfomed(ActionEvent arg0)
176 if (!pcaModel.isNucleotide())
178 pcaModel.setNucleotide(true);
179 Thread worker = new Thread(this);
186 protected void protSetting_actionPerfomed(ActionEvent arg0)
189 if (pcaModel.isNucleotide())
191 pcaModel.setNucleotide(false);
192 Thread worker = new Thread(this);
198 protected void jvVersionSetting_actionPerfomed(ActionEvent arg0)
200 pcaModel.setJvCalcMode(jvVersionSetting.isSelected());
201 Thread worker = new Thread(this);
208 void doDimensionChange()
215 int dim1 = top - xCombobox.getSelectedIndex();
216 int dim2 = top - yCombobox.getSelectedIndex();
217 int dim3 = top - zCombobox.getSelectedIndex();
218 pcaModel.updateRcView(dim1, dim2, dim3);
220 rc.rotmat.setIdentity();
222 rc.paint(rc.getGraphics());
231 protected void xCombobox_actionPerformed(ActionEvent e)
242 protected void yCombobox_actionPerformed(ActionEvent e)
253 protected void zCombobox_actionPerformed(ActionEvent e)
258 public void outputValues_actionPerformed(ActionEvent e)
260 CutAndPasteTransfer cap = new CutAndPasteTransfer();
263 cap.setText(pcaModel.getDetails());
264 Desktop.addInternalFrame(cap, MessageManager.getString("label.pca_details"), 500, 500);
265 } catch (OutOfMemoryError oom)
267 new OOMWarning("opening PCA details", oom);
272 public void showLabels_actionPerformed(ActionEvent e)
274 rc.showLabels(showLabels.getState());
277 public void print_actionPerformed(ActionEvent e)
279 PCAPrinter printer = new PCAPrinter();
283 public void originalSeqData_actionPerformed(ActionEvent e)
285 // this was cut'n'pasted from the equivalent TreePanel method - we should
286 // make this an abstract function of all jalview analysis windows
287 if (pcaModel.getSeqtrings() == null)
289 jalview.bin.Cache.log
290 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
293 // decide if av alignment is sufficiently different to original data to
294 // warrant a new window to be created
295 // create new alignmnt window with hidden regions (unhiding hidden regions
296 // yields unaligned seqs)
297 // or create a selection box around columns in alignment view
298 // test Alignment(SeqCigar[])
302 // we try to get the associated view's gap character
303 // but this may fail if the view was closed...
304 gc = av.getGapCharacter();
305 } catch (Exception ex)
309 Object[] alAndColsel = pcaModel.getSeqtrings()
310 .getAlignmentAndColumnSelection(gc);
312 if (alAndColsel != null && alAndColsel[0] != null)
314 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
316 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
317 Alignment dataset = (av != null && av.getAlignment() != null) ? av
318 .getAlignment().getDataset() : null;
321 al.setDataset(dataset);
327 AlignFrame af = new AlignFrame(al,
328 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
329 AlignFrame.DEFAULT_HEIGHT);
331 // >>>This is a fix for the moment, until a better solution is
333 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
335 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
338 Desktop.addInternalFrame(af, MessageManager.formatMessage("label.original_data_for_params", new String[]{this.title}),
339 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
343 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
344 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
345 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
348 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
352 class PCAPrinter extends Thread implements Printable
356 PrinterJob printJob = PrinterJob.getPrinterJob();
357 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
359 printJob.setPrintable(this, pf);
361 if (printJob.printDialog())
366 } catch (Exception PrintException)
368 PrintException.printStackTrace();
373 public int print(Graphics pg, PageFormat pf, int pi)
374 throws PrinterException
376 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
378 rc.drawBackground(pg, rc.bgColour);
380 if (rc.drawAxes == true)
387 return Printable.PAGE_EXISTS;
391 return Printable.NO_SUCH_PAGE;
402 public void eps_actionPerformed(ActionEvent e)
404 makePCAImage(jalview.util.ImageMaker.EPS);
413 public void png_actionPerformed(ActionEvent e)
415 makePCAImage(jalview.util.ImageMaker.PNG);
418 void makePCAImage(int type)
420 int width = rc.getWidth();
421 int height = rc.getHeight();
423 jalview.util.ImageMaker im;
425 if (type == jalview.util.ImageMaker.PNG)
427 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
428 "Make PNG image from PCA", width, height, null, null);
432 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
433 "Make EPS file from PCA", width, height, null,
437 if (im.getGraphics() != null)
439 rc.drawBackground(im.getGraphics(), Color.black);
440 rc.drawScene(im.getGraphics());
441 if (rc.drawAxes == true)
443 rc.drawAxes(im.getGraphics());
449 public void viewMenu_menuSelected()
451 buildAssociatedViewMenu();
454 void buildAssociatedViewMenu()
456 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
457 .getSequenceSetId());
458 if (aps.length == 1 && rc.av == aps[0].av)
460 associateViewsMenu.setVisible(false);
464 associateViewsMenu.setVisible(true);
466 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
468 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
471 associateViewsMenu.removeAll();
473 JRadioButtonMenuItem item;
474 ButtonGroup buttonGroup = new ButtonGroup();
475 int i, iSize = aps.length;
476 final PCAPanel thisPCAPanel = this;
477 for (i = 0; i < iSize; i++)
479 final AlignmentPanel ap = aps[i];
480 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
481 buttonGroup.add(item);
482 item.addActionListener(new ActionListener()
484 public void actionPerformed(ActionEvent evt)
486 rc.applyToAllViews = false;
489 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
493 associateViewsMenu.add(item);
496 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
498 buttonGroup.add(itemf);
500 itemf.setSelected(rc.applyToAllViews);
501 itemf.addActionListener(new ActionListener()
503 public void actionPerformed(ActionEvent evt)
505 rc.applyToAllViews = itemf.isSelected();
508 associateViewsMenu.add(itemf);
516 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
519 protected void outputPoints_actionPerformed(ActionEvent e)
521 CutAndPasteTransfer cap = new CutAndPasteTransfer();
524 cap.setText(pcaModel.getPointsasCsv(false,
525 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
526 zCombobox.getSelectedIndex()));
527 Desktop.addInternalFrame(cap, MessageManager.formatMessage("label.points_for_params", new String[]{this.getTitle()}), 500, 500);
528 } catch (OutOfMemoryError oom)
530 new OOMWarning("exporting PCA points", oom);
539 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
542 protected void outputProjPoints_actionPerformed(ActionEvent e)
544 CutAndPasteTransfer cap = new CutAndPasteTransfer();
547 cap.setText(pcaModel.getPointsasCsv(true,
548 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
549 zCombobox.getSelectedIndex()));
550 Desktop.addInternalFrame(cap, MessageManager.formatMessage("label.transformed_points_for_params", new String[]{this.getTitle()}),
552 } catch (OutOfMemoryError oom)
554 new OOMWarning("exporting transformed PCA points", oom);
559 // methods for implementing IProgressIndicator
560 // need to refactor to a reusable stub class
561 Hashtable progressBars, progressBarHandlers;
566 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
569 public void setProgressBar(String message, long id)
571 if (progressBars == null)
573 progressBars = new Hashtable();
574 progressBarHandlers = new Hashtable();
577 JPanel progressPanel;
578 Long lId = new Long(id);
579 GridLayout layout = (GridLayout) statusPanel.getLayout();
580 if (progressBars.get(lId) != null)
582 progressPanel = (JPanel) progressBars.get(new Long(id));
583 statusPanel.remove(progressPanel);
584 progressBars.remove(lId);
585 progressPanel = null;
588 statusBar.setText(message);
590 if (progressBarHandlers.contains(lId))
592 progressBarHandlers.remove(lId);
594 layout.setRows(layout.getRows() - 1);
598 progressPanel = new JPanel(new BorderLayout(10, 5));
600 JProgressBar progressBar = new JProgressBar();
601 progressBar.setIndeterminate(true);
603 progressPanel.add(new JLabel(message), BorderLayout.WEST);
604 progressPanel.add(progressBar, BorderLayout.CENTER);
606 layout.setRows(layout.getRows() + 1);
607 statusPanel.add(progressPanel);
609 progressBars.put(lId, progressPanel);
612 // setMenusForViewport();
617 public void registerHandler(final long id,
618 final IProgressIndicatorHandler handler)
620 if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
623 "call setProgressBar before registering the progress bar's handler.");
625 progressBarHandlers.put(new Long(id), handler);
626 final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
627 if (handler.canCancel())
629 JButton cancel = new JButton(MessageManager.getString("action.cancel"));
630 final IProgressIndicator us = this;
631 cancel.addActionListener(new ActionListener()
635 public void actionPerformed(ActionEvent e)
637 handler.cancelActivity(id);
640 + ((JLabel) progressPanel.getComponent(0))
644 progressPanel.add(cancel, BorderLayout.EAST);
650 * @return true if any progress bars are still active
653 public boolean operationInProgress()
655 if (progressBars != null && progressBars.size() > 0)
663 protected void resetButton_actionPerformed(ActionEvent e)
666 top = 0; // ugly - prevents dimensionChanged events from being processed
667 xCombobox.setSelectedIndex(0);
668 yCombobox.setSelectedIndex(1);
670 zCombobox.setSelectedIndex(2);