apply version 2.7 copyright
[jalview.git] / examples / javascript / jalview.js
1 /*******************************************************************************
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  *******************************************************************************/
18 // default console to report messages\r
19 var _console = document.getElementById("stdout");\r
20 var _jvapps = new Array();\r
21 // jvjmols is a list associating a jmol id to { modelstofiles }\r
22 var _jvjmols = new Hashtable();\r
23 // array of model names used to lookup index in Jmol\r
24 var _modeltofiles = new Array();\r
25 // counter for jmol structures\r
26 var mnum = 1;\r
27 \r
28 function setConsole(console) {\r
29         _console = console;\r
30 }\r
31 \r
32 function getDestinationFrms(source, frames) {\r
33         var frms = new Array();\r
34         var frid = "";\r
35         for (frm in frames) {\r
36                 try {\r
37                         frid = (source!=null) && (("" + source.getSequenceSetId()) == ("" + frames[frm].currentAlignFrame\r
38                                         .getSequenceSetId()));\r
39                 } catch (q) {\r
40                 };\r
41                 \r
42                 if (!frames[frm].equals(source) && !frid\r
43                                 && !frames[frm].currentAlignFrame.equals(source)) {\r
44                         frms[frms.length] = frames[frm];\r
45                 }\r
46         }\r
47         return frms;\r
48 }\r
49 \r
50 function mouseover(list1, list2, list3, list4) {\r
51         // list1 = new Object(list1);\r
52         var list = new Array(("" + list1), ("" + list2), ("" + list3), ("" + list4));\r
53         var msg = "Mouse over :\n" + "AlignFrame obj: " + list1 + " Seq : "\r
54                         + list[1] + "\nPos: " + list[2] + "(" + list[3] + ")\n";\r
55 \r
56         var flist = getDestinationFrms(list1, _jvapps);\r
57         if (_console) {\r
58                 _console.value = msg + "\n";\r
59         }\r
60 \r
61         for (follower in flist) {\r
62                 if (_console) {\r
63                         _console.value += "Sending to " + flist[follower] + "\n";\r
64                 }\r
65                 flist[follower].highlight(list[1], list[2], "true");\r
66         }\r
67         return true;\r
68 }\r
69 \r
70 function sellist(list1, list2, list3, list4) {\r
71         // list1 = new Object(list1);\r
72         var list = new Array(("" + list1), ("" + list2), ("" + list3), ("" + list4));\r
73         var msg = "Selection:\n" + "AlignFrame obj: " + list[0] + " id : "\r
74                         + list[1] + "\nSeqs " + list[2] + "\nColumns " + list[3] + "\n";\r
75         var flist = getDestinationFrms(list1, _jvapps);\r
76         if (_console) {\r
77                 _console.value = msg + "\n";\r
78         }\r
79         \r
80         for (follower in flist) {\r
81                 if (_console) {\r
82                         _console.value += "Sending to " + flist[follower] + "\n";\r
83                 }\r
84                 flist[follower].selectIn(flist[follower].getDefaultTargetFrame(),\r
85                                 list[2], list[3])\r
86         }\r
87         return true;\r
88 }\r
89 \r
90 function viewlist(list1, list2, list3, list4) {\r
91         // list1 = new Object(list1);\r
92         var list = new Array(("" + list1), ("" + list2), ("" + list3), ("" + list4));\r
93         var msg = "Viewport extent change::\n" + "AlignFrame obj: " + list[0]\r
94                         + " id : " + list[1] + "\nRow from " + list[2] + " and to "\r
95                         + list[3] + "\nVisible columns: " + list[4] + "\n";\r
96         var flist = getDestinationFrms(list1, _jvapps);\r
97         if (_console) {\r
98                 _console.value = msg + "\n";\r
99         }\r
100 \r
101         for (follower in flist) {\r
102                 if (_console) {\r
103                         _console.value += "Sending to " + flist[follower] + "\n";\r
104                 }\r
105                 flist[follower].scrollToViewIn(flist[follower].getDefaultTargetFrame(),\r
106                                 list[2], "-1");\r
107         }\r
108         return true;\r
109 }\r
110 \r
111 // register a jalview applet and add some handlers to it\r
112 // jmolView is a reference to a jmol applet that is displaying the PDB files listed (in order) in the modeltofiles Array\r
113 function linkJvJmol(applet, jmolView, modeltofiles) {\r
114         var i = _jvapps.length;\r
115         while (i--) {\r
116                 if (_jvapps[i].equals(applet)) {\r
117                         throw ("Ignoring additional linkJvJmol call for "\r
118                                         + applet.getName() + ".");\r
119                 }\r
120         }\r
121         _jvapps[_jvapps.length] = applet;\r
122         applet.setMouseoverListener("mouseover");\r
123         applet.setSelectionListener("sellist");\r
124         // viewListener not fully implemented in 2.7\r
125         // try { applet.setViewListener("viewlist"); } catch (err) {};\r
126         if (jmolView)\r
127         {\r
128                 var sep = applet.getSeparator();\r
129                 var oldjm=jmolView;\r
130                 // recover full id of Jmol applet\r
131                 jmolView=_jmolGetApplet(jmolView).id;\r
132                 var jmbinding=_jvjmols.get(jmolView);\r
133                 if (!jmbinding)\r
134                 {       \r
135                         jmbinding=new Object();\r
136                         jmbinding._modelstofiles=new Array();\r
137                         jmbinding._fullmpath=new Array();\r
138                         jmbinding._filetonum=new Hashtable();\r
139                         jmbinding._jmol=jmolView;\r
140                         jmbinding._jmhandle=oldjm;\r
141                         _jvjmols.put(jmolView,jmbinding);\r
142                 }\r
143                 \r
144                 jmbinding._modelstofiles=jmbinding._modelstofiles.concat(jmbinding._modelstofiles,modeltofiles);\r
145                 jmbinding._jmol=jmolView;\r
146                 // now update structureListener list\r
147                 mtf="";\r
148                 var dbase = document.baseURI.substring(0,document.baseURI.lastIndexOf("/")+1);\r
149                 for (m in jmbinding._modelstofiles)\r
150                 { if (m>0) { mtf+=sep; }\r
151                 mtf+=jmbinding._modelstofiles[m];\r
152                 if (jmbinding._modelstofiles[m].indexOf("//")==-1)\r
153                         { jmbinding._fullmpath[m] = dbase+((jmbinding._modelstofiles[m].indexOf("/")==0) ? jmbinding._modelstofiles[m].substring(1) : jmbinding._modelstofiles[m]); }\r
154                   jmbinding._filetonum.put(jmbinding._modelstofiles[m], m+1); \r
155                   jmbinding._filetonum.put(jmbinding._fullmpath[m], m+1);\r
156                   \r
157                   }\r
158                 applet.setStructureListener("_structure", mtf);\r
159         }\r
160 }\r
161 \r
162 /*function _addJmolModel(jmolid, modelname) {\r
163         modelname=""+modelname;\r
164         var jminf = _jvjmols[jmolid];\r
165         if (!jminf) {\r
166                 jminf = new Object();\r
167                 jminf._modelstofiles = new Array(); //new Hashtable();\r
168                 jminf._jmol = jmolid;\r
169                 jminf._modellist=new Array();\r
170                 _jvjmols[jmolid] = jminf;\r
171         }\r
172         var obj = new Object();\r
173         jminf._modeltofiles[modelname] = obj; // .put(modelname, obj);\r
174         obj.id = modelname;\r
175         obj.mnum = jminf._modeltofiles.length;\r
176         jminf._modellist+=modelname;\r
177 }*/\r
178 \r
179 \r
180 \r
181 // jmol Jalview Methods\r
182 \r
183 function _structure(list1, list2, list3, list4) {\r
184         var follower;\r
185         // if (_console) { if (!_console.value) { _console.value="";} }\r
186         if (list1 == "mouseover") {\r
187                 var list = new Array(("" + list1), ("" + list2), ("" + list3),\r
188                                 ("" + list4));\r
189                 // 1 is pdb file, 2 is residue number, 3 is chain\r
190                 // list1 = new Object(list1);\r
191                 var base = list[1].indexOf(document.baseURI\r
192                                 .substring(0, document.baseURI.lastIndexOf('/'))\r
193                                 ); // .indexOf(_path);\r
194                 if (base==0) { base = document.baseURI.lastIndexOf('/'); }\r
195                 var sid = list[1]; // .substring(base);\r
196                 base = list[1].substring(0, base);\r
197                 if (_console) {\r
198                         _console.value += "Model is " + list[1] + ", Structure id is : "\r
199                                         + sid + "\n";\r
200                 }\r
201                 ;\r
202                 var siddat;\r
203                 for ( var jmolappi in _jvjmols.values()) {\r
204                         var jmolapp=_jvjmols.values()[jmolappi];\r
205                         var msg = "";\r
206                         if (siddat = jmolapp._filetonum.get(sid)) {\r
207                                 // we don't putin chain number because there isn't one ?\r
208                                 // skip select 0 bit\r
209                                 var ch = ""+list[3];\r
210                                 if ((""+list[2]).trim().length==1)\r
211                                         {\r
212                                         ch+=":"+list[2];\r
213                                         }\r
214                                 msg = "select (" + ch + " /" + siddat + ") ;";\r
215                         }\r
216                         if (msg) {\r
217                                 if (_console) {\r
218                                         _console.value += "Sending '" + msg + "' to jmol." + "\n";\r
219                                 }\r
220                         }\r
221                         jmolScriptWait(msg, "" + jmolapp._jmhandle);\r
222                         // only do highlight for one jmol ?\r
223                         // return 1;\r
224                 }\r
225         }\r
226         if (list1 == "colourstruct") {\r
227                 if (_console) {\r
228                         _console.value += 'colourStruct("' + list1 + '","' + list2\r
229                         + '") [' + list4 + ']' + "\n";\r
230                 }\r
231                 setTimeout('colourStruct("'+list4+'","' + list1 + '","' + list2 + '")', 1);\r
232                 return 1;\r
233         }\r
234         return 1;\r
235 }\r
236 // last colour message\r
237 var _lastMsg = "";\r
238 // indicator - if _colourStruct==0 then no colouring is going on\r
239 var _colourStruct = 0;\r
240 \r
241 function colourStruct(involves, msg, handle) {\r
242         if (_colourStruct == 0) {\r
243                 _colourStruct = 1;\r
244                 for (ap in _jvapps) {\r
245                         var _msg = "";\r
246                         do {\r
247                                 if (_msg.match(/\S/)) {\r
248                                         _lastMsg += _msg;\r
249                                 }\r
250                                 _msg = "" + _jvapps[ap].getJsMessage(msg, handle);\r
251                         } while (_msg.match(/\S/));\r
252                 }\r
253                 // locate the jmol that should get the message\r
254                 for (var jmol in _jvjmols.values())\r
255                         {\r
256                         var jml=_jvjmols.values()[jmol];\r
257                         if (jml._filetonum.get(involves))\r
258                                 {\r
259                                         colourStructs(jml._jmhandle);\r
260                                 }\r
261                         }\r
262                 _colourStruct = 0;\r
263         } else {\r
264                 // setTimeout('colourStruct("'+msg+'","'+handle+'")',3);\r
265         }\r
266 }\r
267 \r
268 function colourStructs(jmolapp) {\r
269         dbg(0, "Colouring the structures\n");\r
270         jmolScriptWait("set selectionhalos false;" + _lastMsg\r
271                         + "; select 0; set selectionhalos true;", jmolapp);\r
272         _lastMsg = "";\r
273 }\r
274 var _jmolhovermsg="";\r
275 function _jmolhover(jmid, atomlabel, atomidx) {\r
276         var msg=""+jmid+" "+atomlabel+" "+atomidx;\r
277         if (_jmolhovermsg==msg)\r
278                 {\r
279                 return;\r
280                 }\r
281         _jmolhovermsg=msg;\r
282         modeltofiles = _jvjmols.get(jmid)._modelstofiles;\r
283         // atomlabel=(""+atomlabel).match(/\[(.+)\](\d+):(.)\.(\S+)\s*\/(\d+)\..+/);\r
284         // relaxed third parameter - may be null or a model number for multi model\r
285         // views\r
286         atomlabel = ("" + atomlabel)\r
287                         .match(/\[(.+)\](\d+):(.)\.([^\/]+)(\/\d+\.|).+/);\r
288         atomidx = "" + atomidx;\r
289         if (atomlabel[5]) {\r
290                 atomlabel[5] = atomlabel[5].match(/\/(.+)\./)[1];\r
291                 atomlabel[5] = parseInt(atomlabel[5])-1;\r
292         } else {\r
293                 // default - first model\r
294                 atomlabel[5] = 0;\r
295         }\r
296         // use atomlabel[5] to look up model filename so we can highlight associated positions in any jalviews\r
297         for (ap in _jvapps) {\r
298                 _jvapps[ap].mouseOverStructure(atomlabel[2], atomlabel[3],\r
299                                 document.baseURI\r
300                                                 .substring(0, document.baseURI.lastIndexOf('/'))\r
301                                                 + "/" + \r
302                                                 modeltofiles[atomlabel[5]]);\r
303                 msg = _jmolhovermsg;\r
304         }\r
305 }\r
306 function _jmolpick(jmid, atomlabel, atomidx) {\r
307         atomlabel = "" + atomlabel;\r
308         atomidx = "" + atomidx;\r
309         // label is atom id, atom number, and xyz coordinates in the form:\r
310         // C6 #6 -0.30683374 -1.6836332 -0.716934\r
311         // atom index, starting with 0.\r
312 \r
313 }\r
314 function _jmolMessagecallback(jmid, statmess) {\r
315         // if (statmess.indexOf("Script Terminated")==0)\r
316         {\r
317                 var thisTime = new Date();\r
318                 if (_console) {\r
319                         _console.value += "Last script execution took : "\r
320                                         + (thisTime.valueOf() - _lastTime.valueOf()) / 1000.0\r
321                                         + " seconds.";\r
322                 }\r
323                 _lastTime = thisTime;\r
324 \r
325         }\r
326 }\r