-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMILES2PDB.py
executable file
·129 lines (107 loc) · 4.51 KB
/
SMILES2PDB.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
#!/usr/bin/env python
import os
import yaml
import sys
import glob
import subprocess
from datetime import date
def SMILE_Preprocessor(MOL, SmileCode):
"""
Process
-------------------------
1. create PDB from smile code
2. fix PDB file with right labels
INPUT
-------------------------
SmileCode = Code to create all-atom model
OUTPUT
-------------------------
1. Creates SMILES code into PDB and xyz file with the name StructureOutput
"""
# Create Smilecode file to use later.
with open("{}smile.txt".format(MOL), 'w') as outfile:
outfile.write(SmileCode)
subprocess.run(["obabel", "-:{}".format(SmileCode), "-O", "{}tmp.pdb".format(MOL),
"--gen3d", "--ff", "GAFF"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if os.path.isfile("{}tmp.pdb".format(MOL)):
print('Obabel Finished successfully')
AtomsDictionary = {}
with open('{}tmp.pdb'.format(MOL), 'r') as infile:
LabelCoordinate = []
Labels = []
for line in infile:
if 'HETATM' in line:
column = line.split()
Label = column[2]
coordinate = line[20:80]
Labels.append(Label)
LabelCoordinate.append([Label, coordinate])
Elements = list(sorted(set(Labels)))
print('PDB has {} elements'.format(Elements))
for element in Elements:
ElementList = []
for idx in range(0, len(LabelCoordinate)):
if element in LabelCoordinate[idx]:
# print(LabelCoordinate[idx])
ElementList.append(LabelCoordinate[idx])
# print(ElementList)
for id, info in enumerate(ElementList):
#print(id, info)
AtomsDictionary[str(info[1])] = '{}{}'.format(
info[0], str(id+1))
with open('{}.pdb'.format("StructureOutput"), 'w') as outfile:
Coords = []
with open('{}tmp.pdb'.format(MOL), 'r') as infile:
for line in infile:
if 'HETATM' in line:
column = line.split()
coordinate = line[20:80]
Coords.append(coordinate)
# Write new PDB file
outfile.write("REMARK temporal PDB for parameterization \n")
outfile.write("AUTHOR SMILE2PDB " + str(date.today()) + " \n")
for ai in range(0, len(Coords)):
if ai < 9:
if int(AtomsDictionary.get(Coords[ai])[1:]) < 10:
PDBline = "ATOM " + \
str(ai+1) + " " + \
AtomsDictionary.get(
Coords[ai]) + " " + MOL + Coords[ai] + " \n"
else:
PDBline = "ATOM " + \
str(ai+1) + " " + \
AtomsDictionary.get(
Coords[ai]) + " " + MOL + Coords[ai] + " \n"
elif ai >= 99:
if int(AtomsDictionary.get(Coords[ai])[1:]) < 10:
PDBline = "ATOM " + \
str(ai+1) + " " + \
AtomsDictionary.get(
Coords[ai]) + " " + MOL + Coords[ai] + " \n"
else:
PDBline = "ATOM " + \
str(ai+1) + " " + \
AtomsDictionary.get(
Coords[ai]) + " " + MOL + Coords[ai] + " \n"
else:
if int(AtomsDictionary.get(Coords[ai])[1:]) < 10:
PDBline = "ATOM " + \
str(ai+1) + " " + \
AtomsDictionary.get(
Coords[ai]) + " " + MOL + Coords[ai] + " \n"
else:
PDBline = "ATOM " + \
str(ai+1) + " " + \
AtomsDictionary.get(
Coords[ai]) + " " + MOL + Coords[ai] + " \n"
outfile.write(PDBline)
outfile.write("END")
subprocess.run(["obabel", "-i", "pdb", "{}tmp.pdb".format(MOL), "-o", "xyz", "-O",
"{}.xyz".format("StructureOutput")], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
os.system("rm {}tmp.pdb".format(MOL))
# -------------MAIN-----------
settings = {}
with open('rendered_wano.yml', 'r') as file:
wano_file = yaml.full_load(file)
settings['SMILECode'] = wano_file['SMILES-CODE']
SMILE_Preprocessor('MOL', settings.get('SMILECode'))