Diff of /tests/test_surface.py [000000] .. [3b722e]

Switch to unified view

a b/tests/test_surface.py
1
import os
2
from distutils.version import LooseVersion
3
4
import numpy as np
5
import pytest
6
from numpy.testing import assert_array_equal
7
from skimage import __version__ as skimage_version
8
9
import oddt
10
from oddt.surface import (generate_surface_marching_cubes,
11
                          find_surface_residues)
12
13
test_data_dir = os.path.dirname(os.path.abspath(__file__))
14
protein = next(oddt.toolkit.readfile('pdb', os.path.join(
15
    test_data_dir, 'data/dude/xiap/receptor_rdkit.pdb')))
16
protein.protein = True
17
protein.addh(only_polar=True)
18
19
20
def test_generate_surface_marching_cubes():
21
    """Tests generating surfaces"""
22
    verts1, faces1 = generate_surface_marching_cubes(protein, scaling=1., probe_radius=1.4, remove_hoh=False)
23
    verts2, faces2 = generate_surface_marching_cubes(protein, scaling=2., probe_radius=1.4, remove_hoh=False)
24
    verts3, faces3 = generate_surface_marching_cubes(protein, scaling=1., probe_radius=1.4, remove_hoh=True)
25
    verts4, faces4 = generate_surface_marching_cubes(protein, scaling=1., probe_radius=0, remove_hoh=True)
26
27
    # Higher scaling should result in a higher number of vertices
28
    assert len(verts2) > len(verts1), ('Higher scaling should result in '
29
                                       'a higher number of vertices')
30
31
    # versions of skimage older than 0.12 use a slightly different version of the marching cubes algorithm
32
    # producing slightly different results
33
    if LooseVersion(skimage_version) >= LooseVersion('0.13'):
34
        if (oddt.toolkit.backend == 'ob' or
35
                oddt.toolkit.backend == 'rdk' and oddt.toolkits.rdk.__version__ >= '2019.09'):
36
            ref_vert_shape_1 = (9040, 3)
37
            ref_face_shape_1 = (18094, 3)
38
            ref_vert_shape_2 = (35950, 3)
39
            ref_face_shape_2 = (71926, 3)
40
            ref_vert_shape_3 = (9040, 3)
41
            ref_face_shape_3 = (18094, 3)
42
            ref_vert_shape_4 = (14881, 3)
43
            ref_face_shape_4 = (30468, 3)
44
        else:
45
            ref_vert_shape_1 = (9044, 3)
46
            ref_face_shape_1 = (18102, 3)
47
            ref_vert_shape_2 = (35788, 3)
48
            ref_face_shape_2 = (71578, 3)
49
            ref_vert_shape_3 = (9044, 3)
50
            ref_face_shape_3 = (18102, 3)
51
            ref_vert_shape_4 = (15035, 3)
52
            ref_face_shape_4 = (30848, 3)
53
    else:
54
        if oddt.toolkit.backend == 'ob':
55
            ref_vert_shape_1 = (5923, 3)
56
            ref_face_shape_1 = (11862, 3)
57
            ref_vert_shape_2 = (20819, 3)
58
            ref_face_shape_2 = (41634, 3)
59
            ref_vert_shape_3 = (5923, 3)
60
            ref_face_shape_3 = (11862, 3)
61
            ref_vert_shape_4 = (10263, 3)
62
            ref_face_shape_4 = (21658, 3)
63
        else:
64
            ref_vert_shape_1 = (5916, 3)
65
            ref_face_shape_1 = (11848, 3)
66
            ref_vert_shape_2 = (20845, 3)
67
            ref_face_shape_2 = (41686, 3)
68
            ref_vert_shape_3 = (5916, 3)
69
            ref_face_shape_3 = (11848, 3)
70
            ref_vert_shape_4 = (10243, 3)
71
            ref_face_shape_4 = (21686, 3)
72
73
    assert ref_vert_shape_1 == verts1.shape
74
    assert ref_face_shape_1 == faces1.shape
75
76
    assert ref_vert_shape_2 == verts2.shape
77
    assert ref_face_shape_2 == faces2.shape
78
79
    assert ref_vert_shape_3 == verts3.shape
80
    assert ref_face_shape_3 == faces3.shape
81
82
    assert ref_vert_shape_4 == verts4.shape
83
    assert ref_face_shape_4 == faces4.shape
84
85
    with pytest.raises(TypeError):
86
        generate_surface_marching_cubes(molecule=1)
87
    with pytest.raises(ValueError):
88
        generate_surface_marching_cubes(molecule=protein, probe_radius=-1)
89
    with pytest.raises(ValueError):
90
        generate_surface_marching_cubes(molecule=protein, scaling=0.1)
91
92
93
def test_find_surface_residues():
94
    """Tests finding residues on the surface"""
95
    atom_dict_0 = find_surface_residues(protein, max_dist=0, scaling=1)
96
    atom_dict_1 = find_surface_residues(protein, max_dist=2, scaling=1)
97
    atom_dict_2 = find_surface_residues(protein, max_dist=3, scaling=1)
98
    atom_dict_3 = find_surface_residues(protein, max_dist=None, scaling=1)
99
    atom_dict_4 = find_surface_residues(protein, max_dist=None, scaling=2)
100
101
    assert atom_dict_0.size == 0
102
    assert len(atom_dict_1) > len(atom_dict_0), ('Increasing max_dist should '
103
                                                 'result in more/equal number '
104
                                                 'of atoms found')
105
106
    assert len(atom_dict_2) >= len(atom_dict_1), ('Increasing max_dist should '
107
                                                  'result in more/equal number '
108
                                                  'of atoms found')
109
    assert_array_equal(np.intersect1d(atom_dict_1['id'], atom_dict_2['id']), atom_dict_1['id'])
110
111
    if oddt.toolkit.backend == 'ob':
112
        ref_len_1 = 762
113
        ref_len_2 = 968
114
        ref_len_3 = 654
115
        ref_len_4 = 379
116
    elif oddt.toolkit.backend == 'rdk' and oddt.toolkits.rdk.__version__ >= '2019.09':
117
        ref_len_1 = 762
118
        ref_len_2 = 968
119
        ref_len_3 = 664
120
        ref_len_4 = 393
121
    else:
122
        ref_len_1 = 759
123
        ref_len_2 = 966
124
        ref_len_3 = 735
125
        ref_len_4 = 489
126
127
    assert len(atom_dict_1) == ref_len_1
128
    assert len(atom_dict_2) == ref_len_2
129
    assert len(atom_dict_3) == ref_len_3
130
    assert len(atom_dict_4) == ref_len_4
131
132
    # Adding hydrogen atoms should have no effect on the result
133
    protein.addh()
134
    atom_dict_addh = find_surface_residues(protein, max_dist=2, scaling=1)
135
    assert_array_equal(atom_dict_addh['id'], atom_dict_1['id'])
136
137
    with pytest.raises(TypeError):
138
        find_surface_residues(molecule=1)
139
    with pytest.raises(ValueError):
140
        find_surface_residues(molecule=protein, max_dist='a')
141
    with pytest.raises(ValueError):
142
        find_surface_residues(molecule=protein, max_dist=[1, 1, 1])