HackaMol/Roles/ReadPdbRole.pm

filter: Statementstmt 96.0% 98 / 102 Branchbran 92.5% 37 / 40 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 8 / 8 Podpod 33.3% 1 / 3 totaltotal 93.2% 151 / 162 SCAR 33.4
CC
28
Cov
95%
read_pdb_atoms
26.2
read_pdb_header
3.0
BEGIN
1.0
CRAP 28.1
« HackaMol/Roles/PhysVecMVRRole.pm ↑ Summary HackaMol/Roles/ReadPdbqtRole.pm »
1 package HackaMol::Roles::ReadPdbRole;
2 11 $HackaMol::Roles::ReadPdbRole::VERSION = '0.051';
3 # ABSTRACT: Read files with molecular information
4 11 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use HackaMol::PeriodicTable qw(_element_name _trim _qstring_num);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Math::Vector::Real;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 requires 'readline_func';
10  
11 sub read_pdb_parts{
12 1 ▶ my $self = shift;
Subroutine read_pdb_parts: called CC=1 SCAR=0.0
Pod undocumented
13 1 my $fh = shift;
14  
15 1 my $header = $self->read_pdb_header( $fh );
16 1 my ($atoms,$model_ids) = $self->read_pdb_atoms( $fh );
17  
18 1 return ($header,$atoms,$model_ids);
19  
20 }
21  
22 sub read_pdb_header{
23 1 ▶ my $self = shift;
Subroutine read_pdb_header: called CC=3 SCAR=11.0
Pod undocumented
24 1 my $fh = shift;
25 1 my $header;
26 my $line;
27 1 ▶ while($line = <$fh>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 3 0 3
28 3 ▶ if ($line =~ m/^(?:MODEL|ATOM|HETATM)\s/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(?:MODEL|ATOM|HETATM)\s/) { } 1 2 3
29 1 seek($fh, -length($line),1); # rewind back and end
30 1 last;
31 }
32 else{
33 2 $header .= $line;
34 }
35 }
36 1 return ($header);
37 }
38  
39 sub read_pdb_atoms {
40  
41 #read pdb file and generate list of Atom objects
42 15 ▶ my $self = shift;
Subroutine read_pdb_atoms: called CC=26 SCAR=32.6
Pod documented
43  
44 15 my $fh = shift;
45 #my $file = shift;
46 #my $fh = FileHandle->new("<$file") or croak "unable to open $file";
47  
48 15 my @atoms;
49 my @model_ids;
50 15 my ( $n, $t ) = ( 0, 0 );
51 15 my $q_tbad = 0;
52 15 my $something_dirty = 0;
53 15 my $t0_atom_count = 0;
54 15 my $t_atom_count = 0;
55 15 my $pdb_model_id;
56  
57 15 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 8525 15 8540
58 8525 ▶ if($self->has_readline_func){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_readline_func) 921 7604 8525
59 921 ▶ next if $self->readline_func->($_) eq 'PDB_SKIP';
Branch: 1 branch
BranchTrueFalseTotal
if $self->readline_func->($_) eq "PDB_SKIP" 855 66 921
60 }
61 7670 ▶ if (/^(?:MODEL\s+(\d+))/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^(?:MODEL\s+(\d+))/) { } 19 7651 7670
elsif (/^(?:ENDMDL)/) { } 19 7632 7651
elsif (/^(?:HETATM|ATOM)/) { } 7326 306 7632
62  
63 19 $n = 0;
64 19 $q_tbad = 0; # flag a bad model and never read again!
65 19 $t_atom_count = 0 ;
66 19 $pdb_model_id = $1;
67 19 $model_ids[$t] = $pdb_model_id;
68 }
69 elsif (/^(?:ENDMDL)/) {
70 # delete coords if the number of atoms on t is != to that at start
71 19 ▶ if ($t){
Branch: 1 branch
BranchTrueFalseTotal
if ($t) 9 10 19
72 9 ▶ if ($t_atom_count != $t0_atom_count){
Branch: 1 branch
BranchTrueFalseTotal
if ($t_atom_count != $t0_atom_count) 0 9 9
73 0 my $carp_message =
74 "BAD t->$t PDB atom list length changed from $t0_atom_count to $t_atom_count: ignoring model $t";
75 0 carp $carp_message;
76 0 $_->delete_coords($t) foreach @atoms;
77 0 $t--;
78 }
79 }
80 19 $t++;
81 }
82 elsif (/^(?:HETATM|ATOM)/) {
83 7326 ▶ next if $q_tbad;
Branch: 1 branch
BranchTrueFalseTotal
if $q_tbad 69 7257 7326
84 my (
85 7257 $record_name, $serial, $name, $altloc,
86 $resName, $chainID, $resSeq, $icod,
87 $x, $y, $z, $occ,
88 $B, $segID, $element, $charge
89 ) = unpack "A6A5x1A4A1A3x1A1A4A1x3A8A8A8A6A6x6A4A2A2", $_ . (" " x 12); # padded out to accommodate truncated pdbs
90  
91 7257 ▶ if ( $charge =~ m/\d/ ) { $charge = _qstring_num($charge) }
Branch: 1 branch
BranchTrueFalseTotal
if ($charge =~ /\d/) { } 7 7250 7257
92 7250 else { $charge = 0 }
93  
94 7257 ▶ if ( $chainID =~ m/\w/ ) { $chainID = _trim($chainID) }
Branch: 1 branch
BranchTrueFalseTotal
if ($chainID =~ /\w/) { } 7242 15 7257
95 15 else { $chainID = ' ' }
96  
97 7257 $name = _trim($name);
98 7257 $resName = _trim($resName);
99 7257 $resSeq = _trim($resSeq);
100  
101 #$resSeq = 0 if ( $resSeq < 0 );
102 7257 $serial = _trim($serial);
103 7257 $segID = _trim($segID);
104  
105 7257 $element = ucfirst( lc( _trim($element) ) );
106 7257 my $qdirt = 0;
107 7257 ▶ ( $element, $qdirt ) = _element_name($name)
Branch: 1 branch
BranchTrueFalseTotal
unless $element =~ /\w+/ 72 7185 7257
108 unless ( $element =~ /\w+/ );
109 7257 ▶ $something_dirty++ if ($qdirt);
Branch: 1 branch
BranchTrueFalseTotal
if $qdirt 4 7253 7257
110 7257 my $xyz = V( $x, $y, $z );
111  
112 7257 ▶ if ( $t == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 0) { } 4863 2394 7257
113 4863 $atoms[$n] = HackaMol::Atom->new(
114 name => $name,
115 record_name => $record_name,
116 serial => $serial,
117 chain => $chainID,
118 symbol => $element,
119 charges => [$charge],
120 coords => [$xyz],
121 occ => $occ * 1,
122 bfact => $B * 1,
123 resname => $resName,
124 resid => $resSeq,
125 icode => $icod,
126 segid => $segID,
127 altloc => $altloc,
128 );
129 4863 ▶ $atoms[$n]->is_dirty($qdirt) unless $atoms[$n]->is_dirty;
Branch: 1 branch
BranchTrueFalseTotal
unless $atoms[$n]->is_dirty 4863 0 4863
130 4863 $t0_atom_count++;
131 }
132 else {
133 # croak condition if atom changes between models
134 # does not catch case were number of atoms shrinks!
135 2394 ▶ if ( $n > $#atoms or $name ne $atoms[$n]->name
Branch: 1 branch
BranchTrueFalseTotal
if ($n > $#atoms or $name ne $atoms[$n]->name or $element ne $atoms[$n]->symbol) 2 2392 2394
Condition83%5/6
$n > $#atoms or $name ne $atoms[$n]->name or $element ne $atoms[$n]->symbol
l!l&&r!l&&!r
112392
$n > $#atoms or $name ne $atoms[$n]->name
l!l&&r!l&&!r
012393
MC/DC66%2/3
$n > $#atoms or $name ne $atoms[$n]->name or $element ne $atoms[$n]->symbol
$n > $#atoms $name ne $atoms[$n]->name $element ne $atoms[$n]->symbol
Truth table75%3/4
A or B or C
A= $n > $#atomsB= $name ne $atoms[$n]->nameC= $element ne $atoms[$n]->symbol
ABCresult
1XX1
01X1
0011
0000
136 or $element ne $atoms[$n]->symbol )
137 {
138 2 my $carp_message =
139 "BAD t->$t PDB Atom $n "
140 . "serial $serial resname $resName "
141 . "has changed";
142 2 carp $carp_message;
143 2 $q_tbad = $t; # this is a bad model!
144 # wipe out all the coords prior
145 2 $atoms[$_]->delete_coords($t) foreach 0 .. $n - 1;
146 2 $t--;
147 2 next;
148 }
149 2392 $atoms[$n]->set_coords( $t, $xyz );
150 2392 $t_atom_count++;
151 }
152 7255 $n++;
153 }
154 }
155  
156 # set iatom to track the array. diff from serial which refers to pdb
157 15 $atoms[$_]->iatom($_) foreach ( 0 .. $#atoms );
158 15 ▶ if ($something_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($something_dirty) 2 13 15
159 2 ▶ if ( $self->hush_read <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hush_read <= 0) 1 1 2
160 1 my $message = "MolReadRole> found $something_dirty dirty atoms. ";
161 1 $message .= "Check symbols and lookup names PeriodicTable.pm:";
162 1 my @sprintf;
163 1 foreach my $atom (grep {$_->is_dirty} @atoms){
164 2 push @sprintf, sprintf(" DIRTY: index %s name %s element %s %10.3f %10.3f %10.3f;", $atom->iatom, $atom->name, $atom->symbol, @{$atom->xyz});
165 }
166 1 $message .= $_ foreach @sprintf;
167 1 carp $message;
168 }
169 }
170 15 return (\@atoms,\@model_ids);
171 }
172  
173 11 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
174  
175 11 1;
176  
177 __END__
178  
179 =pod
180  
181 =head1 NAME
182  
183 HackaMol::Roles::ReadPdbRole - Read files with molecular information
184  
185 =head1 VERSION
186  
187 version 0.051
188  
189 =head1 SYNOPSIS
190  
191 my @atoms = HackaMol->new
192 ->read_pdb_atoms("some.pdb");
193  
194 =head1 DESCRIPTION
195  
196 The HackaMol::Roles::ReadPdbRole provides read_pdb_atoms reading protein database files.
197  
198 =head1 METHODS
199  
200 =head2 read_pdb_atoms
201  
202 One argument: the filename
203 Returns a list of HackaMol::Atom objects.
204  
205 =head1 Additional information
206  
207 According to the PDB specification, the element symbol should be present in columns
208 77-78. The element is often ommitted by programs, such as charmm, that can write
209 pdbs because it makes the file larger, and the information is accessible somewhere
210 else (protein structure file). Unfortunately, other programs require the information.
211 HackaMol::MolReadRole, uses HackaMol::PeriodicTable to map common names to the element
212 (e.g. POT => 'K'). read_pdb_atoms will carp if the name is not in the hash, and then
213 set the element to the first letter of the name. If you see one of these messages for
214 a common atom, please submit an issue or pull request so the atom can be added!
215  
216 =head1 SEE ALSO
217  
218 =over 4
219  
220 =item *
221  
222 L<HackaMol>
223  
224 =item *
225  
226 L<HackaMol::Atom>
227  
228 =item *
229  
230 L<HackaMol::Roles::MolReadRole>
231  
232 =item *
233  
234 L<Protein Data Bank|http://pdb.org>
235  
236 =back
237  
238 =head1 AUTHOR
239  
240 Demian Riccardi <demianriccardi@gmail.com>
241  
242 =head1 COPYRIGHT AND LICENSE
243  
244 This software is copyright (c) 2017 by Demian Riccardi.
245  
246 This is free software; you can redistribute it and/or modify it under
247 the same terms as the Perl 5 programming language system itself.
248  
249 =cut
« HackaMol/Roles/PhysVecMVRRole.pm ↑ Summary HackaMol/Roles/ReadPdbqtRole.pm »