2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 import java.awt.event.*;
24 import java.awt.print.*;
27 import jalview.analysis.*;
28 import jalview.datamodel.*;
29 import jalview.jbgui.*;
37 public class PCAPanel extends GPCAPanel implements Runnable
50 AlignmentView seqstrings;
55 * use the identity matrix for calculating similarity between sequences.
57 private boolean useidentity=false;
60 * Creates a new PCAPanel object.
67 public PCAPanel(AlignmentPanel ap)
72 boolean sameLength = true;
74 seqstrings = av.getAlignmentView(av.getSelectionGroup() != null);
75 useidentity=av.getAlignment().isNucleotide();
76 if (av.getSelectionGroup() == null)
78 seqs = av.alignment.getSequencesArray();
82 seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment);
84 SeqCigar sq[] = seqstrings.getSequences();
85 int length = sq[0].getWidth();
87 for (int i = 0; i < seqs.length; i++)
89 if (sq[i].getWidth() != length)
101 "The sequences must be aligned before calculating PCA.\n"
102 + "Try using the Pad function in the edit menu,\n"
103 + "or one of the multiple sequence alignment web services.",
104 "Sequences not aligned", JOptionPane.WARNING_MESSAGE);
109 Desktop.addInternalFrame(this, "Principal component analysis", 400, 400);
111 PaintRefresher.Register(this, av.getSequenceSetId());
113 rc = new RotatableCanvas(ap);
114 this.getContentPane().add(rc, BorderLayout.CENTER);
115 Thread worker = new Thread(this);
119 public void bgcolour_actionPerformed(ActionEvent e)
121 Color col = JColorChooser.showDialog(this, "Select Background Colour",
138 pca = new PCA(seqstrings.getSequenceStrings(' '), useidentity);
141 // Now find the component coordinates
144 while ((ii < seqs.length) && (seqs[ii] != null))
149 double[][] comps = new double[ii][ii];
151 for (int i = 0; i < ii; i++)
153 if (pca.getEigenvalue(i) > 1e-4)
155 comps[i] = pca.component(i);
160 xCombobox.setSelectedIndex(0);
161 yCombobox.setSelectedIndex(1);
162 zCombobox.setSelectedIndex(2);
164 top = pca.getM().rows - 1;
166 Vector points = new Vector();
167 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
169 for (int i = 0; i < pca.getM().rows; i++)
171 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
172 points.addElement(sp);
175 rc.setPoints(points, pca.getM().rows);
180 } catch (OutOfMemoryError er)
182 new OOMWarning("calculating PCA", er);
191 void doDimensionChange()
198 int dim1 = top - xCombobox.getSelectedIndex();
199 int dim2 = top - yCombobox.getSelectedIndex();
200 int dim3 = top - zCombobox.getSelectedIndex();
202 float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);
204 for (int i = 0; i < pca.getM().rows; i++)
206 ((SequencePoint) rc.points.elementAt(i)).coord = scores[i];
210 rc.rotmat.setIdentity();
212 rc.paint(rc.getGraphics());
221 protected void xCombobox_actionPerformed(ActionEvent e)
232 protected void yCombobox_actionPerformed(ActionEvent e)
243 protected void zCombobox_actionPerformed(ActionEvent e)
248 public void outputValues_actionPerformed(ActionEvent e)
250 CutAndPasteTransfer cap = new CutAndPasteTransfer();
253 cap.setText(pca.getDetails());
254 Desktop.addInternalFrame(cap, "PCA details", 500, 500);
255 } catch (OutOfMemoryError oom)
257 new OOMWarning("opening PCA details", oom);
262 public void showLabels_actionPerformed(ActionEvent e)
264 rc.showLabels(showLabels.getState());
267 public void print_actionPerformed(ActionEvent e)
269 PCAPrinter printer = new PCAPrinter();
273 public void originalSeqData_actionPerformed(ActionEvent e)
275 // this was cut'n'pasted from the equivalent TreePanel method - we should
276 // make this an abstract function of all jalview analysis windows
277 if (seqstrings == null)
279 jalview.bin.Cache.log
280 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
283 // decide if av alignment is sufficiently different to original data to
284 // warrant a new window to be created
285 // create new alignmnt window with hidden regions (unhiding hidden regions
286 // yields unaligned seqs)
287 // or create a selection box around columns in alignment view
288 // test Alignment(SeqCigar[])
292 // we try to get the associated view's gap character
293 // but this may fail if the view was closed...
294 gc = av.getGapCharacter();
295 } catch (Exception ex)
299 Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(gc);
301 if (alAndColsel != null && alAndColsel[0] != null)
303 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
305 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
306 Alignment dataset = (av != null && av.getAlignment() != null) ? av
307 .getAlignment().getDataset() : null;
310 al.setDataset(dataset);
316 AlignFrame af = new AlignFrame(al,
317 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
318 AlignFrame.DEFAULT_HEIGHT);
320 // >>>This is a fix for the moment, until a better solution is
322 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
324 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
327 Desktop.addInternalFrame(af, "Original Data for " + this.title,
328 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
332 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
333 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
334 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
337 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
341 class PCAPrinter extends Thread implements Printable
345 PrinterJob printJob = PrinterJob.getPrinterJob();
346 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
348 printJob.setPrintable(this, pf);
350 if (printJob.printDialog())
355 } catch (Exception PrintException)
357 PrintException.printStackTrace();
362 public int print(Graphics pg, PageFormat pf, int pi)
363 throws PrinterException
365 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
367 rc.drawBackground(pg, rc.bgColour);
369 if (rc.drawAxes == true)
376 return Printable.PAGE_EXISTS;
380 return Printable.NO_SUCH_PAGE;
391 public void eps_actionPerformed(ActionEvent e)
393 makePCAImage(jalview.util.ImageMaker.EPS);
402 public void png_actionPerformed(ActionEvent e)
404 makePCAImage(jalview.util.ImageMaker.PNG);
407 void makePCAImage(int type)
409 int width = rc.getWidth();
410 int height = rc.getHeight();
412 jalview.util.ImageMaker im;
414 if (type == jalview.util.ImageMaker.PNG)
416 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
417 "Make PNG image from PCA", width, height, null, null);
421 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
422 "Make EPS file from PCA", width, height, null,
426 if (im.getGraphics() != null)
428 rc.drawBackground(im.getGraphics(), Color.black);
429 rc.drawScene(im.getGraphics());
430 if (rc.drawAxes == true)
432 rc.drawAxes(im.getGraphics());
438 public void viewMenu_menuSelected()
440 buildAssociatedViewMenu();
443 void buildAssociatedViewMenu()
445 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
446 .getSequenceSetId());
447 if (aps.length == 1 && rc.av == aps[0].av)
449 associateViewsMenu.setVisible(false);
453 associateViewsMenu.setVisible(true);
455 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
457 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
460 associateViewsMenu.removeAll();
462 JRadioButtonMenuItem item;
463 ButtonGroup buttonGroup = new ButtonGroup();
464 int i, iSize = aps.length;
465 final PCAPanel thisPCAPanel = this;
466 for (i = 0; i < iSize; i++)
468 final AlignmentPanel ap = aps[i];
469 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
470 buttonGroup.add(item);
471 item.addActionListener(new ActionListener()
473 public void actionPerformed(ActionEvent evt)
475 rc.applyToAllViews = false;
478 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
482 associateViewsMenu.add(item);
485 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
487 buttonGroup.add(itemf);
489 itemf.setSelected(rc.applyToAllViews);
490 itemf.addActionListener(new ActionListener()
492 public void actionPerformed(ActionEvent evt)
494 rc.applyToAllViews = itemf.isSelected();
497 associateViewsMenu.add(itemf);
505 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
508 protected void outputPoints_actionPerformed(ActionEvent e)
510 CutAndPasteTransfer cap = new CutAndPasteTransfer();
513 cap.setText(getPointsasCsv(false));
514 Desktop.addInternalFrame(cap, "Points for " + getTitle(), 500, 500);
515 } catch (OutOfMemoryError oom)
517 new OOMWarning("exporting PCA points", oom);
522 private String getPointsasCsv(boolean transformed)
524 StringBuffer csv = new StringBuffer();
525 csv.append("\"Sequence\"");
529 csv.append(xCombobox.getSelectedIndex());
531 csv.append(yCombobox.getSelectedIndex());
533 csv.append(zCombobox.getSelectedIndex());
537 for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
543 for (int s = 0; s < seqs.length; s++)
545 csv.append("\"" + seqs[s].getName() + "\"");
549 // output pca in correct order
550 fl = pca.component(s);
551 for (int d = fl.length - 1; d >= 0; d--)
559 // output current x,y,z coords for points
560 fl = rc.getPointPosition(s);
561 for (int d = 0; d < fl.length; d++)
569 return csv.toString();
576 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
579 protected void outputProjPoints_actionPerformed(ActionEvent e)
581 CutAndPasteTransfer cap = new CutAndPasteTransfer();
584 cap.setText(getPointsasCsv(true));
585 Desktop.addInternalFrame(cap, "Transformed points for " + getTitle(),
587 } catch (OutOfMemoryError oom)
589 new OOMWarning("exporting transformed PCA points", oom);