Chem-Structure-Parser
view release on metacpan or search on metacpan
t/structure.t view on Meta::CPAN
my $sum = 0;
$sum += $_ for values %{ $info->{stats}{elements} };
is($sum, $info->{stats}{n_atoms}, 'the element counts add up to n_atoms');
for my $cid (@{ $info->{chain_order} }) {
my $c = $info->{chains}{$cid};
my $n = 0;
$n += $_ for values %{ $c->{elements} };
is($n, $c->{n_atoms}, "chain $cid: its element counts add up to its n_atoms");
}
# every count is a non-negative whole number, not a string that looks like one
my @bad = grep { !/\A[0-9]+\z/ } values %{ $info->{stats}{elements} };
is_deeply(\@bad, [], 'every count is an unsigned integer');
is($info->{chains}{A}{elements}{Zn}, 1, 'the zinc is tallied against the chain it sits in');
ok(!exists $info->{chains}{B}{elements}{Zn}, 'and against no other chain');
is_deeply([ sort keys %{ $info->{chains}{B}{elements} } ], [ qw(C O P) ],
'a chain tallies only the elements it holds');
}
ok($info->{stats}{bfactor}{min} <= $info->{stats}{bfactor}{mean}, 'B-factor min <= mean');
ok($info->{stats}{bfactor}{mean} <= $info->{stats}{bfactor}{max}, 'B-factor mean <= max');
ok($info->{stats}{bbox}{xmin} < $info->{stats}{bbox}{xmax}, 'the bounding box has a width');
is(scalar @{ $info->{stats}{center} }, 3, 'and a centre');
#--------
# a file with nothing but coordinates in it
#--------
{
my $bare = structure_info("$data/bare.pdb");
is($bare->{id}, 'BARE', 'with no HEADER, the id falls back to the file name');
is($bare->{chains}{A}{sequence}, 'VK', 'and the sequence is still read');
is($bare->{chains}{A}{seqres}, undef, 'a file with no SEQRES has no seqres sequence');
is($bare->{chains}{A}{residues}{1}{atoms}{CA}{element}, 'C',
'and the elements are worked out from the atom names');
is($bare->{title}, undef, 'a missing TITLE is undef rather than missing');
is_deeply($bare->{keywords}, [], 'and a missing KEYWDS is an empty list');
}
#--------
# the id a file with no HEADER takes from its name. The archive's own spelling,
# pdb1abc.ent, loses its 'pdb'; until 0.039 every name did, so a file called
# pdbfixer_output.pdb came back as FIXER_OUTPUT, and a .pdbx -- a suffix the
# format is read from -- kept its suffix in the id.
#--------
{
my $dir = tempdir(CLEANUP => 1);
open my $in, '<', "$data/bare.pdb" or die "Can't open '$data/bare.pdb' with mode '<': '$!'";
my $text = do { local $/; <$in> };
close $in or die "Can't close '$data/bare.pdb': '$!'";
for my $w ([ 'pdb1abc.ent', '1ABC', "the archive's pdb1abc.ent is 1ABC" ],
[ 'pdbfixer_output.pdb', 'PDBFIXER_OUTPUT', 'and a name that only begins with pdb keeps it' ],
[ '2abc.pdbx', '2ABC', 'a .pdbx is a suffix like the others' ]) {
my ($name, $id, $why) = @$w;
open my $out, '>', "$dir/$name" or die "Can't open '$dir/$name' with mode '>': '$!'";
print {$out} $text;
close $out or die "Can't close '$dir/$name': '$!'";
is(structure_info("$dir/$name", features => 0, format => 'pdb')->{id}, $id, $why);
}
}
#--------
# a coordinate that reads as nan. The bounding box, the centres and the
# B-factor extremes are over the finite values only; they took a NaN in or not
# according to where in the file it fell -- first, and every comparison after
# it failed, so the box was NaN; anywhere later, and it was left out.
#--------
{
my $nan = "ATOM 1 N ALA A 1 nan 5.000 5.000 1.00 nan N\n";
my $real = "ATOM 2 CA ALA A 1 5.000 5.000 5.000 1.00 10.00 C\n";
my $first = structure_info_string($nan . $real, features => 0)->{stats};
my $second = structure_info_string($real . $nan, features => 0)->{stats};
is_deeply([ @{ $first->{bbox} }{qw(xmin xmax)}, $first->{bfactor}{max} ], [ 5, 5, 10 ],
'a NaN on the first atom does not become the box or the B-factor range');
is_deeply($first, $second, 'and where in the file it falls makes no difference');
}
#--------
# an empty file
#--------
{
my $empty = structure_info("$data/empty.pdb");
is_deeply($empty->{chain_order}, [], 'an empty file has no chains');
is($empty->{stats}{n_atoms}, 0, 'and no atoms');
lives_ok { structure_summary($empty) } 'and can still be summarised';
is_deeply(structure_sequences($empty), {}, 'and has no sequences');
}
#--------
# the same file through a string
#--------
{
open my $fh, '<', "$data/mini.pdb" or die $!;
my $text = do { local $/; <$fh> };
close $fh;
my $s = structure_info_string($text);
is($s->{id}, '9XYZ', 'structure_info_string: reads a structure from a string');
is($s->{chains}{A}{sequence}, $info->{chains}{A}{sequence},
'and gets the same sequence as reading the file');
is($s->{file}, undef, 'with no file name to record');
}
#--------
# naming the format, rather than letting it be detected
#--------
is_deeply(structure_info("$data/mini.pdb", format => 'pdb'), $info,
"format => 'pdb': the same answer as letting structure_info() detect it");
done_testing();
( run in 1.219 second using v1.01-cache-2.11-cpan-60884a8df76 )