Hello,

I am new to perl, but I would like write a perl script that can convert xyz cordinates from a pdb file to distances using the formula:
distance = SQRT[(X1-X2)^2 + (Y1-Y2)^2 + (Z1-Z2)^2]

eg: line 1 xyz to line2 xyz
line 1 xyz to line3 xyz
line 1 to ...73
line 2 to 3 and so on.

But I need to know which line to which line.
Any help would be so great and appreciated.

The pdb file format look like this
REMARK
ATOM 1 O1 VVA 1 20.542 18.415 14.035
ATOM 2 C2 VVA 1 21.588 17.620 14.671
ATOM 3 H3 VVA 1 21.893 18.095 15.605
ATOM 4 C4 VVA 1 22.849 17.597 13.752
ATOM 5 H5 VVA 1 22.659 16.957 12.887
ATOM 6 C6 VVA 1 23.208 19.009 13.227
ATOM 7 H7 VVA 1 22.404 19.424 12.617
ATOM 8 H8 VVA 1 23.407 19.695 14.054
ATOM 9 H9 VVA 1 24.101 18.970 12.600
ATOM 10 C10 VVA 1 24.078 17.043 14.513
ATOM 11 1H1 VVA 1 24.986 17.171 13.920
ATOM 12 2H1 VVA 1 24.223 17.571 15.458
ATOM 13 3H1 VVA 1 23.979 15.976 14.725
ATOM 14 C14 VVA 1 21.040 16.197 15.003
ATOM 15 H15 VVA 1 21.881 15.519 15.161
ATOM 16 C16 VVA 1 20.191 15.577 13.866
ATOM 17 H17 VVA 1 19.272 16.141 13.690
ATOM 18 H18 VVA 1 20.755 15.531 12.932
ATOM 19 H19 VVA 1 19.903 14.555 14.128
ATOM 20 C20 VVA 1 20.251 16.232 16.295

Thank you.

Dani AI

Generated

For this task the reliable approach is: read only ATOM/HETATM records, extract the X,Y,Z fields from the fixed-width PDB columns, keep an identifier (atom serial or the file line number) so you know “which line to which line”, then compute the Euclidean distance for any pair you want (single reference vs all, or every unique pair). As asked, the “which line to which line” is just the pair of atom identifiers; use nested loops or combinations to produce i<j pairs. is right that regexes can find floats, but for standard PDB files slicing the fixed columns is simpler and less error-prone.

Perl example (reads ATOM/HETATM, stores serial + coords, prints all unique pair distances):

open my $fh, '<', 'structure.pdb' or die $!;
my @atoms;
while (<$fh>) {
  next unless /^(?:ATOM|HETATM)/;
  my $serial = int(substr($_,6,5));
  my $x = substr($_,30,8) + 0;
  my $y = substr($_,38,8) + 0;
  my $z = substr($_,46,8) + 0;
  push @atoms, { serial=>$serial, x=>$x, y=>$y, z=>$z };
}
for my $i (0..$#atoms-1) {
  for my $j ($i+1..$#atoms) {
    my $dx = $atoms[$i]{x} - $atoms[$j]{x};
    my $dy = $atoms[$i]{y} - $atoms[$j]{y};
    my $dz = $atoms[$i]{z} - $atoms[$j]{z};
    my $d = sqrt($dx*$dx + $dy*$dy + $dz*$dz);
    printf "%5d %5d %8.3f\n", $atoms[$i]{serial}, $atoms[$j]{serial}, $d;
  }
}

Python alternative (same idea, using itertools.combinations):

from itertools import combinations
from math import sqrt
atoms=[]
with open('structure.pdb') as fh:
    for line in fh:
        if line.startswith(('ATOM','HETATM')):
            serial=int(line[6:11])
            x=float(line[30:38]); y=float(line[38:46]); z=float(line[46:54])
            atoms.append((serial,(x,y,z)))
for (s1,(x1,y1,z1)),(s2,(x2,y2,z2)) in combinations(atoms,2):
    d=sqrt((x1-x2)**2+(y1-y2)**2+(z1-z2)**2)
    print(f"{s1:5d} {s2:5d} {d:8.3f}")

Notes and tips: store either the file line number ($. in Perl / enumerate index in Python) or the atom serial (columns 7–11) so output maps back to the original file. For large sets O(N^2) becomes expensive — use a KD-tree (scipy.spatial.cKDTree or similar) if you only need neighbors within a cutoff. Watch for alternate locations, occupancy and mmCIF files (different format); for robust parsing consider BioPerl or Biopython if you hit edge cases.

Recommended Answers

All 2 Replies

hi,

I want to know what is the regular expression to be written in order to match

ATOM 2 CA ARG A 1 6.324 32.707 50.379

the above XYZ coordinates in this line. I have a pdb file and i want to calculate the distance between 4 coordinates. how do i write this in perl script?

I am completely stuck.

Thank you all in advance

Well \d+\.\d+ would match a single floating point number in non-scientific format.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.