File: timings.py

package info (click to toggle)
rdkit 201809.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 123,688 kB
  • sloc: cpp: 230,509; python: 70,501; java: 6,329; ansic: 5,427; sql: 1,899; yacc: 1,739; lex: 1,243; makefile: 445; xml: 229; fortran: 183; sh: 123; cs: 93
file content (231 lines) | stat: -rw-r--r-- 5,803 bytes parent folder | download
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
from __future__ import print_function
import time, gzip, random, os, sys
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import Recap
from rdkit.RDLogger import logger

logger = logger()

tests = [1] * 1001
if len(sys.argv) > 1:
  tests = [0] * 1001
  tests[1] = 1
  for x in sys.argv[1:]:
    x = int(x)
    tests[x] = 1
ts = []

sdData = gzip.open('../Data/mols.1000.sdf.gz').read()
logger.info('mols from sdf')
suppl = Chem.SDMolSupplier()
suppl.SetData(sdData)
mols = []
nMols = 0
nBad = 0
t1 = time.time()
for m in suppl:
  if m:
    nMols += 1
    mols.append(m)
  else:
    nBad += 1
t2 = time.time()
logger.info('Results1: %.2f seconds, %d passed, %d failed' % (t2 - t1, nMols, nBad))
ts.append(t2 - t1)

if tests[2]:
  lines = gzip.open('../Data/mols.1000.txt.gz').readlines()
  logger.info('mols from smiles')
  nMols = 0
  nBad = 0
  t1 = time.time()
  for line in lines:
    line = line.decode().strip().split(' ')
    m = Chem.MolFromSmiles(line[1])
    if m:
      nMols += 1
    else:
      nBad += 1
  t2 = time.time()
  logger.info('Results2: %.2f seconds, %d passed, %d failed' % (t2 - t1, nMols, nBad))
  ts.append(t2 - t1)

if tests[3] or tests[4] or tests[5]:
  pattData = gzip.open('../Data/queries.txt.gz').readlines()
  pattData = [x.decode().strip().replace('[H]', '').replace('()', '') for x in pattData]
  logger.info('patterns from smiles')
  patts = []
  t1 = time.time()
  for line in pattData:
    m = Chem.MolFromSmarts(line)
    if m:
      nMols += 1
      patts.append(m)
    else:
      nBad += 1
  t2 = time.time()
  logger.info('Results3: %.2f seconds, %d passed, %d failed' % (t2 - t1, nMols, nBad))
  ts.append(t2 - t1)
  random.seed(23)
  random.shuffle(patts)
  patts = patts[:100]

if tests[4]:
  logger.info('Matching1: HasSubstructMatch')
  t1 = time.time()
  for mol in mols:
    for patt in patts:
      mol.HasSubstructMatch(patt)
  t2 = time.time()
  logger.info('Results4: %.2f seconds' % (t2 - t1))
  ts.append(t2 - t1)

if tests[5]:
  logger.info('Matching2: GetSubstructMatches')
  t1 = time.time()
  for mol in mols:
    for patt in patts:
      mol.GetSubstructMatches(patt)
  t2 = time.time()
  logger.info('Results5: %.2f seconds' % (t2 - t1))
  ts.append(t2 - t1)

if tests[6] or tests[7] or tests[8]:
  logger.info('reading SMARTS')
  patts = []
  t1 = time.time()
  for line in open('../Data/RLewis_smarts.txt'):
    line = line.strip()
    if line == '' or line[0] == '#':
      continue
    splitL = line.split(' ')
    sma = splitL[0]
    m = Chem.MolFromSmarts(sma)
    if m:
      patts.append(m)
  t2 = time.time()
  logger.info('Results6: %.2f seconds for %d patterns' % (t2 - t1, len(patts)))
  ts.append(t2 - t1)

if tests[7]:
  logger.info('Matching3: HasSubstructMatch')
  t1 = time.time()
  for mol in mols:
    for patt in patts:
      mol.HasSubstructMatch(patt)
  t2 = time.time()
  logger.info('Results7: %.2f seconds' % (t2 - t1))
  ts.append(t2 - t1)

if tests[8]:
  logger.info('Matching4: GetSubstructMatches')
  t1 = time.time()
  for mol in mols:
    for patt in patts:
      mol.GetSubstructMatches(patt)
  t2 = time.time()
  logger.info('Results8: %.2f seconds' % (t2 - t1))
  ts.append(t2 - t1)

if tests[9]:
  logger.info('Writing: Canonical SMILES')
  t1 = time.time()
  for mol in mols:
    smi = Chem.MolToSmiles(mol)
  t2 = time.time()
  logger.info('Results9: %.2f seconds' % (t2 - t1))
  ts.append(t2 - t1)

if tests[10]:
  logger.info('Writing: Mol blocks')
  t1 = time.time()
  for mol in mols:
    mb = Chem.MolToMolBlock(mol)
  t2 = time.time()
  logger.info('Results10: %.2f seconds' % (t2 - t1))
  ts.append(t2 - t1)

if tests[11]:
  logger.info('RECAP decomposition')
  t1 = time.time()
  for mol in mols:
    d = Recap.RecapDecompose(mol)
  t2 = time.time()
  logger.info('Results11: %.2f seconds' % (t2 - t1))
  ts.append(t2 - t1)

if tests[12]:
  logger.info('Generate 2D coords')
  t1 = time.time()
  for mol in mols:
    AllChem.Compute2DCoords(mol)
  t2 = time.time()
  logger.info('Results12: %.2f seconds' % (t2 - t1))
  ts.append(t2 - t1)

if tests[13]:
  logger.info('Generate 3D coords')
  t1 = time.time()
  nBad = 0
  for mol in mols:
    cid = AllChem.EmbedMolecule(mol, randomSeed=0xF00D)
    if cid < 0:
      nBad += 1
  t2 = time.time()
  logger.info('Results13: %.2f seconds %d failures' % (t2 - t1, nBad))
  ts.append(t2 - t1)

if tests[14]:
  logger.info('UFF optimizing those:')
  t1 = time.time()
  for mol in mols:
    if not mol.GetNumConformers():
      continue
    mol = Chem.Mol(mol)
    needMore = 1
    while needMore:
      needMore = AllChem.UFFOptimizeMolecule(mol, maxIters=200)
  t2 = time.time()
  logger.info('Results14: %.2f seconds' % (t2 - t1))
  ts.append(t2 - t1)

if tests[15]:
  logger.info('Find unique subgraphs')
  t1 = time.time()
  for mol in mols:
    Chem.FindUniqueSubgraphsOfLengthN(mol, 6)
  t2 = time.time()
  logger.info('Results15: %.2f seconds' % (t2 - t1))
  ts.append(t2 - t1)

if tests[16]:
  logger.info('Generate topological fingerprints')
  t1 = time.time()
  for mol in mols:
    Chem.RDKFingerprint(mol)
  t2 = time.time()
  logger.info('Results16: %.2f seconds' % (t2 - t1))
  ts.append(t2 - t1)

if tests[17]:
  logger.info('MMFF optimizing the molecules:')
  t1 = time.time()
  for i, mol in enumerate(mols):
    mol = Chem.Mol(mol)
    if not mol.GetNumConformers():
      continue
    if not AllChem.MMFFHasAllMoleculeParams(mol):
      continue
    needMore = 1
    while needMore:
      try:
        needMore = AllChem.MMFFOptimizeMolecule(mol, maxIters=200)
      except ValueError:
        logger.warning('Problems with MMFF and mol %d' % i)
        break
  t2 = time.time()
  logger.info('Results17: %.2f seconds' % (t2 - t1))
  ts.append(t2 - t1)

print('times: ', ' || '.join(['%.1f' % x for x in ts]))