Chem-Structure-Parser
view release on metacpan or search on metacpan
# the charge columns, 79-80, which in a file old enough to keep its entry id in
# columns 73-80 hold the tail of that id instead. A field that is not a digit
# and a sign is not a charge, and reads as the empty field a blank column would
# have given -- pdb1gdr.ent ends its records '1GDR 109', where '09' is two
# digits and not a charge either.
#--------
{
# columns 1-66 are the record up to the B-factor, 67-76 are blank, 77-78 are
# the element and 79-80 the charge
my $head = 'ATOM 1 CA ALA A 1 10.000 10.000 10.000 1.00 20.00';
is(length $head, 66, 'the record prefix ends where the B-factor does');
my $rec = sub { $head . (' ' x 10) . sprintf('%2s%2s', @_) };
is(length $rec->(' C', 'DR'), 80, 'and the whole record runs to column 80');
my $q = Chem::Structure::Parser::_parse_string($rec->(' C', 'DR') . "\n", {});
is($q->{charge}[0], '', 'letters in the charge columns are not a charge');
is($q->{element}[0], 'C', 'and the element beside them is still read');
my $r = Chem::Structure::Parser::_parse_string($rec->(' C', '1+') . "\n", {});
is($r->{charge}[0], '1+', 'a digit and a sign is');
my $s = Chem::Structure::Parser::_parse_string($rec->(' C', '09') . "\n", {});
is($s->{charge}[0], '', 'and two digits, as pdb1gdr.ent writes its line numbers, are not');
}
#--------
# a residue number that is not a number. Four columns hold at most 9999, and a
# file written by something that overflowed them puts '****' there, the same
# way an overflowed serial number becomes '*****'. It has to read as undef --
# the file does not say which residue this is -- rather than as a zero that
# would merge every such residue with a real residue 0.
#--------
{
# built with sprintf rather than typed: the fields either side of this one
# are one character wide and a hand-typed record slides them
my $rec = sprintf('ATOM %5s %-4s%1s%3s %1s%4s%1s %8s%8s%8s 1.00 20.00 C',
1, ' CA ', '', 'ALA', 'A', '****', '', 10, 10, 10);
is(length $rec, 78, 'the record is as long as a record with an element column');
my $q = Chem::Structure::Parser::_parse_string("$rec\n", {});
is($q->{resseq}[0], undef, 'a residue number of **** is undef, not zero');
is($q->{icode}[0], '', 'and it has not been read as an insertion code');
is($q->{resname}[0], 'ALA', 'the fields around it are unharmed');
is($q->{x}[0], 10, 'including the coordinates');
my $i = structure_info_string("$rec\n");
is_deeply($i->{chains}{A}{residue_order}, [ '' ],
'the residue it builds is keyed by the insertion code alone');
is($i->{chains}{A}{residues}{''}{number}, undef, 'and has no number to give');
is($i->{chains}{A}{n_gaps}, 0, 'a chain whose residues have no numbers has no gaps');
}
#--------
# arguments
#--------
throws_ok { Chem::Structure::Parser::_parse_string(undef) } qr/undefined/,
'_parse_string: undefined text dies';
throws_ok { Chem::Structure::Parser::_parse_file(undef) } qr/undefined/,
'_parse_file: an undefined file name dies';
throws_ok { Chem::Structure::Parser::_parse_file('t/data/does.not.exist.pdb') } qr/cannot read/,
'_parse_file: a missing file dies, and says so';
throws_ok { Chem::Structure::Parser::_parse_string('', 'not a hashref') } qr/hash reference/,
'_parse_string: options must be a hash reference';
# A regular file is read into a buffer of exactly its size, which stat() gives;
# a named pipe has no size to give, and is read in growing chunks instead. The
# two must read the same. mkfifo() is POSIX, and the fork below is the writer
# a pipe needs, so this is only possible where both are.
SKIP: {
my $dir = File::Temp::tempdir(CLEANUP => 1);
my $fifo = "$dir/mini.fifo";
skip 'no named pipes here', 2
unless $^O ne 'MSWin32' && eval { require POSIX; POSIX::mkfifo($fifo, 0600) };
my $pid = fork;
skip 'no fork here', 2 unless defined $pid;
if (!$pid) {
open my $in, '<', 't/data/mini.pdb' or POSIX::_exit(1);
open my $out, '>', $fifo or POSIX::_exit(1);
local $/;
print {$out} <$in>;
close $out;
POSIX::_exit(0);
}
my $p = Chem::Structure::Parser::_parse_file($fifo);
waitpid $pid, 0;
my $f = Chem::Structure::Parser::_parse_file('t/data/mini.pdb');
delete $_->{file} for $p, $f;
is($p->{n_atoms}, $f->{n_atoms}, '_parse_file: a named pipe is read to the end');
is_deeply($p, $f, 'and reads the same as the file does');
}
#--------
# the answer does not depend on the locale.
#
# perl calls setlocale(LC_ALL, "") at startup on any USE_LOCALE build, so
# LC_CTYPE in an XS module is whatever the caller's environment says -- the
# NetBSD smoker that reported 0.031 ran under en_US.UTF-8. Everything this
# reader classifies is ASCII by definition (an element symbol, a residue name,
# a CIF keyword), so the answer must not move when the locale does: isalpha()
# in a Latin-1 locale calls an accented byte a letter, and tolower('I') in a
# Turkish one is not 'i', which would stop an mmCIF file written in capitals
# from being recognised at all. Parser.xs uses perl's ASCII-only toUPPER(),
# toLOWER(), isALPHA() and isDIGIT() rather than <ctype.h> for exactly that.
#
# Which locales exist is the machine's business, so the loop asks for a spread
# and tests the ones it gets; "C" is always one of them, so this never becomes
# a test that runs nowhere.
#--------
SKIP: {
eval { require POSIX; 1 } or skip 'POSIX is not available', 1;
my $lc = POSIX::setlocale(POSIX::LC_CTYPE());
my $pdb = <<'PDB';
ATOM 1 N MET A 1 11.104 13.207 10.000 1.00 15.00 N
HETATM 2 ZN ZN A 202 45.000 22.000 22.000 1.00 20.00 ZN
ATOM 3 HG11ILE A 2 11.000 13.000 10.000 1.00 15.00
PDB
# uppercase tags and keywords: legal mmCIF, and what a case-insensitive
# compare through tolower() gets wrong in a Turkish locale
my @tag = qw(group_PDB id type_symbol auth_atom_id auth_comp_id
auth_asym_id auth_seq_id Cartn_x Cartn_y Cartn_z);
my $cif = "DATA_X\nLOOP_\n"
. join('', map { "_ATOM_SITE.$_\n" } @tag)
. "ATOM 1 N N MET A 1 11.104 13.207 10.000\n";
my $want_pdb = structure_info_string($pdb);
my $want_cif = structure_info_string($cif, format => 'mmcif');
( run in 0.640 second using v1.01-cache-2.11-cpan-eb134014566 )