I need help figuring out how these two subroutines work and what values or data structures they return. Here’s a minimal representation of the code:
#!/usr/bin/perl
use strict; use warnings;
# an array of ASCII encrypted characters
my @quality = ("C~#p)eOA`/>*", "DCCec)ds~~", "*^&*"); # for instance
# input the quality
# the '@' character in front deferences the subroutine's returned array ref
my @q = @{unpack_qual_to_phred(@quality)};
print pack_phred_to_qual(\@q) . "\n";
sub unpack_qual_to_phred{
my ($qual)=@_;
my $upack_code='c' . length($qual);
my @q=unpack("$upack_code",$qual);
for(my $i=0;$i<@q;$i++){
$q[$i]-=64;
}
return(\@q);
}
sub pack_phred_to_qual{
my ($q_ref)=@_;
@q=@{$q_ref};
for(my $i=0;$i<@q;$i++){
$q[$i]+=64;
}
my $pack_code='c' . int(@q);
my $qual=pack("$pack_code",@q);
return ($qual);
}
1;
From my understanding, the unpack_qual_to_phread() subroutine apparently decrypts the ASCII character elements stored in @quality. The subroutine reads in an array containing elements of ASCII characters. Each element of the array is processed and apparently decrypted. The subroutine then returns an array ref containing elements of the decrypted array. I understand this much however I’m not really familiar with the Perl functions pack and unpack. Also I was unable to find any good examples of them online.
I think the pack_phred_to_qual subroutine converts the quality array ref back into ASCII characters and prints them.
thanks. any help or suggestions are greatly appreciated. Also if someone could provide a simple example of how Perl’s pack and unpack functions work that would help too.
Calculating the length is needless. Those functions can be simplified to
In encryption terms, it’s a crazy simple substitution cypher. It simply subtracts 64 from the character number of each character. It could have been written as
The pack/unpack doesn’t factor in the encryption/decryption at all; it’s just a way of iterating over each byte.