File: test_complib.py

package info (click to toggle)
openstructure 2.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 206,240 kB
  • sloc: cpp: 188,571; python: 36,686; ansic: 34,298; fortran: 3,275; sh: 312; xml: 146; makefile: 29
file content (150 lines) | stat: -rw-r--r-- 6,135 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
140
141
142
143
144
145
146
147
148
149
150
import unittest, os, sys
import ost
from ost import conop
import subprocess
import tempfile
import warnings


def CreateComplib(compound_dict_path, chemlib_out_path, extra_args=None):
    prefix_path = ost.GetPrefixPath()
    chemdict_tool_path = os.path.join(prefix_path, "bin", "chemdict_tool")
    if not os.path.exists(chemdict_tool_path):
        raise RuntimeError("Expect chemdict_tool:", chemdict_tool_path)
    cmd = [chemdict_tool_path, "create", compound_dict_path, chemlib_out_path, "-q"]
    if extra_args:
        cmd += extra_args
    subprocess.run(cmd, stdout=subprocess.PIPE, check=True)


class TestCompLib(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.tmp_dir = tempfile.TemporaryDirectory()
        compound_dict_path = os.path.join("testfiles", "test_compounds.cif")
        complib_path = os.path.join(cls.tmp_dir.name, "test_complib.dat")
        CreateComplib(compound_dict_path, complib_path)
        cls.complib = conop.CompoundLib.Load(complib_path)

    @classmethod
    def tearDownClass(cls):
        cls.tmp_dir.cleanup()

    def test_three_vs_five_letter_code(self):
        complib = self.complib

        comp_001 = complib.FindCompound("001")
        comp_A1LU6 = complib.FindCompound("A1LU6")
        comp_yolo = complib.FindCompound("yolo")

        self.assertFalse(comp_001 is None)
        self.assertFalse(comp_A1LU6 is None)
        self.assertTrue(comp_yolo is None)

    def test_smiles(self):
        complib = self.complib
        comp_001 = complib.FindCompound("001")
        self.assertTrue(comp_001.smiles == "COc1cc(cc(c1OC)OC)C(C(=O)N2CCCC[C@H]2C(=O)O[C@@H](CCCc3ccccc3)CCCc4cccnc4)(F)F")

    def test_charges(self):
        complib = self.complib
        comp_nh4 = complib.FindCompound("NH4")
        self.assertTrue(comp_nh4.atom_specs[0].charge == 1)
        self.assertTrue(comp_nh4.atom_specs[1].charge == 0)

    def test_obsolete(self):
        complib = self.complib
        comp_ox = complib.FindCompound("OX")
        # First test that we do get data
        self.assertTrue(comp_ox.smiles == "O")
        # Now the obsolete part
        self.assertTrue(comp_ox.obsolete)
        self.assertTrue(comp_ox.replaced_by == "O")
        # Ensure not everything is obsolete
        comp_001 = complib.FindCompound("001")
        self.assertFalse(comp_001.obsolete)

    def test_default_lib_version(self):
        compound_lib = conop.GetDefaultLib()
        if compound_lib is None:
            warnings.warn("Compound library not available. Some functionality may not work as expected.")
        else:
            lib_version = compound_lib.GetOSTVersionUsed()
            if lib_version < ost.__version__:
                warnings.warn("Using old version of the compound library: %s" % lib_version)

    def test_ignore_reserved(self):
        compound_dict_path = os.path.join("testfiles", "test_compounds.cif")
        complib_no_reserved_path = os.path.join(self.tmp_dir.name, "test_complib_no_reserved.dat")
        CreateComplib(compound_dict_path, complib_no_reserved_path, ["-i"])
        complib_no_reserved = conop.CompoundLib.Load(complib_no_reserved_path)

        # 01-98 are reserved
        assert self.complib.FindCompound("98") is not None
        assert complib_no_reserved.FindCompound("98") is None

        # DRG, INH and LIG are reserved
        assert self.complib.FindCompound("DRG") is not None
        assert complib_no_reserved.FindCompound("DRG") is None
        assert self.complib.FindCompound("INH") is not None
        assert complib_no_reserved.FindCompound("INH") is None
        assert self.complib.FindCompound("LIG") is not None
        assert complib_no_reserved.FindCompound("LIG") is None

        # OX is obsolete but not reserved
        assert complib_no_reserved.FindCompound("OX") is not None

        # 00, 000, 001, 010, 986, 98B are not reserved
        assert complib_no_reserved.FindCompound("00") is not None
        assert complib_no_reserved.FindCompound("000") is not None
        assert complib_no_reserved.FindCompound("001") is not None
        assert complib_no_reserved.FindCompound("010") is not None
        assert complib_no_reserved.FindCompound("986") is not None
        assert complib_no_reserved.FindCompound("98B") is not None

    def test_ignore_obsolete(self):
        compound_dict_path = os.path.join("testfiles", "test_compounds.cif")
        complib_no_obsolete_path = os.path.join(self.tmp_dir.name, "test_complib_no_obsolete.dat")
        CreateComplib(compound_dict_path, complib_no_obsolete_path, ["-o"])
        complib_no_obsolete = conop.CompoundLib.Load(complib_no_obsolete_path)

        # 01-98, DRG, INH and LIG are reserved but not obsolete
        assert complib_no_obsolete.FindCompound("98") is not None
        assert complib_no_obsolete.FindCompound("DRG") is not None
        assert complib_no_obsolete.FindCompound("INH") is not None
        assert complib_no_obsolete.FindCompound("LIG") is not None

        # OX is obsolete
        assert complib_no_obsolete.FindCompound("OX") is None

    def test_bird(self):
        """ the test file in test_compounds.cif contains two entries from
          BIRD:

          - PRD_900116 is not in a compound format and should not be added
            to the compound lib.
          - PRDCC_900116 is in a valid format and should be read in
        """
        complib = self.complib
        # PRD_900116 not added
        comp = complib.FindCompound("PRD_900116")
        self.assertIsNone(comp)

        # PRD_900116 is added
        comp = complib.FindCompound("PRDCC_900116")
        self.assertIsNotNone(comp)
        self.assertEqual(
            comp.smiles,
            "C1[C@H]([C@@H]([C@H]([C@@H](O1)O[C@@H]2CO[C@H]([C@@H]([C@H]2O)O)O)O)O)O")
        # The release flag is REF_ONLY and compound should not be marked as
        # obsolete
        self.assertFalse(comp.obsolete)
        # Contains atoms and bonds
        self.assertEqual(len(comp.atom_specs), 37)
        self.assertEqual(len(comp.bond_specs), 38)


if __name__ == "__main__":
    from ost import testutils
    testutils.RunTests()