X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FPCAPanel.java;h=57a14a0cc38e01ca3047bb47bc318c475c981458;hb=17e77c3f2949a0729322b4a8d907f3f34b6a9914;hp=084311fa6b8942e46247dd52abeb2d55249ad660;hpb=153dd62dc91da13ae732600e6ea55ddbe15eab39;p=jalview.git diff --git a/src/jalview/gui/PCAPanel.java b/src/jalview/gui/PCAPanel.java old mode 100755 new mode 100644 index 084311f..57a14a0 --- a/src/jalview/gui/PCAPanel.java +++ b/src/jalview/gui/PCAPanel.java @@ -1,32 +1,52 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6) - * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9) + * Copyright (C) 2015 The Jalview Authors * * This file is part of Jalview. * * Jalview is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * * Jalview is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with Jalview. If not, see . + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.gui; -import java.util.*; - -import java.awt.*; -import java.awt.event.*; -import java.awt.print.*; -import javax.swing.*; - -import jalview.analysis.*; -import jalview.datamodel.*; -import jalview.jbgui.*; +import jalview.datamodel.Alignment; +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; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.print.PageFormat; +import java.awt.print.Printable; +import java.awt.print.PrinterException; +import java.awt.print.PrinterJob; + +import javax.swing.ButtonGroup; +import javax.swing.JCheckBoxMenuItem; +import javax.swing.JColorChooser; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.JRadioButtonMenuItem; /** * DOCUMENT ME! @@ -34,22 +54,21 @@ import jalview.jbgui.*; * @author $author$ * @version $Revision$ */ -public class PCAPanel extends GPCAPanel implements Runnable +public class PCAPanel extends GPCAPanel implements Runnable, + IProgressIndicator { - PCA pca; - - int top; + private IProgressIndicator progressBar; RotatableCanvas rc; AlignmentPanel ap; - AlignViewport av; + AlignmentViewport av; - AlignmentView seqstrings; + PCAModel pcaModel; - SequenceI[] seqs; + int top = 0; /** * Creates a new PCAPanel object. @@ -64,16 +83,21 @@ public class PCAPanel extends GPCAPanel implements Runnable this.av = ap.av; this.ap = ap; - boolean sameLength = true; + progressBar = new ProgressBar(statusPanel, statusBar); - seqstrings = av.getAlignmentView(av.getSelectionGroup() != null); - if (av.getSelectionGroup() == null) + boolean sameLength = true; + boolean selected = av.getSelectionGroup() != null + && av.getSelectionGroup().getSize() > 0; + AlignmentView seqstrings = av.getAlignmentView(selected); + boolean nucleotide = av.getAlignment().isNucleotide(); + SequenceI[] seqs; + if (!selected) { - seqs = av.alignment.getSequencesArray(); + seqs = av.getAlignment().getSequencesArray(); } else { - seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment); + seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment()); } SeqCigar sq[] = seqstrings.getSequences(); int length = sq[0].getWidth(); @@ -89,21 +113,14 @@ public class PCAPanel extends GPCAPanel implements Runnable if (!sameLength) { - JOptionPane - .showMessageDialog( - Desktop.desktop, - "The sequences must be aligned before calculating PCA.\n" - + "Try using the Pad function in the edit menu,\n" - + "or one of the multiple sequence alignment web services.", - "Sequences not aligned", JOptionPane.WARNING_MESSAGE); + JOptionPane.showMessageDialog(Desktop.desktop, + MessageManager.getString("label.pca_sequences_not_aligned"), + MessageManager.getString("label.sequences_not_aligned"), + JOptionPane.WARNING_MESSAGE); return; } - - Desktop - .addInternalFrame(this, "Principal component analysis", 400, - 400); - + pcaModel = new PCAModel(seqstrings, seqs, nucleotide); PaintRefresher.Register(this, av.getSequenceSetId()); rc = new RotatableCanvas(ap); @@ -112,9 +129,48 @@ public class PCAPanel extends GPCAPanel implements Runnable worker.start(); } + @Override + protected void scoreMatrix_menuSelected() + { + scoreMatrixMenu.removeAll(); + for (final String sm : ResidueProperties.scoreMatrices.keySet()) + { + if (ResidueProperties.getScoreMatrix(sm) != null) + { + // 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()) + { + final PCAPanel us = this; + jm.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + if (!pcaModel.getScore_matrix().equals(sm)) + { + pcaModel.setScore_matrix(sm); + Thread worker = new Thread(us); + worker.start(); + } + } + }); + scoreMatrixMenu.add(jm); + } + } + } + } + public void bgcolour_actionPerformed(ActionEvent e) { - Color col = JColorChooser.showDialog(this, "Select Background Colour", + Color col = JColorChooser.showDialog(this, + MessageManager.getString("label.select_backgroud_colour"), rc.bgColour); if (col != null) @@ -129,56 +185,81 @@ public class PCAPanel extends GPCAPanel implements Runnable */ public void run() { + long progId = System.currentTimeMillis(); + IProgressIndicator progress = this; + String message = MessageManager.getString("label.pca_recalculating"); + if (getParent() == null) + { + progress = ap.alignFrame; + message = MessageManager.getString("label.pca_calculating"); + } + progress.setProgressBar(message, progId); try { - pca = new PCA(seqstrings.getSequenceStrings(' ')); - pca.run(); - - // Now find the component coordinates - int ii = 0; - - while ((ii < seqs.length) && (seqs[ii] != null)) - { - ii++; - } - - double[][] comps = new double[ii][ii]; - - for (int i = 0; i < ii; i++) - { - if (pca.getEigenvalue(i) > 1e-4) - { - comps[i] = pca.component(i); - } - } - + calcSettings.setEnabled(false); + pcaModel.run(); // //////////////// xCombobox.setSelectedIndex(0); yCombobox.setSelectedIndex(1); zCombobox.setSelectedIndex(2); - top = pca.getM().rows - 1; + pcaModel.updateRc(rc); + // rc.invalidate(); + nuclSetting.setSelected(pcaModel.isNucleotide()); + protSetting.setSelected(!pcaModel.isNucleotide()); + jvVersionSetting.setSelected(pcaModel.isJvCalcMode()); + top = pcaModel.getTop(); - Vector points = new Vector(); - float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100); + } catch (OutOfMemoryError er) + { + new OOMWarning("calculating PCA", er); + return; + } finally + { + progress.setProgressBar("", progId); + } + calcSettings.setEnabled(true); + repaint(); + if (getParent() == null) + { + addKeyListener(rc); + Desktop.addInternalFrame(this, MessageManager + .getString("label.principal_component_analysis"), 475, 450); + } + } - for (int i = 0; i < pca.getM().rows; i++) - { - SequencePoint sp = new SequencePoint(seqs[i], scores[i]); - points.addElement(sp); - } + @Override + protected void nuclSetting_actionPerfomed(ActionEvent arg0) + { + if (!pcaModel.isNucleotide()) + { + pcaModel.setNucleotide(true); + pcaModel.setScore_matrix("DNA"); + Thread worker = new Thread(this); + worker.start(); + } - rc.setPoints(points, pca.getM().rows); - rc.repaint(); + } - addKeyListener(rc); + @Override + protected void protSetting_actionPerfomed(ActionEvent arg0) + { - } catch (OutOfMemoryError er) + if (pcaModel.isNucleotide()) { - new OOMWarning("calculating PCA", er); - + pcaModel.setNucleotide(false); + pcaModel.setScore_matrix("BLOSUM62"); + Thread worker = new Thread(this); + worker.start(); } + } + @Override + protected void jvVersionSetting_actionPerfomed(ActionEvent arg0) + { + pcaModel.setJvCalcMode(jvVersionSetting.isSelected()); + Thread worker = new Thread(this); + worker.start(); } /** @@ -194,14 +275,7 @@ public class PCAPanel extends GPCAPanel implements Runnable int dim1 = top - xCombobox.getSelectedIndex(); int dim2 = top - yCombobox.getSelectedIndex(); int dim3 = top - zCombobox.getSelectedIndex(); - - float[][] scores = pca.getComponents(dim1, dim2, dim3, 100); - - for (int i = 0; i < pca.getM().rows; i++) - { - ((SequencePoint) rc.points.elementAt(i)).coord = scores[i]; - } - + pcaModel.updateRcView(dim1, dim2, dim3); rc.img = null; rc.rotmat.setIdentity(); rc.initAxes(); @@ -244,12 +318,14 @@ public class PCAPanel extends GPCAPanel implements Runnable public void outputValues_actionPerformed(ActionEvent e) { CutAndPasteTransfer cap = new CutAndPasteTransfer(); - try { - cap.setText(pca.getDetails()); - Desktop.addInternalFrame(cap, "PCA details", 500, 500); + try + { + cap.setText(pcaModel.getDetails()); + Desktop.addInternalFrame(cap, + MessageManager.getString("label.pca_details"), 500, 500); } catch (OutOfMemoryError oom) { - new OOMWarning("opening PCA details",oom); + new OOMWarning("opening PCA details", oom); cap.dispose(); } } @@ -269,7 +345,7 @@ public class PCAPanel extends GPCAPanel implements Runnable { // this was cut'n'pasted from the equivalent TreePanel method - we should // make this an abstract function of all jalview analysis windows - if (seqstrings == null) + if (pcaModel.getSeqtrings() == null) { jalview.bin.Cache.log .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action."); @@ -291,7 +367,8 @@ public class PCAPanel extends GPCAPanel implements Runnable { } ; - Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(gc); + Object[] alAndColsel = pcaModel.getSeqtrings() + .getAlignmentAndColumnSelection(gc); if (alAndColsel != null && alAndColsel[0] != null) { @@ -319,8 +396,10 @@ public class PCAPanel extends GPCAPanel implements Runnable // af.addSortByOrderMenuItem(ServiceName + " Ordering", // msaorder); - Desktop.addInternalFrame(af, "Original Data for " + this.title, - AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT); + Desktop.addInternalFrame(af, MessageManager.formatMessage( + "label.original_data_for_params", + new String[] { this.title }), AlignFrame.DEFAULT_WIDTH, + AlignFrame.DEFAULT_HEIGHT); } } /* @@ -385,7 +464,7 @@ public class PCAPanel extends GPCAPanel implements Runnable */ public void eps_actionPerformed(ActionEvent e) { - makePCAImage(jalview.util.ImageMaker.EPS); + makePCAImage(jalview.util.ImageMaker.TYPE.EPS); } /** @@ -396,26 +475,34 @@ public class PCAPanel extends GPCAPanel implements Runnable */ public void png_actionPerformed(ActionEvent e) { - makePCAImage(jalview.util.ImageMaker.PNG); + makePCAImage(jalview.util.ImageMaker.TYPE.PNG); } - void makePCAImage(int type) + void makePCAImage(jalview.util.ImageMaker.TYPE type) { int width = rc.getWidth(); int height = rc.getHeight(); jalview.util.ImageMaker im; - if (type == jalview.util.ImageMaker.PNG) + if (type == jalview.util.ImageMaker.TYPE.PNG) { - im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG, - "Make PNG image from PCA", width, height, null, null); + im = new jalview.util.ImageMaker(this, + jalview.util.ImageMaker.TYPE.PNG, "Make PNG image from PCA", + width, height, null, null); + } + 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()); } else { - im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS, - "Make EPS file from PCA", width, height, null, this - .getTitle()); + im = new jalview.util.ImageMaker(this, + jalview.util.ImageMaker.TYPE.SVG, "Make SVG file from PCA", + width, height, null, this.getTitle()); + } if (im.getGraphics() != null) @@ -492,80 +579,161 @@ public class PCAPanel extends GPCAPanel implements Runnable associateViewsMenu.add(itemf); } - /* (non-Javadoc) - * @see jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent) + + /* + * (non-Javadoc) + * + * @see + * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent + * ) */ protected void outputPoints_actionPerformed(ActionEvent e) { CutAndPasteTransfer cap = new CutAndPasteTransfer(); - try { - cap.setText(getPointsasCsv(false)); - Desktop.addInternalFrame(cap, "Points for "+getTitle(), 500, 500); + try + { + cap.setText(pcaModel.getPointsasCsv(false, + xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(), + zCombobox.getSelectedIndex())); + Desktop.addInternalFrame(cap, MessageManager.formatMessage( + "label.points_for_params", new String[] { this.getTitle() }), + 500, 500); } catch (OutOfMemoryError oom) { - new OOMWarning("exporting PCA points",oom); + new OOMWarning("exporting PCA points", oom); cap.dispose(); } } - private String getPointsasCsv(boolean transformed) - { - StringBuffer csv = new StringBuffer(); - csv.append("\"Sequence\""); - if (transformed) { - csv.append(","); - csv.append(xCombobox.getSelectedIndex()); - csv.append(","); - csv.append(yCombobox.getSelectedIndex()); - csv.append(","); - csv.append(zCombobox.getSelectedIndex()); - } else { - for (int d=1,dmax=pca.component(1).length;d<=dmax;d++) - { - csv.append(","+d); - } - } - csv.append("\n"); - for (int s=0;s=0;d--) - { - csv.append(","); - csv.append(fl[d]); - } - } else { - // output current x,y,z coords for points - fl = rc.getPointPosition(s); - for (int d=0; d