How to reference a hash of array of hashes in orde

2019-09-15 04:47发布

问题:

I have the following data structure:

my %hash = (
    'hsa_circ_0024017|chr11:93463035-93463135+|NM_033395|KIAA1731  FORWARD' => [ 
        { 
          'energy' => '-4.3', 
          'spacer' => 'AGGCACC', 
          'end' => '97', 
          'start' => '81' 
        } 
    ],
    'hsa_circ_0067224|chr3:128345575-128345675-|NM_002950|RPN1  FORWARD' => [ 
        { 
          'energy' => '-4.4', 
          'spacer' => 'CAGT', 
          'end' => '17', 
          'start' => '6' 
        }, 
        { 
          'energy' => '-4.1', 
          'spacer' => 'GTT', 
          'end' => '51', 
          'start' => '26' 
        }, 
        { 
          'energy' => '-4.1', 
          'spacer' => 'TTG', 
          'end' => '53', 
          'start' => '28' 
        } 
    ],
    ...
);

How do I access the contents of my hash to be able to compare the contents within a loop?

For each parent hash (hsa_circ...) I want to compare the child hashes (spacers) together. Forgive me I'm struggling to word this right. This is a small sample of the data of course. My goal, in brief, is to detect the arrays of hashes which have the same spacer and if they do have the same spacer then I want to then choose the array of hashes which has the lowest energy score.

回答1:

The problem: there may be groups of hashrefs in each arrayref with the equal spacer value. In each such group the hashref with the lowest energy value need be identified, to replace that group.

Most work is done in partition_equal(), which identifies hashref groups with equal spacers

use warnings;
use strict;
use List::Util qw(reduce);
use Data::Dump qq(dd);

# Test data: two groups of equal-spacer hashrefs, in the first array only
my %hash = (  
    kA => [
        { 'energy' => -4.3, 'spacer' => 'AGGCACC' },
        { 'energy' => -2.3, 'spacer' => 'AGGCACC' },
        { 'energy' => -3.3, 'spacer' => 'CAGT' },
        { 'energy' => -1.5, 'spacer' => 'GTT' },
        { 'energy' => -2.5, 'spacer' => 'GTT' },
    ],
    kB => [
        { 'energy' => -4.4, 'spacer' => 'CAGT' },
        { 'energy' => -4.1, 'spacer' => 'GTT' },
        { 'energy' => -4.1, 'spacer' => 'TTG' },
    ],
);
#dd \%hash;

for my $key (keys %hash) {
    my ($spv, $unique) = partition_equal($hash{$key});
    next if not $spv;
    # Extract minimum-energy hashref from each group and add to arrayref
    # $unique, so that it can eventually overwrite this key's arrayref
    foreach my $spacer (keys %$spv) {
        my $hr_min = reduce { 
            $a->{energy} < $b->{energy} ? $a : $b 
        } @{$spv->{$spacer}};
        push @$unique, $hr_min;
    }
    # new: unique + lowest-energy ones for each equal-spacer group   
    $hash{$key} = $unique  if keys %$spv;
}    
dd \%hash;

# Sort array and compare neighbouring elements (hashrefs) 
sub partition_equal {
    my $ra = shift;
    my @sr = sort { $a->{spacer} cmp $b->{spacer} } @$ra;

    # %spv:    spacer value => [ hashrefs with it ], ...
    # @unique: hasrefs with unique spacer values    
    my (%spv, @unique);

    # Process first and last separately, to not have to test for them
    ($sr[0]{spacer} eq $sr[1]{spacer})
        ? push @{$spv{$sr[0]{spacer}}}, $sr[0]
        : push @unique, $sr[0];
    for my $i (1..$#sr-1) {
        if ($sr[$i]{spacer} eq $sr[$i-1]{spacer}  or 
            $sr[$i]{spacer} eq $sr[$i+1]{spacer}) 
        {
            push @{$spv{$sr[$i]{spacer}}}, $sr[$i]
        }
        else { push @unique, $sr[$i] }
    }
    ($sr[-1]{spacer} eq $sr[-2]{spacer})
        ? push @{$spv{$sr[-1]{spacer}}}, $sr[-1]
        : push @unique, $sr[-1];

    return if not keys %spv;
    return \%spv, \@unique;
}

Output

kA => [
        { energy => -3.3, spacer => "CAGT" },
        { energy => -2.5, spacer => "GTT" },
        { energy => -4.3, spacer => "AGGCACC" },
      ],
kB => [
        { energy => -4.4, spacer => "CAGT" },
        { energy => -4.1, spacer => "GTT" },
        { energy => -4.1, spacer => "TTG" },
      ],

The order inside arrayrefs is not maintained; the new arrayref has first hashrefs with unique spacer values, then those with lowest-energy value (for each original group with same spacer-values).

The sub sorts input by spacer values, so that it can identify equal ones by simply iterating through the sorted array and comparing only neighbors. This should be reasonably efficient.



回答2:

Like this

$_=$hash{'hsa_circ_0024017|chr11:93463035-93463135+|NM_033395|KIAA1731  FORWARD'}->[0]{energy};

print  $_ ."\n";

so it's $this{hash}->[array_ref]{hash]

share|improve this answer
  • Not sure if i understand the question... you can access a hash element by directly accessing it (as above) or doing a loop through the hash keys – hoffmeister Jul 26 '17 at 23:18
0
for my $line (keys(%by_line)) {
   my $spacer_defs = $by_line{$line};
   ...
}

or

for my $spacer_defs (values(%by_line)) {
   ...
}

will get you (the reference to) the array of (references to) spacer definitions for each line in turn. Then, you can edit that array as follows:

   my %uniq_spacer_defs;
   for my $spacer_def (@$spacer_defs) {
      my $spacer = $spacer_def->{spacer};
      $uniq_spacer_defs{$spacer} = $spacer_defs
         if !$uniq_spacer_defs{$spacer}
         || $uniq_spacer_defs{$spacer}{energy} < $spacer_def->{energy};
   }

   @$spacer_defs = values(%uniq_spacer_defs);

Notes:

  • The order of the "spacer definitions" may change. If that's a problem, replace

    @$spacer_defs = values(%uniq_spacer_defs);
    

    with

    @$spacer_defs = grep { $uniq_spacer_defs{$_->{spacer}} == $_ } @$spacer_defs;
    
  • In case of two spacer definitions with the same spacer and the same energy, the first one encountered is kept.

share|improve this answer

Your Answer

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged arrays perl hashmap perl-data-structures or ask your own question.

收藏的人(0)

Ta的文章 更多文章
登录 后发表评论
0条评论
还没有人评论过~