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