f470ffc445b9eacd95e80f774bdda7d5a05684eb
[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, SequenceI[] s)\r
42   {\r
43 \r
44     this.av = av;\r
45     if (av.getSelectionGroup().getSize() > 3)\r
46     {\r
47       seqs = new Sequence[av.getSelectionGroup().getSize()];\r
48       for (int i = 0; i < s.length; i++)\r
49       {\r
50         seqs[i] = av.getSelectionGroup().getSequenceAt(i);\r
51       }\r
52     }\r
53 \r
54     if (seqs == null)\r
55     {\r
56       seqs = new Sequence[av.getAlignment().getHeight()];\r
57       for (int i = 0; i < av.getAlignment().getHeight(); i++)\r
58       {\r
59         seqs[i] = av.getAlignment().getSequenceAt(i);\r
60       }\r
61     }\r
62     rc = new RotatableCanvas(av);\r
63     add(rc, BorderLayout.CENTER);\r
64     Thread worker = new Thread(this);\r
65     worker.start();\r
66   }\r
67 \r
68   /**\r
69    * DOCUMENT ME!\r
70    */\r
71   public void run()\r
72   {\r
73               pca = new PCA(seqs);\r
74               pca.run();\r
75 \r
76               // Now find the component coordinates\r
77               int ii = 0;\r
78 \r
79               while ((ii < seqs.length) && (seqs[ii] != null))\r
80               {\r
81                   ii++;\r
82               }\r
83 \r
84               double[][] comps = new double[ii][ii];\r
85 \r
86               for (int i = 0; i < ii; i++)\r
87               {\r
88                   if (pca.getEigenvalue(i) > 1e-4)\r
89                   {\r
90                       comps[i] = pca.component(i);\r
91                   }\r
92               }\r
93 \r
94               //////////////////\r
95               xCombobox.select(0);\r
96               yCombobox.select(1);\r
97               zCombobox.select(2);\r
98 \r
99               top = pca.getM().rows - 1;\r
100 \r
101               Vector points = new Vector();\r
102               float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);\r
103 \r
104               for (int i = 0; i < pca.getM().rows; i++)\r
105               {\r
106                   SequencePoint sp = new SequencePoint(seqs[i], scores[i]);\r
107                   points.addElement(sp);\r
108               }\r
109 \r
110               rc.setPoints(points, pca.getM().rows);\r
111               rc.repaint();\r
112               seqs = null;\r
113   }\r
114 \r
115   void doDimensionChange()\r
116   {\r
117     if (top == 0)\r
118     {\r
119       return;\r
120     }\r
121 \r
122     int dim1 = top - xCombobox.getSelectedIndex();\r
123     int dim2 = top - yCombobox.getSelectedIndex();\r
124     int dim3 = top - zCombobox.getSelectedIndex();\r
125 \r
126     float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);\r
127     for (int i = 0; i < pca.getM().rows; i++)\r
128     {\r
129       ( (SequencePoint) rc.points.elementAt(i)).coord = scores[i];\r
130     }\r
131 \r
132     rc.img = null;\r
133     rc.rotmat.setIdentity();\r
134     rc.initAxes();\r
135     rc.paint(rc.getGraphics());\r
136   }\r
137 \r
138   protected void xCombobox_actionPerformed(ActionEvent e)\r
139   {\r
140     doDimensionChange();\r
141   }\r
142 \r
143   protected void yCombobox_actionPerformed(ActionEvent e)\r
144   {\r
145     doDimensionChange();\r
146   }\r
147 \r
148   protected void zCombobox_actionPerformed(ActionEvent e)\r
149   {\r
150     doDimensionChange();\r
151   }\r
152 \r
153 }\r