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