2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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.*;
39 extends GPCAPanel implements Runnable
46 AlignmentView seqstrings;
50 * Creates a new PCAPanel object.
52 * @param av DOCUMENT ME!
53 * @param s DOCUMENT ME!
55 public PCAPanel(AlignmentPanel ap)
60 boolean sameLength = true;
62 seqstrings = av.getAlignmentView(av.getSelectionGroup() != null);
63 if (av.getSelectionGroup() == null)
65 seqs = av.alignment.getSequencesArray();
69 seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment);
71 SeqCigar sq[] = seqstrings.getSequences();
72 int length = sq[0].getWidth();
74 for (int i = 0; i < seqs.length; i++)
76 if (sq[i].getWidth() != length)
85 JOptionPane.showMessageDialog(Desktop.desktop,
86 "The sequences must be aligned before calculating PCA.\n" +
87 "Try using the Pad function in the edit menu,\n" +
88 "or one of the multiple sequence alignment web services.",
89 "Sequences not aligned",
90 JOptionPane.WARNING_MESSAGE);
95 Desktop.addInternalFrame(this, "Principal component analysis",
98 PaintRefresher.Register(this, av.getSequenceSetId());
100 rc = new RotatableCanvas(ap);
101 this.getContentPane().add(rc, BorderLayout.CENTER);
102 Thread worker = new Thread(this);
106 public void bgcolour_actionPerformed(ActionEvent e)
108 Color col = JColorChooser.showDialog(this, "Select Background Colour",
125 pca = new PCA(seqstrings.getSequenceStrings(' '));
128 // Now find the component coordinates
131 while ( (ii < seqs.length) && (seqs[ii] != null))
136 double[][] comps = new double[ii][ii];
138 for (int i = 0; i < ii; i++)
140 if (pca.getEigenvalue(i) > 1e-4)
142 comps[i] = pca.component(i);
147 xCombobox.setSelectedIndex(0);
148 yCombobox.setSelectedIndex(1);
149 zCombobox.setSelectedIndex(2);
151 top = pca.getM().rows - 1;
153 Vector points = new Vector();
154 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
156 for (int i = 0; i < pca.getM().rows; i++)
158 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
159 points.addElement(sp);
162 rc.setPoints(points, pca.getM().rows);
168 catch (OutOfMemoryError er)
170 JOptionPane.showInternalMessageDialog(Desktop.desktop,
171 "Out of memory calculating PCA!!"
173 "\nSee help files for increasing Java Virtual Machine memory."
175 JOptionPane.WARNING_MESSAGE);
176 System.out.println("PCAPanel: " + 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());
213 * @param e DOCUMENT ME!
215 protected void xCombobox_actionPerformed(ActionEvent e)
223 * @param e DOCUMENT ME!
225 protected void yCombobox_actionPerformed(ActionEvent e)
233 * @param e DOCUMENT ME!
235 protected void zCombobox_actionPerformed(ActionEvent e)
240 public void outputValues_actionPerformed(ActionEvent e)
242 CutAndPasteTransfer cap = new CutAndPasteTransfer();
243 Desktop.addInternalFrame(cap, "PCA details", 500,
246 cap.setText(pca.getDetails());
249 public void showLabels_actionPerformed(ActionEvent e)
251 rc.showLabels(showLabels.getState());
254 public void print_actionPerformed(ActionEvent e)
256 PCAPrinter printer = new PCAPrinter();
260 public void originalSeqData_actionPerformed(ActionEvent e)
262 // this was cut'n'pasted from the equivalent TreePanel method - we should make this an abstract function of all jalview analysis windows
263 if (seqstrings == null)
265 jalview.bin.Cache.log.info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
268 // decide if av alignment is sufficiently different to original data to warrant a new window to be created
269 // create new alignmnt window with hidden regions (unhiding hidden regions yields unaligned seqs)
270 // or create a selection box around columns in alignment view
271 // test Alignment(SeqCigar[])
274 // we try to get the associated view's gap character
275 // but this may fail if the view was closed...
278 } catch (Exception ex) {};
279 Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(gc);
281 if (alAndColsel != null && alAndColsel[0] != null)
283 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
285 Alignment al = new Alignment( (SequenceI[]) alAndColsel[0]);
286 Alignment dataset = (av!=null && av.getAlignment()!=null) ? av.getAlignment().getDataset() : null;
289 al.setDataset(dataset);
295 AlignFrame af = new AlignFrame(al, (ColumnSelection) alAndColsel[1],
296 AlignFrame.DEFAULT_WIDTH,
297 AlignFrame.DEFAULT_HEIGHT
300 //>>>This is a fix for the moment, until a better solution is found!!<<<
301 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
303 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
306 Desktop.addInternalFrame(af, "Original Data for " + this.title,
307 AlignFrame.DEFAULT_WIDTH,
308 AlignFrame.DEFAULT_HEIGHT);
311 /* CutAndPasteTransfer cap = new CutAndPasteTransfer();
312 for (int i = 0; i < seqs.length; i++)
314 cap.appendText(new jalview.util.Format("%-" + 15 + "s").form(
316 cap.appendText(" " + seqstrings[i] + "\n");
320 Desktop.addInternalFrame(cap, "Original Data",
326 extends Thread implements Printable
330 PrinterJob printJob = PrinterJob.getPrinterJob();
331 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
333 printJob.setPrintable(this, pf);
335 if (printJob.printDialog())
341 catch (Exception PrintException)
343 PrintException.printStackTrace();
348 public int print(Graphics pg, PageFormat pf, int pi)
349 throws PrinterException
351 pg.translate( (int) pf.getImageableX(), (int) pf.getImageableY());
353 rc.drawBackground(pg, rc.bgColour);
355 if (rc.drawAxes == true)
362 return Printable.PAGE_EXISTS;
366 return Printable.NO_SUCH_PAGE;
374 * @param e DOCUMENT ME!
376 public void eps_actionPerformed(ActionEvent e)
378 makePCAImage(jalview.util.ImageMaker.EPS);
384 * @param e DOCUMENT ME!
386 public void png_actionPerformed(ActionEvent e)
388 makePCAImage(jalview.util.ImageMaker.PNG);
391 void makePCAImage(int type)
393 int width = rc.getWidth();
394 int height = rc.getHeight();
396 jalview.util.ImageMaker im;
398 if (type == jalview.util.ImageMaker.PNG)
400 im = new jalview.util.ImageMaker(this,
401 jalview.util.ImageMaker.PNG,
402 "Make PNG image from PCA",
408 im = new jalview.util.ImageMaker(this,
409 jalview.util.ImageMaker.EPS,
410 "Make EPS file from PCA",
412 null, this.getTitle());
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.
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);