2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.gui;
\r
21 import jalview.analysis.*;
\r
23 import jalview.datamodel.*;
\r
25 import jalview.jbgui.*;
\r
28 import java.awt.event.*;
\r
31 import javax.swing.*;
\r
32 import java.awt.print.*;
\r
39 * @version $Revision$
\r
41 public class PCAPanel extends GPCAPanel implements Runnable
\r
47 String [] seqstrings;
\r
51 * Creates a new PCAPanel object.
\r
53 * @param av DOCUMENT ME!
\r
54 * @param s DOCUMENT ME!
\r
56 public PCAPanel(AlignViewport av)
\r
60 boolean sameLength = true;
\r
62 seqstrings = av.getViewAsString(true);
\r
64 if (av.getSelectionGroup() == null)
\r
66 seqs = av.alignment.getSequencesArray();
\r
70 seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment);
\r
73 int length = seqs[0].getLength();
\r
75 for (int i = 0; i < seqs.length; i++)
\r
77 if (seqs[i].getLength() != length)
\r
86 JOptionPane.showMessageDialog(Desktop.desktop,
\r
87 "The sequences must be aligned before calculating PCA.\n" +
\r
88 "Try using the Pad function in the edit menu,\n" +
\r
89 "or one of the multiple sequence alignment web services.",
\r
90 "Sequences not aligned",
\r
91 JOptionPane.WARNING_MESSAGE);
\r
97 Desktop.addInternalFrame(this, "Principal component analysis",
\r
101 rc = new RotatableCanvas(av);
\r
102 this.getContentPane().add(rc, BorderLayout.CENTER);
\r
103 Thread worker = new Thread(this);
\r
107 public void bgcolour_actionPerformed(ActionEvent e)
\r
109 Color col = JColorChooser.showDialog(this, "Select Background Colour",
\r
126 pca = new PCA(seqstrings);
\r
129 // Now find the component coordinates
\r
132 while ( (ii < seqs.length) && (seqs[ii] != null))
\r
137 double[][] comps = new double[ii][ii];
\r
139 for (int i = 0; i < ii; i++)
\r
141 if (pca.getEigenvalue(i) > 1e-4)
\r
143 comps[i] = pca.component(i);
\r
148 xCombobox.setSelectedIndex(0);
\r
149 yCombobox.setSelectedIndex(1);
\r
150 zCombobox.setSelectedIndex(2);
\r
152 top = pca.getM().rows - 1;
\r
154 Vector points = new Vector();
\r
155 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
\r
157 for (int i = 0; i < pca.getM().rows; i++)
\r
159 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
\r
160 points.addElement(sp);
\r
163 rc.setPoints(points, pca.getM().rows);
\r
166 catch(OutOfMemoryError er)
\r
167 { JOptionPane.showInternalMessageDialog(Desktop.desktop,
\r
168 "Out of memory calculating PCA!!"
\r
170 "\nSee help files for increasing Java Virtual Machine memory."
\r
172 JOptionPane.WARNING_MESSAGE);
\r
173 System.out.println("PCAPanel: "+er);
\r
183 void doDimensionChange()
\r
190 int dim1 = top - xCombobox.getSelectedIndex();
\r
191 int dim2 = top - yCombobox.getSelectedIndex();
\r
192 int dim3 = top - zCombobox.getSelectedIndex();
\r
194 float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);
\r
196 for (int i = 0; i < pca.getM().rows; i++)
\r
198 ((SequencePoint) rc.points.elementAt(i)).coord = scores[i];
\r
202 rc.rotmat.setIdentity();
\r
204 rc.paint(rc.getGraphics());
\r
210 * @param e DOCUMENT ME!
\r
212 protected void xCombobox_actionPerformed(ActionEvent e)
\r
214 doDimensionChange();
\r
220 * @param e DOCUMENT ME!
\r
222 protected void yCombobox_actionPerformed(ActionEvent e)
\r
224 doDimensionChange();
\r
230 * @param e DOCUMENT ME!
\r
232 protected void zCombobox_actionPerformed(ActionEvent e)
\r
234 doDimensionChange();
\r
238 public void outputValues_actionPerformed(ActionEvent e)
\r
240 CutAndPasteTransfer cap = new CutAndPasteTransfer();
\r
241 Desktop.addInternalFrame(cap, "PCA details", 500,
\r
244 cap.setText(pca.getDetails());
\r
247 public void showLabels_actionPerformed(ActionEvent e)
\r
249 rc.showLabels(showLabels.getState());
\r
252 public void print_actionPerformed(ActionEvent e)
\r
254 PCAPrinter printer = new PCAPrinter();
\r
258 public void originalSeqData_actionPerformed(ActionEvent e)
\r
260 CutAndPasteTransfer cap = new CutAndPasteTransfer();
\r
261 for (int i = 0; i < seqs.length; i++)
\r
263 cap.appendText(new jalview.util.Format("%-" + 15 + "s").form(
\r
264 seqs[i].getName()));
\r
265 cap.appendText(" " + seqstrings[i] + "\n");
\r
269 Desktop.addInternalFrame(cap, "Original Data",
\r
276 class PCAPrinter extends Thread implements Printable
\r
280 PrinterJob printJob = PrinterJob.getPrinterJob();
\r
281 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
\r
283 printJob.setPrintable(this, pf);
\r
285 if (printJob.printDialog())
\r
291 catch (Exception PrintException)
\r
293 PrintException.printStackTrace();
\r
298 public int print(Graphics pg, PageFormat pf, int pi)
\r
299 throws PrinterException
\r
301 pg.translate( (int) pf.getImageableX(), (int) pf.getImageableY());
\r
303 rc.drawBackground(pg, rc.bgColour);
\r
305 if (rc.drawAxes == true)
\r
311 return Printable.PAGE_EXISTS;
\r
313 return Printable.NO_SUCH_PAGE;
\r
322 * @param e DOCUMENT ME!
\r
324 public void eps_actionPerformed(ActionEvent e)
\r
326 makePCAImage(jalview.util.ImageMaker.EPS);
\r
332 * @param e DOCUMENT ME!
\r
334 public void png_actionPerformed(ActionEvent e)
\r
336 makePCAImage(jalview.util.ImageMaker.PNG);
\r
339 void makePCAImage(int type)
\r
341 int width = rc.getWidth();
\r
342 int height = rc.getHeight();
\r
344 jalview.util.ImageMaker im;
\r
346 if(type == jalview.util.ImageMaker.PNG)
\r
347 im = new jalview.util.ImageMaker(this,
\r
348 jalview.util.ImageMaker.PNG,
\r
349 "Make PNG image from PCA",
\r
353 im = new jalview.util.ImageMaker(this,
\r
354 jalview.util.ImageMaker.EPS,
\r
355 "Make EPS file from PCA",
\r
357 null, this.getTitle());
\r
359 if(im.getGraphics()!=null)
\r
361 rc.drawBackground(im.getGraphics(), Color.black);
\r
362 rc.drawScene(im.getGraphics());
\r
363 if (rc.drawAxes == true)
\r
365 rc.drawAxes(im.getGraphics());
\r