File: UnitTestFeatMapParser.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 (128 lines) | stat: -rw-r--r-- 4,564 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
# $Id$
#
#  Copyright (C) 2006  greg Landrum
#
#   @@ 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.
#
from contextlib import closing
import unittest

from io import StringIO

from rdkit.Chem.FeatMaps import FeatMaps, FeatMapParser


def feq(n1, n2, tol=1e-5):
    return abs(n1 - n2) <= tol


class TestCase(unittest.TestCase):
    data = """

ScoreMode=Best
DirScoreMode=DotFullRange

BeginParams
  family=Aromatic radius=2.5 width=1.0 profile=Triangle
  family=Acceptor radius=1.5
EndParams

# optional
BeginPoints
  family=Acceptor pos=(1.0, 0.0, 5.0) weight=1.25 dir=(1, 1, 0)
  family=Aromatic pos=(0.0,1.0,0.0) weight=2.0 dir=(0,0,1) dir=(0,0,-1)
  family=Acceptor pos=(1.0,1.0,2.0) weight=1.25
EndPoints

"""

    def test1Basics(self):
        p = FeatMapParser.FeatMapParser()
        p.SetData(self.data)
        fm = p.Parse()
        self.assertTrue(fm.scoreMode == FeatMaps.FeatMapScoreMode.Best)
        self.assertTrue(fm.dirScoreMode == FeatMaps.FeatDirScoreMode.DotFullRange)
        self.assertTrue(fm.GetNumFeatures() == 3)

        feats = fm.GetFeatures()
        self.assertTrue(feq(feats[0].weight, 1.25))
        self.assertTrue(feq(feats[1].weight, 2.0))
        self.assertTrue(feq(feats[2].weight, 1.25))

        self.assertTrue(len(feats[0].featDirs) == 1)
        self.assertTrue(len(feats[1].featDirs) == 2)
        self.assertTrue(len(feats[2].featDirs) == 0)

        fams = [x.GetFamily() for x in feats]
        self.assertTrue(fams == ['Acceptor', 'Aromatic', 'Acceptor'])

    def test_FeatMapParser(self):
        # We can use a string
        p = FeatMapParser.FeatMapParser(data=self.data)
        fm = p.Parse()
        self.assertEqual(fm.GetNumFeatures(), 3)
        self.assertEqual([x.GetFamily() for x in fm.GetFeatures()],
                         ['Acceptor', 'Aromatic', 'Acceptor'])

        # We can use a list of strings
        p = FeatMapParser.FeatMapParser(data=self.data.split('\n'))
        fm = p.Parse()
        self.assertEqual(fm.GetNumFeatures(), 3)
        self.assertEqual([x.GetFamily() for x in fm.GetFeatures()],
                         ['Acceptor', 'Aromatic', 'Acceptor'])

        # and a stream
        with closing(StringIO(self.data)) as file:
            p = FeatMapParser.FeatMapParser(file=file)
        fm = p.Parse()
        self.assertEqual(fm.GetNumFeatures(), 3)
        self.assertEqual([x.GetFamily() for x in fm.GetFeatures()],
                         ['Acceptor', 'Aromatic', 'Acceptor'])

    def test_ParseErrors(self):
        # Typos in scoreMode or dirscoreMode section
        data = "scoreMode = typo\nbeginParams\nfamily=Acceptor radius=1.5\nEndParams"
        p = FeatMapParser.FeatMapParser(data=data)
        self.assertRaises(FeatMapParser.FeatMapParseError, p.Parse)

        data = "dirscoremode = typo\nbeginParams\nfamily=Acceptor radius=1.5\nEndParams"
        p = FeatMapParser.FeatMapParser(data=data)
        self.assertRaises(FeatMapParser.FeatMapParseError, p.Parse)

        data = "typo = All\nbeginParams\nfamily=Acceptor radius=1.5\nEndParams"
        p = FeatMapParser.FeatMapParser(data=data)
        self.assertRaises(FeatMapParser.FeatMapParseError, p.Parse)

        # Typos in paramBlock
        data = "beginTypo\nfamily=Acceptor radius=1.5\nEndParams"
        p = FeatMapParser.FeatMapParser(data=data)
        self.assertRaises(FeatMapParser.FeatMapParseError, p.Parse)

        data = "beginParams\nfamily=Acceptor radius=1.5\nEndTypo"
        p = FeatMapParser.FeatMapParser(data=data)
        self.assertRaises(FeatMapParser.FeatMapParseError, p.Parse)

        data = "beginParams\ntypo=Acceptor radius=1.5\nEndParams"
        p = FeatMapParser.FeatMapParser(data=data)
        self.assertRaises(FeatMapParser.FeatMapParseError, p.Parse)

        data = "beginParams\nprofile=Typo\nEndParams"
        p = FeatMapParser.FeatMapParser(data=data)
        self.assertRaises(FeatMapParser.FeatMapParseError, p.Parse)

        # Typos in points block
        data = "BeginPoints\npos=(1.0, 0.0, 5.0, 4.0)\nEndPoints"
        p = FeatMapParser.FeatMapParser(data=data)
        self.assertRaises(ValueError, p.Parse)

        data = "BeginPoints\npos=(1.0, 0.0, 5.0) typo=Acceptor\nEndPoints"
        p = FeatMapParser.FeatMapParser(data=data)
        self.assertRaises(FeatMapParser.FeatMapParseError, p.Parse)


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