forked from chaolinzhanglab/czplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPDB.pm
466 lines (370 loc) · 10.4 KB
/
PDB.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
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
#
#===============================================================================
#
# FILE: PDB.pm
#
# DESCRIPTION: Package to handle PDB files
# BUGS: ---
# NOTES:
# AUTHOR: Chaolin Zhang (cz), [email protected]
# COMPANY: Columbia University
# VERSION: 1.0
# CREATED: 02/10/2017
# REVISION: ---
#===============================================================================
package PDB;
require Exporter;
our $VERSION = 1.00;
@ISA = qw (Exporter);
@EXPORT = qw (
three2one
getPrimarySeq
extractComplexInfo
res2res
);
=head1 NAME
PDB - subroutines that deal with PDB files
subroutines starting with a hyphen should not be called outside
=cut
use strict;
use warnings;
use Data::Dumper;
use Carp;
use Bio::Structure::IO;
my %_t2o = (
'ALA' => 'A',
'ARG' => 'R',
'ASN' => 'N',
'ASP' => 'D',
'CYS' => 'C',
'GLN' => 'Q',
'GLU' => 'E',
'GLY' => 'G',
'HIS' => 'H',
'ILE' => 'I',
'LEU' => 'L',
'LYS' => 'K',
'MET' => 'M',
'PHE' => 'F',
'PRO' => 'P',
'SEC' => 'U',
'SER' => 'S',
'THR' => 'T',
'TRP' => 'W',
'TYR' => 'Y',
'VAL' => 'V',
'ASX' => 'B',
'GLX' => 'Z'
);
=head2 three2one
convert amino acid in three letter format to one letter abbreviation
=cut
sub three2one
{
my $aa = $_[0];
return exists $_t2o{$aa} ? $_t2o{$aa} : "";
}
=head2 getPrimarySeq
#return amino acid or nucleotide sequence and type
{seq=>$seqStr, type=>$type}
#0 otherwise
=cut
sub getPrimarySeq
{
my ($struct, $chain) = @_;
my @seq;
for my $res ($struct->get_residues ($chain))
{
my $resid = $res->id;
#print $resid, "\n";
my ($r, $n) = split("-", $resid);
next if $r eq 'HOH';
next if $r eq '6U0' || $r eq 'UVP' || $r eq 'GTP' || $r eq 'G5J'; #just workaround
push @seq, $r; # assume HETATOM has been removed
}
return 0 if @seq < 1;
my $type = "";
my $seqStr = "";
#print "chain=", $chain->id, "\n";
#print Dumper (\@seq), "\n";
if (length ($seq[0]) == 3)
{
if (three2one($seq[0]))
{
$type = "protein";
foreach my $res (@seq)
{
if (length ($res) != 3)
{
#can be small molecule, eg. 1CVJ
warn "unknown amino acid: $res\n";
next;
#if ($res eq 'DA' || $res eq 'DC' || $res eq 'DG' || $res eq 'DT')
#{
# #DNA adduct?
# return 0;
#}
#else
#{
# Carp::croak "unknown amino acid: $res in ", Dumper (\@seq), "\n";
#}
}
if (my $aa = three2one ($res))
{
$seqStr .= $aa;
}
elsif ($res eq 'UNK' || $res eq 'MSE' || $res eq 'AMP' || $res eq 'SO4' || $res eq 'DIO' || $res eq '1PE'
|| $res eq 'ADP' || $res eq 'ALF' || $res eq 'GOL' || $res eq 'ANP' || $res eq 'M2M' || $res eq 'EDO'
|| $res eq 'IPH' || $res eq 'IPA' || $res eq 'ACT' || $res eq 'UNX' || $res eq 'SAH' || $res eq 'TRS'
|| $res eq 'CME' || $res eq 'PO4' || $res eq 'BEF')
{
warn "unknown amino acid: $res\n";
next;
#return 0;
}
else
{
Carp::croak "unknown amino acid: $res in ", Dumper (\@seq), "\n";
}
}
#$seqStr = join ("", map {$three2one{$_}} @seq);
}
elsif ($seq[0] eq 'UNK')
{
return 0;
}
else
{
Carp::croak "unknown amino acid: $seq[0]\n";
}
}
elsif (length ($seq[0]) == 1)
{
if ($seq[0] =~/[ACGUI]/i)
{
$type = "rna";
foreach my $b (@seq)
{
if (length($b) != 1)
{
if ($b eq 'DA' || $b eq 'DC' || $b eq 'DG' || $b eq 'DT' || $b eq 'DU')
{
$type="dna_rna_hybrid";
$seqStr .= substr($b, 1, 1);
next;
}
elsif ($b eq '5BU' || three2one($b) ne '')
{
#some weired stuff
#return 0;
warn "some weird stuff: $b\n";
next;
}
else
{
warn "unknown nucleotide: $b\n";
next;
#Carp::croak "unknown nucleotide: $b in ", Dumper (\@seq), "\n";
}
}
if ($b=~/[ACGUI]/i)
{
$seqStr .= $b;
}
elsif ($b eq 'N' || $b eq 'K')
{
warn "unknown nucleotide: $b\n";
next;
#return 0;
}
else
{
Carp::croak "unknown nucleotide: $b in ", Dumper (\@seq), "\n";
}
}
#$seqStr = join ("", @seq);
}
elsif ($seq[0] eq 'N')
{
Carp::croak "unknown nucleotide: $seq[0]\n", Dumper (\@seq), "\n";
return 0;
}
else
{
Carp::croak "unknown nucleotide: $seq[0]\n";
}
}
elsif (length ($seq[0]) == 2)
{
my $b = substr ($seq[0], 1, 1);
if ($b =~/[ACGTIU]/i)
{
$type = "dna";
foreach my $bb (@seq)
{
if (length($bb) != 2)
{
if ($bb eq 'A' || $bb eq 'C' || $bb eq 'G' || $bb eq 'U')
{
$type="dna_rna_hybrid";
$seqStr .= $bb;
next;
}
else
{
Carp::croak "unknown nucleotide: $bb in ", Dumper (\@seq), "\n";
}
}
my $b = substr ($bb, 1, 1);
if ($b=~/[ACGTIU]/i)
{
$seqStr .= $b;
}
else
{
Carp::croak "unknown nucleotide: $b in ", Dumper (\@seq), "\n";
}
}
#$seqStr = join ("", @seq);
}
}
if ($type eq 'protein' || $type eq 'rna' || $type eq 'dna' || $type eq 'dna_rna_hybrid')
{
return {seq=>$seqStr, type=>$type};
}
else
{
return 0;
}
}
=head2 extractComplexInfo
#return reference of complex structure if legitimate
{
id=>
complex=>{chainid}->{db, acc, name, type, seq}
}
#return 0 otherwise
=cut
sub extractComplexInfo
{
my $struct = $_[0];
#my $inFile = $_[0];
#my $io = Bio::Structure::IO->new(-file => $inFile, -format => 'PDB');
#my $struct = $io->next_structure;
my $structid = $struct->id;
#Carp::croak Dumper ($struct), "\n";
my @chains = $struct->get_chains;
return 0 if @chains < 2;
my %complex;
for my $chain ($struct->get_chains)
{
my $chainid = $chain->id;
my $seqInfo = getPrimarySeq ($struct, $chain);
if ($seqInfo == 0)
{
warn ("unable to parse sequence in structure $structid, chain $chainid\n");
return 0;
}
my $type = $seqInfo->{'type'};
my $seq = $seqInfo->{'seq'};
$complex{$chainid} = {type=>$type, seq=>$seq};
}
my $annot = $struct->annotation;
my ($dbref) = $annot->get_Annotations('dbref');
if (not defined $dbref)
{
warn ("dbref not found in struct $structid\n");
return 0;
}
my @cols = split (/\s+/, $dbref->value);
my $ncols = @cols;
if ($ncols % 9 != 0)
{
warn "error in parsing dbref in struct $structid\n";
return 0;
}
for (my $i = 0; $i < @cols; $i += 9)
{
my $chainid = $cols[$i+1];
if (not exists $complex{$chainid})
{
warn "error in parsing dbref in struct $structid: chain $chainid does not exist\n";
return 0;
}
my $db = $cols[$i+4];
my $acc = $cols[$i+5];
my $name = $cols[$i+6];
$complex{$chainid}{'db'} = $db;
$complex{$chainid}{'acc'} = $acc;
$complex{$chainid}{'name'} = $name;
}
my ($compnd) = $annot->get_Annotations('compnd');
my @molecules;
my $compndStr = $compnd->value;
$compndStr=~s/\s+$//g;
$compndStr .= ";" unless $compndStr=~/;$/;
#in some weired cases, there is no ; at the end, so we have to add this for correct parsing below
#print "compnd=$compndStr\n";
my $nchains = 0;
#while ($compndStr=~m/MOL_ID: (\d+);\s+MOLECULE: (\S.*?);*\s*CHAIN: (\S.*?);/g)
#while ($compndStr=~m/MOL_ID: (\d+);\s+MOLECULE: (\S.*?);*\s*CHAIN: (\w{1,2}[;|\,\s.*?;])/g)
#print $compndStr, "\n";
while ($compndStr=~m/MOL_ID: (\d+);\s+MOLECULE:\s+(\S.*?);*\s*CHAIN: (\w{1,2}(?:\s*\,\s*\w{1,2}){0,})\,?;/g)
{
my ($id, $desc, $chainId) = ($1, $2, $3);
chop $chainId if $chainId=~/[;\,]$/;
#print "id=$id, desc=$desc, chainId=$chainId\n";
my %mol = (id=>$id, desc=>$desc, chain=>$chainId);
if ($mol{'chain'}=~/\,$/)
{
#due to inconsistancy in some record
chop $mol{'chain'};
}
my @chains = split(/\s*\,\s*/, $mol{'chain'});
$nchains += @chains;
if (not exists $complex{$chains[0]})
{
warn "chain $chains[0] does not exist in structure $structid. Detected chains: \n", join("\t", sort keys %complex), "\n";
return 0;
}
$mol{'type'} = $complex{$chains[0]}{'type'};
$mol{'seq'} = $complex{$chains[0]}{'seq'};
$mol{'db'} = $complex{$chains[0]}{'db'};
$mol{'acc'} = $complex{$chains[0]}{'acc'};
$mol{'name'} = $complex{$chains[0]}{'name'};
push @molecules, \%mol;
}
#Carp::croak Dumper (\@molecules), "\n";
if ($nchains != keys %complex) #this can happy when there are many chains and the meta data was truncated by bioperl. rediculous! we ignore those stucture for now
{
warn "inconsistent number of chains in structure $structid. Detected chains: \n", join("\t", sort keys %complex), "\n", "Chains in compnd: \n", join(", ", map{$_->{"chain"}} @molecules), "\n";
return 0;
}
#Carp::croak Dumper (\@molecules), "\n";
my %ret = (id=>$structid, molecules=>\@molecules, chains=>\%complex);
return \%ret;
}
=head2 res2res
calculate residue to residue distance by the pair of atoms closest to each other
=cut
sub res2res
{
my ($struct, $res1, $res2) = @_;
my $d = -1;
my ($atomId1, $atomId2);
foreach my $atom1 ($struct->get_atoms ($res1))
{
my ($x1, $y1, $z1) = $atom1->xyz;
foreach my $atom2 ($struct->get_atoms ($res2))
{
my ($x2, $y2, $z2) = $atom2->xyz;
my $d2 = sqrt(($x1-$x2)**2 + ($y1-$y2)**2 + ($z1-$z2)**2);
next if $d >= 0 && $d2 > $d;
$d = $d2;
$atomId1 = $atom1->id;
$atomId2 = $atom2->id;
}
}
return {d=>$d, atom1=>$atomId1, atom2=>$atomId2};
}
1;