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