2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import java.awt.event.*;
25 import java.awt.print.*;
28 import jalview.analysis.*;
29 import jalview.datamodel.*;
30 import jalview.jbgui.*;
38 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();
247 Desktop.addInternalFrame(cap, "PCA details", 500, 500);
249 cap.setText(pca.getDetails());
252 public void showLabels_actionPerformed(ActionEvent e)
254 rc.showLabels(showLabels.getState());
257 public void print_actionPerformed(ActionEvent e)
259 PCAPrinter printer = new PCAPrinter();
263 public void originalSeqData_actionPerformed(ActionEvent e)
265 // this was cut'n'pasted from the equivalent TreePanel method - we should
266 // make this an abstract function of all jalview analysis windows
267 if (seqstrings == null)
269 jalview.bin.Cache.log
270 .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
273 // decide if av alignment is sufficiently different to original data to
274 // warrant a new window to be created
275 // create new alignmnt window with hidden regions (unhiding hidden regions
276 // yields unaligned seqs)
277 // or create a selection box around columns in alignment view
278 // test Alignment(SeqCigar[])
282 // we try to get the associated view's gap character
283 // but this may fail if the view was closed...
284 gc = av.getGapCharacter();
285 } catch (Exception ex)
289 Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(gc);
291 if (alAndColsel != null && alAndColsel[0] != null)
293 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
295 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
296 Alignment dataset = (av != null && av.getAlignment() != null) ? av
297 .getAlignment().getDataset() : null;
300 al.setDataset(dataset);
306 AlignFrame af = new AlignFrame(al,
307 (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
308 AlignFrame.DEFAULT_HEIGHT);
310 // >>>This is a fix for the moment, until a better solution is
312 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
314 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
317 Desktop.addInternalFrame(af, "Original Data for " + this.title,
318 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
322 * CutAndPasteTransfer cap = new CutAndPasteTransfer(); for (int i = 0; i <
323 * seqs.length; i++) { cap.appendText(new jalview.util.Format("%-" + 15 +
324 * "s").form( seqs[i].getName())); cap.appendText(" " + seqstrings[i] +
328 * Desktop.addInternalFrame(cap, "Original Data", 400, 400);
332 class PCAPrinter extends Thread implements Printable
336 PrinterJob printJob = PrinterJob.getPrinterJob();
337 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
339 printJob.setPrintable(this, pf);
341 if (printJob.printDialog())
346 } catch (Exception PrintException)
348 PrintException.printStackTrace();
353 public int print(Graphics pg, PageFormat pf, int pi)
354 throws PrinterException
356 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
358 rc.drawBackground(pg, rc.bgColour);
360 if (rc.drawAxes == true)
367 return Printable.PAGE_EXISTS;
371 return Printable.NO_SUCH_PAGE;
382 public void eps_actionPerformed(ActionEvent e)
384 makePCAImage(jalview.util.ImageMaker.EPS);
393 public void png_actionPerformed(ActionEvent e)
395 makePCAImage(jalview.util.ImageMaker.PNG);
398 void makePCAImage(int type)
400 int width = rc.getWidth();
401 int height = rc.getHeight();
403 jalview.util.ImageMaker im;
405 if (type == jalview.util.ImageMaker.PNG)
407 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
408 "Make PNG image from PCA", width, height, null, null);
412 im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
413 "Make EPS file from PCA", width, height, null, this
417 if (im.getGraphics() != null)
419 rc.drawBackground(im.getGraphics(), Color.black);
420 rc.drawScene(im.getGraphics());
421 if (rc.drawAxes == true)
423 rc.drawAxes(im.getGraphics());
429 public void viewMenu_menuSelected()
431 buildAssociatedViewMenu();
434 void buildAssociatedViewMenu()
436 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
437 .getSequenceSetId());
438 if (aps.length == 1 && rc.av == aps[0].av)
440 associateViewsMenu.setVisible(false);
444 associateViewsMenu.setVisible(true);
446 if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
448 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
451 associateViewsMenu.removeAll();
453 JRadioButtonMenuItem item;
454 ButtonGroup buttonGroup = new ButtonGroup();
455 int i, iSize = aps.length;
456 final PCAPanel thisPCAPanel = this;
457 for (i = 0; i < iSize; i++)
459 final AlignmentPanel ap = aps[i];
460 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
461 buttonGroup.add(item);
462 item.addActionListener(new ActionListener()
464 public void actionPerformed(ActionEvent evt)
466 rc.applyToAllViews = false;
469 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
473 associateViewsMenu.add(item);
476 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
478 buttonGroup.add(itemf);
480 itemf.setSelected(rc.applyToAllViews);
481 itemf.addActionListener(new ActionListener()
483 public void actionPerformed(ActionEvent evt)
485 rc.applyToAllViews = itemf.isSelected();
488 associateViewsMenu.add(itemf);