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.


In reply to Re: Re: checking for overlap by tedrek
in thread checking for overlap by ioana

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.