title: Perl program to calculate neibouring residues from a pdb file
body: <p>This is a program to extract the neighboring residues within 4.5 angstrom. I have resolved the program up to the atom numbers. From these I want to extract</p>
  
  <ul>
  <li>the residue numbers,</li>
  <li>residue name,</li>
  <li>atom № and</li>
  <li>atom name.</li>
  </ul>
  
  <p>I would like to output this data in a tabular form so that I can directly copy the results. But now I'm stuck and require help to extract those fields for the atom numbers I obtained in <code>$close_atomno</code>, and also how to use the program for multiple pdb files and different catalytic residues in one go.</p>
  
  <p>Any help is appreciated.</p>
  
  <pre><code>#!/usr/bin/perl
  use List::Util qw(sum);
  use Data::Dumper qw(Dumper);
  use 5.010;
  
  say "enter residue no.";
  chomp(my $r_no = &lt;STDIN&gt;);
  
  my (@all_pos, @all_data, @matching_pos, @matching_data);
  
  my $residue_file = "neighbouring_residues.txt";
  open my $out_fh1, '&gt;', $residue_file or die "Can't open $residue_file: $!";
  
  say "enter file name";
  chomp(my $infile_name = &lt;STDIN&gt;);
  open IN, '&lt;', $infile_name or die "Can't open $infile_name: $!";
  
  LINE: while (&lt;IN&gt;) {
     chomp; 
     /^ATOM/ or next LINE;
     my @fields = split;
     push @all_pos,  [ @fields[6 .. 8]    ];
     push @all_data, [ @fields[1 .. 3, 5] ];
  
     if( $fields[6] eq $r_no) { 
        say $_;
        push @matching_pos,  [ @fields[6 .. 8]    ];
        push @matching_data, [ @fields[1 .. 3, 5] ];
     } 
  }
  
  say $out_fh1 "Neighbouring residues at position $r_no in the 4.5A region are:";
  my %close_atoms;
  
  MATCHING_ATOM:
  for my $i1 ( 0 .. $#matching_pos ) {
     my $matching_atom = $matching_data[$i1][1];
     $matching_atom eq $_ and next MATCHING_ATOM for qw/N CA O C/;
     for my $i2 ( 0 .. $#all_pos ) {
        my ($close_atomno, $close_residueno) = @{$all_data[$i2]}[0, 3];
        my $dist = distance($matching_pos[$i1], $all_pos[$i2]);
        if($dist &lt; 4.5 and $close_residueno != $r_no) {
           $close_atoms{$close_atomno} = 1;
        }
     }
  }
  
  sub distance { sqrt sum map {$_**2} map {$_[0][$_] - $_[1][$_]} 0 .. $#{$_[0]} };
  
  my @close_atoms = keys %close_atoms;
  
  say $out_fh1 "@close_atoms";
  for my $m (0 .. $#close_atoms) { 
     say $out_fh1 $all_pos[$m];# here is problem i want residue details according to $close_atomno 
  }
  
  say "result in $residue_file";
  </code></pre>
  
  <p>This would be a typical input file:</p>
  
  <pre><code>ATOM 9   N GLU A 1 35.540 1.925 27.662 1.00 19.70 N
  ATOM 10 CA GLU A 1 35.626 1.018 28.802 1.00 20.96 C
  ATOM 11  C GLU A 1 34.264 0.794 29.444 1.00 20.22 C
  </code></pre>
  
  <p>with columns in this order:</p>
  
  <ul>
  <li>atom</li>
  <li>atom number</li>
  <li>atom name</li>
  <li>residue name</li>
  <li>chain</li>
  <li>residue number</li>
  <li>x coord</li>
  <li>y coord</li>
  <li>z coord</li>
  <li>irrelevant</li>
  <li>irrelevant</li>
  <li>irrelevant</li>
  </ul>