JAL-1033, JAL-1013 - ensure pca window shown only after calculation completes, and...
[jalview.git] / src / jalview / appletgui / PCAPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.appletgui;
19
20 import java.util.*;
21
22 import java.awt.*;
23 import java.awt.event.*;
24
25 import jalview.analysis.*;
26 import jalview.datamodel.*;
27
28 public class PCAPanel extends EmbmenuFrame implements Runnable,
29         ActionListener, ItemListener
30 {
31   PCA pca;
32
33   int top;
34
35   RotatableCanvas rc;
36
37   AlignViewport av;
38
39   AlignmentView seqstrings;
40
41   SequenceI[] seqs;
42
43   
44   /**
45    * use the identity matrix for calculating similarity between sequences. 
46    */
47   private boolean useidentity=false;
48
49
50   public PCAPanel(AlignViewport av)
51   {
52     try
53     {
54       jbInit();
55     } catch (Exception e)
56     {
57       e.printStackTrace();
58     }
59
60     for (int i = 1; i < 8; i++)
61     {
62       xCombobox.addItem("dim " + i);
63       yCombobox.addItem("dim " + i);
64       zCombobox.addItem("dim " + i);
65     }
66
67     this.av = av;
68     seqstrings = av.getAlignmentView(av.getSelectionGroup() != null);
69     useidentity=av.getAlignment().isNucleotide();
70     if (av.getSelectionGroup() == null)
71     {
72       seqs = av.alignment.getSequencesArray();
73     }
74     else
75     {
76       seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment);
77     }
78     SeqCigar sq[] = seqstrings.getSequences();
79     int length = sq[0].getWidth();
80
81     for (int i = 0; i < seqs.length; i++)
82     {
83       if (sq[i].getWidth() != length)
84       {
85         System.out
86                 .println("Sequences must be equal length for PCA analysis");
87         return;
88       }
89     }
90
91     rc = new RotatableCanvas(av);
92     embedMenuIfNeeded(rc);
93     add(rc, BorderLayout.CENTER);
94
95     jalview.bin.JalviewLite.addFrame(this, "Principal component analysis",
96             400, 400);
97
98     Thread worker = new Thread(this);
99     worker.start();
100   }
101
102   /**
103    * DOCUMENT ME!
104    */
105   public void run()
106   {
107     pca = new PCA(seqstrings.getSequenceStrings(' '), useidentity);
108     pca.run();
109
110     // Now find the component coordinates
111     int ii = 0;
112
113     while ((ii < seqs.length) && (seqs[ii] != null))
114     {
115       ii++;
116     }
117
118     double[][] comps = new double[ii][ii];
119
120     for (int i = 0; i < ii; i++)
121     {
122       if (pca.getEigenvalue(i) > 1e-4)
123       {
124         comps[i] = pca.component(i);
125       }
126     }
127
128     // ////////////////
129     xCombobox.select(0);
130     yCombobox.select(1);
131     zCombobox.select(2);
132
133     top = pca.getM().rows - 1;
134
135     Vector points = new Vector();
136     float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
137
138     for (int i = 0; i < pca.getM().rows; i++)
139     {
140       SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
141       points.addElement(sp);
142     }
143
144     rc.setPoints(points, pca.getM().rows);
145     rc.repaint();
146     seqs = null;
147     this.repaint();
148   }
149
150   void doDimensionChange()
151   {
152     if (top == 0)
153     {
154       return;
155     }
156
157     int dim1 = top - xCombobox.getSelectedIndex();
158     int dim2 = top - yCombobox.getSelectedIndex();
159     int dim3 = top - zCombobox.getSelectedIndex();
160
161     float[][] scores = pca.getComponents(dim1, dim2, dim3, 100);
162     for (int i = 0; i < pca.getM().rows; i++)
163     {
164       ((SequencePoint) rc.points.elementAt(i)).coord = scores[i];
165     }
166
167     rc.img = null;
168     rc.rotmat.setIdentity();
169     rc.initAxes();
170     rc.paint(rc.getGraphics());
171   }
172
173   public void actionPerformed(ActionEvent evt)
174   {
175     if (evt.getSource() == inputData)
176     {
177       showOriginalData();
178     }
179     else
180     {
181       values_actionPerformed();
182     }
183   }
184
185   public void itemStateChanged(ItemEvent evt)
186   {
187     if (evt.getSource() == xCombobox)
188     {
189       xCombobox_actionPerformed();
190     }
191     else if (evt.getSource() == yCombobox)
192     {
193       yCombobox_actionPerformed();
194     }
195     else if (evt.getSource() == zCombobox)
196     {
197       zCombobox_actionPerformed();
198     }
199   }
200
201   protected void xCombobox_actionPerformed()
202   {
203     doDimensionChange();
204   }
205
206   protected void yCombobox_actionPerformed()
207   {
208     doDimensionChange();
209   }
210
211   protected void zCombobox_actionPerformed()
212   {
213     doDimensionChange();
214   }
215
216   public void values_actionPerformed()
217   {
218
219     CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
220     Frame frame = new Frame();
221     frame.add(cap);
222     jalview.bin.JalviewLite.addFrame(frame, "PCA details", 500, 500);
223
224     cap.setText(pca.getDetails());
225   }
226
227   void showOriginalData()
228   {
229     // decide if av alignment is sufficiently different to original data to
230     // warrant a new window to be created
231     // create new alignmnt window with hidden regions (unhiding hidden regions
232     // yields unaligned seqs)
233     // or create a selection box around columns in alignment view
234     // test Alignment(SeqCigar[])
235     char gc = '-';
236     try
237     {
238       // we try to get the associated view's gap character
239       // but this may fail if the view was closed...
240       gc = av.getGapCharacter();
241     } catch (Exception ex)
242     {
243     }
244     ;
245     Object[] alAndColsel = seqstrings.getAlignmentAndColumnSelection(gc);
246
247     if (alAndColsel != null && alAndColsel[0] != null)
248     {
249       Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
250       AlignFrame af = new AlignFrame(al, av.applet,
251               "Original Data for PCA", false);
252
253       af.viewport.setHiddenColumns((ColumnSelection) alAndColsel[1]);
254     }
255   }
256
257   public void labels_itemStateChanged(ItemEvent itemEvent)
258   {
259     rc.showLabels(labels.getState());
260   }
261
262   Panel jPanel2 = new Panel();
263
264   Label jLabel1 = new Label();
265
266   Label jLabel2 = new Label();
267
268   Label jLabel3 = new Label();
269
270   protected Choice xCombobox = new Choice();
271
272   protected Choice yCombobox = new Choice();
273
274   protected Choice zCombobox = new Choice();
275
276   FlowLayout flowLayout1 = new FlowLayout();
277
278   BorderLayout borderLayout1 = new BorderLayout();
279
280   MenuBar menuBar1 = new MenuBar();
281
282   Menu menu1 = new Menu();
283
284   Menu menu2 = new Menu();
285
286   protected CheckboxMenuItem labels = new CheckboxMenuItem();
287
288   MenuItem values = new MenuItem();
289
290   MenuItem inputData = new MenuItem();
291
292   private void jbInit() throws Exception
293   {
294     this.setLayout(borderLayout1);
295     jPanel2.setLayout(flowLayout1);
296     jLabel1.setFont(new java.awt.Font("Verdana", 0, 12));
297     jLabel1.setText("x=");
298     jLabel2.setFont(new java.awt.Font("Verdana", 0, 12));
299     jLabel2.setText("y=");
300     jLabel3.setFont(new java.awt.Font("Verdana", 0, 12));
301     jLabel3.setText("z=");
302     jPanel2.setBackground(Color.white);
303     zCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
304     zCombobox.addItemListener(this);
305     yCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
306     yCombobox.addItemListener(this);
307     xCombobox.setFont(new java.awt.Font("Verdana", 0, 12));
308     xCombobox.addItemListener(this);
309     this.setMenuBar(menuBar1);
310     menu1.setLabel("File");
311     menu2.setLabel("View");
312     labels.setLabel("Labels");
313     labels.addItemListener(this);
314     values.setLabel("Output Values...");
315     values.addActionListener(this);
316     inputData.setLabel("Input Data...");
317     this.add(jPanel2, BorderLayout.SOUTH);
318     jPanel2.add(jLabel1, null);
319     jPanel2.add(xCombobox, null);
320     jPanel2.add(jLabel2, null);
321     jPanel2.add(yCombobox, null);
322     jPanel2.add(jLabel3, null);
323     jPanel2.add(zCombobox, null);
324     menuBar1.add(menu1);
325     menuBar1.add(menu2);
326     menu2.add(labels);
327     menu1.add(values);
328     menu1.add(inputData);
329     inputData.addActionListener(this);
330   }
331
332 }