-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpdbParser.pm
342 lines (308 loc) · 11.4 KB
/
pdbParser.pm
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
#!/usr/bin/perl -w
##############################################################################
# pdbParser.pm
#
# This module is used for simple parsing of pdb files.
# After parsing a pdb file via read(filename) function, several items can
# be retrieved via get("item name")
#
# Available item names:
# "HEADER.classification" - as in pdb specification
# "HEADER.depDate" - as in pdb specification
# "HEADER.idCode" - as in pdb specification
# "NMR" - if defined with value 1 means that more than one NMR model was found.
# in this case the pdb file is shorten to include only the first model
# "SEQRESx.raw" - amino acid list of chain x in aa one letter format (SEQRES)
# "SEQRES_chains" - array of chains, according to SEQRES
# "MODIFIED_COUNTx.raw" - number of modified residues to chain x (SEQRES)
# "MODIFIED_LISTx.raw" - list of modified residues for chain x (SEQRES)
# "ATOM_chains" - array of chains, according to ATOM
# "ATOMx.raw" - amino acid list of chain x in aa one letter format (ATOM)
# "ATOM.xMODIFIED_X" - if defined with value 1 means that in chain x
# there was a residue which was translated to x (ATOM) - not really
# possible, becaue it should be under HETATM and not under ATOM
#
#
#
# Note:
# The read() function does not clear previous data. It is best to
# create a new pdbParser for each file parsed.
#
# Changelog:
# 2007-Nov-10, Ofir Goldenberg - Created
# 2008-Nov-1, Elana Erez - added ATOM fields
##############################################################################
package pdbParser;
use strict;
# global vars
my %aa;
my %modified_residues;
my $nucleic_acid_flag = "!";
##############################################################################
# new()
# initialize object and amino acid hash conversion table
##############################################################################
sub new {
my ($class_name) = @_;
my ($self) = {};
bless ($self, $class_name);
$self->{'_created'} = 1;
# the conversion table
# !!! keys in lower case !!!
$aa{"ala"} = "A"; # Alanine
$aa{"arg"} = "R"; # Arginine
$aa{"asn"} = "N"; # Asparagine
$aa{"asp"} = "D"; # Aspartic acid
$aa{"cys"} = "C"; # Cysteine
$aa{"gln"} = "Q"; # Glutamine
$aa{"glu"} = "E"; # Glutamic acid
$aa{"gly"} = "G"; # Glycine
$aa{"his"} = "H"; # Histidine
$aa{"ile"} = "I"; # Isoleucine
$aa{"leu"} = "L"; # Leucine
$aa{"lys"} = "K"; # Lysine
$aa{"met"} = "M"; # Methionine
$aa{"phe"} = "F"; # Phenylalanine
$aa{"pro"} = "P"; # Proline
$aa{"ser"} = "S"; # Serine
$aa{"thr"} = "T"; # Threonine
$aa{"trp"} = "W"; # Tryptophan
$aa{"tyr"} = "Y"; # Tyrosine
$aa{"val"} = "V"; # Valine
# known items in SEQRES that will cause the file to be skipped
$aa{"a"} = $nucleic_acid_flag;
$aa{"t"} = $nucleic_acid_flag;
$aa{"c"} = $nucleic_acid_flag;
$aa{"g"} = $nucleic_acid_flag;
$aa{"u"} = $nucleic_acid_flag; # old style rna
$aa{"da"} = $nucleic_acid_flag;
$aa{"dt"} = $nucleic_acid_flag;
$aa{"dc"} = $nucleic_acid_flag;
$aa{"dg"} = $nucleic_acid_flag;
$aa{"du"} = $nucleic_acid_flag;
$aa{"di"} = $nucleic_acid_flag;
$aa{"5cm"} = $nucleic_acid_flag;
$modified_residues{"mse"} = "met";
$modified_residues{"asn"} = "asn";
$modified_residues{"mly"} = "lys";
$modified_residues{"hyp"} = "pro";
$modified_residues{"ser"} = "ser";
$modified_residues{"thr"} = "thr";
$modified_residues{"cme"} = "cys";
$modified_residues{"cgu"} = "glu";
$modified_residues{"sep"} = "ser";
$modified_residues{"kcx"} = "lys";
$modified_residues{"mle"} = "leu";
$modified_residues{"tpo"} = "thr";
$modified_residues{"cso"} = "cys";
$modified_residues{"ptr"} = "tyr";
$modified_residues{"dle"} = "leu";
$modified_residues{"llp"} = "lys";
$modified_residues{"dva"} = "val";
$modified_residues{"tys"} = "tyr";
$modified_residues{"aib"} = "ala";
$modified_residues{"ocs"} = "cys";
$modified_residues{"nle"} = "leu";
$modified_residues{"mva"} = "val";
return $self;
}
##############################################################################
# read(filename)
# Reads file and extracts needed data
#
# Params:
# filename - path to plain text pdb file (unzipped)
##############################################################################
sub read {
my ($self, $file) = @_;
my $line;
my $result = "1";
my $last_record_found = "";
my @chains = (); # stores the SEQRES fields chains
my @ATOM_chains = (); # stores the ATOM fields chains
my $pdb_line = 0;
# open file
open (PDBFILE, $file) or return 0;
# save filename we just read
$self->{'_filename'} = $file;
LINE: while ($line = <PDBFILE>)
{
$pdb_line++;
chomp $line;
# HEADER record
if ($line =~ /^HEADER.*/)
{
# save data
$self->{"HEADER.classification"} = substr($line,10,40);
$self->{"HEADER.depDate"} = substr($line,50,8);
$self->{"HEADER.idCode"} = substr($line,62,4);
}
# SEQRES record
elsif ($line =~ /^SEQRES.*/)
{
# get chain id and prev data
my $chainID = substr($line,11,1);
my $seq = $self->{"SEQRES$chainID.raw"};
#print "seq is: $seq\n";
my $modified_residue_counter = $self->{"MODIFIED_COUNT$chainID.raw"};
my $modified_residue_list = $self->{"MODIFIED_LIST$chainID.raw"};
# skip to next line if this is a nucleic acid chain
if (defined($seq) and substr($seq,0,1) eq $nucleic_acid_flag)
{
next;
}
# check if this chain was already processed
my $chainfound = 0;
foreach my $id (@chains)
{
if ($id eq $chainID) {$chainfound = 1;}
}
if (!$chainfound)
{
push(@chains,$chainID);
$modified_residue_counter = 0;
}
# convert to one letter format
foreach my $aminoName (split(" ",substr($line,19,51)))
{
# try to convert using regular amino acid hash
my $shortName = $aa{lc($aminoName)};
# check if residue is identified
if (!defined($shortName))
{
# count this modified residue
$modified_residue_counter++;
# try to check for known modifed residues
my $std_residue_name = $modified_residues{lc($aminoName)};
# check if residue is identified
if (!defined($std_residue_name))
{
# set residue name to X
$shortName = "X";
# add message to front of modified residue list
my $modified_changed_to_X_msg = "Modified residue(s) in this chain were converted to the one letter representation 'X'\n";
if (!defined($modified_residue_list))
{
$modified_residue_list = $modified_changed_to_X_msg;
}
elsif ($modified_residue_list !~ /Modified residue/)
{
$modified_residue_list = $modified_changed_to_X_msg.$modified_residue_list;
}
}
else
{
$shortName = $aa{$std_residue_name};
my $modified_residue_name = uc($aminoName);
# add to modified residue list
if (!defined($modified_residue_list))
{
$modified_residue_list = $modified_residue_name."->".uc($std_residue_name)."\n";
}
elsif ($modified_residue_list !~ /$modified_residue_name/)
{
$modified_residue_list .= $modified_residue_name."->".uc($std_residue_name)."\n";
}
}
}
# check if we found nucleic acid
elsif ($shortName eq $nucleic_acid_flag)
{
$seq = $nucleic_acid_flag."101 this chain contains nucleic acid";
last;
}
# add one letter code to sequence
$seq .= $shortName;
}
# save data
$self->{"SEQRES$chainID.raw"} = $seq;
$self->{"MODIFIED_COUNT$chainID.raw"} = $modified_residue_counter;
$self->{"MODIFIED_LIST$chainID.raw"} = $modified_residue_list;
}
elsif($line =~ /^MODEL\s+2/){
# if it NMR model: we copy only the first model to the pdb file.
$self->{"NMR"} = 1;
close PDBFILE;
open PDBFILE, "<$file";
my @orig_pdb = <PDBFILE>;
close PDBFILE;
open (PDBFILE, ">".$file) or return 0;
COPIED_LINE: foreach my $orig_line (@orig_pdb){
print PDBFILE $orig_line;
last COPIED_LINE if ($orig_line =~ /^ENDMDL/);
}
close PDBFILE;
last LINE;
}
elsif ($line =~ /^ATOM.*/)
{
# extract atom data
my $Res3L = substr ($line, 17, 3);
$Res3L = lc $Res3L;
my $ATOM_chain = substr ($line, 21, 1); # $_chain
my $residue_number = substr ($line, 22, 4); # $number
$residue_number =~ s/\s+//g;
# seems like these fields are important only for protein explorer
#my $atomNo .= substr ($line, 7, 5);
#my $iCode = substr ($line, 26, 2);
#if ($iCode !~ /[A-Z]/i){
# $chainInsCode = '';
#}
# get chain previous data
my $last_residue_num = $self->{"ATOM_RES_NUM$ATOM_chain.raw"};
if (!defined $last_residue_num){$last_residue_num="NA";}
my $ATOM_seq = $self->{"ATOM$ATOM_chain.raw"};
# check if this chain was already processed
my $chainfound = 0;
foreach my $id (@ATOM_chains)
{
if ($id eq $ATOM_chain) {$chainfound = 1;}
}
if (!$chainfound)
{
push(@ATOM_chains,$ATOM_chain);
}
# update ATOM sequence only if we read the next residue (not the next atom)
if (($last_residue_num eq "NA") or $last_residue_num < $residue_number){
if ($last_residue_num eq "NA") {$last_residue_num=0;}
while ($residue_number!=$last_residue_num+1){ # For Disorder regions
$ATOM_seq=$ATOM_seq."X";
$last_residue_num++;
}
# convert to one letter format
my $resShortName = $aa{$Res3L};
if (!defined($resShortName))
{
$resShortName = "X";
$self->{"ATOM.$ATOM_chain"."MODIFIED_X"} = 1;
}
$ATOM_seq .= $resShortName;
$self->{"ATOM_RES_NUM$ATOM_chain.raw"} = $residue_number;
$self->{"ATOM$ATOM_chain.raw"} = $ATOM_seq;
}
}
}
# save chain list
#$self->{"chains"} = [@chains]; # OLD
$self->{"SEQRES_chains"} = [@chains];
$self->{"ATOM_chains"} = [@ATOM_chains];
# finish up
close PDBFILE;
return $result;
}
# just return the $nucleic_acid_flag
sub nucleic_acid_flag
{
return $nucleic_acid_flag;
}
# just return the $nucleic_acid_flag
sub skip_file_code
{
return $nucleic_acid_flag;
}
# get a parsed value
sub get {
my ($self, $key) = @_;
return $self->{$key};
}
1;