-
Notifications
You must be signed in to change notification settings - Fork 2
/
preprocess.py
78 lines (46 loc) · 1.29 KB
/
preprocess.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
import rdkit.Chem as Chem
import json
path = './input/drd/drd2_train.txt'
Target_file = './input/drd/train/'
def preprocess(pairs):
smiles = pairs[0]
props = float(pairs[1])
processed = {}
processed['label'] = props
processed['smiles'] = smiles
count = 0
for atom in mol.GetAtoms():
atom.SetAtomMapNum(atom.GetIdx())
count += 1
edge = []
for bond in mol.GetBonds():
edge.append([bond.GetBeginAtomIdx(),bond.GetEndAtomIdx()])
processed['edges'] = edge
node = {}
for atom in mol.GetAtoms():
node[str(atom.GetIdx())] = atom.GetSymbol()
#print(type(atom.GetSymbol()),atom.GetIdx())
processed['features'] = node
return processed
def get_mol(smiles):
mol = Chem.MolFromSmiles(smiles)
if mol is None:
return None
return mol
def save_json(data,path):
b = json.dumps(data)
f2 = open(path, 'w')
f2.write(b)
f2.close()
with open(path,'r') as f:
data_1 = f.readlines()
data = [i.split(' ')[0:2] for i in data_1]
count = 0
for pairs in data:
mol = get_mol(pairs[0])
if mol is not None:
processed = preprocess(pairs)
save_path = Target_file + str(count) + '.json'
save_json(processed,save_path)
print(count)
count += 1