2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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 nucleotide=false;
60 * Creates a new PCAPanel object.
67 public PCAPanel(AlignmentPanel ap)
72 boolean sameLength = true;
74 seqstrings = av.getAlignmentView(av.getSelectionGroup() != null);
75 nucleotide=av.getAlignment().isNucleotide();
76 if (av.getSelectionGroup() == null)
78 seqs = av.getAlignment().getSequencesArray();
82 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
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 PaintRefresher.Register(this, av.getSequenceSetId());
111 rc = new RotatableCanvas(ap);
112 this.getContentPane().add(rc, BorderLayout.CENTER);
113 Thread worker = new Thread(this);
117 public void bgcolour_actionPerformed(ActionEvent e)
119 Color col = JColorChooser.showDialog(this, "Select Background Colour",
136 calcSettings.setEnabled(false);
137 pca = new PCA(seqstrings.getSequenceStrings(' '), nucleotide);
140 // Now find the component coordinates
143 while ((ii < seqs.length) && (seqs[ii] != null))
148 double[][] comps = new double[ii][ii];
150 for (int i = 0; i < ii; i++)
152 if (pca.getEigenvalue(i) > 1e-4)
154 comps[i] = pca.component(i);
159 xCombobox.setSelectedIndex(0);
160 yCombobox.setSelectedIndex(1);
161 zCombobox.setSelectedIndex(2);
163 top = pca.getM().rows - 1;
165 Vector points = new Vector();
166 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
168 for (int i = 0; i < pca.getM().rows; i++)
170 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
171 points.addElement(sp);
174 rc.setPoints(points, pca.getM().rows);
176 nuclSetting.setSelected(nucleotide);
177 protSetting.setSelected(!nucleotide);
179 } catch (OutOfMemoryError er)
181 new OOMWarning("calculating PCA", er);
184 calcSettings.setEnabled(true);
186 if (getParent()==null)
189 Desktop.addInternalFrame(this, "Principal component analysis", 475, 450);
193 protected void nuclSetting_actionPerfomed(ActionEvent arg0)
196 Thread worker = new Thread(this);
200 protected void protSetting_actionPerfomed(ActionEvent arg0)
203 Thread worker = new Thread(this);
209 void doDimensionChange()
216 int dim1 = top - xCombobox.getSelectedIndex();
217 int dim2 = top - yCombobox.getSelectedIndex();
218 int dim3 = top - zCombobox.getSelectedIndex();
220 float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);
222 for (int i = 0; i < pca.getM().rows; i++)
224 ((SequencePoint) rc.points.elementAt(i)).coord = scores[i];
228 rc.rotmat.setIdentity();
230 rc.paint(rc.getGraphics());
239 protected void xCombobox_actionPerformed(ActionEvent e)
250 protected void yCombobox_actionPerformed(ActionEvent e)
261 protected void zCombobox_actionPerformed(ActionEvent e)
266 public void outputValues_actionPerformed(ActionEvent e)
268 CutAndPasteTransfer cap = new CutAndPasteTransfer();
271 cap.setText(pca.getDetails());
272 Desktop.addInternalFrame(cap, "PCA details", 500, 500);
273 } catch (OutOfMemoryError oom)
275 new OOMWarning("opening PCA details", oom);
280 public void showLabels_actionPerformed(ActionEvent e)
282 rc.showLabels(showLabels.getState());
285 public void print_actionPerformed(ActionEvent e)
287 PCAPrinter printer = new PCAPrinter();
291 public void originalSeqData_actionPerformed(ActionEvent e)
293 // this was cut'n'pasted from the equivalent TreePanel method - we should
294 // make this an abstract function of all jalview analysis windows
295 if (seqstrings == null)
297 jalview.bin.Cache.log
298 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
301 // decide if av alignment is sufficiently different to original data to
302 // warrant a new window to be created
303 // create new alignmnt window with hidden regions (unhiding hidden regions
304 // yields unaligned seqs)
305 // or create a selection box around columns in alignment view
306 // test Alignment(SeqCigar[])
310 // we try to get the associated view's gap character
311 // but this may fail if the view was closed...
312 gc = av.getGapCharacter();
313 } catch (Exception ex)
317 Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(gc);
319 if (alAndColsel != null && alAndColsel[0] != null)
321 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
323 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
324 Alignment dataset = (av != null && av.getAlignment() != null) ? av
325 .getAlignment().getDataset() : null;
328 al.setDataset(dataset);
334 AlignFrame af = new AlignFrame(al,
335 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
336 AlignFrame.DEFAULT_HEIGHT);
338 // >>>This is a fix for the moment, until a better solution is
340 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
342 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
345 Desktop.addInternalFrame(af, "Original Data for " + this.title,
346 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
350 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
351 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
352 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
355 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
359 class PCAPrinter extends Thread implements Printable
363 PrinterJob printJob = PrinterJob.getPrinterJob();
364 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
366 printJob.setPrintable(this, pf);
368 if (printJob.printDialog())
373 } catch (Exception PrintException)
375 PrintException.printStackTrace();
380 public int print(Graphics pg, PageFormat pf, int pi)
381 throws PrinterException
383 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
385 rc.drawBackground(pg, rc.bgColour);
387 if (rc.drawAxes == true)
394 return Printable.PAGE_EXISTS;
398 return Printable.NO_SUCH_PAGE;
409 public void eps_actionPerformed(ActionEvent e)
411 makePCAImage(jalview.util.ImageMaker.EPS);
420 public void png_actionPerformed(ActionEvent e)
422 makePCAImage(jalview.util.ImageMaker.PNG);
425 void makePCAImage(int type)
427 int width = rc.getWidth();
428 int height = rc.getHeight();
430 jalview.util.ImageMaker im;
432 if (type == jalview.util.ImageMaker.PNG)
434 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
435 "Make PNG image from PCA", width, height, null, null);
439 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
440 "Make EPS file from PCA", width, height, null,
444 if (im.getGraphics() != null)
446 rc.drawBackground(im.getGraphics(), Color.black);
447 rc.drawScene(im.getGraphics());
448 if (rc.drawAxes == true)
450 rc.drawAxes(im.getGraphics());
456 public void viewMenu_menuSelected()
458 buildAssociatedViewMenu();
461 void buildAssociatedViewMenu()
463 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
464 .getSequenceSetId());
465 if (aps.length == 1 && rc.av == aps[0].av)
467 associateViewsMenu.setVisible(false);
471 associateViewsMenu.setVisible(true);
473 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
475 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
478 associateViewsMenu.removeAll();
480 JRadioButtonMenuItem item;
481 ButtonGroup buttonGroup = new ButtonGroup();
482 int i, iSize = aps.length;
483 final PCAPanel thisPCAPanel = this;
484 for (i = 0; i < iSize; i++)
486 final AlignmentPanel ap = aps[i];
487 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
488 buttonGroup.add(item);
489 item.addActionListener(new ActionListener()
491 public void actionPerformed(ActionEvent evt)
493 rc.applyToAllViews = false;
496 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
500 associateViewsMenu.add(item);
503 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
505 buttonGroup.add(itemf);
507 itemf.setSelected(rc.applyToAllViews);
508 itemf.addActionListener(new ActionListener()
510 public void actionPerformed(ActionEvent evt)
512 rc.applyToAllViews = itemf.isSelected();
515 associateViewsMenu.add(itemf);
523 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
526 protected void outputPoints_actionPerformed(ActionEvent e)
528 CutAndPasteTransfer cap = new CutAndPasteTransfer();
531 cap.setText(getPointsasCsv(false));
532 Desktop.addInternalFrame(cap, "Points for " + getTitle(), 500, 500);
533 } catch (OutOfMemoryError oom)
535 new OOMWarning("exporting PCA points", oom);
540 private String getPointsasCsv(boolean transformed)
542 StringBuffer csv = new StringBuffer();
543 csv.append("\"Sequence\"");
547 csv.append(xCombobox.getSelectedIndex());
549 csv.append(yCombobox.getSelectedIndex());
551 csv.append(zCombobox.getSelectedIndex());
555 for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
561 for (int s = 0; s < seqs.length; s++)
563 csv.append("\"" + seqs[s].getName() + "\"");
567 // output pca in correct order
568 fl = pca.component(s);
569 for (int d = fl.length - 1; d >= 0; d--)
577 // output current x,y,z coords for points
578 fl = rc.getPointPosition(s);
579 for (int d = 0; d < fl.length; d++)
587 return csv.toString();
594 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
597 protected void outputProjPoints_actionPerformed(ActionEvent e)
599 CutAndPasteTransfer cap = new CutAndPasteTransfer();
602 cap.setText(getPointsasCsv(true));
603 Desktop.addInternalFrame(cap, "Transformed points for " + getTitle(),
605 } catch (OutOfMemoryError oom)
607 new OOMWarning("exporting transformed PCA points", oom);
614 protected void resetButton_actionPerformed(ActionEvent e)
617 top=0; // ugly - prevents dimensionChanged events from being processed
618 xCombobox.setSelectedIndex(0);
619 yCombobox.setSelectedIndex(1);
621 zCombobox.setSelectedIndex(2);