X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FPCAPanel.java;h=254f5faef06f3aef64975256d0cfad4c3eb3f1ce;hb=a8f483d04205bb8273ee311c12968b7e86d205fa;hp=06f1b25d48fbbf92aa7cf8d27fdb3816bb4590b7;hpb=feda4a6c19b530b08c3dca57abb86cd70412e984;p=jalview.git diff --git a/src/jalview/gui/PCAPanel.java b/src/jalview/gui/PCAPanel.java old mode 100755 new mode 100644 index 06f1b25..254f5fa --- a/src/jalview/gui/PCAPanel.java +++ b/src/jalview/gui/PCAPanel.java @@ -1,56 +1,63 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2007 AM Waterhouse, J Procter, 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 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. - * - * 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 + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2) + * Copyright (C) 2014 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. + * + * 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 . + * 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.schemes.ResidueProperties; +import jalview.util.MessageManager; +import jalview.viewmodel.PCAModel; /** * DOCUMENT ME! - * + * * @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; + AlignmentPanel ap; + AlignViewport av; - AlignmentView seqstrings; - SequenceI[] seqs; + + PCAModel pcaModel; + + int top = 0; /** * Creates a new PCAPanel object. - * - * @param av DOCUMENT ME! - * @param s DOCUMENT ME! + * + * @param av + * DOCUMENT ME! + * @param s + * DOCUMENT ME! */ public PCAPanel(AlignmentPanel ap) { @@ -58,15 +65,18 @@ public class PCAPanel 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(); @@ -82,19 +92,17 @@ public class PCAPanel 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, + "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); return; } - - Desktop.addInternalFrame(this, "Principal component analysis", - 400, 400); - + pcaModel = new PCAModel(seqstrings, seqs, nucleotide); PaintRefresher.Register(this, av.getSequenceSetId()); rc = new RotatableCanvas(ap); @@ -102,11 +110,47 @@ public class PCAPanel Thread worker = new Thread(this); 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((String) sm)) + { + pcaModel.setScore_matrix((String) 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", - rc.bgColour); + rc.bgColour); if (col != null) { @@ -120,64 +164,81 @@ public class PCAPanel */ 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, 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()) { - JOptionPane.showInternalMessageDialog(Desktop.desktop, - "Out of memory calculating PCA!!" - + - "\nSee help files for increasing Java Virtual Machine memory." - , "Out of memory", - JOptionPane.WARNING_MESSAGE); - System.out.println("PCAPanel: " + er); - System.gc(); - + 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(); } /** @@ -193,14 +254,7 @@ public class PCAPanel 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(); @@ -209,8 +263,9 @@ public class PCAPanel /** * DOCUMENT ME! - * - * @param e DOCUMENT ME! + * + * @param e + * DOCUMENT ME! */ protected void xCombobox_actionPerformed(ActionEvent e) { @@ -219,8 +274,9 @@ public class PCAPanel /** * DOCUMENT ME! - * - * @param e DOCUMENT ME! + * + * @param e + * DOCUMENT ME! */ protected void yCombobox_actionPerformed(ActionEvent e) { @@ -229,8 +285,9 @@ public class PCAPanel /** * DOCUMENT ME! - * - * @param e DOCUMENT ME! + * + * @param e + * DOCUMENT ME! */ protected void zCombobox_actionPerformed(ActionEvent e) { @@ -240,10 +297,15 @@ public class PCAPanel 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, MessageManager.getString("label.pca_details"), 500, 500); + } catch (OutOfMemoryError oom) + { + new OOMWarning("opening PCA details", oom); + cap.dispose(); + } } public void showLabels_actionPerformed(ActionEvent e) @@ -259,31 +321,40 @@ public class PCAPanel public void originalSeqData_actionPerformed(ActionEvent e) { - // 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) + // this was cut'n'pasted from the equivalent TreePanel method - we should + // make this an abstract function of all jalview analysis windows + if (pcaModel.getSeqtrings() == null) { - jalview.bin.Cache.log.info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action."); + jalview.bin.Cache.log + .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action."); return; } - // decide if av alignment is sufficiently different to original data to warrant a new window to be created - // create new alignmnt window with hidden regions (unhiding hidden regions yields unaligned seqs) + // decide if av alignment is sufficiently different to original data to + // warrant a new window to be created + // create new alignmnt window with hidden regions (unhiding hidden regions + // yields unaligned seqs) // or create a selection box around columns in alignment view // test Alignment(SeqCigar[]) char gc = '-'; - try { + try + { // we try to get the associated view's gap character // but this may fail if the view was closed... - gc = av. - getGapCharacter(); - } catch (Exception ex) {}; - Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(gc); + gc = av.getGapCharacter(); + } catch (Exception ex) + { + } + ; + Object[] alAndColsel = pcaModel.getSeqtrings() + .getAlignmentAndColumnSelection(gc); if (alAndColsel != null && alAndColsel[0] != null) { // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]); - Alignment al = new Alignment( (SequenceI[]) alAndColsel[0]); - Alignment dataset = (av!=null && av.getAlignment()!=null) ? av.getAlignment().getDataset() : null; + Alignment al = new Alignment((SequenceI[]) alAndColsel[0]); + Alignment dataset = (av != null && av.getAlignment() != null) ? av + .getAlignment().getDataset() : null; if (dataset != null) { al.setDataset(dataset); @@ -292,38 +363,32 @@ public class PCAPanel if (true) { // make a new frame! - AlignFrame af = new AlignFrame(al, (ColumnSelection) alAndColsel[1], - AlignFrame.DEFAULT_WIDTH, - AlignFrame.DEFAULT_HEIGHT - ); + AlignFrame af = new AlignFrame(al, + (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH, + AlignFrame.DEFAULT_HEIGHT); - //>>>This is a fix for the moment, until a better solution is found!!<<< + // >>>This is a fix for the moment, until a better solution is + // found!!<<< // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer()); - // af.addSortByOrderMenuItem(ServiceName + " Ordering", - // msaorder); + // 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); } } - /* 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"); - - } - - Desktop.addInternalFrame(cap, "Original Data", - 400, 400); + /* + * 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"); } + * + * Desktop.addInternalFrame(cap, "Original Data", 400, 400); */ } - class PCAPrinter - extends Thread implements Printable + class PCAPrinter extends Thread implements Printable { public void run() { @@ -337,8 +402,7 @@ public class PCAPanel try { printJob.print(); - } - catch (Exception PrintException) + } catch (Exception PrintException) { PrintException.printStackTrace(); } @@ -346,9 +410,9 @@ public class PCAPanel } public int print(Graphics pg, PageFormat pf, int pi) - throws PrinterException + throws PrinterException { - pg.translate( (int) pf.getImageableX(), (int) pf.getImageableY()); + pg.translate((int) pf.getImageableX(), (int) pf.getImageableY()); rc.drawBackground(pg, rc.bgColour); rc.drawScene(pg); @@ -370,8 +434,9 @@ public class PCAPanel /** * DOCUMENT ME! - * - * @param e DOCUMENT ME! + * + * @param e + * DOCUMENT ME! */ public void eps_actionPerformed(ActionEvent e) { @@ -380,8 +445,9 @@ public class PCAPanel /** * DOCUMENT ME! - * - * @param e DOCUMENT ME! + * + * @param e + * DOCUMENT ME! */ public void png_actionPerformed(ActionEvent e) { @@ -397,19 +463,14 @@ public class PCAPanel if (type == jalview.util.ImageMaker.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.PNG, + "Make PNG image from PCA", width, height, null, null); } 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.EPS, + "Make EPS file from PCA", width, height, null, + this.getTitle()); } if (im.getGraphics() != null) @@ -424,6 +485,7 @@ public class PCAPanel } } + public void viewMenu_menuSelected() { buildAssociatedViewMenu(); @@ -431,8 +493,8 @@ public class PCAPanel void buildAssociatedViewMenu() { - AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av. - getSequenceSetId()); + AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av + .getSequenceSetId()); if (aps.length == 1 && rc.av == aps[0].av) { associateViewsMenu.setVisible(false); @@ -441,7 +503,7 @@ public class PCAPanel associateViewsMenu.setVisible(true); - if ( (viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem)) + if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem)) { viewMenu.insertSeparator(viewMenu.getItemCount() - 1); } @@ -487,4 +549,164 @@ public class PCAPanel } + /* + * (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, MessageManager.formatMessage("label.points_for_params", new String[]{this.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, MessageManager.formatMessage("label.transformed_points_for_params", new String[]{this.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(MessageManager.getString("action.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); + } }