2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 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 * 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.alignment.getSequencesArray();
76 seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment);
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);
104 .addInternalFrame(this, "Principal component analysis", 400,
107 PaintRefresher.Register(this, av.getSequenceSetId());
109 rc = new RotatableCanvas(ap);
110 this.getContentPane().add(rc, BorderLayout.CENTER);
111 Thread worker = new Thread(this);
115 public void bgcolour_actionPerformed(ActionEvent e)
117 Color col = JColorChooser.showDialog(this, "Select Background Colour",
134 pca = new PCA(seqstrings.getSequenceStrings(' '));
137 // Now find the component coordinates
140 while ((ii < seqs.length) && (seqs[ii] != null))
145 double[][] comps = new double[ii][ii];
147 for (int i = 0; i < ii; i++)
149 if (pca.getEigenvalue(i) > 1e-4)
151 comps[i] = pca.component(i);
156 xCombobox.setSelectedIndex(0);
157 yCombobox.setSelectedIndex(1);
158 zCombobox.setSelectedIndex(2);
160 top = pca.getM().rows - 1;
162 Vector points = new Vector();
163 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
165 for (int i = 0; i < pca.getM().rows; i++)
167 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
168 points.addElement(sp);
171 rc.setPoints(points, pca.getM().rows);
176 } catch (OutOfMemoryError er)
178 new OOMWarning("calculating PCA", er);
187 void doDimensionChange()
194 int dim1 = top - xCombobox.getSelectedIndex();
195 int dim2 = top - yCombobox.getSelectedIndex();
196 int dim3 = top - zCombobox.getSelectedIndex();
198 float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);
200 for (int i = 0; i < pca.getM().rows; i++)
202 ((SequencePoint) rc.points.elementAt(i)).coord = scores[i];
206 rc.rotmat.setIdentity();
208 rc.paint(rc.getGraphics());
217 protected void xCombobox_actionPerformed(ActionEvent e)
228 protected void yCombobox_actionPerformed(ActionEvent e)
239 protected void zCombobox_actionPerformed(ActionEvent e)
244 public void outputValues_actionPerformed(ActionEvent e)
246 CutAndPasteTransfer cap = new CutAndPasteTransfer();
248 cap.setText(pca.getDetails());
249 Desktop.addInternalFrame(cap, "PCA details", 500, 500);
250 } catch (OutOfMemoryError oom)
252 new OOMWarning("opening PCA details",oom);
257 public void showLabels_actionPerformed(ActionEvent e)
259 rc.showLabels(showLabels.getState());
262 public void print_actionPerformed(ActionEvent e)
264 PCAPrinter printer = new PCAPrinter();
268 public void originalSeqData_actionPerformed(ActionEvent e)
270 // this was cut'n'pasted from the equivalent TreePanel method - we should
271 // make this an abstract function of all jalview analysis windows
272 if (seqstrings == null)
274 jalview.bin.Cache.log
275 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
278 // decide if av alignment is sufficiently different to original data to
279 // warrant a new window to be created
280 // create new alignmnt window with hidden regions (unhiding hidden regions
281 // yields unaligned seqs)
282 // or create a selection box around columns in alignment view
283 // test Alignment(SeqCigar[])
287 // we try to get the associated view's gap character
288 // but this may fail if the view was closed...
289 gc = av.getGapCharacter();
290 } catch (Exception ex)
294 Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(gc);
296 if (alAndColsel != null && alAndColsel[0] != null)
298 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
300 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
301 Alignment dataset = (av != null && av.getAlignment() != null) ? av
302 .getAlignment().getDataset() : null;
305 al.setDataset(dataset);
311 AlignFrame af = new AlignFrame(al,
312 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
313 AlignFrame.DEFAULT_HEIGHT);
315 // >>>This is a fix for the moment, until a better solution is
317 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
319 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
322 Desktop.addInternalFrame(af, "Original Data for " + this.title,
323 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
327 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
328 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
329 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
332 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
336 class PCAPrinter extends Thread implements Printable
340 PrinterJob printJob = PrinterJob.getPrinterJob();
341 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
343 printJob.setPrintable(this, pf);
345 if (printJob.printDialog())
350 } catch (Exception PrintException)
352 PrintException.printStackTrace();
357 public int print(Graphics pg, PageFormat pf, int pi)
358 throws PrinterException
360 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
362 rc.drawBackground(pg, rc.bgColour);
364 if (rc.drawAxes == true)
371 return Printable.PAGE_EXISTS;
375 return Printable.NO_SUCH_PAGE;
386 public void eps_actionPerformed(ActionEvent e)
388 makePCAImage(jalview.util.ImageMaker.EPS);
397 public void png_actionPerformed(ActionEvent e)
399 makePCAImage(jalview.util.ImageMaker.PNG);
402 void makePCAImage(int type)
404 int width = rc.getWidth();
405 int height = rc.getHeight();
407 jalview.util.ImageMaker im;
409 if (type == jalview.util.ImageMaker.PNG)
411 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
412 "Make PNG image from PCA", width, height, null, null);
416 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
417 "Make EPS file from PCA", width, height, null, this
421 if (im.getGraphics() != null)
423 rc.drawBackground(im.getGraphics(), Color.black);
424 rc.drawScene(im.getGraphics());
425 if (rc.drawAxes == true)
427 rc.drawAxes(im.getGraphics());
433 public void viewMenu_menuSelected()
435 buildAssociatedViewMenu();
438 void buildAssociatedViewMenu()
440 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
441 .getSequenceSetId());
442 if (aps.length == 1 && rc.av == aps[0].av)
444 associateViewsMenu.setVisible(false);
448 associateViewsMenu.setVisible(true);
450 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
452 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
455 associateViewsMenu.removeAll();
457 JRadioButtonMenuItem item;
458 ButtonGroup buttonGroup = new ButtonGroup();
459 int i, iSize = aps.length;
460 final PCAPanel thisPCAPanel = this;
461 for (i = 0; i < iSize; i++)
463 final AlignmentPanel ap = aps[i];
464 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
465 buttonGroup.add(item);
466 item.addActionListener(new ActionListener()
468 public void actionPerformed(ActionEvent evt)
470 rc.applyToAllViews = false;
473 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
477 associateViewsMenu.add(item);
480 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
482 buttonGroup.add(itemf);
484 itemf.setSelected(rc.applyToAllViews);
485 itemf.addActionListener(new ActionListener()
487 public void actionPerformed(ActionEvent evt)
489 rc.applyToAllViews = itemf.isSelected();
492 associateViewsMenu.add(itemf);
496 * @see jalview.jbgui.GPCAPanel#outputPoints_actionPerformed(java.awt.event.ActionEvent)
498 protected void outputPoints_actionPerformed(ActionEvent e)
500 CutAndPasteTransfer cap = new CutAndPasteTransfer();
502 cap.setText(getPointsasCsv(false));
503 Desktop.addInternalFrame(cap, "Points for "+getTitle(), 500, 500);
504 } catch (OutOfMemoryError oom)
506 new OOMWarning("exporting PCA points",oom);
511 private String getPointsasCsv(boolean transformed)
513 StringBuffer csv = new StringBuffer();
514 csv.append("\"Sequence\"");
517 csv.append(xCombobox.getSelectedIndex());
519 csv.append(yCombobox.getSelectedIndex());
521 csv.append(zCombobox.getSelectedIndex());
523 for (int d=1,dmax=pca.component(1).length;d<=dmax;d++)
529 for (int s=0;s<seqs.length;s++)
531 csv.append("\""+seqs[s].getName()+"\"");
534 // output pca in correct order
535 fl = pca.component(s);
536 for (int d=fl.length-1; d>=0;d--)
542 // output current x,y,z coords for points
543 fl = rc.getPointPosition(s);
544 for (int d=0; d<fl.length;d++)
552 return csv.toString();
556 * @see jalview.jbgui.GPCAPanel#outputProjPoints_actionPerformed(java.awt.event.ActionEvent)
558 protected void outputProjPoints_actionPerformed(ActionEvent e)
560 CutAndPasteTransfer cap = new CutAndPasteTransfer();
562 cap.setText(getPointsasCsv(true));
563 Desktop.addInternalFrame(cap, "Transformed points for "+getTitle(), 500, 500);
564 } catch (OutOfMemoryError oom)
566 new OOMWarning("exporting transformed PCA points",oom);