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.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 JCheckBoxMenuItem jm=new JCheckBoxMenuItem();
120 jm.setText(MessageManager.getStringOrReturn("label.score_model", sm));
121 jm.setSelected(pcaModel.getScore_matrix().equals(sm));
122 if (pcaModel.isNucleotide()==ResidueProperties.scoreMatrices.get(sm).isDNA())
124 final PCAPanel us = this;
125 jm.addActionListener(new ActionListener()
128 public void actionPerformed(ActionEvent e)
130 if (!pcaModel.getScore_matrix().equals((String) sm))
132 pcaModel.setScore_matrix((String) sm);
133 Thread worker = new Thread(us);
138 scoreMatrixMenu.add(jm);
142 public void bgcolour_actionPerformed(ActionEvent e)
144 Color col = JColorChooser.showDialog(this, "Select Background Colour",
159 long progId = System.currentTimeMillis();
160 IProgressIndicator progress = this;
161 String message = "Recalculating PCA";
162 if (getParent() == null)
164 progress = ap.alignFrame;
165 message = "Calculating PCA";
167 progress.setProgressBar(message, progId);
170 calcSettings.setEnabled(false);
173 xCombobox.setSelectedIndex(0);
174 yCombobox.setSelectedIndex(1);
175 zCombobox.setSelectedIndex(2);
177 pcaModel.updateRc(rc);
179 nuclSetting.setSelected(pcaModel.isNucleotide());
180 protSetting.setSelected(!pcaModel.isNucleotide());
181 jvVersionSetting.setSelected(pcaModel.isJvCalcMode());
182 top = pcaModel.getTop();
184 } catch (OutOfMemoryError er)
186 new OOMWarning("calculating PCA", er);
190 progress.setProgressBar("", progId);
192 calcSettings.setEnabled(true);
194 if (getParent() == null)
197 Desktop.addInternalFrame(this, MessageManager.getString("label.principal_component_analysis"), 475,
203 protected void nuclSetting_actionPerfomed(ActionEvent arg0)
205 if (!pcaModel.isNucleotide())
207 pcaModel.setNucleotide(true);
208 pcaModel.setScore_matrix("DNA");
209 Thread worker = new Thread(this);
216 protected void protSetting_actionPerfomed(ActionEvent arg0)
219 if (pcaModel.isNucleotide())
221 pcaModel.setNucleotide(false);
222 pcaModel.setScore_matrix("BLOSUM62");
223 Thread worker = new Thread(this);
229 protected void jvVersionSetting_actionPerfomed(ActionEvent arg0)
231 pcaModel.setJvCalcMode(jvVersionSetting.isSelected());
232 Thread worker = new Thread(this);
239 void doDimensionChange()
246 int dim1 = top - xCombobox.getSelectedIndex();
247 int dim2 = top - yCombobox.getSelectedIndex();
248 int dim3 = top - zCombobox.getSelectedIndex();
249 pcaModel.updateRcView(dim1, dim2, dim3);
251 rc.rotmat.setIdentity();
253 rc.paint(rc.getGraphics());
262 protected void xCombobox_actionPerformed(ActionEvent e)
273 protected void yCombobox_actionPerformed(ActionEvent e)
284 protected void zCombobox_actionPerformed(ActionEvent e)
289 public void outputValues_actionPerformed(ActionEvent e)
291 CutAndPasteTransfer cap = new CutAndPasteTransfer();
294 cap.setText(pcaModel.getDetails());
295 Desktop.addInternalFrame(cap, MessageManager.getString("label.pca_details"), 500, 500);
296 } catch (OutOfMemoryError oom)
298 new OOMWarning("opening PCA details", oom);
303 public void showLabels_actionPerformed(ActionEvent e)
305 rc.showLabels(showLabels.getState());
308 public void print_actionPerformed(ActionEvent e)
310 PCAPrinter printer = new PCAPrinter();
314 public void originalSeqData_actionPerformed(ActionEvent e)
316 // this was cut'n'pasted from the equivalent TreePanel method - we should
317 // make this an abstract function of all jalview analysis windows
318 if (pcaModel.getSeqtrings() == null)
320 jalview.bin.Cache.log
321 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
324 // decide if av alignment is sufficiently different to original data to
325 // warrant a new window to be created
326 // create new alignmnt window with hidden regions (unhiding hidden regions
327 // yields unaligned seqs)
328 // or create a selection box around columns in alignment view
329 // test Alignment(SeqCigar[])
333 // we try to get the associated view's gap character
334 // but this may fail if the view was closed...
335 gc = av.getGapCharacter();
336 } catch (Exception ex)
340 Object[] alAndColsel = pcaModel.getSeqtrings()
341 .getAlignmentAndColumnSelection(gc);
343 if (alAndColsel != null && alAndColsel[0] != null)
345 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
347 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
348 Alignment dataset = (av != null && av.getAlignment() != null) ? av
349 .getAlignment().getDataset() : null;
352 al.setDataset(dataset);
358 AlignFrame af = new AlignFrame(al,
359 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
360 AlignFrame.DEFAULT_HEIGHT);
362 // >>>This is a fix for the moment, until a better solution is
364 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
366 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
369 Desktop.addInternalFrame(af, MessageManager.formatMessage("label.original_data_for_params", new String[]{this.title}),
370 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
374 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
375 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
376 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
379 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
383 class PCAPrinter extends Thread implements Printable
387 PrinterJob printJob = PrinterJob.getPrinterJob();
388 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
390 printJob.setPrintable(this, pf);
392 if (printJob.printDialog())
397 } catch (Exception PrintException)
399 PrintException.printStackTrace();
404 public int print(Graphics pg, PageFormat pf, int pi)
405 throws PrinterException
407 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
409 rc.drawBackground(pg, rc.bgColour);
411 if (rc.drawAxes == true)
418 return Printable.PAGE_EXISTS;
422 return Printable.NO_SUCH_PAGE;
433 public void eps_actionPerformed(ActionEvent e)
435 makePCAImage(jalview.util.ImageMaker.EPS);
444 public void png_actionPerformed(ActionEvent e)
446 makePCAImage(jalview.util.ImageMaker.PNG);
449 void makePCAImage(int type)
451 int width = rc.getWidth();
452 int height = rc.getHeight();
454 jalview.util.ImageMaker im;
456 if (type == jalview.util.ImageMaker.PNG)
458 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
459 "Make PNG image from PCA", width, height, null, null);
463 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
464 "Make EPS file from PCA", width, height, null,
468 if (im.getGraphics() != null)
470 rc.drawBackground(im.getGraphics(), Color.black);
471 rc.drawScene(im.getGraphics());
472 if (rc.drawAxes == true)
474 rc.drawAxes(im.getGraphics());
481 public void viewMenu_menuSelected()
483 buildAssociatedViewMenu();
486 void buildAssociatedViewMenu()
488 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
489 .getSequenceSetId());
490 if (aps.length == 1 && rc.av == aps[0].av)
492 associateViewsMenu.setVisible(false);
496 associateViewsMenu.setVisible(true);
498 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
500 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
503 associateViewsMenu.removeAll();
505 JRadioButtonMenuItem item;
506 ButtonGroup buttonGroup = new ButtonGroup();
507 int i, iSize = aps.length;
508 final PCAPanel thisPCAPanel = this;
509 for (i = 0; i < iSize; i++)
511 final AlignmentPanel ap = aps[i];
512 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
513 buttonGroup.add(item);
514 item.addActionListener(new ActionListener()
516 public void actionPerformed(ActionEvent evt)
518 rc.applyToAllViews = false;
521 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
525 associateViewsMenu.add(item);
528 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
530 buttonGroup.add(itemf);
532 itemf.setSelected(rc.applyToAllViews);
533 itemf.addActionListener(new ActionListener()
535 public void actionPerformed(ActionEvent evt)
537 rc.applyToAllViews = itemf.isSelected();
540 associateViewsMenu.add(itemf);
548 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
551 protected void outputPoints_actionPerformed(ActionEvent e)
553 CutAndPasteTransfer cap = new CutAndPasteTransfer();
556 cap.setText(pcaModel.getPointsasCsv(false,
557 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
558 zCombobox.getSelectedIndex()));
559 Desktop.addInternalFrame(cap, MessageManager.formatMessage("label.points_for_params", new String[]{this.getTitle()}), 500, 500);
560 } catch (OutOfMemoryError oom)
562 new OOMWarning("exporting PCA points", oom);
571 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
574 protected void outputProjPoints_actionPerformed(ActionEvent e)
576 CutAndPasteTransfer cap = new CutAndPasteTransfer();
579 cap.setText(pcaModel.getPointsasCsv(true,
580 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
581 zCombobox.getSelectedIndex()));
582 Desktop.addInternalFrame(cap, MessageManager.formatMessage("label.transformed_points_for_params", new String[]{this.getTitle()}),
584 } catch (OutOfMemoryError oom)
586 new OOMWarning("exporting transformed PCA points", oom);
591 // methods for implementing IProgressIndicator
592 // need to refactor to a reusable stub class
593 Hashtable progressBars, progressBarHandlers;
598 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
601 public void setProgressBar(String message, long id)
603 if (progressBars == null)
605 progressBars = new Hashtable();
606 progressBarHandlers = new Hashtable();
609 JPanel progressPanel;
610 Long lId = new Long(id);
611 GridLayout layout = (GridLayout) statusPanel.getLayout();
612 if (progressBars.get(lId) != null)
614 progressPanel = (JPanel) progressBars.get(new Long(id));
615 statusPanel.remove(progressPanel);
616 progressBars.remove(lId);
617 progressPanel = null;
620 statusBar.setText(message);
622 if (progressBarHandlers.contains(lId))
624 progressBarHandlers.remove(lId);
626 layout.setRows(layout.getRows() - 1);
630 progressPanel = new JPanel(new BorderLayout(10, 5));
632 JProgressBar progressBar = new JProgressBar();
633 progressBar.setIndeterminate(true);
635 progressPanel.add(new JLabel(message), BorderLayout.WEST);
636 progressPanel.add(progressBar, BorderLayout.CENTER);
638 layout.setRows(layout.getRows() + 1);
639 statusPanel.add(progressPanel);
641 progressBars.put(lId, progressPanel);
644 // setMenusForViewport();
649 public void registerHandler(final long id,
650 final IProgressIndicatorHandler handler)
652 if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
655 "call setProgressBar before registering the progress bar's handler.");
657 progressBarHandlers.put(new Long(id), handler);
658 final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
659 if (handler.canCancel())
661 JButton cancel = new JButton(MessageManager.getString("action.cancel"));
662 final IProgressIndicator us = this;
663 cancel.addActionListener(new ActionListener()
667 public void actionPerformed(ActionEvent e)
669 handler.cancelActivity(id);
672 + ((JLabel) progressPanel.getComponent(0))
676 progressPanel.add(cancel, BorderLayout.EAST);
682 * @return true if any progress bars are still active
685 public boolean operationInProgress()
687 if (progressBars != null && progressBars.size() > 0)
695 protected void resetButton_actionPerformed(ActionEvent e)
698 top = 0; // ugly - prevents dimensionChanged events from being processed
699 xCombobox.setSelectedIndex(0);
700 yCombobox.setSelectedIndex(1);
702 zCombobox.setSelectedIndex(2);