X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FPCAPanel.java;h=885c3bf8a2026a9c05418479999d03e5103bf171;hb=5c45059a31d5f2ed14a8898d700ead9b3431bccf;hp=5ad4901eb3eaf89f0e076e736a8ae2bcd1fb98aa;hpb=838e4f91d4a53dd315640dbc9ff6ef7a815ee576;p=jalview.git diff --git a/src/jalview/gui/PCAPanel.java b/src/jalview/gui/PCAPanel.java index 5ad4901..885c3bf 100644 --- a/src/jalview/gui/PCAPanel.java +++ b/src/jalview/gui/PCAPanel.java @@ -1,6 +1,6 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b1) - * Copyright (C) 2015 The Jalview Authors + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * @@ -20,13 +20,19 @@ */ package jalview.gui; +import jalview.analysis.scoremodels.ScoreModels; +import jalview.analysis.scoremodels.SimilarityParams; +import jalview.api.analysis.ScoreModelI; +import jalview.api.analysis.SimilarityParamsI; +import jalview.api.analysis.ViewBasedAnalysisI; +import jalview.bin.Cache; import jalview.datamodel.Alignment; +import jalview.datamodel.AlignmentI; import jalview.datamodel.AlignmentView; import jalview.datamodel.ColumnSelection; import jalview.datamodel.SeqCigar; import jalview.datamodel.SequenceI; import jalview.jbgui.GPCAPanel; -import jalview.schemes.ResidueProperties; import jalview.util.MessageManager; import jalview.viewmodel.AlignmentViewport; import jalview.viewmodel.PCAModel; @@ -45,8 +51,9 @@ import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JColorChooser; import javax.swing.JMenuItem; -import javax.swing.JOptionPane; import javax.swing.JRadioButtonMenuItem; +import javax.swing.event.InternalFrameAdapter; +import javax.swing.event.InternalFrameEvent; /** * DOCUMENT ME! @@ -71,25 +78,47 @@ public class PCAPanel extends GPCAPanel implements Runnable, int top = 0; /** - * Creates a new PCAPanel object. + * Creates a new PCAPanel object using default score model and parameters * - * @param av - * DOCUMENT ME! - * @param s - * DOCUMENT ME! + * @param alignPanel + */ + public PCAPanel(AlignmentPanel alignPanel) + { + this(alignPanel, ScoreModels.getInstance().getDefaultModel( + !alignPanel.av.getAlignment().isNucleotide()), + SimilarityParams.SeqSpace); + } + + /** + * Constructor given sequence data, a similarity (or distance) score model, + * and score calculation parameters + * + * @param alignPanel + * @param scoreModel + * @param params */ - public PCAPanel(AlignmentPanel ap) + public PCAPanel(AlignmentPanel alignPanel, ScoreModelI scoreModel, + SimilarityParamsI params) { - this.av = ap.av; - this.ap = ap; + super(); + this.av = alignPanel.av; + this.ap = alignPanel; + boolean nucleotide = av.getAlignment().isNucleotide(); progressBar = new ProgressBar(statusPanel, statusBar); - boolean sameLength = true; + addInternalFrameListener(new InternalFrameAdapter() + { + @Override + public void internalFrameClosed(InternalFrameEvent e) + { + close_actionPerformed(); + } + }); + boolean selected = av.getSelectionGroup() != null && av.getSelectionGroup().getSize() > 0; AlignmentView seqstrings = av.getAlignmentView(selected); - boolean nucleotide = av.getAlignment().isNucleotide(); SequenceI[] seqs; if (!selected) { @@ -99,78 +128,114 @@ public class PCAPanel extends GPCAPanel implements Runnable, { seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment()); } - SeqCigar sq[] = seqstrings.getSequences(); - int length = sq[0].getWidth(); - - for (int i = 0; i < seqs.length; i++) - { - if (sq[i].getWidth() != length) - { - sameLength = false; - break; - } - } - if (!sameLength) + // TODO can we allow PCA on unaligned data given choice of + // similarity measure parameters? + if (!checkAligned(seqstrings)) { - JOptionPane.showMessageDialog(Desktop.desktop, + JvOptionPane.showMessageDialog(Desktop.desktop, MessageManager.getString("label.pca_sequences_not_aligned"), MessageManager.getString("label.sequences_not_aligned"), - JOptionPane.WARNING_MESSAGE); + JvOptionPane.WARNING_MESSAGE); return; } - pcaModel = new PCAModel(seqstrings, seqs, nucleotide); + + pcaModel = new PCAModel(seqstrings, seqs, nucleotide, scoreModel, + params); PaintRefresher.Register(this, av.getSequenceSetId()); - rc = new RotatableCanvas(ap); + rc = new RotatableCanvas(alignPanel); this.getContentPane().add(rc, BorderLayout.CENTER); Thread worker = new Thread(this); worker.start(); } - @Override - protected void scoreMatrix_menuSelected() + /** + * Answers true if all sequences have the same aligned length, else false + * + * @param seqstrings + * @return + */ + protected boolean checkAligned(AlignmentView seqstrings) { - scoreMatrixMenu.removeAll(); - for (final String sm : ResidueProperties.scoreMatrices.keySet()) + SeqCigar sq[] = seqstrings.getSequences(); + int length = sq[0].getWidth(); + boolean sameLength = true; + for (int i = 0; i < sq.length; i++) { - if (ResidueProperties.getScoreMatrix(sm) != null) + if (sq[i].getWidth() != length) { - // create an entry for this score matrix for use in PCA - JCheckBoxMenuItem jm = new JCheckBoxMenuItem(); - jm.setText(MessageManager - .getStringOrReturn("label.score_model", sm)); - jm.setSelected(pcaModel.getScore_matrix().equals(sm)); - if ((ResidueProperties.scoreMatrices.get(sm).isDNA() && ResidueProperties.scoreMatrices - .get(sm).isProtein()) - || pcaModel.isNucleotide() == ResidueProperties.scoreMatrices - .get(sm).isDNA()) + sameLength = false; + break; + } + } + return sameLength; + } + + /** + * Ensure references to potentially very large objects (the PCA matrices) are + * nulled when the frame is closed + */ + protected void close_actionPerformed() + { + pcaModel = null; + } + + /** + * Repopulate the options and actions under the score model menu when it is + * selected. Options will depend on whether 'nucleotide' or 'peptide' + * modelling is selected (and also possibly on whether any additional score + * models have been added). + */ + @Override + protected void scoreModel_menuSelected() + { + scoreModelMenu.removeAll(); + for (final ScoreModelI sm : ScoreModels.getInstance().getModels()) + { + final String name = sm.getName(); + JCheckBoxMenuItem jm = new JCheckBoxMenuItem(name); + + /* + * if the score model doesn't provide a description, try to look one + * up in the text bundle, falling back on its name + */ + String tooltip = sm.getDescription(); + if (tooltip == null) + { + tooltip = MessageManager.getStringOrReturn("label.score_model_", + name); + } + jm.setToolTipText(tooltip); + jm.setSelected(pcaModel.getScoreModelName().equals(name)); + if ((pcaModel.isNucleotide() && sm.isDNA()) + || (!pcaModel.isNucleotide() && sm.isProtein())) + { + jm.addActionListener(new ActionListener() { - final PCAPanel us = this; - jm.addActionListener(new ActionListener() + @Override + public void actionPerformed(ActionEvent e) { - @Override - public void actionPerformed(ActionEvent e) + if (!pcaModel.getScoreModelName().equals(name)) { - if (!pcaModel.getScore_matrix().equals(sm)) - { - pcaModel.setScore_matrix(sm); - Thread worker = new Thread(us); - worker.start(); - } + ScoreModelI sm2 = configureScoreModel(sm); + pcaModel.setScoreModel(sm2); + Thread worker = new Thread(PCAPanel.this); + worker.start(); } - }); - scoreMatrixMenu.add(jm); - } + } + }); + scoreModelMenu.add(jm); } } } + @Override public void bgcolour_actionPerformed(ActionEvent e) { Color col = JColorChooser.showDialog(this, - MessageManager.getString("label.select_backgroud_colour"), + MessageManager.getString("label.select_background_colour"), rc.bgColour); if (col != null) @@ -183,6 +248,7 @@ public class PCAPanel extends GPCAPanel implements Runnable, /** * DOCUMENT ME! */ + @Override public void run() { long progId = System.currentTimeMillis(); @@ -234,7 +300,8 @@ public class PCAPanel extends GPCAPanel implements Runnable, if (!pcaModel.isNucleotide()) { pcaModel.setNucleotide(true); - pcaModel.setScore_matrix("DNA"); + pcaModel.setScoreModel(ScoreModels.getInstance().getDefaultModel( + false)); Thread worker = new Thread(this); worker.start(); } @@ -248,7 +315,8 @@ public class PCAPanel extends GPCAPanel implements Runnable, if (pcaModel.isNucleotide()) { pcaModel.setNucleotide(false); - pcaModel.setScore_matrix("BLOSUM62"); + pcaModel.setScoreModel(ScoreModels.getInstance() + .getDefaultModel(true)); Thread worker = new Thread(this); worker.start(); } @@ -288,6 +356,7 @@ public class PCAPanel extends GPCAPanel implements Runnable, * @param e * DOCUMENT ME! */ + @Override protected void xCombobox_actionPerformed(ActionEvent e) { doDimensionChange(); @@ -299,6 +368,7 @@ public class PCAPanel extends GPCAPanel implements Runnable, * @param e * DOCUMENT ME! */ + @Override protected void yCombobox_actionPerformed(ActionEvent e) { doDimensionChange(); @@ -310,11 +380,13 @@ public class PCAPanel extends GPCAPanel implements Runnable, * @param e * DOCUMENT ME! */ + @Override protected void zCombobox_actionPerformed(ActionEvent e) { doDimensionChange(); } + @Override public void outputValues_actionPerformed(ActionEvent e) { CutAndPasteTransfer cap = new CutAndPasteTransfer(); @@ -330,17 +402,20 @@ public class PCAPanel extends GPCAPanel implements Runnable, } } + @Override public void showLabels_actionPerformed(ActionEvent e) { rc.showLabels(showLabels.getState()); } + @Override public void print_actionPerformed(ActionEvent e) { PCAPrinter printer = new PCAPrinter(); printer.start(); } + @Override public void originalSeqData_actionPerformed(ActionEvent e) { // this was cut'n'pasted from the equivalent TreePanel method - we should @@ -374,8 +449,8 @@ public class PCAPanel extends GPCAPanel implements Runnable, { // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]); - Alignment al = new Alignment((SequenceI[]) alAndColsel[0]); - Alignment dataset = (av != null && av.getAlignment() != null) ? av + AlignmentI al = new Alignment((SequenceI[]) alAndColsel[0]); + AlignmentI dataset = (av != null && av.getAlignment() != null) ? av .getAlignment().getDataset() : null; if (dataset != null) { @@ -414,6 +489,7 @@ public class PCAPanel extends GPCAPanel implements Runnable, class PCAPrinter extends Thread implements Printable { + @Override public void run() { PrinterJob printJob = PrinterJob.getPrinterJob(); @@ -433,6 +509,7 @@ public class PCAPanel extends GPCAPanel implements Runnable, } } + @Override public int print(Graphics pg, PageFormat pf, int pi) throws PrinterException { @@ -462,6 +539,7 @@ public class PCAPanel extends GPCAPanel implements Runnable, * @param e * DOCUMENT ME! */ + @Override public void eps_actionPerformed(ActionEvent e) { makePCAImage(jalview.util.ImageMaker.TYPE.EPS); @@ -473,6 +551,7 @@ public class PCAPanel extends GPCAPanel implements Runnable, * @param e * DOCUMENT ME! */ + @Override public void png_actionPerformed(ActionEvent e) { makePCAImage(jalview.util.ImageMaker.TYPE.PNG); @@ -489,19 +568,19 @@ public class PCAPanel extends GPCAPanel implements Runnable, { im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.TYPE.PNG, "Make PNG image from PCA", - width, height, null, null); + width, height, null, null, null, 0, false); } else if (type == jalview.util.ImageMaker.TYPE.EPS) { im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.TYPE.EPS, "Make EPS file from PCA", - width, height, null, this.getTitle()); + width, height, null, this.getTitle(), null, 0, false); } else { im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.TYPE.SVG, "Make SVG file from PCA", - width, height, null, this.getTitle()); + width, height, null, this.getTitle(), null, 0, false); } @@ -517,6 +596,7 @@ public class PCAPanel extends GPCAPanel implements Runnable, } } + @Override public void viewMenu_menuSelected() { buildAssociatedViewMenu(); @@ -552,6 +632,7 @@ public class PCAPanel extends GPCAPanel implements Runnable, buttonGroup.add(item); item.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent evt) { rc.applyToAllViews = false; @@ -571,6 +652,7 @@ public class PCAPanel extends GPCAPanel implements Runnable, itemf.setSelected(rc.applyToAllViews); itemf.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent evt) { rc.applyToAllViews = itemf.isSelected(); @@ -587,6 +669,7 @@ public class PCAPanel extends GPCAPanel implements Runnable, * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent * ) */ + @Override protected void outputPoints_actionPerformed(ActionEvent e) { CutAndPasteTransfer cap = new CutAndPasteTransfer(); @@ -612,6 +695,7 @@ public class PCAPanel extends GPCAPanel implements Runnable, * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event * .ActionEvent) */ + @Override protected void outputProjPoints_actionPerformed(ActionEvent e) { CutAndPasteTransfer cap = new CutAndPasteTransfer(); @@ -736,4 +820,28 @@ public class PCAPanel extends GPCAPanel implements Runnable, top = t; zCombobox.setSelectedIndex(2); } + + /** + * If the score model is one that requires to get state data from the current + * view, allow it to do so + * + * @param sm + * @return + */ + protected ScoreModelI configureScoreModel(ScoreModelI sm) + { + if (sm instanceof ViewBasedAnalysisI) + { + try + { + sm = sm.getClass().newInstance(); + ((ViewBasedAnalysisI) sm).configureFromAlignmentView(ap); + } catch (Exception q) + { + Cache.log.error("Couldn't create a scoremodel instance for " + + sm.getName()); + } + } + return sm; + } }