Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Distance between xyz coordinates in pdb files

Status
Not open for further replies.

stellaparallax

Technical User
Apr 29, 2012
1
AU
Hi,

I am creating a program that calculates the distance between the x, y, z coordinates of atoms listed in a pdb file.

So far i have this:

#!/usr/bin/perl -w


$num = 0;
$count = 0;

while (<>) {

# Find x, y, z coordinates and store in separate arrays

if ($_ =~ /^ATOM/) {
@line = $_ =~ m/^(.....).(.....).(....).(...)..(....)....(........)(........)(........)/;

$x = $line[5];
$arrayx[$num] = $x;

$y = $line[6];
$arrayy[$num] = $y;

$z = $line[7];
$arrayz[$num] = $z;

++$num;
}

# Count number of atoms

if ($_ =~ /^ATOM/) {
++$count;
}
}

# Calculate distance between all atom coordinates

foreach $i (0..$count) {

foreach $j ($i + 1..$count) {

$dist = sqrt(
($arrayx[$i] - $arrayx[$j])**2 +
($arrayy[$i] - $arrayy[$j])**2 +
($arrayz[$i] - $arrayz[$j])**2
);

print "$dist\n"

}
}

When I run the program i get this message popping up for some of the lines and I don't know what to do to fix it:

"Use of uninitialized value in subtraction (-) at ./gas.pl line 42, <> line 14368"

The line that it states is the last line of the pdb file, however i don't see why this line is involved in my calculations as this is not present in any of my arrays.

The pdb file I'm using is located here :
Any help would be much appreciated as I am VERY new to Perl.
Thanks
 
Please enclose code between [ignore]
Code:
[/ignore][i]your code here[/i][ignore]
[/ignore] tags.
There are at least two errors in your code:
- [tt]$count[/tt] will contain the number of counts + 1 . You should initialize it like so [tt]$count=-1[/tt] instead of [tt]$count=0[/tt]
- the first [tt]foreach[/tt] is incorrect, it should go to [tt]$count-1[/tt] ; also, if you have thousands of lines, you should write it like so
Code:
foreach($i=0;$i<$count;$i++){
  foreach($j=$i+1;$j<=$count;$j++){

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
[tt]#!/usr/bin/perl

$num = 0;

while ( <DATA> ) {

# Find x, y, z coordinates and store in separate arrays

if ( $_ =~ s/^ATOM__(.....).(.....).(....).(...)..(....)....(........)(........)(........)/$arrayx[ $num ] = $6; $arrayy[ $num ] = $7; $arrayz[ $num ] = $8/e ) {

print "num is $num\n";
print '=' x ( 7 + ( length $num ) );
print "\n";

print $arrayx[ $num ] . "\n";
print $arrayy[ $num ] . "\n";
print $arrayz[ $num ] . "\n\n";

$num++;
}

}

foreach( $i = 0; $i < $num; $i++ ) {

foreach( $j = $i + 1; $j <= $num; $j++ ) {

print $arrayx[$i] . " \$arrayx[$i]\n";
print $arrayy[$i] . " \$arrayx[$i]\n";
print $arrayz[$i] . " \$arrayx[$i]\n";
print $arrayx[$j] . " \$arrayx[$j]\n";
print $arrayy[$j] . " \$arrayx[$j]\n";
print $arrayz[$j] . " \$arrayx[$j]\n\n";


if ( $arrayx[$i] > 0 && $arrayy[$i] > 0 && $arrayz[$i] > 0 &&
$arrayx[$j] > 0 && $arrayy[$j] > 0 && $arrayz[$j] > 0 ) {

$dist = sqrt(
( $arrayx[$i] - $arrayx[$j] ) ** 2 +
( $arrayy[$i] - $arrayy[$j] ) ** 2 +
( $arrayz[$i] - $arrayz[$j] ) ** 2
);

print "distance = $dist\n\n";
}
}
}

__DATA__
ATOM__123456789123456789123456789123456789123456789123456789
ATOM__234567891234567891234567891234567891234567891234567891
ATOM__345678912345678912345678912345678912345678912345678912[/tt]

Kind Regards
Duncan
 
coloring in regex shows (...) mapping to $6, $7 * $8 respectively

[tt]#!/usr/bin/perl

$num = 0;

s/^ATOM__(.....).(.....).(....).(...)..(....)....[green](........)[/green][blue](........)[/blue](........)/$arrayx[ $num ] = [green]$6[/green]; $arrayy[ $num ] = [blue]$7[/blue]; $arrayz[ $num++ ] = $8/e while ( <DATA> );

foreach( $i = 0; $i < $num; $i++ ) {

foreach( $j = $i + 1; $j <= $num; $j++ ) {

if ( $arrayx[$i] > 0 && $arrayy[$i] > 0 && $arrayz[$i] > 0 && $arrayx[$j] > 0 && $arrayy[$j] > 0 && $arrayz[$j] > 0 ) {

$dist = sqrt(( $arrayx[$i] - $arrayx[$j] ) ** 2 + ( $arrayy[$i] - $arrayy[$j] ) ** 2 + ( $arrayz[$i] - $arrayz[$j] ) ** 2 );

print "distance = $dist\n";
}
}
}

__DATA__
ATOM__123456789123456789123456789123456789123456789123456789
ATOM__234567891234567891234567891234567891234567891234567891
ATOM__345678912345678912345678912345678912345678912345678912[/tt]

Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top