#!/usr/bin/perl use strict; use warnings; my @helix; while ( ) { chomp; next if /^\s*$/; if (/^([^:]+):/) { # Start of new HELIX definition push @helix, { name => $1 }; # Anonymous hash with 1 element (name) next; } # Atom definition if (/^\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s*/) { push @{$helix[-1]{atom}}, {name => $1, id => $2, x => $3, y => $4, z => $5 }; } } # Go through @helix in order for my $i (0 .. $#helix - 1) { print "Comparing helix $helix[$i]{name}\n"; my @atom_1 = sort { $a->{id} <=> $b->{id} } @{$helix[$i]{atom}}; for my $j ($i + 1 .. $#helix) { print "\tAgainst helix $helix[$j]{name}\n"; my @atom_2 = sort { $a->{id} <=> $b->{id} } @{$helix[$j]{atom}}; for my $a1 (@atom_1) { for my $a2 (@atom_2) { my $distance = distance($a1, $a2); print "\t\tDistance between $a1->{name} and $a2->{name} is $distance\n"; } } } print "\n\n"; } sub distance { my ($a1, $a2) = @_; my $delta_x = ($a1->{x} - $a2->{x}) ** 2; my $delta_y = ($a1->{y} - $a2->{y}) ** 2; my $delta_z = ($a1->{z} - $a2->{z}) ** 2; return sqrt($delta_x + $delta_y + $delta_z); } __DATA__ HELIX1: x y z A 1 -1.115 8.537 7.075 B 2 -2.745 5.280 7.165 C 3 -0.777 3.267 7.329 D 4 1.610 5.225 10.885 E 5 0.296 5.263 10.912 HELIX2: K 1 -0.696 13.041 22.357 L 2 1.152 11.081 23.082 M 3 2.200 17.590 18.424