sarf13 has asked for the wisdom of the Perl Monks concerning the following question:

I have two file of given structure
File 1 structure :
SER A ARG A GLU A LYS A ASN A GLN A PRO A LYS A PRO A SER A PRO A LYS A ARG A
File 2 structures (PDB file):
ATOM 2 CA PRO A 6 2.437 5.295 -23.029 1.00 0.00 + C ATOM 3 C PRO A 6 2.307 5.113 -21.515 1.00 0.00 + C ATOM 4 O PRO A 6 3.276 4.869 -20.823 1.00 0.00 + O ATOM 15 N TRP A 7 1.114 5.225 -20.998 1.00 0.00 + N ATOM 16 CA TRP A 7 0.916 5.054 -19.531 1.00 0.00 + C ATOM 17 C TRP A 7 0.772 3.567 -19.197 1.00 0.00 + C <>

the above given two file I have to match file one value with third column value(PRO A) of second which is repeated but I have to consider this only one and pick the column fourth value(6). i have created I subroutine for that for first time it matching correctly but for second file it has missing some value.these both file value will change after completion of first time.

MY code
sub parse_pdb() { my $file = shift; my %hash_pdb = (); # Opening of PDB file after dowloading open(FH, $file); @atomrecord = <FH>; foreach my $record (@atomrecord) { chomp($record); if($record =~/^ATOM/) { @temp = split(/\s+/, $record); $con_key = ""; $con_key = "$temp[3]"." "."$temp[4]"; $count = 0; if( $hash_pdb{$con_key} ) { if($val_seen{$temp[5]}) { } else { $hash_pdb{$con_key} = $hash_pdb{$con_key}."\t"."$t +emp[5]"; } } else { $hash_pdb{$con_key} = $temp[5]; } $val_seen{$temp[5]} = 1; } if($record =~ /^ENDMDL/){ print "i am going out of loop\n"; last; } } return \%hash_pdb; }
thanks in advance

Replies are listed 'Best First'.
Re: perl hash problem for comparing two files
by Utilitarian (Vicar) on Mar 25, 2011 at 06:56 UTC
    You have an unnecessary set of parentheses where you declare your sub, assuming you didn't mean to set an invalid prototype, you should use code tags around code as otherwise the array indices are linkified and indentation is lost.
    #! /usr/bin/perl use strict; use warnings; my $hash_ref= parse_pdb("pdb.txt"); use Data::Dumper; print Dumper($hash_ref); sub parse_pdb{ my $file = shift; my %hash_pdb = (); my %val_seen=(); # Opening of PDB file after dowloading open(FH, $file); my @atomrecord = <FH>; for my $record (@atomrecord){ chomp($record); if($record =~/^ATOM/) { my $con_key = ""; $con_key = "$temp[3]"." "."$temp[4]"; my $count = 0; if( $hash_pdb{$con_key} ) { if($val_seen{$temp[5]}) { } else { $hash_pdb{$con_key} = $hash_pdb{$con_key}."\t"."$t +emp[5]"; } } else { $hash_pdb{$con_key} = $temp[5]; } $val_seen{$temp[5]} = 1; } if($record =~ /^ENDMDL/){ print "i am going out of loop\n"; last; } } return \%hash_pdb; }
    running this gives you
    $VAR1 = { 'PRO A' => '6', 'TRP A' => '7' };

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."