File: UnitTestLipinski.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 (167 lines) | stat: -rwxr-xr-x 6,292 bytes parent folder | download | duplicates (2)
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
#
#  Copyright (C) 2003-2006  Rational Discovery LLC
#
#   @@ All Rights Reserved @@
#  This file is part of the RDKit.
#  The contents are covered by the terms of the BSD license
#  which is included in the file license.txt, found at the root
#  of the RDKit source tree.
#
""" unit testing code for Lipinski parameter calculation

  This provides a workout for the SMARTS matcher

"""


import os
import unittest

from rdkit import Chem
from rdkit import RDConfig
from rdkit.Chem import Lipinski, rdMolDescriptors, Crippen

NonStrict = "NUM_ROTATABLEBONDS_O"
Strict = "NUM_ROTATABLEBONDS"

doLong = False


class TestCase(unittest.TestCase):

  def setUp(self):
    self.inFileName = os.path.join(RDConfig.RDDataDir, 'NCI', 'first_200.props.sdf')

  def test1(self):
    " testing first 200 mols from NCI "
    # figure out which rotor version we are using
    m = Chem.MolFromSmiles("CC(C)(C)c1cc(O)c(cc1O)C(C)(C)C")
    if Lipinski.NumRotatableBonds(m) == 2:
      rot_prop = NonStrict
    else:
      rot_prop = Strict

    suppl = Chem.SDMolSupplier(self.inFileName)
    idx = 1
    for m in suppl:
      if m:
        calc = Lipinski.NHOHCount(m)
        orig = int(m.GetProp('NUM_LIPINSKIHDONORS'))
        assert calc == orig, 'bad num h donors for mol %d (%s): %d != %d' % (
          idx, m.GetProp('SMILES'), calc, orig)

        calc = Lipinski.NOCount(m)
        orig = int(m.GetProp('NUM_LIPINSKIHACCEPTORS'))
        assert calc == orig, 'bad num h acceptors for mol %d (%s): %d != %d' % (
          idx, m.GetProp('SMILES'), calc, orig)

        calc = Lipinski.NumHDonors(m)
        orig = int(m.GetProp('NUM_HDONORS'))
        assert calc == orig, 'bad num h donors for mol %d (%s): %d != %d' % (
          idx, m.GetProp('SMILES'), calc, orig)

        calc = Lipinski.NumHAcceptors(m)
        orig = int(m.GetProp('NUM_HACCEPTORS'))
        assert calc == orig, 'bad num h acceptors for mol %d (%s): %d != %d' % (
          idx, m.GetProp('SMILES'), calc, orig)

        calc = Lipinski.NumHeteroatoms(m)
        orig = int(m.GetProp('NUM_HETEROATOMS'))
        assert calc == orig, 'bad num heteroatoms for mol %d (%s): %d != %d' % (
          idx, m.GetProp('SMILES'), calc, orig)

        calc = Lipinski.NumRotatableBonds(m)
        orig = int(m.GetProp(rot_prop))
        assert calc == orig, 'bad num rotors for mol %d (%s): %d != %d' % (idx, m.GetProp('SMILES'),
                                                                           calc, orig)

        # test the underlying numrotatable bonds
        calc = rdMolDescriptors.CalcNumRotatableBonds(
          m, rdMolDescriptors.NumRotatableBondsOptions.NonStrict)
        orig = int(m.GetProp(NonStrict))
        assert calc == orig, 'bad num rotors for mol %d (%s): %d != %d' % (idx, m.GetProp('SMILES'),
                                                                           calc, orig)

        calc = rdMolDescriptors.CalcNumRotatableBonds(
          m, rdMolDescriptors.NumRotatableBondsOptions.Strict)
        orig = int(m.GetProp(Strict))
        assert calc == orig, 'bad num rotors for mol %d (%s): %d != %d' % (idx, m.GetProp('SMILES'),
                                                                           calc, orig)

      idx += 1

  def testIssue2183420(self):
    " testing a problem with the acceptor definition "
    self.assertTrue(Lipinski.NumHAcceptors(Chem.MolFromSmiles('NC')) == 1)
    self.assertTrue(Lipinski.NumHAcceptors(Chem.MolFromSmiles('CNC')) == 1)
    self.assertTrue(Lipinski.NumHAcceptors(Chem.MolFromSmiles('CN(C)C')) == 1)
    self.assertTrue(Lipinski.NumHAcceptors(Chem.MolFromSmiles('NC(=O)')) == 1)
    self.assertTrue(Lipinski.NumHAcceptors(Chem.MolFromSmiles('NC(=O)C')) == 1)
    self.assertTrue(Lipinski.NumHAcceptors(Chem.MolFromSmiles('CNC(=O)')) == 1)
    self.assertTrue(Lipinski.NumHAcceptors(Chem.MolFromSmiles('CNC(=O)C')) == 1)
    self.assertTrue(Lipinski.NumHAcceptors(Chem.MolFromSmiles('O=CNC(=O)C')) == 2)
    self.assertTrue(Lipinski.NumHAcceptors(Chem.MolFromSmiles('O=C(C)NC(=O)C')) == 2)


class TestCase_Regression(unittest.TestCase):

  @staticmethod
  def _regressionData(filename, col):
    """ Return entries form regression dataset.
    Returns the line number, smiles, molecule, and the value found in column col
    """
    with open(os.path.join(RDConfig.RDCodeDir, 'Chem', 'test_data', filename), 'r') as inF:
      for lineNum, line in enumerate(inF, 1):
        if line[0] == '#':
          continue
        splitL = line.split(',')
        smi = splitL[0]
        mol = Chem.MolFromSmiles(smi)
        if mol is None:
          raise AssertionError('line %d, smiles: %s' % (lineNum, smi))
        expected = float(splitL[col])
        yield lineNum, smi, mol, expected

  def __testDesc(self, fileN, col, func):
    for lineNum, smi, mol, expected in self._regressionData(fileN, col):
      if abs(expected - 666.0) < 1e-4:  # Unavailable data point
        continue
      try:
        val = func(mol)
      except Exception:
        val = 666
      assert abs(val - expected) < 1e-4, 'line %d, mol %s (calc = %f) should have val = %f' % (
        lineNum, smi, val, expected)

  def testLipinskiLong(self):
    """ Lipinski parameter """
    if not doLong:
      raise unittest.SkipTest('long test')
    fName = 'PP_descrs_regress.csv'
    self.__testDesc(fName, 30, Lipinski.NumHDonors)
    self.__testDesc(fName, 31, Lipinski.NumHeteroatoms)
    self.__testDesc(fName, 32, Lipinski.NumRotatableBonds)
    self.__testDesc(fName, 33, lambda x: Crippen.MolLogP(x, includeHs=1))

    fName = 'Block_regress.Lip.csv'
    self.__testDesc(fName, 1, Lipinski.NumHAcceptors)
    self.__testDesc(fName, 2, Lipinski.NumHDonors)
    self.__testDesc(fName, 3, Lipinski.NumHeteroatoms)
    self.__testDesc(fName, 4, Lipinski.NumRotatableBonds)

    fName = 'PP_descrs_regress.2.csv'
    self.__testDesc(fName, 33, lambda x: Crippen.MolLogP(x, includeHs=1))


if __name__ == '__main__':
  import argparse
  import sys
  parser = argparse.ArgumentParser()
  parser.add_argument('-l', default=False, action='store_true', dest='doLong')
  args = parser.parse_args()
  doLong = args.doLong

  # Remove the -l flag if present so that it isn't interpreted by unittest.main()
  if 'l' in sys.argv:
    sys.argv.remove('-l')
  unittest.main()