blib/lib/String/Alignment.pm

filter: Statementstmt 88.0% 81 / 92 Branchbran 88.4% 46 / 52 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 6 / 6 Subroutinesub 77.7% 7 / 9 Podpod 33.3% 2 / 6 totaltotal 86.7% 151 / 174 CC 32 SCAR 27.9
CC
32
Cov
89%
similarity_print
30.0
calculate_matrix
21.0
get_align_result
7.0
CRAP 16.2
↑ Summary
1 package String::Alignment;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use List::Util qw(max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 String::Alignment - Pair Sentence Alignment
10  
11 =head1 VERSION
12  
13 Version 0.01
14  
15 =cut
16  
17 4 our $VERSION = '0.01';
18  
19 =head1 SYNOPSIS
20  
21 This module process string alignment.
22 Now it provide two kind of alignment method, Global and Local Alignment.
23  
24 use String::Alignment;
25  
26 use String::Alignment qw(do_alignment);
27  
28 # local alignment
29 my $result = do_alignment($s1,$s2,1);
30  
31 # global alignment
32 my $result = do_alignment($s1,$s2);
33  
34 =head1 EXPORT
35  
36 =cut
37  
38 4 require Exporter;
39 4 our @ISA = qw(Exporter);
40 4 our @EXPORT_OK = qw(do_alignment);
41  
42 =head1 BUILD-IN VARIABLES
43  
44 =cut
45  
46 4 my ($s1,$s2); # string1, string2
47 4 my (@sa1, @sa2); # string array 1, string array 2
48  
49 4 my ($len_s1, $len_s2) = (0,0); # length of s1/s2
50 4 my $is_local = 1; # 0 for global alignment
51  
52 4 my %table; # Dynamic Programming Table
53  
54 my $max_len; # for global
55 4 my %best; # Best path, for local
56  
57 =head1 FUNCTIONS
58  
59 =cut
60  
61 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=0.0
Pod undocumented
62 # print STDERR "I'm loaded\n";
63 }
64  
65 3 ▶sub do_alignment {
Subroutine do_alignment: called CC=2 SCAR=6.9
Pod undocumented
66 3 $s1 = shift;
67 3 $s2 = shift;
68 3 $is_local = shift;
69 3 ▶ $is_local = 0 unless defined($is_local);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $is_local 1 2 3
70 3 give_string_pair($s1,$s2);
71 3 calculate_matrix();
72 # similarity_print();
73 3 return get_align_result();
74 }
75 =head2 give_string_pair
76  
77 =cut
78  
79 3 ▶sub give_string_pair {
Subroutine give_string_pair: called CC=1 SCAR=0.0
Pod undocumented
80 3 $s1 = shift;
81 3 $s2 = shift;
82 3 @sa1 = split //,$s1;
83 3 @sa2 = split //,$s2;
84 3 %table = ();
85 3 %best = ();
86 3 $best{MAX} = 0;
87 3 $table{0}{0} = 0;
88 3 ($len_s1, $len_s2) = (0,0);
89 }
90  
91 =head2 cululate_matrix
92  
93 =cut
94  
95 3 ▶sub calculate_matrix {
Subroutine calculate_matrix: called CC=21 SCAR=30.4
Pod undocumented
96 3 ▶ if ($is_local) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_local) { } 2 1 3
97 2 $max_len = 0;
98 } else {
99 1 ▶ $max_len = scalar(@sa1) > scalar(@sa2) ? scalar(@sa1): scalar(@sa2); # for global
Branch: 1 branch
BranchTrueFalseTotal
scalar @sa1 > scalar @sa2 ? : 0 1 1
100 }
101 # print STDERR "max_len is ".$max_len."\n";
102 3 ▶ while ($len_s1 <= (scalar @sa1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_s1 <= scalar @sa1) 34 3 37
103 34 ▶ while ($len_s2 <= scalar @sa2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_s2 <= scalar @sa2) 402 34 436
104 402 my ($candidate1, $candidate2, $candidate3) = ($max_len,$max_len,$max_len);
105 402 ▶ if ($len_s1 > 0 and $len_s2 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_s1 > 0 and $len_s2 > 0) 337 65 402
Condition100%3/3
$len_s1 > 0 and $len_s2 > 0
!ll&&!rl&&r
3431337
MC/DC100%2/2
$len_s1 > 0 and $len_s2 > 0
$len_s1 > 0 $len_s2 > 0
Truth table100%3/3
A and B
A= $len_s1 > 0B= $len_s2 > 0
ABresult
0X0
100
111
106 # if match, we add 1 for local, 0 for global
107 # else (not matched), we add -1 for local, 1 for global
108 337 ▶ $candidate1 = int($table{$len_s1-1}{$len_s2-1}) +
Branch: 2 branches
BranchTrueFalseTotal
$is_local ? : 288 49 337
$sa1[$len_s1 - 1] eq $sa2[$len_s2 - 1] ? : 37 300 337
109 ( $is_local ? 1: -1) *
110 ( ( $sa1[$len_s1-1] eq $sa2[$len_s2-1] )? 1+(-1+$is_local) : -1 )
111 ;
112 }
113 402 ▶ if ($len_s1 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_s1 > 0) 368 34 402
114 368 ▶ $candidate2 = int($table{$len_s1-1}{$len_s2}) +
Branch: 1 branch
BranchTrueFalseTotal
$is_local ? : 312 56 368
115 ( $is_local ? (-1) : 1);
116 }
117 402 ▶ if ($len_s2 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_s2 > 0) 368 34 402
118 368 ▶ $candidate3 = int($table{$len_s1}{$len_s2 - 1}) +
Branch: 1 branch
BranchTrueFalseTotal
$is_local ? : 312 56 368
119 ( $is_local ? (-1) : 1);
120 }
121 # print STDERR "setting ($len_s1,$len_s2)...";
122 # print STDERR "(".$candidate1."\t".$candidate2."\t".$candidate3.")\n";
123 402 ▶ if ($is_local) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_local) { } 338 64 402
124 338 ▶ $table{$len_s1}{$len_s2} = max (
Branch: 1 branch
BranchTrueFalseTotal
if $len_s1 > 0 or $len_s2 > 0 336 2 338
Condition100%3/3
$len_s1 > 0 or $len_s2 > 0
l!l&&r!l&&!r
312242
MC/DC100%2/2
$len_s1 > 0 or $len_s2 > 0
$len_s1 > 0 $len_s2 > 0
Truth table100%3/3
A or B
A= $len_s1 > 0B= $len_s2 > 0
ABresult
1X1
011
000
125 $candidate1, $candidate2, $candidate3, 0
126 ) if ($len_s1 > 0 or $len_s2 > 0);
127 338 ▶ $best{X} = $len_s1 if $best{MAX} <= $table{$len_s1}{$len_s2};
Branch: 1 branch
BranchTrueFalseTotal
if $best{"MAX"} <= $table{$len_s1}{$len_s2} 95 243 338
128 338 ▶ $best{Y} = $len_s2 if $best{MAX} <= $table{$len_s1}{$len_s2};
Branch: 1 branch
BranchTrueFalseTotal
if $best{"MAX"} <= $table{$len_s1}{$len_s2} 95 243 338
129 338 ▶ $best{MAX} = $table{$len_s1}{$len_s2} if $best{MAX} <= $table{$len_s1}{$len_s2};
Branch: 1 branch
BranchTrueFalseTotal
if $best{"MAX"} <= $table{$len_s1}{$len_s2} 95 243 338
130 } else { # global
131 64 ▶ $table{$len_s1}{$len_s2} = min (
Branch: 1 branch
BranchTrueFalseTotal
if $len_s1 > 0 or $len_s2 > 0 63 1 64
Condition100%3/3
$len_s1 > 0 or $len_s2 > 0
l!l&&r!l&&!r
5671
MC/DC100%2/2
$len_s1 > 0 or $len_s2 > 0
$len_s1 > 0 $len_s2 > 0
Truth table100%3/3
A or B
A= $len_s1 > 0B= $len_s2 > 0
ABresult
1X1
011
000
132 $candidate1, $candidate2, $candidate3
133 ) if ($len_s1 > 0 or $len_s2 > 0);
134 }
135 402 $len_s2 +=1;
136 }
137 34 $len_s2 = 0;
138 34 $len_s1 +=1;
139 }
140 }
141  
142 =head2 similarity_print
143  
144 =cut
145  
146 0 ▶sub similarity_print {
Subroutine similarity_print: not called CC=5 SCAR=34.0
Pod documented
147 0 print STDERR "\n \t \t".join("\t",@sa2)."\n";
148 0 for my $key (sort {int($a) <=> int($b)}(keys %table)) {
149 0 ▶ print STDERR $sa1[$key-1]."\t" if $key > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $key > 0 0 0 0
150 0 ▶ print STDERR " \t" unless $key > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $key > 0 0 0 0
151 0 for my $subkey (sort {int($a) <=> int($b)} (keys %{$table{$key}})) {
152 0 print STDERR $table{$key}{$subkey}."\t";
153 }
154 0 print STDERR "\n";
155 }
156 };
157  
158 =head2 get_align_result
159  
160 =cut
161  
162 3 ▶sub get_align_result {
Subroutine get_align_result: called CC=7 SCAR=19.5
Pod documented
163 3 my ($i, $j) = (0, 0);
164 3 my (@as1, @as2);
165 3 my $baseline = 0;
166 3 ▶ if ($is_local) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_local) { } 2 1 3
167 2 $i = $best{X};
168 2 $j = $best{Y};
169 } else {
170 1 $i = scalar @sa1;
171 1 $j = scalar @sa2;
172 }
173 3 ▶ while ( $table{$i}{$j} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table{$i}{$j} > 0) 26 3 29
174 26 ▶ if ($is_local) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_local) { } 18 8 26
175 18 $baseline = max($table{$i-1}{$j-1},$table{$i-1}{$j},$table{$i}{$j-1});
176 } else {
177 8 $baseline = min($table{$i-1}{$j-1},$table{$i-1}{$j},$table{$i}{$j-1});
178 }
179 26 ▶ if ($table{$i-1}{$j-1} == $baseline) {
Branch: 3 branches
BranchTrueFalseTotal
if ($table{$i - 1}{$j - 1} == $baseline) { } 23 3 26
elsif ($table{$i}{$j - 1} == $baseline) { } 2 1 3
elsif ($table{$i - 1}{$j} == $baseline) { } 1 0 1
180 23 push @as1, $sa1[$i-1];
181 23 push @as2, $sa2[$j-1];
182 23 $i--;
183 23 $j--;
184 } elsif ($table{$i}{$j-1} == $baseline) {
185 2 push @as1, "-"; # gap
186 2 push @as2, $sa2[$j-1];
187 2 $j--;
188 } elsif ($table{$i-1}{$j} == $baseline) {
189 1 push @as1, $sa1[$i-1];
190 1 push @as2, "-"; # gap
191 1 $i--;
192 } else {
193 0 die $!;
194 }
195 }
196 3 return ( join ("",reverse @as2)."\t".join ("",reverse @as1) );
197 }
198  
199 =head1 AUTHOR
200  
201 Cheng-Lung Sung, C<< <clsung@cpan.org> >>
202  
203 =head1 BUGS
204  
205 Please report any bugs or feature requests to
206 C<bug-string-alignment@rt.cpan.org>, or through the web interface at
207 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=String-Alignment>.
208 I will be notified, and then you'll automatically be notified of progress on
209 your bug as I make changes.
210  
211 =head1 ACKNOWLEDGEMENTS
212  
213 =head1 COPYRIGHT & LICENSE
214  
215 Copyright 2006 Cheng-Lung Sung, All Rights Reserved.
216  
217 This program is free software; you can redistribute it and/or modify it
218 under the same terms as Perl itself.
219  
220 =cut
221  
222 4 1; # End of String::Alignment
↑ Summary