File: testsmartssym.py

package info (click to toggle)
openbabel 2.4.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 58,308 kB
  • sloc: cpp: 459,210; ansic: 90,514; php: 13,963; python: 7,899; perl: 6,518; pascal: 793; sh: 179; xml: 97; ruby: 64; makefile: 46; java: 23; cs: 14
file content (110 lines) | stat: -rw-r--r-- 4,251 bytes parent folder | download
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
"""Test OpenBabel executables from Python

Note: Python bindings not used

On Windows or Linux, you can run these tests at the commandline
in the build folder with:
"C:\Program Files\CMake 2.6\bin\ctest.exe" -C CTestTestfile.cmake
                                           -R pytest -VV

You could also "chdir" into build/test and run the test file directly:
python ../../../test/testsmartssym.py

In both cases, the test file is run directly from the source folder,
and so you can quickly develop the tests and try them out.
"""

import os
import unittest
import pdb

from testbabel import run_exec, executable, log, BaseTest

def checkmatch(query, molecules):
    result = []
    for smi in molecules:
        output, error = run_exec("obabel -:%s -s%s -osmi" % (smi, query))
        result.append(output.strip() != "")
    return result

def fastcheckmatch(query, molecules):
    """May fail where Open Babel does not output the input query, e.g.
    [C@@]([H])(Br)(Cl)I is output as [C@@H](Br)(Cl)I"""
    output, error = run_exec("\n".join(molecules), "obabel -ismi -s%s -osmi" % query)
    converted = [x.rstrip() for x in output.split("\n")]
    results = [smi in converted for smi in molecules]
    return results

class TestSmartsSym(BaseTest):
    """Base class for a series of tests relating to symmetry"""

    def testSelfMatch(self):
        """Verify that a molecule matches itself"""
        data = [
                '[C@@](F)(Br)(Cl)I',
                '[C@](F)(Br)(Cl)I',
                'F[C@](Br)(Cl)I',
                '[C@H](Br)(Cl)I',
                'Br[C@H](Cl)I',
                '[C@]1(Br)(Cl)NC1',
                '[C@@]1(Br)(Cl)NC1',
                'Br[C@]1(Cl)NC1',
                'C1N[C@]1(Cl)Br',
                'F[C@]1(Br)N[C@]1(Br)Cl',
                '[C@H]1(Cl)NC1'
            ]
        for smi in data:
            output, error = run_exec("obabel -:%s -s%s -osmi" % (smi, smi))
            self.assertEqual(output.rstrip(), smi)

    def testTetStereo(self):
        data = ['[C@@](F)(Br)(Cl)I',
                '[C@](F)(Br)(Cl)I',
                'F[C@](Br)(Cl)I',
                'F[C@@](Br)(Cl)I',
                'C(F)(Br)(Cl)I',
                'FC(Br)(Cl)I']
        self.assertEqual(fastcheckmatch(data[0], data[0:6]),
                         [True, False, False, True, False, False])
        self.assertEqual(fastcheckmatch(data[2], data[0:6]),
                         [False, True, True, False, False, False])
        self.assertEqual(fastcheckmatch(data[4], data[0:6]), [True]*6)
        
    def testTetStereoImplicitH(self):
        data = ['[C@H](Br)(Cl)I',
                '[C@@H](Br)(Cl)I',
                'Br[C@H](Cl)I',
                'Br[C@@H](Cl)I',
                'BrC(Cl)I',
                'BrC([H])(Cl)I',
                'Br[C@@]([H])(Cl)I'
                ]
        self.assertEqual(checkmatch(data[0], data[0:7]),
                         [True, False, False, True, False, False, True])
        self.assertEqual(checkmatch(data[2], data[0:7]),
                         [False, True, True, False, False, False, False])
        self.assertEqual(checkmatch(data[4], data[0:7]), [True]*7)
        self.assertEqual(checkmatch(data[6], data[0:7]),
                         [True, False, False, True, False, False, True])

    def testRingClosures(self):
        data = ['[C@]1(Br)(Cl)NC1',
                '[C@@]1(Br)(Cl)NC1',
                'Br[C@]1(Cl)NC1',
                'Br[C@@]1(Cl)NC1',
                'C1N[C@]1(Cl)Br',
                'C1NC1(Cl)Br']
        self.assertEqual(fastcheckmatch(data[0], data[0:6]),
                         [True, False, False, True, True, False])
        self.assertEqual(fastcheckmatch(data[2], data[0:6]),
                         [False, True, True, False, False, False])
        self.assertEqual(fastcheckmatch(data[5], data[0:6]), [True]*6)
        

if __name__ == "__main__":
    testsuite = []
    allclasses = [TestSmartsSym]
    for myclass in allclasses:
        suite = unittest.TestLoader().loadTestsFromTestCase(myclass)
        testsuite.append(suite)
    unittest.TextTestRunner().run(unittest.TestSuite(testsuite))