X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FPCAPanel.java;h=e680ddeab3fcb15970cd0817878fad8521c221a2;hb=e9af35537cd673f2f7eaf5fc0e03187cc126d2e5;hp=d0b3bbca7501c30aed5537e60a0d0247d5801ff4;hpb=8f2acf658e855209c6db6b68d5cc0779733c3d7e;p=jalview.git diff --git a/src/jalview/gui/PCAPanel.java b/src/jalview/gui/PCAPanel.java index d0b3bbc..e680dde 100755 --- a/src/jalview/gui/PCAPanel.java +++ b/src/jalview/gui/PCAPanel.java @@ -1,122 +1,363 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2005 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 + */ package jalview.gui; +import jalview.analysis.*; + import jalview.datamodel.*; -import jalview.analysis.PCA; + import jalview.jbgui.*; + import java.awt.*; import java.awt.event.*; + import java.util.*; +import javax.swing.*; +import java.awt.print.*; +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ public class PCAPanel extends GPCAPanel implements Runnable { - PCA pca; - int top; - RotatableCanvas rc; - AlignViewport av; - - public void run() - { - // do stuff - } + PCA pca; + int top; + RotatableCanvas rc; + AlignViewport av; + SequenceI [] seqs; + + /** + * Creates a new PCAPanel object. + * + * @param av DOCUMENT ME! + * @param s DOCUMENT ME! + */ + public PCAPanel(AlignViewport av) + { + this.av = av; + + boolean sameLength = true; + + if ((av.getSelectionGroup() != null) && + (av.getSelectionGroup().getSize() > 3)) + { + seqs = new Sequence[av.getSelectionGroup().getSize()]; + int length = av.getSelectionGroup().getSequenceAt(0).getLength(); + for (int i = 0; i < av.getSelectionGroup().getSize(); i++) + { + seqs[i] = av.getSelectionGroup().getSequenceAt(i); + if(seqs[i].getLength()!=length) + { + sameLength = false; + break; + } + } + } + else + { + seqs = new Sequence[av.getAlignment().getHeight()]; + int length = av.alignment.getSequenceAt(0).getLength(); - public PCAPanel(AlignViewport av, SequenceI[] s) { + for (int i = 0; i < av.getAlignment().getHeight(); i++) + { + seqs[i] = av.getAlignment().getSequenceAt(i); + if(seqs[i].getLength()!=length) + { + sameLength = false; + break; + } - this.av = av; - if(av.getSelectionGroup()!=null && av.getSelectionGroup().getSize()>3) + } + } + + 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); + + return; + } + + + Desktop.addInternalFrame(this, "Principal component analysis", + 400, 400); + + + rc = new RotatableCanvas(av); + this.getContentPane().add(rc, BorderLayout.CENTER); + Thread worker = new Thread(this); + worker.start(); + } + + public void bgcolour_actionPerformed(ActionEvent e) { - s = new Sequence[av.getSelectionGroup().getSize()]; - for(int i=0; i 1e-4) + { + comps[i] = pca.component(i); + } + } + + ////////////////// + xCombobox.setSelectedIndex(0); + yCombobox.setSelectedIndex(1); + zCombobox.setSelectedIndex(2); + + top = pca.getM().rows - 1; -//////////////////////This part was done in PCATHread originally. Is it too slow??? - pca = new PCA(s); - pca.run(); + Vector points = new Vector(); + float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100); - // Now find the component coordinates - int ii=0; - while (ii < s.length && s[ii] != null) + for (int i = 0; i < pca.getM().rows; i++) + { + SequencePoint sp = new SequencePoint(seqs[i], scores[i]); + points.addElement(sp); + } + + rc.setPoints(points, pca.getM().rows); + rc.repaint(); + seqs = null; + } + catch(OutOfMemoryError er) + { 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(); + + } + + } + + /** + * DOCUMENT ME! + */ + void doDimensionChange() { - ii++; + if (top == 0) + { + return; + } + + 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]; + } + + rc.img = null; + rc.rotmat.setIdentity(); + rc.initAxes(); + rc.paint(rc.getGraphics()); } - double[][] comps = new double[ii][ii]; + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void xCombobox_actionPerformed(ActionEvent e) + { + doDimensionChange(); + } - for (int i=0; i < ii; i++ ) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void yCombobox_actionPerformed(ActionEvent e) { - if (pca.getEigenvalue(i) > 1e-4) - { - comps[i] = pca.component(i); - } + doDimensionChange(); } - ////////////////// + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void zCombobox_actionPerformed(ActionEvent e) + { + doDimensionChange(); + } - xCombobox.setSelectedIndex(0); - yCombobox.setSelectedIndex(1); - zCombobox.setSelectedIndex(2); - top = pca.getM().rows-1; + public void outputValues_actionPerformed(ActionEvent e) + { + CutAndPasteTransfer cap = new CutAndPasteTransfer(); + Desktop.addInternalFrame(cap, "PCA details", 500, + 500); + + cap.setText(pca.getDetails()); + } - Vector points = new Vector(); - float[][] scores = pca.getComponents(top-1,top-2,top-3,100); + public void showLabels_actionPerformed(ActionEvent e) + { + rc.showLabels(showLabels.getState()); + } - for (int i =0; i < pca.getM().rows; i++ ) + public void print_actionPerformed(ActionEvent e) { - SequencePoint sp = new SequencePoint(s[i],scores[i]); - points.addElement(sp); + PCAPrinter printer = new PCAPrinter(); + printer.start(); } - rc = new RotatableCanvas(av,points,pca.getM().rows); - //rc.printPoints(); + class PCAPrinter extends Thread implements Printable + { + public void run() + { + PrinterJob printJob = PrinterJob.getPrinterJob(); + PageFormat pf = printJob.pageDialog(printJob.defaultPage()); - add(rc, BorderLayout.CENTER); - } + printJob.setPrintable(this, pf); + if (printJob.printDialog()) + { + try + { + printJob.print(); + } + catch (Exception PrintException) + { + PrintException.printStackTrace(); + } + } + } - void doDimensionChange() - { - if(top==0) - return; + public int print(Graphics pg, PageFormat pf, int pi) + throws PrinterException + { + pg.translate( (int) pf.getImageableX(), (int) pf.getImageableY()); - int dim1 = top - xCombobox.getSelectedIndex(); - int dim2 = top - yCombobox.getSelectedIndex(); - int dim3 = top - zCombobox.getSelectedIndex(); + rc.drawBackground(pg, rc.bgColour); + rc.drawScene(pg); + if (rc.drawAxes == true) + { + rc.drawAxes(pg); + } - 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]; + if (pi == 0) + return Printable.PAGE_EXISTS; + else + return Printable.NO_SUCH_PAGE; + } } - rc.img = null; - rc.rotmat.setIdentity(); - rc.initAxes(); - rc.paint(rc.getGraphics()); - } - protected void xCombobox_actionPerformed(ActionEvent e) - { - doDimensionChange(); - } - protected void yCombobox_actionPerformed(ActionEvent e) - { - doDimensionChange(); - } + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void eps_actionPerformed(ActionEvent e) + { + makePCAImage(jalview.util.ImageMaker.EPS); + } - protected void zCombobox_actionPerformed(ActionEvent e) - { - doDimensionChange(); - } + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void png_actionPerformed(ActionEvent e) + { + makePCAImage(jalview.util.ImageMaker.PNG); + } + + void makePCAImage(int type) + { + int width = rc.getWidth(); + int height = rc.getHeight(); -} + jalview.util.ImageMaker im; + + 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); + else + im = new jalview.util.ImageMaker(this, + jalview.util.ImageMaker.EPS, + "Make EPS file from PCA", + width, height, + null, this.getTitle()); + + if(im.getGraphics()!=null) + { + rc.drawBackground(im.getGraphics(), Color.black); + rc.drawScene(im.getGraphics()); + if (rc.drawAxes == true) + { + rc.drawAxes(im.getGraphics()); + } + im.writeImage(); + } + } + }