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