2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.javascript;
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;
36 import java.io.IOException;
37 import java.io.InputStream;
39 import java.util.ArrayList;
40 import java.util.List;
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:
48 * <li>mouseover: javascript function called with arguments
51 * ['mouseover',String(pdb file URI), String(pdb file chain ID), String(residue
52 * number moused over), String(atom index corresponding to residue)]
56 * <li>colourstruct: javascript function called with arguments
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
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
68 * .javascript.JalviewLiteJsApi.getJsMessage('colourstruct',String(alignment
69 * view id)) to retrieve successive chunks of the message.</li>
72 * @author Jim Procter (jprocter)
75 public class MouseOverStructureListener extends JSFunctionExec implements
76 JsCallBack, StructureListener
83 public MouseOverStructureListener(JalviewLite jalviewLite,
84 String listener, String[] modelList)
87 _listenerfn = listener;
91 for (int i = 0; i < modelSet.length; i++)
93 modelSet[i] = resolveModelFile(modelSet[i]);
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.
105 public String resolveModelFile(String file)
107 if (isValidUrl(file))
112 String db = jvlite.getDocumentBase().toString();
113 db = db.substring(0, db.lastIndexOf("/"));
114 String docBaseFile = db + "/" + file;
115 if (isValidUrl(docBaseFile))
120 String cb = jvlite.getCodeBase() + file;
130 * Returns true if it is possible to open an input stream at the given URL,
131 * else false. The input stream is closed.
136 private boolean isValidUrl(String file)
138 InputStream is = null;
141 is = new URL(file).openStream();
146 } catch (IOException x)
148 // MalformedURLException, FileNotFoundException
157 } catch (IOException e)
167 public String[] getPdbFile()
172 public void mouseOverStructure(int atomIndex, String strInfo)
175 // StructureSelectionManager.getStructureSelectionManager().mouseOverStructure(atomIndex,
177 // TODO Auto-generated method stub
182 public void highlightAtoms(List<AtomSpec> atoms)
184 for (AtomSpec atom : atoms)
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)
198 System.err.println("Couldn't execute callback with " + _listenerfn
199 + " for atomSpec: " + atom);
200 ex.printStackTrace();
206 public synchronized void updateColours(Object srce)
208 final Object source = srce;
209 StructureSelectionManager ssm = StructureSelectionManager
210 .getStructureSelectionManager(jvlite);
212 if (JalviewLite.debug)
214 System.err.println(this.getClass().getName() + " modelSet[0]: "
219 if (source instanceof jalview.api.AlignmentViewPanel)
221 SequenceI[][] sequence = new SequenceI[modelSet.length][];
222 for (int m = 0; m < modelSet.length; m++)
224 StructureMapping[] sm = ssm.getMapping(modelSet[m]);
225 if (sm != null && sm.length > 0)
227 sequence[m] = new SequenceI[sm.length];
228 for (int i = 0; i < sm.length; i++)
230 sequence[m][i] = sm[i].getSequence();
235 sequence[m] = new SequenceI[0];
239 // System.err.println("Mapped '" + modelSet[m] + "' to "
240 // + sequence[m].length + " sequences.");
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;
251 ((jalview.appletgui.FeatureRenderer) fr)
252 .transferSettings(((jalview.appletgui.AlignmentPanel) source)
253 .getFeatureRenderer());
257 // Form a colour command from the given alignment panel for each distinct
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)
269 for (jalview.structure.StructureMappingcommandSet ccset : colcommands)
271 sz += ccset.commands.length;
272 ccomands.add(ccset.commands);
273 pdbfn.add(ccset.mapping);
276 String mclass, mhandle;
277 String ccomandset[] = new String[sz];
279 for (String[] ccset : ccomands)
281 System.arraycopy(ccset, 0, ccomandset, sz, ccset.length);
284 if (jvlite.isJsMessageSetChanged(
285 mclass = "colourstruct",
286 mhandle = ((jalview.appletgui.AlignmentPanel) source).av
287 .getViewId(), ccomandset))
289 jvlite.setJsMessageSet(mclass, mhandle, ccomandset);
290 // and notify javascript handler
291 String st[] = new String[] {
293 "" + ((jalview.appletgui.AlignmentPanel) source).av.getViewId(),
294 "" + ccomandset.length,
295 jvlite.arrayToSeparatorList(pdbfn.toArray(new String[pdbfn
299 executeJavascriptFunction(true, _listenerfn, st);
300 } catch (Exception ex)
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();
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();
327 public AlignFrame getAlignFrame()
329 // associated with all alignframes, always.
334 public String getListenerFunction()
339 public void finalise()
346 public void releaseReferences(Object svl)
349 // TODO Auto-generated method stub
354 public boolean isListeningFor(SequenceI seq)