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