88fba0284f1ecfd91c8d575bd27f80ad7c41142c
[jalview.git] / src / jalview / javascript / MouseOverStructureListener.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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.io.IOException;
37 import java.io.InputStream;
38 import java.net.URL;
39 import java.util.ArrayList;
40 import java.util.List;
41
42 /**
43  * Propagate events involving PDB structures associated with sequences to a
44  * javascript function. Generally, the javascript handler is called with a
45  * series of arguments like (eventname, ... ). As of Jalview 2.7, the following
46  * different types of events are supported:
47  * <ul>
48  * <li>mouseover: javascript function called with arguments
49  * 
50  * <pre>
51  * ['mouseover',String(pdb file URI), String(pdb file chain ID), String(residue
52  * number moused over), String(atom index corresponding to residue)]
53  * </pre>
54  * 
55  * </li>
56  * <li>colourstruct: javascript function called with arguments
57  * 
58  * <pre>
59  * ['colourstruct',String(alignment view id),String(number of javascript message
60  * chunks to collect),String(length of first chunk in set of messages - or zero
61  * for null message)]
62  * </pre>
63  * 
64  * <br>
65  * The message contains a series of Jmol script commands that will colour
66  * structures according to their associated sequences in the current view. Use
67  * jalview
68  * .javascript.JalviewLiteJsApi.getJsMessage('colourstruct',String(alignment
69  * view id)) to retrieve successive chunks of the message.</li>
70  * </ul>
71  * 
72  * @author Jim Procter (jprocter)
73  * 
74  */
75 public class MouseOverStructureListener extends JSFunctionExec implements
76         JsCallBack, StructureListener
77 {
78
79   String _listenerfn;
80
81   String[] modelSet;
82
83   public MouseOverStructureListener(JalviewLite jalviewLite,
84           String listener, String[] modelList)
85   {
86     super(jalviewLite);
87     _listenerfn = listener;
88     modelSet = modelList;
89     if (modelSet != null)
90     {
91       for (int i = 0; i < modelSet.length; i++)
92       {
93         modelSet[i] = resolveModelFile(modelSet[i]);
94       }
95     }
96   }
97
98   /**
99    * Returns the first of file, file prefixed by document base, file prefixed by
100    * codebase which can be resolved to a valid URL. If none can, returns the
101    * input parameter value.
102    * 
103    * @param file
104    */
105   public String resolveModelFile(String file)
106   {
107     if (isValidUrl(file))
108     {
109       return file;
110     }
111
112     String db = jvlite.getDocumentBase().toString();
113     db = db.substring(0, db.lastIndexOf("/"));
114     String docBaseFile = db + "/" + file;
115     if (isValidUrl(docBaseFile))
116     {
117       return docBaseFile;
118     }
119
120     String cb = jvlite.getCodeBase() + file;
121     if (isValidUrl(cb))
122     {
123       return cb;
124     }
125
126     return file;
127   }
128
129   /**
130    * Returns true if it is possible to open an input stream at the given URL,
131    * else false. The input stream is closed.
132    * 
133    * @param file
134    * @return
135    */
136   private boolean isValidUrl(String file)
137   {
138     InputStream is = null;
139     try
140     {
141       is = new URL(file).openStream();
142       if (is != null)
143       {
144         return true;
145       }
146     } catch (IOException x)
147     {
148       // MalformedURLException, FileNotFoundException
149       return false;
150     } finally
151     {
152       if (is != null)
153       {
154         try
155         {
156           is.close();
157         } catch (IOException e)
158         {
159           // ignore
160         }
161       }
162     }
163     return false;
164   }
165
166   @Override
167   public String[] getPdbFile()
168   {
169     return modelSet;
170   }
171
172   public void mouseOverStructure(int atomIndex, String strInfo)
173   {
174
175     // StructureSelectionManager.getStructureSelectionManager().mouseOverStructure(atomIndex,
176     // chain, pdbfile)
177     // TODO Auto-generated method stub
178
179   }
180
181   @Override
182   public void highlightAtoms(List<AtomSpec> atoms)
183   {
184     for (AtomSpec atom : atoms)
185     {
186       try
187       {
188         // TODO is this right? StructureSelectionManager passes pdbFile as the
189         // field that is interpreted (in 2.8.2) as pdbId?
190         // JBPComment: yep - this is right! the Javascript harness uses the
191         // absolute pdbFile URI to locate the PDB file in the external viewer
192         executeJavascriptFunction(_listenerfn,
193                 new String[] { "mouseover", "" + atom.getPdbFile(),
194                     "" + atom.getChain(), "" + (atom.getPdbResNum()),
195                     "" + atom.getAtomIndex() });
196       } catch (Exception ex)
197       {
198         System.err.println("Couldn't execute callback with " + _listenerfn
199                 + " for atomSpec: " + atom);
200         ex.printStackTrace();
201       }
202     }
203   }
204
205   @Override
206   public synchronized void updateColours(Object srce)
207   {
208     final Object source = srce;
209     StructureSelectionManager ssm = StructureSelectionManager
210             .getStructureSelectionManager(jvlite);
211
212     if (JalviewLite.debug)
213     {
214       System.err.println(this.getClass().getName() + " modelSet[0]: "
215               + modelSet[0]);
216       ssm.reportMapping();
217     }
218
219     if (source instanceof jalview.api.AlignmentViewPanel)
220     {
221       SequenceI[][] sequence = new SequenceI[modelSet.length][];
222       for (int m = 0; m < modelSet.length; m++)
223       {
224         StructureMapping[] sm = ssm.getMapping(modelSet[m]);
225         if (sm != null && sm.length > 0)
226         {
227           sequence[m] = new SequenceI[sm.length];
228           for (int i = 0; i < sm.length; i++)
229           {
230             sequence[m][i] = sm[i].getSequence();
231           }
232         }
233         else
234         {
235           sequence[m] = new SequenceI[0];
236         }
237         // if (jvlite.debug)
238         // {
239         // System.err.println("Mapped '" + modelSet[m] + "' to "
240         // + sequence[m].length + " sequences.");
241         // }
242       }
243
244       SequenceRenderer sr = ((jalview.appletgui.AlignmentPanel) source)
245               .getSequenceRenderer();
246       FeatureRenderer fr = ((jalview.appletgui.AlignmentPanel) source).av
247               .isShowSequenceFeatures() ? new jalview.appletgui.FeatureRenderer(
248               ((jalview.appletgui.AlignmentPanel) source).av) : null;
249       if (fr != null)
250       {
251         ((jalview.appletgui.FeatureRenderer) fr)
252                 .transferSettings(((jalview.appletgui.AlignmentPanel) source)
253                         .getFeatureRenderer());
254       }
255       ;
256
257       // Form a colour command from the given alignment panel for each distinct
258       // structure
259       ArrayList<String[]> ccomands = new ArrayList<String[]>();
260       ArrayList<String> pdbfn = new ArrayList<String>();
261       StructureMappingcommandSet[] colcommands = JmolCommands
262               .getColourBySequenceCommand(ssm, modelSet, sequence, sr, fr,
263                       ((AlignmentViewPanel) source).getAlignment());
264       if (colcommands == null)
265       {
266         return;
267       }
268       int sz = 0;
269       for (jalview.structure.StructureMappingcommandSet ccset : colcommands)
270       {
271         sz += ccset.commands.length;
272         ccomands.add(ccset.commands);
273         pdbfn.add(ccset.mapping);
274       }
275
276       String mclass, mhandle;
277       String ccomandset[] = new String[sz];
278       sz = 0;
279       for (String[] ccset : ccomands)
280       {
281         System.arraycopy(ccset, 0, ccomandset, sz, ccset.length);
282         sz += ccset.length;
283       }
284       if (jvlite.isJsMessageSetChanged(
285               mclass = "colourstruct",
286               mhandle = ((jalview.appletgui.AlignmentPanel) source).av
287                       .getViewId(), ccomandset))
288       {
289         jvlite.setJsMessageSet(mclass, mhandle, ccomandset);
290         // and notify javascript handler
291         String st[] = new String[] {
292             "colourstruct",
293             "" + ((jalview.appletgui.AlignmentPanel) source).av.getViewId(),
294             "" + ccomandset.length,
295             jvlite.arrayToSeparatorList(pdbfn.toArray(new String[pdbfn
296                     .size()])) };
297         try
298         {
299           executeJavascriptFunction(true, _listenerfn, st);
300         } catch (Exception ex)
301         {
302           System.err.println("Couldn't execute callback with "
303                   + _listenerfn + " using args { " + st[0] + ", " + st[1]
304                   + ", " + st[2] + "," + st[3] + "}"); // + ","+st[4]+"\n");
305           ex.printStackTrace();
306
307         }
308       }
309       /*
310        * new Thread(new Runnable() { public void run() { // and send to
311        * javascript handler String st[] = new String[0]; int i = 0; for (String
312        * colcommand : colcommands) { // do sync execution for each chunk try {
313        * executeJavascriptFunction( false, _listenerfn, st = new String[] {
314        * "colourstruct", "" + ((jalview.appletgui.AlignmentPanel) source).av
315        * .getViewId(), handle, "" }); } catch (Exception ex) {
316        * System.err.println("Couldn't execute callback with " + _listenerfn +
317        * " using args { " + st[0] + ", " + st[1] + ", " + st[2] + "," + st[3] +
318        * "\n"); ex.printStackTrace();
319        * 
320        * } } } }).start();
321        */
322     }
323
324   }
325
326   @Override
327   public AlignFrame getAlignFrame()
328   {
329     // associated with all alignframes, always.
330     return null;
331   }
332
333   @Override
334   public String getListenerFunction()
335   {
336     return _listenerfn;
337   }
338
339   public void finalise()
340   {
341     jvlite = null;
342     super.finalize();
343   }
344
345   @Override
346   public void releaseReferences(Object svl)
347   {
348
349     // TODO Auto-generated method stub
350
351   }
352
353   @Override
354   public boolean isListeningFor(SequenceI seq)
355   {
356     return true;
357   }
358
359 }