Chem-Structure-Parser

 view release on metacpan or  search on metacpan

t/properties.t  view on Meta::CPAN

	my $a = structure_features($i);
	my $b = structure_features($i);
	is_deeply($b, $a, 'asking twice gives the same answer');
	my $c = structure_features($i, probe => 2.0);
	cmp_ok($i->{chains}{A}{sasa}, '>', $a->{sasa}{total} / 2,
		'and asking again with a different probe replaces what was stored');
	cmp_ok(abs($i->{chains}{A}{sasa} - $c->{sasa}{total}), '<', 1e-9,
		'with the new figure, not the old one');
}

# ---- an atom with more neighbours than a structure can give it -----------
#
# The surface kernel sorts an atom's neighbours nearest-first so that the scan
# over the sphere points ends sooner, and skips the sort above SASA_SORT_MAX
# neighbours, where it would be quadratic work on a list no real structure
# produces (141 is the longest over a 405-structure spread of PDBbind).  A
# lattice of 729 atoms six tenths of an angstrom apart gives the atom in the
# middle of it 728 neighbours, which is how the skipped path is reached at all
# -- and what is asserted is what holds whichever path ran: the parts add up to
# the whole, and no atom has more surface than a free sphere of its own radius.
{
	my ($pdb, $n) = ('', 0);
	for my $i (0 .. 8) {
		for my $j (0 .. 8) {
			for my $k (0 .. 8) {
				$n++;
				$pdb .= sprintf "ATOM  %5d  CA  ALA A%4d    %8.3f%8.3f%8.3f  1.00  0.00           C\n",
					$n, $n, $i * 0.6, $j * 0.6, $k * 0.6;
			}
		}
	}
	my $i = structure_info_string($pdb);
	my $f = $i->{features}{sasa};
	my ($sum, $over) = (0, 0);
	# carbon's van der Waals radius plus the 1.4 A probe, which is the sphere
	# the points are spread over
	my $free = 4 * atan2(1, 0) * 2 * (1.70 + 1.4) ** 2;
	for my $c (@{ $i->{chain_order} }) {
		for my $rk (@{ $i->{chains}{$c}{residue_order} }) {
			my $r = $i->{chains}{$c}{residues}{$rk};
			for my $an (@{ $r->{atom_order} }) {
				my $a = $r->{atoms}{$an}{sasa};
				$sum += $a;
				$over++ if $a > $free + 1e-9 || $a < 0;
			}
		}
	}
	is($n, 729, 'the lattice is 729 atoms, which is more neighbours than the sort takes');
	# the total is a sum of the same NVs in the same order, so it is exact
	cmp_ok(abs($f->{total} - $sum), '<', 1e-9,
		'the atoms account for the whole surface');
	is($over, 0, 'and none of them has more surface than a free sphere');
}

# ---- a coordinate that is not a number ------------------------------------
# A field that says nan is read as the NV it names, as strtod() reads it, and
# the atom is kept.  One such atom anywhere in a structure used to make the
# neighbour grid's box non-finite, which put every atom into one cell and made
# the whole surface quadratic in the atoms -- the same answer, got the slow way.
# The box is now taken over the finite atoms, and the answer for them is still
# the answer they have without the other atom there: every distance to a NaN
# compares false, so it covers none of anybody's sphere.
{
	my $atoms = <<'PDB';
ATOM      1  N   ALA A   1      10.000  10.000  10.000  1.00 20.00           N
ATOM      2  CA  ALA A   1      11.458  10.000  10.000  1.00 20.00           C
ATOM      3  C   ALA A   1      12.009  11.420  10.000  1.00 20.00           C
PDB
	my $nan = "HETATM    4  O   HOH W   1         nan  10.000  10.000  1.00 20.00           O\n";
	my $plain = structure_info_string($atoms, features => 0);
	my $odd   = structure_info_string($nan . $atoms, features => 0);
	structure_sasa($plain);
	structure_sasa($odd);
	my ($p, $o) = map { $_->{chains}{A}{residues}{1}{atoms} } $plain, $odd;
	is_deeply([ map { $o->{$_}{sasa} } qw(N CA C) ], [ map { $p->{$_}{sasa} } qw(N CA C) ],
		'an atom at NaN changes no other atom\'s surface, even as the first atom of the set');
}

# ...and it is nobody's neighbour anywhere else either.  Every neighbour test
# was written as the test a neighbour fails, `d2 >= cut2', which a NaN distance
# does not fail, so the atom was a contact of every residue near the end cell
# it was put in, at a distance of NaN; the secondary structure's energy took a
# NaN distance for two atoms at one point, the strongest bond there is; and a
# ring torsion through an infinite coordinate came out a NaN that the sugar
# pucker cast to an index into its table of names.
{
	my $i = structure_info_string(<<'PDB');
ATOM      1  CA  ALA A   1       0.000   0.000   0.000  1.00 10.00           C
ATOM      2  CA  ALA A   5      50.000  50.000  50.000  1.00 10.00           C
ATOM      3  CA  ALA B   9         nan   0.000   0.000  1.00 10.00           C
PDB
	is_deeply($i->{features}{contacts}, [], 'an atom at NaN is in no contact');

	open my $fh, '<', "$data/fold.pdb" or die "Can't open '$data/fold.pdb' with mode '<': '$!'";
	my $text = do { local $/; <$fh> };
	close $fh or die "Can't close '$data/fold.pdb': '$!'";
	(my $odd = $text) =~ s/^(ATOM.{9}O   PRO A  13    ).{8}/${1}     nan/m or die 'no PRO A 13 O';
	my $h = structure_hbonds(structure_info_string($odd));
	is_deeply([ grep { $_->{energy} <= -9.9 } @$h ], [],
		'a carbonyl O at NaN makes no hydrogen bond, at the floor or anywhere');
	is_deeply([ grep { $_->{acceptor_residue} eq '13' } @$h ], [],
		'and its residue accepts none');

	open $fh, '<', "$data/rna.pdb" or die "Can't open '$data/rna.pdb' with mode '<': '$!'";
	$text = do { local $/; <$fh> };
	close $fh or die "Can't close '$data/rna.pdb': '$!'";
	(my $inf = $text) =~ s/^(ATOM.{9}C2'.{14}).{8}/${1}     inf/m or die "no C2'";
	my $r = structure_info_string($inf);
	my @res = map { my $c = $r->{chains}{$_}; map { $c->{residues}{$_} } @{ $c->{residue_order} } }
	          @{ $r->{chain_order} };
	is_deeply([ grep { defined $_->{pucker_phase} && $_->{pucker_phase} != $_->{pucker_phase} } @res ], [],
		'a ring atom at infinity gives no pucker phase that is not a number');
	is(scalar(grep { defined $_->{pucker} } @res), 5,
		'and its sugar no pucker, while the other five keep theirs');
}

# One atom far from the rest.  The neighbour grid caps its cells at eight per
# atom, and a box wide enough to take in a zinc at (9999.999, 9999.999,
# 9999.999) met the cap by widening every cell to hundreds of angstroms: 4fqr
# (PDBbind v2020) with that zinc took structure_sasa() from 1.4 s to 29.5 s.
# The box is taken over the bulk of the atoms instead, and the far ones are put
# in its end cells -- which can change no answer, and does not: every surface and
# every contact of fold.pdb is what it is without the zinc, to the bit.
{
	open my $fh, '<', "$data/fold.pdb" or die "Can't open '$data/fold.pdb' with mode '<': '$!'";
	my $text = do { local $/; <$fh> };
	close $fh or die "Can't close '$data/fold.pdb': '$!'";
	(my $far = $text) =~ s/^END\s*\z//m;
	$far .= "HETATM 9999 ZN    ZN Z 999    9999.9999999.9999999.999  1.00 10.00          ZN\n";
	my $plain = structure_info_string($text);
	my $odd   = structure_info_string($far);
	my $surface = sub {
		my ($i) = @_;
		my $c = $i->{chains}{A};
		return [ map { my $r = $c->{residues}{$_}; map { $r->{atoms}{$_}{sasa} } @{ $r->{atom_order} } }
		         @{ $c->{residue_order} } ];
	};
	is_deeply($surface->($odd), $surface->($plain),
		'a far atom changes no surface of the atoms near each other');
	# as a set: a residue's partners are listed in the order the grid's cells
	# are walked, which a box of another size walks in another order, and
	# structure_contacts() promises no order beyond that
	my $pairs = sub {
		return [ sort map { "$_->{chain1}/$_->{residue1} $_->{chain2}/$_->{residue2} $_->{distance}" }
		         @{ $_[0]{features}{contacts} } ];
	};
	is_deeply($pairs->($odd), $pairs->($plain), 'nor any contact, or its distance');
	is_deeply($odd->{features}{hbonds}, $plain->{features}{hbonds}, 'nor any hydrogen bond');
}

done_testing();



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