| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package FASTX::sw; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# file: Sequence/Alignment.pm |
|
5
|
|
|
|
|
|
|
|
|
6
|
13
|
|
|
13
|
|
798
|
use strict; |
|
|
13
|
|
|
|
|
24
|
|
|
|
13
|
|
|
|
|
384
|
|
|
7
|
13
|
|
|
13
|
|
62
|
use Carp; |
|
|
13
|
|
|
|
|
24
|
|
|
|
13
|
|
|
|
|
691
|
|
|
8
|
13
|
|
|
13
|
|
71
|
use vars '@DEFAULTS'; |
|
|
13
|
|
|
|
|
23
|
|
|
|
13
|
|
|
|
|
654
|
|
|
9
|
13
|
|
|
13
|
|
101
|
use constant DEBUG=>0; |
|
|
13
|
|
|
|
|
32
|
|
|
|
13
|
|
|
|
|
1003
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
13
|
|
|
13
|
|
79
|
use vars qw(@ISA @EXPORT @EXPORT_OK); |
|
|
13
|
|
|
|
|
24
|
|
|
|
13
|
|
|
|
|
1402
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
require Exporter; |
|
14
|
|
|
|
|
|
|
@ISA = 'Exporter'; |
|
15
|
|
|
|
|
|
|
@EXPORT = (); |
|
16
|
|
|
|
|
|
|
@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
|
|
|
|
|
749
|
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
|
|
|
13
|
|
82
|
}; |
|
|
13
|
|
|
|
|
25
|
|
|
30
|
|
|
|
|
|
|
|
|
31
|
13
|
|
|
13
|
|
317
|
use constant SCORE => 0; |
|
|
13
|
|
|
|
|
33
|
|
|
|
13
|
|
|
|
|
649
|
|
|
32
|
13
|
|
|
13
|
|
70
|
use constant EVENT => 1; |
|
|
13
|
|
|
|
|
24
|
|
|
|
13
|
|
|
|
|
559
|
|
|
33
|
13
|
|
|
13
|
|
65
|
use constant EXTEND => 0; |
|
|
13
|
|
|
|
|
28
|
|
|
|
13
|
|
|
|
|
624
|
|
|
34
|
13
|
|
|
13
|
|
96
|
use constant GAP_SRC => 1; |
|
|
13
|
|
|
|
|
23
|
|
|
|
13
|
|
|
|
|
598
|
|
|
35
|
13
|
|
|
13
|
|
82
|
use constant GAP_TGT => 2; |
|
|
13
|
|
|
|
|
24
|
|
|
|
13
|
|
|
|
|
18208
|
|
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
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
|
|
|
2
|
1
|
582
|
my ($class,$src,$tgt,$matrix) = @_; |
|
44
|
2
|
50
|
33
|
|
|
15
|
croak 'Usage: Realign->new($src,$tgt [,\%matrix])' |
|
45
|
|
|
|
|
|
|
unless $src && $tgt; |
|
46
|
2
|
|
50
|
|
|
12
|
my $self = bless { |
|
47
|
|
|
|
|
|
|
src => $src, |
|
48
|
|
|
|
|
|
|
target => $tgt, |
|
49
|
|
|
|
|
|
|
matrix => $matrix || {}, |
|
50
|
|
|
|
|
|
|
},$class; |
|
51
|
2
|
|
|
|
|
5
|
my $implementor = $class; |
|
52
|
|
|
|
|
|
|
|
|
53
|
2
|
|
|
|
|
20
|
my ($score,$alignment) = $implementor->_do_alignment($src,$tgt,$self->{matrix}); |
|
54
|
2
|
|
|
|
|
292
|
$self->{score} = $score; |
|
55
|
2
|
|
|
|
|
7
|
$self->{alignment} = $alignment; |
|
56
|
2
|
|
|
|
|
15
|
return $self; |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# return the score of the aligned region |
|
62
|
0
|
|
|
0
|
1
|
0
|
sub score { return shift()->{'score'}; } |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# return the start of the aligned region |
|
67
|
|
|
|
|
|
|
sub start { |
|
68
|
0
|
|
|
0
|
1
|
0
|
return shift()->{'alignment'}->[0]; |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# return the end of the aligned region |
|
74
|
|
|
|
|
|
|
sub end { |
|
75
|
0
|
|
|
0
|
1
|
0
|
my $alignment = shift()->{'alignment'}; |
|
76
|
0
|
|
|
|
|
0
|
return $alignment->[$#$alignment]; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# return the alignment as an array |
|
82
|
0
|
|
|
0
|
1
|
0
|
sub alignment { shift()->{'alignment'}; } |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# return the alignment as three padded strings for pretty-printing, etc. |
|
87
|
|
|
|
|
|
|
sub pads { |
|
88
|
2
|
|
|
2
|
1
|
10
|
my ($align,$src,$tgt) = @{shift()}{'alignment','src','target'}; |
|
|
2
|
|
|
|
|
9
|
|
|
89
|
2
|
|
|
|
|
12
|
my ($ps,$pt,$last); |
|
90
|
2
|
50
|
|
|
|
21
|
$ps = '-' x ($align->[0]) if defined $align->[0]; # pad up the source |
|
91
|
2
|
50
|
|
|
|
11
|
$pt = substr($tgt,0,$align->[0]) if defined $align->[0]; |
|
92
|
2
|
|
100
|
|
|
9
|
$last = $align->[0] || 0; |
|
93
|
2
|
|
|
|
|
12
|
for (my $i=0;$i<@$align;$i++) { |
|
94
|
489
|
|
|
|
|
529
|
my $t = $align->[$i]; |
|
95
|
489
|
100
|
|
|
|
610
|
if (defined $t) { |
|
96
|
488
|
50
|
|
|
|
631
|
$pt .= $t-$last > 1 ? substr($tgt,$last+1,$t-$last): substr($tgt,$t,1); |
|
97
|
488
|
|
|
|
|
530
|
$ps .= '-' x ($t-$last-1); |
|
98
|
488
|
|
|
|
|
462
|
$last = $t; |
|
99
|
|
|
|
|
|
|
} else { |
|
100
|
1
|
|
|
|
|
4
|
$pt .= '-'; |
|
101
|
|
|
|
|
|
|
} |
|
102
|
489
|
|
|
|
|
697
|
$ps .= substr($src,$i,1); |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
# clean up the ends |
|
105
|
2
|
|
|
|
|
8
|
$ps .= substr($src,@$align); |
|
106
|
2
|
|
|
|
|
6
|
$pt .= substr($tgt,$last+1); |
|
107
|
2
|
50
|
|
|
|
11812
|
$pt .= '-' x (length($ps) - length($pt)) if length($ps) > length($pt); |
|
108
|
2
|
50
|
|
|
|
31
|
$ps .= '-' x (length($pt) - length($ps)) unless length($ps) > length($pt); |
|
109
|
|
|
|
|
|
|
my $match = join('', |
|
110
|
2
|
100
|
|
|
|
77
|
map { uc substr($ps,$_,1) eq uc substr($pt,$_,1) ? '|' : ' ' } |
|
|
819
|
|
|
|
|
1566
|
|
|
111
|
|
|
|
|
|
|
(0..length($pt)-1)); |
|
112
|
2
|
|
|
|
|
50
|
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
|
|
|
0
|
1
|
0
|
my ($seq1,$seq2,$matrix) = @_; |
|
122
|
0
|
|
|
|
|
0
|
my $align = __PACKAGE__->new($seq1,$seq2,$matrix); |
|
123
|
0
|
|
|
|
|
0
|
return $align->pads; |
|
124
|
|
|
|
|
|
|
} |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub align_segs { |
|
129
|
0
|
|
|
0
|
1
|
0
|
my ($gap1,$align,$gap2) = align(@_); |
|
130
|
0
|
|
|
|
|
0
|
return __PACKAGE__->pads_to_segments($gap1,$align,$gap2); |
|
131
|
|
|
|
|
|
|
} |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub pads_to_segments { |
|
135
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
136
|
0
|
|
|
|
|
0
|
my ($gap1,$align,$gap2) = @_; |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
# create arrays that map residue positions to gap positions |
|
139
|
0
|
|
|
|
|
0
|
my @maps; |
|
140
|
0
|
|
|
|
|
0
|
for my $seq ($gap1,$gap2) { |
|
141
|
0
|
|
|
|
|
0
|
my @seq = split '',$seq; |
|
142
|
0
|
|
|
|
|
0
|
my @map; |
|
143
|
0
|
|
|
|
|
0
|
my $residue = 0; |
|
144
|
0
|
|
|
|
|
0
|
for (my $i=0;$i<@seq;$i++) { |
|
145
|
0
|
|
|
|
|
0
|
$map[$i] = $residue; |
|
146
|
0
|
0
|
|
|
|
0
|
$residue++ if $seq[$i] ne '-'; |
|
147
|
|
|
|
|
|
|
} |
|
148
|
0
|
|
|
|
|
0
|
push @maps,\@map; |
|
149
|
|
|
|
|
|
|
} |
|
150
|
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
0
|
my @result; |
|
152
|
0
|
|
|
|
|
0
|
while ($align =~ /(\S+)/g) { |
|
153
|
0
|
|
|
|
|
0
|
my $align_end = pos($align) - 1; |
|
154
|
0
|
|
|
|
|
0
|
my $align_start = $align_end - length($1) + 1; |
|
155
|
0
|
|
|
|
|
0
|
push @result,[@{$maps[0]}[$align_start,$align_end], |
|
156
|
0
|
|
|
|
|
0
|
@{$maps[1]}[$align_start,$align_end]]; |
|
|
0
|
|
|
|
|
0
|
|
|
157
|
|
|
|
|
|
|
} |
|
158
|
0
|
0
|
|
|
|
0
|
return wantarray ? @result : \@result; |
|
159
|
|
|
|
|
|
|
} |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub _do_alignment { |
|
162
|
2
|
|
|
2
|
|
4
|
my $class = shift; |
|
163
|
2
|
|
|
|
|
9
|
local $^W = 0; |
|
164
|
2
|
|
|
|
|
5
|
my($src,$tgt,$custom_matrix) = @_; |
|
165
|
2
|
|
|
|
|
2
|
my @alignment; |
|
166
|
2
|
|
50
|
|
|
7
|
$custom_matrix ||= {}; |
|
167
|
2
|
|
|
|
|
3
|
my %matrix = (%{DEFAULT_MATRIX()},%$custom_matrix); |
|
|
2
|
|
|
|
|
12
|
|
|
168
|
|
|
|
|
|
|
|
|
169
|
2
|
|
|
|
|
6
|
my ($max_score,$max_row,$max_col); |
|
170
|
2
|
|
|
|
|
19
|
my $scores = [([0,EXTEND])x(length($tgt)+1)]; |
|
171
|
|
|
|
|
|
|
|
|
172
|
2
|
|
|
|
|
3
|
print join(' ',map {sprintf("%-4s",$_)} (' ',split('',$tgt))),"\n" if DEBUG; |
|
173
|
2
|
|
|
|
|
4
|
my $wildcard = $matrix{wildcard}; |
|
174
|
2
|
|
|
|
|
7
|
for (my $row=0;$row
|
|
175
|
624
|
|
|
|
|
2315
|
my $s = uc substr($src,$row,1); |
|
176
|
624
|
|
|
|
|
1938
|
my @row = ([0,EXTEND]); |
|
177
|
624
|
|
|
|
|
1529
|
for (my $col=0;$col
|
|
178
|
414456
|
|
|
|
|
530671
|
my $t = uc substr($tgt,$col,1); |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# what happens if we extend the both strands one character? |
|
181
|
414456
|
|
|
|
|
421070
|
my $extend = $scores->[$col][SCORE]; |
|
182
|
|
|
|
|
|
|
$extend += ($t eq $wildcard || $s eq $wildcard) ? $matrix{wildcard_match} : |
|
183
|
|
|
|
|
|
|
($t eq $s) ? $matrix{match} : |
|
184
|
414456
|
100
|
33
|
|
|
971926
|
$matrix{mismatch}; |
|
|
|
50
|
|
|
|
|
|
|
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
|
100
|
|
|
|
630779
|
: $matrix{gap}); |
|
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
|
100
|
|
|
|
636225
|
: $matrix{gap}); |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
# find the best score among the possibilities |
|
195
|
414456
|
|
|
|
|
386517
|
my $score; |
|
196
|
414456
|
100
|
100
|
|
|
758961
|
if ($gap_src >= $gap_tgt && $gap_src >= $extend) { |
|
|
|
100
|
|
|
|
|
|
|
197
|
197837
|
|
|
|
|
278969
|
$score = [$gap_src,GAP_SRC]; |
|
198
|
|
|
|
|
|
|
} |
|
199
|
|
|
|
|
|
|
elsif ($gap_tgt >= $extend) { |
|
200
|
132393
|
|
|
|
|
176743
|
$score = [$gap_tgt,GAP_TGT]; |
|
201
|
|
|
|
|
|
|
} |
|
202
|
|
|
|
|
|
|
else { |
|
203
|
84226
|
|
|
|
|
125846
|
$score = [$extend,EXTEND]; |
|
204
|
|
|
|
|
|
|
} |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
# save it for posterity |
|
207
|
414456
|
|
|
|
|
475669
|
push(@row,$score); |
|
208
|
414456
|
100
|
|
|
|
772987
|
($max_score,$max_row,$max_col) = ($score->[SCORE],$row,$col) if $score->[SCORE] >= $max_score; |
|
209
|
|
|
|
|
|
|
} |
|
210
|
624
|
|
|
|
|
732
|
print join(' ',($s,map {sprintf("%4d",$_->[SCORE])} @row[1..$#row])),"\n" if DEBUG; |
|
211
|
624
|
|
|
|
|
16188
|
$scores = \@row; |
|
212
|
624
|
|
|
|
|
49394
|
push(@alignment,[@row[1..$#row]]); |
|
213
|
|
|
|
|
|
|
} |
|
214
|
2
|
|
|
|
|
17
|
my $alignment = $class->_trace_back($max_row,$max_col,\@alignment); |
|
215
|
2
|
|
|
|
|
6
|
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
|
|
|
|
|
46960
|
return ($max_score,$alignment); |
|
224
|
|
|
|
|
|
|
} |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
sub _trace_back { |
|
227
|
2
|
|
|
2
|
|
5
|
my $self = shift; |
|
228
|
2
|
|
|
|
|
7
|
my ($row,$col,$m) = @_; |
|
229
|
2
|
|
|
|
|
4
|
my @alignment; |
|
230
|
2
|
|
66
|
|
|
15
|
while ($row >= 0 && $col >= 0) { |
|
231
|
489
|
|
|
|
|
422
|
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
|
|
|
|
|
471
|
$alignment[$row] = $col; |
|
233
|
489
|
|
|
|
|
1161
|
my $score = $m->[$row][$col]; |
|
234
|
489
|
100
|
|
|
|
1444
|
$row--,$col--,next if $score->[EVENT] == EXTEND; |
|
235
|
1
|
50
|
|
|
|
9
|
$col--, next if $score->[EVENT] == GAP_TGT; |
|
236
|
1
|
50
|
|
|
|
8
|
undef($alignment[$row]),$row--,next if $score->[EVENT] == GAP_SRC; |
|
237
|
|
|
|
|
|
|
} |
|
238
|
2
|
|
|
|
|
7
|
return \@alignment; |
|
239
|
|
|
|
|
|
|
} |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
1; |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
__END__ |