sw.pm

filter: Statementstmt 79.4% 112 / 141 Branchbran 67.3% 35 / 52 Conditioncond 61.1% 11 / 18 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 68.1% 15 / 22 Podpod 100% 9 / 9 totaltotal 73.5% 186 / 253 CC 35 SCAR 23.7
CC
35
Cov
75%
pads_to_segments
42.0
_do_alignment
13.1
pads
10.2
CRAP 10.7
↑ Summary Abi.pm »
1 package FASTX::sw;
2  
3
4 # file: Sequence/Alignment.pm
5
6 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use vars '@DEFAULTS';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use constant DEBUG=>0;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 13 ▶use vars qw(@ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 13 require Exporter;
14 13 @ISA = 'Exporter';
15 13 @EXPORT = ();
16 13 @EXPORT_OK = qw(align align_segs);
17
18 # The default scoring matrix introduces a penalty of -2 for opening
19 # a gap in the sequence, but no penalty for extending an already opened
20 # gap. A nucleotide mismatch has a penalty of -1, and a match has a
21 # positive score of +1. An ambiguous nucleotide ("N") can match
22 # anything with no penalty.
23 13 use constant DEFAULT_MATRIX => { 'wildcard_match' => 0,
24 'match' => 1,
25 'mismatch' => -1,
26 'gap' => -4,
27 'gap_extend' => 0,
28 'wildcard' => 'N',
29 13 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
30
31 13 ▶use constant SCORE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 13 ▶use constant EVENT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 13 ▶use constant EXTEND => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 13 ▶use constant GAP_SRC => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 13 ▶use constant GAP_TGT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
36
37 13 my @EVENTS = qw(extend gap_src gap_tgt);
38  
39
40
41 # Construct a new alignment object. May be time consuming.
42 sub new {
43 2 ▶ my ($class,$src,$tgt,$matrix) = @_;
Subroutine new: called CC=4 SCAR=14.6
Pod documented
44 2 ▶ croak 'Usage: Realign->new($src,$tgt [,\%matrix])'
Branch: 1 branch
BranchTrueFalseTotal
unless $src and $tgt 0 2 2
Condition33%1/3
$src and $tgt
!ll&&!rl&&r
002
MC/DC0%0/2
$src and $tgt
$src $tgt
Truth table33%1/3
A and B
A= $srcB= $tgt
ABresult
0X0
100
111
45 unless $src && $tgt;
46 2 ▶ my $self = bless {
Condition50%1/2
$matrix || {}
l!l
02
MC/DC0%0/1
$matrix || {}
$matrix
Truth table50%1/2
A
A= $matrix
Aresult
11
00
47 src => $src,
48 target => $tgt,
49 matrix => $matrix || {},
50 },$class;
51 2 my $implementor = $class;
52  
53 2 my ($score,$alignment) = $implementor->_do_alignment($src,$tgt,$self->{matrix});
54 2 $self->{score} = $score;
55 2 $self->{alignment} = $alignment;
56 2 return $self;
57 }
58  
59
60
61 # return the score of the aligned region
62 0 ▶sub score { return shift()->{'score'}; }
Subroutine score: not called CC=1 SCAR=6.9
Pod documented
63  
64
65
66 # return the start of the aligned region
67 sub start {
68 0 ▶ return shift()->{'alignment'}->[0];
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
69 }
70  
71
72
73 # return the end of the aligned region
74 sub end {
75 0 ▶ my $alignment = shift()->{'alignment'};
Subroutine end: not called CC=1 SCAR=6.9
Pod documented
76 0 return $alignment->[$#$alignment];
77 }
78  
79
80
81 # return the alignment as an array
82 0 ▶sub alignment { shift()->{'alignment'}; }
Subroutine alignment: not called CC=1 SCAR=6.9
Pod documented
83  
84
85
86 # return the alignment as three padded strings for pretty-printing, etc.
87 sub pads {
88 2 ▶ my ($align,$src,$tgt) = @{shift()}{'alignment','src','target'};
Subroutine pads: called CC=10 SCAR=23.2
Pod documented
89 2 my ($ps,$pt,$last);
90 2 ▶ $ps = '-' x ($align->[0]) if defined $align->[0]; # pad up the source
Branch: 1 branch
BranchTrueFalseTotal
if defined $align->[0] 2 0 2
91 2 ▶ $pt = substr($tgt,0,$align->[0]) if defined $align->[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $align->[0] 2 0 2
92 2 ▶ $last = $align->[0] || 0;
Condition100%2/2
$align->[0] || 0
l!l
11
MC/DC100%1/1
$align->[0] || 0
$align->[0]
Truth table100%2/2
A
A= $align->[0]
Aresult
11
00
93 2 ▶ for (my $i=0;$i<@$align;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$align) 489 2 491
94 489 my $t = $align->[$i];
95 489 ▶ if (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t) { } 488 1 489
96 488 ▶ $pt .= $t-$last > 1 ? substr($tgt,$last+1,$t-$last): substr($tgt,$t,1);
Branch: 1 branch
BranchTrueFalseTotal
$t - $last > 1 ? : 0 488 488
97 488 $ps .= '-' x ($t-$last-1);
98 488 $last = $t;
99 } else {
100 1 $pt .= '-';
101 }
102 489 $ps .= substr($src,$i,1);
103 }
104 # clean up the ends
105 2 $ps .= substr($src,@$align);
106 2 $pt .= substr($tgt,$last+1);
107 2 ▶ $pt .= '-' x (length($ps) - length($pt)) if length($ps) > length($pt);
Branch: 1 branch
BranchTrueFalseTotal
if length $ps > length $pt 2 0 2
108 2 ▶ $ps .= '-' x (length($pt) - length($ps)) unless length($ps) > length($pt);
Branch: 1 branch
BranchTrueFalseTotal
unless length $ps > length $pt 2 0 2
109 my $match = join('',
110 2 ▶ map { uc substr($ps,$_,1) eq uc substr($pt,$_,1) ? '|' : ' ' }
Branch: 1 branch
BranchTrueFalseTotal
uc substr($ps, $_, 1) eq uc substr($pt, $_, 1) ? : 488 331 819
111 (0..length($pt)-1));
112 2 return ($ps,$match,$pt);
113 }
114
115
116
117 # take two sequences as strings, align them and return
118 # a three element array consisting of gapped seq1, match string, and
119 # gapped seq2.
120 sub align {
121 0 ▶ my ($seq1,$seq2,$matrix) = @_;
Subroutine align: not called CC=1 SCAR=6.9
Pod documented
122 0 my $align = __PACKAGE__->new($seq1,$seq2,$matrix);
123 0 return $align->pads;
124 }
125  
126
127
128 sub align_segs {
129 0 ▶ my ($gap1,$align,$gap2) = align(@_);
Subroutine align_segs: not called CC=1 SCAR=6.9
Pod documented
130 0 return __PACKAGE__->pads_to_segments($gap1,$align,$gap2);
131 }
132
133
134 sub pads_to_segments {
135 0 ▶ my $self = shift;
Subroutine pads_to_segments: not called CC=6 SCAR=37.4
Pod documented
136 0 my ($gap1,$align,$gap2) = @_;
137
138 # create arrays that map residue positions to gap positions
139 0 my @maps;
140 0 for my $seq ($gap1,$gap2) {
141 0 my @seq = split '',$seq;
142 0 my @map;
143 0 my $residue = 0;
144 0 ▶ for (my $i=0;$i<@seq;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @seq) 0 0 0
145 0 $map[$i] = $residue;
146 0 ▶ $residue++ if $seq[$i] ne '-';
Branch: 1 branch
BranchTrueFalseTotal
if $seq[$i] ne "-" 0 0 0
147 }
148 0 push @maps,\@map;
149 }
150
151 0 my @result;
152 0 ▶ while ($align =~ /(\S+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($align =~ /(\S+)/g) 0 0 0
153 0 my $align_end = pos($align) - 1;
154 0 my $align_start = $align_end - length($1) + 1;
155 0 push @result,[@{$maps[0]}[$align_start,$align_end],
156 0 @{$maps[1]}[$align_start,$align_end]];
157 }
158 0 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
159 }
160
161 sub _do_alignment {
162 2 ▶ my $class = shift;
Subroutine _do_alignment: called CC=13 SCAR=25.7
163 2 local $^W = 0;
164 2 my($src,$tgt,$custom_matrix) = @_;
165 2 my @alignment;
166 2 ▶ $custom_matrix ||= {};
Condition50%1/2
$custom_matrix ||= {}
l!l
20
MC/DC0%0/1
$custom_matrix ||= {}
$custom_matrix
Truth table50%1/2
A
A= $custom_matrix
Aresult
11
00
167 2 my %matrix = (%{DEFAULT_MATRIX()},%$custom_matrix);
168
169 2 my ($max_score,$max_row,$max_col);
170 2 my $scores = [([0,EXTEND])x(length($tgt)+1)];
171
172 2 print join(' ',map {sprintf("%-4s",$_)} (' ',split('',$tgt))),"\n" if DEBUG;
173 2 my $wildcard = $matrix{wildcard};
174 2 ▶ for (my $row=0;$row<length($src);$row++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < length $src) 624 2 626
175 624 my $s = uc substr($src,$row,1);
176 624 my @row = ([0,EXTEND]);
177 624 ▶ for (my $col=0;$col<length($tgt);$col++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < length $tgt) 414456 624 415080
178 414456 my $t = uc substr($tgt,$col,1);
179
180 # what happens if we extend the both strands one character?
181 414456 my $extend = $scores->[$col][SCORE];
182 $extend += ($t eq $wildcard || $s eq $wildcard) ? $matrix{wildcard_match} :
183 ($t eq $s) ? $matrix{match} :
184 414456 ▶ $matrix{mismatch};
Branch: 2 branches
BranchTrueFalseTotal
$t eq $wildcard || $s eq $wildcard ? : 0 414456 414456
$t eq $s ? : 109063 305393 414456
Condition33%1/3
$t eq $wildcard or $s eq $wildcard
l!l&&r!l&&!r
00414456
MC/DC0%0/2
$t eq $wildcard or $s eq $wildcard
$t eq $wildcard $s eq $wildcard
Truth table33%1/3
A or B
A= $t eq $wildcardB= $s eq $wildcard
ABresult
1X1
011
000
185
186 # what happens if we extend the src strand one character, gapping the tgt?
187 my $gap_tgt = $row[$#row][SCORE] + (($row[$#row][EVENT]==GAP_TGT) ? $matrix{gap_extend}
188 414456 ▶ : $matrix{gap});
Branch: 1 branch
BranchTrueFalseTotal
$row[$#row][1] == 2 ? : 131918 282538 414456
189
190 # what happens if we extend the tgt strand one character, gapping the src?
191 my $gap_src = $scores->[$col+1][SCORE] + (($scores->[$col+1][EVENT] == GAP_SRC) ? $matrix{gap_extend}
192 414456 ▶ : $matrix{gap});
Branch: 1 branch
BranchTrueFalseTotal
$$scores[$col + 1][1] == 1 ? : 197325 217131 414456
193
194 # find the best score among the possibilities
195 414456 my $score;
196 414456 ▶ if ($gap_src >= $gap_tgt && $gap_src >= $extend) {
Branch: 2 branches
BranchTrueFalseTotal
if ($gap_src >= $gap_tgt and $gap_src >= $extend) { } 197837 216619 414456
elsif ($gap_tgt >= $extend) { } 132393 84226 216619
Condition100%3/3
$gap_src >= $gap_tgt and $gap_src >= $extend
!ll&&!rl&&r
17331443305197837
MC/DC100%2/2
$gap_src >= $gap_tgt and $gap_src >= $extend
$gap_src >= $gap_tgt $gap_src >= $extend
Truth table100%3/3
A and B
A= $gap_src >= $gap_tgtB= $gap_src >= $extend
ABresult
0X0
100
111
197 197837 $score = [$gap_src,GAP_SRC];
198 }
199 elsif ($gap_tgt >= $extend) {
200 132393 $score = [$gap_tgt,GAP_TGT];
201 }
202 else {
203 84226 $score = [$extend,EXTEND];
204 }
205
206 # save it for posterity
207 414456 push(@row,$score);
208 414456 ▶ ($max_score,$max_row,$max_col) = ($score->[SCORE],$row,$col) if $score->[SCORE] >= $max_score;
Branch: 1 branch
BranchTrueFalseTotal
if $score->[0] >= $max_score 706 413750 414456
209 }
210 624 print join(' ',($s,map {sprintf("%4d",$_->[SCORE])} @row[1..$#row])),"\n" if DEBUG;
211 624 $scores = \@row;
212 624 push(@alignment,[@row[1..$#row]]);
213 }
214 2 my $alignment = $class->_trace_back($max_row,$max_col,\@alignment);
215 2 if (DEBUG) {
216 for (my $i=0;$i<@$alignment;$i++) {
217 printf STDERR ("%3d %1s %3d %1s\n",
218 $i,substr($src,$i,1),
219 $alignment->[$i],defined $alignment->[$i] ? substr($tgt,$alignment->[$i],1):'');
220 }
221 }
222
223 2 return ($max_score,$alignment);
224 }
225
226 sub _trace_back {
227 2 ▶ my $self = shift;
Subroutine _trace_back: called CC=6 SCAR=18.1
228 2 my ($row,$col,$m) = @_;
229 2 my @alignment;
230 2 ▶ while ($row >= 0 && $col >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row >= 0 and $col >= 0) 489 2 491
Condition66%2/3
$row >= 0 and $col >= 0
!ll&&!rl&&r
20489
MC/DC50%1/2
$row >= 0 and $col >= 0
$row >= 0 $col >= 0
Truth table66%2/3
A and B
A= $row >= 0B= $col >= 0
ABresult
0X0
100
111
231 489 printf STDERR "row=%d, col=%d score=%d event=%s\n",$row,$col,$m->[$row][$col][SCORE],$EVENTS[$m->[$row][$col][EVENT]] if DEBUG;
232 489 $alignment[$row] = $col;
233 489 my $score = $m->[$row][$col];
234 489 ▶ $row--,$col--,next if $score->[EVENT] == EXTEND;
Branch: 1 branch
BranchTrueFalseTotal
if $score->[1] == 0 488 1 489
235 1 ▶ $col--, next if $score->[EVENT] == GAP_TGT;
Branch: 1 branch
BranchTrueFalseTotal
if $score->[1] == 2 0 1 1
236 1 ▶ undef($alignment[$row]),$row--,next if $score->[EVENT] == GAP_SRC;
Branch: 1 branch
BranchTrueFalseTotal
if $score->[1] == 1 1 0 1
237 }
238 2 return \@alignment;
239 }
240
241 13 1;
242  
243 __END__
↑ Summary Abi.pm »