Merge remote-tracking branch 'origin/develop' into
[jalview.git] / src / jalview / javascript / MouseOverStructureListener.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.javascript;
22
23 import jalview.api.AlignmentViewPanel;
24 import jalview.api.FeatureRenderer;
25 import jalview.api.SequenceRenderer;
26 import jalview.appletgui.AlignFrame;
27 import jalview.bin.JalviewLite;
28 import jalview.datamodel.SequenceI;
29 import jalview.ext.jmol.JmolCommands;
30 import jalview.structure.AtomSpec;
31 import jalview.structure.StructureListener;
32 import jalview.structure.StructureMapping;
33 import jalview.structure.StructureMappingcommandSet;
34 import jalview.structure.StructureSelectionManager;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39 /**
40  * Propagate events involving PDB structures associated with sequences to a
41  * javascript function. Generally, the javascript handler is called with a
42  * series of arguments like (eventname, ... ). As of Jalview 2.7, the following
43  * different types of events are supported:
44  * <ul>
45  * <li>mouseover: javascript function called with arguments
46  * 
47  * <pre>
48  * ['mouseover',String(pdb file URI), String(pdb file chain ID), String(residue
49  * number moused over), String(atom index corresponding to residue)]
50  * </pre>
51  * 
52  * </li>
53  * <li>colourstruct: javascript function called with arguments
54  * 
55  * <pre>
56  * ['colourstruct',String(alignment view id),String(number of javascript message
57  * chunks to collect),String(length of first chunk in set of messages - or zero
58  * for null message)]
59  * </pre>
60  * 
61  * <br>
62  * The message contains a series of Jmol script commands that will colour
63  * structures according to their associated sequences in the current view. Use
64  * jalview
65  * .javascript.JalviewLiteJsApi.getJsMessage('colourstruct',String(alignment
66  * view id)) to retrieve successive chunks of the message.</li>
67  * </ul>
68  * 
69  * @author Jim Procter (jprocter)
70  * 
71  */
72 public class MouseOverStructureListener extends JSFunctionExec implements
73         JsCallBack, StructureListener
74 {
75
76   String _listenerfn;
77
78   String[] modelSet;
79
80   public MouseOverStructureListener(JalviewLite jalviewLite,
81           String listener, String[] modelList)
82   {
83     super(jalviewLite);
84     _listenerfn = listener;
85     modelSet = modelList;
86     if (modelSet != null)
87     {
88       for (int i = 0; i < modelSet.length; i++)
89       {
90         // resolve a real filename
91         try
92         {
93           if (new java.net.URL(modelSet[i]).openConnection() != null)
94           {
95             continue;
96           }
97         } catch (Exception x)
98         {
99         }
100         ;
101         try
102         {
103           String db = jvlite.getDocumentBase().toString();
104           db = db.substring(0, db.lastIndexOf("/"));
105           if (new java.net.URL(db + "/" + modelSet[i]).openConnection() != null)
106           {
107             modelSet[i] = db + "/" + modelSet[i];
108             continue;
109           }
110         } catch (Exception x)
111         {
112         }
113         ;
114         try
115         {
116           if (new java.net.URL(jvlite.getCodeBase() + modelSet[i])
117                   .openConnection() != null)
118           {
119             modelSet[i] = jvlite.getCodeBase() + modelSet[i];
120             continue;
121           }
122         } catch (Exception x)
123         {
124         }
125         ;
126
127       }
128     }
129   }
130
131   @Override
132   public String[] getPdbFile()
133   {
134     return modelSet;
135   }
136
137   public void mouseOverStructure(int atomIndex, String strInfo)
138   {
139
140     // StructureSelectionManager.getStructureSelectionManager().mouseOverStructure(atomIndex,
141     // chain, pdbfile)
142     // TODO Auto-generated method stub
143
144   }
145
146   @Override
147   public void highlightAtoms(List<AtomSpec> atoms)
148   {
149     for (AtomSpec atom : atoms)
150     {
151       try
152       {
153         // TODO is this right? StructureSelectionManager passes pdbFile as the
154         // field that is interpreted (in 2.8.2) as pdbId?
155         executeJavascriptFunction(_listenerfn, new String[]
156         { "mouseover", "" + atom.getPdbFile(),
157                     "" + atom.getChain(),
158             "" + (atom.getPdbResNum()), "" + atom.getAtomIndex() });
159       } catch (Exception ex)
160       {
161         System.err.println("Couldn't execute callback with " + _listenerfn
162                 + " for atomSpec: " + atom);
163         ex.printStackTrace();
164       }
165     }
166   }
167
168   @Override
169   public synchronized void updateColours(Object srce)
170   {
171     final Object source = srce;
172     StructureSelectionManager ssm = StructureSelectionManager
173             .getStructureSelectionManager(jvlite);
174     // if (jvlite.debug)
175     // {
176     // ssm.reportMapping();
177     // }
178     if (source instanceof jalview.api.AlignmentViewPanel)
179     {
180       SequenceI[][] sequence = new SequenceI[modelSet.length][];
181       for (int m = 0; m < modelSet.length; m++)
182       {
183         StructureMapping[] sm = ssm.getMapping(modelSet[m]);
184         if (sm != null && sm.length > 0)
185         {
186           sequence[m] = new SequenceI[sm.length];
187           for (int i = 0; i < sm.length; i++)
188           {
189             sequence[m][i] = sm[i].getSequence();
190           }
191         }
192         else
193         {
194           sequence[m] = new SequenceI[0];
195         }
196         // if (jvlite.debug)
197         // {
198         // System.err.println("Mapped '" + modelSet[m] + "' to "
199         // + sequence[m].length + " sequences.");
200         // }
201       }
202
203       SequenceRenderer sr = ((jalview.appletgui.AlignmentPanel) source)
204               .getSequenceRenderer();
205       FeatureRenderer fr = ((jalview.appletgui.AlignmentPanel) source).av
206               .isShowSequenceFeatures() ? new jalview.appletgui.FeatureRenderer(
207               ((jalview.appletgui.AlignmentPanel) source).av) : null;
208       if (fr != null)
209       {
210         ((jalview.appletgui.FeatureRenderer) fr)
211                 .transferSettings(((jalview.appletgui.AlignmentPanel) source)
212                         .getFeatureRenderer());
213       }
214       ;
215
216       // Form a colour command from the given alignment panel for each distinct
217       // structure
218       ArrayList<String[]> ccomands = new ArrayList<String[]>();
219       ArrayList<String> pdbfn = new ArrayList<String>();
220       StructureMappingcommandSet[] colcommands = JmolCommands
221               .getColourBySequenceCommand(ssm, modelSet, sequence, sr, fr,
222                       ((AlignmentViewPanel) source).getAlignment());
223       if (colcommands == null)
224       {
225         return;
226       }
227       int sz = 0;
228       for (jalview.structure.StructureMappingcommandSet ccset : colcommands)
229       {
230         sz += ccset.commands.length;
231         ccomands.add(ccset.commands);
232         pdbfn.add(ccset.mapping);
233       }
234
235       String mclass, mhandle;
236       String ccomandset[] = new String[sz];
237       sz = 0;
238       for (String[] ccset : ccomands)
239       {
240         System.arraycopy(ccset, 0, ccomandset, sz, ccset.length);
241         sz += ccset.length;
242       }
243       if (jvlite.isJsMessageSetChanged(
244               mclass = "colourstruct",
245               mhandle = ((jalview.appletgui.AlignmentPanel) source).av
246                       .getViewId(), ccomandset))
247       {
248         jvlite.setJsMessageSet(mclass, mhandle, ccomandset);
249         // and notify javascript handler
250         String st[] = new String[]
251         {
252             "colourstruct",
253             "" + ((jalview.appletgui.AlignmentPanel) source).av.getViewId(),
254             "" + ccomandset.length,
255             jvlite.arrayToSeparatorList(pdbfn.toArray(new String[pdbfn
256                     .size()])) };
257         try
258         {
259           executeJavascriptFunction(true, _listenerfn, st);
260         } catch (Exception ex)
261         {
262           System.err.println("Couldn't execute callback with "
263                   + _listenerfn + " using args { " + st[0] + ", " + st[1]
264                   + ", " + st[2] + "," + st[3] + "}"); // + ","+st[4]+"\n");
265           ex.printStackTrace();
266
267         }
268       }
269       /*
270        * new Thread(new Runnable() { public void run() { // and send to
271        * javascript handler String st[] = new String[0]; int i = 0; for (String
272        * colcommand : colcommands) { // do sync execution for each chunk try {
273        * executeJavascriptFunction( false, _listenerfn, st = new String[] {
274        * "colourstruct", "" + ((jalview.appletgui.AlignmentPanel) source).av
275        * .getViewId(), handle, "" }); } catch (Exception ex) {
276        * System.err.println("Couldn't execute callback with " + _listenerfn +
277        * " using args { " + st[0] + ", " + st[1] + ", " + st[2] + "," + st[3] +
278        * "\n"); ex.printStackTrace();
279        * 
280        * } } } }).start();
281        */
282     }
283
284   }
285
286   @Override
287   public AlignFrame getAlignFrame()
288   {
289     // associated with all alignframes, always.
290     return null;
291   }
292
293   @Override
294   public String getListenerFunction()
295   {
296     return _listenerfn;
297   }
298
299   public void finalise()
300   {
301     jvlite = null;
302     super.finalize();
303   }
304
305   @Override
306   public void releaseReferences(Object svl)
307   {
308
309     // TODO Auto-generated method stub
310
311   }
312
313 }