2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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.util.MessageManager;
32 import jalview.viewmodel.PCAModel;
40 public class PCAPanel extends GPCAPanel implements Runnable,
55 * Creates a new PCAPanel object.
62 public PCAPanel(AlignmentPanel ap)
67 boolean sameLength = true;
68 boolean selected = av.getSelectionGroup() != null
69 && av.getSelectionGroup().getSize() > 0;
70 AlignmentView seqstrings = av.getAlignmentView(selected);
71 boolean nucleotide = av.getAlignment().isNucleotide();
75 seqs = av.getAlignment().getSequencesArray();
79 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
81 SeqCigar sq[] = seqstrings.getSequences();
82 int length = sq[0].getWidth();
84 for (int i = 0; i < seqs.length; i++)
86 if (sq[i].getWidth() != length)
98 "The sequences must be aligned before calculating PCA.\n"
99 + "Try using the Pad function in the edit menu,\n"
100 + "or one of the multiple sequence alignment web services.",
101 "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);
114 protected void scoreMatrix_menuSelected()
116 scoreMatrixMenu.removeAll();
117 for (final String sm:ResidueProperties.scoreMatrices.keySet())
119 if (ResidueProperties.getScoreMatrix(sm) != null)
121 // create an entry for this score matrix for use in PCA
122 JCheckBoxMenuItem jm = new JCheckBoxMenuItem();
123 jm.setText(MessageManager
124 .getStringOrReturn("label.score_model", sm));
125 jm.setSelected(pcaModel.getScore_matrix().equals(sm));
126 if ((ResidueProperties.scoreMatrices.get(sm).isDNA() && ResidueProperties.scoreMatrices
127 .get(sm).isProtein())
128 || pcaModel.isNucleotide() == ResidueProperties.scoreMatrices
131 final PCAPanel us = this;
132 jm.addActionListener(new ActionListener()
135 public void actionPerformed(ActionEvent e)
137 if (!pcaModel.getScore_matrix().equals((String) sm))
139 pcaModel.setScore_matrix((String) sm);
140 Thread worker = new Thread(us);
145 scoreMatrixMenu.add(jm);
150 public void bgcolour_actionPerformed(ActionEvent e)
152 Color col = JColorChooser.showDialog(this, "Select Background Colour",
167 long progId = System.currentTimeMillis();
168 IProgressIndicator progress = this;
169 String message = "Recalculating PCA";
170 if (getParent() == null)
172 progress = ap.alignFrame;
173 message = "Calculating PCA";
175 progress.setProgressBar(message, progId);
178 calcSettings.setEnabled(false);
181 xCombobox.setSelectedIndex(0);
182 yCombobox.setSelectedIndex(1);
183 zCombobox.setSelectedIndex(2);
185 pcaModel.updateRc(rc);
187 nuclSetting.setSelected(pcaModel.isNucleotide());
188 protSetting.setSelected(!pcaModel.isNucleotide());
189 jvVersionSetting.setSelected(pcaModel.isJvCalcMode());
190 top = pcaModel.getTop();
192 } catch (OutOfMemoryError er)
194 new OOMWarning("calculating PCA", er);
198 progress.setProgressBar("", progId);
200 calcSettings.setEnabled(true);
202 if (getParent() == null)
205 Desktop.addInternalFrame(this, MessageManager.getString("label.principal_component_analysis"), 475,
211 protected void nuclSetting_actionPerfomed(ActionEvent arg0)
213 if (!pcaModel.isNucleotide())
215 pcaModel.setNucleotide(true);
216 pcaModel.setScore_matrix("DNA");
217 Thread worker = new Thread(this);
224 protected void protSetting_actionPerfomed(ActionEvent arg0)
227 if (pcaModel.isNucleotide())
229 pcaModel.setNucleotide(false);
230 pcaModel.setScore_matrix("BLOSUM62");
231 Thread worker = new Thread(this);
237 protected void jvVersionSetting_actionPerfomed(ActionEvent arg0)
239 pcaModel.setJvCalcMode(jvVersionSetting.isSelected());
240 Thread worker = new Thread(this);
247 void doDimensionChange()
254 int dim1 = top - xCombobox.getSelectedIndex();
255 int dim2 = top - yCombobox.getSelectedIndex();
256 int dim3 = top - zCombobox.getSelectedIndex();
257 pcaModel.updateRcView(dim1, dim2, dim3);
259 rc.rotmat.setIdentity();
261 rc.paint(rc.getGraphics());
270 protected void xCombobox_actionPerformed(ActionEvent e)
281 protected void yCombobox_actionPerformed(ActionEvent e)
292 protected void zCombobox_actionPerformed(ActionEvent e)
297 public void outputValues_actionPerformed(ActionEvent e)
299 CutAndPasteTransfer cap = new CutAndPasteTransfer();
302 cap.setText(pcaModel.getDetails());
303 Desktop.addInternalFrame(cap, MessageManager.getString("label.pca_details"), 500, 500);
304 } catch (OutOfMemoryError oom)
306 new OOMWarning("opening PCA details", oom);
311 public void showLabels_actionPerformed(ActionEvent e)
313 rc.showLabels(showLabels.getState());
316 public void print_actionPerformed(ActionEvent e)
318 PCAPrinter printer = new PCAPrinter();
322 public void originalSeqData_actionPerformed(ActionEvent e)
324 // this was cut'n'pasted from the equivalent TreePanel method - we should
325 // make this an abstract function of all jalview analysis windows
326 if (pcaModel.getSeqtrings() == null)
328 jalview.bin.Cache.log
329 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
332 // decide if av alignment is sufficiently different to original data to
333 // warrant a new window to be created
334 // create new alignmnt window with hidden regions (unhiding hidden regions
335 // yields unaligned seqs)
336 // or create a selection box around columns in alignment view
337 // test Alignment(SeqCigar[])
341 // we try to get the associated view's gap character
342 // but this may fail if the view was closed...
343 gc = av.getGapCharacter();
344 } catch (Exception ex)
348 Object[] alAndColsel = pcaModel.getSeqtrings()
349 .getAlignmentAndColumnSelection(gc);
351 if (alAndColsel != null && alAndColsel[0] != null)
353 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
355 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
356 Alignment dataset = (av != null && av.getAlignment() != null) ? av
357 .getAlignment().getDataset() : null;
360 al.setDataset(dataset);
366 AlignFrame af = new AlignFrame(al,
367 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
368 AlignFrame.DEFAULT_HEIGHT);
370 // >>>This is a fix for the moment, until a better solution is
372 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
374 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
377 Desktop.addInternalFrame(af, MessageManager.formatMessage("label.original_data_for_params", new String[]{this.title}),
378 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
382 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
383 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
384 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
387 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
391 class PCAPrinter extends Thread implements Printable
395 PrinterJob printJob = PrinterJob.getPrinterJob();
396 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
398 printJob.setPrintable(this, pf);
400 if (printJob.printDialog())
405 } catch (Exception PrintException)
407 PrintException.printStackTrace();
412 public int print(Graphics pg, PageFormat pf, int pi)
413 throws PrinterException
415 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
417 rc.drawBackground(pg, rc.bgColour);
419 if (rc.drawAxes == true)
426 return Printable.PAGE_EXISTS;
430 return Printable.NO_SUCH_PAGE;
441 public void eps_actionPerformed(ActionEvent e)
443 makePCAImage(jalview.util.ImageMaker.EPS);
452 public void png_actionPerformed(ActionEvent e)
454 makePCAImage(jalview.util.ImageMaker.PNG);
457 void makePCAImage(int type)
459 int width = rc.getWidth();
460 int height = rc.getHeight();
462 jalview.util.ImageMaker im;
464 if (type == jalview.util.ImageMaker.PNG)
466 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
467 "Make PNG image from PCA", width, height, null, null);
471 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
472 "Make EPS file from PCA", width, height, null,
476 if (im.getGraphics() != null)
478 rc.drawBackground(im.getGraphics(), Color.black);
479 rc.drawScene(im.getGraphics());
480 if (rc.drawAxes == true)
482 rc.drawAxes(im.getGraphics());
489 public void viewMenu_menuSelected()
491 buildAssociatedViewMenu();
494 void buildAssociatedViewMenu()
496 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
497 .getSequenceSetId());
498 if (aps.length == 1 && rc.av == aps[0].av)
500 associateViewsMenu.setVisible(false);
504 associateViewsMenu.setVisible(true);
506 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
508 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
511 associateViewsMenu.removeAll();
513 JRadioButtonMenuItem item;
514 ButtonGroup buttonGroup = new ButtonGroup();
515 int i, iSize = aps.length;
516 final PCAPanel thisPCAPanel = this;
517 for (i = 0; i < iSize; i++)
519 final AlignmentPanel ap = aps[i];
520 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
521 buttonGroup.add(item);
522 item.addActionListener(new ActionListener()
524 public void actionPerformed(ActionEvent evt)
526 rc.applyToAllViews = false;
529 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
533 associateViewsMenu.add(item);
536 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
538 buttonGroup.add(itemf);
540 itemf.setSelected(rc.applyToAllViews);
541 itemf.addActionListener(new ActionListener()
543 public void actionPerformed(ActionEvent evt)
545 rc.applyToAllViews = itemf.isSelected();
548 associateViewsMenu.add(itemf);
556 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
559 protected void outputPoints_actionPerformed(ActionEvent e)
561 CutAndPasteTransfer cap = new CutAndPasteTransfer();
564 cap.setText(pcaModel.getPointsasCsv(false,
565 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
566 zCombobox.getSelectedIndex()));
567 Desktop.addInternalFrame(cap, MessageManager.formatMessage("label.points_for_params", new String[]{this.getTitle()}), 500, 500);
568 } catch (OutOfMemoryError oom)
570 new OOMWarning("exporting PCA points", oom);
579 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
582 protected void outputProjPoints_actionPerformed(ActionEvent e)
584 CutAndPasteTransfer cap = new CutAndPasteTransfer();
587 cap.setText(pcaModel.getPointsasCsv(true,
588 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
589 zCombobox.getSelectedIndex()));
590 Desktop.addInternalFrame(cap, MessageManager.formatMessage("label.transformed_points_for_params", new String[]{this.getTitle()}),
592 } catch (OutOfMemoryError oom)
594 new OOMWarning("exporting transformed PCA points", oom);
599 // methods for implementing IProgressIndicator
600 // need to refactor to a reusable stub class
601 Hashtable progressBars, progressBarHandlers;
606 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
609 public void setProgressBar(String message, long id)
611 if (progressBars == null)
613 progressBars = new Hashtable();
614 progressBarHandlers = new Hashtable();
617 JPanel progressPanel;
618 Long lId = new Long(id);
619 GridLayout layout = (GridLayout) statusPanel.getLayout();
620 if (progressBars.get(lId) != null)
622 progressPanel = (JPanel) progressBars.get(new Long(id));
623 statusPanel.remove(progressPanel);
624 progressBars.remove(lId);
625 progressPanel = null;
628 statusBar.setText(message);
630 if (progressBarHandlers.contains(lId))
632 progressBarHandlers.remove(lId);
634 layout.setRows(layout.getRows() - 1);
638 progressPanel = new JPanel(new BorderLayout(10, 5));
640 JProgressBar progressBar = new JProgressBar();
641 progressBar.setIndeterminate(true);
643 progressPanel.add(new JLabel(message), BorderLayout.WEST);
644 progressPanel.add(progressBar, BorderLayout.CENTER);
646 layout.setRows(layout.getRows() + 1);
647 statusPanel.add(progressPanel);
649 progressBars.put(lId, progressPanel);
652 // setMenusForViewport();
657 public void registerHandler(final long id,
658 final IProgressIndicatorHandler handler)
660 if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
663 "call setProgressBar before registering the progress bar's handler.");
665 progressBarHandlers.put(new Long(id), handler);
666 final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
667 if (handler.canCancel())
669 JButton cancel = new JButton(MessageManager.getString("action.cancel"));
670 final IProgressIndicator us = this;
671 cancel.addActionListener(new ActionListener()
675 public void actionPerformed(ActionEvent e)
677 handler.cancelActivity(id);
680 + ((JLabel) progressPanel.getComponent(0))
684 progressPanel.add(cancel, BorderLayout.EAST);
690 * @return true if any progress bars are still active
693 public boolean operationInProgress()
695 if (progressBars != null && progressBars.size() > 0)
703 protected void resetButton_actionPerformed(ActionEvent e)
706 top = 0; // ugly - prevents dimensionChanged events from being processed
707 xCombobox.setSelectedIndex(0);
708 yCombobox.setSelectedIndex(1);
710 zCombobox.setSelectedIndex(2);