Chem-Structure-Parser

 view release on metacpan or  search on metacpan

lib/Chem/Structure/Parser.pm  view on Meta::CPAN

  <td>—</td>
  <td>hash reference: what a chain of the second and later structures is called in the first</td>
</tr>
<tr>
  <td><code>detail</code></td>
  <td>0</td>
  <td>return the hash rather than the one number</td>
</tr>
</tbody>
</table>

=end html



C<model>, C<altloc>, C<hydrogens>, C<waters>, C<hetatm>, C<chains> and C<format> are
C<structure_info()>'s own and mean the same thing here; they apply to the
arguments that are file names. C<chains> also applies to a structure already
read, as a filter over the chains it has.

Reading a file without its hydrogens and selecting the heavy atoms of one that
has them are the same answer over the same atoms — C<t/rmsd.t> asserts it — so
either will do.

=head3 Against gemmi and Biopython

The superposition is Theobald's quaternion characteristic polynomial (Theobald,
D L (2005) I<Acta Cryst> A61:478), as its reference implementation C<qcprot.c>
writes it (Liu, Agrafiotis and Theobald (2010) I<J Comput Chem> 31:1561) and as
Biopython 1.85 ships it in C<Bio/PDB/qcprot.py>.

The deviation itself is not read off the eigenvalue, which is what makes QCP
fast, and that is deliberate. C<sqrt(2|E0 - L|/n)> subtracts two numbers that
agree in as many figures as the two structures do, and two structures being
nearly the same is the ordinary case: models 24 and 25 of 1JM4 have identical
coordinates, and gemmi 0.7.5 — which takes that route — answers 8.6e-07 A for
them where this answers 0. Here the rotation is formed and the deviation
measured with it, which costs one more pass over the paired atoms and has no
cancellation in it anywhere.

Against gemmi's C<superpose_positions> over every pair of models of the 40 NMR
entries in the first two thousand files of PDBbind v2020 — 5,598 superpositions
— the largest relative difference is 5.65e-12 and the median 1.43e-14. Against
Biopython's C<SVDSuperimposer> the two agree to every figure either prints.

Biopython's C<QCPSuperimposer> is the exception and does not agree with any of
the three: over the 20 models of 2LL7 it reports 3.5022 A where gemmi,
C<SVDSuperimposer> and this module all report 3.6090 A. Its Newton-Raphson
convergence test lost the absolute value C<qcprot.c> has around it, so it stops
on the first iteration and reads the RMSD off a barely-improved starting guess.
Measuring with the rotation it returns itself gives 3.60899. C<t/rmsd.t> says so
in its header, so that the next person to compare against it knows what they
are looking at.

Atoms that lie on one line are the one shape where the three references part
company with QCP's own arithmetic. A line can be turned about itself freely,
the eigenvalue the rotation comes from is a repeated one, and the cofactors
QCP takes the rotation from all vanish; here the rotation is then found by
Jacobi rotations of the 4x4 matrix instead, and the answer is
C<SVDSuperimposer>'s — 0 for three carbons along one axis against the same
three along another, where gemmi's C<superpose_positions> answers NaN. Before
0.039 this module took the identity rotation there and answered 1.732 A.

=head2 convert

 convert('2mnz.cif', '2mnz.pdb');               # mmCIF to PDB, by the names
 convert('1a22.pdb', '1a22.cif.gz');            # PDB to mmCIF, gzipped

 my $r = convert('2ll7.pdb', \my $text, to => 'mmcif', model => 1, hydrogens => 0);
 print "$r->{atoms} atoms written, $r->{rounded} numbers rounded\n";

A structure written out in the other format — or in its own, with the options
applied. The first argument is the structure: a file name, which may be
C<.gz> or C<.bz2>, or a reference to its text. The second is where it goes: a
file name, which is gzipped or bzipped when its name says so, a file handle, or
a reference to a scalar to hold the text. Each end's format comes from its name
as C<structure_info()> works one out, or from C<from> and C<to>; a handle or a
scalar has no name, so writing to one needs C<to>.

The coordinates are converted in C, by the same reader C<structure_info()> uses:
it hands each atom it keeps to a writer for the other format rather than
building it. So the options mean what they mean to C<structure_info()>, an
element the file does not give is worked out the same way, and nothing is kept
per atom but the text written — converting the largest entry in PDBbind v2020
takes a third of a second and the memory of the file and its conversion, where
reading it with C<structure_info()> takes a second and half a gigabyte (see I<What
it costs>, below).

B<A converted file reads back as the structure it was read from.> Every chain,
residue, atom, model and count C<structure_info()> finds in it compares equal,
C<is_deeply>, with what it found in the original — C<t/convert.t> holds every
structure in C<t/data> to that by every path, there and back, and C<t/real_cif.t>
a spread of real entries. And another reader agrees: gemmi reads every atom of
the converted fixtures as it read the originals, which C<t/oracle.t> checks, and
read every one of 63 PDBbind entries the same from its own mmCIF, from the PDB
this made of that, and from the mmCIF this made of it again.

=head3 What goes across

Every atom of every model, ATOM and HETATM, with every alternate location, its
occupancy, B-factor, element and charge; the anisotropic displacements, as
ANISOU records or the C<_atom_site_anisotrop> table; and the models, as MODEL
records or C<pdbx_PDB_model_num>. Of the header, the facts both formats have a
place for, read and written through the keys C<structure_info()> files them under:



=begin html

<table>
<thead>
<tr>
  <th><code>$info</code> key</th>
  <th>PDB record</th>
  <th>mmCIF category</th>
</tr>
</thead>
<tbody>
<tr>
  <td><code>header</code> (id, classification, deposition date)</td>
  <td><code>HEADER</code></td>

lib/Chem/Structure/Parser.pm  view on Meta::CPAN

  <td>pepPriML's <code>features.pdb.20260806.py</code></td>
</tr>
</tbody>
</table>

=end html



mdtraj works in nanometres and float32; this module works in angstrom and NV,
which is what the two file formats are written in and what the rest of the
module already returns. The formulae are the same ones, so the answers agree to
the width of a float32: run in float64, mdtraj's own Shrake-Rupley loop and this
one give the same surface for every atom of every structure in C<t/data> to nine
digits, and mdtraj as it ships differs on four atoms of 620, by one sphere point
each — a point sitting within a float32 ulp of a neighbouring atom's surface is
accessible at one width and covered at the other.

The secondary structure is the exception, and computes in float32 and nanometre
as mdtraj does. It is not a number but a letter, and the letter turns on two
comparisons against constants — an energy below -0.5 kcal/mol is a hydrogen
bond, a CA-to-CA separation under 0.9 nm is worth testing at all. Computing
those wider does not make them better; it makes them different, and one bond
either way is worth several residues' letters.

Three of these are deliberately not what the reference does, and each is argued
where it is written down. mdtraj computes a torsion angle, and places a Kabsch–
Sander amide hydrogen, between whichever residues are next to each other in the
file — across a chain break, where the chemistry never joined them; here the two
must be peptide-bonded, or phosphodiester-bonded, first. mdtraj's C<Topology.create_disulfide_bonds()>
compares angstrom coordinates against a nanometre cutoff and so finds no
disulfide in any file; the rule it documents is the one implemented here. And
the face-to-face pi-stacking distance, above.

Two more are deliberately not mdtraj's: C<rg_mass>, and
C<compute_rg(traj, masses=m)> weights the distances by mass but still measures
them from the geometric centroid; C<rg_mass> measures from the centre of mass,
which is what the quantity means. C<rg> takes mdtraj's default of equal weights,
where the two centres are the same point and the two answers agree exactly.

The secondary structure is not one of the three, and is the reason the hydrogen
bonds are computed twice: C<structure_dssp()> wants mdtraj's table, bonds across
chain breaks and all, because that is the table mdtraj's answer is defined on,
and C<structure_hbonds()> reports the other one. Both are written up under
C<structure_dssp> above.

=head1 What is parsed in C, and why

The C side does one pass over the bytes. It splits ATOM/HETATM records into
their fields, marks where each residue begins and ends, sums what has to be
summed over every atom — the element tally, the bounding box, the B-factors,
each residue's centre — and groups every other record by record name for Perl
to take apart. Residue name lookup, three letters to one letter and the amino
acid/nucleotide/water question, is a switch on three packed bytes, and one
table serves C<aa3to1()>, C<res1()> and C<res_type()> so the three can never
disagree.

The sums are over the finite coordinates and B-factors only. A field that says
C<nan> or C<inf> reads back as that number in the atom, as the file has it, and is
left out of the box, the centres and the B-factor range wherever in the file
it falls; in the physical properties an atom at NaN is nobody's neighbour, so
it makes no contact, no hydrogen bond and no torsion.

When atoms are wanted the parse builds the atom hashes itself, rather than
handing back columns for Perl to rebuild them from; building every atom twice
cost more than everything else in the read put together. When they are not
wanted — C<< atoms =E<gt> 0 >> — it builds none, and the Perl that follows walks
residues rather than atoms.

Everything else is Perl. The header records are irregular, they are a few
dozen lines per file rather than hundreds of thousands, and they are where the
next surprise will turn up; none of that is worth writing in C.

The mmCIF reader is a second pass written to the same division. A PDB file is
fixed columns and an mmCIF file is tag/value pairs and C<loop_> tables, so none
of the column arithmetic carries over and the tokenizer — quoting, semicolon
text fields, comments, the two spellings of null — is its own code. What it is
not is a second answer: it fills in the same output, the same column arrays and
residue boundaries and counts, so everything downstream of it, in C and in
Perl, is written once. C<_atom_site> goes through that path; every other
category is handed to Perl as tags and loops, which is the same place the line
between the two languages falls for PDB.

C<convert()> is the same pass with a writer on the end of it. The reader for the
input's format reads and filters every atom as it does for C<structure_info()>,
and hands each one it keeps to a writer for the output's format instead of
building it into the atom hashes — so a conversion holds the file and the text
it becomes, and nothing per atom besides. The header is converted in Perl, out
of what the header reader makes of it, for the reason it is read in Perl.

On 200 structures from PDBbind v2020, the parse runs at about 2.8 times the
speed of the same parse written in Perl. C<structure_info()> as a whole comes
out close to a pure-Perl reader that gathers the same statistics, while also
reading the headers, SEQRES, the gaps, the chain types and the ligands; see
C<benchmark.pl>, which measures all of it rather than asserting any of it.

=head1 Author

David E. Condon L<mailto:dec986@gmail.com>

=head1 COPYRIGHT AND LICENSE

This library is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.

=head1 Thanks

Most of this was done with the help of Claude models, which was paid for by the University of Idaho's IMCI.



( run in 1.431 second using v1.01-cache-2.11-cpan-60884a8df76 )