X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FPCAPanel.java;h=772e67e8b8fccb12aa41810cc8e1af3c81e18e63;hb=6de402db90987d3645f3429d9073c13678d8e980;hp=8c2ecc9f851bbbfde7462e87e131893b067ea6c6;hpb=7c05496d1da22f21ce5ae1a199afb9b16c8777ea;p=jalview.git diff --git a/src/jalview/gui/PCAPanel.java b/src/jalview/gui/PCAPanel.java index 8c2ecc9..772e67e 100755 --- a/src/jalview/gui/PCAPanel.java +++ b/src/jalview/gui/PCAPanel.java @@ -1,6 +1,6 @@ /* * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * 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 @@ -18,19 +18,16 @@ */ package jalview.gui; -import jalview.analysis.*; - -import jalview.datamodel.*; - -import jalview.jbgui.*; +import java.util.*; import java.awt.*; import java.awt.event.*; - -import java.util.*; -import javax.swing.*; import java.awt.print.*; +import javax.swing.*; +import jalview.analysis.*; +import jalview.datamodel.*; +import jalview.jbgui.*; /** * DOCUMENT ME! @@ -38,329 +35,450 @@ import java.awt.print.*; * @author $author$ * @version $Revision$ */ -public class PCAPanel extends GPCAPanel implements Runnable +public class PCAPanel + extends GPCAPanel implements Runnable { - PCA pca; - int top; - RotatableCanvas rc; - AlignViewport av; - String [] seqstrings; - SequenceI [] seqs; - - /** - * Creates a new PCAPanel object. - * - * @param av DOCUMENT ME! - * @param s DOCUMENT ME! - */ - public PCAPanel(AlignViewport av) + PCA pca; + int top; + RotatableCanvas rc; + AlignmentPanel ap; + AlignViewport av; + AlignmentView seqstrings; + SequenceI[] seqs; + + /** + * Creates a new PCAPanel object. + * + * @param av DOCUMENT ME! + * @param s DOCUMENT ME! + */ + public PCAPanel(AlignmentPanel ap) + { + this.av = ap.av; + this.ap = ap; + + boolean sameLength = true; + + seqstrings = av.getAlignmentView(av.getSelectionGroup() != null); + if (av.getSelectionGroup() == null) { - this.av = av; + seqs = av.alignment.getSequencesArray(); + } + else + { + seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment); + } + SeqCigar sq[] = seqstrings.getSequences(); + int length = sq[0].getWidth(); - boolean sameLength = true; + for (int i = 0; i < seqs.length; i++) + { + if (sq[i].getWidth() != length) + { + sameLength = false; + break; + } + } - seqstrings = av.getSelectionAsString(); + 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; + } - if (av.getSelectionGroup() == null) - { - seqs = av.alignment.getSequencesArray(); - } - else - { - seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment); - } + Desktop.addInternalFrame(this, "Principal component analysis", + 400, 400); - int length = seqs[0].getLength(); + PaintRefresher.Register(this, av.getSequenceSetId()); - for (int i = 0; i < seqs.length; i++) - { - if (seqs[i].getLength() != length) - { - sameLength = false; - break; - } - } + rc = new RotatableCanvas(ap); + this.getContentPane().add(rc, BorderLayout.CENTER); + Thread worker = new Thread(this); + worker.start(); + } + + public void bgcolour_actionPerformed(ActionEvent e) + { + Color col = JColorChooser.showDialog(this, "Select Background Colour", + rc.bgColour); + + if (col != null) + { + rc.bgColour = col; + } + rc.repaint(); + } - if (!sameLength) + /** + * DOCUMENT ME! + */ + public void run() + { + 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) { - 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; + comps[i] = pca.component(i); } + } + ////////////////// + xCombobox.setSelectedIndex(0); + yCombobox.setSelectedIndex(1); + zCombobox.setSelectedIndex(2); - Desktop.addInternalFrame(this, "Principal component analysis", - 400, 400); + top = pca.getM().rows - 1; + Vector points = new Vector(); + float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100); - rc = new RotatableCanvas(av); - this.getContentPane().add(rc, BorderLayout.CENTER); - Thread worker = new Thread(this); - worker.start(); - } + 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(); - public void bgcolour_actionPerformed(ActionEvent e) + addKeyListener(rc); + + } + catch (OutOfMemoryError er) { - Color col = JColorChooser.showDialog(this, "Select Background Colour", - rc.bgColour); + 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(); - if(col!=null) - rc.bgColour = col; - rc.repaint(); } + } + /** + * DOCUMENT ME! + */ + void doDimensionChange() + { + if (top == 0) + { + return; + } + int dim1 = top - xCombobox.getSelectedIndex(); + int dim2 = top - yCombobox.getSelectedIndex(); + int dim3 = top - zCombobox.getSelectedIndex(); - /** - * DOCUMENT ME! - */ - public void run() + float[][] scores = pca.getComponents(dim1, dim2, dim3, 100); + + for (int i = 0; i < pca.getM().rows; i++) { - try{ - pca = new PCA(seqstrings); - pca.run(); + ( (SequencePoint) rc.points.elementAt(i)).coord = scores[i]; + } - // Now find the component coordinates - int ii = 0; + rc.img = null; + rc.rotmat.setIdentity(); + rc.initAxes(); + rc.paint(rc.getGraphics()); + } - while ( (ii < seqs.length) && (seqs[ii] != null)) - { - ii++; - } + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void xCombobox_actionPerformed(ActionEvent e) + { + doDimensionChange(); + } - double[][] comps = new double[ii][ii]; + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void yCombobox_actionPerformed(ActionEvent e) + { + doDimensionChange(); + } - for (int i = 0; i < ii; i++) - { - if (pca.getEigenvalue(i) > 1e-4) - { - comps[i] = pca.component(i); - } - } + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void zCombobox_actionPerformed(ActionEvent e) + { + doDimensionChange(); + } - ////////////////// - xCombobox.setSelectedIndex(0); - yCombobox.setSelectedIndex(1); - zCombobox.setSelectedIndex(2); + public void outputValues_actionPerformed(ActionEvent e) + { + CutAndPasteTransfer cap = new CutAndPasteTransfer(); + Desktop.addInternalFrame(cap, "PCA details", 500, + 500); - top = pca.getM().rows - 1; + 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++) - { - SequencePoint sp = new SequencePoint(seqs[i], scores[i]); - points.addElement(sp); - } + public void print_actionPerformed(ActionEvent e) + { + PCAPrinter printer = new PCAPrinter(); + printer.start(); + } - rc.setPoints(points, pca.getM().rows); - rc.repaint(); - } - 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(); + 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) + { + 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) + // or create a selection box around columns in alignment view + // test Alignment(SeqCigar[]) + Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(av. + getGapCharacter()); + + if (alAndColsel != null && alAndColsel[0] != null) + { + // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]); + Alignment al = new Alignment( (SequenceI[]) alAndColsel[0]); + Alignment dataset = av.getAlignment().getDataset(); + if (dataset != null) + { + al.setDataset(dataset); } + if (true) + { + // make a new frame! + 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!!<<< + // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer()); + + // af.addSortByOrderMenuItem(ServiceName + " Ordering", + // msaorder); + + Desktop.addInternalFrame(af, "Original Data for " + 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"); - /** - * DOCUMENT ME! + } + + Desktop.addInternalFrame(cap, "Original Data", + 400, 400); */ - void doDimensionChange() - { - if (top == 0) - { - return; - } + } - int dim1 = top - xCombobox.getSelectedIndex(); - int dim2 = top - yCombobox.getSelectedIndex(); - int dim3 = top - zCombobox.getSelectedIndex(); + class PCAPrinter + extends Thread implements Printable + { + public void run() + { + PrinterJob printJob = PrinterJob.getPrinterJob(); + PageFormat pf = printJob.pageDialog(printJob.defaultPage()); - float[][] scores = pca.getComponents(dim1, dim2, dim3, 100); + printJob.setPrintable(this, pf); - for (int i = 0; i < pca.getM().rows; i++) + if (printJob.printDialog()) + { + try { - ((SequencePoint) rc.points.elementAt(i)).coord = scores[i]; + printJob.print(); } - - rc.img = null; - rc.rotmat.setIdentity(); - rc.initAxes(); - rc.paint(rc.getGraphics()); + catch (Exception PrintException) + { + PrintException.printStackTrace(); + } + } } - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - protected void xCombobox_actionPerformed(ActionEvent e) + public int print(Graphics pg, PageFormat pf, int pi) + throws PrinterException { - doDimensionChange(); - } + pg.translate( (int) pf.getImageableX(), (int) pf.getImageableY()); - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - protected void yCombobox_actionPerformed(ActionEvent e) - { - doDimensionChange(); - } + rc.drawBackground(pg, rc.bgColour); + rc.drawScene(pg); + if (rc.drawAxes == true) + { + rc.drawAxes(pg); + } - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - protected void zCombobox_actionPerformed(ActionEvent e) - { - doDimensionChange(); + if (pi == 0) + { + return Printable.PAGE_EXISTS; + } + else + { + return Printable.NO_SUCH_PAGE; + } } + } + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void eps_actionPerformed(ActionEvent e) + { + makePCAImage(jalview.util.ImageMaker.EPS); + } - public void outputValues_actionPerformed(ActionEvent e) - { - CutAndPasteTransfer cap = new CutAndPasteTransfer(); - Desktop.addInternalFrame(cap, "PCA details", 500, - 500); + /** + * 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(); - cap.setText(pca.getDetails()); - } + jalview.util.ImageMaker im; - public void showLabels_actionPerformed(ActionEvent e) + if (type == jalview.util.ImageMaker.PNG) { - rc.showLabels(showLabels.getState()); + im = new jalview.util.ImageMaker(this, + jalview.util.ImageMaker.PNG, + "Make PNG image from PCA", + width, height, + null, null); } - - public void print_actionPerformed(ActionEvent e) + else { - PCAPrinter printer = new PCAPrinter(); - printer.start(); + im = new jalview.util.ImageMaker(this, + jalview.util.ImageMaker.EPS, + "Make EPS file from PCA", + width, height, + null, this.getTitle()); } - public void originalSeqData_actionPerformed(ActionEvent e) + if (im.getGraphics() != null) { - CutAndPasteTransfer cap = new CutAndPasteTransfer(); - for(int i=0; i