blib/lib/Bio/Graphics/Karyotype.pm untested

filter: Statementstmt 0.0% 0 / 222 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 81 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 407 CC 26 SCAR 34.8
CC
26
Cov
0%
generate_panels
132.0
feature2link
20.0
hits_table
20.0
CRAP 32.4
1 package Bio::Graphics::Karyotype;
2  
3 # $Id$
4 # Utility class to create a display of a karyotype and a series of "hits" on the individual chromosomes
5 # Used for searching
6  
7  
8 use strict;
9 use Bio::Graphics::Panel;
10 use GD 'gdSmallFont';
11 use CGI qw(img div span b url table TR th td b escapeHTML a br);
12 use Carp 'croak';
13  
14 # there is a bug in the ideogram glyph that causes a core dump when
15 # drawing small chromosomes - this "fixes" the problem
16 use constant SUPPRESS_SMALL_CHROMOSOMES => 50; # suppress chromosomes smaller than 50 pixels
17  
18 sub new {
19 my $class = shift;
20 my %args = @_;
21 my $source = $args{source} or croak "source argument mandatory";
22 my $lang = $args{language};
23 return bless {
24 source => $source,
25 language => $lang,
26 },ref $class || $class;
27 }
28  
29 sub db {
30 my $self = shift;
31 my $source = $self->data_source;
32 my $db = $source->karyotype_setting('database');
33 return $db ? $source->open_database("$db:database") : $source->open_database();
34 }
35 sub data_source { shift->{source} }
36 sub language { shift->{language} }
37  
38 sub trans {
39 my $self = shift;
40 my $lang = $self->language or return '';
41 return $lang->tr(@_);
42 }
43  
44 sub chrom_type {
45 return shift->data_source->karyotype_setting('chromosome') || 'chromosome';
46 }
47  
48 sub chrom_width {
49 return shift->data_source->karyotype_setting('chrom_width') || 16;
50 }
51  
52 sub chrom_height {
53 return shift->data_source->karyotype_setting('chrom_height') || 140;
54 }
55  
56 sub chrom_background {
57 my $band_colors = shift->data_source->karyotype_setting('bgcolor')
58 || 'gneg:white gpos25:gray gpos75:darkgray gpos100:black gvar:var stalk:#666666';
59 }
60  
61 sub chrom_background_fallback {
62 my $band_colors = shift->data_source->karyotype_setting('bgfallback')
63 || 'yellow';
64 }
65  
66 sub add_hits {
67 my $self = shift;
68 my $features = shift;
69 $self->{hits} ||= {};
70  
71 for my $f (@$features) {
72 my $ref = $f->seq_id;
73 push @{$self->{hits}{$ref}},$f;
74 $self->{hit_count}++;
75 }
76  
77 }
78  
79 sub hit_count { shift->{hit_count} }
80  
81 sub seqid_order {
82 my $self = shift;
83 return $self->{seqid_order} if exists $self->{seqid_order};
84  
85 my @chromosomes = $self->chromosomes;
86 my $sort_sub = $self->sort_sub || \&by_chromosome_name;
87 my @sorted_chroms = sort $sort_sub @chromosomes;
88 my $i = 0;
89 my %order = map {$_->seq_id => $i++} @sorted_chroms;
90  
91 return $self->{seqid_order} = \%order;
92 }
93  
94 sub hits {
95 my $self = shift;
96 my $seq_id = shift;
97  
98 my $hits = $self->{hits} or return;
99 defined $seq_id or return map {@$_} values %{$hits};
100  
101 my $list = $self->{hits}{$seq_id} or return;
102 return @$list;
103 }
104  
105 # to sort chromosomes into their proper left->right order
106 sub sort_sub {
107 my $self = shift;
108 my $d = $self->{sort_sub};
109 $self->{sort_sub} = shift if @_;
110 $d;
111 }
112  
113 sub to_html {
114 my $self = shift;
115 my $terms2hilite = shift;
116  
117 my $sort_order = $self->seqid_order; # returns a hash of {seqid=>index}
118  
119 my $source = $self->data_source;
120 my $panels = $self->{panels} ||= $self->generate_panels or return;
121  
122 my $html;
123  
124 my $hit_count = $self->hit_count;
125 my $message = $self->language->tr('HIT_COUNT',$hit_count);
126 $html .= CGI::h2($message);
127  
128 my @panels;
129 for my $seqid (
130 sort {$sort_order->{$a} <=> $sort_order->{$b}} keys %$panels
131 ) {
132  
133 my $panel = $self->{panels}{$seqid}{panel};
134 # workaround bug/coredump in ideogram glyph
135 next if $panel->height < SUPPRESS_SMALL_CHROMOSOMES;
136  
137 my $url = $source->generate_image($panel->gd);
138  
139 my $margin = Bio::Graphics::Panel->can('rotate')
140 ? $self->chrom_height - $panel->gd->height
141 : 5;
142  
143 my $imagemap = $self->image_map(scalar $panel->boxes,"${seqid}.");
144 push @panels,
145 div(
146 {-style=>"cursor:default;float:left;margin-top:${margin}px;margin-left:0.5em;margin-right;0.5em"},
147 div({-style=>'position:relative'},
148 img({-src=>$url,-border=>0}),
149 $imagemap
150 ),
151 div({-align=>'center'},b($seqid))
152 );
153 }
154 my $bgcolor = $self->data_source->global_setting('overview bgcolor') || 'wheat:0.5';
155 $html .= div({-style=>"background-color:$bgcolor"},@panels);
156  
157 my $table = $self->hits_table($terms2hilite);
158  
159 return $html.br({-clear=>'all'}).$table;
160 }
161  
162 # not really an imagemap, but actually a "rollover" map
163 sub image_map {
164 my $self = shift;
165 my $boxes = shift;
166  
167 my $chromosome = $self->chrom_type;
168  
169 my $divs = '';
170  
171 for (my $i=0; $i<@$boxes; $i++) {
172 next if $boxes->[$i][0]->primary_tag eq $chromosome;
173  
174 my ($left,$top,$right,$bottom) = @{$boxes->[$i]}[1,2,3,4];
175 $left -= 2;
176 $top -= 2;
177 my $width = $right-$left+3;
178 my $height = $bottom-$top+3;
179
180 my $name = $boxes->[$i][0]->display_name || "feature id #".$boxes->[$i][0]->primary_id;
181 my $id = $self->feature2id($boxes->[$i][0]);
182 my $link = $self->feature2link($boxes->[$i][0]);
183 $divs .= div({-class => 'nohilite',
184 -id => "box_${id}",
185 -style => "z-index:10; top:${top}px; left:${left}px; width:${width}px; height:${height}px",
186 -title => $name,
187 -onMouseOver=>"k_hilite_feature(this,true)",
188 -onMouseOut =>"k_unhilite_feature(this)",
189 -onMouseDown=>"location.href='$link'",
190 },'&nbsp;'
191 )."\n";
192 }
193 return $divs;
194 }
195  
196 sub feature2link {
197 my $self = shift;
198 my $feature = shift;
199 my $url = url(-absolute=>1,-path_info=>1)."?name=";
200 my $match_id = eval {$feature->primary_id};
201 my $class = eval {$feature->class};
202 my $name = $feature->display_name || '';
203 my $fid = $match_id
204 && $match_id !~ /\w+:\w+\(/ # work around a bioperl bug
205 ? "id:$match_id"
206 : $class ? "$class:$name"
207 : $name;
208 my $dbid = $feature->gbrowse_dbid if $feature && $feature->can('gbrowse_dbid');
209 $dbid ||= '';
210 return "$url$fid;dbid=$dbid";
211 }
212  
213 sub by_chromosome_length ($$) {
214 my ($a,$b) = @_;
215 my $n1 = $a->length;
216 my $n2 = $b->length;
217 return $n1 <=> $n2;
218 }
219  
220 sub by_chromosome_name ($$){
221 my ($a,$b) = @_;
222 my $n1 = $a->seq_id;
223 my $n2 = $b->seq_id;
224  
225 if ($n1 =~ /^\d+$/ && $n2 =~ /^\d+$/) {
226 return $n1 <=> $n2;
227 }
228 elsif ($n1 =~ /^\w+\d+$/ && $n2 =~ /^\w+\d+$/) {
229 $n1 =~ s/^\w+//;
230 $n2 =~ s/^\w+//;
231 return $n1 <=> $n2;
232 } else {
233 return $n1 cmp $n2;
234 }
235 }
236  
237 sub chromosomes {
238 my $self = shift;
239 my $db = $self->db;
240 my $chrom_type = $self->chrom_type;
241 my @chroms = $db->features($chrom_type);
242
243 # if no chromosomes defined, then generate from seqids
244 unless (@chroms) {
245 my @seq_ids = keys %{$self->{hits}};
246 @chroms = map {
247 Bio::Graphics::Feature->new(
248 -name => $_->display_name,
249 -seq_id => $_->seq_id,
250 -start => 1,
251 -end => $_->length,
252 -type => 'chromosome')
253 } map {$db->segment(-name=>$_)} @seq_ids;
254 }
255 return @chroms;
256 }
257  
258 sub generate_panels {
259 my $self = shift;
260 my $format = shift || 'GD';
261  
262 my $image_class = $format;
263 $image_class .= '::Image' unless $image_class =~ /::Image/;
264 eval "require $image_class" unless $image_class->can('new');
265  
266 my $chrom_type = $self->chrom_type;
267 my $chrom_width = $self->chrom_width;
268  
269 my @features = grep {$self->hits($_->seq_id)} $self->chromosomes;
270 return unless @features;
271  
272 my $minimal_width = 0;
273 my $maximal_length = 0;
274  
275 for my $f (@features) {
276 my $name = $f->seq_id;
277 my $width = length($name) * gdSmallFont->width;
278 $minimal_width = $width if $chrom_width < $width;
279 $maximal_length = $f->length if $maximal_length < $f->length;
280 }
281 $chrom_width = $minimal_width
282 if $chrom_width eq 'auto';
283 my $pixels_per_base = $self->chrom_height / $maximal_length;
284 my $band_colors = $self->chrom_background;
285 my $fallback_color = $self->chrom_background_fallback;
286  
287 # each becomes a panel
288 my %results;
289 for my $chrom (@features) {
290 my $height = int($chrom->length * $pixels_per_base);
291 $height = 20 if $height < 20; # prevent tiny tiny chromosomes
292 my $panel = Bio::Graphics::Panel->new(-width => $height, # not an error, will rotate image later
293 -length=> $chrom->length,
294 -pad_top =>10,
295 -pad_bottom=>10,
296 -pad_right => 0,
297 -bgcolor => $self->data_source->global_setting('overview bgcolor')
298 || 'wheat:0.5',
299 -image_class => $format,
300 );
301  
302 my @hits = $self->hits($chrom->seq_id);
303 $panel->add_track(\@hits,
304 -glyph => sub {
305 my $feature = shift;
306 return $feature->length/$chrom->length > 0.05
307 ? 'generic'
308 : 'diamond';
309 },
310 -glyph => 'generic',
311 -maxdepth => 0,
312 -height => 6,
313 -bgcolor => 'red',
314 -fgcolor => 'red',
315 -bump => -1,
316 );
317  
318 my $rotate = $panel->can('rotate') && $image_class->can('copyRotate90');
319  
320 my $method = $rotate ? 'add_track' : 'unshift_track';
321  
322 $panel->$method($chrom,
323 -glyph => 'ideogram', # not an error, will rotate image later
324 -height => $chrom_width,
325 -bgcolor => $band_colors,
326 -bgfallback => $fallback_color,
327 -label => 0,
328 -description => 0);
329  
330 $panel->rotate(1) if $rotate; # need bioperl-live from 20 August 2008 for this to work
331 $results{$chrom->seq_id}{chromosome} = $chrom;
332 $results{$chrom->seq_id}{panel} = $panel;
333 }
334  
335 return \%results;
336 }
337  
338 sub feature2id {
339 my $self = shift;
340 my $feature = shift;
341 return overload::StrVal($feature);
342 }
343  
344 sub hits_table {
345 my $self = shift;
346 my $term2hilite = shift;
347  
348 local $^W = 0; # quash uninit variable warnings
349  
350 my @hits = $self->hits;
351  
352 my $regexp = join '|',($term2hilite =~ /(\w+)/g)
353 if defined $term2hilite;
354  
355 my $na = $self->trans('NOT_APPLICABLE') || '-';
356  
357 my $sort_order = $self->seqid_order;
358 my $url = url(-absolute=>1,-path_info=>1)."?name=";
359  
360 # a way long map call here
361 my @rows = map {
362 my $name = $_->display_name || '';
363 my $link = $self->feature2link($_);
364 my $id = $self->feature2id($_); # as an internal <div> id for hilighting
365 my $pos = $_->seq_id.':'.$_->start.'..'.$_->end;
366 my $desc = escapeHTML(Bio::Graphics::Glyph::generic->get_description($_));
367 $desc =~ s/($regexp)/<b class="keyword">$1<\/b>/ig if $regexp;
368 $desc =~ s/(\S{60})/$1 /g; # wrap way long lines
369
370 TR({-class=>'nohilite',
371 -id=>"feature_${id}",
372 -onMouseOver=>"k_hilite_feature(this)",
373 -onMouseOut =>"k_unhilite_feature(this)",
374 },
375 th({-align=>'left'},a({-href=>$link},$name)),
376 td(eval{$_->method} || 'region'),
377 td($desc),
378 td(a({-href=>"$url$pos"},$pos)),
379 td($_->score || $na)
380 )
381 } sort {
382 ($b->score||0) <=> ($a->score||0)
383 || $sort_order->{$a->seq_id} <=> $sort_order->{$b->seq_id}
384 || $a->start <=> $b->start
385 || $a->end <=> $b->end
386 } @hits;
387  
388 my $count = $self->language ? b($self->trans('HIT_COUNT',scalar @hits)) : '';
389  
390 return
391 b($count),
392 div({-id=>'scrolling_table',-style=>'cursor:default'},
393 table({-class=>'searchbody',-style=>'width:95%'}, #firefox display problems
394 TR(
395 th({-align=>'left'},
396 [$self->trans('NAME'),
397 $self->trans('Type'),
398 $self->trans('Description'),
399 $self->trans('Position'),
400 $self->trans('score')
401 ])
402 ),
403 @rows)
404 );
405 }
406  
407  
408 1;