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