Wrap alignment with annotations
[jalview.git] / src / jalview / appletgui / PCAPanel.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 \r
20 package jalview.appletgui;\r
21 \r
22 import java.util.*;\r
23 \r
24 import java.awt.*;\r
25 import java.awt.event.*;\r
26 \r
27 import jalview.analysis.*;\r
28 import jalview.datamodel.*;\r
29 import jalview.jbappletgui.*;\r
30 \r
31 public class PCAPanel\r
32     extends GPCAPanel implements Runnable\r
33 {\r
34   PCA pca;\r
35   int top;\r
36   RotatableCanvas rc;\r
37   AlignViewport av;\r
38   SequenceI [] seqs;\r
39 \r
40 \r
41   public PCAPanel(AlignViewport av)\r
42   {\r
43     this.av = av;\r
44     if (av.getSelectionGroup()!=null && av.getSelectionGroup().getSize() > 3)\r
45     {\r
46       seqs = new Sequence[av.getSelectionGroup().getSize()];\r
47       for (int i = 0; i < av.getSelectionGroup().getSize(); i++)\r
48       {\r
49         seqs[i] = av.getSelectionGroup().getSequenceAt(i);\r
50       }\r
51     }\r
52     else\r
53     {\r
54       seqs = new Sequence[av.getAlignment().getHeight()];\r
55       for (int i = 0; i < av.getAlignment().getHeight(); i++)\r
56       {\r
57         seqs[i] = av.getAlignment().getSequenceAt(i);\r
58       }\r
59     }\r
60 \r
61     rc = new RotatableCanvas(av);\r
62     add(rc, BorderLayout.CENTER);\r
63 \r
64     jalview.bin.JalviewLite.addFrame(this, "Principal component analysis",\r
65                                        400, 400);\r
66 \r
67 \r
68     Thread worker = new Thread(this);\r
69     worker.start();\r
70   }\r
71 \r
72   /**\r
73    * DOCUMENT ME!\r
74    */\r
75   public void run()\r
76   {\r
77               pca = new PCA(seqs);\r
78               pca.run();\r
79 \r
80               // Now find the component coordinates\r
81               int ii = 0;\r
82 \r
83               while ((ii < seqs.length) && (seqs[ii] != null))\r
84               {\r
85                   ii++;\r
86               }\r
87 \r
88               double[][] comps = new double[ii][ii];\r
89 \r
90               for (int i = 0; i < ii; i++)\r
91               {\r
92                   if (pca.getEigenvalue(i) > 1e-4)\r
93                   {\r
94                       comps[i] = pca.component(i);\r
95                   }\r
96               }\r
97 \r
98               //////////////////\r
99               xCombobox.select(0);\r
100               yCombobox.select(1);\r
101               zCombobox.select(2);\r
102 \r
103               top = pca.getM().rows - 1;\r
104 \r
105               Vector points = new Vector();\r
106               float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);\r
107 \r
108               for (int i = 0; i < pca.getM().rows; i++)\r
109               {\r
110                   SequencePoint sp = new SequencePoint(seqs[i], scores[i]);\r
111                   points.addElement(sp);\r
112               }\r
113 \r
114               rc.setPoints(points, pca.getM().rows);\r
115               rc.repaint();\r
116               seqs = null;\r
117               this.repaint();\r
118   }\r
119 \r
120   void doDimensionChange()\r
121   {\r
122     if (top == 0)\r
123     {\r
124       return;\r
125     }\r
126 \r
127     int dim1 = top - xCombobox.getSelectedIndex();\r
128     int dim2 = top - yCombobox.getSelectedIndex();\r
129     int dim3 = top - zCombobox.getSelectedIndex();\r
130 \r
131     float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);\r
132     for (int i = 0; i < pca.getM().rows; i++)\r
133     {\r
134       ( (SequencePoint) rc.points.elementAt(i)).coord = scores[i];\r
135     }\r
136 \r
137     rc.img = null;\r
138     rc.rotmat.setIdentity();\r
139     rc.initAxes();\r
140     rc.paint(rc.getGraphics());\r
141   }\r
142 \r
143   protected void xCombobox_actionPerformed(ActionEvent e)\r
144   {\r
145     doDimensionChange();\r
146   }\r
147 \r
148   protected void yCombobox_actionPerformed(ActionEvent e)\r
149   {\r
150     doDimensionChange();\r
151   }\r
152 \r
153   protected void zCombobox_actionPerformed(ActionEvent e)\r
154   {\r
155     doDimensionChange();\r
156   }\r
157 \r
158   public void values_actionPerformed(ActionEvent actionEvent)\r
159   {\r
160 \r
161     CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);\r
162     Frame frame = new Frame();\r
163     frame.add(cap);\r
164     jalview.bin.JalviewLite.addFrame(frame, "PCA details", 500, 500);\r
165 \r
166       cap.setText(pca.getDetails());\r
167   }\r
168 \r
169   public void labels_itemStateChanged(ItemEvent itemEvent)\r
170   {\r
171     rc.showLabels( labels.getState() );\r
172   }\r
173 \r
174 }\r