a b/tests/test_datasets.py
1
import os
2
3
import pytest
4
5
import oddt
6
from oddt.datasets import pdbbind, dude
7
8
test_data_dir = os.path.dirname(os.path.abspath(__file__))
9
10
11
def test_pdbbind():
12
13
    results = {
14
        'core': (['4yef', '10gs'],
15
                 [5.35, 6.4]),
16
        'refined': (['1nlp', '1imx', '4yef', '10gs'],
17
                    [4.96, 3.52, 5.35, 6.4]),
18
        'general_PL': (['1k9q', '1nlo', '1nlp', '1imx', '4yef', '10gs'],
19
                       [3.15, 5.47, 4.96, 3.52, 5.35, 6.4]),
20
    }
21
22
    with pytest.raises(ValueError):
23
        pdbbind(home=os.path.join(test_data_dir, 'data', 'pdbbind'))
24
25
    for year in [2007, 2013, 2016]:
26
        pdbbind_db = pdbbind(home=os.path.join(test_data_dir, 'data', 'pdbbind'),
27
                             version=year, default_set='core')
28
29
        for set_name, (ids, activities) in results.items():
30
            if set_name == 'general_PL' and year == 2007:
31
                set_name = 'general'
32
            pdbbind_db.default_set = set_name
33
            assert pdbbind_db.ids == ids
34
            assert pdbbind_db.activities == activities
35
36
            for pid in pdbbind_db:
37
                assert isinstance(pid.pocket, oddt.toolkit.Molecule)
38
                assert len(pid.pocket.atoms) > 0
39
                assert isinstance(pid.ligand, oddt.toolkit.Molecule)
40
                assert len(pid.ligand.atoms) > 0
41
                if pid.id == '10gs':
42
                    assert pid.protein is None
43
                else:
44
                    assert isinstance(pid.protein, oddt.toolkit.Molecule)
45
                    assert len(pid.protein.atoms) > 0
46
47
        # reset the pdbbind set
48
        pdbbind_db.default_set = 'refined'
49
50
        # getting by name
51
        assert pdbbind_db['1imx'].id == '1imx'
52
53
        # getting by id
54
        assert pdbbind_db[-3].id == '1imx'
55
        assert pdbbind_db[1].id == '1imx'
56
57
        with pytest.raises(KeyError):
58
            pdbbind_db['xxxx']
59
        with pytest.raises(KeyError):
60
            pdbbind_db[123456]
61
        with pytest.raises(KeyError):
62
            pdbbind_db[-123456]
63
64
        pid = pdbbind_db['1imx']
65
        # get ligand
66
        ligand = pid.ligand
67
        ligand.removeh()
68
        assert len(ligand.atoms) == 60
69
70
        # get pocket
71
        pocket = pid.pocket
72
        pocket.removeh()
73
        assert len(pocket.atoms) == 234
74
75
        # protein do exist
76
        protein = pid.protein
77
        protein.removeh()
78
        assert len(protein.atoms) == 478
79
80
81
def test_dude():
82
    results = {
83
        'fabp4': (1022, 36, 57, 2855),
84
        'inha': (1857, 22, 71, 2318),
85
    }
86
87
    dude_db = dude(home=os.path.join(test_data_dir, 'data', 'dude'))
88
89
    for target in dude_db:
90
        if target.dude_id == 'xiap':
91
            # different file names
92
            assert target.protein is None
93
            assert target.ligand is None
94
            assert target.actives is None
95
            assert target.decoys is None
96
            continue
97
98
        prot_atoms, lig_atoms, num_act, num_dec = results[target.dude_id]
99
100
        prot = target.protein
101
        prot.removeh()
102
        assert len(prot.atoms) == prot_atoms
103
        lig = target.ligand
104
        lig.removeh()
105
        assert len(lig.atoms) == lig_atoms
106
107
        assert len(list(target.actives)) == num_act
108
        for a in target.actives:
109
            assert len(a.atoms) > 0
110
        assert len(list(target.decoys)) == num_dec
111
        for d in target.decoys:
112
            assert len(d.atoms) > 0
113
114
        with pytest.raises(KeyError):
115
            dude_db['xxxx']