user2303599
user2303599

Reputation: 21

Check if keys of one hash are the same as the values of another hash?

I am trying to see the values in the the hash %pc are in the keys in the hash %fasta and then print the key and value of % fasta if it is.

So far I have the script:

#!/usr/bin/perl
use warnings;
use strict;
use Bio::SearchIO;
use Bio::SeqIO;

my %pc;
my %fasta;

#get list of core protein clusters
my $file1 = $ARGV[0];
open (INPUT1, $file1) or die "Could not open $file1\n";
while (my $line1 = <INPUT1>) {
    chomp $line1;
    my @spl = split (/\|/, $line1);
    my $id = $spl[0];
    my $gene = $spl[1];
    $pc{$id}=$gene;
}


my @files = glob("*_PC.fa");
foreach my $fil (@files) {
    my $seqio = Bio::SeqIO->new(-format => 'fasta', -file   => $fil);
    my $file = $fil;
    $file =~ /^(.+)_PC.fa/;
    my $outfile = $1."_core.fa";
    open (OUTFILE,'>', $outfile) or die "Could not open $outfile \n";
    while (my $seqobj = $seqio->next_seq) {
        my $seqid = $seqobj->display_id;
        my $nuc = $seqobj->seq();
        $fasta{$seqid}=$nuc;
        foreach my $key (keys %fasta) {
            my @spl2 = split (/\|/,$key);
            my $fasta_gene = $spl2[1];
            if ($fasta_gene eq (keys %fasta)) {
                print OUTFILE ">$key\n$fasta{$key}\n";
            }
        }
    }
    close OUTFILE;    
}

Thanks in advance!

Upvotes: 0

Views: 132

Answers (1)

mpapec
mpapec

Reputation: 50677

%fasta gets printed if all values from %pc exists as keys in %fasta

my $some_values_missing = grep { ! exists $fasta{$_} } values %pc;

unless ($some_values_missing) {
  print "key:$_; value:$fasta{$_}\n" for keys %fasta;
}

Upvotes: 1

Related Questions