File: test_convenient_superpose.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 (235 lines) | stat: -rw-r--r-- 11,285 bytes parent folder | download | duplicates (4)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from ost import io,mol,geom
import unittest
import os
import random

class TestConvenientSuperpose(unittest.TestCase):
  
  def setUp(self):
    pass
  
  def assertEqualAtomOrder(self, view1, view2):
    self.assertEqual(len(view1.atoms),len(view2.atoms))
    for a1, a2 in zip(view1.atoms, view2.atoms):
      self.assertEqual(a1.element, a2.element)
      self.assertTrue(geom.Equal(a1.pos, a2.pos))

  def testAssertion(self):
    ent1_ent = io.LoadEntity(os.path.join("testfiles","1aho.pdb"))
    ent1_view = ent1_ent.Select("")
    self.assertEqualAtomOrder(ent1_ent, ent1_ent)
    self.assertEqualAtomOrder(ent1_ent, ent1_view)
    self.assertEqualAtomOrder(ent1_view, ent1_ent)
    self.assertEqualAtomOrder(ent1_view, ent1_view)
    self.assertRaises(AssertionError, self.assertEqualAtomOrder, ent1_view.Select("ele!=H"), ent1_view)
    
  def testCorrectlyOrdered(self):
    ent1_ent = io.LoadEntity(os.path.join("testfiles","1aho.pdb"))
    ent1_view = ent1_ent.Select("")
    ## test MatchResidueByNum
    view1, view2 = mol.alg.MatchResidueByNum(ent1_ent, ent1_ent)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByNum(ent1_view, ent1_ent)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByNum(ent1_ent, ent1_view)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByNum(ent1_view, ent1_view)
    self.assertEqualAtomOrder(view1, view2)
    ## test MatchResidueByIdx
    view1, view2 = mol.alg.MatchResidueByIdx(ent1_ent, ent1_ent)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByIdx(ent1_view, ent1_ent)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByIdx(ent1_ent, ent1_view)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByIdx(ent1_view, ent1_view)
    self.assertEqualAtomOrder(view1, view2)
    ## test MatchResidueByGlobalAln
    view1, view2 = mol.alg.MatchResidueByGlobalAln(ent1_ent, ent1_ent)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByGlobalAln(ent1_view, ent1_ent)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByGlobalAln(ent1_ent, ent1_view)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByGlobalAln(ent1_view, ent1_view)
    self.assertEqualAtomOrder(view1, view2)
    ## test MatchResidueByLocalAln
    view1, view2 = mol.alg.MatchResidueByLocalAln(ent1_ent, ent1_ent)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByLocalAln(ent1_view, ent1_ent)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByLocalAln(ent1_ent, ent1_view)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByLocalAln(ent1_view, ent1_view)
    self.assertEqualAtomOrder(view1, view2)

  def testMissingFirstAtom(self):
    ent_view = io.LoadEntity(os.path.join("testfiles","1aho.pdb")).Select("")
    ent_view_missing = ent_view.Select("not (cname=A and rnum=1 and aname=N)")
    ## test MatchResidueByNum
    view1, view2 = mol.alg.MatchResidueByNum(ent_view, ent_view_missing)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByNum(ent_view_missing, ent_view)
    self.assertEqualAtomOrder(view1, view2)
    ## test MatchResidueByIdx
    view1, view2 = mol.alg.MatchResidueByIdx(ent_view, ent_view_missing)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByIdx(ent_view_missing, ent_view)
    self.assertEqualAtomOrder(view1, view2)
    ## test MatchResidueByGlobalAln
    view1, view2 = mol.alg.MatchResidueByGlobalAln(ent_view, ent_view_missing)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByGlobalAln(ent_view_missing, ent_view)
    self.assertEqualAtomOrder(view1, view2)
    ## test MatchResidueByLocalAln
    view1, view2 = mol.alg.MatchResidueByLocalAln(ent_view, ent_view_missing)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByLocalAln(ent_view_missing, ent_view)
    self.assertEqualAtomOrder(view1, view2)

  def testMissingManyAtoms(self):
    ent_view = io.LoadEntity(os.path.join("testfiles","1aho.pdb")).Select("")
    ent_view_missing = ent_view.Select("not (cname=A and rnum=3,19,32 and aname=CB,CA,CD)")
    ## test MatchResidueByNum
    view1, view2 = mol.alg.MatchResidueByNum(ent_view, ent_view_missing)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByNum(ent_view_missing, ent_view)
    self.assertEqualAtomOrder(view1, view2)
    ## test MatchResidueByIdx
    view1, view2 = mol.alg.MatchResidueByIdx(ent_view, ent_view_missing)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByIdx(ent_view_missing, ent_view)
    self.assertEqualAtomOrder(view1, view2)
    ## test MatchResidueByGlobalAln
    view1, view2 = mol.alg.MatchResidueByGlobalAln(ent_view, ent_view_missing)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByGlobalAln(ent_view_missing, ent_view)
    self.assertEqualAtomOrder(view1, view2)
    ## test MatchResidueByLocalAln
    view1, view2 = mol.alg.MatchResidueByLocalAln(ent_view, ent_view_missing)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByLocalAln(ent_view_missing, ent_view)
    self.assertEqualAtomOrder(view1, view2)

  def testMissingFirstResidue(self):
    ent_view = io.LoadEntity(os.path.join("testfiles","1aho.pdb")).Select("")
    ent_view_missing = ent_view.Select("not (cname=A and rnum=1)")
    ## test MatchResidueByNum
    view1, view2 = mol.alg.MatchResidueByNum(ent_view, ent_view_missing)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByNum(ent_view_missing, ent_view)
    self.assertEqualAtomOrder(view1, view2)
    ## test MatchResidueByGlobalAln
    view1, view2 = mol.alg.MatchResidueByGlobalAln(ent_view, ent_view_missing)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByGlobalAln(ent_view_missing, ent_view)
    self.assertEqualAtomOrder(view1, view2)
    ## test MatchResidueByLocalAln
    view1, view2 = mol.alg.MatchResidueByLocalAln(ent_view, ent_view_missing)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByLocalAln(ent_view_missing, ent_view)
    self.assertEqualAtomOrder(view1, view2)

  def testMissingHydrogens(self):
    ent_view = io.LoadEntity(os.path.join("testfiles","1aho.pdb")).Select("")
    ent_view_missing = ent_view.Select("ele!=H")
    ## test MatchResidueByNum
    view1, view2 = mol.alg.MatchResidueByNum(ent_view, ent_view_missing)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByNum(ent_view_missing, ent_view)
    self.assertEqualAtomOrder(view1, view2)
    ## test MatchResidueByIdx
    view1, view2 = mol.alg.MatchResidueByIdx(ent_view, ent_view_missing)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByIdx(ent_view_missing, ent_view)
    self.assertEqualAtomOrder(view1, view2)
  
  def testWrongAtomOrder(self):
    ent_view = io.LoadEntity(os.path.join("testfiles","1aho.pdb")).Select("")
    ent_view_wrong = ent_view.CreateEmptyView()
    for c in ent_view.chains:
      ent_view_wrong.AddChain(c)
      for r in c.residues:
        ent_view_wrong.AddResidue(r)
        atoms = list(r.atoms)
        random.shuffle(atoms)
        for a in atoms:
          ent_view_wrong.AddAtom(a)
    ## test MatchResidueByNum
    view1, view2 = mol.alg.MatchResidueByNum(ent_view, ent_view_wrong)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByNum(ent_view_wrong, ent_view)
    self.assertEqualAtomOrder(view1, view2)
    ## test MatchResidueByIdx
    view1, view2 = mol.alg.MatchResidueByIdx(ent_view, ent_view_wrong)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByIdx(ent_view_wrong, ent_view)
    self.assertEqualAtomOrder(view1, view2)
    ## test MatchResidueByGlobalAln
    view1, view2 = mol.alg.MatchResidueByGlobalAln(ent_view, ent_view_wrong)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByGlobalAln(ent_view_wrong, ent_view)
    self.assertEqualAtomOrder(view1, view2)
    ## test MatchResidueByLocalAln
    view1, view2 = mol.alg.MatchResidueByLocalAln(ent_view, ent_view_wrong)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByLocalAln(ent_view_wrong, ent_view)
    self.assertEqualAtomOrder(view1, view2)

  def testWrongResidueOrder(self):
    ent_view = io.LoadEntity(os.path.join("testfiles","1aho.pdb")).Select("")
    ent_view_wrong = ent_view.CreateEmptyView()
    for c in ent_view.chains:
      ent_view_wrong.AddChain(c)
      residues = list(c.residues)
      random.shuffle(residues)
      for r in residues:
        ent_view_wrong.AddResidue(r)
        for a in r.atoms:
          av=ent_view_wrong.AddAtom(a)
    ## test MatchResidueByNum
    view1, view2 = mol.alg.MatchResidueByNum(ent_view, ent_view_wrong)
    self.assertEqualAtomOrder(view1, view2)
    view1, view2 = mol.alg.MatchResidueByNum(ent_view_wrong, ent_view)
    self.assertEqualAtomOrder(view1, view2)

  def testPeptideFilter(self):
    # check that CA only chains are ok and ligand chains are skipped
    ent_1_full = io.LoadPDB(os.path.join("testfiles", "5tglA.pdb"))
    ent_2_full = io.LoadPDB(os.path.join("testfiles", "5tglA_modified.pdb"))
    exp_atom_count = 253
    # check CA-only chain
    ent_1 = ent_1_full.Select("cname=A")
    ent_2 = ent_2_full.Select("cname=A")
    view1, view2 = mol.alg.MatchResidueByLocalAln(ent_1, ent_2)
    self.assertEqual(view1.atom_count, exp_atom_count)
    self.assertEqual(view2.atom_count, exp_atom_count)
    view1, view2 = mol.alg.MatchResidueByGlobalAln(ent_1, ent_2)
    self.assertEqual(view1.atom_count, exp_atom_count)
    self.assertEqual(view2.atom_count, exp_atom_count)
    # check ligand chain
    ent_1_ligand = ent_1_full.Select("cname='_'")
    ent_2_ligand = ent_2_full.Select("cname='_'")
    view1, view2 = mol.alg.MatchResidueByLocalAln(ent_1_ligand, ent_2_ligand)
    self.assertEqual(view1.atom_count, 0)
    self.assertEqual(view2.atom_count, 0)
    view1, view2 = mol.alg.MatchResidueByGlobalAln(ent_1_ligand, ent_2_ligand)
    self.assertEqual(view1.atom_count, 0)
    self.assertEqual(view2.atom_count, 0)
    # check both together
    view1, view2 = mol.alg.MatchResidueByLocalAln(ent_1_full, ent_2_full)
    self.assertEqual(view1.atom_count, exp_atom_count)
    self.assertEqual(view2.atom_count, exp_atom_count)
    view1, view2 = mol.alg.MatchResidueByGlobalAln(ent_1_full, ent_2_full)
    self.assertEqual(view1.atom_count, exp_atom_count)
    self.assertEqual(view2.atom_count, exp_atom_count)
    # try case where local alignment fails
    ev1 = ent_1.Select('rindex<2')   # seq = GI
    ev2 = ent_2.Select('rindex=2:3') # seq = RA
    view1, view2 = mol.alg.MatchResidueByLocalAln(ev1, ev2)
    self.assertEqual(view1.atom_count, 0)
    self.assertEqual(view2.atom_count, 0)

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