-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathutils.py
144 lines (112 loc) · 4.03 KB
/
utils.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
"""Tools for manipulating graphs and converting from atom and pair features."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from rdkit import Chem
from rdkit.Chem import Descriptors
from rdkit.Chem.Scaffolds import MurckoScaffold
import torch.nn as nn
from rdkit import DataStructs
from rdkit.Chem import AllChem
from rdkit.Chem import RDConfig
import numpy as np
import hyp
import sys
import os
sys.path.append(os.path.join(RDConfig.RDContribDir, "SA_Score"))
import sascorer
def get_fingerprint(smiles, fingerprint_length, fingerprint_radius):
"""Get Morgan Fingerprint of a specific SMILES string.
Args:
smiles: String. The SMILES string of the molecule.
hparams: tf.contrib.training.HParams. Hyper parameters.
Returns:
np.array. shape = [hparams.fingerprint_length]. The Morgan fingerprint.
"""
if smiles is None:
return np.zeros((hyp.fingerprint_length,))
molecule = Chem.MolFromSmiles(smiles)
if molecule is None:
return np.zeros((hyp.fingerprint_length,))
fingerprint = AllChem.GetMorganFingerprintAsBitVect(
molecule, hyp.fingerprint_radius, hyp.fingerprint_length
)
arr = np.zeros((1,))
# ConvertToNumpyArray takes ~ 0.19 ms, while
# np.asarray takes ~ 4.69 ms
DataStructs.ConvertToNumpyArray(fingerprint, arr)
return arr
def atom_valences(atom_types):
"""Creates a list of valences corresponding to atom_types.
Note that this is not a count of valence electrons, but a count of the
maximum number of bonds each element will make. For example, passing
atom_types ['C', 'H', 'O'] will return [4, 1, 2].
Args:
atom_types: List of string atom types, e.g. ['C', 'H', 'O'].
Returns:
List of integer atom valences.
"""
periodic_table = Chem.GetPeriodicTable()
return [
max(list(periodic_table.GetValenceList(atom_type))) for atom_type in atom_types
]
def get_scaffold(mol):
"""Computes the Bemis-Murcko scaffold for a molecule.
Args:
mol: RDKit Mol.
Returns:
String scaffold SMILES.
"""
return Chem.MolToSmiles(MurckoScaffold.GetScaffoldForMol(mol), isomericSmiles=True)
def contains_scaffold(mol, scaffold):
"""Returns whether mol contains the given scaffold.
NOTE: This is more advanced than simply computing scaffold equality (i.e.
scaffold(mol_a) == scaffold(mol_b)). This method allows the target scaffold to
be a subset of the (possibly larger) scaffold in mol.
Args:
mol: RDKit Mol.
scaffold: String scaffold SMILES.
Returns:
Boolean whether scaffold is found in mol.
"""
pattern = Chem.MolFromSmiles(scaffold)
matches = mol.GetSubstructMatches(pattern)
return bool(matches)
def get_largest_ring_size(molecule):
"""Calculates the largest ring size in the molecule.
Refactored from
https://github.com/wengong-jin/icml18-jtnn/blob/master/bo/run_bo.py
Args:
molecule: Chem.Mol. A molecule.
Returns:
Integer. The largest ring size.
"""
cycle_list = molecule.GetRingInfo().AtomRings()
if cycle_list:
cycle_length = max([len(j) for j in cycle_list])
else:
cycle_length = 0
return cycle_length
def penalized_logp(molecule):
"""Calculates the penalized logP of a molecule.
Refactored from
https://github.com/wengong-jin/icml18-jtnn/blob/master/bo/run_bo.py
See Junction Tree Variational Autoencoder for Molecular Graph Generation
https://arxiv.org/pdf/1802.04364.pdf
Section 3.2
Penalized logP is defined as:
y(m) = logP(m) - SA(m) - cycle(m)
y(m) is the penalized logP,
logP(m) is the logP of a molecule,
SA(m) is the synthetic accessibility score,
cycle(m) is the largest ring size minus by six in the molecule.
Args:
molecule: Chem.Mol. A molecule.
Returns:
Float. The penalized logP value.
"""
log_p = Descriptors.MolLogP(molecule)
sas_score = sascorer.calculateScore(molecule)
largest_ring_size = get_largest_ring_size(molecule)
cycle_score = max(largest_ring_size - 6, 0)
return log_p - sas_score - cycle_score