File: UnitTestSmilesMolSupplier.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 (113 lines) | stat: -rwxr-xr-x 3,821 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
# $Id$
#
#  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 the Smiles file handling stuff
"""
import unittest

from rdkit import Chem

from rdkit import RDLogger


class TestCase(unittest.TestCase):

    def setUp(self):
        self.smis = ['CC', 'CCC', 'CCCCC', 'CCCCCC', 'CCCCCCC', 'CC', 'CCCCOC']
        self.nMolecules = len(self.smis)

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

    def assertMolecule(self, mol, i, msg=''):
        """ Assert that we have a valid molecule """
        self.assertIsNotNone(mol, '{0}read {1} failed'.format(msg, i))
        self.assertGreater(mol.GetNumAtoms(), 0, '{0}no atoms in mol {1}'.format(msg, i))

    def test_SmilesReaderIndex(self):
        # tests lazy reads
        supp = Chem.SmilesMolSupplierFromText('\n'.join(self.smis), ',', 0, -1, 0)
        for i in range(4):
            self.assertMolecule(next(supp), i)

        i = len(supp) - 1
        self.assertMolecule(supp[i], i)

        # Use in a list comprehension
        ms = [Chem.MolToSmiles(mol) for mol in supp]
        self.assertEqual(ms, self.smis)

        self.assertEqual(len(supp), self.nMolecules, 'bad supplier length')

        # Despite iterating through the whole supplier, we can still access by index
        i = self.nMolecules - 3
        self.assertMolecule(supp[i - 1], i, msg='back index: ')

        with self.assertRaises(IndexError):
            _ = supp[self.nMolecules]  # out of bound read must fail

        # and we can access with negative numbers
        mol1 = supp[len(supp) - 1]
        mol2 = supp[-1]
        self.assertEqual(Chem.MolToSmiles(mol1), Chem.MolToSmiles(mol2))

    def test_SmilesReaderIterator(self):
        # tests lazy reads using the iterator interface "
        supp = Chem.SmilesMolSupplierFromText('\n'.join(self.smis), ',', 0, -1, 0)

        nDone = 0
        for mol in supp:
            self.assertMolecule(mol, nDone)
            nDone += 1
        self.assertEqual(nDone, self.nMolecules, 'bad number of molecules')

        self.assertEqual(len(supp), self.nMolecules, 'bad supplier length')

        # Despite iterating through the whole supplier, we can still access by index
        i = self.nMolecules - 3
        self.assertMolecule(supp[i - 1], i, msg='back index: ')

        with self.assertRaises(IndexError):
            _ = supp[self.nMolecules]  # out of bound read must not fail

    def test_SmilesReaderBoundaryConditions(self):
        # Suppress the error message due to the incorrect smiles
        RDLogger.DisableLog('rdApp.error')

        smis = ['CC', 'CCOC', 'fail', 'CCO']
        supp = Chem.SmilesMolSupplierFromText('\n'.join(smis), ',', 0, -1, 0)
        self.assertEqual(len(supp), 4)
        self.assertIsNone(supp[2])
        self.assertIsNotNone(supp[3])

        supp = Chem.SmilesMolSupplierFromText('\n'.join(smis), ',', 0, -1, 0)
        self.assertIsNone(supp[2])
        self.assertIsNotNone(supp[3])
        self.assertEqual(len(supp), 4)
        with self.assertRaises(IndexError):
            supp[4]

        supp = Chem.SmilesMolSupplierFromText('\n'.join(smis), ',', 0, -1, 0)
        self.assertEqual(len(supp), 4)
        self.assertIsNotNone(supp[3])
        with self.assertRaises(IndexError):
            supp[4]

        supp = Chem.SmilesMolSupplierFromText('\n'.join(smis), ',', 0, -1, 0)
        with self.assertRaises(IndexError):
            supp[4]

        self.assertEqual(len(supp), 4)
        self.assertIsNotNone(supp[3])


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