-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_pmi.py
executable file
·323 lines (281 loc) · 9.06 KB
/
calc_pmi.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
####################
Calculate PMI values
####################
*Created on Tue Aug 31 2021 08:45 by A. Pahl*
Molecules are excluded from the PMI calculation when they have either
- more than one undefined stereocenter, or
- one or more defined stereocenters and at least one undefined stereocenter
(creating diastereomers)
"""
import csv, gzip, sys
import time
import argparse
import signal
from contextlib import contextmanager
from rdkit.Chem import AllChem as Chem
from rdkit import RDLogger
from jupy_tools import pmi
LOG = RDLogger.logger()
LOG.setLevel(RDLogger.CRITICAL)
# Timeout code is taken from José's NPFC project:
# https://github.com/mpimp-comas/npfc/blob/master/npfc/utils.py
def raise_timeout(signum, frame):
"""Function to actually raise the TimeoutError when the time has come."""
raise TimeoutError
@contextmanager
def timeout(time):
# register a function to raise a TimeoutError on the signal.
signal.signal(signal.SIGALRM, raise_timeout)
# schedule the signal to be sent after time
signal.alarm(time)
# run the code block within the with statement
try:
yield
except TimeoutError:
pass # exit the with statement
finally:
# unregister the signal so it won't be triggered if the timeout is not reached
signal.signal(signal.SIGALRM, signal.SIG_IGN)
def get_value(str_val):
"""convert a string into float or int, if possible."""
if not str_val:
return ""
if str_val is None:
return ""
try:
val = float(str_val)
if "." not in str_val:
val = int(val)
except ValueError:
val = str_val
return val
def smiles_to_mol(smiles: str) -> Chem.Mol:
"""Generate a RDKit Molecule from a Smiles.
Parameters:
===========
smiles: the input string
Returns:
========
The RDKit Molecule. If the Smiles parsing failed, NAN is returned instead.
"""
try:
mol = Chem.MolFromSmiles(smiles)
if mol is not None:
return mol
return None
except:
return None
def mol_to_smiles(mol: Chem.Mol, canonical: bool = True) -> str:
"""Generate Smiles from mol.
Parameters:
===========
mol: the input molecule
canonical: whether to return the canonical Smiles or not
Returns:
========
The Smiles of the molecule (canonical by default). NAN for failed molecules."""
if mol is None:
return None
try:
smi = Chem.MolToSmiles(mol, canonical=canonical)
return smi
except:
return None
def csv_supplier(fo, dialect):
reader = csv.DictReader(fo, dialect=dialect)
for row in reader:
mol = smiles_to_mol(row["Smiles"])
if mol is None:
yield {"Mol": None}
continue
d = {}
for prop in row:
if prop == "Smiles":
continue
d[prop] = get_value(row[prop])
d["Mol"] = mol
yield d
def sdf_supplier(fo):
reader = Chem.ForwardSDMolSupplier(fo)
for mol in reader:
if mol is None:
yield {"Mol": None}
continue
d = {}
# Is the SD file name property used?
name = mol.GetProp("_Name")
if len(name) > 0:
d["Name"] = get_value(name)
for prop in mol.GetPropNames():
d[prop] = get_value(mol.GetProp(prop))
for prop in mol.GetPropNames():
d[prop] = get_value(mol.GetProp(prop))
mol.ClearProp(prop)
d["Mol"] = mol
yield d
def process(
fn: str,
id_col: str,
tout: int,
verbose: bool,
every_n: int,
):
columns = [id_col, "PMI1", "PMI2", "Duration", "Smiles"]
header = []
ctr = {x: 0 for x in ["In", "Out", "Fail_NoMol", "UndefStereo", "Timeout"]}
first_mol = True
sd_props = set()
fn = fn.split(",") # allow comma separated list of files
first_dot = fn[0].find(".")
fn_base = fn[0][:first_dot]
out_fn = f"{fn_base}_pmi.tsv"
outfile = open(out_fn, "w")
# Initialize reader for the correct input type
if verbose:
# Add file name info and print newline after each info line.
fn_info = f"({fn_base})"
end_char = "\n"
else:
fn_info = ""
end_char = "\r"
for f in fn:
do_close = True
if "sd" in f:
if f.endswith(".gz"):
file_obj = gzip.open(f, mode="rb")
else:
file_obj = open(f, "rb")
reader = sdf_supplier(file_obj)
elif "csv" in f:
if f.endswith(".gz"):
file_obj = gzip.open(f, mode="rb")
else:
file_obj = open(f, "r")
reader = csv_supplier(file_obj, dialect="excel")
elif "tsv" in f:
if f.endswith(".gz"):
file_obj = gzip.open(f, mode="rb")
else:
file_obj = open(f, "r")
reader = csv_supplier(file_obj, dialect="excel-tab")
else:
raise ValueError(f"Unknown input file format: {f}")
for rec in reader:
ctr["In"] += 1
mol = rec["Mol"]
if mol is None:
ctr["Fail_NoMol"] += 1
continue
# Check for undefined stereochemistry.
# Skip molecules that have either
# more than one undefined stereocenter or
# more than 0 defined stereocenters and at least one undefined stereocenter
# (creating diastereomers)
_, _, is_diastereomer = pmi.get_stereo_counts(mol)
if is_diastereomer:
ctr["UndefStereo"] += 1
continue
if first_mol:
first_mol = False
header = columns.copy()
sd_props = set(header.copy())
outfile.write("\t".join(header) + "\n")
mol_props = set()
d = {}
for prop in rec:
if prop in sd_props:
if prop == "Mol":
continue
mol_props.add(prop)
d[prop] = rec[prop]
# append "" to the missing props that were not in the mol:
missing_props = sd_props - mol_props
for prop in missing_props:
d[prop] = ""
# Measure how long the calculation takes
start = time.time()
timed_out = True
with timeout(tout):
try:
pmi1, pmi2 = pmi.calc_pmi(mol, n_conformers=15, avg=3)
timed_out = False
except:
pass
if timed_out:
ctr["Timeout"] += 1
continue
end = time.time()
elapsed = round(end - start, 1)
d["PMI1"] = pmi1
d["PMI2"] = pmi2
d["Smiles"] = mol_to_smiles(mol)
d["Duration"] = elapsed
ctr["Out"] += 1
line = [str(d[x]) for x in header]
outfile.write("\t".join(line) + "\n")
if ctr["In"] % every_n == 0:
print(
f"{fn_info} In: {ctr['In']:8d} Out: {ctr['Out']: 8d} Failed: {ctr['Fail_NoMol']:6d} "
f"UndefStereo: {ctr['UndefStereo']:6d} Timeout: {ctr['Timeout']:6d} ",
end=end_char,
)
sys.stdout.flush()
if do_close:
file_obj.close()
outfile.close()
print(
f"{fn_info} In: {ctr['In']:8d} Out: {ctr['Out']: 8d} Failed: {ctr['Fail_NoMol']:6d} "
f"UndefStereo: {ctr['UndefStereo']:6d} Timeout: {ctr['Timeout']:6d} done."
)
print("")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="""
Calculation of Principal Moment of Inertia (PMI).
Generation of multiple conformations and averaging by Median.
Molecules are excluded from the MPI calculation when they have either
- more than one undefined stereocenter, or
- one or more defined stereocenters and at least one undefined stereocenter
(creating diastereomers)
Example:
`$ ./calc_pmi.py enamine_full.tsv`
""",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"in_file",
help="The optionally gzipped input file (CSV, TSV or SDF). Can also be a comma-separated list of file names.",
)
parser.add_argument(
"id_col",
help=("The name of the ID columnoutput type, " "e.g. `Compound_Id` "),
)
parser.add_argument(
"-t",
type=int,
default=30,
help="Timeout in seconds for each molecule (default: 30).",
)
parser.add_argument(
"-v",
action="store_true",
help="Turn on verbose status output.",
)
parser.add_argument(
"-n",
type=int,
default=200,
help="Show info every `N` records (default: 200).",
)
args = parser.parse_args()
print(args)
process(
args.in_file,
args.id_col,
args.t,
args.v,
args.n,
)