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 new OOMWarning("calculating PCA", er);
179 void doDimensionChange()
186 int dim1 = top - xCombobox.getSelectedIndex();
187 int dim2 = top - yCombobox.getSelectedIndex();
188 int dim3 = top - zCombobox.getSelectedIndex();
190 float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);
192 for (int i = 0; i < pca.getM().rows; i++)
194 ( (SequencePoint) rc.points.elementAt(i)).coord = scores[i];
198 rc.rotmat.setIdentity();
200 rc.paint(rc.getGraphics());
206 * @param e DOCUMENT ME!
208 protected void xCombobox_actionPerformed(ActionEvent e)
216 * @param e DOCUMENT ME!
218 protected void yCombobox_actionPerformed(ActionEvent e)
226 * @param e DOCUMENT ME!
228 protected void zCombobox_actionPerformed(ActionEvent e)
233 public void outputValues_actionPerformed(ActionEvent e)
235 CutAndPasteTransfer cap = new CutAndPasteTransfer();
236 Desktop.addInternalFrame(cap, "PCA details", 500,
239 cap.setText(pca.getDetails());
242 public void showLabels_actionPerformed(ActionEvent e)
244 rc.showLabels(showLabels.getState());
247 public void print_actionPerformed(ActionEvent e)
249 PCAPrinter printer = new PCAPrinter();
253 public void originalSeqData_actionPerformed(ActionEvent e)
255 // this was cut'n'pasted from the equivalent TreePanel method - we should make this an abstract function of all jalview analysis windows
256 if (seqstrings == null)
258 jalview.bin.Cache.log.info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
261 // decide if av alignment is sufficiently different to original data to warrant a new window to be created
262 // create new alignmnt window with hidden regions (unhiding hidden regions yields unaligned seqs)
263 // or create a selection box around columns in alignment view
264 // test Alignment(SeqCigar[])
267 // we try to get the associated view's gap character
268 // but this may fail if the view was closed...
271 } catch (Exception ex) {};
272 Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(gc);
274 if (alAndColsel != null && alAndColsel[0] != null)
276 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
278 Alignment al = new Alignment( (SequenceI[]) alAndColsel[0]);
279 Alignment dataset = (av!=null && av.getAlignment()!=null) ? av.getAlignment().getDataset() : null;
282 al.setDataset(dataset);
288 AlignFrame af = new AlignFrame(al, (ColumnSelection) alAndColsel[1],
289 AlignFrame.DEFAULT_WIDTH,
290 AlignFrame.DEFAULT_HEIGHT
293 //>>>This is a fix for the moment, until a better solution is found!!<<<
294 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
296 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
299 Desktop.addInternalFrame(af, "Original Data for " + this.title,
300 AlignFrame.DEFAULT_WIDTH,
301 AlignFrame.DEFAULT_HEIGHT);
304 /* CutAndPasteTransfer cap = new CutAndPasteTransfer();
305 for (int i = 0; i < seqs.length; i++)
307 cap.appendText(new jalview.util.Format("%-" + 15 + "s").form(
309 cap.appendText(" " + seqstrings[i] + "\n");
313 Desktop.addInternalFrame(cap, "Original Data",
319 extends Thread implements Printable
323 PrinterJob printJob = PrinterJob.getPrinterJob();
324 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
326 printJob.setPrintable(this, pf);
328 if (printJob.printDialog())
334 catch (Exception PrintException)
336 PrintException.printStackTrace();
341 public int print(Graphics pg, PageFormat pf, int pi)
342 throws PrinterException
344 pg.translate( (int) pf.getImageableX(), (int) pf.getImageableY());
346 rc.drawBackground(pg, rc.bgColour);
348 if (rc.drawAxes == true)
355 return Printable.PAGE_EXISTS;
359 return Printable.NO_SUCH_PAGE;
367 * @param e DOCUMENT ME!
369 public void eps_actionPerformed(ActionEvent e)
371 makePCAImage(jalview.util.ImageMaker.EPS);
377 * @param e DOCUMENT ME!
379 public void png_actionPerformed(ActionEvent e)
381 makePCAImage(jalview.util.ImageMaker.PNG);
384 void makePCAImage(int type)
386 int width = rc.getWidth();
387 int height = rc.getHeight();
389 jalview.util.ImageMaker im;
391 if (type == jalview.util.ImageMaker.PNG)
393 im = new jalview.util.ImageMaker(this,
394 jalview.util.ImageMaker.PNG,
395 "Make PNG image from PCA",
401 im = new jalview.util.ImageMaker(this,
402 jalview.util.ImageMaker.EPS,
403 "Make EPS file from PCA",
405 null, this.getTitle());
408 if (im.getGraphics() != null)
410 rc.drawBackground(im.getGraphics(), Color.black);
411 rc.drawScene(im.getGraphics());
412 if (rc.drawAxes == true)
414 rc.drawAxes(im.getGraphics());
420 public void viewMenu_menuSelected()
422 buildAssociatedViewMenu();
425 void buildAssociatedViewMenu()
427 AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av.
429 if (aps.length == 1 && rc.av == aps[0].av)
431 associateViewsMenu.setVisible(false);
435 associateViewsMenu.setVisible(true);
437 if ( (viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
439 viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
442 associateViewsMenu.removeAll();
444 JRadioButtonMenuItem item;
445 ButtonGroup buttonGroup = new ButtonGroup();
446 int i, iSize = aps.length;
447 final PCAPanel thisPCAPanel = this;
448 for (i = 0; i < iSize; i++)
450 final AlignmentPanel ap = aps[i];
451 item = new JRadioButtonMenuItem(ap.av.viewName, ap.av == rc.av);
452 buttonGroup.add(item);
453 item.addActionListener(new ActionListener()
455 public void actionPerformed(ActionEvent evt)
457 rc.applyToAllViews = false;
460 PaintRefresher.Register(thisPCAPanel, ap.av.getSequenceSetId());
464 associateViewsMenu.add(item);
467 final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
469 buttonGroup.add(itemf);
471 itemf.setSelected(rc.applyToAllViews);
472 itemf.addActionListener(new ActionListener()
474 public void actionPerformed(ActionEvent evt)
476 rc.applyToAllViews = itemf.isSelected();
479 associateViewsMenu.add(itemf);