X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FPCAPanel.java;h=f423a43935271495642a5eedea794c66ac8b15ff;hb=f6a7e5514230be7ea8c2d4cccb504b4ed95f80ef;hp=b397cf598a6bd0372c9534c6ddbd6ce6412ec6e2;hpb=506d60f0e188723ddc91c26824b41ac7034df3fe;p=jalview.git diff --git a/src/jalview/gui/PCAPanel.java b/src/jalview/gui/PCAPanel.java index b397cf5..f423a43 100755 --- a/src/jalview/gui/PCAPanel.java +++ b/src/jalview/gui/PCAPanel.java @@ -1,20 +1,19 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4) - * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8) + * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle * - * This program 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 2 - * of the License, or (at your option) any later version. + * This file is part of Jalview. * - * This program 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. + * 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. + * + * 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 this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + * You should have received a copy of the GNU General Public License along with Jalview. If not, see . */ package jalview.gui; @@ -28,6 +27,7 @@ import javax.swing.*; import jalview.analysis.*; import jalview.datamodel.*; import jalview.jbgui.*; +import jalview.viewmodel.PCAModel; /** * DOCUMENT ME! @@ -35,11 +35,9 @@ 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; RotatableCanvas rc; @@ -47,17 +45,17 @@ public class PCAPanel extends GPCAPanel implements Runnable AlignViewport av; - AlignmentView seqstrings; + PCAModel pcaModel; - SequenceI[] seqs; + int top = 0; /** * Creates a new PCAPanel object. * * @param av - * DOCUMENT ME! + * DOCUMENT ME! * @param s - * DOCUMENT ME! + * DOCUMENT ME! */ public PCAPanel(AlignmentPanel ap) { @@ -65,15 +63,18 @@ public class PCAPanel extends GPCAPanel implements Runnable this.ap = ap; boolean sameLength = true; - - seqstrings = av.getAlignmentView(av.getSelectionGroup() != null); - if (av.getSelectionGroup() == null) + 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(); @@ -99,11 +100,7 @@ public class PCAPanel extends GPCAPanel implements Runnable return; } - - Desktop - .addInternalFrame(this, "Principal component analysis", 400, - 400); - + pcaModel = new PCAModel(seqstrings, seqs, nucleotide); PaintRefresher.Register(this, av.getSequenceSetId()); rc = new RotatableCanvas(ap); @@ -129,56 +126,79 @@ public class PCAPanel extends GPCAPanel implements Runnable */ public void run() { + long progId = System.currentTimeMillis(); + IProgressIndicator progress = this; + String message = "Recalculating PCA"; + if (getParent() == null) + { + progress = ap.alignFrame; + message = "Calculating PCA"; + } + 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, "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); + 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); + 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 +214,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(); @@ -212,7 +225,7 @@ public class PCAPanel extends GPCAPanel implements Runnable * DOCUMENT ME! * * @param e - * DOCUMENT ME! + * DOCUMENT ME! */ protected void xCombobox_actionPerformed(ActionEvent e) { @@ -223,7 +236,7 @@ public class PCAPanel extends GPCAPanel implements Runnable * DOCUMENT ME! * * @param e - * DOCUMENT ME! + * DOCUMENT ME! */ protected void yCombobox_actionPerformed(ActionEvent e) { @@ -234,7 +247,7 @@ public class PCAPanel extends GPCAPanel implements Runnable * DOCUMENT ME! * * @param e - * DOCUMENT ME! + * DOCUMENT ME! */ protected void zCombobox_actionPerformed(ActionEvent e) { @@ -244,9 +257,15 @@ public class PCAPanel extends GPCAPanel implements Runnable public void outputValues_actionPerformed(ActionEvent e) { CutAndPasteTransfer cap = new CutAndPasteTransfer(); - Desktop.addInternalFrame(cap, "PCA details", 500, 500); - - cap.setText(pca.getDetails()); + try + { + cap.setText(pcaModel.getDetails()); + Desktop.addInternalFrame(cap, "PCA details", 500, 500); + } catch (OutOfMemoryError oom) + { + new OOMWarning("opening PCA details", oom); + cap.dispose(); + } } public void showLabels_actionPerformed(ActionEvent e) @@ -264,7 +283,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."); @@ -286,7 +305,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) { @@ -322,8 +342,7 @@ public class PCAPanel extends GPCAPanel implements Runnable * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i < * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 + * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] + - * "\n"); - * } + * "\n"); } * * Desktop.addInternalFrame(cap, "Original Data", 400, 400); */ @@ -377,7 +396,7 @@ public class PCAPanel extends GPCAPanel implements Runnable * DOCUMENT ME! * * @param e - * DOCUMENT ME! + * DOCUMENT ME! */ public void eps_actionPerformed(ActionEvent e) { @@ -388,7 +407,7 @@ public class PCAPanel extends GPCAPanel implements Runnable * DOCUMENT ME! * * @param e - * DOCUMENT ME! + * DOCUMENT ME! */ public void png_actionPerformed(ActionEvent e) { @@ -410,8 +429,8 @@ public class PCAPanel extends GPCAPanel implements Runnable else { im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS, - "Make EPS file from PCA", width, height, null, this - .getTitle()); + "Make EPS file from PCA", width, height, null, + this.getTitle()); } if (im.getGraphics() != null) @@ -489,4 +508,164 @@ public class PCAPanel extends GPCAPanel implements Runnable } + /* + * (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(pcaModel.getPointsasCsv(false, + xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(), + zCombobox.getSelectedIndex())); + Desktop.addInternalFrame(cap, "Points for " + getTitle(), 500, 500); + } catch (OutOfMemoryError oom) + { + new OOMWarning("exporting PCA points", oom); + cap.dispose(); + } + } + + /* + * (non-Javadoc) + * + * @see + * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event + * .ActionEvent) + */ + protected void outputProjPoints_actionPerformed(ActionEvent e) + { + CutAndPasteTransfer cap = new CutAndPasteTransfer(); + try + { + cap.setText(pcaModel.getPointsasCsv(true, + xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(), + zCombobox.getSelectedIndex())); + Desktop.addInternalFrame(cap, "Transformed points for " + getTitle(), + 500, 500); + } catch (OutOfMemoryError oom) + { + new OOMWarning("exporting transformed PCA points", oom); + cap.dispose(); + } + } + + // methods for implementing IProgressIndicator + // need to refactor to a reusable stub class + Hashtable progressBars, progressBarHandlers; + + /* + * (non-Javadoc) + * + * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long) + */ + @Override + public void setProgressBar(String message, long id) + { + if (progressBars == null) + { + progressBars = new Hashtable(); + progressBarHandlers = new Hashtable(); + } + + JPanel progressPanel; + Long lId = new Long(id); + GridLayout layout = (GridLayout) statusPanel.getLayout(); + if (progressBars.get(lId) != null) + { + progressPanel = (JPanel) progressBars.get(new Long(id)); + statusPanel.remove(progressPanel); + progressBars.remove(lId); + progressPanel = null; + if (message != null) + { + statusBar.setText(message); + } + if (progressBarHandlers.contains(lId)) + { + progressBarHandlers.remove(lId); + } + layout.setRows(layout.getRows() - 1); + } + else + { + progressPanel = new JPanel(new BorderLayout(10, 5)); + + JProgressBar progressBar = new JProgressBar(); + progressBar.setIndeterminate(true); + + progressPanel.add(new JLabel(message), BorderLayout.WEST); + progressPanel.add(progressBar, BorderLayout.CENTER); + + layout.setRows(layout.getRows() + 1); + statusPanel.add(progressPanel); + + progressBars.put(lId, progressPanel); + } + // update GUI + // setMenusForViewport(); + validate(); + } + + @Override + public void registerHandler(final long id, + final IProgressIndicatorHandler handler) + { + if (progressBarHandlers == null || !progressBars.contains(new Long(id))) + { + throw new Error( + "call setProgressBar before registering the progress bar's handler."); + } + progressBarHandlers.put(new Long(id), handler); + final JPanel progressPanel = (JPanel) progressBars.get(new Long(id)); + if (handler.canCancel()) + { + JButton cancel = new JButton("Cancel"); + final IProgressIndicator us = this; + cancel.addActionListener(new ActionListener() + { + + @Override + public void actionPerformed(ActionEvent e) + { + handler.cancelActivity(id); + us.setProgressBar( + "Cancelled " + + ((JLabel) progressPanel.getComponent(0)) + .getText(), id); + } + }); + progressPanel.add(cancel, BorderLayout.EAST); + } + } + + /** + * + * @return true if any progress bars are still active + */ + @Override + public boolean operationInProgress() + { + if (progressBars != null && progressBars.size() > 0) + { + return true; + } + return false; + } + + @Override + protected void resetButton_actionPerformed(ActionEvent e) + { + int t = top; + top = 0; // ugly - prevents dimensionChanged events from being processed + xCombobox.setSelectedIndex(0); + yCombobox.setSelectedIndex(1); + top = t; + zCombobox.setSelectedIndex(2); + } }