blib/conf/plugins/Aligner.pm untested

filter: Statementstmt 0.0% 0 / 244 Branchbran 0.0% 0 / 110 Conditioncond 0.0% 0 / 30 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 418 CC 56 SCAR 70.8
CC
56
Cov
0%
dump
2070.0
configure_form
42.0
init
20.0
CRAP 1184.2
1 package Bio::Graphics::Browser2::Plugin::Aligner;
2 # $Id: Aligner.pm,v 1.13 2008-09-18 15:27:07 lstein Exp $
3  
4 use strict;
5 use Bio::Graphics::Browser2::Plugin;
6 use CGI qw(table a TR td th p popup_menu radio_group checkbox checkbox_group h1 h2 pre);
7 use Bio::Graphics::Browser2::Realign 'align_segs';
8 use Bio::Graphics::Browser2::PadAlignment;
9 use Bio::Graphics::Browser2::Util 'shellwords';
10  
11 use constant DEBUG => 0;
12 use constant DEFAULT_RAGGED_ENDS => (0,10,25,50,100,150,500);
13  
14 use vars '$VERSION','@ISA';
15 $VERSION = '0.23';
16 @ISA = qw(Bio::Graphics::Browser2::Plugin);
17  
18 use constant TARGET => 0;
19 use constant SRC_START => 1;
20 use constant SRC_END => 2;
21 use constant TGT_START => 3;
22 use constant TGT_END => 4;
23  
24 sub name { "Alignments" }
25  
26 sub description {
27 p("This plugin prints out a multiple alignment of the selected features.",
28 'It was written by',a({-href=>'mailto:lstein@cshl.org'},'Lincoln Stein.')
29 );
30 }
31  
32 sub init {
33 my $self = shift;
34 my $browser_conf = $self->browser_config;
35 my @alignable = shellwords($browser_conf->plugin_setting('alignable_tracks'));
36 @alignable = grep {$browser_conf->setting($_=>'draw_target') } $browser_conf->labels
37 unless @alignable;
38 $self->{alignable} = \@alignable;
39  
40 my @upcase = shellwords($browser_conf->plugin_setting('upcase_tracks'));
41 $self->{upcase} = \@upcase;
42  
43 my @ragged = shellwords($browser_conf->plugin_setting('ragged_ends'));
44 @ragged = DEFAULT_RAGGED_ENDS unless @ragged;
45 $self->{ragged} = \@ragged;
46  
47 $self->{upcase_default} = $browser_conf->plugin_setting('upcase_default');
48 $self->{align_default} = $browser_conf->plugin_setting('align_default')
49 ? [shellwords($browser_conf->plugin_setting('align_default'))]
50 : \@alignable;
51 $self->{ragged_default} = $browser_conf->plugin_setting('ragged_default');
52  
53 }
54  
55 sub config_defaults {
56 my $self = shift;
57 return { align => @{$self->{align_default}} ? $self->{align_default} : $self->{alignable},
58 upcase => $self->{upcase}[0]
59 };
60 }
61  
62 sub configure_form {
63 my $self = shift;
64 my $current = $self->configuration;
65 my $browser = $self->browser_config;
66 my $html;
67 if ($self->{upcase}) {
68 my %labels = map {$_ => $browser->setting($_=>'key') || $_} @{$self->{upcase}};
69 $html .= TR(
70 th('Features to render uppercase:'),
71 td(radio_group(-name => $self->config_name('upcase'),
72 -values => ['none',@{$self->{upcase}}],
73 -default => $current->{upcase} || $self->{upcase_default} || 'none',
74 -labels => \%labels,
75 @{$self->{upcase}} > 4 ? (-cols => 4) : ()
76 ))
77 );
78 }
79 if ($self->{alignable} && @{$self->{alignable}}) {
80 my %labels = map {$_ => $browser->setting($_=>'key') || $_} @{$self->{alignable}};
81 $html .= TR(
82 th('Features to include in alignment:'),
83 td(checkbox_group(-name => $self->config_name('align'),
84 -values => $self->{alignable},
85 -defaults => $current->{align},
86 -labels => \%labels,
87 @{$self->{alignable}} > 4 ? (-cols => 4) : ()
88 )));
89 }
90 $html .= TR(
91 th({-colspan=>2,-align=>'left'},
92 'Allow up to',popup_menu(-name => $self->config_name('ragged'),
93 -values => $self->{ragged},
94 -default => $current->{ragged} || $self->{ragged_default} || 0),
95 ' bp of unaligned sequence at ends.')
96 );
97 return $html ? table({-class=>'searchtitle'},$html) : undef;
98 }
99  
100 sub reconfigure {
101 my $self = shift;
102 my $current = $self->configuration;
103 my @align = $self->config_param('align');
104 my $upcase = $self->config_param('upcase');
105 $current->{align} = \@align;
106 $current->{upcase} = $upcase eq 'none' ? undef : $upcase;
107 $current->{ragged} = $self->config_param('ragged');
108 $current->{flip} = $self->config_param('flip');
109 }
110  
111 sub mime_type { 'text/html' }
112  
113 sub dump {
114 my $self = shift;
115 my $segment = shift;
116  
117 unless ($segment) {
118 print "No sequence specified.\n";
119 exit 0;
120 }
121  
122 my $database = $self->database;
123 my $browser = $self->browser_config;
124 my $configuration = $self->configuration;
125  
126 # $configuration->{flip} = $self->page_settings->{flip};
127  
128 my $flipped = $configuration->{flip} ? " (reverse complemented)" :'';
129 print h1("Alignments for $segment$flipped");
130  
131 my $ref_dna = lc $segment->dna;
132  
133 if ($segment->strand < 0) { # don't ask
134 $ref_dna = reversec($ref_dna);
135 $configuration->{flip} = 1;
136 }
137  
138 my ($abs_start,$abs_end) = ($segment->start,$segment->end);
139  
140 # do upcasing
141 if (my $upcase_track = $configuration->{upcase}) {
142 my @upcase_types = shellwords($browser->setting($upcase_track=>'feature'));
143 my @upcase_features = $segment->features(-types=>\@upcase_types);
144 for my $f (@upcase_features) {
145 my @segments = $f->segments;
146 @segments = $f unless @segments;
147 for my $s (@segments) {
148 my $upstart = $s->low-$abs_start;
149 my $uplength = $s->length;
150 $upstart = 0 if $upstart < 0;
151 $uplength = length($ref_dna) if $uplength > length($ref_dna);
152 substr($ref_dna,$upstart,$uplength) =~ tr/a-z/A-Z/;
153 }
154 }
155 }
156  
157 # here's where we handle aligned objects
158 my @feature_types = map {shellwords($browser->setting($_=>'feature'))} @{$configuration->{align}};
159 my @features = $segment->features(-types=>\@feature_types);
160  
161 my (@segments,%strands);
162  
163 for my $f (@features) {
164 warn "f strand = ",$f->strand if DEBUG;
165 my @s = $f->segments;
166 @s = $f unless @s;
167 @s = grep {$abs_start<=$_->abs_end && $abs_end>=$_->abs_start} @s;
168  
169 for my $s (@s) {
170 my $target = $s->target;
171 my ($src_start,$src_end) = ($s->start,$s->end);
172 my ($tgt_start,$tgt_end) = ($target->start,$target->end);
173  
174 my $flip_bug;
175  
176 unless (exists $strands{$target}) {
177 my $strand = $f->strand;
178 if ($tgt_start > $tgt_end) {
179 $strand = -1;
180 ($tgt_start,$tgt_end) = ($tgt_end,$tgt_start);
181 $flip_bug++;
182 }
183 $strands{$target} = $strand;
184 $strands{$target->seq_id} = $strand;
185 }
186  
187 # Realign the segment a bit
188 my ($sdna,$tdna) = ($s->dna,$target->dna);
189 if ($flip_bug) {
190 $sdna = reversec($sdna);
191 $tdna = reversec($tdna);
192 }
193 warn "raw alignment:\n" if DEBUG;
194 warn $sdna,"\n",$tdna,"\n" if DEBUG;
195 warn "Realigning [$target,$src_start,$src_end,$tgt_start,$tgt_end].\n" if DEBUG;
196 my @result = $self->realign($sdna,$tdna);
197 foreach (@result) {
198 warn "=========> [$target,@$_]\n" if DEBUG;
199 my $a = $strands{$target} >= 0 ? [$target->seq_id,$_->[0]+$src_start,$_->[1]+$src_start,$_->[2]+$tgt_start,$_->[3]+$tgt_start]
200 : [$target->seq_id,$src_end-$_->[1],$src_end-$_->[0],$_->[2]+$tgt_start,$_->[3]+$tgt_start];
201 warn "[$target,$_->[0]+$src_start,$_->[1]+$src_start,$tgt_end-$_->[3],$tgt_end-$_->[2]]" if DEBUG;
202 warn "=========> [@$a]\n" if DEBUG;
203 warn substr($sdna, $_->[0],$_->[1]-$_->[0]+1),"\n" if DEBUG;
204 warn substr($tdna,$_->[2],$_->[3]-$_->[2]+1),"\n" if DEBUG;
205 push @segments,$a;
206 }
207 }
208 }
209  
210 # We're now going to do all the alignments
211 my %clip;
212 for my $seg (@segments) {
213  
214 warn "clipping [@$seg]\n" if DEBUG;
215 my $target = $seg->[TARGET];
216  
217 # left clipping
218 if ( (my $delta = $seg->[SRC_START] - $abs_start) < 0 ) {
219 warn "clip left $delta" if DEBUG;
220 $seg->[SRC_START] = $abs_start;
221 if ($strands{$target} >= 0) {
222 $seg->[TGT_START] -= $delta;
223 }
224 warn "Left clipping gives [@$seg]\n" if DEBUG;
225 }
226  
227 # right clipping
228 if ( (my $delta = $abs_end - $seg->[SRC_END]) < 0) {
229 warn "clip right $delta" if DEBUG;
230 $seg->[SRC_END] = $abs_end;
231 if ($strands{$target} < 0) {
232 $seg->[TGT_START] -= $delta;
233 }
234 warn "Right clipping gives [@$seg]\n" if DEBUG;
235 }
236  
237 my $length = $seg->[SRC_END]-$seg->[SRC_START]+1;
238 $seg->[TGT_END] = $seg->[TGT_START]+$length-1;
239  
240 warn "Clipping gives [@$seg]\n" if DEBUG;
241 $clip{$target}{low} = $seg->[TGT_START]
242 if !defined $clip{$target}{low} || $clip{$target}{low} > $seg->[TGT_START];
243 $clip{$target}{high} = $seg->[TGT_END]
244 if !defined $clip{$target}{high} || $seg->[TGT_END] > $clip{$target}{high};
245 }
246  
247 my $ragged = $configuration->{ragged} || 0;
248  
249 # sort aligned sequences from left to right and store them in the data structure
250 # needed by Bio::Graphics::Browser2::PadAlignment
251 my @sequences = ($segment->seq_id => $ref_dna);
252  
253 my %seqs;
254 for my $t (sort {$clip{$a}{low}<=>$clip{$b}{low}} keys %clip) {
255  
256 # adjust for ragged ends
257 $clip{$t}{low} -= $ragged;
258 $clip{$t}{high} += $ragged;
259  
260 $clip{$t}{low} = 1 if $clip{$t}{low} < 1;
261  
262 my @order = $strands{$t}>=0?('low','high'):('high','low');
263 my $dna = lc $database->dna($t,@{$clip{$t}}{@order});
264 push @sequences,($t => $dna); # dna() api gives implicit reversec
265  
266 # sanity check - needed for adjusting for ragged ends
267 warn "$t low = $clip{$t}{low}, dna = $dna\n" if DEBUG;
268 warn "expected ",$clip{$t}{high}-$clip{$t}{low}+1," and got ",length($dna) if DEBUG;
269 $clip{$t}{high} = $clip{$t}{low}+length($dna)-1 if $clip{$t}{high} > $clip{$t}{low}+length($dna)-1;
270 }
271  
272 for my $seg (@segments) {
273 my ($target,$src_start,$src_end,$tgt_start,$tgt_end) = @$seg;
274 warn "clip high = $clip{$target}{high}" if DEBUG;
275 warn "was [$target,$src_start,$src_end,$tgt_start,$tgt_end]" if DEBUG;
276 $seg->[SRC_START] -= $abs_start;
277 $seg->[SRC_END] -= $abs_start;
278  
279 if ($strands{$target} >= 0) {
280 $seg->[TGT_START] -= $clip{$target}{low};
281 $seg->[TGT_END] -= $clip{$target}{low};
282 } else {
283 @{$seg}[TGT_START,TGT_END] = ($clip{$target}{high} - $seg->[TGT_END],
284 $clip{$target}{high} - $seg->[TGT_START]);
285 }
286 ($target,$src_start,$src_end,$tgt_start,$tgt_end) = @$seg;
287 warn "is [$target,$src_start,$src_end,$tgt_start,$tgt_end]" if DEBUG;
288 }
289  
290 # remove segments that got clipped out of existence
291 @segments = grep { $_->[SRC_START]<=$_->[SRC_END] } @segments;
292  
293 if (DEBUG) {
294 warn "DEBUG:";
295 my %sequences = @sequences;
296 foreach (@segments) {
297 my ($t,$s,$e,$ts,$te) = @$_;
298 warn "[@$_]\n";
299 warn substr($sequences{$segment->display_name},$s,$e-$s+1),"\n";
300 warn substr($sequences{$t},$ts,$te-$ts+1),"\n";
301 }
302 }
303  
304 my $align = Bio::Graphics::Browser2::PadAlignment->new(\@sequences,\@segments);
305 my %offsets = map {$_ => $strands{$_} >= 0 ? $clip{$_}{low} : -$clip{$_}{low}} keys %clip;
306 $offsets{$segment->display_name} = $abs_start;
307  
308 print pre($align->alignment(\%offsets,{show_mismatches => 1,
309 flip => $configuration->{flip}}
310 ));
311 }
312  
313 sub realign {
314 my $self = shift;
315 my ($src,$tgt) = @_;
316 warn join "\n",Bio::Graphics::Browser2::Realign::align($src,$tgt) if DEBUG;
317 return align_segs($src,$tgt);
318 }
319  
320 sub reversec {
321 my $dna = reverse shift;
322 $dna =~ tr/gatcGATC/ctagCTAG/;
323 $dna;
324 }
325  
326 1;
327  
328 __END__