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.GPaSiMapPanel;
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;
52 import java.beans.PropertyChangeEvent;
53 import java.beans.PropertyChangeListener;
55 import javax.swing.ButtonGroup;
56 import javax.swing.JMenuItem;
57 import javax.swing.JProgressBar;
58 import javax.swing.JRadioButtonMenuItem;
59 import javax.swing.event.InternalFrameAdapter;
60 import javax.swing.event.InternalFrameEvent;
63 * The panel holding the Pairwise Similarity Map 3-D visualisation
65 public class PaSiMapPanel extends GPaSiMapPanel
66 implements Runnable, IProgressIndicator
68 private static final int MIN_WIDTH = 470;
70 private static final int MIN_HEIGHT = 250;
72 private final int GAP_OPEN_COST = 100;
74 private final int GAP_EXTEND_COST = 5;
76 private RotatableCanvas rc;
82 private PaSiMapModel pasimapModel;
86 private IProgressIndicator progressBar;
90 private boolean working;
93 * Constructor given sequence data, a similarity (or distance) score model
94 * name, and score calculation parameters
100 public PaSiMapPanel(AlignmentPanel alignPanel, String modelName,
101 SimilarityParamsI params)
104 this.av = alignPanel.av;
105 this.ap = alignPanel;
106 boolean nucleotide = av.getAlignment().isNucleotide();
108 //progressBar = new ProgressBar(statusPanel, statusBar);
110 addInternalFrameListener(new InternalFrameAdapter()
113 public void internalFrameClosed(InternalFrameEvent e)
115 close_actionPerformed();
119 boolean selected = av.getSelectionGroup() != null
120 && av.getSelectionGroup().getSize() > 0;
124 seqs = av.getAlignment().getSequencesArray();
128 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
131 ScoreModelI scoreModel = ScoreModels.getInstance()
132 .getScoreModel(modelName, ap);
134 new PaSiMapModel(av, seqs, nucleotide, scoreModel));
135 PaintRefresher.Register(this, av.getSequenceSetId());
137 setRotatableCanvas(new RotatableCanvas(alignPanel));
138 this.getContentPane().add(getRotatableCanvas(), BorderLayout.CENTER);
140 addKeyListener(getRotatableCanvas());
145 * Ensure references to potentially very large objects (the PaSiMap matrices) are
146 * nulled when the frame is closed
148 protected void close_actionPerformed()
150 setPasimapModel(null);
153 this.rc.sequencePoints = null;
154 this.rc.setAxisEndPoints(null);
160 protected void bgcolour_actionPerformed()
162 String ttl = MessageManager.getString("label.select_background_colour");
163 ColourChooserListener listener = new ColourChooserListener()
166 public void colourSelected(Color c)
172 JalviewColourChooser.showColourChooser(this, ttl, rc.getBgColour(),
177 * Calculates the PaSiMap and displays the results
183 progId = System.currentTimeMillis();
185 String message = MessageManager.getString("label.pasimap_recalculating");
186 if (getParent() == null)
188 progressBar = ap.alignFrame;
189 message = MessageManager.getString("label.pasimap_calculating");
191 progressBar.setProgressBar(message, progId);
195 for (SequenceI seq : av.getAlignment().getSequencesArray())
197 if (seq.getLength() > 20000)
199 //TODO add warning dialog
200 av.getAlignment().deleteSequence(seq);
204 PairwiseAlignPanel pap = new PairwiseAlignPanel(av, true, GAP_OPEN_COST, GAP_EXTEND_COST, false);
205 System.out.println(pap != null);
206 setPairwiseAlignPanel(pap);
207 getPasimapModel().calculate(pap);
209 xCombobox.setSelectedIndex(0);
210 yCombobox.setSelectedIndex(1);
211 zCombobox.setSelectedIndex(2);
213 getPasimapModel().updateRc(getRotatableCanvas());
215 setTop(getPasimapModel().getTop());
217 } catch (OutOfMemoryError er)
219 new OOMWarning("calculating PaSiMap", er);
224 progressBar.setProgressBar("", progId);
228 if (getParent() == null)
230 Desktop.addInternalFrame(this,
231 MessageManager.formatMessage("label.calc_title", "PaSiMap",
232 getPasimapModel().getScoreModelName()),
234 this.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
240 * Updates the PaSiMap display after a change of component to use for x, y or z
244 protected void doDimensionChange()
251 int dim1 = getTop() - xCombobox.getSelectedIndex();
252 int dim2 = getTop() - yCombobox.getSelectedIndex();
253 int dim3 = getTop() - zCombobox.getSelectedIndex();
254 getPasimapModel().updateRcView(dim1, dim2, dim3);
255 getRotatableCanvas().resetView();
259 * Sets the selected checkbox item index for PaSiMap dimension (1, 2, 3...) for
260 * the given axis (X/Y/Z)
265 public void setSelectedDimensionIndex(int index, Axis axis)
270 xCombobox.setSelectedIndex(index);
273 yCombobox.setSelectedIndex(index);
276 zCombobox.setSelectedIndex(index);
283 protected void outputValues_actionPerformed()
285 CutAndPasteTransfer cap = new CutAndPasteTransfer();
288 cap.setText(getPasimapModel().getDetails());
289 Desktop.addInternalFrame(cap,
290 MessageManager.getString("label.pasimap_details"), 500, 500);
291 } catch (OutOfMemoryError oom)
293 new OOMWarning("opening PaSiMap details", oom);
299 protected void showLabels_actionPerformed()
301 getRotatableCanvas().showLabels(showLabels.getState());
305 protected void print_actionPerformed()
307 PaSiMapPrinter printer = new PaSiMapPrinter();
312 * If available, shows the data which formed the inputs for the PaSiMap as a new
316 public void originalSeqData_actionPerformed()
318 // JAL-2647 disabled after load from project (until save to project done)
319 if (getPasimapModel().getInputData() == null)
322 "Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
325 // decide if av alignment is sufficiently different to original data to
326 // warrant a new window to be created
327 // create new alignment window with hidden regions (unhiding hidden regions
328 // yields unaligned seqs)
329 // or create a selection box around columns in alignment view
330 // test Alignment(SeqCigar[])
334 // we try to get the associated view's gap character
335 // but this may fail if the view was closed...
336 gc = av.getGapCharacter();
337 } catch (Exception ex)
341 Object[] alAndColsel = getPasimapModel().getInputData()
342 .getAlignmentView(false).getAlignmentAndHiddenColumns(gc);
344 if (alAndColsel != null && alAndColsel[0] != null)
346 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
348 AlignmentI al = new Alignment((SequenceI[]) alAndColsel[0]);
349 AlignmentI dataset = (av != null && av.getAlignment() != null)
350 ? av.getAlignment().getDataset()
354 al.setDataset(dataset);
360 AlignFrame af = new AlignFrame(al, (HiddenColumns) alAndColsel[1],
361 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
363 // >>>This is a fix for the moment, until a better solution is
365 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
367 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
370 Desktop.addInternalFrame(af, MessageManager.formatMessage(
371 "label.original_data_for_params", new String[]
372 { this.title }), AlignFrame.DEFAULT_WIDTH,
373 AlignFrame.DEFAULT_HEIGHT);
377 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
378 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
379 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
382 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
386 class PaSiMapPrinter extends Thread implements Printable
391 PrinterJob printJob = PrinterJob.getPrinterJob();
392 PageFormat defaultPage = printJob.defaultPage();
393 PageFormat pf = printJob.pageDialog(defaultPage);
395 if (defaultPage == pf)
403 printJob.setPrintable(this, pf);
405 if (printJob.printDialog())
410 } catch (Exception PrintException)
412 PrintException.printStackTrace();
418 public int print(Graphics pg, PageFormat pf, int pi)
419 throws PrinterException
421 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
423 getRotatableCanvas().drawBackground(pg);
424 getRotatableCanvas().drawScene(pg);
425 if (getRotatableCanvas().drawAxes)
427 getRotatableCanvas().drawAxes(pg);
432 return Printable.PAGE_EXISTS;
436 return Printable.NO_SUCH_PAGE;
441 public void makePaSiMapImage(ImageMaker.TYPE type) throws Exception
443 int width = getRotatableCanvas().getWidth();
444 int height = getRotatableCanvas().getHeight();
445 ImageWriterI writer = new ImageWriterI()
448 public void exportImage(Graphics g) throws Exception
450 RotatableCanvas canvas = getRotatableCanvas();
451 canvas.drawBackground(g);
459 String pasimap = MessageManager.getString("label.pasimap");
460 ImageExporter exporter = new ImageExporter(writer, null, type, pasimap);
461 exporter.doExport(null, this, width, height, pasimap);
465 protected void viewMenu_menuSelected()
467 buildAssociatedViewMenu();
471 * Builds the menu showing the choice of possible views (for the associated
472 * sequence data) to which the PaSiMap may be linked
474 void buildAssociatedViewMenu()
476 AlignmentPanel[] aps = PaintRefresher
477 .getAssociatedPanels(av.getSequenceSetId());
478 if (aps.length == 1 && getRotatableCanvas().av == aps[0].av)
480 associateViewsMenu.setVisible(false);
484 associateViewsMenu.setVisible(true);
487 .getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
489 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
492 associateViewsMenu.removeAll();
494 JRadioButtonMenuItem item;
495 ButtonGroup buttonGroup = new ButtonGroup();
496 int iSize = aps.length;
498 for (int i = 0; i < iSize; i++)
500 final AlignmentPanel panel = aps[i];
501 item = new JRadioButtonMenuItem(panel.av.getViewName(),
502 panel.av == getRotatableCanvas().av);
503 buttonGroup.add(item);
504 item.addActionListener(new ActionListener()
507 public void actionPerformed(ActionEvent evt)
509 selectAssociatedView(panel);
513 associateViewsMenu.add(item);
516 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem(
519 buttonGroup.add(itemf);
521 itemf.setSelected(getRotatableCanvas().isApplyToAllViews());
522 itemf.addActionListener(new ActionListener()
525 public void actionPerformed(ActionEvent evt)
527 getRotatableCanvas().setApplyToAllViews(itemf.isSelected());
530 associateViewsMenu.add(itemf);
538 * jalview.jbgui.GPaSiMapPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
542 protected void outputPoints_actionPerformed()
544 CutAndPasteTransfer cap = new CutAndPasteTransfer();
547 cap.setText(getPasimapModel().getPointsasCsv(false,
548 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
549 zCombobox.getSelectedIndex()));
550 Desktop.addInternalFrame(cap, MessageManager
551 .formatMessage("label.points_for_params", new String[]
552 { this.getTitle() }), 500, 500);
553 } catch (OutOfMemoryError oom)
555 new OOMWarning("exporting PaSiMap points", oom);
564 * jalview.jbgui.GPaSiMapPanel#outputProjPoints_actionPerformed(java.awt.event
568 protected void outputProjPoints_actionPerformed()
570 CutAndPasteTransfer cap = new CutAndPasteTransfer();
573 cap.setText(getPasimapModel().getPointsasCsv(true,
574 xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
575 zCombobox.getSelectedIndex()));
576 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
577 "label.transformed_points_for_params", new String[]
578 { this.getTitle() }), 500, 500);
579 } catch (OutOfMemoryError oom)
581 new OOMWarning("exporting transformed PaSiMap points", oom);
590 * jalview.jbgui.GPaSiMapPanel#outputAlignment_actionPerformed(java.awt.event
594 protected void outputAlignment_actionPerformed()
596 CutAndPasteTransfer cap = new CutAndPasteTransfer();
599 cap.setText(getPasimapModel().getAlignmentOutput());
600 Desktop.addInternalFrame(cap, MessageManager.formatMessage(
601 "label.pairwise_alignment_for_params", new String[] { this.getTitle() }), 500, 500);
602 } catch (OutOfMemoryError oom)
604 new OOMWarning("exporting pairwise alignments", oom);
612 * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
615 public void setProgressBar(String message, long id)
617 progressBar.setProgressBar(message, id);
618 // if (progressBars == null)
620 // progressBars = new Hashtable();
621 // progressBarHandlers = new Hashtable();
624 // JPanel progressPanel;
625 // Long lId = Long.valueOf(id);
626 // GridLayout layout = (GridLayout) statusPanel.getLayout();
627 // if (progressBars.get(lId) != null)
629 // progressPanel = (JPanel) progressBars.get(Long.valueOf(id));
630 // statusPanel.remove(progressPanel);
631 // progressBars.remove(lId);
632 // progressPanel = null;
633 // if (message != null)
635 // statusBar.setText(message);
637 // if (progressBarHandlers.contains(lId))
639 // progressBarHandlers.remove(lId);
641 // layout.setRows(layout.getRows() - 1);
645 // progressPanel = new JPanel(new BorderLayout(10, 5));
647 // JProgressBar progressBar = new JProgressBar();
648 // progressBar.setIndeterminate(true);
650 // progressPanel.add(new JLabel(message), BorderLayout.WEST);
651 // progressPanel.add(progressBar, BorderLayout.CENTER);
653 // layout.setRows(layout.getRows() + 1);
654 // statusPanel.add(progressPanel);
656 // progressBars.put(lId, progressPanel);
659 // // setMenusForViewport();
664 * make the progressBar determinate and update its progress
666 public void updateProgressBar(int lengthOfTask, int progress)
668 JProgressBar pBar = progressBar.getProgressBar(progId);
669 if (pBar.isIndeterminate())
671 pBar.setMaximum(lengthOfTask);
673 pBar.setIndeterminate(false);
675 updateProgressBar(progress);
677 public void updateProgressBar(int progress)
679 JProgressBar pBar = progressBar.getProgressBar(progId);
680 pBar.setValue(progress);
685 public void setPairwiseAlignPanel(PairwiseAlignPanel pap)
687 pap.addPropertyChangeListener(new PropertyChangeListener()
690 public void propertyChange(PropertyChangeEvent pcEvent)
692 if (PairwiseAlignPanel.PROGRESS.equals(pcEvent.getPropertyName()))
694 updateProgressBar((int) pcEvent.getNewValue());
695 } else if (PairwiseAlignPanel.TOTAL.equals(pcEvent.getPropertyName())) {
696 updateProgressBar((int) pcEvent.getNewValue(), 0);
703 public void registerHandler(final long id,
704 final IProgressIndicatorHandler handler)
706 progressBar.registerHandler(id, handler);
707 // if (progressBarHandlers == null ||
708 // !progressBars.contains(Long.valueOf(id)))
711 // Error(MessageManager.getString("error.call_setprogressbar_before_registering_handler"));
713 // progressBarHandlers.put(Long.valueOf(id), handler);
714 // final JPanel progressPanel = (JPanel) progressBars.get(Long.valueOf(id));
715 // if (handler.canCancel())
717 // JButton cancel = new JButton(
718 // MessageManager.getString("action.cancel"));
719 // final IProgressIndicator us = this;
720 // cancel.addActionListener(new ActionListener()
724 // public void actionPerformed(ActionEvent e)
726 // handler.cancelActivity(id);
727 // us.setProgressBar(MessageManager.formatMessage("label.cancelled_params",
728 // new String[]{((JLabel) progressPanel.getComponent(0)).getText()}), id);
731 // progressPanel.add(cancel, BorderLayout.EAST);
737 * @return true if any progress bars are still active
740 public boolean operationInProgress()
742 return progressBar.operationInProgress();
746 protected void resetButton_actionPerformed()
749 setTop(0); // ugly - prevents dimensionChanged events from being processed
750 xCombobox.setSelectedIndex(0);
751 yCombobox.setSelectedIndex(1);
753 zCombobox.setSelectedIndex(2);
757 * Answers true if PaSiMap calculation is in progress, else false
761 public boolean isWorking()
767 * Answers the selected checkbox item index for PaSiMap dimension for the X, Y or
768 * Z axis of the display
773 public int getSelectedDimensionIndex(Axis axis)
778 return xCombobox.getSelectedIndex();
780 return yCombobox.getSelectedIndex();
782 return zCombobox.getSelectedIndex();
786 public void setShowLabels(boolean show)
788 showLabels.setSelected(show);
792 * Sets the input data used to calculate the PaSiMap. This is provided for
793 * 'restore from project', which does not currently support this (AL-2647), so
794 * sets the value to null, and hides the menu option for "Input Data...". J
798 public void setInputData(AlignmentViewport data)
800 getPasimapModel().setInputData(data);
801 originalSeqData.setVisible(data != null);
804 public AlignViewportI getAlignViewport()
809 public PaSiMapModel getPasimapModel()
814 public void setPasimapModel(PaSiMapModel pasimapModel)
816 this.pasimapModel = pasimapModel;
819 public RotatableCanvas getRotatableCanvas()
824 public void setRotatableCanvas(RotatableCanvas rc)
834 public void setTop(int top)
840 * set the associated view for this PaSiMap.
844 public void selectAssociatedView(AlignmentPanel panel)
846 getRotatableCanvas().setApplyToAllViews(false);
851 getRotatableCanvas().av = panel.av;
852 getRotatableCanvas().ap = panel;
853 PaintRefresher.Register(PaSiMapPanel.this, panel.av.getSequenceSetId());
857 public JProgressBar getProgressBar(long id)
859 return progressBar.getProgressBar(id);