-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDBEntry.pm
executable file
·324 lines (294 loc) · 11.4 KB
/
PDBEntry.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
## PDBEntry.pm
############################################################################################
###
### Written by Sen Yao, 07/20/2016
### Copyright Sen Yao, Robert Flight, and Hunter Moseley, 07/20/2016. All rights reserved.
###
############################################################################################
package PDBEntry;
use strict;
use Atom;
use Sequence;
use Residue;
use Math::MatrixReal;
our @defaultDataMembers = ("singlePdbFile" => "" , "metal" => "");
our %metalFull = ("ZN" => "ZINC", "FE" => "IRON", "CA" => "CALCIUM", "NA" => "SODIUM", "MG" => "MAGNESIUM");
## usage PDBEntry->new(singlePdbFile => "file_path")
sub new
{
my $class = shift @_;
# my $self = { @defaultDataMembers, @_, "zn" => $zn, "atoms" => 0 };
my $self = { @defaultDataMembers, "atoms" => [], "residues" => [], "_residueIDmap" => {}, "sequences" => {}, @_ };
bless $self, ref $class || $class;
$self->read() if ($self->{singlePdbFile} ne "");
return $self;
}
##
sub read
{
my $self = shift @_;
my $singlePdbFile = (@_) ? shift @_ : $self->{singlePdbFile};
$self->{singlePdbFile} = $singlePdbFile;
my $atoms = (@_) ? shift @_ : [];
$self->{atoms} = $atoms;
$self->{residues} = [];
$self->{_residueIDmap} = {};
if ($singlePdbFile =~ /\.gz$/)
{ open (PDBFILE, "/bin/zcat $singlePdbFile|") || die "Error in opening $singlePdbFile: $!"; }
else
{ open (PDBFILE, "<$singlePdbFile") || die "Error in opening $singlePdbFile: $!"; }
my $modelCount = 0;
my ($method, $PDBid, $date);
my $chainSeqs = {};
my $resolution = -1;
my $rValue = -1;
my $rFree = -1;
my $solvent = "False";
my $crystalMats = [];
my $bioMats = [];
my $ortha = [];
my $orthb = [];
my $orthc = [];
my $biomolecules = [];
my $authorU = 0;
while ( my $record = <PDBFILE>)
{
chomp $record;
if ($record =~ /^ATOM/ || $record =~ /^HETATM/)
{
my @keyValues = ('record' => $record,
'recordType' => substr($record, 0, 6),
'serial' => substr($record, 6, 5),
'atomName'=> substr($record, 12, 4),
'alternateLocation' => substr($record, 16, 1),
'residueName'=> substr($record, 17, 3),
'chainID' => substr($record, 21, 1),
'residueNumber' => substr($record, 22, 4),
'x' => substr($record, 30, 8),
'y' => substr($record, 38, 8),
'z' => substr($record, 46, 8),
'occupancy' => substr($record, 54, 6),
'bFactor' => substr($record, 60, 6),
'element' => substr($record, 76, 2),
'file' => $singlePdbFile,
'PDBid' => $PDBid,
'method' => $method,
'date' => $date,
'resolution' => $resolution,
'rValue' => $rValue,
'rFree' => $rFree,
'solvent' => $solvent
) ;
foreach my $value (@keyValues)
{
$value =~ s/^\s+//; ## Remove leading white spaces.
$value =~ s/\s+$//; ## Remove tailing white spaces.
}
push @$atoms, Atom->new(@keyValues);
}
elsif ($record =~ /^HEADER/)
{
$PDBid = substr($record, 62, 4);
$date = substr($record, 57, 2);
}
elsif ($record =~ /^EXPDTA/)
{
$method = substr($record, 6, 30);
$method =~ s/^\s+//; ## Remove leading white spaces.
$method =~ s/\s+$//; ## Remove tailing white spaces.
$method =~ s/\s/_/g;
}
elsif ($record =~ /^REMARK 2 RESOLUTION.(.+)ANGSTROMS/)
{ $resolution = $1;}
elsif ($record =~ /^REMARK 3 R VALUE \(WORKING SET\) : (.+)$/)
{ $rValue = $1; }
elsif ($record =~ /^REMARK 3 FREE R VALUE : (.+)$/)
{$rFree = $1; }
elsif ($record =~ /^MODEL/)
{
$modelCount++;
last if ($modelCount > 1); ## Use the first model if there are more the one model structure
}
elsif ($record =~ /^REMARK 280.* ($self->{metal}|$metalFull{$self->{metal}})/)
{$solvent = "True";}
elsif ($record =~ /^REMARK 290 SMTRY/)
{
my @mat = split (/\s+/, $record);
my $smtryNum = substr ($mat[2], -1);
$$crystalMats[$mat[3]-1][$smtryNum] = [0, $mat[4], $mat[5], $mat[6]]; ## add 0 in front so that the index will match
$$crystalMats[$mat[3]-1][4][$smtryNum] = $mat[7];
}
elsif ($record =~ /^REMARK 350 BIOMOLECULE:/)
{
## Use author-determined instead of sofeware-determined biological symmetry matrix if there is any. Otherwise use the sofeware-determined.
my $authorL;
my $bioMats = [];
my $matChains = [];
while (my $nextline = <PDBFILE>)
{
if ($nextline =~ /^REMARK 350 AUTHOR DETERMINED BIOLOGICAL UNIT/)
{
$authorL = 1;
$authorU = 1;
}
elsif ($nextline =~ /^REMARK 350 SOFTWARE DETERMINED QUATERNARY STRUCTURE/)
{
next if $authorL == 1;
last if $authorU == 1;
}
elsif ($nextline =~ /^REMARK 350 APPLY THE FOLLOWING TO CHAINS: (.+)$/)
{
my $var = $1;
$var =~ s/\s+$//;
$var =~ s/,$// if substr($var, -1) eq ",";
@$matChains = split (', ', $var);
}
elsif ($nextline =~ /^REMARK 350 AND CHAINS: (.+)$/)
{
my $var = $1;
$var =~ s/\s+$//;
$var =~ s/,$// if substr($var, -1) eq ",";
push @$matChains, (split (', ', $var));
}
elsif ($nextline =~ /^REMARK ... /)
{ last; }
elsif ($nextline =~ /^REMARK 350 BIOMT/)
{
my @mat = split (/\s+/, $nextline);
my $smtryNum = substr ($mat[2], -1);
$$bioMats[$mat[3]-1][$smtryNum] = [0, $mat[4], $mat[5], $mat[6]]; ## add 0 in front so that the index will match
$$bioMats[$mat[3]-1][4][$smtryNum] = $mat[7];
}
}
push @$biomolecules, {"matrices" => $bioMats, "chains" => $matChains} if @$matChains;
}
elsif ($record =~ /^SEQRES/)
{
my $chain = substr($record, 11, 1);
my $chainSeqOne = substr($record, 19, 51);
push (@{$$chainSeqs{$chain}}, $chainSeqOne);
}
elsif ($record =~ /^CRYST1/)
{ my @cryst = split (/\s+/, $record); }
elsif ($record =~ /^SCALE1/)
{ $ortha = [(split (/\s+/, $record))[1..3]]; }
elsif ($record =~ /^SCALE2/)
{ $orthb = [(split (/\s+/, $record))[1..3]]; }
elsif ($record =~ /^SCALE3/)
{ $orthc = [(split (/\s+/, $record))[1..3]]; }
}
close(PDBFILE);
## comment out residue calc for now, 2016.3.13, as no longer calc chi dihedreal angles.
#my $residueMap = $self->{_residueIDmap};
#foreach my $atom (@$atoms)
# {
# my $resID = $atom->resID();
# if (! exists $$residueMap{$resID})
# { $$residueMap{$resID} = Residue->new($atom); }
# else
# { $$residueMap{$resID}->add($atom); }
# }
#@{$self->{residues}} = sort { $a->cmp($b); } (values %$residueMap);
my $seqs = [];
foreach my $chain (keys %$chainSeqs)
{
push (@$seqs, Sequence->createSEQRESseq($chain, $$chainSeqs{$chain}, $atoms)) ;
}
$self->{sequences}->{seqres} = $seqs; #SEQRES sequences
$self->{sequences}->{atom} = Sequence->createATOMseqs($atoms); #ATOM sequences
$self->{sequences}->{number} = Sequence->createATOMnum($atoms); #ATOM numbers
return $self if ($method ne "X-RAY_DIFFRACTION");
## biological symmetry
my @bioAtoms;
foreach my $bio (@$biomolecules)
{
next if scalar @{$$bio{"matrices"}} == 1;
foreach my $num (1..(@{$$bio{"matrices"}}-1))
{
my @atomsChain = grep {my $atom = $_; grep {$_ eq $atom->{chainID};} (@{$$bio{chains}}); } (@$atoms);
my @atomsNew = map { $_->transformBio($$bio{"matrices"}[$num]); } (@atomsChain);
push @bioAtoms, @atomsNew;
}
}
## symmetry related atoms
## x' = Rx + T
my $xmin = (sort {$a <=> $b} (map {$_->{x}} (@$atoms)))[0];
my $xmax = (sort {$b <=> $a} (map {$_->{x}} (@$atoms)))[0];
my $ymin = (sort {$a <=> $b} (map {$_->{y}} (@$atoms)))[0];
my $ymax = (sort {$b <=> $a} (map {$_->{y}} (@$atoms)))[0];
my $zmin = (sort {$a <=> $b} (map {$_->{z}} (@$atoms)))[0];
my $zmax = (sort {$b <=> $a} (map {$_->{z}} (@$atoms)))[0];
## Get orthogonalization matrix O from deororthogonalization matrix O' (given by SCALE record)
## The neighbering cells can be calculated using formula, X' = O(O'(RX + T) + T') = OO'(RX+T) + OT' = RX+T + O[-1/0/1,-1/0/1,-1/0/1]
my $inverseO = Math::MatrixReal->new_from_rows([$ortha, $orthb, $orthc]);
my $bigO = $inverseO->inverse();
my @metals = grep { $_->{element} eq $self->{metal} && substr($_->{chainID}, 0,1) ne "#"; } (@$atoms);
#print "metal: ", scalar @metals, ", ", $metals[0]->{x}, ", ", $metals[0]->{y}, ", ", $metals[0]->{z}, "\n";
#print "xyz min/max: $xmin, $xmax, $ymin, $ymax, $zmin, $zmax\n";
#print "matrix:\n";
#map { print join (", ", @$_), "\n" if $_;} (@{$$crystalMats[2]});
my $numSym = 0;
my @symAtoms;
foreach my $i (-1, 0, 1)
{
foreach my $j (-1, 0, 1)
{
foreach my $k (-1, 0, 1)
{
foreach my $t (0..(@$crystalMats-1))
{
next if ($i == 0 && $j == 0 && $k == 0 && $t == 0);
my $mat = $$crystalMats[$t];
my $neighborX = $bigO->element(1,1) * $i + $bigO->element(1,2) * $j + $bigO->element(1,3) * $k;
my $neighborY = $bigO->element(2,1) * $i + $bigO->element(2,2) * $j + $bigO->element(2,3) * $k;
my $neighborZ = $bigO->element(3,1) * $i + $bigO->element(3,2) * $j + $bigO->element(3,3) * $k;
my (@xs, @ys, @zs);
foreach my $xx ($xmin, $xmax)
{
foreach my $yy ($ymin, $ymax)
{
foreach my $zz ($zmin, $zmax)
{
push (@xs, ($$mat[1][1] * $xx + $$mat[1][2] * $yy + $$mat[1][3] * $zz + $$mat[4][1] + $neighborX));
push (@ys, ($$mat[2][1] * $xx + $$mat[2][2] * $yy + $$mat[2][3] * $zz + $$mat[4][2] + $neighborY));
push (@zs, ($$mat[3][1] * $xx + $$mat[3][2] * $yy + $$mat[3][3] * $zz + $$mat[4][3] + $neighborZ));
}
}
}
@xs = sort {$a <=> $b} (@xs);
@ys = sort {$a <=> $b} (@ys);
@zs = sort {$a <=> $b} (@zs);
#print "transform: $i, $j, $k, $t: $xs[0], $ys[0], $zs[0]; $xs[-1], $ys[-1], $zs[-1]; $neighborX, $neighborY, $neighborZ\n" if ($i == 0 && $j == 0 && $k == -1 && $t == 2);
foreach my $metal (@metals)
{
if (($xs[0]-3.5 < $metal->{x}) && ($ys[0]-3.5 < $metal->{y}) && ($zs[0]-3.5 < $metal->{z}) && ($xs[-1] + 3.5 > $metal->{x}) && ($ys[-1] + 3.5 > $metal->{y}) && ($zs[-1] + 3.5 > $metal->{z}))
{
#print "$i, $j, $k, $t\n";
$numSym += 1;
my @atomsNew = map { $_->transform($mat, $neighborX, $neighborY, $neighborZ, $i.$j.$k.$t); } (@$atoms);
push @symAtoms, @atomsNew;
last;
}
}
#map { print join (", ", @$_), "\n" if $_;} (@$mat);
#my $atom = (grep {$_->{residueNumber} == 126 && $_->{atomName} eq "OE1"} (@$atoms))[0];
#my $newAtom = $atom->transform($mat, $i * $crystA, $j * $crystB, $k * $crystC);
#print $atom->coordinates(), "\n";
#print $newAtom->coordinates(), "\n";
#print "originals, $xmin, $xmax, $ymin, $ymax, $zmin, $zmax\n";
#print "new, $xminNew, $xmaxNew, $yminNew, $ymaxNew, $zminNew, $zmaxNew\n";
}
}
}
}
push @$atoms, @symAtoms;
$self->{symNum} = $numSym;
return $self;
}
sub residue
{
my $self = shift @_;
my $resID = shift @_;
return (exists $self->{_residueIDmap}{$resID}) ? $self->{_residueIDmap}{$resID} : 0;
}
1;