2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 import jalview.analysis.*;
23 import jalview.datamodel.*;
25 import jalview.jbgui.*;
28 import java.awt.event.*;
32 import java.awt.print.*;
41 public class PCAPanel extends GPCAPanel implements Runnable
47 AlignmentView seqstrings;
51 * Creates a new PCAPanel object.
53 * @param av DOCUMENT ME!
54 * @param s DOCUMENT ME!
56 public PCAPanel(AlignViewport av)
60 boolean sameLength = true;
62 seqstrings = av.getAlignmentView(av.getSelectionGroup()!=null);
63 if(av.getSelectionGroup()==null)
65 seqs = av.alignment.getSequencesArray();
69 seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment);
71 SeqCigar sq[]=seqstrings.getSequences();
72 int length = sq[0].getWidth();
74 for (int i = 0; i < seqs.length; i++)
76 if (sq[i].getWidth() != length)
85 JOptionPane.showMessageDialog(Desktop.desktop,
86 "The sequences must be aligned before calculating PCA.\n" +
87 "Try using the Pad function in the edit menu,\n" +
88 "or one of the multiple sequence alignment web services.",
89 "Sequences not aligned",
90 JOptionPane.WARNING_MESSAGE);
96 Desktop.addInternalFrame(this, "Principal component analysis",
100 rc = new RotatableCanvas(av);
101 this.getContentPane().add(rc, BorderLayout.CENTER);
102 Thread worker = new Thread(this);
106 public void bgcolour_actionPerformed(ActionEvent e)
108 Color col = JColorChooser.showDialog(this, "Select Background Colour",
125 pca = new PCA(seqstrings.getSequenceStrings(' '));
128 // Now find the component coordinates
131 while ( (ii < seqs.length) && (seqs[ii] != null))
136 double[][] comps = new double[ii][ii];
138 for (int i = 0; i < ii; i++)
140 if (pca.getEigenvalue(i) > 1e-4)
142 comps[i] = pca.component(i);
147 xCombobox.setSelectedIndex(0);
148 yCombobox.setSelectedIndex(1);
149 zCombobox.setSelectedIndex(2);
151 top = pca.getM().rows - 1;
153 Vector points = new Vector();
154 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
156 for (int i = 0; i < pca.getM().rows; i++)
158 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
159 points.addElement(sp);
162 rc.setPoints(points, pca.getM().rows);
165 catch(OutOfMemoryError er)
166 { JOptionPane.showInternalMessageDialog(Desktop.desktop,
167 "Out of memory calculating PCA!!"
169 "\nSee help files for increasing Java Virtual Machine memory."
171 JOptionPane.WARNING_MESSAGE);
172 System.out.println("PCAPanel: "+er);
182 void doDimensionChange()
189 int dim1 = top - xCombobox.getSelectedIndex();
190 int dim2 = top - yCombobox.getSelectedIndex();
191 int dim3 = top - zCombobox.getSelectedIndex();
193 float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);
195 for (int i = 0; i < pca.getM().rows; i++)
197 ((SequencePoint) rc.points.elementAt(i)).coord = scores[i];
201 rc.rotmat.setIdentity();
203 rc.paint(rc.getGraphics());
209 * @param e DOCUMENT ME!
211 protected void xCombobox_actionPerformed(ActionEvent e)
219 * @param e DOCUMENT ME!
221 protected void yCombobox_actionPerformed(ActionEvent e)
229 * @param e DOCUMENT ME!
231 protected void zCombobox_actionPerformed(ActionEvent e)
237 public void outputValues_actionPerformed(ActionEvent e)
239 CutAndPasteTransfer cap = new CutAndPasteTransfer();
240 Desktop.addInternalFrame(cap, "PCA details", 500,
243 cap.setText(pca.getDetails());
246 public void showLabels_actionPerformed(ActionEvent e)
248 rc.showLabels(showLabels.getState());
251 public void print_actionPerformed(ActionEvent e)
253 PCAPrinter printer = new PCAPrinter();
257 public void originalSeqData_actionPerformed(ActionEvent e)
259 // this was cut'n'pasted from the equivalent TreePanel method - we should make this an abstract function of all jalview analysis windows
260 if (seqstrings==null)
262 jalview.bin.Cache.log.info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
265 // decide if av alignment is sufficiently different to original data to warrant a new window to be created
266 // create new alignmnt window with hidden regions (unhiding hidden regions yields unaligned seqs)
267 // or create a selection box around columns in alignment view
268 // test Alignment(SeqCigar[])
269 Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(av.
273 if (alAndColsel != null && alAndColsel[0]!=null)
275 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
277 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
278 Alignment dataset = av.getAlignment().getDataset();
281 al.setDataset(dataset);
287 AlignFrame af = new AlignFrame(al, (ColumnSelection) alAndColsel[1]);
289 //>>>This is a fix for the moment, until a better solution is found!!<<<
290 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
292 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
295 Desktop.addInternalFrame(af, "Original Data for " + this.title,
296 AlignFrame.NEW_WINDOW_WIDTH,
297 AlignFrame.NEW_WINDOW_HEIGHT);
300 /* CutAndPasteTransfer cap = new CutAndPasteTransfer();
301 for (int i = 0; i < seqs.length; i++)
303 cap.appendText(new jalview.util.Format("%-" + 15 + "s").form(
305 cap.appendText(" " + seqstrings[i] + "\n");
309 Desktop.addInternalFrame(cap, "Original Data",
316 class PCAPrinter extends Thread implements Printable
320 PrinterJob printJob = PrinterJob.getPrinterJob();
321 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
323 printJob.setPrintable(this, pf);
325 if (printJob.printDialog())
331 catch (Exception PrintException)
333 PrintException.printStackTrace();
338 public int print(Graphics pg, PageFormat pf, int pi)
339 throws PrinterException
341 pg.translate( (int) pf.getImageableX(), (int) pf.getImageableY());
343 rc.drawBackground(pg, rc.bgColour);
345 if (rc.drawAxes == true)
351 return Printable.PAGE_EXISTS;
353 return Printable.NO_SUCH_PAGE;
362 * @param e DOCUMENT ME!
364 public void eps_actionPerformed(ActionEvent e)
366 makePCAImage(jalview.util.ImageMaker.EPS);
372 * @param e DOCUMENT ME!
374 public void png_actionPerformed(ActionEvent e)
376 makePCAImage(jalview.util.ImageMaker.PNG);
379 void makePCAImage(int type)
381 int width = rc.getWidth();
382 int height = rc.getHeight();
384 jalview.util.ImageMaker im;
386 if(type == jalview.util.ImageMaker.PNG)
387 im = new jalview.util.ImageMaker(this,
388 jalview.util.ImageMaker.PNG,
389 "Make PNG image from PCA",
393 im = new jalview.util.ImageMaker(this,
394 jalview.util.ImageMaker.EPS,
395 "Make EPS file from PCA",
397 null, this.getTitle());
399 if(im.getGraphics()!=null)
401 rc.drawBackground(im.getGraphics(), Color.black);
402 rc.drawScene(im.getGraphics());
403 if (rc.drawAxes == true)
405 rc.drawAxes(im.getGraphics());