File: test_modelling.py

package info (click to toggle)
promod3 3.2.1%2Bds-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,033,844 kB
  • sloc: cpp: 55,507; python: 17,487; makefile: 84; sh: 51
file content (570 lines) | stat: -rw-r--r-- 27,044 bytes parent folder | download
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# Copyright (c) 2013-2020, SIB - Swiss Institute of Bioinformatics and
#                          Biozentrum - University of Basel
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""
Unit tests for modelling.
"""
import unittest
from promod3 import modelling
from ost import conop, seq, io, mol, geom

class ModellingTests(unittest.TestCase):

    def setUp(self):
        compound_lib = conop.CompoundLib.Load(
            'data/compounds.chemlib')
        conop.SetDefaultLib(compound_lib)
        io.profiles['DEFAULT'].processor = conop.RuleBasedProcessor(
            compound_lib)

    #######################################################################
    # HELPERs
    #######################################################################
    def checkModel(self, mhandle):
        '''
        Check if model residues are properly set (peptide_linking, protein,
        torsion).
        '''
        # check residue properties
        for r in mhandle.model.residues:
            # check types
            self.assertTrue(r.peptide_linking)
            self.assertTrue(r.is_protein)
            # check links
            if r.next.IsValid() and (r.next.number - r.number) == 1:
                self.assertTrue(mol.InSequence(r, r.next))
            # check torsions
            if r.prev.IsValid() and mol.InSequence(r.prev, r):
                self.assertTrue(r.phi_torsion.IsValid())
                self.assertTrue(r.omega_torsion.IsValid())
            if r.next.IsValid() and mol.InSequence(r, r.next):
                self.assertTrue(r.psi_torsion.IsValid())
    #######################################################################

    def testRaiseNoAttachedView(self):
        # test that BuildRawModel throws exception when no view is attached
        aln = seq.CreateAlignment(seq.CreateSequence('A', 'acdef'),
                                  seq.CreateSequence('B', 'ac-ef'))
        self.assertRaises(RuntimeError, modelling.BuildRawModel, aln)

    def testRaiseInvalidChainNames(self):
        tpl = io.LoadPDB('data/gly.pdb')
        aln = io.LoadAlignment('data/seq.fasta')
        aln.AttachView(1, tpl.CreateFullView())

        # perform stuff that doesn't raise, i.e. do it correctly
        result = modelling.BuildRawModel(aln)
        result = modelling.BuildRawModel(aln, chain_names='cheese')
        self.assertEqual(result.model.chains[0].GetName(), 'cheese')
        aln_lst  = seq.AlignmentList()
        aln_lst.append(aln)
        aln_lst.append(aln)
        result = modelling.BuildRawModel(aln_lst)
        result = modelling.BuildRawModel(aln_lst, ['cheese', 'steak'])
        self.assertEqual(result.model.chains[0].GetName(), 'cheese')
        self.assertEqual(result.model.chains[1].GetName(), 'steak')
        result = modelling.BuildRawModel(aln_lst, 'ch')
        self.assertEqual(result.model.chains[0].GetName(), 'c')
        self.assertEqual(result.model.chains[1].GetName(), 'h')
        result = modelling.BuildRawModel(aln_lst, 'cheese')
        self.assertEqual(result.model.chains[0].GetName(), 'c')
        self.assertEqual(result.model.chains[1].GetName(), 'h')

        # we only accept a string as chain_names if aln is AlignmentHandle
        self.assertRaises(RuntimeError, modelling.BuildRawModel, aln, 1)
        self.assertRaises(RuntimeError, modelling.BuildRawModel, aln, ['A'])

        # we only accept a list or str as chain_names if aln is AlignmentList
        self.assertRaises(RuntimeError, modelling.BuildRawModel, aln_lst, 1)
        # size also matters...
        self.assertRaises(RuntimeError, modelling.BuildRawModel, aln_lst, ['A'])
        self.assertRaises(RuntimeError, modelling.BuildRawModel, aln_lst, 'A')

        # increase size of aln_list => at some point we should run out of 
        # default chain_names
        aln_lst = 100*[aln]
        self.assertRaises(RuntimeError, modelling.BuildRawModel, aln_lst)

    def testModeledSequence(self):
        # test if the model has the sequence we want.
        tpl = io.LoadPDB('data/gly.pdb')
        aln = io.LoadAlignment('data/seq.fasta')
        aln.AttachView(1, tpl.CreateFullView())
        result = modelling.BuildRawModel(aln)
        seq1 = seq.SequenceFromChain('MODEL', result.model.chains[0])
        self.assertEqual(len(result.gaps), 0)
        self.assertEqual(seq1.string, aln.sequences[0].string)

    def testDeletion(self):
        # test if the result contains a "deletion" gap at the right spot.
        tpl = io.LoadPDB('data/gly.pdb')
        aln = io.LoadAlignment('data/del.fasta')
        aln.AttachView(1, tpl.CreateFullView())
        # disable aln_preprocessing to keep the deletion in the example aln
        result = modelling.BuildRawModel(aln, aln_preprocessing=False)
        residues = result.model.residues
        self.assertEqual(len(result.gaps), 1)
        self.assertEqual(result.gaps[0].before, residues[2])
        self.assertEqual(result.gaps[0].after, residues[3])
        self.assertEqual(result.gaps[0].seq, '')

    def testInsertion(self):
        # test if the result contains an "insertion" gap at the right spot.
        tpl = io.LoadPDB('data/gly.pdb')
        aln = io.LoadAlignment('data/ins.fasta')
        aln.AttachView(1, tpl.CreateFullView())
        result = modelling.BuildRawModel(aln)
        residues = result.model.residues
        self.assertEqual(len(result.gaps), 1)
        self.assertEqual(result.gaps[0].before, residues[1])
        self.assertEqual(result.gaps[0].after, residues[2])
        self.assertEqual(result.gaps[0].seq, 'AV')

    def testNonFeasiblePeptideBond(self):
        # test if the result contains a gap when two residues cannot be 
        # connected with a feasible peptide bond
        tpl = io.LoadPDB('data/gly_shifted_nter.pdb')
        aln = io.LoadAlignment('data/seq.fasta')
        aln.AttachView(1, tpl.CreateFullView())
        result = modelling.BuildRawModel(aln)
        residues = result.model.residues
        self.assertEqual(len(result.gaps), 1)
        self.assertEqual(result.gaps[0].before, residues[1])
        self.assertEqual(result.gaps[0].after, residues[2])
        self.assertEqual(result.gaps[0].seq, '')

    def testTer(self):
        # test if the result contains two terminal gaps, one at the beginning,
        # one at the end
        tpl = io.LoadPDB('data/gly.pdb')
        aln = io.LoadAlignment('data/ter.fasta')
        aln.AttachView(1, tpl.CreateFullView())
        result = modelling.BuildRawModel(aln)
        residues = result.model.residues
        self.assertEqual(len(result.gaps), 2)
        self.assertEqual(result.gaps[0].before, mol.ResidueHandle())
        self.assertEqual(result.gaps[0].after, residues[0])
        self.assertEqual(result.gaps[0].seq, 'G')
        self.assertEqual(result.gaps[1].before, residues[-1])
        self.assertEqual(result.gaps[1].after, mol.ResidueHandle())
        self.assertEqual(result.gaps[1].seq, 'G')

    def testTerTrg(self):
        # test if raw model ignores terminal gaps in target sequence
        tpl = io.LoadPDB('data/gly.pdb')
        aln = seq.CreateAlignment(
            seq.CreateSequence('trg', '--GG--'),
            seq.CreateSequence('tpl', 'GGGGGG'))
        aln.AttachView(1, tpl.CreateFullView())
        result = modelling.BuildRawModel(aln)
        self.assertEqual(len(result.gaps), 0)

    def testModified(self):
        # test if we correctly strip off modifications
        tpl = io.LoadPDB('data/sep.pdb')
        aln = io.LoadAlignment('data/sep.fasta')
        aln.AttachView(1, tpl.CreateFullView())
        result = modelling.BuildRawModel(aln)
        residues = result.model.residues
        self.assertEqual(len(residues), 1)
        self.assertEqual(len(residues[0].atoms), 6)
        self.assertTrue(residues[0].FindAtom("N"))
        self.assertTrue(residues[0].FindAtom("CA"))
        self.assertTrue(residues[0].FindAtom("C"))
        self.assertTrue(residues[0].FindAtom("O"))
        self.assertTrue(residues[0].FindAtom("CB"))
        self.assertTrue(residues[0].FindAtom("OG"))

    def testModifiedMismatch(self):
        # test if we allow OLC mismatch for modified AA
        tpl = io.LoadPDB('data/sep.pdb')
        aln = seq.CreateAlignment(
            seq.CreateSequence('trg', 'S'),
            seq.CreateSequence('tpl', 'X'))
        aln.AttachView(1, tpl.CreateFullView())
        result = modelling.BuildRawModel(aln)
        residues = result.model.residues
        # same as before as OLC of SEP is 'S' (i.e. matches)
        self.assertEqual(len(residues), 1)
        self.assertEqual(len(residues[0].atoms), 6)
        self.assertTrue(residues[0].FindAtom("N"))
        self.assertTrue(residues[0].FindAtom("CA"))
        self.assertTrue(residues[0].FindAtom("C"))
        self.assertTrue(residues[0].FindAtom("O"))
        self.assertTrue(residues[0].FindAtom("CB"))
        self.assertTrue(residues[0].FindAtom("OG"))
        # NOTE: relevant seq-vs-str mismatch tested in testOffset
        # See OST's nonstandard.cc for additional tests of handling modified
        # residues. Code duplication will be removed in SCHWED-3569.

    def testInsertCBeta(self):
        # test if the dst residues contain cbeta, unless they are glycines
        tpl = io.LoadPDB('data/cbeta.pdb')
        aln = io.LoadAlignment('data/cbeta.fasta')
        aln.AttachView(1, tpl.CreateFullView())
        result = modelling.BuildRawModel(aln)
        residues = result.model.residues
        self.assertFalse(residues[0].FindAtom("CB").IsValid())
        self.assertFalse(residues[1].FindAtom("CB").IsValid())
        self.assertTrue(residues[2].FindAtom("CB").IsValid())
        self.assertTrue(residues[3].FindAtom("CB").IsValid())

    def testOffset(self):
        # test if we can construct a raw model with an offset
        tpl = io.LoadPDB('data/2jlp-1.pdb')
        aln = io.LoadAlignment('data/2jlp-1.fasta')
        aln.AttachView(1, tpl.Select("cname=A").CreateFullView())
        # fail w/o offset
        with self.assertRaises(RuntimeError):
            mhandle = modelling.BuildRawModel(aln)
        # try with hard-coded offset
        aln.SetSequenceOffset(1, 55)
        mhandle = modelling.BuildRawModel(aln)
        self.assertEqual(len(mhandle.gaps), 4)

    def testOnTop(self):
        # do we clean up atoms that are on top of each other?
        # ResNum 8 has CA-pos == C-pos
        tpl = io.LoadPDB('data/gly_on_top.pdb')
        aln = io.LoadAlignment('data/seq.fasta')
        aln.AttachView(1, tpl.CreateFullView())
        result = modelling.BuildRawModel(aln)
        self.assertEqual(len(result.gaps), 1)
        self.assertEqual(result.gaps[0].before.number.num, 7)
        self.assertEqual(result.gaps[0].after.number.num, 9)

    #######################################################################

    def testModellingHandleOperations(self):
        # handle with gap
        tpl = io.LoadPDB('data/1crn_cut.pdb')
        aln = io.LoadAlignment('data/1crn.fasta')
        aln.AttachView(1, tpl.CreateFullView())
        mhandle = modelling.BuildRawModel(aln)

        # check ModellingHandle
        self.assertEqual(len(mhandle.gaps), 1)
        self.assertEqual(str(mhandle.gaps[0]), 'A.ALA24-(ICATYT)-A.GLY31')
        self.checkModel(mhandle)

        # check copy
        mhandle_copy = mhandle.Copy()
        self.assertEqual([str(a) for a in mhandle_copy.model.atoms],
                         [str(a) for a in mhandle.model.atoms])
        self.assertEqual([str(g) for g in mhandle_copy.gaps],
                         [str(g) for g in mhandle.gaps])
        self.assertEqual([str(s) for s in mhandle_copy.seqres],
                         [str(s) for s in mhandle.seqres])
        # rest of the fields not checked...yet

        # handle without gap
        tpl_closed = io.LoadPDB('data/1crn_build.pdb')
        seqres = str(aln.sequences[0])
        aln_closed = seq.CreateAlignment(seq.CreateSequence('trg', seqres),
                                         seq.CreateSequence('tpl', seqres))
        aln_closed.AttachView(1, tpl_closed.CreateFullView())
        mhandle_closed = modelling.BuildRawModel(aln_closed)

        # merge with no overlap (no change)
        mhandle_copy = mhandle.Copy()
        modelling.MergeMHandle(mhandle_closed, mhandle_copy, 0, 0, 10, 19,
                               geom.Mat4())
        self.assertEqual([str(a) for a in mhandle_copy.model.atoms],
                         [str(a) for a in mhandle.model.atoms])
        self.assertEqual([str(g) for g in mhandle_copy.gaps],
                         [str(g) for g in mhandle.gaps])
        self.assertEqual([str(s) for s in mhandle_copy.seqres],
                         [str(s) for s in mhandle.seqres])
        self.checkModel(mhandle_copy)

        # merge with overlap on N-side
        mhandle_copy = mhandle.Copy()
        modelling.MergeMHandle(mhandle_closed, mhandle_copy, 0, 0, 20, 29,
                               geom.Mat4())
        self.assertEqual(len(mhandle_copy.gaps), 1)
        self.assertEqual(str(mhandle_copy.gaps[0]), 'A.TYR29-(T)-A.GLY31')
        self.checkModel(mhandle_copy)

        # merge with overlap on C-side
        mhandle_copy = mhandle.Copy()
        modelling.MergeMHandle(mhandle_closed, mhandle_copy, 0, 0, 27, 35,
                               geom.Mat4())
        self.assertEqual(len(mhandle_copy.gaps), 1)
        self.assertEqual(str(mhandle_copy.gaps[0]), 'A.ALA24-(IC)-A.ALA27')
        self.checkModel(mhandle_copy)

        # merge with full overlap
        mhandle_copy = mhandle.Copy()
        modelling.MergeMHandle(mhandle_closed, mhandle_copy, 0, 0, 20, 35,
                               geom.Mat4())
        self.assertEqual(len(mhandle_copy.gaps), 0)
        self.checkModel(mhandle_copy)

    #######################################################################

    def testMergeGaps(self):
        tpl = io.LoadPDB('data/1mcg.pdb')
        aln = seq.CreateAlignment(seq.CreateSequence('trg', 'DDFAGTHN'),
                                  seq.CreateSequence('tpl', 'N-N-A-LF'))
        aln.AttachView(1, tpl.CreateFullView())
        mhandle = modelling.BuildRawModel(aln)
        # check
        seqres = ''.join([r.one_letter_code for r in mhandle.model.residues])
        self.assertEqual(seqres, 'DFGHN')
        self.assertEqual(len(mhandle.gaps), 3)
        modelling.MergeGaps(mhandle, 0)
        seqres = ''.join([r.one_letter_code for r in mhandle.model.residues])
        self.assertEqual(seqres, 'DGHN')
        self.assertEqual(len(mhandle.gaps), 2)
        self.assertEqual(str(mhandle.gaps[0]), 'A.ASP1-(DFA)-A.GLY5')
        modelling.MergeGaps(mhandle, 0)
        seqres = ''.join([r.one_letter_code for r in mhandle.model.residues])
        self.assertEqual(seqres, 'DHN')
        self.assertEqual(len(mhandle.gaps), 1)
        self.assertEqual(str(mhandle.gaps[0]), 'A.ASP1-(DFAGT)-A.HIS7')
        # last gap: should throw exception
        with self.assertRaises(RuntimeError):
            modelling.MergeGaps(mhandle, 0)

    def testMergeGapsOligo(self):
        # check that merge gaps fails if on diff. chains
        tpl = io.LoadPDB('data/5d52-1.pdb')
        aln_A = seq.CreateAlignment(
            seq.CreateSequence('trg', 'GIVEQAAACCTSICSLYQLENYCN'),
            seq.CreateSequence('tpl', 'GIVEQ---CCTSICSLYQLENYCN'))
        aln_B = seq.CreateAlignment(
            seq.CreateSequence('trg', 'FVNQHLCG---LEALTLVCGERGFFYTPKA'),
            seq.CreateSequence('tpl', 'FVNQHLCGSHLVEALYLVCGERGFFYTPKA'))
        aln_A.AttachView(1, tpl.Select("cname=A").CreateFullView())
        aln_B.AttachView(1, tpl.Select("cname=B").CreateFullView())
        alns = seq.AlignmentList()
        alns.append(aln_A)
        alns.append(aln_B)
        mhandle = modelling.BuildRawModel(alns)
        # check
        self.assertEqual(len(mhandle.gaps), 2)
        with self.assertRaises(RuntimeError):
            modelling.MergeGaps(mhandle, 0)

    def testCountEnclosedGaps(self):
        tpl = io.LoadPDB('data/2dbs.pdb')
        aln = seq.CreateAlignment(
            seq.CreateSequence('trg', 'GATLNGFTVPAGNTLVLN---PD--KG---ATVTMAGA'),
            seq.CreateSequence('tpl', '--NGG--TL--LI--PNGTYHFLGIQMKSNVHIRVE--'))
        aln.AttachView(1, tpl.CreateFullView())
        mhandle = modelling.BuildRawModel(aln)
        # check
        self.assertEqual(len(mhandle.gaps), 8)
        for g in mhandle.gaps:
          self.assertEqual(modelling.CountEnclosedGaps(mhandle, g), 1)
          self.assertEqual(modelling.CountEnclosedInsertions(mhandle, g),
                           1 if (g.length > 0) else 0)

        mych = mhandle.model.chains[0]
        # none
        mygap = modelling.StructuralGap(mych.FindResidue(15),
                                        mych.FindResidue(17), "L")
        self.assertEqual(modelling.CountEnclosedGaps(mhandle, mygap), 0)
        self.assertEqual(modelling.CountEnclosedInsertions(mhandle, mygap), 0)
        # extended singles
        mygap = modelling.StructuralGap(mych.FindResidue(3),
                                        mych.FindResidue(9), "LNGFT")
        self.assertEqual(modelling.CountEnclosedGaps(mhandle, mygap), 1)
        self.assertEqual(modelling.CountEnclosedInsertions(mhandle, mygap), 1)
        mygap = modelling.StructuralGap(mych.FindResidue(20),
                                        mych.FindResidue(22), "K")
        self.assertEqual(modelling.CountEnclosedGaps(mhandle, mygap), 1)
        self.assertEqual(modelling.CountEnclosedInsertions(mhandle, mygap), 0)
        # doubles
        mygap = modelling.StructuralGap(mych.FindResidue(3),
                                        mych.FindResidue(12), "LNGFTVPA")
        self.assertEqual(modelling.CountEnclosedGaps(mhandle, mygap), 2)
        self.assertEqual(modelling.CountEnclosedInsertions(mhandle, mygap), 2)
        mygap = modelling.StructuralGap(mych.FindResidue(13),
                                        mych.FindResidue(19), "TLVLN")
        self.assertEqual(modelling.CountEnclosedGaps(mhandle, mygap), 2)
        self.assertEqual(modelling.CountEnclosedInsertions(mhandle, mygap), 1)
        mygap = modelling.StructuralGap(mych.FindResidue(20),
                                        mych.FindResidue(25), "KGAT")
        self.assertEqual(modelling.CountEnclosedGaps(mhandle, mygap), 2)
        self.assertEqual(modelling.CountEnclosedInsertions(mhandle, mygap), 0)

    #######################################################################

    def testClearGapsExceptions(self):
        # check that clear gaps throws exceptions when used wrongly
        tpl = io.LoadPDB('data/1mcg.pdb')
        aln = seq.CreateAlignment(seq.CreateSequence('trg', 'DDFAGTHN'),
                                  seq.CreateSequence('tpl', 'N-N-A-LF'))
        aln.AttachView(1, tpl.CreateFullView())
        mhandle = modelling.BuildRawModel(aln)
        # get gaps before we remove residues in MergeGaps
        mych = mhandle.model.chains[0]
        mygap = modelling.StructuralGap(mych.FindResidue(0),
                                        mych.FindResidue(3), "DD")
        mygap2 = modelling.StructuralGap(mych.FindResidue(3),
                                         mych.FindResidue(9), "AGTHN")
        mygap3 = modelling.StructuralGap(mych.FindResidue(3),
                                         mych.FindResidue(5), "A")
        # check
        modelling.MergeGaps(mhandle, 0)
        modelling.MergeGaps(mhandle, 0)
        self.assertEqual(len(mhandle.gaps), 1)
        with self.assertRaises(RuntimeError):
            modelling.ClearGaps(mhandle, mygap)
        with self.assertRaises(RuntimeError):
            modelling.ClearGaps(mhandle, mygap2)
        with self.assertRaises(RuntimeError):
            modelling.ClearGaps(mhandle, mygap3)
        self.assertEqual(len(mhandle.gaps), 1)

    def testClearGaps(self):
        tpl = io.LoadPDB('data/2dbs.pdb')
        aln = seq.CreateAlignment(
            seq.CreateSequence('trg', 'TLNGFTVPAGNTLVLN---PDKG--ATVTM-A'),
            seq.CreateSequence('tpl', 'N-GG-TLLI--PNGTYHFLGIQMKSNVHIRVE'))
        aln.AttachView(1, tpl.CreateFullView())
        # disable aln_preprocessing to also keep the small deletion in the end
        mhandle = modelling.BuildRawModel(aln, aln_preprocessing=False)
        # check
        self.assertEqual(len(mhandle.gaps), 6)
        self.assertEqual(modelling.ClearGaps(mhandle, mhandle.gaps[1]), 1)
        self.assertEqual(len(mhandle.gaps), 5)
        self.assertEqual(modelling.ClearGaps(mhandle, mhandle.gaps[2]), 2)
        self.assertEqual(len(mhandle.gaps), 4)
        # special gaps
        mych = mhandle.model.chains[0]
        mygap = modelling.StructuralGap(mych.FindResidue(8),
                                        mych.FindResidue(13), "AGNT")
        self.assertEqual(modelling.ClearGaps(mhandle, mygap), 1)
        self.assertEqual(len(mhandle.gaps), 3)
        mygap = modelling.StructuralGap(mych.FindResidue(19),
                                        mych.FindResidue(22), "GA")
        self.assertEqual(modelling.ClearGaps(mhandle, mygap), 1)
        self.assertEqual(len(mhandle.gaps), 2)
        mygap = modelling.StructuralGap(mych.FindResidue(25),
                                        mych.FindResidue(27), "A")
        self.assertEqual(modelling.ClearGaps(mhandle, mygap), -1)
        self.assertEqual(len(mhandle.gaps), 1)
        mygap = modelling.StructuralGap(mych.FindResidue(0),
                                        mych.FindResidue(3), "TL")
        self.assertEqual(modelling.ClearGaps(mhandle, mygap), -1)
        self.assertEqual(len(mhandle.gaps), 0)
        self.assertEqual(modelling.ClearGaps(mhandle, mygap), -1)

    def testClearMultipleGaps(self):
        # check that we can clear multiple gaps at once
        tpl = io.LoadPDB('data/2dbs.pdb')
        aln = seq.CreateAlignment(
            seq.CreateSequence('trg', 'TLNGFTVPAGNTLVLN---PD--KG---ATVTMA'),
            seq.CreateSequence('tpl', 'NGG--TL--LI--PNGTYHFLGIQMKSNVHIRVE'))
        aln.AttachView(1, tpl.CreateFullView())
        mhandle = modelling.BuildRawModel(aln)
        # check
        self.assertEqual(len(mhandle.gaps), 6)
        mych = mhandle.model.chains[0]
        mygap = modelling.StructuralGap(mych.FindResidue(1),
                                        mych.FindResidue(10), "LNGFTVPA")
        self.assertEqual(modelling.ClearGaps(mhandle, mygap), 0)
        self.assertEqual(len(mhandle.gaps), 4)
        mygap = modelling.StructuralGap(mych.FindResidue(11),
                                        mych.FindResidue(17), "TLVLN")
        self.assertEqual(modelling.ClearGaps(mhandle, mygap), 0)
        self.assertEqual(len(mhandle.gaps), 2)
        mygap = modelling.StructuralGap(mych.FindResidue(18),
                                        mych.FindResidue(23), "KGAT")
        self.assertEqual(modelling.ClearGaps(mhandle, mygap), -1)
        self.assertEqual(len(mhandle.gaps), 0)

    def testClearGapsTermnini(self):
        # check that we can clear terminal gaps
        tpl = io.LoadPDB('data/1mcg.pdb')
        aln = seq.CreateAlignment(seq.CreateSequence('trg', 'DDFAGTHN'),
                                  seq.CreateSequence('tpl', '--NNALF-'))
        aln.AttachView(1, tpl.CreateFullView())
        mhandle = modelling.BuildRawModel(aln)
        # check
        self.assertEqual(len(mhandle.gaps), 2)
        self.assertEqual(modelling.ClearGaps(mhandle, mhandle.gaps[0]), 0)
        self.assertEqual(len(mhandle.gaps), 1)
        self.assertEqual(modelling.ClearGaps(mhandle, mhandle.gaps[0]), -1)
        self.assertEqual(len(mhandle.gaps), 0)

    def testClearGapsOligo(self):
        # check that we can clear gaps in oligomers
        tpl = io.LoadPDB('data/5d52-1.pdb')
        aln_A = seq.CreateAlignment(
            seq.CreateSequence('trg', 'GIVEQPAYFWPPPAHCCTSICSLYQLENYCN'),
            seq.CreateSequence('tpl', 'GIVEQ----------CCTSICSLYQLENYCN'))
        aln_B = seq.CreateAlignment(
            seq.CreateSequence('trg', 'FVNQHLCGSGHLVEALYLVCGERGFFYTPKA'),
            seq.CreateSequence('tpl', 'FVNQHLCGS-HLVEALYLVCGERGFFYTPKA'))
        aln_A.AttachView(1, tpl.Select("cname=A").CreateFullView())
        aln_B.AttachView(1, tpl.Select("cname=B").CreateFullView())
        alns = seq.AlignmentList()
        alns.append(aln_A)
        alns.append(aln_B)
        mhandle = modelling.BuildRawModel(alns)
        # check
        self.assertEqual(len(mhandle.gaps), 2)
        self.assertEqual(modelling.ClearGaps(mhandle, mhandle.gaps[0]), 0)
        self.assertEqual(len(mhandle.gaps), 1)
        self.assertEqual(modelling.ClearGaps(mhandle, mhandle.gaps[0]), -1)
        self.assertEqual(len(mhandle.gaps), 0)

    #######################################################################

    def testRemoveTerminalGaps(self):
        # check that we can remove both terminal gaps
        tpl = io.LoadPDB('data/1mcg.pdb')
        aln = seq.CreateAlignment(seq.CreateSequence('trg', 'DDFAGTHN'),
                                  seq.CreateSequence('tpl', '--NNALF-'))
        aln.AttachView(1, tpl.CreateFullView())
        mhandle = modelling.BuildRawModel(aln)
        # check
        self.assertEqual(len(mhandle.gaps), 2)
        self.assertEqual(modelling.RemoveTerminalGaps(mhandle), 2)
        self.assertEqual(len(mhandle.gaps), 0)

    def testRemoveTerminalGapsN(self):
        # check that we can remove N-terminal gaps
        tpl = io.LoadPDB('data/1mcg.pdb')
        aln = seq.CreateAlignment(seq.CreateSequence('trg', 'DDFAGTH'),
                                  seq.CreateSequence('tpl', '--NNALF'))
        aln.AttachView(1, tpl.CreateFullView())
        mhandle = modelling.BuildRawModel(aln)
        # check
        self.assertEqual(len(mhandle.gaps), 1)
        self.assertEqual(modelling.RemoveTerminalGaps(mhandle), 1)
        self.assertEqual(len(mhandle.gaps), 0)

    def testRemoveTerminalGapsC(self):
        # check that we can remove C-terminal gaps
        tpl = io.LoadPDB('data/1mcg.pdb')
        aln = seq.CreateAlignment(seq.CreateSequence('trg', 'FAGTHN'),
                                  seq.CreateSequence('tpl', 'NNALF-'))
        aln.AttachView(1, tpl.CreateFullView())
        mhandle = modelling.BuildRawModel(aln)
        # check
        self.assertEqual(len(mhandle.gaps), 1)
        self.assertEqual(modelling.RemoveTerminalGaps(mhandle), 1)
        self.assertEqual(len(mhandle.gaps), 0)
        

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