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 * Creates a new PCAPanel object.
62 public PCAPanel(AlignmentPanel ap)
67 boolean sameLength = true;
69 seqstrings = av.getAlignmentView(av.getSelectionGroup() != null);
70 if (av.getSelectionGroup() == null)
72 seqs = av.getAlignment().getSequencesArray();
76 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
78 SeqCigar sq[] = seqstrings.getSequences();
79 int length = sq[0].getWidth();
81 for (int i = 0; i < seqs.length; i++)
83 if (sq[i].getWidth() != length)
95 "The sequences must be aligned before calculating PCA.\n"
96 + "Try using the Pad function in the edit menu,\n"
97 + "or one of the multiple sequence alignment web services.",
98 "Sequences not aligned", JOptionPane.WARNING_MESSAGE);
103 Desktop.addInternalFrame(this, "Principal component analysis", 400, 400);
105 PaintRefresher.Register(this, av.getSequenceSetId());
107 rc = new RotatableCanvas(ap);
108 this.getContentPane().add(rc, BorderLayout.CENTER);
109 Thread worker = new Thread(this);
113 public void bgcolour_actionPerformed(ActionEvent e)
115 Color col = JColorChooser.showDialog(this, "Select Background Colour",
132 pca = new PCA(seqstrings.getSequenceStrings(' '));
135 // Now find the component coordinates
138 while ((ii < seqs.length) && (seqs[ii] != null))
143 double[][] comps = new double[ii][ii];
145 for (int i = 0; i < ii; i++)
147 if (pca.getEigenvalue(i) > 1e-4)
149 comps[i] = pca.component(i);
154 xCombobox.setSelectedIndex(0);
155 yCombobox.setSelectedIndex(1);
156 zCombobox.setSelectedIndex(2);
158 top = pca.getM().rows - 1;
160 Vector points = new Vector();
161 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
163 for (int i = 0; i < pca.getM().rows; i++)
165 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
166 points.addElement(sp);
169 rc.setPoints(points, pca.getM().rows);
174 } catch (OutOfMemoryError er)
176 new OOMWarning("calculating PCA", er);
185 void doDimensionChange()
192 int dim1 = top - xCombobox.getSelectedIndex();
193 int dim2 = top - yCombobox.getSelectedIndex();
194 int dim3 = top - zCombobox.getSelectedIndex();
196 float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);
198 for (int i = 0; i < pca.getM().rows; i++)
200 ((SequencePoint) rc.points.elementAt(i)).coord = scores[i];
204 rc.rotmat.setIdentity();
206 rc.paint(rc.getGraphics());
215 protected void xCombobox_actionPerformed(ActionEvent e)
226 protected void yCombobox_actionPerformed(ActionEvent e)
237 protected void zCombobox_actionPerformed(ActionEvent e)
242 public void outputValues_actionPerformed(ActionEvent e)
244 CutAndPasteTransfer cap = new CutAndPasteTransfer();
247 cap.setText(pca.getDetails());
248 Desktop.addInternalFrame(cap, "PCA details", 500, 500);
249 } catch (OutOfMemoryError oom)
251 new OOMWarning("opening PCA details", oom);
256 public void showLabels_actionPerformed(ActionEvent e)
258 rc.showLabels(showLabels.getState());
261 public void print_actionPerformed(ActionEvent e)
263 PCAPrinter printer = new PCAPrinter();
267 public void originalSeqData_actionPerformed(ActionEvent e)
269 // this was cut'n'pasted from the equivalent TreePanel method - we should
270 // make this an abstract function of all jalview analysis windows
271 if (seqstrings == null)
273 jalview.bin.Cache.log
274 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
277 // decide if av alignment is sufficiently different to original data to
278 // warrant a new window to be created
279 // create new alignmnt window with hidden regions (unhiding hidden regions
280 // yields unaligned seqs)
281 // or create a selection box around columns in alignment view
282 // test Alignment(SeqCigar[])
286 // we try to get the associated view's gap character
287 // but this may fail if the view was closed...
288 gc = av.getGapCharacter();
289 } catch (Exception ex)
293 Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(gc);
295 if (alAndColsel != null && alAndColsel[0] != null)
297 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
299 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
300 Alignment dataset = (av != null && av.getAlignment() != null) ? av
301 .getAlignment().getDataset() : null;
304 al.setDataset(dataset);
310 AlignFrame af = new AlignFrame(al,
311 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
312 AlignFrame.DEFAULT_HEIGHT);
314 // >>>This is a fix for the moment, until a better solution is
316 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
318 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
321 Desktop.addInternalFrame(af, "Original Data for " + this.title,
322 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
326 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
327 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
328 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
331 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
335 class PCAPrinter extends Thread implements Printable
339 PrinterJob printJob = PrinterJob.getPrinterJob();
340 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
342 printJob.setPrintable(this, pf);
344 if (printJob.printDialog())
349 } catch (Exception PrintException)
351 PrintException.printStackTrace();
356 public int print(Graphics pg, PageFormat pf, int pi)
357 throws PrinterException
359 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
361 rc.drawBackground(pg, rc.bgColour);
363 if (rc.drawAxes == true)
370 return Printable.PAGE_EXISTS;
374 return Printable.NO_SUCH_PAGE;
385 public void eps_actionPerformed(ActionEvent e)
387 makePCAImage(jalview.util.ImageMaker.EPS);
396 public void png_actionPerformed(ActionEvent e)
398 makePCAImage(jalview.util.ImageMaker.PNG);
401 void makePCAImage(int type)
403 int width = rc.getWidth();
404 int height = rc.getHeight();
406 jalview.util.ImageMaker im;
408 if (type == jalview.util.ImageMaker.PNG)
410 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
411 "Make PNG image from PCA", width, height, null, null);
415 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
416 "Make EPS file from PCA", width, height, null,
420 if (im.getGraphics() != null)
422 rc.drawBackground(im.getGraphics(), Color.black);
423 rc.drawScene(im.getGraphics());
424 if (rc.drawAxes == true)
426 rc.drawAxes(im.getGraphics());
432 public void viewMenu_menuSelected()
434 buildAssociatedViewMenu();
437 void buildAssociatedViewMenu()
439 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
440 .getSequenceSetId());
441 if (aps.length == 1 && rc.av == aps[0].av)
443 associateViewsMenu.setVisible(false);
447 associateViewsMenu.setVisible(true);
449 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
451 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
454 associateViewsMenu.removeAll();
456 JRadioButtonMenuItem item;
457 ButtonGroup buttonGroup = new ButtonGroup();
458 int i, iSize = aps.length;
459 final PCAPanel thisPCAPanel = this;
460 for (i = 0; i < iSize; i++)
462 final AlignmentPanel ap = aps[i];
463 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
464 buttonGroup.add(item);
465 item.addActionListener(new ActionListener()
467 public void actionPerformed(ActionEvent evt)
469 rc.applyToAllViews = false;
472 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
476 associateViewsMenu.add(item);
479 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
481 buttonGroup.add(itemf);
483 itemf.setSelected(rc.applyToAllViews);
484 itemf.addActionListener(new ActionListener()
486 public void actionPerformed(ActionEvent evt)
488 rc.applyToAllViews = itemf.isSelected();
491 associateViewsMenu.add(itemf);
499 * jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent
502 protected void outputPoints_actionPerformed(ActionEvent e)
504 CutAndPasteTransfer cap = new CutAndPasteTransfer();
507 cap.setText(getPointsasCsv(false));
508 Desktop.addInternalFrame(cap, "Points for " + getTitle(), 500, 500);
509 } catch (OutOfMemoryError oom)
511 new OOMWarning("exporting PCA points", oom);
516 private String getPointsasCsv(boolean transformed)
518 StringBuffer csv = new StringBuffer();
519 csv.append("\"Sequence\"");
523 csv.append(xCombobox.getSelectedIndex());
525 csv.append(yCombobox.getSelectedIndex());
527 csv.append(zCombobox.getSelectedIndex());
531 for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
537 for (int s = 0; s < seqs.length; s++)
539 csv.append("\"" + seqs[s].getName() + "\"");
543 // output pca in correct order
544 fl = pca.component(s);
545 for (int d = fl.length - 1; d >= 0; d--)
553 // output current x,y,z coords for points
554 fl = rc.getPointPosition(s);
555 for (int d = 0; d < fl.length; d++)
563 return csv.toString();
570 * jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event
573 protected void outputProjPoints_actionPerformed(ActionEvent e)
575 CutAndPasteTransfer cap = new CutAndPasteTransfer();
578 cap.setText(getPointsasCsv(true));
579 Desktop.addInternalFrame(cap, "Transformed points for " + getTitle(),
581 } catch (OutOfMemoryError oom)
583 new OOMWarning("exporting transformed PCA points", oom);