File: UnitTestSuppliers.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 (103 lines) | stat: -rwxr-xr-x 3,021 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
#
#  Copyright (C) 2003-2017  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 molecule suppliers

"""
import os
import tempfile
import unittest

from rdkit import Chem, RDLogger
from rdkit import RDConfig


class TestCase(unittest.TestCase):

    def tearDown(self):
        RDLogger.EnableLog('rdApp.error')

    def test1SDSupplier(self):
        fileN = os.path.join(RDConfig.RDCodeDir, 'VLib', 'NodeLib', 'test_data', 'NCI_aids.10.sdf')

        suppl = Chem.SDMolSupplier(fileN)
        ms = [x for x in suppl]
        self.assertEqual(len(ms), 10)

        # test repeating:
        ms = [x for x in suppl]
        self.assertEqual(len(ms), 10)

        # test reset:
        suppl.reset()
        m = next(suppl)
        self.assertEqual(m.GetProp('_Name'), '48')
        self.assertEqual(m.GetProp('NSC'), '48')
        self.assertEqual(m.GetProp('CAS_RN'), '15716-70-8')
        m = next(suppl)
        self.assertEqual(m.GetProp('_Name'), '78')
        self.assertEqual(m.GetProp('NSC'), '78')
        self.assertEqual(m.GetProp('CAS_RN'), '6290-84-2')

        suppl.reset()
        for _ in range(10):
            m = next(suppl)

        with self.assertRaises(StopIteration):
            m = next(suppl)

    def test2SmilesSupplier(self):
        fileN = os.path.join(RDConfig.RDCodeDir, 'VLib', 'NodeLib', 'test_data', 'pgp_20.txt')

        suppl = Chem.SmilesMolSupplier(
            fileN, delimiter='\t', smilesColumn=2, nameColumn=1, titleLine=1)
        ms = [x for x in suppl]
        self.assertEqual(len(ms), 20)

        # test repeating:
        ms = [x for x in suppl]
        self.assertEqual(len(ms), 20)
        # test reset:
        suppl.reset()
        m = next(suppl)
        self.assertEqual(m.GetProp('_Name'), 'ALDOSTERONE')
        self.assertEqual(m.GetProp('ID'), 'RD-PGP-0001')
        m = next(suppl)
        self.assertEqual(m.GetProp('_Name'), 'AMIODARONE')
        self.assertEqual(m.GetProp('ID'), 'RD-PGP-0002')
        suppl.reset()
        for _ in range(20):
            m = next(suppl)
        with self.assertRaises(StopIteration):
            m = next(suppl)

    def test3SmilesSupplier(self):
        txt = """C1CC1,1
CC(=O)O,3
fail,4
CCOC,5
"""
        RDLogger.DisableLog('rdApp.error')

        try:
            with tempfile.NamedTemporaryFile('w+', suffix='.csv', delete=False) as tmp:
                tmp.write(txt)
            suppl = Chem.SmilesMolSupplier(tmp.name, delimiter=',', smilesColumn=0, nameColumn=1,
                                           titleLine=0)
            ms = [x for x in suppl]
            suppl = None
            while ms.count(None):
                ms.remove(None)
            self.assertEqual(len(ms), 3)
        finally:
            os.unlink(tmp.name)


if __name__ == '__main__':
    unittest.main()