X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fappletgui%2FPCAPanel.java;h=b121828c8e5bf3b3b18db3cdfac298f38af0958f;hb=da66cf1130998cd785d9bc2954bb79a3c7da1266;hp=0368d2a8770e934428a251144fefee9fff219c06;hpb=55e2e9b22b133db8b9ff0979b0338a33081fc8fd;p=jalview.git diff --git a/src/jalview/appletgui/PCAPanel.java b/src/jalview/appletgui/PCAPanel.java index 0368d2a..b121828 100755 --- a/src/jalview/appletgui/PCAPanel.java +++ b/src/jalview/appletgui/PCAPanel.java @@ -1,6 +1,6 @@ /* * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -27,16 +27,15 @@ import java.awt.event.*; import jalview.analysis.*; import jalview.datamodel.*; - public class PCAPanel - extends Frame implements Runnable + extends Frame implements Runnable, ActionListener, ItemListener { PCA pca; int top; RotatableCanvas rc; AlignViewport av; - SequenceI [] seqs; - + SequenceI[] seqs; + AlignmentView seqstrings; public PCAPanel(AlignViewport av) { @@ -57,20 +56,24 @@ public class PCAPanel } this.av = av; - if (av.getSelectionGroup()!=null && av.getSelectionGroup().getSize() > 3) + seqstrings = av.getAlignmentView(av.getSelectionGroup() != null); + if (av.getSelectionGroup() == null) { - seqs = new Sequence[av.getSelectionGroup().getSize()]; - for (int i = 0; i < av.getSelectionGroup().getSize(); i++) - { - seqs[i] = av.getSelectionGroup().getSequenceAt(i); - } + seqs = av.alignment.getSequencesArray(); } else { - seqs = new Sequence[av.getAlignment().getHeight()]; - for (int i = 0; i < av.getAlignment().getHeight(); i++) + seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment); + } + SeqCigar sq[] = seqstrings.getSequences(); + int length = sq[0].getWidth(); + + for (int i = 0; i < seqs.length; i++) + { + if (sq[i].getWidth() != length) { - seqs[i] = av.getAlignment().getSequenceAt(i); + System.out.println("Sequences must be equal length for PCA analysis"); + return; } } @@ -78,8 +81,7 @@ public class PCAPanel add(rc, BorderLayout.CENTER); jalview.bin.JalviewLite.addFrame(this, "Principal component analysis", - 400, 400); - + 400, 400); Thread worker = new Thread(this); worker.start(); @@ -90,47 +92,47 @@ public class PCAPanel */ public void run() { - pca = new PCA(seqs); - pca.run(); - - // Now find the component coordinates - int ii = 0; - - while ((ii < seqs.length) && (seqs[ii] != null)) - { - ii++; - } - - double[][] comps = new double[ii][ii]; - - for (int i = 0; i < ii; i++) - { - if (pca.getEigenvalue(i) > 1e-4) - { - comps[i] = pca.component(i); - } - } - - ////////////////// - xCombobox.select(0); - yCombobox.select(1); - zCombobox.select(2); - - top = pca.getM().rows - 1; - - Vector points = new Vector(); - float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100); - - for (int i = 0; i < pca.getM().rows; i++) - { - SequencePoint sp = new SequencePoint(seqs[i], scores[i]); - points.addElement(sp); - } - - rc.setPoints(points, pca.getM().rows); - rc.repaint(); - seqs = null; - this.repaint(); + pca = new PCA(seqstrings.getSequenceStrings(' ')); + pca.run(); + + // Now find the component coordinates + int ii = 0; + + while ( (ii < seqs.length) && (seqs[ii] != null)) + { + ii++; + } + + double[][] comps = new double[ii][ii]; + + for (int i = 0; i < ii; i++) + { + if (pca.getEigenvalue(i) > 1e-4) + { + comps[i] = pca.component(i); + } + } + + ////////////////// + xCombobox.select(0); + yCombobox.select(1); + zCombobox.select(2); + + top = pca.getM().rows - 1; + + Vector points = new Vector(); + float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100); + + for (int i = 0; i < pca.getM().rows; i++) + { + SequencePoint sp = new SequencePoint(seqs[i], scores[i]); + points.addElement(sp); + } + + rc.setPoints(points, pca.getM().rows); + rc.repaint(); + seqs = null; + this.repaint(); } void doDimensionChange() @@ -156,22 +158,50 @@ public class PCAPanel rc.paint(rc.getGraphics()); } - protected void xCombobox_actionPerformed(ActionEvent e) + public void actionPerformed(ActionEvent evt) + { + if (evt.getSource() == inputData) + { + showOriginalData(); + } + else + { + values_actionPerformed(); + } + } + + public void itemStateChanged(ItemEvent evt) + { + if (evt.getSource() == xCombobox) + { + xCombobox_actionPerformed(); + } + else if (evt.getSource() == yCombobox) + { + yCombobox_actionPerformed(); + } + else if (evt.getSource() == zCombobox) + { + zCombobox_actionPerformed(); + } + } + + protected void xCombobox_actionPerformed() { doDimensionChange(); } - protected void yCombobox_actionPerformed(ActionEvent e) + protected void yCombobox_actionPerformed() { doDimensionChange(); } - protected void zCombobox_actionPerformed(ActionEvent e) + protected void zCombobox_actionPerformed() { doDimensionChange(); } - public void values_actionPerformed(ActionEvent actionEvent) + public void values_actionPerformed() { CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null); @@ -179,13 +209,35 @@ public class PCAPanel frame.add(cap); jalview.bin.JalviewLite.addFrame(frame, "PCA details", 500, 500); - cap.setText(pca.getDetails()); + cap.setText(pca.getDetails()); + } + + void showOriginalData() + { + // decide if av alignment is sufficiently different to original data to warrant a new window to be created + // create new alignmnt window with hidden regions (unhiding hidden regions yields unaligned seqs) + // or create a selection box around columns in alignment view + // test Alignment(SeqCigar[]) + Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(av. + getGapCharacter()); + + if (alAndColsel != null && alAndColsel[0] != null) + { + Alignment al = new Alignment( (SequenceI[]) alAndColsel[0]); + AlignFrame af = new AlignFrame(al, + av.applet, + "Original Data for PCA", + false); + + af.viewport.setHiddenColumns( (ColumnSelection) alAndColsel[1]); + } } public void labels_itemStateChanged(ItemEvent itemEvent) { - rc.showLabels( labels.getState() ); + rc.showLabels(labels.getState()); } + Panel jPanel2 = new Panel(); Label jLabel1 = new Label(); Label jLabel2 = new Label(); @@ -200,6 +252,7 @@ public class PCAPanel Menu menu2 = new Menu(); protected CheckboxMenuItem labels = new CheckboxMenuItem(); MenuItem values = new MenuItem(); + MenuItem inputData = new MenuItem(); private void jbInit() throws Exception @@ -214,48 +267,19 @@ public class PCAPanel jLabel3.setText("z="); jPanel2.setBackground(Color.white); zCombobox.setFont(new java.awt.Font("Verdana", 0, 12)); - zCombobox.addItemListener(new java.awt.event.ItemListener() - { - public void itemStateChanged(ItemEvent e) - { - zCombobox_actionPerformed(null); - } - }); + zCombobox.addItemListener(this); yCombobox.setFont(new java.awt.Font("Verdana", 0, 12)); - yCombobox.addItemListener(new java.awt.event.ItemListener() - { - public void itemStateChanged(ItemEvent e) - { - yCombobox_actionPerformed(null); - } - }); + yCombobox.addItemListener(this); xCombobox.setFont(new java.awt.Font("Verdana", 0, 12)); - xCombobox.addItemListener(new java.awt.event.ItemListener() - { - public void itemStateChanged(ItemEvent e) - { - xCombobox_actionPerformed(null); - } - }); + xCombobox.addItemListener(this); this.setMenuBar(menuBar1); menu1.setLabel("File"); menu2.setLabel("View"); labels.setLabel("Labels"); - labels.addItemListener(new ItemListener() - { - public void itemStateChanged(ItemEvent itemEvent) - { - labels_itemStateChanged(itemEvent); - } - }); + labels.addItemListener(this); values.setLabel("Output Values..."); - values.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent actionEvent) - { - values_actionPerformed(actionEvent); - } - }); + values.addActionListener(this); + inputData.setLabel("Input Data..."); this.add(jPanel2, BorderLayout.SOUTH); jPanel2.add(jLabel1, null); jPanel2.add(xCombobox, null); @@ -267,6 +291,8 @@ public class PCAPanel menuBar1.add(menu2); menu2.add(labels); menu1.add(values); + menu1.add(inputData); + inputData.addActionListener(this); } }