How can i make a loop for a string array in Perl?

65 views Asked by At

I am trying to write a program that will accept a pdb file, extract all the information (atom number, atom type, residue name, residue number, x, y, z, b factor), rearrange the residue number, and save the new pdb in a new archive. I can't find a way to use a loop with a string array

This is the code:

print "\nEnter the input file: ";
$inputFile = <STDIN>;
chomp $inputFile;

unless ( open( INPUTFILE, $inputFile ) ) {
    print "Cannot read from '$inputFile'.\nProgram closing.\n";
    <STDIN>;
    exit;
}

chomp( @dataArray = <INPUTFILE> );
close(INPUTFILE);
for ( $line = 0 ; $line <= scalar @dataArray ; $line++ ) {
    if ( $dataArray[$line] =~ m/ATOM\s+(\d+)\s+(\w+)\s+(\w{3})\s+(\w)+\s+(\d+)\s+(\S+\.\S+)\s+(\S+\.\S+)\s+(\S+\.\S+)\s+(.+\S)(.\d\d+\.\d\d.+)/ig ) {
        $m1  = $1;
        $m2  = $2;
        $m3  = $3;
        $m5  = $5;
        $m6  = $6;
        $m7  = $7;
        $m8  = $8;
        $m9  = $9;
        $m10 = $10;
        push( @m3, $m3 );
        push( @m5, $m5 );

        foreach $line ( @m3, @m5 ) {
            if ( $m3[$line] eq $m3[ $line + 1 ] ) {
                $m5[i] = $m5[ i + 1 ];
            }
            elsif ( $m3[$line] ne $m3[ $line + 1 ] ) {
                $m5[ i + 1 ] = $m5[i] + 1;
            }
        }

        $~ = "PDBFORMAT";

        format PDBFORMAT =

ATOM @|||| @||| @|| @|||     @|||||| @|||||| @|||||| @>>>>> @>>>>>

$m1, $m2, $m3,$m51,     $m6,    $m7,    $m8,    $m9,   $m10
.

        open( PDBFORMAT, ">>my2pdb.txt" ) or die "Can't open anything";
        write PDBFORMAT;
    }
}

close PDBFORMAT;

I need to make a script that will make the 6th column continuous according to the residue name (4th column)

This is an example of the input

ATOM 316 CB LEU A 608 -38.110 31.803 16.459 1.00 64.64
ATOM 317 CG LEU A 608 -39.261 32.481 15.719 1.00 71.07
ATOM 318 CD1 LEU A 608 -38.782 33.704 14.929 1.00 73.68
ATOM 319 CD2 LEU A 608 -39.981 31.498 14.829 1.00 69.63
ATOM 320 H LEU A 608 -36.638 31.041 18.563 1.00 99.99
ATOM 321 N ARG A 565 -38.634 34.587 18.911 1.00 22.27
1

There are 1 answers

0
Borodin On BEST ANSWER

I think this will do as you want. Your sample data isn't very comprehensive, so all it does here is change the final residue number to 609

This program expects the path to the input file as a parameter on the command line, so something like

perl process_pdb.pl infile.pdb
use strict;
use warnings;

my ($last_name, $last_num);

while ( <> ) {

  next unless /^ATOM/;

  my @fields = split;
  my $name = $fields[3];

  if ( $last_name ) {
    $fields[5] = $name eq $last_name ? $last_num : $last_num + 1;
  }

  print "@fields\n";

  ($last_name, $last_num) = @fields[3,5];
}

output

ATOM 316 CB LEU A 608 -38.110 31.803 16.459 1.00 64.64
ATOM 317 CG LEU A 608 -39.261 32.481 15.719 1.00 71.07
ATOM 318 CD1 LEU A 608 -38.782 33.704 14.929 1.00 73.68
ATOM 319 CD2 LEU A 608 -39.981 31.498 14.829 1.00 69.63
ATOM 320 H LEU A 608 -36.638 31.041 18.563 1.00 99.99
ATOM 321 N ARG A 609 -38.634 34.587 18.911 1.00 22.27