File: FraggleSim.py

package info (click to toggle)
rdkit 202009.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 129,624 kB
  • sloc: cpp: 288,030; python: 75,571; java: 6,999; ansic: 5,481; sql: 1,968; yacc: 1,842; lex: 1,254; makefile: 572; javascript: 461; xml: 229; fortran: 183; sh: 134; cs: 93
file content (407 lines) | stat: -rw-r--r-- 14,463 bytes parent folder | download | duplicates (3)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# Copyright (c) 2013, GlaxoSmithKline Research & Development Ltd.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
#     * Neither the name of GlaxoSmithKline Research & Development Ltd.
#       nor the names of its contributors may be used to endorse or promote
#       products derived from this software without specific prior written
#       permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Created by Jameed Hussain, May 2013
"""
Fragmentation algorithm
-----------------------

identify acyclic bonds
enumerate all single cuts
make sure you chop off more that 1 atom
keeps bits which are >60% query mol
enumerate all double cuts
keeps bits with 1 attachment point (i.e throw middle bit away)
need to be >60% query mol

identify exocyclic bonds
enumerate all single "ring" cuts
Check if it results in more that one component
keep correct bit if >40% query mol

enumerate successful "rings" cuts with an acyclic cut
Check if it results in more that one component
keep correct if >60% query mol

"""
from itertools import combinations
import sys

from rdkit import Chem, DataStructs
from rdkit.Chem import rdqueries


# our default rdkit fingerprinter parameters:
rdkitFpParams = {'maxPath': 5, 'fpSize': 1024, 'nBitsPerHash': 2}

# Considered fragment types
FTYPE_ACYCLIC = 'acyclic'
FTYPE_CYCLIC = 'cyclic'
FTYPE_CYCLIC_ACYCLIC = 'cyclic_and_acyclic'

# Global SMARTS used by the program

# acyclic bond smarts
ACYC_SMARTS = Chem.MolFromSmarts("*!@!=!#*")
# exocyclic/fused exocyclic bond smarts
CYC_SMARTS = Chem.MolFromSmarts("[R1,R2]@[r;!R1]")

# smarts used to find appropriate fragment for
# would use SMARTS: [$([#0][r].[r][#0]),$([#0][r][#0])]
# but RDkit doesn't support component SMARTS in recursive one - $([#0][r].[r][#0])
# hence split into two
cSma1 = Chem.MolFromSmarts("[#0][r].[r][#0]")
cSma2 = Chem.MolFromSmarts("[#0][r][#0]")
dummyAtomQuery = rdqueries.AtomNumEqualsQueryAtom(0)


def delete_bonds(mol, bonds, ftype, hac):
  """ Fragment molecule on bonds and reduce to fraggle fragmentation SMILES.
  If none exists, returns None """

  # Replace the given bonds with attachment points (B1-B2 -> B1-*.*-B2)
  bondIdx = [mol.GetBondBetweenAtoms(*bond).GetIdx() for bond in bonds]
  modifiedMol = Chem.FragmentOnBonds(mol, bondIdx, dummyLabels=[(0, 0)] * len(bondIdx))

  # should be able to get away without sanitising mol as the valencies should be okay
  # do not do a full sanitization, but do find rings and calculate valences:
  Chem.SanitizeMol(modifiedMol, Chem.SanitizeFlags.SANITIZE_PROPERTIES |
                   Chem.SanitizeFlags.SANITIZE_SYMMRINGS)

  fragments = Chem.GetMolFrags(modifiedMol, asMols=True, sanitizeFrags=False)
  return select_fragments(fragments, ftype, hac)


def select_fragments(fragments, ftype, hac):
  if ftype == FTYPE_ACYCLIC:
    result = []
    result_hcount = 0
    for fMol in fragments:
      nAttachments = len(fMol.GetAtomsMatchingQuery(dummyAtomQuery))
      # check if terminal fragment
      if nAttachments == 1:
        fhac = fMol.GetNumAtoms()

        # if the fragment is 2 atoms (or less - includes attachment) it is too small
        # to be interesting. This check has the additional benefit
        # of pulling out the relevant single cuts as it discards
        # fragments where we only chop off a small part of the input cmpd
        if fhac > 3:
          result.append(Chem.MolToSmiles(fMol))
          result_hcount += fhac

    # needs to be greater than 60% of parent mol
    if result and (result_hcount > 0.6 * hac):
      result = '.'.join(result)
    else:
      result = None
    return result

  elif ftype == FTYPE_CYCLIC:
    # make sure it is 2 components
    if len(fragments) != 2:
      return None
    result = None
    for fMol in fragments:
      f = Chem.MolToSmiles(fMol)
      # check if a valid cut
      # needs to be greater 3 heavy atoms and greater than 40% of parent mol
      if isValidRingCut(fMol):
        result_hcount = fMol.GetNumAtoms()
        if (result_hcount > 3) and (result_hcount > 0.4 * hac):
          result = f
    return result

  elif (ftype == FTYPE_CYCLIC_ACYCLIC):
    # need to find the fragments which are valid which means they must be:
    #  Terminal (one attachment point) or valid ring cut
    result = []
    result_hcount = 0
    for fMol in fragments:
      nAttachments = len(fMol.GetAtomsMatchingQuery(dummyAtomQuery))
      # We need to have a fragment that has 1 or 2 attachment points and that has more than 3 atoms
      if nAttachments >= 3:
        continue
      fhac = fMol.GetNumAtoms()
      if fhac <= 3:
        continue

      if nAttachments == 2:
        # check if a valid cut
        if isValidRingCut(fMol):
          result.append(Chem.MolToSmiles(fMol))
          result_hcount += fhac
      elif nAttachments == 1:
        result.append(Chem.MolToSmiles(fMol))
        result_hcount += fhac

    # appropriate fragmentation must have 2 components and needs to be greater than 60% of
    # parent mol
    if len(result) == 2 and result_hcount > 0.6 * hac:
      result = '.'.join(result)
    else:
      result = None
    return result

  else:
    raise NotImplementedError('Invalid fragmentation type {0}'.format(ftype))


def isValidRingCut(mol):
  """ to check is a fragment is a valid ring cut, it needs to match the
  SMARTS: [$([#0][r].[r][#0]),$([#0][r][#0])] """
  # At this point, the molecule requires the identification of rings, so we need to sanitize
  Chem.SanitizeMol(mol, Chem.SanitizeFlags.SANITIZE_SYMMRINGS)
  return mol.HasSubstructMatch(cSma1) or mol.HasSubstructMatch(cSma2)


def generate_fraggle_fragmentation(mol, verbose=False):
  """ Create all possible fragmentations for molecule
    >>> q = Chem.MolFromSmiles('COc1cc(CN2CCC(NC(=O)c3cncc(C)c3)CC2)c(OC)c2ccccc12')
    >>> fragments = generate_fraggle_fragmentation(q)
    >>> fragments = sorted(['.'.join(sorted(s.split('.'))) for s in fragments])
    >>> fragments
     ['*C(=O)NC1CCN(Cc2cc(OC)c3ccccc3c2OC)CC1',
      '*C(=O)c1cncc(C)c1.*C1CCN(Cc2cc(OC)c3ccccc3c2OC)CC1',
      '*C(=O)c1cncc(C)c1.*Cc1cc(OC)c2ccccc2c1OC',
      '*C(=O)c1cncc(C)c1.*c1cc(OC)c2ccccc2c1OC',
      '*C1CCN(Cc2cc(OC)c3ccccc3c2OC)CC1',
      '*C1CCN(Cc2cc(OC)c3ccccc3c2OC)CC1.*c1cncc(C)c1',
      '*Cc1cc(OC)c2ccccc2c1OC.*NC(=O)c1cncc(C)c1',
      '*Cc1cc(OC)c2ccccc2c1OC.*c1cncc(C)c1',
      '*N1CCC(NC(=O)c2cncc(C)c2)CC1.*c1cc(OC)c2ccccc2c1OC',
      '*NC(=O)c1cncc(C)c1.*c1cc(OC)c2ccccc2c1OC',
      '*NC1CCN(Cc2cc(OC)c3ccccc3c2OC)CC1',
      '*NC1CCN(Cc2cc(OC)c3ccccc3c2OC)CC1.*c1cncc(C)c1',
      '*c1c(CN2CCC(NC(=O)c3cncc(C)c3)CC2)cc(OC)c2ccccc12',
      '*c1c(OC)cc(CN2CCC(NC(=O)c3cncc(C)c3)CC2)c(OC)c1*',
      '*c1cc(CN2CCC(NC(=O)c3cncc(C)c3)CC2)c(OC)c2ccccc12',
      '*c1cc(OC)c2ccccc2c1OC.*c1cncc(C)c1']
  """
  # query mol heavy atom count
  hac = mol.GetNumAtoms()

  # find the relevant bonds to break
  acyclic_matching_atoms = mol.GetSubstructMatches(ACYC_SMARTS)
  cyclic_matching_atoms = mol.GetSubstructMatches(CYC_SMARTS)
  if verbose:
    print("Matching Atoms:")
    print("acyclic matching atoms: ", acyclic_matching_atoms)
    print("cyclic matching atoms: ", cyclic_matching_atoms)

  # different cuts can give the same fragments
  # to use out_fragments to remove them
  out_fragments = set()

  ######################
  # Single acyclic Cuts
  ######################
  # loop to generate every single and double cut in the molecule
  # single cuts are not required as relevant single cut fragments can be found
  # from the double cuts. For explanation see check_fragments method
  for bond1, bond2 in combinations(acyclic_matching_atoms, 2):
    fragment = delete_bonds(mol, [bond1, bond2], FTYPE_ACYCLIC, hac)
    if fragment is not None:
      out_fragments.add(fragment)

  ##################################
  # Fused/Spiro exocyclic bond Cuts
  ##################################
  for bond1, bond2 in combinations(cyclic_matching_atoms, 2):
    fragment = delete_bonds(mol, [bond1, bond2], FTYPE_CYCLIC, hac)
    if fragment is None:
      continue
    out_fragments.add(fragment)
    # now do an acyclic cut with the successful cyclic cut
    for abond in acyclic_matching_atoms:
      fragment = delete_bonds(mol, [bond1, bond2, abond], FTYPE_CYCLIC_ACYCLIC, hac)
      if fragment is not None:
        out_fragments.add(fragment)

  return sorted(out_fragments)


def atomContrib(subs, mol, tverskyThresh=0.8):
  """ atomContrib algorithm
  generate fp of query_substructs (qfp)

  loop through atoms of smiles
    For each atom
    Generate partial fp of the atom (pfp)
    Find Tversky sim of pfp in qfp
    If Tversky < 0.8, mark atom in smiles

  Loop through marked atoms
    If marked atom in ring - turn all atoms in that ring to * (aromatic) or Sc (aliphatic)
    For each marked atom
      If aromatic turn to a *
      If aliphatic turn to a Sc

  Return modified smiles
  """

  def partialSimilarity(atomID):
    """ Determine similarity for the atoms set by atomID """
    # create empty fp
    modifiedFP = DataStructs.ExplicitBitVect(1024)
    modifiedFP.SetBitsFromList(aBits[atomID])
    return DataStructs.TverskySimilarity(subsFp, modifiedFP, 0, 1)

  # generate mol object & fp for input mol (we are interested in the bits each atom sets)
  pMol = Chem.Mol(mol)
  aBits = []
  _ = Chem.RDKFingerprint(pMol, atomBits=aBits, **rdkitFpParams)

  # generate fp of query_substructs
  qsMol = Chem.MolFromSmiles(subs)
  subsFp = Chem.RDKFingerprint(qsMol, **rdkitFpParams)

  # loop through atoms of smiles get atoms that have a high similarity with substructure
  marked = set()
  for atom in pMol.GetAtoms():
    if partialSimilarity(atom.GetIdx()) < tverskyThresh:
      marked.add(atom.GetIdx())

  # get rings to change

  # If a marked atom is within a ring, mark the whole ring
  markRingAtoms = set()
  for ring in pMol.GetRingInfo().AtomRings():
    if any(ringAtom in marked for ringAtom in ring):
      markRingAtoms.update(ring)
  marked.update(markRingAtoms)

  if marked:
    # now mutate the marked atoms
    for idx in marked:
      if pMol.GetAtomWithIdx(idx).GetIsAromatic():
        pMol.GetAtomWithIdx(idx).SetAtomicNum(0)
        pMol.GetAtomWithIdx(idx).SetNoImplicit(True)
      else:
        # gives best sim
        pMol.GetAtomWithIdx(idx).SetAtomicNum(21)
        # works better but when replace S it fails due to valency
        # pMol.GetAtomWithIdx(idx).SetAtomicNum(6)

    try:
      Chem.SanitizeMol(pMol, sanitizeOps=Chem.SANITIZE_ALL ^ Chem.SANITIZE_KEKULIZE ^
                       Chem.SANITIZE_SETAROMATICITY)
    except Exception:
      sys.stderr.write("Can't parse smiles: %s\n" % (Chem.MolToSmiles(pMol)))
      pMol = Chem.Mol(mol)

  return pMol


modified_query_fps = {}


def compute_fraggle_similarity_for_subs(inMol, qMol, qSmi, qSubs, tverskyThresh=0.8):
  qFP = Chem.RDKFingerprint(qMol, **rdkitFpParams)
  iFP = Chem.RDKFingerprint(inMol, **rdkitFpParams)

  rdkit_sim = DataStructs.TanimotoSimilarity(qFP, iFP)

  qm_key = "%s_%s" % (qSubs, qSmi)
  if qm_key in modified_query_fps:
    qmMolFp = modified_query_fps[qm_key]
  else:
    qmMol = atomContrib(qSubs, qMol, tverskyThresh)
    qmMolFp = Chem.RDKFingerprint(qmMol, **rdkitFpParams)
    modified_query_fps[qm_key] = qmMolFp

  rmMol = atomContrib(qSubs, inMol, tverskyThresh)

  # wrap in a try, catch
  try:
    rmMolFp = Chem.RDKFingerprint(rmMol, **rdkitFpParams)
    fraggle_sim = max(DataStructs.FingerprintSimilarity(qmMolFp, rmMolFp), rdkit_sim)
  except Exception:
    sys.stderr.write("Can't generate fp for: %s\n" % (Chem.MolToSmiles(rmMol, True)))
    fraggle_sim = 0.0

  return rdkit_sim, fraggle_sim


def GetFraggleSimilarity(queryMol, refMol, tverskyThresh=0.8):
  """ return the Fraggle similarity between two molecules

    >>> q = Chem.MolFromSmiles('COc1cc(CN2CCC(NC(=O)c3cncc(C)c3)CC2)c(OC)c2ccccc12')
    >>> m = Chem.MolFromSmiles('COc1cc(CN2CCC(NC(=O)c3ccccc3)CC2)c(OC)c2ccccc12')
    >>> sim,match = GetFraggleSimilarity(q,m)
    >>> sim
    0.980...
    >>> match
    '*C1CCN(Cc2cc(OC)c3ccccc3c2OC)CC1'

    >>> m = Chem.MolFromSmiles('COc1cc(CN2CCC(Nc3nc4ccccc4s3)CC2)c(OC)c2ccccc12')
    >>> sim,match = GetFraggleSimilarity(q,m)
    >>> sim
    0.794...
    >>> match
    '*C1CCN(Cc2cc(OC)c3ccccc3c2OC)CC1'

    >>> q = Chem.MolFromSmiles('COc1ccccc1')
    >>> sim,match = GetFraggleSimilarity(q,m)
    >>> sim
    0.347...
    >>> match
    '*c1ccccc1'

    """
  if hasattr(queryMol, '_fraggleDecomp'):
    frags = queryMol._fraggleDecomp
  else:
    frags = generate_fraggle_fragmentation(queryMol)
    queryMol._fraggleDecomp = frags
  qSmi = Chem.MolToSmiles(queryMol, True)
  result = 0.0
  bestMatch = None
  for frag in frags:
    _, fragsim = compute_fraggle_similarity_for_subs(refMol, queryMol, qSmi, frag, tverskyThresh)
    if fragsim > result:
      result = fragsim
      bestMatch = frag
  return result, bestMatch


# ------------------------------------
#
#  doctest boilerplate
#
def _runDoctests(verbose=None):  # pragma: nocover
  import doctest
  failed, _ = doctest.testmod(optionflags=doctest.ELLIPSIS + doctest.NORMALIZE_WHITESPACE,
                              verbose=verbose)
  sys.exit(failed)


if __name__ == '__main__':  # pragma: nocover
  _runDoctests()