Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create checksum algorithm according to new spec #4

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/Fastadb/Util.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package Fastadb::Util;

use strict;
use warnings;

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(reference_retrieval_digest);

use Digest::SHA qw/sha512_hex/;

sub reference_retrieval_digest {
my ($sequence, $offset) = @_; # offset expressed in bytes
$offset = 24 if ! defined $offset;
my $digest = sha512_hex($sequence);
my $substring = substr($digest, 0, $offset*2); #going into hex from bytes
return $substring;
}

1;
31 changes: 31 additions & 0 deletions t/checksum.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use strict;
use warnings;
use Test::More;

use Fastadb::Util qw/reference_retrieval_digest/;
use Fastadb::Fmt::Fasta;
use File::Basename qw/dirname/;
use File::Spec;

is(reference_retrieval_digest('ACGT'), '68a178f7c740c5c240aa67ba41843b119d3bf9f8b0f0ac36', 'Check basic round tripping of reference retrieval digest');

my $test_data_dir = File::Spec->catdir(File::Spec->rel2abs(dirname(__FILE__)), 'data');

my $fasta_iter = Fastadb::Fmt::Fasta->new(file => File::Spec->catfile($test_data_dir, 'test.fa'), type => 'dna');
is(
reference_retrieval_digest($fasta_iter->iterate()->{sequence}),
'b28e54e972297b88324983e18b20420470acac31baff8520',
'Checking basic encoding of known sequence test1'
);
is(
reference_retrieval_digest($fasta_iter->iterate()->{sequence}),
'999687f722592a0959abb475879ccb3b20064d0ad7bbbd85',
'Checking basic encoding of known sequence test2'
);
is(
reference_retrieval_digest($fasta_iter->iterate()->{sequence}),
'21b4f4bd9e64ed355c3eb676a28ebedaf6d8f17bdc365995',
'Checking basic encoding of known sequence test3'
);

done_testing();