File: UnitTestFingerprints.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 (84 lines) | stat: -rwxr-xr-x 2,918 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
#
#  Copyright (C) 2003-2006  Greg Landrum and 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 fingerprinting

"""
import unittest

from rdkit import Chem
from rdkit import DataStructs
from rdkit.Chem.Fingerprints import FingerprintMols

class TestCase(unittest.TestCase):

  def test1(self):
    # FIX: test HashAtom
    pass

  def test2(self):
    # FIX: test HashBond
    pass

  def test3(self):
    # FIX: test HashPath
    pass

  def test4(self):
    """ check containing mols, no Hs, no valence """
    tgts = [('CCC(O)C(=O)O', ('CCC', 'OCC', 'OCC=O', 'OCCO', 'CCCC', 'OC=O', 'CC(O)C')), ]
    for smi, matches in tgts:
      m = Chem.MolFromSmiles(smi)
      fp1 = Chem.RDKFingerprint(m, 2, 7, 9192, 4, 0)
      _ = fp1.GetOnBits()
      for match in matches:
        m2 = Chem.MolFromSmiles(match)
        fp2 = Chem.RDKFingerprint(m2, 2, 7, 9192, 4, 0)
        v1, _ = DataStructs.OnBitProjSimilarity(fp2, fp1)
        self.assertAlmostEqual(v1, 1, 'substruct %s not properly contained in %s' % (match, smi))

  def test5(self):
    """ check containing mols, use Hs, no valence """
    tgts = [('CCC(O)C(=O)O', ('O[CH-][CH2-]', 'O[CH-][C-]=O')), ]
    for smi, matches in tgts:
      m = Chem.MolFromSmiles(smi)
      fp1 = Chem.RDKFingerprint(m, 2, 7, 9192, 4, 1)
      _ = fp1.GetOnBits()
      for match in matches:
        m2 = Chem.MolFromSmiles(match)
        fp2 = Chem.RDKFingerprint(m2, 2, 7, 9192, 4, 1)
        v1, _ = DataStructs.OnBitProjSimilarity(fp2, fp1)
        self.assertAlmostEqual(v1, 1, 'substruct %s not properly contained in %s' % (match, smi))

  def test6(self):
    """ check that the bits in a signature of size N which has been folded in half
      are the same as those in a signature of size N/2 """
    smis = ['CCC(O)C(=O)O', 'c1ccccc1', 'C1CCCCC1', 'C1NCCCC1', 'CNCNCNC']
    for smi in smis:
      m = Chem.MolFromSmiles(smi)
      fp1 = Chem.RDKFingerprint(m, 2, 7, 4096)
      fp2 = DataStructs.FoldFingerprint(fp1, 2)
      fp3 = Chem.RDKFingerprint(m, 2, 7, 2048)
      self.assertEqual(tuple(fp2.GetOnBits()), tuple(fp3.GetOnBits()))

      fp2 = DataStructs.FoldFingerprint(fp2, 2)
      fp3 = Chem.RDKFingerprint(m, 2, 7, 1024)
      self.assertEqual(tuple(fp2.GetOnBits()), tuple(fp3.GetOnBits()))

      fp2 = DataStructs.FoldFingerprint(fp1, 4)
      self.assertEqual(tuple(fp2.GetOnBits()), tuple(fp3.GetOnBits()))

  def testGithub1747(self):
    """ test github #1747: deprecated apply() function causes GetRDKFingerprint
        to fail in Python 3 """
    fp = FingerprintMols.GetRDKFingerprint(Chem.MolFromSmiles('CCO'))
    self.assertNotEqual(0,fp.GetNumOnBits())

if __name__ == '__main__':  # pragma: nocover
  unittest.main()