2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.javascript;
20 import java.awt.Color;
21 import java.util.ArrayList;
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.StructureListener;
31 import jalview.structure.StructureMapping;
32 import jalview.structure.StructureMappingcommandSet;
33 import jalview.structure.StructureSelectionManager;
36 * Propagate events involving PDB structures associated with sequences to a
37 * javascript function. Generally, the javascript handler is called with a
38 * series of arguments like (eventname, ... ). As of Jalview 2.7, the following
39 * different types of events are supported:
41 * <li>mouseover: javascript function called with arguments
44 * ['mouseover',String(pdb file URI), String(pdb file chain ID), String(residue
45 * number moused over), String(atom index corresponding to residue)]
49 * <li>colourstruct: javascript function called with arguments
52 * ['colourstruct',String(alignment view id),String(number of javascript message
53 * chunks to collect),String(length of first chunk in set of messages - or zero
58 * The message contains a series of Jmol script commands that will colour
59 * structures according to their associated sequences in the current view. Use
61 * .javascript.JalviewLiteJsApi.getJsMessage('colourstruct',String(alignment
62 * view id)) to retrieve successive chunks of the message.</li>
65 * @author Jim Procter (jprocter)
68 public class MouseOverStructureListener extends JSFunctionExec implements
69 JsCallBack, StructureListener
76 public MouseOverStructureListener(JalviewLite jalviewLite,
77 String listener, String[] modelList)
80 _listenerfn = listener;
84 for (int i = 0; i < modelSet.length; i++)
86 // resolve a real filename
89 if (new java.net.URL(modelSet[i]).openConnection() != null)
99 String db = jvlite.getDocumentBase().toString();
100 db = db.substring(0, db.lastIndexOf("/"));
101 if (new java.net.URL(db + "/" + modelSet[i]).openConnection() != null)
103 modelSet[i] = db + "/" + modelSet[i];
106 } catch (Exception x)
112 if (new java.net.URL(jvlite.getCodeBase() + modelSet[i])
113 .openConnection() != null)
115 modelSet[i] = jvlite.getCodeBase() + modelSet[i];
118 } catch (Exception x)
128 public String[] getPdbFile()
134 public void mouseOverStructure(int atomIndex, String strInfo)
137 // StructureSelectionManager.getStructureSelectionManager().mouseOverStructure(atomIndex,
139 // TODO Auto-generated method stub
144 public void highlightAtom(int atomIndex, int pdbResNum, String chain,
147 String[] st = new String[0];
150 executeJavascriptFunction(_listenerfn, st = new String[]
151 { "mouseover", "" + pdbId, "" + chain, "" + (pdbResNum),
153 } catch (Exception ex)
155 System.err.println("Couldn't execute callback with " + _listenerfn
156 + " using args { " + st[0] + ", " + st[1] + ", " + st[2]
157 + "," + st[3] + "\n");
158 ex.printStackTrace();
165 public synchronized void updateColours(Object srce)
167 final Object source = srce;
168 StructureSelectionManager ssm = StructureSelectionManager
169 .getStructureSelectionManager(jvlite);
172 // ssm.reportMapping();
174 if (source instanceof jalview.api.AlignmentViewPanel)
176 SequenceI[][] sequence = new SequenceI[modelSet.length][];
177 for (int m = 0; m < modelSet.length; m++)
179 StructureMapping[] sm = ssm.getMapping(modelSet[m]);
180 if (sm != null && sm.length > 0)
182 sequence[m] = new SequenceI[sm.length];
183 for (int i = 0; i < sm.length; i++)
185 sequence[m][i] = sm[i].getSequence();
190 sequence[m] = new SequenceI[0];
194 // System.err.println("Mapped '" + modelSet[m] + "' to "
195 // + sequence[m].length + " sequences.");
199 SequenceRenderer sr = ((jalview.appletgui.AlignmentPanel) source)
200 .getSequenceRenderer();
201 FeatureRenderer fr = ((jalview.appletgui.AlignmentPanel) source).av
202 .getShowSequenceFeatures() ? new jalview.appletgui.FeatureRenderer(
203 ((jalview.appletgui.AlignmentPanel) source).av) : null;
206 ((jalview.appletgui.FeatureRenderer) fr)
207 .transferSettings(((jalview.appletgui.AlignmentPanel) source)
208 .getFeatureRenderer());
212 // Form a colour command from the given alignment panel for each distinct
214 ArrayList<String[]> ccomands = new ArrayList<String[]>();
215 ArrayList<String> pdbfn = new ArrayList<String>();
216 StructureMappingcommandSet[] colcommands = JmolCommands
217 .getColourBySequenceCommand(ssm, modelSet, sequence, sr, fr,
218 ((AlignmentViewPanel) source).getAlignment());
219 if (colcommands == null)
224 for (jalview.structure.StructureMappingcommandSet ccset : colcommands)
226 sz += ccset.commands.length;
227 ccomands.add(ccset.commands);
228 pdbfn.add(ccset.mapping);
231 String mclass, mhandle;
232 String ccomandset[] = new String[sz];
234 for (String[] ccset : ccomands)
236 System.arraycopy(ccset, 0, ccomandset, sz, ccset.length);
239 if (jvlite.isJsMessageSetChanged(
240 mclass = "colourstruct",
241 mhandle = ((jalview.appletgui.AlignmentPanel) source).av
242 .getViewId(), ccomandset))
244 jvlite.setJsMessageSet(mclass, mhandle, ccomandset);
245 // and notify javascript handler
246 String st[] = new String[]
249 "" + ((jalview.appletgui.AlignmentPanel) source).av.getViewId(),
250 "" + ccomandset.length,
251 jvlite.arrayToSeparatorList(pdbfn.toArray(new String[pdbfn
255 executeJavascriptFunction(true, _listenerfn, st);
256 } catch (Exception ex)
258 System.err.println("Couldn't execute callback with "
259 + _listenerfn + " using args { " + st[0] + ", " + st[1]
260 + ", " + st[2] + "," + st[3] + "}"); // + ","+st[4]+"\n");
261 ex.printStackTrace();
266 * new Thread(new Runnable() { public void run() { // and send to
267 * javascript handler String st[] = new String[0]; int i = 0; for (String
268 * colcommand : colcommands) { // do sync execution for each chunk try {
269 * executeJavascriptFunction( false, _listenerfn, st = new String[] {
270 * "colourstruct", "" + ((jalview.appletgui.AlignmentPanel) source).av
271 * .getViewId(), handle, "" }); } catch (Exception ex) {
272 * System.err.println("Couldn't execute callback with " + _listenerfn +
273 * " using args { " + st[0] + ", " + st[1] + ", " + st[2] + "," + st[3] +
274 * "\n"); ex.printStackTrace();
283 public Color getColour(int atomIndex, int pdbResNum, String chain,
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