From 329961ff6ed69969c2fd1e6d60dc9fdc1d55a5ab Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sat, 11 Mar 2017 21:05:34 +0200 Subject: [PATCH] CIGAR format --- Edlib.xs | 4 ++++ lib/Text/Levenshtein/Edlib.pm | 13 +++++++++++-- t/Text-Levenshtein-Edlib.t | 8 +++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Edlib.xs b/Edlib.xs index 507284e..c450346 100644 --- a/Edlib.xs +++ b/Edlib.xs @@ -54,3 +54,7 @@ CODE: RETVAL = newRV_noinc((SV*)returned_av); OUTPUT: RETVAL + + +char* +edlibAlignmentToCigar(unsigned char *alignment, int length(alignment), int format) diff --git a/lib/Text/Levenshtein/Edlib.pm b/lib/Text/Levenshtein/Edlib.pm index 9274517..2948818 100644 --- a/lib/Text/Levenshtein/Edlib.pm +++ b/lib/Text/Levenshtein/Edlib.pm @@ -24,7 +24,7 @@ my @constants = EDLIB_TASK_PATH/; our %EXPORT_TAGS = - (all => [ @constants, qw/align distance/ ], constants => \@constants); + (all => [ @constants, qw/align distance to_cigar/ ], constants => \@constants); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = ( @{ $EXPORT_TAGS{'constants'} } ); our $VERSION = '0.001'; @@ -71,6 +71,13 @@ sub distance { align($q, $t, $k)->{editDistance} } +sub to_cigar { + my ($align, $format) = @_; + $align = pack 'C*', @$align; + $format //= EDLIB_CIGAR_STANDARD(); + edlibAlignmentToCigar($align, $format); +} + 1; __END__ @@ -95,7 +102,9 @@ Text::Levenshtein::Edlib - XS edit distance and optimal alignment path calculati say "Start locations are: @{$align->{startLocations}}"; say "End locations are: @{$align->{endLocations}}"; say "Alignment path is: @{$align->{alignment}}"; - + say "Alignment path (in CIGAR format): ", to_cigar $align->{alignment}; + say "Alignment path (in extended CIGAR format): ", + to_cigar $align->{alignment}, EDLIB_CIGAR_EXTENDED; =head1 DESCRIPTION diff --git a/t/Text-Levenshtein-Edlib.t b/t/Text-Levenshtein-Edlib.t index ddd487c..1cb186a 100644 --- a/t/Text-Levenshtein-Edlib.t +++ b/t/Text-Levenshtein-Edlib.t @@ -2,7 +2,7 @@ use strict; use warnings; -use Test::More tests => 9; +use Test::More tests => 11; BEGIN { use_ok('Text::Levenshtein::Edlib', ':all') }; @@ -36,3 +36,9 @@ is $r->{alphabetLength}, 7, 'align->{alphabetLength}'; is_deeply $r->{endLocations}, [6], 'align->{endLocations}'; is_deeply $r->{startLocations}, [0], 'align->{startLocations}'; is_deeply $r->{alignment}, [3, 0, 0, 0, 3, 0, 2], 'align->{alignment}'; + +my $cigar; +$cigar = to_cigar $r->{alignment}; +is $cigar, '6M1D', 'to_cigar'; +$cigar = to_cigar $r->{alignment}, EDLIB_CIGAR_EXTENDED; +is $cigar, '1X3=1X1=1D', 'to_cigar (extended)'; -- 2.39.2