Search code examples
arraysperlhashmapperl-data-structures

How to reference a hash of array of hashes in order to compare values


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.


Solution

  • 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.