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.appletgui.AlignmentPanel;
28 import jalview.bin.JalviewLite;
29 import jalview.datamodel.SequenceI;
30 import jalview.ext.jmol.JmolCommands;
31 import jalview.structure.AtomSpec;
32 import jalview.structure.StructureListener;
33 import jalview.structure.StructureMapping;
34 import jalview.structure.StructureMappingcommandSet;
35 import jalview.structure.StructureSelectionManager;
37 import java.util.ArrayList;
38 import java.util.List;
41 * Propagate events involving PDB structures associated with sequences to a
42 * javascript function. Generally, the javascript handler is called with a
43 * series of arguments like (eventname, ... ). As of Jalview 2.7, the following
44 * different types of events are supported:
46 * <li>mouseover: javascript function called with arguments
49 * ['mouseover',String(pdb file URI), String(pdb file chain ID), String(residue
50 * number moused over), String(atom index corresponding to residue)]
54 * <li>colourstruct: javascript function called with arguments
57 * ['colourstruct',String(alignment view id),String(number of javascript message
58 * chunks to collect),String(length of first chunk in set of messages - or zero
63 * The message contains a series of Jmol script commands that will colour
64 * structures according to their associated sequences in the current view. Use
66 * .javascript.JalviewLiteJsApi.getJsMessage('colourstruct',String(alignment
67 * view id)) to retrieve successive chunks of the message.</li>
70 * @author Jim Procter (jprocter)
73 public class MouseOverStructureListener extends JSFunctionExec implements
74 JsCallBack, StructureListener
81 public MouseOverStructureListener(JalviewLite jalviewLite,
82 String listener, String[] modelList)
85 _listenerfn = listener;
89 for (int i = 0; i < modelSet.length; i++)
91 // resolve a real filename
94 if (new java.net.URL(modelSet[i]).openConnection() != null)
104 String db = jvlite.getDocumentBase().toString();
105 db = db.substring(0, db.lastIndexOf("/"));
106 if (new java.net.URL(db + "/" + modelSet[i]).openConnection() != null)
108 modelSet[i] = db + "/" + modelSet[i];
111 } catch (Exception x)
117 if (new java.net.URL(jvlite.getCodeBase() + modelSet[i])
118 .openConnection() != null)
120 modelSet[i] = jvlite.getCodeBase() + modelSet[i];
123 } catch (Exception x)
133 public String[] getPdbFile()
138 public void mouseOverStructure(int atomIndex, String strInfo)
141 // StructureSelectionManager.getStructureSelectionManager().mouseOverStructure(atomIndex,
143 // TODO Auto-generated method stub
148 public void highlightAtoms(List<AtomSpec> atoms)
150 for (AtomSpec atom : atoms)
154 // TODO is this right? StructureSelectionManager passes pdbFile as the
155 // field that is interpreted (in 2.8.2) as pdbId?
156 // JBPComment: yep - this is right! the Javascript harness uses the
157 // absolute pdbFile URI to locate the PDB file in the external viewer
158 executeJavascriptFunction(_listenerfn, new String[]
159 { "mouseover", "" + atom.getPdbFile(),
160 "" + atom.getChain(),
161 "" + (atom.getPdbResNum()), "" + atom.getAtomIndex() });
162 } catch (Exception ex)
164 System.err.println("Couldn't execute callback with " + _listenerfn
165 + " for atomSpec: " + atom);
166 ex.printStackTrace();
172 public synchronized void updateColours(Object srce)
174 final Object source = srce;
175 StructureSelectionManager ssm = StructureSelectionManager
176 .getStructureSelectionManager(jvlite);
179 // ssm.reportMapping();
181 if (source instanceof AlignmentViewPanel)
183 SequenceI[][] sequence = new SequenceI[modelSet.length][];
184 for (int m = 0; m < modelSet.length; m++)
186 StructureMapping[] sm = ssm.getMapping(modelSet[m]);
187 if (sm != null && sm.length > 0)
189 sequence[m] = new SequenceI[sm.length];
190 for (int i = 0; i < sm.length; i++)
192 sequence[m][i] = sm[i].getSequence();
197 sequence[m] = new SequenceI[0];
201 // System.err.println("Mapped '" + modelSet[m] + "' to "
202 // + sequence[m].length + " sequences.");
206 SequenceRenderer sr = ((AlignmentPanel) source)
207 .getSequenceRenderer();
208 FeatureRenderer fr = ((AlignmentPanel) source).av
209 .isShowSequenceFeatures() ? new jalview.appletgui.FeatureRenderer(
210 ((AlignmentPanel) source).av) : null;
213 ((jalview.appletgui.FeatureRenderer) fr)
214 .transferSettings(((AlignmentPanel) source)
215 .getFeatureRenderer());
219 // Form a colour command from the given alignment panel for each distinct
221 ArrayList<String[]> ccomands = new ArrayList<String[]>();
222 ArrayList<String> pdbfn = new ArrayList<String>();
223 StructureMappingcommandSet[] colcommands = JmolCommands
224 .getColourBySequenceCommand(ssm, modelSet, sequence, sr, fr,
225 ((AlignmentViewPanel) source).getAlignment());
226 if (colcommands == null)
231 for (StructureMappingcommandSet ccset : colcommands)
233 sz += ccset.commands.length;
234 ccomands.add(ccset.commands);
235 pdbfn.add(ccset.mapping);
238 String mclass, mhandle;
239 String ccomandset[] = new String[sz];
241 for (String[] ccset : ccomands)
243 System.arraycopy(ccset, 0, ccomandset, sz, ccset.length);
246 if (jvlite.isJsMessageSetChanged(
247 mclass = "colourstruct",
248 mhandle = ((AlignmentPanel) source).av
249 .getViewId(), ccomandset))
251 jvlite.setJsMessageSet(mclass, mhandle, ccomandset);
252 // and notify javascript handler
253 String st[] = new String[]
256 "" + ((AlignmentPanel) source).av.getViewId(),
257 "" + ccomandset.length,
258 jvlite.arrayToSeparatorList(pdbfn.toArray(new String[pdbfn
262 executeJavascriptFunction(true, _listenerfn, st);
263 } catch (Exception ex)
265 System.err.println("Couldn't execute callback with "
266 + _listenerfn + " using args { " + st[0] + ", " + st[1]
267 + ", " + st[2] + "," + st[3] + "}"); // + ","+st[4]+"\n");
268 ex.printStackTrace();
273 * new Thread(new Runnable() { public void run() { // and send to
274 * javascript handler String st[] = new String[0]; int i = 0; for (String
275 * colcommand : colcommands) { // do sync execution for each chunk try {
276 * executeJavascriptFunction( false, _listenerfn, st = new String[] {
277 * "colourstruct", "" + ((jalview.appletgui.AlignmentPanel) source).av
278 * .getViewId(), handle, "" }); } catch (Exception ex) {
279 * System.err.println("Couldn't execute callback with " + _listenerfn +
280 * " using args { " + st[0] + ", " + st[1] + ", " + st[2] + "," + st[3] +
281 * "\n"); ex.printStackTrace();
290 public AlignFrame getAlignFrame()
292 // associated with all alignframes, always.
297 public String getListenerFunction()
302 public void finalise()
309 public void releaseReferences(Object svl)
312 // TODO Auto-generated method stub
317 public boolean isListeningFor(SequenceI seq)