2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.analysis.scoremodels.ScoreModels;
24 import jalview.api.AlignViewportI;
25 import jalview.api.analysis.ScoreModelI;
26 import jalview.api.analysis.SimilarityParamsI;
27 import jalview.bin.Console;
28 import jalview.datamodel.Alignment;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.AlignmentView;
31 import jalview.datamodel.HiddenColumns;
32 import jalview.datamodel.SequenceI;
33 import jalview.gui.ImageExporter.ImageWriterI;
34 import jalview.gui.JalviewColourChooser.ColourChooserListener;
35 import jalview.jbgui.GPCAPanel;
36 import jalview.math.RotatableMatrix.Axis;
37 import jalview.util.ImageMaker;
38 import jalview.util.MessageManager;
39 import jalview.viewmodel.AlignmentViewport;
40 import jalview.viewmodel.PaSiMapModel;
42 import java.awt.BorderLayout;
43 import java.awt.Color;
44 import java.awt.Dimension;
45 import java.awt.Graphics;
46 import java.awt.event.ActionEvent;
47 import java.awt.event.ActionListener;
48 import java.awt.print.PageFormat;
49 import java.awt.print.Printable;
50 import java.awt.print.PrinterException;
51 import java.awt.print.PrinterJob;
53 import javax.swing.ButtonGroup;
54 import javax.swing.JMenuItem;
55 import javax.swing.JRadioButtonMenuItem;
56 import javax.swing.event.InternalFrameAdapter;
57 import javax.swing.event.InternalFrameEvent;
60 * The panel holding the Pairwise Similarity Map 3-D visualisation
62 public class PaSiMapPanel extends GPCAPanel
63 implements Runnable, IProgressIndicator
65 private static final int MIN_WIDTH = 470;
67 private static final int MIN_HEIGHT = 250;
69 private RotatableCanvas rc;
75 private PaSiMapModel pasimapModel;
79 private IProgressIndicator progressBar;
81 private boolean working;
84 * Constructor given sequence data, a similarity (or distance) score model
85 * name, and score calculation parameters
91 public PaSiMapPanel(AlignmentPanel alignPanel, String modelName,
92 SimilarityParamsI params)
95 this.av = alignPanel.av;
97 boolean nucleotide = av.getAlignment().isNucleotide();
99 progressBar = new ProgressBar(statusPanel, statusBar);
101 addInternalFrameListener(new InternalFrameAdapter()
104 public void internalFrameClosed(InternalFrameEvent e)
106 close_actionPerformed();
110 boolean selected = av.getSelectionGroup() != null
111 && av.getSelectionGroup().getSize() > 0;
112 //&! do i need seqstrings?
113 AlignmentView seqstrings = av.getAlignmentView(selected);
117 seqs = av.getAlignment().getSequencesArray();
121 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
124 ScoreModelI scoreModel = ScoreModels.getInstance()
125 .getScoreModel(modelName, ap);
128 //new PaSiMapModel(seqstrings, seqs, nucleotide, scoreModel, params));
129 new PaSiMapModel(av, seqs, nucleotide, scoreModel, params));
130 PaintRefresher.Register(this, av.getSequenceSetId());
132 setRotatableCanvas(new RotatableCanvas(alignPanel));
133 this.getContentPane().add(getRotatableCanvas(), BorderLayout.CENTER);
135 addKeyListener(getRotatableCanvas());
140 * Ensure references to potentially very large objects (the PaSiMap matrices) are
141 * nulled when the frame is closed
143 protected void close_actionPerformed()
145 setPasimapModel(null);
148 this.rc.sequencePoints = null;
149 this.rc.setAxisEndPoints(null);
155 protected void bgcolour_actionPerformed()
157 String ttl = MessageManager.getString("label.select_background_colour");
158 ColourChooserListener listener = new ColourChooserListener()
161 public void colourSelected(Color c)
167 JalviewColourChooser.showColourChooser(this, ttl, rc.getBgColour(),
172 * Calculates the PaSiMap and displays the results
178 long progId = System.currentTimeMillis();
179 IProgressIndicator progress = this;
180 String message = MessageManager.getString("label.pasimap_recalculating");
181 if (getParent() == null)
183 progress = ap.alignFrame;
184 message = MessageManager.getString("label.pasimap_calculating");
186 progress.setProgressBar(message, progId);
189 getPasimapModel().calculate();
191 xCombobox.setSelectedIndex(0);
192 yCombobox.setSelectedIndex(1);
193 zCombobox.setSelectedIndex(2);
195 getPasimapModel().updateRc(getRotatableCanvas());
197 setTop(getPasimapModel().getTop());
199 } catch (OutOfMemoryError er)
201 new OOMWarning("calculating PaSiMap", er);
206 progress.setProgressBar("", progId);
210 if (getParent() == null)
212 Desktop.addInternalFrame(this,
213 MessageManager.formatMessage("label.calc_title", "PaSiMap",
214 getPasimapModel().getScoreModelName()),
216 this.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
222 * Updates the PaSiMap display after a change of component to use for x, y or z
226 protected void doDimensionChange()
233 int dim1 = getTop() - xCombobox.getSelectedIndex();
234 int dim2 = getTop() - yCombobox.getSelectedIndex();
235 int dim3 = getTop() - zCombobox.getSelectedIndex();
236 getPasimapModel().updateRcView(dim1, dim2, dim3);
237 getRotatableCanvas().resetView();
241 * Sets the selected checkbox item index for PaSiMap dimension (1, 2, 3...) for
242 * the given axis (X/Y/Z)
247 public void setSelectedDimensionIndex(int index, Axis axis)
252 xCombobox.setSelectedIndex(index);
255 yCombobox.setSelectedIndex(index);
258 zCombobox.setSelectedIndex(index);
265 protected void outputValues_actionPerformed()
267 CutAndPasteTransfer cap = new CutAndPasteTransfer();
270 cap.setText(getPasimapModel().getDetails());
271 Desktop.addInternalFrame(cap,
272 MessageManager.getString("label.pasimap_details"), 500, 500);
273 } catch (OutOfMemoryError oom)
275 new OOMWarning("opening PaSiMap details", oom);
281 protected void showLabels_actionPerformed()
283 getRotatableCanvas().showLabels(showLabels.getState());
287 protected void print_actionPerformed()
289 PaSiMapPrinter printer = new PaSiMapPrinter();
294 * If available, shows the data which formed the inputs for the PaSiMap as a new
298 public void originalSeqData_actionPerformed()
300 // JAL-2647 disabled after load from project (until save to project done)
301 if (getPasimapModel().getInputData() == null)
304 "Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
307 // decide if av alignment is sufficiently different to original data to
308 // warrant a new window to be created
309 // create new alignment window with hidden regions (unhiding hidden regions
310 // yields unaligned seqs)
311 // or create a selection box around columns in alignment view
312 // test Alignment(SeqCigar[])
316 // we try to get the associated view's gap character
317 // but this may fail if the view was closed...
318 gc = av.getGapCharacter();
319 } catch (Exception ex)
323 Object[] alAndColsel = getPasimapModel().getInputData()
324 .getAlignmentView(false).getAlignmentAndHiddenColumns(gc);
326 if (alAndColsel != null && alAndColsel[0] != null)
328 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
330 AlignmentI al = new Alignment((SequenceI[]) alAndColsel[0]);
331 AlignmentI dataset = (av != null && av.getAlignment() != null)
332 ? av.getAlignment().getDataset()
336 al.setDataset(dataset);
342 AlignFrame af = new AlignFrame(al, (HiddenColumns) alAndColsel[1],
343 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
345 // >>>This is a fix for the moment, until a better solution is
347 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
349 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
352 Desktop.addInternalFrame(af, MessageManager.formatMessage(
353 "label.original_data_for_params", new String[]
354 { this.title }), AlignFrame.DEFAULT_WIDTH,
355 AlignFrame.DEFAULT_HEIGHT);
359 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
360 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
361 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
364 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
368 class PaSiMapPrinter extends Thread implements Printable
373 PrinterJob printJob = PrinterJob.getPrinterJob();
374 PageFormat defaultPage = printJob.defaultPage();
375 PageFormat pf = printJob.pageDialog(defaultPage);
377 if (defaultPage == pf)
385 printJob.setPrintable(this, pf);
387 if (printJob.printDialog())
392 } catch (Exception PrintException)
394 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 getRotatableCanvas().drawBackground(pg);
406 getRotatableCanvas().drawScene(pg);
407 if (getRotatableCanvas().drawAxes)
409 getRotatableCanvas().drawAxes(pg);
414 return Printable.PAGE_EXISTS;
418 return Printable.NO_SUCH_PAGE;
423 public void makePaSiMapImage(ImageMaker.TYPE type)
425 int width = getRotatableCanvas().getWidth();
426 int height = getRotatableCanvas().getHeight();
427 ImageWriterI writer = new ImageWriterI()
430 public void exportImage(Graphics g) throws Exception
432 RotatableCanvas canvas = getRotatableCanvas();
433 canvas.drawBackground(g);
441 String pasimap = MessageManager.getString("label.pasimap");
442 ImageExporter exporter = new ImageExporter(writer, null, type, pasimap);
443 exporter.doExport(null, this, width, height, pasimap);
447 protected void viewMenu_menuSelected()
449 buildAssociatedViewMenu();
453 * Builds the menu showing the choice of possible views (for the associated
454 * sequence data) to which the PaSiMap may be linked
456 void buildAssociatedViewMenu()
458 AlignmentPanel[] aps = PaintRefresher
459 .getAssociatedPanels(av.getSequenceSetId());
460 if (aps.length == 1 && getRotatableCanvas().av == aps[0].av)
462 associateViewsMenu.setVisible(false);
466 associateViewsMenu.setVisible(true);
469 .getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
471 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
474 associateViewsMenu.removeAll();
476 JRadioButtonMenuItem item;
477 ButtonGroup buttonGroup = new ButtonGroup();
478 int iSize = aps.length;
480 for (int i = 0; i < iSize; i++)
482 final AlignmentPanel panel = aps[i];
483 item = new JRadioButtonMenuItem(panel.av.getViewName(),
484 panel.av == getRotatableCanvas().av);
485 buttonGroup.add(item);
486 item.addActionListener(new ActionListener()
489 public void actionPerformed(ActionEvent evt)
491 selectAssociatedView(panel);
495 associateViewsMenu.add(item);
498 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem(
501 buttonGroup.add(itemf);
503 itemf.setSelected(getRotatableCanvas().isApplyToAllViews());
504 itemf.addActionListener(new ActionListener()
507 public void actionPerformed(ActionEvent evt)
509 getRotatableCanvas().setApplyToAllViews(itemf.isSelected());
512 associateViewsMenu.add(itemf);
520 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
524 protected void outputPoints_actionPerformed()
526 CutAndPasteTransfer cap = new CutAndPasteTransfer();
529 cap.setText(getPasimapModel().getPointsasCsv(false,
530 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
531 zCombobox.getSelectedIndex()));
532 Desktop.addInternalFrame(cap, MessageManager
533 .formatMessage("label.points_for_params", new String[]
534 { this.getTitle() }), 500, 500);
535 } catch (OutOfMemoryError oom)
537 new OOMWarning("exporting PaSiMap points", oom);
546 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
550 protected void outputProjPoints_actionPerformed()
552 CutAndPasteTransfer cap = new CutAndPasteTransfer();
555 cap.setText(getPasimapModel().getPointsasCsv(true,
556 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
557 zCombobox.getSelectedIndex()));
558 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
559 "label.transformed_points_for_params", new String[]
560 { this.getTitle() }), 500, 500);
561 } catch (OutOfMemoryError oom)
563 new OOMWarning("exporting transformed PaSiMap points", oom);
571 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
574 public void setProgressBar(String message, long id)
576 progressBar.setProgressBar(message, id);
577 // if (progressBars == null)
579 // progressBars = new Hashtable();
580 // progressBarHandlers = new Hashtable();
583 // JPanel progressPanel;
584 // Long lId = Long.valueOf(id);
585 // GridLayout layout = (GridLayout) statusPanel.getLayout();
586 // if (progressBars.get(lId) != null)
588 // progressPanel = (JPanel) progressBars.get(Long.valueOf(id));
589 // statusPanel.remove(progressPanel);
590 // progressBars.remove(lId);
591 // progressPanel = null;
592 // if (message != null)
594 // statusBar.setText(message);
596 // if (progressBarHandlers.contains(lId))
598 // progressBarHandlers.remove(lId);
600 // layout.setRows(layout.getRows() - 1);
604 // progressPanel = new JPanel(new BorderLayout(10, 5));
606 // JProgressBar progressBar = new JProgressBar();
607 // progressBar.setIndeterminate(true);
609 // progressPanel.add(new JLabel(message), BorderLayout.WEST);
610 // progressPanel.add(progressBar, BorderLayout.CENTER);
612 // layout.setRows(layout.getRows() + 1);
613 // statusPanel.add(progressPanel);
615 // progressBars.put(lId, progressPanel);
618 // // setMenusForViewport();
623 public void registerHandler(final long id,
624 final IProgressIndicatorHandler handler)
626 progressBar.registerHandler(id, handler);
627 // if (progressBarHandlers == null ||
628 // !progressBars.contains(Long.valueOf(id)))
631 // Error(MessageManager.getString("error.call_setprogressbar_before_registering_handler"));
633 // progressBarHandlers.put(Long.valueOf(id), handler);
634 // final JPanel progressPanel = (JPanel) progressBars.get(Long.valueOf(id));
635 // if (handler.canCancel())
637 // JButton cancel = new JButton(
638 // MessageManager.getString("action.cancel"));
639 // final IProgressIndicator us = this;
640 // cancel.addActionListener(new ActionListener()
644 // public void actionPerformed(ActionEvent e)
646 // handler.cancelActivity(id);
647 // us.setProgressBar(MessageManager.formatMessage("label.cancelled_params",
648 // new String[]{((JLabel) progressPanel.getComponent(0)).getText()}), id);
651 // progressPanel.add(cancel, BorderLayout.EAST);
657 * @return true if any progress bars are still active
660 public boolean operationInProgress()
662 return progressBar.operationInProgress();
666 protected void resetButton_actionPerformed()
669 setTop(0); // ugly - prevents dimensionChanged events from being processed
670 xCombobox.setSelectedIndex(0);
671 yCombobox.setSelectedIndex(1);
673 zCombobox.setSelectedIndex(2);
677 * Answers true if PaSiMap calculation is in progress, else false
681 public boolean isWorking()
687 * Answers the selected checkbox item index for PaSiMap dimension for the X, Y or
688 * Z axis of the display
693 public int getSelectedDimensionIndex(Axis axis)
698 return xCombobox.getSelectedIndex();
700 return yCombobox.getSelectedIndex();
702 return zCombobox.getSelectedIndex();
706 public void setShowLabels(boolean show)
708 showLabels.setSelected(show);
712 * Sets the input data used to calculate the PaSiMap. This is provided for
713 * 'restore from project', which does not currently support this (AL-2647), so
714 * sets the value to null, and hides the menu option for "Input Data...". J
718 //public void setInputData(AlignmentView data)
719 //&! viewport or panel?
720 public void setInputData(AlignmentViewport data)
722 getPasimapModel().setInputData(data);
723 originalSeqData.setVisible(data != null);
726 public AlignViewportI getAlignViewport()
731 public PaSiMapModel getPasimapModel()
736 public void setPasimapModel(PaSiMapModel pasimapModel)
738 this.pasimapModel = pasimapModel;
741 public RotatableCanvas getRotatableCanvas()
746 public void setRotatableCanvas(RotatableCanvas rc)
756 public void setTop(int top)
762 * set the associated view for this PaSiMap.
766 public void selectAssociatedView(AlignmentPanel panel)
768 getRotatableCanvas().setApplyToAllViews(false);
773 getRotatableCanvas().av = panel.av;
774 getRotatableCanvas().ap = panel;
775 PaintRefresher.Register(PaSiMapPanel.this, panel.av.getSequenceSetId());