-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdp_util.py
160 lines (124 loc) · 4.44 KB
/
dp_util.py
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
# --------------------------------------------------------------------
# dp.py
#
# Miscelaneous utility classes
#
# history:
# 20090825 - 1.0.0 - JAC - first version
# --------------------------------------------------------------------
import os
import sys
from Bio.PDB import *
class Msg:
STDERR_QUIET = False
STDOUT_QUIET = False
def fatal( msg ):
sys.stderr.write( "Fatal Error!\n%s\n" %msg )
quit()
def out( msg, stderr=True ):
if( stderr ):
if( not Msg.STDERR_QUIET ):
sys.stderr.write( msg )
sys.stderr.flush()
else:
if( not Msg.STDOUT_QUIET ):
sys.stdout.write( msg )
def usage():
cmd = os.path.basename(sys.argv[0])
sys.stderr.write( "\n-------------------\n" )
sys.stderr.write( "Deformation Profile\n" )
sys.stderr.write( "-------------------\n" )
sys.stderr.write( "\nUsage:\n" )
sys.stderr.write( "\t%s <reference pdb> <comparing pdb>\n" %cmd )
sys.stderr.write( "\t%s -c <config_file>\n" %cmd )
sys.stderr.write( "\t%s -o <pdb_file>\n\n" %cmd )
sys.stderr.write( "For more information see 'dps_manual.pdf'\n\n" )
quit()
fatal = staticmethod( fatal )
out = staticmethod( out )
usage = staticmethod( usage )
#
# This implements a short version of S-W local alignment algorithm to obtain the longest gap-free sequence.
#
class SeqAligner:
def __init__(self, match=2, miss=-1,indel=-4):
self._match = match
self._miss = miss
self._indel = indel
self.length = 0
self.start1 = 0
self.start2 = 0
self.bases1 = []
self.bases2 = []
def matrix_build( self, s1, s2 ):
F = [[0] * (len(s2)+1) for i in xrange(len(s1)+1)]
max_coords = (0, 0)
for i in xrange(1, len(s1)+1):
for j in xrange(1, len(s2)+1):
if( s1[i-1] == s2[j-1] ):
score = self._match
else:
score = self._miss
choice0 = 0
choice1 = F[i-1][j-1] + score
choice2 = F[i-1][j] + self._indel
choice3 = F[i][j-1] + self._indel
F[i][j] = max( choice1, choice2, choice3 )
if( F[max_coords[0]][max_coords[1]] < F[i][j] ):
max_coords = (i, j)
return (F, max_coords)
def align( self, s1, s2 ):
self.length = 0
self.start1 = 0
self.start2 = 0
self.bases1 = []
self.bases2 = []
(F, (mi, mj)) = self.matrix_build( s1, s2 )
i = mi
j = mj
while (i > 0 and j > 0):
if( (F[i][j] >= F[i-1][j]) and (F[i][j] >= F[i][j-1]) ):
i -= 1
j -= 1
else:
break
self.length = mi-i
self.start1 = i
self.start2 = j
self.bases1 = s1[i:mi]
self.bases2 = s2[j:mj]
#
# PDB utility functions
#
def get_sequence( chain ):
return( [r.get_resname().strip() for r in chain] )
def get_model( pdb, model_num ):
parser = PDBParser()
struct = parser.get_structure( "X", pdb )
if( model_num < len( struct ) ):
return( struct[model_num] )
else:
Msg.out( "No model '%d' in pdb file '%s'" %(model_num, pdb) )
def show_data(pdb):
parser = PDBParser()
struct = parser.get_structure( "X", pdb )
Msg.out( "PDB '%s' (%d models):\n" %(pdb, len(struct)), False )
for model in struct:
Msg.out( "\tModel '%s' (%d chains):\n" %(model.get_id(), len(model) ), False )
for chain in model:
Msg.out( "\t\tChain '%s' (%s residues):\n" %(chain.get_id(), len(chain)), False )
txt1, txt2 = "", ""
for residue in chain:
txt1 += "%4d " %residue.get_id()[1]
txt2 += "%4s " %residue.get_resname()
Msg.out( "\t\t%s\n\t\t%s\n\n" %(txt1, txt2), False )
if __name__ == '__main__':
s1 = "GCUXAGAUXAYYYGUXUAXXXX"
s2 = "AUUGGCUUAGAUCAAGUGUAGUAUCUGUUCUUUUCAGUGUA"
sa = SeqAligner()
sa.align( s1, s2 )
print sa.length
print sa.start1
print sa.start2
print sa.bases1
print sa.bases2