in reply to Re^4: how to compare two hashes with perl?
in thread how to compare two hashes with perl?
#!/usr/bin/perl use warnings; # Perl interpreter command use strict; my %bow1 = (); my $file1 = shift; open (FILE1, "$file1")|| die "Failed to open $file1 for reading : $!"; + # Open first file while (<FILE1>) { # Reading first hash my ($ID, undef, undef, undef, $Seq) = split; $bow1{$ID}[0] = $ID; $bow1{$ID}[1] = $Seq; } close FILE1 || die "Failed to close $file1 : $!"; my %bow2 = (); my $file2 = shift; open (FILE2, "$file2") || die "Failed to open $file2 for reading : $!" +; # Open first file while (<FILE2>) { # Reading second hash my ($ID, undef, undef, undef, $Seq) = split; $bow2{$ID}[0] = $ID; $bow2{$ID}[1] = $Seq; } close FILE2 || die "Failed to close $file2 : $!"; print"Match status\t$file1 ID\t$file1 Sequence\t$file2 ID\t$file2 Sequ +ence\n"; # Print title my $totalCount=0; #initialize variables for counting my $identical=0; my $diffSeq=0; my $unique=0; foreach my $ID (keys %bow1){ # can use (sort keys %hash) to put items +in a specified order if (exists $bow2{$ID}[0] ){ if ( $bow1{$ID}[0] eq $bow2{$ID}[0] ){ ## id and sequence are stored as key value pairs if ( $bow1{$ID}[1] eq $bow2{$ID}[1] ){ #print "Identical\t$bow1{$ID}[0]\t$bow1{$ID}[1]\t$bow2{$ID}[0 +]\t$bow2{$ID}[1]\n"; #display ID and sequences -->too many: commente +d out $identical=$identical+1; #count identical pairs } else{ print "SameID, DiffSeq\t$bow1{$ID}[0]\t$bow1{$ID}[1]\t$bow2{$I +D}[0]\t$bow2{$ID}[1]\n"; #display ID and sequences $diffSeq=$diffSeq+1; #count pairs with different sequences but + identical IDs } } } else { print "Unique\t$bow1{$ID}[0]\t$bow1{$ID}[1]\t - \t - \n"; #display + ID and sequences $unique=$unique+1; #count unique IDs from first file } } $totalCount = $identical + $diffSeq + $unique; #total count - should m +atch with total ID in first file print "Identical\tSeq is different\tUnique in $file1\tTotal\n"; #print + title print "$identical\t$diffSeq\t$unique\t$totalCount\n"; #print numbers exit;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: UPDATE! I fixed it :D
by BioLion (Curate) on Nov 09, 2009 at 12:08 UTC |