File: startup.py

package info (click to toggle)
ball 1.4.3~beta1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 318,984 kB
  • sloc: cpp: 346,579; ansic: 4,097; python: 2,664; yacc: 1,778; lex: 1,099; xml: 964; sh: 688; sql: 316; awk: 118; makefile: 108
file content (364 lines) | stat: -rw-r--r-- 9,387 bytes parent folder | download | duplicates (3)
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
from sip import *
from random import *
###################### defines: ############################
true=1
false=0

###################### MAIN WIDGETS: #######################
def getMainControl():
	return MainControl.getInstance(0)

def getMolecularStructure():
	return MolecularStructure.getInstance(0)

def getMolecularControl():
	return MolecularControl.getInstance(0)

def getDisplayProperties():
	return DisplayProperties.getInstance(0)

def getScene():
	return Scene.getInstance(0)

#def getEScene():
#	return EditableScene.getInstance(0)

def getGeometricControl():
	return GeometricControl.getInstance(0)

def getLogView():
	return LogView.getInstance(0)

def getPyWidget():
	return PyWidget.getInstance(0)

def getDatasetControl():
	return DatasetControl.getInstance(0)

def hideAllWidgets():
	getScene().setWidgetVisible(0)
	getGeometricControl().setWidgetVisible(0)
	getMolecularControl().setWidgetVisible(0)
	getLogView().setWidgetVisible(0)
	getDatasetControl().setWidgetVisible(0)
	getPyWidget().setWidgetVisible(0)

def showOnlyScene():
	hideAllWidgets()
	getScene().setWidgetVisible(1)
	getMainControl().processEvents(5000)

def setSceneSize(width, height):
	getMainControl().setContentSize(width, height)
	showOnlyScene()
	getScene().update(false)

###################### SHORTCUTS: #######################

# map a key to a python command:
# modifier can be "" or None, Shift, Ctrl
def map(modifier, key, command):
	getPyWidget().map(modifier, key, command)

def log(to_log):
	getMainControl().setStatusbarText(to_log, 1)
	print to_log

def abortScript():
	getPyWidget().abortScript()

def quit():
	getMainControl().quit()

def run(file):
	getPyWidget().openFile(file, true)

def openFile(file):
	return getMainControl().openFile(file)
	
def getSystems():
	return getMainControl().getCompositeManager().getComposites()

def getSystem(nr):
	return getSystems()[nr]

def getSelection():
	return getMainControl().getSelection()

def getMolecularControlSelection():
	return getMainControl().getMolecularControlSelection()

# get a list with the Highlighted System or first one
def getOneSystem():
	s = getMolecularControlSelection()
	if len(s) == 0 or len(s) > 1:
		log("Warning: One System should be highlighted!")
		s.append(getSystem(0))
	S = s[0].getRoot()
	l = []
	l.append(S)
	getMolecularControl().highlight(l)
	return l

def getRepresentations():
	return getMainControl().getRepresentationManager().getRepresentations()

def getRepresentationByName(name):
  """Return the first representation with the given name"""
  reps = getRepresentations()
  for rep in reps:
    if rep.getName() == name:
      return rep
  return None

def getForceField():
	return getMolecularStructure().getForceField()

def setCamera(camera):
	getScene().setCamera(camera)

###################### HOTKEYS: #######################
def hideAllRepresentations():
	for i in range(len(getRepresentations())):
		getRepresentations()[i].setHidden(1)
		getMainControl().update(getRepresentations()[i])

def clearRepresentations():
	while len(getRepresentations()) > 0:
		getMainControl().remove(getRepresentations()[0])

def clearMolecules():
	while len(getSystems()) > 0:
		getMainControl().remove(getSystem(0))

def clearAll():
	clearMolecules()
	clearRepresentations()

def setMultithreading(mode):
	getMainControl().setMultithreading(mode)

def runScript(filename):
	getPyWidget().openFile(filename, true)

def runCurrentScript():
	getPyWidget().runCurrentScript()

def quickSave():
	getMainControl().quickSave()

def quickLoad():
	getMainControl().quickLoad()

def removeWater():
	getMainControl().clearSelection()
	setMultithreading(0)
	if getMolecularControl().applySelector("residue(HOH)") == 0:
		setMultithreading(1)
		return
	getMolecularControl().highlightSelection()
	getMolecularControl().cut()
	setMultithreading(1)

def addOptimizedHydrogens():
	getOneSystem()
	setMultithreading(0)
	getMolecularStructure().addHydrogens()
	getMolecularControl().applySelector("element(H)")
	setMultithreading(1)
	getMolecularStructure().runMinimization(false)

def relaxStructure():	
	s = getOneSystem()
	getScene().optimizeStructure()

def highlightLigand():
	s = getOneSystem()
	removeWater()
	S = s[0] 
	l = []
	for r in residues(S):
		if not r.isAminoAcid():
			l.append(r)
	getMolecularControl().highlight(l)
	return l

def showCartoonAndLigand():
	s = getOneSystem()
	S = s[0] 
	clearRepresentations()
	highlightLigand()
	getDisplayProperties().selectModel(MODEL_VDW)
	getDisplayProperties().selectColoringMethod(COLORING_ELEMENT)
	getDisplayProperties().apply()
	l = []
	l.append(S)
	getMolecularControl().highlight(l)
	getMolecularControl().centerCamera()
	getDisplayProperties().selectModel(MODEL_CARTOON)
	getDisplayProperties().selectColoringMethod(COLORING_RESIDUE_INDEX)
	getDisplayProperties().apply()

def printAtomTypesForHighlighted():
	s = getMolecularControlSelection()
	log("Atom types for highlighted Items:")
	for r in s:
		if r.__class__ == BALL.Atom:
			log(str(r.getFullName(Atom.ADD_RESIDUE_ID))+" : "+str(r.getType()))
		else:
			for a in atoms(r):
				log(a.getFullName(Atom.ADD_RESIDUE_ID) +" : "+str(a.getType()))

def printAtomTypesForLigands():
	highlightLigand()
	printAtomTypesForHighlighted()

def randomizeAtoms(md):
	atoml = atoms(getOneSystem()[0])
	for i in range(0, len(atoml)):
		atoml[i].setPosition(atoml[i].getPosition() + Vector3(uniform(-md,md), uniform(-md,md), uniform(-md,md)))
	getMainControl().update(getOneSystem()[0])

###################### EXAMPLES: #######################
def createStickModel():
	dp = getDisplayProperties()
	dp.setDrawingPrecision(DRAWING_PRECISION_HIGH)
	dp.selectMode(DRAWING_MODE_SOLID)
	dp.selectModel(MODEL_VDW)
	dp.selectColoringMethod(COLORING_ELEMENT)
	dp.setTransparency(0)
	dp.apply()

def createChainSurfaces():
	clearRepresentations()
	s = getOneSystem()[0]
	getDisplayProperties().selectModel(MODEL_SE_SURFACE)
	getDisplayProperties().selectColoringMethod(COLORING_CHAIN)
	for c in chains(s):
		l = []
		l.append(c)
		getDisplayProperties().createRepresentation(l)

def addPlane(plane_specifier, height, boundary, bottom = True):
  systems = getSystems()
  system = systems[0]
  bbp = BoundingBoxProcessor()
  system.apply(bbp)
  v_low = Vector3(bbp.getLower().x, bbp.getLower().y, bbp.getLower().z)
  v_upp = Vector3(bbp.getUpper().x, bbp.getUpper().y, bbp.getUpper().z)
  #
  if (len(systems) > 1):
    for system in systems[1:]:
      system.apply(bbp)
      low = bbp.getLower()
      upp = bbp.getUpper()
      #
      if v_low.x > low.x:
        v_low.x = low.x
      if v_low.y > low.y:
        v_low.y = low.y
      if v_low.z > low.z:
        v_low.z = low.z
      #
      if v_upp.x < upp.x:
        v_upp.x = upp.x 
      if v_upp.y < upp.y:
        v_upp.y = upp.y
      if v_upp.z < upp.z:
        v_upp.z = upp.z
  #
  if (not bottom):
    v_tmp = v_low
    v_low = v_upp
    v_upp = v_tmp
    height = height*(-1)
    boundary = boundary*(-1)
  #
  v_low_left = Vector3()
  v_low_right = Vector3()
  v_upp_right = Vector3()
  v_upp_left = Vector3()
  #
  normal = Vector3()
  #
  if (plane_specifier == 'x'):
    v_low = v_low - Vector3(height, boundary, boundary)
    v_upp = v_upp + Vector3(height, boundary, boundary)
    #
    v_low_left  = Vector3(v_low.x, v_low.y, v_low.z)
    v_low_right = Vector3(v_low.x, v_upp.y, v_low.z)
    v_upp_right = Vector3(v_low.x, v_upp.y, v_upp.z)
    v_upp_left  = Vector3(v_low.x, v_low.y, v_upp.z)
    #
    normal = Vector3(1, 0, 0)
  #
  elif (plane_specifier == 'y'):
    v_low = v_low - Vector3(boundary, height, boundary)
    v_upp = v_upp + Vector3(boundary, height, boundary)
    #
    v_low_left  = Vector3(v_low.x, v_low.y, v_low.z)
    v_low_right = Vector3(v_low.x, v_low.y, v_upp.z)
    v_upp_right = Vector3(v_upp.x, v_low.y, v_upp.z)
    v_upp_left  = Vector3(v_upp.x, v_low.y, v_low.z)
    #
    normal = Vector3(0, 1, 0)
    #
  elif (plane_specifier == 'z'):
    v_low = v_low - Vector3(boundary, boundary, height)
    v_upp = v_upp + Vector3(boundary, boundary, height)
    #
    v_low_left  = Vector3(v_low.x, v_low.y, v_low.z)
    v_low_right = Vector3(v_low.x, v_upp.y, v_low.z)
    v_upp_right = Vector3(v_upp.x, v_upp.y, v_low.z)
    v_upp_left  = Vector3(v_upp.x, v_low.y, v_low.z)
    #
    normal = Vector3(0, 0, 1)
    #
  plane = Mesh()
  #
  # the vertices
  plane.pushBackVertex(v_low_left)
  plane.pushBackVertex(v_low_right)
  plane.pushBackVertex(v_upp_right)
  plane.pushBackVertex(v_upp_left)
  #
  # the triangles
  t1 = Mesh.Triangle()
  t1.v1 = 0 # v_low_left
  t1.v2 = 1 # v_low_right
  t1.v3 = 2 # v_upp_right
  #
  plane.pushBackTriangle(t1)
  #
  t2 = Mesh.Triangle()
  t2.v1 = 2 # v_upp_right
  t2.v2 = 3 # v_upp_left
  t2.v3 = 0 # v_low_left
  #
  plane.pushBackTriangle(t2)
  #
  # the normals
  for i in range(4):
    plane.pushBackNormal(normal)
  #
  color = ColorRGBA(0.2,0.2,0.2,1)
  #
  colors = plane.getColors()
  colors.append(color)
  plane.setColors(colors)
  #
  r = Representation()
  r.insert(plane)
  r.setModelType(MODEL_PLANE)
  #
  getMainControl().insert(r)
  getMainControl().update(r)

import os

if os.environ.has_key('BALLVIEW_ENTER_STEREO') and (os.environ['BALLVIEW_ENTER_STEREO'].upper() == "TRUE"):
  Scene.getInstance(0).enterStereo()

if os.environ.has_key('BALLVIEW_FULLSCREEN') and (os.environ['BALLVIEW_FULLSCREEN'].upper() == "TRUE"):
  getMainControl().showFullScreen()