-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathpdb_annotate_structure.py
237 lines (169 loc) · 7.21 KB
/
pdb_annotate_structure.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
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
################################################################################
#
# MRC FGU Computational Genomics Group
#
# $Id$
#
# Copyright (C) 2009 Andreas Heger
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#################################################################################
'''
pdb_annotate_structure.py -
======================================================
:Author: Andreas Heger
:Release: $Id$
:Date: |today|
:Tags: Python
Purpose
-------
.. todo::
describe purpose of the script.
Usage
-----
Example::
python pdb_annotate_structure.py --help
Type::
python pdb_annotate_structure.py --help
for command line help.
Documentation
-------------
Code
----
'''
USAGE="""
python pdb_annotate_structure.py [OPTIONS] pdb_id
annotate a structure given by pdb_id
"""
import sys, string, re, optparse
import alignlib
import Experiment
import IOTools
import PdbTools
DEBUG = 1
#--------------------------------------------------------------------------------
def MapRight( mapping, row_residue ):
"""return mapping of row_residue Go right, if not found."""
col_residue = mapping.mapColToRow( row_residue )
max_residue = mapping.getColTo()
while col_residue == 0:
row_residue = row_residue + 1
if row_residue > max_residue:
return (0)
col_residue = mapping.mapColToRow( row_residue )
return (col_residue)
#--------------------------------------------------------------------------------
def MapLeft( mapping, row_residue ):
"""return mapping of row_residue Go right, if not found."""
col_residue = mapping.mapColToRow( row_residue )
min_residue = mapping.getColFrom()
while col_residue == 0:
row_residue = row_residue - 1
if row_residue < min_residue:
return (0)
col_residue = mapping.mapColToRow( row_residue )
return (col_residue)
if __name__ == '__main__':
parser = optparse.OptionParser( version = "%prog version: $Id: pdb_annotate_structure.py 2782 2009-09-10 11:40:29Z andreas $",
usage = globals()["__doc__"] )
parser.add_option( "-p", "--filename-pdb", dest="filename_pdb", type="string",
help="filename with pdb structure." )
parser.add_option( "-f", "--filename-fasta", dest="filename_fasta", type="string",
help="filename with sequence on which annotation is based. If not given, the pdb sequence is used." )
parser.set_defaults(
filename_pdb = None,
filename_fasta = None,
)
(options, args) = Experiment.Start( parser, add_pipe_options = True )
if len(args) != 1:
print USAGE, "please supply the pdb identifier"
sys.exit(2)
if string.find(args[0], "-") != -1:
(options.pdb_id,options.pdb_chain) = string.split(args[0], "-")
else:
options.pdb_id = args[0]
options.pdb_chain = ""
message = ""
## pdb_filename = "/homes/heger/pdb_temp/temp.pdb"
## print PdbTools.GetPdbFile( pdb_id, pdb_filename )
## pdb_lines = PdbTools.GetPdbFileLine( pdb_id )
#######################################################################
## retrieve structure
if options.filename_pdb:
infile = open(options.filename_pdb, "r")
pdb_lines = infile.readlines()
infile.close()
else:
pdb_lines = os.popen( param_retrieval_command % string.lower(param_pdb_id) ).readlines()
viewer = PdbTools.RasmolViewInline( pdb_lines, sys.stdout )
viewer.Command( "echo %s" % message)
if options.filename_fasta:
infile = open( options.filename_fasta, "r" )
description, reference_sequence = IOTools.readSequence( infile )
infile.close()
else:
reference_sequence = None
if DEBUG:
viewer.Command("echo cmdline: %s" % (string.join(sys.argv, " ")))
if not pdb_lines:
viewer.Command("echo error: structure not found in local database")
viewer.WriteScript()
sys.exit()
if reference_sequence:
map_pdb2seq, rmap_pdb2seq, rmap_seq2pdb, lstructure, first_residue, last_residue, sequence = PdbTools.buildMapPdb2Sequence( reference_sequence,
options.filename_pdb,
options,
options.pdb_chain )
## note: switchRowCol does not work for AlignataVector, thus the inefficient
## mapping of col to row.
else:
map_pdb2seq, map_pdb2seq = None, None
first = True
ninput, noutput, nmissed = 0, 0, 0
for line in sys.stdin:
if line[0] == "#" : continue
if first:
first = False
continue
residue_number, aminoacid, shape, colour = line[:-1].split("\t")
if colour == "": colour = None
if shape == "": shape = None
ninput += 1
# use pdb2seq for mapping, as the script renumbers the residues.
if "-" in residue_number:
first_res, last_res = residue_number.split( "-" )
pdb_from = MapRight( map_pdb2seq, int(first_res) )
pdb_to = MapLeft( map_pdb2seq, int(last_res) )
else:
pdb_from = map_pdb2seq.mapColToRow( int(residue_number) )
pdb_to = pdb_from
if pdb_from == 0 or pdb_to == 0:
nmissed += 1
if options.loglevel >= 1:
options.stdlog.write( "# residue not found: %s:%s\t%i-%i\n" % (residue_number, aminoacid, pdb_from, pdb_to ))
continue
if options.loglevel >= 3:
options.stdlog.write( "# mapped: %s%s to %s%i-%s%i\n" % (aminoacid, residue_number, sequence[pdb_from-1], pdb_from, sequence[pdb_to-1], pdb_to) )
viewer.highlightResidues( int(pdb_from), int(pdb_to), options.pdb_chain,
colour = colour,
shape = shape )
noutput += 1
if options.pdb_chain:
viewer.Command( "restrict :%s" % options.pdb_chain)
viewer.Command( "centre" )
viewer.WriteScript()
if options.loglevel >= 1:
options.stdlog.write("# ninput=%i, noutput=%i, nmissed=%i\n" % (ninput, noutput, nmissed))
Experiment.Stop()