rdkit.Chem.Scaffolds.MurckoScaffold

Package rdkit :: Package Chem :: Package Scaffolds :: Module MurckoScaffold |
[hide private]
[frames] | no frames]

# Source Code for Module rdkit.Chem.Scaffolds.MurckoScaffold

      1  # $Id: MurckoScaffold.py 3672 2010-06-14 17:10:00Z landrgr1 $
      2  #
      3  # Created by Peter Gedeck, September 2008
      4  #
      5  """
      6    Generation of Murcko scaffolds from a molecule
      7  """
      8
      9  from rdkit import Chem
     10  from rdkit.Chem import AllChem
     11
     12  murckoTransforms = [AllChem.ReactionFromSmarts('[*:1]-[!#1;D1]>>[*:1][H]'),
     13                      AllChem.ReactionFromSmarts('[*:1]-[!#1;D2]#[AD1]>>[*:1][H]'),
     14                      AllChem.ReactionFromSmarts('[*:1]-[!#1;D2]=[AD1]>>[*:1][H]'),
     15                      AllChem.ReactionFromSmarts('[*:1]-[!#1;D3](=[AD1])=[AD1]>>[*:1][H]')]
     16
     17
     18 -def MakeScaffoldGeneric(mol):
     19    """ Makes a Murcko scaffold generic (i.e. all atom types->C and all bonds ->single
     20
     21    >>> Chem.MolToSmiles(MakeScaffoldGeneric(Chem.MolFromSmiles('c1ccccc1')))
     22    'C1CCCCC1'
     23    >>> Chem.MolToSmiles(MakeScaffoldGeneric(Chem.MolFromSmiles('c1ncccc1')))
     24    'C1CCCCC1'
     25
     26    The following were associated with sf.net issue 246
     27    >>> Chem.MolToSmiles(MakeScaffoldGeneric(Chem.MolFromSmiles('c1[nH]ccc1')))
     28    'C1CCCC1'
     29    >>> Chem.MolToSmiles(MakeScaffoldGeneric(Chem.MolFromSmiles('C1[NH2+]C1')))
     30    'C1CC1'
     31    >>> Chem.MolToSmiles(MakeScaffoldGeneric(Chem.MolFromSmiles('C1[C@](Cl)(F)O1')))
     32    'CC1(C)CC1'
     33
     34    """
     35    res = Chem.Mol(mol)
     36    for atom in res.GetAtoms():
     37      if atom.GetAtomicNum() != 1:
     38        atom.SetAtomicNum(6)
     39      atom.SetIsAromatic(False)
     40      atom.SetFormalCharge(0)
     41      atom.SetChiralTag(Chem.ChiralType.CHI_UNSPECIFIED)
     42      atom.SetNoImplicit(0)
     43      atom.SetNumExplicitHs(0)
     44    for bond in res.GetBonds():
     45      bond.SetBondType(Chem.BondType.SINGLE)
     46      bond.SetIsAromatic(False)
     47    return Chem.RemoveHs(res)
     48
     49
     50  murckoPatts = ['[!#1;D3;$([D3]-[!#1])](=[AD1])=[AD1]', '[!#1;D2;$([D2]-[!#1])]=,#[AD1]',
     51                 '[!#1;D1;$([D1]-[!#1;!n])]']
     52  murckoQ = '[' + ','.join(['$(%s)' % x for x in murckoPatts]) + ']'
     53  murckoQ = Chem.MolFromSmarts(murckoQ)
     54  murckoPatts = [Chem.MolFromSmarts(x) for x in murckoPatts]
     55  aromaticNTransform = AllChem.ReactionFromSmarts('[n:1]-[D1]>>[nH:1]')
     56
     57
     58 -def GetScaffoldForMol(mol):
     59    """ Return molecule object containing scaffold of mol
     60
     61    >>> m = Chem.MolFromSmiles('Cc1ccccc1')
     62    >>> GetScaffoldForMol(m)
     63    <rdkit.Chem.rdchem.Mol object at 0x...>
     64    >>> Chem.MolToSmiles(GetScaffoldForMol(m))
     65    'c1ccccc1'
     66
     67    >>> m = Chem.MolFromSmiles('Cc1cc(Oc2nccc(CCC)c2)ccc1')
     68    >>> Chem.MolToSmiles(GetScaffoldForMol(m))
     69    'c1ccc(Oc2ccccn2)cc1'
     70
     71    """
     72    if 1:
     73      res = Chem.MurckoDecompose(mol)
     74      res.ClearComputedProps()
     75      res.UpdatePropertyCache()
     76      Chem.GetSymmSSSR(res)
     77    else:
     78      res = _pyGetScaffoldForMol(mol)
     79    return res
     80
     81
     82 -def _pyGetScaffoldForMol(mol):
     83    while mol.HasSubstructMatch(murckoQ):
     84      for patt in murckoPatts:
     85        mol = Chem.DeleteSubstructs(mol, patt)
     86    for atom in mol.GetAtoms():
     87      if atom.GetAtomicNum() == 6 and atom.GetNoImplicit() and atom.GetExplicitValence() < 4:
     88        atom.SetNoImplicit(False)
     89    h = Chem.MolFromSmiles('[H]')
     90    mol = Chem.ReplaceSubstructs(mol, Chem.MolFromSmarts('[D1;$([D1]-n)]'), h, True)[0]
     91    mol = Chem.RemoveHs(mol)
     92    return mol
     93
     94
     95 -def MurckoScaffoldSmiles(smiles=None, mol=None, includeChirality=False):
     96    """ Returns MurckScaffold Smiles from smiles
     97
     98    >>> MurckoScaffoldSmiles('Cc1cc(Oc2nccc(CCC)c2)ccc1')
     99    'c1ccc(Oc2ccccn2)cc1'
    100
    101    >>> MurckoScaffoldSmiles(mol=Chem.MolFromSmiles('Cc1cc(Oc2nccc(CCC)c2)ccc1'))
    102    'c1ccc(Oc2ccccn2)cc1'
    103
    104    """
    105    if smiles:
    106      mol = Chem.MolFromSmiles(smiles)
    107    if mol is None:
    108      raise ValueError('No molecule provided')
    109    scaffold = GetScaffoldForMol(mol)
    110    if not scaffold:
    111      return None
    112    return Chem.MolToSmiles(scaffold, includeChirality)
    113
    114
    115 -def MurckoScaffoldSmilesFromSmiles(smiles, includeChirality=False):
    116    """ Returns MurckScaffold Smiles from smiles
    117
    118    >>> MurckoScaffoldSmilesFromSmiles('Cc1cc(Oc2nccc(CCC)c2)ccc1')
    119    'c1ccc(Oc2ccccn2)cc1'
    120
    121    """
    122    return MurckoScaffoldSmiles(smiles=smiles, includeChirality=includeChirality)
    123
    124
    125  # ------------------------------------
    126  #
    127  #  doctest boilerplate
    128  #
    129 -def _runDoctests(verbose=None):  # pragma: nocover
    130    import sys
    131    import doctest
    132    failed, _ = doctest.testmod(optionflags=doctest.ELLIPSIS, verbose=verbose)
    133    sys.exit(failed)
    134
    135
    136  if __name__ == '__main__':  # pragma: nocover
    137    _runDoctests()
    138