blib/lib/Chemistry/File/PDB.pm

filter: Statementstmt 29.3% 27 / 92 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 66.6% 8 / 12 Podpod 100% 4 / 4 totaltotal 24.5% 39 / 159 CC 24 SCAR 42.1
CC
24
Cov
20%
read_mol
132.0
write_string
72.3
file_is
42.0
CRAP 67.6
« blib/lib/Chemistry/File/SLN.pm ↑ Summary blib/lib/Statistics/Regression.pm »
1 package Chemistry::File::PDB;
2  
3 9 $VERSION = '0.21';
4 # $Id: PDB.pm,v 1.11 2005/05/16 22:33:48 itubert Exp $
5  
6 9 ▶use base qw(Chemistry::File);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Chemistry::MacroMol;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Chemistry::Domain;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Chemistry::File::PDB - Protein Data Bank file format reader/writer
17  
18 =head1 SYNOPSIS
19  
20 use Chemistry::File::PDB;
21  
22 # read a PDB file
23 my $macro_mol = Chemistry::MacroMol->read("myfile.pdb");
24  
25 # write a PDB file
26 $macro_mol->write("out.pdb");
27  
28 # read all models in a multi-model file
29 my @mols = Chemistry::MacroMol->read("models.pdb");
30  
31 # read one model at a time
32 my $file = Chemistry::MacroMol->file("models.pdb");
33 $file->open;
34 while (my $mol = $file->read_mol($file->fh)) {
35 # do something with $mol
36 }
37  
38 =cut
39  
40 =head1 DESCRIPTION
41  
42 This module reads and writes PDB files. The PDB file format is commonly used to
43 describe proteins, particularly those stored in the Protein Data Bank
44 (L<http://www.rcsb.org/pdb/>). The current version of this module only uses the
45 following record types, ignoring everything else:
46  
47 ATOM
48 HETATM
49 ENDMDL
50  
51 This module automatically registers the 'pdb' format with Chemistry::Mol,
52 so that PDB files may be identified and read by Chemistry::Mol->read(). For
53 autodetection purpuses, it assumes that files ending in .pdb or having
54 a line matching /^(ATOM |HETATM)/ are PDB files.
55  
56 The PDB reader and writer is designed for dealing with Chemistry::MacroMol
57 objects, but it can also create and use Chemistry::Mol objects by throwing some
58 information away.
59  
60 =head2 Properties
61  
62 When reading and writing files, this module stores or gets some of the
63 information in the following places:
64  
65 =over
66  
67 =item $domain->type
68  
69 The residue type, such as "ARG".
70  
71 =item $domain->name
72  
73 The type and sequence number, such as "ARG114".
74  
75 =item $domain->attr("pdb/sequence_number")
76  
77 The residue sequence number as given in the PDB file.
78  
79 =item $domain->attr("pdb/chain_id")
80  
81 The chain to which this residue belongs (one character).
82  
83 =item $domain->attr("pdb/insertion_code")
84  
85 The residue insertion code (see the PDB specification for details).
86  
87 =item $atom->name
88  
89 The PDB atom name, such as "CA".
90  
91 =item $atom->attr("pdb/residue_name")
92  
93 The name of the residue, as discussed above.
94  
95 =item $atom->attr("pdb/serial_number")
96  
97 The serial number for the atom, as given in the PDB file.
98  
99 =back
100  
101 If some of this information is not available when writing a PDB file, this
102 module tries to make it up (by counting the atoms or residues, for example).
103 The default residue name for writing is UNK (unknown). Atom names are just the
104 atomic symbols.
105  
106 =head2 Multi-model files
107  
108 If a PDB file has multiple models (separated by ENDMDL records), each call to
109 read_mol will return one model.
110  
111 =cut
112  
113 9 Chemistry::Mol->register_format(pdb => __PACKAGE__);
114  
115 0 ▶sub read_mol {
Subroutine read_mol: not called CC=11 SCAR=48.8
Pod documented
116 0 my ($self, $fh, %options) = @_;
117 0 ▶ return if $fh->eof;
Branch: 1 branch
BranchTrueFalseTotal
if $fh->eof 0 0 0
118  
119 0 my $domain;
120 0 ▶ my $mol_class = $options{mol_class} || "Chemistry::MacroMol";
Condition0%0/2
$options{'mol_class'} || 'Chemistry::MacroMol'
l!l
00
MC/DC0%0/1
$options{'mol_class'} || 'Chemistry::MacroMol'
$options{'mol_class'}
Truth table0%0/2
A
A= $options{'mol_class'}
Aresult
11
00
121 0 my $mol = $mol_class->new;
122 0 my $is_macro = $mol->isa('Chemistry::MacroMol');
123 0 ▶ $domain = $mol unless $is_macro;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_macro 0 0 0
124  
125 0 local $_;
126 0 my $curr_residue = 0;
127 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
128 0 ▶ if (/^TER/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^TER/) { } 0 0 0
elsif (/^(?:HETATM|ATOM)/) { } 0 0 0
elsif (/^ENDMDL/) { } 0 0 0
129 # TODO read separated molecules?
130 } elsif (/^(?:HETATM|ATOM)/) {
131 0 my ($atom_n, $symbol, $suff, $res_name, $ins_code, $chain_id,
132 $seq_n, $x, $y, $z
133 ) = unpack "x6A5x1A2A2x1A3A1A1A4x4A8A8A8", $_;
134 #print "S:$symbol; N:$name; x:$x; y:$y; z:$z\n";
135 0 $seq_n *= 1;
136 0 ▶ if (!$domain || $seq_n != $curr_residue) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $domain or $seq_n != $curr_residue) 0 0 0
Condition0%0/3
not $domain or $seq_n != $curr_residue
l!l&&r!l&&!r
000
MC/DC0%0/2
not $domain or $seq_n != $curr_residue
not $domain $seq_n != $curr_residue
Truth table0%0/3
not(A) or B
A= not $domainB= $seq_n != $curr_residue
ABresult
1X1
011
000
137 # new residue
138 0 ▶ if ($is_macro) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_macro) 0 0 0
139 0 $domain = Chemistry::Domain->new(
140 parent => $mol, name => "$res_name$seq_n",
141 type => $res_name);
142 0 $mol->add_domain($domain);
143 0 weaken $domain;
144 0 $domain->attr('pdb/sequence_number', $seq_n);
145 0 $domain->attr('pdb/chain_id', $chain_id);
146 0 $domain->attr('pdb/insertion_code', $ins_code);
147 }
148 0 $curr_residue = $seq_n;
149 }
150 0 my $atom_name = $symbol.$suff;
151 0 $atom_name =~ s/ //g;
152 0 $symbol =~ s/\d//g;
153 0 my $a = $domain->new_atom(
154 symbol => $symbol,
155 coords => [$x, $y, $z],
156 name => $atom_name,
157 );
158 0 $a->attr('pdb/residue_name', "$res_name$seq_n");
159 0 $a->attr('pdb/serial_number', $atom_n*1);
160 0 $a->attr('pdb/residue', $domain);
161 } elsif (/^ENDMDL/) {
162 0 return $mol;
163 }
164 }
165 0 return $mol;
166 }
167  
168  
169 0 ▶sub name_is {
Subroutine name_is: not called CC=1 SCAR=6.9
Pod documented
170 0 my ($class, $fname) = @_;
171 0 $fname =~ /\.pdb$/i;
172 }
173  
174 0 ▶sub file_is {
Subroutine file_is: not called CC=6 SCAR=37.4
Pod documented
175 0 my ($class, $fname) = @_;
176
177 0 ▶ return 1 if $fname =~ /\.pdb$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $fname =~ /\.pdb$/i 0 0 0
178  
179 0 ▶ open F, $fname or croak "Could not open file $fname";
Branch: 1 branch
BranchTrueFalseTotal
open F, $fname or croak("Could not open file $fname") 0 0 0
180
181 0 ▶ while (<F>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 0 0 0
182 0 ▶ if (/^ATOM / or /^HETATM/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^ATOM / or /^HETATM/) 0 0 0
Condition0%0/3
/^ATOM / or /^HETATM/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^ATOM / or /^HETATM/
/^ATOM / /^HETATM/
Truth table0%0/3
A or B
A= /^ATOM /B= /^HETATM/
ABresult
1X1
011
000
183 0 close F;
184 0 return 1;
185 }
186 }
187  
188 0 return 0;
189 }
190  
191 0 ▶sub write_string {
Subroutine write_string: not called CC=9 SCAR=42.8
Pod documented
192 0 my ($class, $mol, %opts) = @_;
193 0 my $ret = '';
194 0 ▶ if ($mol->isa("Chemistry::MacroMol")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mol->isa('Chemistry::MacroMol')) { } 0 0 0
195 0 my $i = 1;
196 0 my $j = 1;
197 0 for my $res ($mol->domains) {
198 0 my $seq_n = $res->attr("pdb/sequence_number");
199 0 ▶ $seq_n = $j++ unless defined $seq_n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seq_n 0 0 0
200 0 ▶ my $res_name = substr($res->name, 0, 3) || "UNK";
Condition0%0/2
substr($res->name, 0, 3) || 'UNK'
l!l
00
MC/DC0%0/1
substr($res->name, 0, 3) || 'UNK'
substr($res->name, 0, 3)
Truth table0%0/2
A
A= substr($res->name, 0, 3)
Aresult
11
00
201  
202 0 for my $atom ($res->atoms) {
203 0 my $serial_n = $res->attr("pdb/serial_number");
204 0 ▶ $serial_n = $i++ unless defined $serial_n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $serial_n 0 0 0
205 0 my @coords = $atom->coords->array;
206  
207 # make atom name
208 9 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
209 0 my $symbol = substr $atom->symbol, 0, 2;
210 0 ▶ my $atom_name = $atom->name || $symbol;
Condition0%0/3
$atom->name || $symbol
l!l&&r!l&&!r
000
MC/DC0%0/2
$atom->name || $symbol
$atom->name $symbol
Truth table0%0/3
A || B
A= $atom->nameB= $symbol
ABresult
1X1
011
000
211 0 $atom_name =~ /^(\dH|$symbol)(.{0,2})/;
212 #print "NAME: '$atom_name' ($1,$2); SYMBOL: '$symbol'\n";
213 0 $atom_name = sprintf "%2s%-2s", $1, $2;
214 0 $ret .= sprintf "ATOM %5d %4s %-3s %4d %8.3f%8.3f%8.3f\n",
215 $serial_n, $atom_name, $res_name, $seq_n, @coords;
216 }
217 }
218 } else {
219 0 my $i = 1;
220 0 for my $atom ($mol->atoms) {
221 0 my @coords = $atom->coords->array;
222 0 $ret .= sprintf "HETATM%5d %2s UNK 1 %8.3f%8.3f%8.3f\n",
223 $i++, $atom->symbol, @coords;
224 }
225 }
226 0 $ret .= "TER\nEND\n";
227 0 $ret;
228 }
229  
230 9 1;
231  
232 =head1 VERSION
233  
234 0.21
235  
236 =head1 SEE ALSO
237  
238 L<Chemistry::MacroMol>, L<Chemistry::Mol>, L<Chemistry::File>,
239 L<http://www.perlmol.org/>.
240  
241 The PDB format description at
242 L<http://www.rcsb.org/pdb/docs/format/pdbguide2.2/guide2.2_frame.html>
243  
244 There is another PDB reader in Perl, as part of the BioPerl project:
245 L<Bio::Structure::IO::pdb>.
246  
247 =head1 AUTHOR
248  
249 Ivan Tubert-Brohman <itub@cpan.org>
250  
251 =head1 COPYRIGHT
252  
253 Copyright (c) 2005 Ivan Tubert-Brohman. All rights reserved. This program is
254 free software; you can redistribute it and/or modify it under the same terms as
255 Perl itself.
256  
257 =cut
258  
« blib/lib/Chemistry/File/SLN.pm ↑ Summary blib/lib/Statistics/Regression.pm »