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
49 AlignmentView seqstrings;
54 * Creates a new PCAPanel object.
61 public PCAPanel(AlignmentPanel ap)
66 boolean sameLength = true;
68 seqstrings = av.getAlignmentView(av.getSelectionGroup() != null);
69 if (av.getSelectionGroup() == null)
71 seqs = av.alignment.getSequencesArray();
75 seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment);
77 SeqCigar sq[] = seqstrings.getSequences();
78 int length = sq[0].getWidth();
80 for (int i = 0; i < seqs.length; i++)
82 if (sq[i].getWidth() != length)
94 "The sequences must be aligned before calculating PCA.\n"
95 + "Try using the Pad function in the edit menu,\n"
96 + "or one of the multiple sequence alignment web services.",
97 "Sequences not aligned", JOptionPane.WARNING_MESSAGE);
103 .addInternalFrame(this, "Principal component analysis", 400,
106 PaintRefresher.Register(this, av.getSequenceSetId());
108 rc = new RotatableCanvas(ap);
109 this.getContentPane().add(rc, BorderLayout.CENTER);
110 Thread worker = new Thread(this);
114 public void bgcolour_actionPerformed(ActionEvent e)
116 Color col = JColorChooser.showDialog(this, "Select Background Colour",
133 pca = new PCA(seqstrings.getSequenceStrings(' '));
136 // Now find the component coordinates
139 while ((ii < seqs.length) && (seqs[ii] != null))
144 double[][] comps = new double[ii][ii];
146 for (int i = 0; i < ii; i++)
148 if (pca.getEigenvalue(i) > 1e-4)
150 comps[i] = pca.component(i);
155 xCombobox.setSelectedIndex(0);
156 yCombobox.setSelectedIndex(1);
157 zCombobox.setSelectedIndex(2);
159 top = pca.getM().rows - 1;
161 Vector points = new Vector();
162 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
164 for (int i = 0; i < pca.getM().rows; i++)
166 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
167 points.addElement(sp);
170 rc.setPoints(points, pca.getM().rows);
175 } catch (OutOfMemoryError er)
177 new OOMWarning("calculating PCA", er);
186 void doDimensionChange()
193 int dim1 = top - xCombobox.getSelectedIndex();
194 int dim2 = top - yCombobox.getSelectedIndex();
195 int dim3 = top - zCombobox.getSelectedIndex();
197 float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);
199 for (int i = 0; i < pca.getM().rows; i++)
201 ((SequencePoint) rc.points.elementAt(i)).coord = scores[i];
205 rc.rotmat.setIdentity();
207 rc.paint(rc.getGraphics());
216 protected void xCombobox_actionPerformed(ActionEvent e)
227 protected void yCombobox_actionPerformed(ActionEvent e)
238 protected void zCombobox_actionPerformed(ActionEvent e)
243 public void outputValues_actionPerformed(ActionEvent e)
245 CutAndPasteTransfer cap = new CutAndPasteTransfer();
246 Desktop.addInternalFrame(cap, "PCA details", 500, 500);
248 cap.setText(pca.getDetails());
251 public void showLabels_actionPerformed(ActionEvent e)
253 rc.showLabels(showLabels.getState());
256 public void print_actionPerformed(ActionEvent e)
258 PCAPrinter printer = new PCAPrinter();
262 public void originalSeqData_actionPerformed(ActionEvent e)
264 // this was cut'n'pasted from the equivalent TreePanel method - we should
265 // make this an abstract function of all jalview analysis windows
266 if (seqstrings == null)
268 jalview.bin.Cache.log
269 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
272 // decide if av alignment is sufficiently different to original data to
273 // warrant a new window to be created
274 // create new alignmnt window with hidden regions (unhiding hidden regions
275 // yields unaligned seqs)
276 // or create a selection box around columns in alignment view
277 // test Alignment(SeqCigar[])
281 // we try to get the associated view's gap character
282 // but this may fail if the view was closed...
283 gc = av.getGapCharacter();
284 } catch (Exception ex)
288 Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(gc);
290 if (alAndColsel != null && alAndColsel[0] != null)
292 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
294 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
295 Alignment dataset = (av != null && av.getAlignment() != null) ? av
296 .getAlignment().getDataset() : null;
299 al.setDataset(dataset);
305 AlignFrame af = new AlignFrame(al,
306 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
307 AlignFrame.DEFAULT_HEIGHT);
309 // >>>This is a fix for the moment, until a better solution is
311 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
313 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
316 Desktop.addInternalFrame(af, "Original Data for " + this.title,
317 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
321 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
322 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
323 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
326 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
330 class PCAPrinter extends Thread implements Printable
334 PrinterJob printJob = PrinterJob.getPrinterJob();
335 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
337 printJob.setPrintable(this, pf);
339 if (printJob.printDialog())
344 } catch (Exception PrintException)
346 PrintException.printStackTrace();
351 public int print(Graphics pg, PageFormat pf, int pi)
352 throws PrinterException
354 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
356 rc.drawBackground(pg, rc.bgColour);
358 if (rc.drawAxes == true)
365 return Printable.PAGE_EXISTS;
369 return Printable.NO_SUCH_PAGE;
380 public void eps_actionPerformed(ActionEvent e)
382 makePCAImage(jalview.util.ImageMaker.EPS);
391 public void png_actionPerformed(ActionEvent e)
393 makePCAImage(jalview.util.ImageMaker.PNG);
396 void makePCAImage(int type)
398 int width = rc.getWidth();
399 int height = rc.getHeight();
401 jalview.util.ImageMaker im;
403 if (type == jalview.util.ImageMaker.PNG)
405 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
406 "Make PNG image from PCA", width, height, null, null);
410 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
411 "Make EPS file from PCA", width, height, null, this
415 if (im.getGraphics() != null)
417 rc.drawBackground(im.getGraphics(), Color.black);
418 rc.drawScene(im.getGraphics());
419 if (rc.drawAxes == true)
421 rc.drawAxes(im.getGraphics());
427 public void viewMenu_menuSelected()
429 buildAssociatedViewMenu();
432 void buildAssociatedViewMenu()
434 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
435 .getSequenceSetId());
436 if (aps.length == 1 && rc.av == aps[0].av)
438 associateViewsMenu.setVisible(false);
442 associateViewsMenu.setVisible(true);
444 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
446 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
449 associateViewsMenu.removeAll();
451 JRadioButtonMenuItem item;
452 ButtonGroup buttonGroup = new ButtonGroup();
453 int i, iSize = aps.length;
454 final PCAPanel thisPCAPanel = this;
455 for (i = 0; i < iSize; i++)
457 final AlignmentPanel ap = aps[i];
458 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
459 buttonGroup.add(item);
460 item.addActionListener(new ActionListener()
462 public void actionPerformed(ActionEvent evt)
464 rc.applyToAllViews = false;
467 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
471 associateViewsMenu.add(item);
474 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
476 buttonGroup.add(itemf);
478 itemf.setSelected(rc.applyToAllViews);
479 itemf.addActionListener(new ActionListener()
481 public void actionPerformed(ActionEvent evt)
483 rc.applyToAllViews = itemf.isSelected();
486 associateViewsMenu.add(itemf);