in reply to calculate distance between atoms and a target atom

Some further suggestions. It would be fitting to hold point coordinates in an array type variable. Instead of ($Znx, $Zny, $Znz), a single @Zn variable might be used.

Secondly, regular expressions offer a convenient method to capture specific parts of input. This allows one to write, e.g. @Zn = ($1, $2, $3); — but you must include (...) groups in the pattern. If the data is of rigidly fixed format, pack() and unpack() are often quite useful:

my $xyz_fmt = '@30a7 @38a6 @46a6'; while ($pdbline = <IN>) { if ($pdbline =~ m/ZN1 LG1 X/) { @Zn = unpack $xyz_fmt, $pdbline; next; } if ($pdbline =~ /^ATOM.*(OD2 ASP|NE2 HIS)/) { push @Atoms, [unpack $xyz_fmt, $pdbline]; ... } } # ... do the calculations once we have all data.
Note that the above example looks a bit strange because of the mixed regex and unpack usage. The second regex here combines three patterns from original code. @Atoms is an array of arrays (AoA). You can check the number of points with int(@Atoms).

Update: for questions regarding the handling of AoA, please see perllol or perldsc.

Replies are listed 'Best First'.
Re^2: calculate distance between atoms and a target atom
by mishimakaz (Initiate) on Dec 03, 2013 at 18:16 UTC

    I am trying to use your suggestion, but also at the same time, create an array of "Atoms" such that I can calculate the distance between the Zinc atom and all other OD2 and NE2 atoms. So I am trying to use ++$num to tally up the number of OD2s and NE2s It would appear that the problem is in the this line

    push $Atoms$num, unpack $xyz_fmt, $pdbline;

    #!/usr/bin/perl -W #subroutines for calculations use strict; sub distanceAB($$$$$$) { my ($x1,$y1,$z1,$x2,$y2,$z2) = @_; $distance = sqrt (($x2-$x1)**2 + ($y2 -$y1)**2 + ($z2-$z1)**2); return sprintf("%4.2f", $distance); } #open files for calculations and modify distance cutoff for target res +idues $input=$ARGV[0]; open(IN, "$input"); $num = 0; $count = 0; my $xyz_fmt = '@30a8 @38a8 @46a8'; while ($pdbline = <IN>) { if ($pdbline =~ m/ZN1 LG1 X/) { @ZN = unpack $xyz_fmt, $pdbline; next; } #find xyz coordinates for other atoms and store in array if ($pdbline =~ /^ATOM.*(OD2 ASP|NE2 HIS)/) { push $Atoms[$num], [unpack $xyz_fmt, $pdbline]; $resname[$num] = substr($pdbline, 16, 3); $resnumber[$num] = substr($pdbline, 22, 3); ++$num; } } #count number of potential coordinating atoms if ($pdbline =~ /OD2 ASP/ or $pdbline =~ /NE2 HIS/) { ++$count; } #calculate Zn to each atom distance foreach $i (0..$count) { $Zndistance=distanceAB($x1[$ZN[0]],$y1[$ZN[1]],$z1[$ZN[2]],$x2[$At +oms[$i][0]],$y2[$Atoms[$i][1]],$z2[$Atoms[$i][2]]); if ($Zndistance < 2.5) { $distanceAB=$Zndistance; print "$distanceAB \n"; print "$resname[$i] $resnumber[$i] \n"; } }
      Regarding my new code, how would I implement my subroutine calculation?