File: testPartialCharges.py

package info (click to toggle)
rdkit 202209.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 203,880 kB
  • sloc: cpp: 334,239; python: 80,247; ansic: 24,579; java: 7,667; sql: 2,123; yacc: 1,884; javascript: 1,358; lex: 1,260; makefile: 576; xml: 229; fortran: 183; cs: 181; sh: 101
file content (139 lines) | stat: -rw-r--r-- 4,844 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
129
130
131
132
133
134
135
136
137
138
139

import unittest
import os
import io

import pickle

from rdkit import Chem
from rdkit.Chem import rdPartialCharges
from rdkit import RDConfig


def feq(v1, v2, tol2=1e-4):
    return abs(v1 - v2) <= tol2


class TestCase(unittest.TestCase):

    def setUp(self):
        pass

    def test0HalgrenSet(self):
        smiSup = Chem.SmilesMolSupplier(
          os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'PartialCharges', 'Wrap', 'test_data',
                       'halgren.smi'), delimiter='\t')

        # parse the original file
        with open(
            os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'PartialCharges', 'Wrap', 'test_data',
                         'halgren_out.txt'), 'r') as infil:
            lines = infil.readlines()

        tab = Chem.GetPeriodicTable()

        olst = []
        for mol in smiSup:
            rdPartialCharges.ComputeGasteigerCharges(mol)
            tstr = "Molecule: "
            tstr += mol.GetProp("_Name")
            olst.append(tstr)
            for i in range(mol.GetNumAtoms()):
                at = mol.GetAtomWithIdx(i)
                en = tab.GetElementSymbol(at.GetAtomicNum())
                chg = float(at.GetProp("_GasteigerCharge"))
                tstr = "%i %s %6.4f" % (i, en, chg)
                olst.append(tstr)

        i = 0
        for line in lines:
            self.assertTrue(line.strip() == olst[i])
            i += 1

    def test1PPDataset(self):
        fileN = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'PartialCharges', 'Wrap',
                             'test_data', 'PP_descrs_regress.2.csv')
        infil = open(fileN, 'r')
        lines = infil.readlines()
        infil.close()

        infile = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'PartialCharges', 'Wrap',
                              'test_data', 'PP_combi_charges.pkl')
        with open(infile, 'r') as cchtFile:
            buf = cchtFile.read().replace('\r\n', '\n').encode('utf-8')
            cchtFile.close()
        with io.BytesIO(buf) as cchFile:
            combiCharges = pickle.load(cchFile)

        for lin in lines:
            if (lin[0] == '#'):
                continue
            tlst = lin.strip().split(',')
            smi = tlst[0]
            rdmol = Chem.MolFromSmiles(smi)
            rdPartialCharges.ComputeGasteigerCharges(rdmol)

            nat = rdmol.GetNumAtoms()
            failed = False
            for ai in range(nat):
                rdch = float(rdmol.GetAtomWithIdx(ai).GetProp('_GasteigerCharge'))
                if not feq(rdch, combiCharges[smi][ai], 1.e-2):
                    failed = True
                    print(smi, ai, rdch, combiCharges[smi][ai])
            if failed:
                rdmol.Debug()
            self.assertFalse(failed)

    def test2Params(self):
        """ tests handling of Issue187 """
        m1 = Chem.MolFromSmiles('C(=O)[O-]')
        rdPartialCharges.ComputeGasteigerCharges(m1)

        m2 = Chem.MolFromSmiles('C(=O)[O-].[Na+]')
        rdPartialCharges.ComputeGasteigerCharges(m2)

        for i in range(m1.GetNumAtoms()):
            c1 = float(m1.GetAtomWithIdx(i).GetProp('_GasteigerCharge'))
            c2 = float(m2.GetAtomWithIdx(i).GetProp('_GasteigerCharge'))
            self.assertTrue(feq(c1, c2, 1e-4))

    def test3Params(self):
        """ tests handling of Issue187 """
        m2 = Chem.MolFromSmiles('C(=O)[O-].[Na+]')
        with self.assertRaisesRegex(Exception, ""):
            rdPartialCharges.ComputeGasteigerCharges(m2, 12, 1)

    def testGithubIssue20(self):
        """ tests handling of Github issue 20 """
        m1 = Chem.MolFromSmiles('CB(O)O')
        rdPartialCharges.ComputeGasteigerCharges(m1)
        chgs = [-0.030, 0.448, -0.427, -0.427]
        for i in range(m1.GetNumAtoms()):
            c1 = float(m1.GetAtomWithIdx(i).GetProp('_GasteigerCharge'))
            self.assertAlmostEqual(c1, chgs[i], 3)

    def testGithubIssue577(self):
        """ tests handling of Github issue 577 """
        m1 = Chem.MolFromSmiles('CCO')
        from locale import setlocale, LC_NUMERIC
        try:
            setlocale(LC_NUMERIC, "de_DE")
        except Exception:
            # can't set the required locale, might as well just return
            return
        try:
            rdPartialCharges.ComputeGasteigerCharges(m1)
            for at in m1.GetAtoms():
                float(at.GetProp('_GasteigerCharge'))
        finally:
            setlocale(LC_NUMERIC, "C")
        rdPartialCharges.ComputeGasteigerCharges(m1)
        for at in m1.GetAtoms():
            float(at.GetProp('_GasteigerCharge'))
    def testGithub2480(self):
        with self.assertRaisesRegex(Exception, "^Python argument types"):
            rdPartialCharges.ComputeGasteigerCharges(None)


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