/
X.py
419 lines (324 loc) · 14.2 KB
/
X.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# -*- coding: utf-8 -*-
#
# Copyright 2016 Holger Ballweg
#
# This file is part of chison.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# debug flag
DEBUG = False
basepath = os.path.dirname(os.path.abspath(__file__))
import threading
import utils
reload(utils)
from utils import *
import spatialisation
reload(spatialisation)
from spatialisation import *
import sound_objects
reload(sound_objects)
from sound_objects import *
import chimera_utils
reload(chimera_utils)
from chimera_utils import *
import interaction
reload(interaction)
import sonification_settings
reload(sonification_settings)
cleanup_fn = identity
import chimera_global as cg
grains = False
def m_none(models, objects):
# init mapping
if(len(objects) == 0):
interaction.grain_maker_fn = interaction.default_grain
interaction.grains = False
objects[0] = None
return objects
tFrame = 'new frame'
def m_bfactors_cleanup():
print("cleaning up bfactors")
for model in chimera.openModels.list(modelTypes=[chimera.Molecule]):
for i,atom in enumerate(model.atoms):
try:
atom.color = atom.origColor
except:
pass
try:
chimera.triggers.deleteHandler(tFrame, hFrame)
except:
pass
# cutoff for betafactors
cutoff = 80.0
def m_bfactors_animation(trigger, additional, frameNo):
onFrame = 0
offFrame = 1
for key, sobj in cg.mapping_objects.iteritems():
obj = chimera.openModels.list(id=sobj['ch_model_id'])[0].residues[
sobj['ch_residue']]
if sobj['anim']:
lenAnim = sobj['len_anim']
posAnimF = (frameNo % lenAnim)
#posAnim = posAnimF/lenAnim
if((onFrame == posAnimF) or (offFrame == posAnimF)):
if(onFrame == posAnimF):
modify_sound_object(sobj, "gate", 1)
if animate:
set_color(obj, chimera.MaterialColor(1,0.5,0.2))
else:
modify_sound_object(sobj, "gate", 0)
if animate:
restore_color(obj)
animate = False
def bfactor_for_res(res):
bfactors = list()
for a in res.atoms:
bfactors.append(a.bfactor)
arr = np.array(bfactors)
return arr.mean(), arr.min(), arr.max()
def m_bfactors_grain(obj, dist, az, ele, level, maxLevel):
bfactor, bmin, bmax = bfactor_for_res(obj)
return make_sound_object(-1, "bfactor2Grain",
"dist", dist, "az", az, "ele", ele,
"amp", (1.5 - (float(level)/max(maxLevel, 1))),
"midinote", 55 + ((bfactor - cutoff) * 0.15) )
def m_bfactors(models, objects):
# init mapping
if(len(objects) == 0):
interaction.grain_maker_fn = m_bfactors_grain
interaction.grains = True
# permanently display top 20% of bfactor values
global cutoff
bfs = list()
for model in chimera.openModels.list(modelTypes=[chimera.Molecule]):
for i,res in enumerate(model.residues):
bfactor, bmin, bmax = bfactor_for_res(res)
bfs.append(bfactor)
bfa = np.array(bfs)
bfa.sort()
cutoff = bfa[len(bfa) * 0.8]
for model in models.list(modelTypes=[chimera.Molecule]):
for i,res in enumerate(model.residues):
bfactor, bmin, bmax = bfactor_for_res(res)
if(bfactor > cutoff):
rhfreq = ((bmax - cutoff) / 40 + 1)/20.0
sobj = make_sound_object(None, "bfactor")
sobj = modify_sound_object(sobj,
"rhfreq", rhfreq,
"midinote", 55 + ((bfactor - cutoff) * 0.15))
sobj['len_anim'] = max(1, int(round(15/rhfreq))) * 2
sobj['ch_model_id'] = model.id
sobj['ch_residue'] = i
sobj['anim'] = int(bmax > cutoff)
res.sobj = [sobj['id']]
objects[sobj['id']] = sobj
#global hFrame
#hFrame = chimera.triggers.addHandler(tFrame,m_bfactors_animation, None)
#global cleanup_fn
#cleanup_fn = m_bfactors_cleanup
# update positions
real_eye = ch_get_real_eye()
for key, sobj in objects.iteritems():
obj = chimera.openModels.list(id=sobj['ch_model_id'])[0].residues[
sobj['ch_residue']]
coords = get_coords(obj)
dist, az, ele = ch_calculate_position(real_eye, coords)
sobj = modify_sound_object(sobj,
"amp", np.interp(dist, [0,500], [0.8,0.01]))
objects[key] = position_sound_object(sobj, dist, az, ele)
return objects
######### docking
"""
Run only once: FindHBonds
chimera.runCommand("hbond intermodel 1 intramodel 0 reveal 1 showdist 1")
"""
"""
def m_docking_grain(obj, dist, az, ele, level, maxLevel):
return make_sound_object(-1, "hbondGrain",
"freq", 110 * (level+1 ),
"dist", dist, "az", az, "ele", ele,
"amp", (1.0 - (float(level)/max(maxLevel, 1))) * 0.5 )
def m_docking_grain(obj, dist, az, ele, level, maxLevel):
bfactor, bmin, bmax = bfactor_for_res(obj)
rhfreq = (bfactor - cutoff) / 10 + 1
return make_sound_object(-1, "bfactor2Grain",
"dist", dist, "az", az, "ele", ele,
"amp", (1.0 - (float(level)/max(maxLevel, 1))),
"rhfreq", rhfreq,
"freq", 440 + ((bfactor - cutoff) * 10) * 0.5)
"""
def m_docking(models, objects):
# init mapping
if(len(objects) == 0):
interaction.grain_maker_fn = m_bfactors_grain
interaction.grains = True
for model in models.list(modelTypes=[chimera.Molecule]):
if model.display and is_ligand(model):
for i,r in enumerate(model.atoms):
midinote = ((r.molecule.dockGridVdw + 50) * 30.0/100) + 55
mfreq = ((r.charge + 1) * 0.5) * 200
#print(midinote, mfreq)
sobj = make_sound_object(None, "hbond1",
"midinote", midinote,
"mfreq", mfreq,
)
sobj['ch_model_id'] = model.id
sobj['ch_model_subid'] = model.subid
sobj['ch_atom'] = i
objects[sobj['id']] = sobj
r.sobj = [sobj['id']]
for j,b in enumerate(r.pseudoBonds):
sobj = make_sound_object(None, "hbond")
sobj['ch_model_id'] = model.id
sobj['ch_model_subid'] = model.subid
sobj['ch_atom'] = i
sobj['ch_pseudoBond'] = j
objects[sobj['id']] = sobj
r.sobj.append(sobj['id'])
# update positions
real_eye = ch_get_real_eye()
for key, sobj in objects.iteritems():
model = chimera.openModels.list(id=sobj['ch_model_id'], subid=sobj['ch_model_subid'])[0]
if(model.display):
obj = model.atoms[sobj['ch_atom']]
coords = obj.xformCoord()
objects[key] = ch_position_sound_object(sobj, real_eye, coords)
return objects
def stop_mapping(objects):
reset_sound_objects()
cleanup_fn()
return dict()
def set_mapping(new_map_fn):
cg.mapping_objects = stop_mapping(cg.mapping_objects)
global mapping_fn
mapping_fn = new_map_fn
global cleanup_fn
cleanup_fn = identity
cg.mapping_objects = mapping_fn(chimera.openModels, cg.mapping_objects)
# clean the slate both on client and on sound server
reset_sound_objects()
mapping_fn = m_none
# chimera
import chimera
ch_models = dict()
def ch_add_model(model):
return ch_delete_model(0)
def ch_modify_model():
pass
def ch_delete_model(id):
print("deleting model ", id)
"""
for m in chimera.openModels.list(modelTypes=[chimera.Molecule]):
for a in m.atoms:
a.doneC = False
a.origColor = a.color
for r in m.residues:
r.doneC = False
r.origColor = r.ribbonColor
"""
objects = stop_mapping(cg.mapping_objects)
objects = mapping_fn(chimera.openModels, objects)
return objects
def ch_change_view(viewer, models):
objects = mapping_fn(chimera.openModels, cg.mapping_objects)
return objects
# chimera triggers
modelTrigger = u'Model'
viewerTrigger = u'Viewer'
try:
chimera.triggers.deleteHandler(modelTrigger, modelHandler)
chimera.triggers.deleteHandler(viewerTrigger, viewerHandler)
except:
pass
# animation trigger
tFrame = 'new frame'
try:
chimera.triggers.deleteHandler(tFrame, hFrame)
except:
pass
def viewer_changed(trigger, additional, atomChanges):
if DEBUG:
print("triggered viewer changed")
cg.mapping_objects = ch_change_view(chimera.viewer, chimera.openModels)
openModelIds = set()
def models_changed(trigger, additional, changes):
global openModelIds
for i in changes.modified:
# TODO
#print("triggered modified", changes.modified, changes.reasons)
#print("triggered modified", changes.reasons)
# send_osc("/model/modified", i.id, *changes.reasons)
newOpenModelIds = set()
if(u'display changed' in changes.reasons):
#print("triggered modified", changes.created, changes.reasons)
for model in chimera.openModels.list():
newOpenModelIds.add(model.id)
openModelIds = newOpenModelIds
cg.mapping_objects = ch_add_model(i)
for i in changes.created:
print("triggered create", changes.created, changes.reasons)
cg.mapping_objects = ch_add_model(i)
newOpenModelIds = set()
for model in chimera.openModels.list():
newOpenModelIds.add(model.id)
openModelIds = newOpenModelIds
for i in changes.deleted:
print("triggered delete", changes.reasons)
newOpenModelIds = set()
for model in chimera.openModels.list():
newOpenModelIds.add(model.id)
for id in openModelIds.difference(newOpenModelIds):
cg.mapping_objects = ch_delete_model(id)
openModelIds = newOpenModelIds
viewerHandler = chimera.triggers.addHandler(viewerTrigger,viewer_changed, None)
modelHandler = chimera.triggers.addHandler(modelTrigger, models_changed, None)
# GUI
import chimera
import Tkinter
from chimera.baseDialog import ModelessDialog
mapping = None
mappings = {
#'Test mapping': m_test,
'Docking': m_docking,
'Betafactors': m_bfactors,
'None': m_none,
#'Earcons': m_earcons,
#'none': identity
}
class MappingDialog(ModelessDialog):
name = "mapping dialog"
buttons = ("Apply", "Close")
title = "Mapping settings"
def fillInUI(self, parent):
global mapping
width = 16
mapping = Tkinter.StringVar(parent)
mapping.set(mappings.keys()[0])
mappingLabel = Tkinter.Label(parent, text='Sonification Mapping')
mappingLabel.grid(column=0, row=0)
# Create the menu button and the option menu that it brings up.
mappingButton = Tkinter.Menubutton(parent, indicatoron=1,
textvariable=mapping, width=width,
relief=Tkinter.RAISED, borderwidth=2)
mappingButton.grid(column=1, row=0)
mappingMenu = Tkinter.Menu(mappingButton, tearoff=0)
# Add radio buttons for all possible choices to the menu.
for mapname in mappings.keys():
mappingMenu.add_radiobutton(label=mapname, variable=mapping, value=mapname)
# Assigns the option menu to the menu button.
mappingButton['menu'] = mappingMenu
def Apply(self):
print("setting mapping to " + mapping.get())
set_mapping(mappings[mapping.get()])
redisplay(MappingDialog)