| 1 | package Pheno::Ranker::Compare::Encoding; | ||||||||||
| 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 | |||||||||||
| 6 | 8 | ▶ | use Compress::Zlib qw(compress uncompress); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 8 | ▶ | use MIME::Base64 qw(encode_base64 decode_base64); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 8 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 8 | our @EXPORT_OK = | |||||||||
| 11 | qw(create_binary_digit_string binary_to_base64 _base64_to_binary); | ||||||||||
| 12 | |||||||||||
| 13 | 28 | ▶ | sub create_binary_digit_string { | ||||||||
|
Subroutine
create_binary_digit_string: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||
| 14 | 28 | my ( $export, $weight, $glob_hash, $cmp_hash ) = @_; | |||||||||
| 15 | 28 | my %out_hash; | |||||||||
| 16 | |||||||||||
| 17 | # *** IMPORTANT *** | ||||||||||
| 18 | # Being a nested for, keys %{$glob_hash} does not need sorting | ||||||||||
| 19 | # BUT, we sort to follow the same order as serialized (sorted) | ||||||||||
| 20 | 28 | my @sorted_keys_glob_hash = sort keys %{$glob_hash}; | |||||||||
| 21 | |||||||||||
| 22 | # IDs of each indidividual | ||||||||||
| 23 | 28 | for my $individual_id ( keys %{$cmp_hash} ) { # no need to sort | |||||||||
| 24 | |||||||||||
| 25 | # One-hot encoding = Representing categorical data as numerical | ||||||||||
| 26 | 542 | my ( $binary_str, $binary_str_weighted ) = ('') x 2; | |||||||||
| 27 | |||||||||||
| 28 | 542 | for my $key (@sorted_keys_glob_hash) { | |||||||||
| 29 | 73750 | my $has_value = exists $cmp_hash->{$individual_id}{$key}; | |||||||||
| 30 | 73750 | ▶ | $binary_str .= $has_value ? '1' : '0'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 73750 | ▶ | if ( defined $weight ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | $binary_str_weighted .= | ||||||||||
| 33 | $has_value | ||||||||||
| 34 | ? ( '1' x $glob_hash->{$key} ) | ||||||||||
| 35 | 13402 | ▶ | : ( '0' x $glob_hash->{$key} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | } | ||||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | # If weight is not defined, simply assign the unweighted string once. | ||||||||||
| 40 | 542 | ▶ | $binary_str_weighted = $binary_str unless defined $weight; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | |||||||||||
| 42 | 542 | $out_hash{$individual_id}{binary_digit_string} = $binary_str; | |||||||||
| 43 | $out_hash{$individual_id}{binary_digit_string_weighted} = | ||||||||||
| 44 | 542 | $binary_str_weighted; | |||||||||
| 45 | |||||||||||
| 46 | 542 | ▶ | if ( defined $export ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | |||||||||||
| 48 | # Convert string => raw bytes > zlib-compres => Base64 | ||||||||||
| 49 | $out_hash{$individual_id}{zlib_base64_binary_digit_string} = | ||||||||||
| 50 | 44 | binary_to_base64($binary_str); | |||||||||
| 51 | |||||||||||
| 52 | $out_hash{$individual_id}{zlib_base64_binary_digit_string_weighted} | ||||||||||
| 53 | = defined $weight | ||||||||||
| 54 | ? binary_to_base64($binary_str_weighted) | ||||||||||
| 55 | 44 | ▶ | : $out_hash{$individual_id}{zlib_base64_binary_digit_string}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | } | ||||||||||
| 59 | 28 | return \%out_hash; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 47 | ▶ | sub binary_to_base64 { | ||||||||
|
Subroutine
binary_to_base64: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 63 | 47 | my $binary_string = shift; | |||||||||
| 64 | |||||||||||
| 65 | # Convert binary string (e.g. "0010...") to raw bytes | ||||||||||
| 66 | 47 | my $raw_data = pack( "B*", $binary_string ); | |||||||||
| 67 | |||||||||||
| 68 | # Compress the raw data (note: compressing very short data may not save space) | ||||||||||
| 69 | 47 | my $compressed = compress($raw_data); | |||||||||
| 70 | |||||||||||
| 71 | # Base64 encode the compressed data, without any newline breaks | ||||||||||
| 72 | 47 | return encode_base64( $compressed, "" ); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 3 | ▶ | sub _base64_to_binary { | ||||||||
|
Subroutine
_base64_to_binary: called CC=2 SCAR=6.9
| |||||||||||
| 76 | 3 | my ( $b64_string, $original_length ) = @_; | |||||||||
| 77 | |||||||||||
| 78 | # Decode the Base64 encoded compressed data | ||||||||||
| 79 | 3 | my $compressed_data = decode_base64($b64_string); | |||||||||
| 80 | |||||||||||
| 81 | # Decompress the data back to raw bytes | ||||||||||
| 82 | 3 | ▶ | my $raw_data = uncompress($compressed_data) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | or die "Decompression failed: $Compress::Zlib::gzerrno\n"; | ||||||||||
| 84 | |||||||||||
| 85 | # Convert the raw bytes back into a binary string (sequence of 0s and 1s) | ||||||||||
| 86 | 2 | my $binary_string = unpack( "B*", $raw_data ); | |||||||||
| 87 | |||||||||||
| 88 | # Trim the binary string to the original length to remove any padded bits | ||||||||||
| 89 | 2 | return substr( $binary_string, 0, $original_length ); | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 8 | 1; | |||||||||