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 java.util.ArrayList;
24 import java.util.List;
26 import jalview.api.AlignmentViewPanel;
27 import jalview.api.FeatureRenderer;
28 import jalview.api.SequenceRenderer;
29 import jalview.appletgui.AlignFrame;
30 import jalview.bin.JalviewLite;
31 import jalview.datamodel.SequenceI;
32 import jalview.ext.jmol.JmolCommands;
33 import jalview.structure.AtomSpec;
34 import jalview.structure.StructureListener;
35 import jalview.structure.StructureMapping;
36 import jalview.structure.StructureMappingcommandSet;
37 import jalview.structure.StructureSelectionManager;
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:
45 * <li>mouseover: javascript function called with arguments
48 * ['mouseover',String(pdb file URI), String(pdb file chain ID), String(residue
49 * number moused over), String(atom index corresponding to residue)]
53 * <li>colourstruct: javascript function called with arguments
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
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
65 * .javascript.JalviewLiteJsApi.getJsMessage('colourstruct',String(alignment
66 * view id)) to retrieve successive chunks of the message.</li>
69 * @author Jim Procter (jprocter)
72 public class MouseOverStructureListener extends JSFunctionExec implements
73 JsCallBack, StructureListener
80 public MouseOverStructureListener(JalviewLite jalviewLite,
81 String listener, String[] modelList)
84 _listenerfn = listener;
88 for (int i = 0; i < modelSet.length; i++)
90 // resolve a real filename
93 if (new java.net.URL(modelSet[i]).openConnection() != null)
103 String db = jvlite.getDocumentBase().toString();
104 db = db.substring(0, db.lastIndexOf("/"));
105 if (new java.net.URL(db + "/" + modelSet[i]).openConnection() != null)
107 modelSet[i] = db + "/" + modelSet[i];
110 } catch (Exception x)
116 if (new java.net.URL(jvlite.getCodeBase() + modelSet[i])
117 .openConnection() != null)
119 modelSet[i] = jvlite.getCodeBase() + modelSet[i];
122 } catch (Exception x)
132 public String[] getPdbFile()
137 public void mouseOverStructure(int atomIndex, String strInfo)
140 // StructureSelectionManager.getStructureSelectionManager().mouseOverStructure(atomIndex,
142 // TODO Auto-generated method stub
147 public void highlightAtoms(List<AtomSpec> atoms)
149 for (AtomSpec atom : atoms)
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, new String[]
158 { "mouseover", "" + atom.getPdbFile(),
159 "" + atom.getChain(),
160 "" + (atom.getPdbResNum()), "" + atom.getAtomIndex() });
161 } catch (Exception ex)
163 System.err.println("Couldn't execute callback with " + _listenerfn
164 + " for atomSpec: " + atom);
165 ex.printStackTrace();
171 public synchronized void updateColours(Object srce)
173 final Object source = srce;
174 StructureSelectionManager ssm = StructureSelectionManager
175 .getStructureSelectionManager(jvlite);
178 // ssm.reportMapping();
180 if (source instanceof jalview.api.AlignmentViewPanel)
182 SequenceI[][] sequence = new SequenceI[modelSet.length][];
183 for (int m = 0; m < modelSet.length; m++)
185 StructureMapping[] sm = ssm.getMapping(modelSet[m]);
186 if (sm != null && sm.length > 0)
188 sequence[m] = new SequenceI[sm.length];
189 for (int i = 0; i < sm.length; i++)
191 sequence[m][i] = sm[i].getSequence();
196 sequence[m] = new SequenceI[0];
200 // System.err.println("Mapped '" + modelSet[m] + "' to "
201 // + sequence[m].length + " sequences.");
205 SequenceRenderer sr = ((jalview.appletgui.AlignmentPanel) source)
206 .getSequenceRenderer();
207 FeatureRenderer fr = ((jalview.appletgui.AlignmentPanel) source).av
208 .isShowSequenceFeatures() ? new jalview.appletgui.FeatureRenderer(
209 ((jalview.appletgui.AlignmentPanel) source).av) : null;
212 ((jalview.appletgui.FeatureRenderer) fr)
213 .transferSettings(((jalview.appletgui.AlignmentPanel) source)
214 .getFeatureRenderer());
218 // Form a colour command from the given alignment panel for each distinct
220 ArrayList<String[]> ccomands = new ArrayList<String[]>();
221 ArrayList<String> pdbfn = new ArrayList<String>();
222 StructureMappingcommandSet[] colcommands = JmolCommands
223 .getColourBySequenceCommand(ssm, modelSet, sequence, sr, fr,
224 ((AlignmentViewPanel) source).getAlignment());
225 if (colcommands == null)
230 for (jalview.structure.StructureMappingcommandSet ccset : colcommands)
232 sz += ccset.commands.length;
233 ccomands.add(ccset.commands);
234 pdbfn.add(ccset.mapping);
237 String mclass, mhandle;
238 String ccomandset[] = new String[sz];
240 for (String[] ccset : ccomands)
242 System.arraycopy(ccset, 0, ccomandset, sz, ccset.length);
245 if (jvlite.isJsMessageSetChanged(
246 mclass = "colourstruct",
247 mhandle = ((jalview.appletgui.AlignmentPanel) source).av
248 .getViewId(), ccomandset))
250 jvlite.setJsMessageSet(mclass, mhandle, ccomandset);
251 // and notify javascript handler
252 String st[] = new String[]
255 "" + ((jalview.appletgui.AlignmentPanel) source).av.getViewId(),
256 "" + ccomandset.length,
257 jvlite.arrayToSeparatorList(pdbfn.toArray(new String[pdbfn
261 executeJavascriptFunction(true, _listenerfn, st);
262 } catch (Exception ex)
264 System.err.println("Couldn't execute callback with "
265 + _listenerfn + " using args { " + st[0] + ", " + st[1]
266 + ", " + st[2] + "," + st[3] + "}"); // + ","+st[4]+"\n");
267 ex.printStackTrace();
272 * new Thread(new Runnable() { public void run() { // and send to
273 * javascript handler String st[] = new String[0]; int i = 0; for (String
274 * colcommand : colcommands) { // do sync execution for each chunk try {
275 * executeJavascriptFunction( false, _listenerfn, st = new String[] {
276 * "colourstruct", "" + ((jalview.appletgui.AlignmentPanel) source).av
277 * .getViewId(), handle, "" }); } catch (Exception ex) {
278 * System.err.println("Couldn't execute callback with " + _listenerfn +
279 * " using args { " + st[0] + ", " + st[1] + ", " + st[2] + "," + st[3] +
280 * "\n"); ex.printStackTrace();
289 public AlignFrame getAlignFrame()
291 // associated with all alignframes, always.
296 public String getListenerFunction()
301 public void finalise()
308 public void releaseReferences(Object svl)
311 // TODO Auto-generated method stub
316 public boolean isListeningFor(SequenceI seq)