-
Notifications
You must be signed in to change notification settings - Fork 2
/
sa_score.py
executable file
·253 lines (204 loc) · 8.45 KB
/
sa_score.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
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
#!/usr/bin/env python
# Copyright 2022 Informatics Matters Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Synthetic accessibility score.
This is based on the work of Peter Ertl and Greg Landrum that can be found here:
https://github.com/rdkit/rdkit/tree/master/Contrib/SA_Score
That in turn is based on this paper:
Peter Ertl and Ansgar Schuffenhauer
Journal of Cheminformatics 1:8 (2009)
http://www.jcheminf.com/content/1/1/8
"""
import argparse, time, os, pickle, traceback, math
from sigfig import round
import utils, rdkit_utils
from dm_job_utilities.dm_log import DmLog
from rdkit import Chem
from rdkit.Chem import rdMolDescriptors
_fscores = None
def readFragmentScores(name='fpscores'):
import gzip
global _fscores
# generate the full path filename:
if name == "fpscores":
name = os.path.join(os.path.dirname(__file__), name)
data = pickle.load(gzip.open('%s.pkl.gz' % name))
outDict = {}
for i in data:
for j in range(1, len(i)):
outDict[i[j]] = float(i[0])
_fscores = outDict
def numBridgeheadsAndSpiro(mol, ri=None):
nSpiro = rdMolDescriptors.CalcNumSpiroAtoms(mol)
nBridgehead = rdMolDescriptors.CalcNumBridgeheadAtoms(mol)
return nBridgehead, nSpiro
def calculateScore(m):
if _fscores is None:
readFragmentScores()
# fragment score
fp = rdMolDescriptors.GetMorganFingerprint(m,
2) # <- 2 is the *radius* of the circular fingerprint
fps = fp.GetNonzeroElements()
score1 = 0.
nf = 0
for bitId, v in fps.items():
nf += v
sfp = bitId
score1 += _fscores.get(sfp, -4) * v
score1 /= nf
# features score
nAtoms = m.GetNumAtoms()
nChiralCenters = len(Chem.FindMolChiralCenters(m, includeUnassigned=True))
ri = m.GetRingInfo()
nBridgeheads, nSpiro = numBridgeheadsAndSpiro(m, ri)
nMacrocycles = 0
for x in ri.AtomRings():
if len(x) > 8:
nMacrocycles += 1
sizePenalty = nAtoms**1.005 - nAtoms
stereoPenalty = math.log10(nChiralCenters + 1)
spiroPenalty = math.log10(nSpiro + 1)
bridgePenalty = math.log10(nBridgeheads + 1)
macrocyclePenalty = 0.
# ---------------------------------------
# This differs from the paper, which defines:
# macrocyclePenalty = math.log10(nMacrocycles+1)
# This form generates better results when 2 or more macrocycles are present
if nMacrocycles > 0:
macrocyclePenalty = math.log10(2)
score2 = 0. - sizePenalty - stereoPenalty - spiroPenalty - bridgePenalty - macrocyclePenalty
# correction for the fingerprint density
# not in the original publication, added in version 1.1
# to make highly symmetrical molecules easier to synthetise
score3 = 0.
if nAtoms > len(fps):
score3 = math.log(float(nAtoms) / len(fps)) * .5
sascore = score1 + score2 + score3
# need to transform "raw" value into scale between 1 and 10
min = -4.0
max = 2.5
sascore = 11. - (sascore - min + 1) / (max - min) * 9.
# smooth the 10-end
if sascore > 8.:
sascore = 8. + math.log(sascore + 1. - 9.)
if sascore > 10.:
sascore = 10.0
elif sascore < 1.:
sascore = 1.0
return sascore
def process(input,
outfile,
delimiter,
id_column=None,
mol_column=0,
read_header=False,
write_header=False,
read_records=100,
interval=0):
utils.expand_path(outfile)
count = 0
errors = 0
# setup the reader
reader = rdkit_utils.create_reader(input,
id_column=id_column,
mol_column=mol_column,
read_records=read_records,
read_header=read_header,
delimiter=delimiter)
extra_field_names = reader.get_extra_field_names()
calc_field_names = ['sa_score']
# setup the writer
writer = rdkit_utils.create_writer(outfile,
extra_field_names=extra_field_names,
calc_prop_names=calc_field_names,
delimiter=delimiter,
id_column=id_column,
mol_column=mol_column)
id_col_type, id_col_value = utils.is_type(id_column, int)
# read the input records and write the output
while True:
t = reader.read()
# break if no more data to read
if not t:
break
mol, smi, id, props = t
if count == 0 and write_header:
headers = rdkit_utils.generate_headers(
id_col_type,
id_col_value,
reader.get_mol_field_name(),
reader.field_names,
calc_field_names,
False)
writer.write_header(headers)
count += 1
if interval and count % interval == 0:
DmLog.emit_event("Processed {} records".format(count))
if count % 50000 == 0:
# Emit a 'total' cost, replacing all prior costs
DmLog.emit_cost(count)
if not mol:
errors += 1
DmLog.emit_event("Failed to process record", count)
continue
# calculate the synthetic accessibility score props
try:
sa_score = calculateScore(mol)
if sa_score is not None:
sa_score = round(sa_score, sigfigs=3)
except:
errors += 1
DmLog.emit_event('Failed to process record', count)
traceback.print_exc()
continue
# write the output
writer.write(smi, mol, id, props, [sa_score])
writer.close()
reader.close()
return count, errors
def main():
# Example usage:
# ./sa_score.py -i data/100.smi -o out.smi -d tab --id-column 1 --write-header
parser = argparse.ArgumentParser(description='SA Score')
parser.add_argument('-i', '--input', required=True, help="Input file as SMILES or SDF")
parser.add_argument('-o', '--outfile', required=True, help="Output file as SMILES or SDF")
# to pass tab as the delimiter specify it as $'\t' or use one of the symbolic names 'comma', 'tab', 'space' or 'pipe'
parser.add_argument('-d', '--delimiter', help="Delimiter when using SMILES")
parser.add_argument('--id-column', help="Column for name field (zero based integer for .smi, text for SDF)")
parser.add_argument('--mol-column', type=int, default=0,
help="Column index for molecule when using delineated text formats (zero based integer)")
parser.add_argument('--read-header', action='store_true',
help="Read a header line with the field names when reading .smi or .txt")
parser.add_argument('--write-header', action='store_true', help='Write a header line when writing .smi or .txt')
parser.add_argument('--read-records', default=100, type=int,
help="Read this many records to determine the fields that are present")
parser.add_argument("--interval", type=int, help="Reporting interval")
args = parser.parse_args()
DmLog.emit_event("rdk_props.py: ", args)
delimiter = utils.read_delimiter(args.delimiter)
t0 = time.time()
count, errors = process(args.input, args.outfile, delimiter, id_column=args.id_column, mol_column=args.mol_column,
read_header=args.read_header, write_header=args.write_header,
read_records=args.read_records, interval=args.interval, )
t1 = time.time()
# Duration? No less than 1 second?
duration_s = int(t1 - t0)
if duration_s < 1:
duration_s = 1
DmLog.emit_event('Processed {} records in {} seconds. {} errors.'.format(count, duration_s, errors))
# Emit final 'total' cost, replacing all prior costs
DmLog.emit_cost(count)
if __name__ == "__main__":
main()