X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fgui%2FPCAPanel.java;h=d7e0cd78d3c7bea344592aa99589c2570127f426;hb=b69e028fe41a867e72cda2a6d69182ff0c6bd90e;hp=7bda65a597613aeac4e41f4ab5ae531b6f4bb862;hpb=efc31b4a8d5cee63555586804a2b79c06bdb5a14;p=jalview.git diff --git a/src/jalview/gui/PCAPanel.java b/src/jalview/gui/PCAPanel.java index 7bda65a..d7e0cd7 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) 2006 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 @@ -28,6 +28,8 @@ import java.awt.*; import java.awt.event.*; import java.util.*; +import javax.swing.*; +import java.awt.print.*; /** @@ -41,7 +43,10 @@ public class PCAPanel extends GPCAPanel implements Runnable PCA pca; int top; RotatableCanvas rc; + AlignmentPanel ap; AlignViewport av; + AlignmentView seqstrings; + SequenceI [] seqs; /** * Creates a new PCAPanel object. @@ -49,51 +54,98 @@ public class PCAPanel extends GPCAPanel implements Runnable * @param av DOCUMENT ME! * @param s DOCUMENT ME! */ - public PCAPanel(AlignViewport av, SequenceI[] s) + public PCAPanel(AlignmentPanel ap) { - this.av = av; + this.av = ap.av; + this.ap = ap; - if ((av.getSelectionGroup() != null) && - (av.getSelectionGroup().getSize() > 3)) - { - s = new Sequence[av.getSelectionGroup().getSize()]; + boolean sameLength = true; - for (int i = 0; i < s.length; i++) - { - s[i] = av.getSelectionGroup().getSequenceAt(i); - } + seqstrings = av.getAlignmentView(av.getSelectionGroup()!=null); + if(av.getSelectionGroup()==null) + { + seqs = av.alignment.getSequencesArray(); } + else + { + seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment); + } + SeqCigar sq[]=seqstrings.getSequences(); + int length = sq[0].getWidth(); - if (s == null) + for (int i = 0; i < seqs.length; i++) { - s = new Sequence[av.getAlignment().getHeight()]; + if (sq[i].getWidth() != length) + { + sameLength = false; + break; + } + } - for (int i = 0; i < av.getAlignment().getHeight(); i++) - { - s[i] = av.getAlignment().getSequenceAt(i); - } + 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; } - //////////////////////This part was done in PCATHread originally. Is it too slow??? - pca = new PCA(s); + + Desktop.addInternalFrame(this, "Principal component analysis", + 400, 400); + + + + PaintRefresher.Register(this, av.getSequenceSetId()); + + 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(); + } + + + + + /** + * DOCUMENT ME! + */ + public void run() + { + try{ + pca = new PCA(seqstrings.getSequenceStrings(' ')); pca.run(); // Now find the component coordinates int ii = 0; - while ((ii < s.length) && (s[ii] != null)) + while ( (ii < seqs.length) && (seqs[ii] != null)) { - ii++; + 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); - } + if (pca.getEigenvalue(i) > 1e-4) + { + comps[i] = pca.component(i); + } } ////////////////// @@ -108,22 +160,28 @@ public class PCAPanel extends GPCAPanel implements Runnable for (int i = 0; i < pca.getM().rows; i++) { - SequencePoint sp = new SequencePoint(s[i], scores[i]); - points.addElement(sp); + SequencePoint sp = new SequencePoint(seqs[i], scores[i]); + points.addElement(sp); } - rc = new RotatableCanvas(av, points, pca.getM().rows); + rc.setPoints(points, pca.getM().rows); + rc.repaint(); - //rc.printPoints(); - add(rc, BorderLayout.CENTER); - } + addKeyListener(rc); + + } + 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! - */ - public void run() - { - // do stuff } /** @@ -182,4 +240,248 @@ public class PCAPanel extends GPCAPanel implements Runnable { doDimensionChange(); } -} + + + public void outputValues_actionPerformed(ActionEvent e) + { + CutAndPasteTransfer cap = new CutAndPasteTransfer(); + Desktop.addInternalFrame(cap, "PCA details", 500, + 500); + + cap.setText(pca.getDetails()); + } + + public void showLabels_actionPerformed(ActionEvent e) + { + rc.showLabels(showLabels.getState()); + } + + public void print_actionPerformed(ActionEvent e) + { + PCAPrinter printer = new PCAPrinter(); + printer.start(); + } + + 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"); + + } + + Desktop.addInternalFrame(cap, "Original Data", + 400, 400); + */ + } + + + + class PCAPrinter extends Thread implements Printable + { + public void run() + { + PrinterJob printJob = PrinterJob.getPrinterJob(); + PageFormat pf = printJob.pageDialog(printJob.defaultPage()); + + printJob.setPrintable(this, pf); + + if (printJob.printDialog()) + { + try + { + printJob.print(); + } + catch (Exception PrintException) + { + PrintException.printStackTrace(); + } + } + } + + public int print(Graphics pg, PageFormat pf, int pi) + throws PrinterException + { + pg.translate( (int) pf.getImageableX(), (int) pf.getImageableY()); + + rc.drawBackground(pg, rc.bgColour); + rc.drawScene(pg); + if (rc.drawAxes == true) + { + rc.drawAxes(pg); + } + + 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); + } + + /** + * 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(); + } + } + + + public void viewMenu_menuSelected() + { + buildAssociatedViewMenu(); + } + + + void buildAssociatedViewMenu() + { + AlignmentPanel [] aps = PaintRefresher.getAssociatedPanels(av.getSequenceSetId()); + if(aps.length==1 && rc.av ==aps[0].av) + { + associateViewsMenu.setVisible(false); + return; + } + + associateViewsMenu.setVisible(true); + + if( (viewMenu.getItem(viewMenu.getItemCount()-2) instanceof JMenuItem)) + { + viewMenu.insertSeparator(viewMenu.getItemCount()-1); + } + + associateViewsMenu.removeAll(); + + + JRadioButtonMenuItem item; + ButtonGroup buttonGroup = new ButtonGroup(); + int i, iSize = aps.length; + final PCAPanel thisPCAPanel = this; + for(i=0; i