-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtorsion.py
More file actions
355 lines (316 loc) · 14.1 KB
/
torsion.py
File metadata and controls
355 lines (316 loc) · 14.1 KB
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
#!/usr/bin/env python3
import os
import glob
import sys
import numpy as np
# '''''''''''''''''''''''''''''''''''''''''
# 'Extracting coordinates if pdb or pdbqt '
# '''''''''''''''''''''''''''''''''''''''''
def For_pdbqt(pdbqt_split, input_atom, input_number, list_num):
n = 0
while n < len(pdbqt_split):
if pdbqt_split[n] == input_atom \
and pdbqt_split[n + 3] == input_number: # If chain id present
if list_num == 1:
list1.append(pdbqt_split[n + 4] + " " +
pdbqt_split[n + 5] + " " +
pdbqt_split[n + 6] + " ")
if list_num == 2:
list2.append(pdbqt_split[n + 4] + " " +
pdbqt_split[n + 5] + " " +
pdbqt_split[n + 6] + " ")
if list_num == 3:
list3.append(pdbqt_split[n + 4] + " " +
pdbqt_split[n + 5] + " " +
pdbqt_split[n + 6] + " ")
if list_num == 4:
list4.append(pdbqt_split[n + 4] + " " +
pdbqt_split[n + 5] + " " +
pdbqt_split[n + 6] + " ")
if pdbqt_split[n] == input_atom \
and pdbqt_split[n + 2] == input_number: # chain id not presnt
if list_num == 1:
list1.append(pdbqt_split[n + 3] + " " +
pdbqt_split[n + 4] + " " +
pdbqt_split[n + 5] + " ")
if list_num == 2:
list2.append(pdbqt_split[n + 3] + " " +
pdbqt_split[n + 4] + " " +
pdbqt_split[n + 5] + " ")
if list_num == 3:
list3.append(pdbqt_split[n + 3] + " " +
pdbqt_split[n + 4] + " " +
pdbqt_split[n + 5] + " ")
if list_num == 4:
list4.append(pdbqt_split[n + 3] + " " +
pdbqt_split[n + 4] + " " +
pdbqt_split[n + 5] + " ")
n += 1
# '''''''''''''''''''''''''''''''
# 'Extracting coordinates if dlg'
# '''''''''''''''''''''''''''''''
def For_dlg(dlg_split, input_atom, input_number, list_num):
n = 0
while n < len(dlg_split):
splitted = dlg_split[n]
if splitted[0:6] == "DOCKED":
split_split = splitted.split()
if len(input_number) == 1:
if splitted[17:19] == " " + input_number \
and split_split[3] == input_atom:
if list_num == 1:
list1.append(splitted[39:47] + " " +
splitted[47:55] + " " +
splitted[55:63] + " ")
if list_num == 2:
list2.append(splitted[39:47] + " " +
splitted[47:55] + " " +
splitted[55:63] + " ")
if list_num == 3:
list3.append(splitted[39:47] + " " +
splitted[47:55] + " " +
splitted[55:63] + " ")
if list_num == 4:
list4.append(splitted[39:47] + " " +
splitted[47:55] + " " +
splitted[55:63] + " ")
if len(input_number) == 2:
if splitted[17:19] == input_number \
and split_split[3] == input_atom:
if list_num == 1:
list1.append(splitted[39:47] + " " +
splitted[47:55] + " " +
splitted[55:63] + " ")
if list_num == 2:
list2.append(splitted[39:47] + " " +
splitted[47:55] + " " +
splitted[55:63] + " ")
if list_num == 3:
list3.append(splitted[39:47] + " " +
splitted[47:55] + " " +
splitted[55:63] + " ")
if list_num == 4:
list4.append(splitted[39:47] + " " +
splitted[47:55] + " " +
splitted[55:63] + " ")
if len(input_number) == 3:
if splitted[16:19] == input_number \
and split_split[3] == input_atom:
if list_num == 1:
list1.append(splitted[39:47] + " " +
splitted[47:55] + " " +
splitted[55:63] + " ")
if list_num == 2:
list2.append(splitted[39:47] + " " +
splitted[47:55] + " " +
splitted[55:63] + " ")
if list_num == 3:
list3.append(splitted[39:47] + " " +
splitted[47:55] + " " +
splitted[55:63] + " ")
if list_num == 4:
list4.append(splitted[39:47] + " " +
splitted[47:55] + " " +
splitted[55:63] + " ")
n += 1
# '''''''''''''''''''''''''''''''
# 'Extracting coordinates if gro'
# '''''''''''''''''''''''''''''''
def For_gro(gro_split, input_atom, input_number, list_num):
n = 0
while n < len(gro_split):
if gro_split[n] == input_atom:
if gro_split[n-1][:-3] == input_number:
if list_num == 1:
list1.append(str(float(gro_split[n + 2]) * 10) + " " +
str(float(gro_split[n + 3]) * 10) + " " +
str(float(gro_split[n + 4]) * 10) + " ")
if list_num == 2:
list2.append(str(float(gro_split[n + 2]) * 10) + " " +
str(float(gro_split[n + 3]) * 10) + " " +
str(float(gro_split[n + 4]) * 10) + " ")
if list_num == 3:
list3.append(str(float(gro_split[n + 2]) * 10) + " " +
str(float(gro_split[n + 3]) * 10) + " " +
str(float(gro_split[n + 4]) * 10) + " ")
if list_num == 4:
list4.append(str(float(gro_split[n + 2]) * 10) + " " +
str(float(gro_split[n + 3]) * 10) + " " +
str(float(gro_split[n + 4]) * 10) + " ")
n += 1
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# 'Opening the files and sending to respective function to extract coordinates'
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
def File_read():
if input_file1[-6:] == ".pdbqt" or input_file1[-4:] == ".pdb":
first = For_pdbqt(pdbqt_split1, input_atom1, input_number1, 1)
elif input_file1[-4:] == ".dlg":
first = For_dlg(dlg_split1, input_atom1, input_number1, 1)
else:
first = For_gro(gro_split1, input_atom1, input_number1, 1)
if input_file2[-6:] == ".pdbqt" or input_file2[-4:] == ".pdb":
second = For_pdbqt(pdbqt_split2, input_atom2, input_number2, 2)
elif input_file2[-4:] == ".dlg":
second = For_dlg(dlg_split2, input_atom2, input_number2, 2)
else:
second = For_gro(gro_split2, input_atom2, input_number2, 2)
if input_file3[-6:] == ".pdbqt" or input_file3[-4:] == ".pdb":
third = For_pdbqt(pdbqt_split3, input_atom3, input_number3, 3)
elif input_file3[-4:] == ".dlg":
third = For_dlg(dlg_split3, input_atom3, input_number3, 3)
else:
third = For_gro(gro_split3, input_atom3, input_number3, 3)
if input_file4[-6:] == ".pdbqt" or input_file4[-4:] == ".pdb":
fourth = For_pdbqt(pdbqt_split4, input_atom4, input_number4, 4)
elif input_file4[-4:] == ".dlg":
fourth = For_dlg(dlg_split4, input_atom4, input_number4, 4)
else:
fourth = For_gro(gro_split4, input_atom4, input_number4, 4)
List_making()
# ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# 'Making equal size list of coordinates to find all the angles'
# ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
def List_making():
list_len = [len(list1), len(list2), len(list3), len(list4)]
angle_num = sorted(list_len)
list1_string = ""
list2_string = ""
list3_string = ""
list4_string = ""
if len(list1) != angle_num[-1]:
n = 1
while n < angle_num[-1]:
list1.append(list1[0])
n += 1
if len(list2) != angle_num[-1]:
n = 1
while n < angle_num[-1]:
list2.append(list2[0])
n += 1
if len(list3) != angle_num[-1]:
n = 1
while n < angle_num[-1]:
list3.append(list3[0])
n += 1
if len(list4) != angle_num[-1]:
n = 1
while n < angle_num[-1]:
list4.append(list4[0])
n += 1
for each1 in list1:
list1_string += each1
for each2 in list2:
list2_string += each2
for each3 in list3:
list3_string += each3
for each4 in list4:
list4_string += each4
Angle_find(list1_string, list2_string, list3_string, list4_string)
# '''''''''''''''''''''''''''''''''''''''''''''''''
# ' Finding angles and output is written to a file'
# '''''''''''''''''''''''''''''''''''''''''''''''''
def Angle_find(list1_string, list2_string, list3_string, list4_string):
list1_split = list1_string.split()
list2_split = list2_string.split()
list3_split = list3_string.split()
list4_split = list4_string.split()
n = 0
m = 0
output = open(output_filename, "a")
print("\n" + output_filename + "\n")
while n < len(list1):
a = np.array([float(list1_split[m]),
float(list1_split[m + 1]),
float(list1_split[m + 2])])
b = np.array([float(list2_split[m]),
float(list2_split[m + 1]),
float(list2_split[m + 2])])
c = np.array([float(list3_split[m]),
float(list3_split[m + 1]),
float(list3_split[m + 2])])
d = np.array([float(list4_split[m]),
float(list4_split[m + 1]),
float(list4_split[m + 2])])
v1 = -1.0*(b - a)
v2 = c - b
v3 = d - c
cp = np.cross(v1, v2)
cp2 = np.cross(v3, v2)
cp3 = np.cross(cp, cp2)
y = np.dot(cp3, v2) * (1.0 / np.linalg.norm(v2))
x = np.dot(cp, cp2)
angle = np.arctan2(y, x)
n += 1
m += 3
output.write(str(np.degrees(angle)) + "\n")
print((np.degrees(angle)))
# ''''''''''''''''''
# ' all inputs '
# ''''''''''''''''''
if len(sys.argv) <= 13:
print("\n \nUSAGE = python torsion.py filename atom_type1 atom_number1\
filename atom_type2 atom_number2 filename atom_type3 atom_number3\
filename atom_type4 atom_number4 output_filename \n\n or \n\npython\
torsion.py test.pdb CA 156 test.gro CB 130 test.dlg C 8 test.pdb CA\
100 test_torsion.txt \n \n \nUse pdb/gro/dlg/pdbqt")
else:
input_file1 = sys.argv[1]
input_atom1 = sys.argv[2]
input_number1 = sys.argv[3]
input_file2 = sys.argv[4]
input_atom2 = sys.argv[5]
input_number2 = sys.argv[6]
input_file3 = sys.argv[7]
input_atom3 = sys.argv[8]
input_number3 = sys.argv[9]
input_file4 = sys.argv[10]
input_atom4 = sys.argv[11]
input_number4 = sys.argv[12]
output_filename = sys.argv[13]
# ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# ' check inputs are pdb,pdbqt or dlg and assigning filename to variables'
# ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if input_file1[-6:] == ".pdbqt" or input_file1[-4:] == ".pdb":
pdbqt_open = open(input_file1, "r").read()
pdbqt_split1 = pdbqt_open.split()
elif input_file1[-4:] == ".dlg":
dlg_open = open(input_file1, "r")
dlg_split1 = dlg_open.readlines()
elif input_file1[-4:] == ".gro":
gro_open = open(input_file1, "r").read()
gro_split1 = gro_open.split()
if input_file2[-6:] == ".pdbqt" or input_file2[-4:] == ".pdb":
pdbqt_open = open(input_file2, "r").read()
pdbqt_split2 = pdbqt_open.split()
elif input_file2[-4:] == ".dlg":
dlg_open = open(input_file2, "r")
dlg_split2 = dlg_open.readlines()
elif input_file2[-4:] == ".gro":
gro_open = open(input_file2, "r").read()
gro_split2 = gro_open.split()
if input_file3[-6:] == ".pdbqt" or input_file3[-4:] == ".pdb":
pdbqt_open = open(input_file3, "r").read()
pdbqt_split3 = pdbqt_open.split()
elif input_file3[-4:] == ".dlg":
dlg_open = open(input_file3, "r")
dlg_split3 = dlg_open.readlines()
elif input_file3[-4:] == ".gro":
gro_open = open(input_file3, "r").read()
gro_split3 = gro_open.split()
if input_file4[-6:] == ".pdbqt" or input_file4[-4:] == ".pdb":
pdbqt_open = open(input_file4, "r").read()
pdbqt_split4 = pdbqt_open.split()
elif input_file4[-4:] == ".dlg":
dlg_open = open(input_file4, "r")
dlg_split4 = dlg_open.readlines()
elif input_file4[-4:] == ".gro":
gro_open = open(input_file4, "r").read()
gro_split4 = gro_open.split()
# ''''''''''''''''''''''''''''''''''''''
# 'Global list for to store coordinates'
# ''''''''''''''''''''''''''''''''''''''
list1 = []
list2 = []
list3 = []
list4 = []
File_read()