in reply to checking for overlap

Questions:
1. At line 58 I read  $rest1[$i] = $line;
but I do not seem to read it correctly as I can't print it out
in the final "for" loop. What is the correct way to do it?
2. Can anyone help me optimize this code?
Thanks,
Ioana

Replies are listed 'Best First'.
Re: Re: checking for overlap
by tedrek (Pilgrim) on Aug 08, 2003 at 06:19 UTC

    I don't see anything specifically wrong with line 58 and the final for loop. However I did find a serious typo, in your code to find the distance between the two atoms, you accidentally used the Z co-ordinate twice instead of Y. I'm not able to help you optimize the algorithm, but I did clean up the code so it is much more readable (and runs under strict). Allthough I did use 'constant' so all of the caveats for it apply. Be careful of PM wrapping this code. Oh and since you only provided one example line I tested this against some randomly generated data which may not have been correct :).

    #!/usr/bin/perl -w use strict; use constant { RAD => 9, X => 5, Y => 6, Z => 7, NB => 1, REST => 10, }; my $file; print "Please type the filename with the solvent: "; chomp($file = <STDIN>); die "File \"$file\" doesn\'t seem to exist!!\n" unless -e $file; open(SOLVFILE, $file) or die("Couldn't open file $file\n"); print "Please type the filename with the structure: \n"; chomp($file = <STDIN>); die "File \"$file\" doesn\'t seem to exist!!\n" unless -e $file; open(STRUCTFILE, $file) or die("Couldn't open file $file\n"); open(PDB, ">Solvent.pdb") or die "$!"; open(OVERL, ">OverL.txt") or die "$!"; my @solv; my $line; while ($line=<SOLVFILE>) { chomp $line; push @solv, [split /\s+/, $line]; push @{$solv[-1]}, $line; } close SOLVFILE; print "Number of atoms in solvent file is ",$#solv+1," \n"; my @struct; while ($line=<STRUCTFILE>) { chomp($line); push @struct, [split /\s+/, $line]; push @{$struct[-1]}, $line; } close STRUCTFILE; print "Number of atoms in structure file is ",$#struct+1," \n"; for (my $i = 0; $i <= $#solv; $i++) { my $v = $solv[$i]; for my $t (@struct) { if ( # Sum of radius is greater than distance # between the atoms ($v->[RAD] + $t->[RAD]) > sqrt( ($v->[X] - $t->[X])**2 + ($v->[Y] - $t->[Y])**2 + ($v->[Z] - $t->[Z])**2 )) { print OVERL "Overlap for atoms ", $v->[NB]," and ",$t->[NB],"\n"; } else { print PDB $v->[REST],"\n"; } } } close OVERL; close PDB; exit;

    HTH

    tedrek

    Update: I realized the whole @overlap functionality could be moved into the main loop, so I did :). Also you could change the outer for loop to to a while(<SOLV>) and not need to read SOLV into memory.

    Update2: After seeing graff's code I realized the print to PDB was supposed to be in a else block.