forked from Pymol-Scripts/Pymol-script-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pymol2glmol.py
176 lines (154 loc) · 5.03 KB
/
pymol2glmol.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
'''
Pymol to GLmol exporter
Written by biochem_fan, 2011
'''
from pymol import cmd
from math import cos, sin, pi, sqrt, acos, asin, atan2
import os
def compactSeq(seq):
seq.sort()
ret = []
prev = -9999
start = -9999
seq.append(-1)
i = 0
while (i < len(seq) - 1):
if (start >= 0):
if (seq[i] + 1 != seq[i + 1]):
ret.append("%d-%d" % (start, seq[i]))
start = -999
else:
if (seq[i] + 1 != seq[i + 1]):
start = -999
ret.append(str(seq[i]))
else:
start = seq[i]
i += 1
return ','.join(ret)
def parseObjMol(obj):
name = obj[0]
ids = []
sphere = []
trace = []
ribbon = []
stick = []
surface = []
line = []
cross = []
smallSphere = []
helix = []
sheet = []
colors = {}
for atom in obj[5][7]:
rep = atom[20] + [0] * 12
serial = atom[22]
ss = atom[10]
bonded = (atom[25] == 1)
if (rep[5] == 1):
ribbon.append(serial)
if (rep[1] == 1):
sphere.append(serial)
if (rep[2] == 1):
surface.append(serial)
if (rep[7] == 1):
line.append(serial)
if (rep[6] == 1):
trace.append(serial)
if (rep[4] == 1 and not bonded):
smallSphere.append(serial)
if (rep[11] == 1 and not bonded):
cross.append(serial)
if (rep[0] == 1 and bonded):
stick.append(serial)
if (ss == 'S'):
sheet.append(serial)
if (ss == 'H'):
helix.append(serial)
c = cmd.get_color_tuple(atom[21])
if (not c in colors):
colors[c] = []
colors[c].append(serial)
ids.append("ID %d is %s in resi %s %s at chain %s"\
% (atom[22], atom[6], atom[3], atom[5], atom[1]))
for c in colors.keys(): # TODO: better compression
colors[c] = compactSeq(colors[c])
ret = ''
ret += "\nsheet:" + compactSeq(sheet)
ret += "\nhelix:" + compactSeq(helix)
ret += "\nsurface:" + compactSeq(surface)
ret += "\nsphere:" + compactSeq(sphere)
ret += "\ntrace:" + compactSeq(trace)
ret += "\nribbon:" + compactSeq(ribbon)
ret += "\nstick:" + compactSeq(stick)
ret += "\nline:" + compactSeq(line)
ret += "\nsmallSphere:" + compactSeq(smallSphere)
ret += "\ncross:" + compactSeq(cross)
for c in colors.keys():
ret += "\ncolor:%.3f,%.3f,%.3f:%s" % (c[0], c[1], c[2], colors[c])
return ret
def parseDistObj(obj):
if (obj[5][0][3][10] != 1): # 'show dashed' flag
return ""
N = obj[5][2][0][0]
points = obj[5][2][0][1]
ret = []
for p in points:
ret.append("%.3f" % p)
color = cmd.get_color_tuple(obj[5][0][2])
return "\ndists:%.3f,%.3f,%.3f:" % color + ','.join(ret)
def dump_rep(name):
if 'PYMOL_GIT_MOD' in os.environ:
import shutil
try:
shutil.copytree(os.path.join(os.environ['PYMOL_GIT_MOD'], 'pymol2glmol', 'js'), os.path.join(os.getcwd(), 'js'))
except OSError:
pass
try:
cmd.set('pse_export_version', 1.74)
except:
pass
names = cmd.get_session()['names']
cmd.set('pdb_retain_ids', 1)
ret = ''
for obj in names:
if (obj == None):
continue
if (obj[2] == 0): # not visible
continue
if (obj[1] == 0 and obj[4] == 1 and obj[0] == name):
ret += parseObjMol(obj)
if (obj[1] == 0 and obj[4] == 4): # currently all dist objects are exported
ret += parseDistObj(obj)
cmd.turn('z', 180)
view = cmd.get_view()
cmd.turn('z', 180)
cx = -view[12]
cy = -view[13]
cz = -view[14]
cameraZ = - view[11] - 150
fov = float(cmd.get("field_of_view"))
fogStart = float(cmd.get("fog_start"))
slabNear = view[15] + view[11]
slabFar = view[16] + view[11]
ret += "\nview:%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f" % \
(cx, cy, cz, cameraZ, slabNear, slabFar, fogStart, fov)
for i in range(9):
ret += ",%.3f" % view[i]
bgcolor = cmd.get_setting_tuple('bg_rgb')[1]
if len(bgcolor) == 1:
bgcolor = cmd.get_color_tuple(bgcolor[0])
ret += "\nbgcolor:%02x%02x%02x" % (int(255 * float(bgcolor[0])), \
int(255 * float(bgcolor[1])), int(255 * float(bgcolor[2])))
if 'PYMOL_GIT_MOD' in os.environ:
template = open(os.path.join(os.environ['PYMOL_GIT_MOD'], 'pymol2glmol', 'imported.html')).read().\
replace("###INCLUDE_PDB_FILE_HERE###", cmd.get_pdbstr(name)).\
replace('###INCLUDE_REPRESENTATION_HERE###', ret)
else:
template = open('imported.html').read().\
replace("###INCLUDE_PDB_FILE_HERE###", cmd.get_pdbstr(name)).\
replace('###INCLUDE_REPRESENTATION_HERE###', ret)
f = open(name + '.html', 'w')
f.write(template)
f.close()
cmd.extend('pymol2glmol', dump_rep)
cmd.auto_arg[0]['pymol2glmol'] = [cmd.object_sc, 'object', '']