| 1 | package Pheno::Ranker::Compare::Prepare; | ||||||||||
| 2 | |||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 8 | ▶ | use feature qw(say); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 8 | ▶ | use List::Util qw(shuffle); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 8 | ▶ | use Pheno::Ranker::Compare::Prune qw(prune_keys_with_weight_zero); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 8 | ▶ | use Pheno::Ranker::Compare::Remap qw(remap_hash); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 8 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 8 | our @EXPORT_OK = qw(create_glob_and_ref_hashes randomize_variables); | |||||||||
| 13 | |||||||||||
| 14 | 19 | ▶ | sub create_glob_and_ref_hashes { | ||||||||
|
Subroutine
create_glob_and_ref_hashes: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||
| 15 | 19 | my ( $array, $weight, $self, $nomenclature ) = @_; | |||||||||
| 16 | 19 | my $primary_key = $self->{primary_key}; | |||||||||
| 17 | 19 | my $glob_hash = {}; | |||||||||
| 18 | 19 | my $ref_hash_flattened; | |||||||||
| 19 | |||||||||||
| 20 | 19 | my $count = 1; | |||||||||
| 21 | 19 | for my $element ( @{$array} ) { | |||||||||
| 22 | |||||||||||
| 23 | # For consistency, we obtain the primary_key for both BFF/PXF | ||||||||||
| 24 | # from $_->{id} (not from subject.id) | ||||||||||
| 25 | 532 | ▶ | my $id = $element->{$primary_key} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | or die | ||||||||||
| 27 | "Sorry but the JSON document [$count] does not have the primary_key <$primary_key> defined\n"; | ||||||||||
| 28 | |||||||||||
| 29 | # Remapping hash | ||||||||||
| 30 | 532 | ▶ | say "Flattening and remapping <id:$id> ..." if $self->{verbose}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | |||||||||||
| 32 | 532 | my $ref_hash = remap_hash( | |||||||||
| 33 | { | ||||||||||
| 34 | hash => $element, | ||||||||||
| 35 | weight => $weight, | ||||||||||
| 36 | self => $self, | ||||||||||
| 37 | nomenclature => $nomenclature, | ||||||||||
| 38 | } | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | # *** IMPORTANT *** | ||||||||||
| 42 | # We eliminate keys with weight = 0 if defined $weight; | ||||||||||
| 43 | 532 | ▶ | prune_keys_with_weight_zero($ref_hash) if defined $weight; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | |||||||||||
| 45 | # Load big hash ref_hash_flattened | ||||||||||
| 46 | 532 | $ref_hash_flattened->{$id} = $ref_hash; | |||||||||
| 47 | |||||||||||
| 48 | # The idea is to create a $glob_hash with unique key-values | ||||||||||
| 49 | # Duplicates will be automatically merged | ||||||||||
| 50 | 532 | $glob_hash = { %$glob_hash, %$ref_hash }; | |||||||||
| 51 | |||||||||||
| 52 | # To keep track of array element indexes | ||||||||||
| 53 | 532 | $count++; | |||||||||
| 54 | } | ||||||||||
| 55 | 19 | return ( $glob_hash, $ref_hash_flattened ); | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 2 | ▶ | sub randomize_variables { | ||||||||
|
Subroutine
randomize_variables: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 59 | 2 | my ( $glob_hash, $self ) = @_; | |||||||||
| 60 | 2 | my $max = $self->{max_number_vars}; | |||||||||
| 61 | 2 | my $seed = $self->{seed}; | |||||||||
| 62 | |||||||||||
| 63 | # set random seed | ||||||||||
| 64 | 2 | srand($seed); | |||||||||
| 65 | |||||||||||
| 66 | # Randomize | ||||||||||
| 67 | # NB:keys have to be sorted for srand to work!!!! | ||||||||||
| 68 | # perl -MList::Util=shuffle -E 'srand 123; say shuffle 1 .. 5' | ||||||||||
| 69 | 2 | my @items = ( shuffle sort keys %$glob_hash )[ 0 .. $max - 1 ]; | |||||||||
| 70 | |||||||||||
| 71 | # Create a new hash with a hash slice | ||||||||||
| 72 | 2 | my %new_glob_hash; | |||||||||
| 73 | 2 | @new_glob_hash{@items} = @{$glob_hash}{@items}; | |||||||||
| 74 | |||||||||||
| 75 | # return reference | ||||||||||
| 76 | 2 | return \%new_glob_hash; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 8 | 1; | |||||||||