| 1 | package Bio::Graphics::Glyph::ideogram; | ||
| 2 | |||
| 3 | # Glyph to draw chromosome ideograms | ||
| 4 | |||
| 5 | use strict qw/vars refs/; | ||
| 6 | use vars '@ISA'; | ||
| 7 | use GD; | ||
| 8 | |||
| 9 | use base qw(Bio::Graphics::Glyph::generic Bio::Graphics::Glyph::heat_map); | ||
| 10 | |||
| 11 | sub my_description { | ||
| 12 | return <<END; | ||
| 13 | This glyph draws a section of a chromosome ideogram. It relies | ||
| 14 | on certain data from the feature to determine which color should | ||
| 15 | be used (stain) and whether the segment is a telomere or | ||
| 16 | centromere or a regular cytoband. See the full manual page for this | ||
| 17 | glyph for instructions on formatting the ideogram. | ||
| 18 | END | ||
| 19 | } | ||
| 20 | sub my_options { | ||
| 21 | { | ||
| 22 | bgcolor => [ | ||
| 23 | 'string', | ||
| 24 | |||
| 25 | ' gneg:white gpos25:silver gpos50:gray gpos:gray gpos75:darkgray gpos100:black acen:cen gvar:var', | ||
| 26 | |||
| 27 | 'This option is redefined to map each chromosome band\'s "stain" attribute', | ||
| 28 | 'into a color or pattern. The default value is saying to use ', | ||
| 29 | '"white" for features whose stain attribute is', | ||
| 30 | '"gneg", "silver" for those whose stain attribute is "gpos25", and so', | ||
| 31 | 'on. Several special values are recognized: "B<stalk>" draws a narrower', | ||
| 32 | 'gray region and is usually used to indicate an acrocentric', | ||
| 33 | 'stalk. "B<var>" creates a diagonal black-on-white pattern if B<-pattern> is enabled.', | ||
| 34 | '"B<cen>" draws a centromere.', | ||
| 35 | 'If -bgcolor is just a color name, like "yellow", the glyph will ignore', | ||
| 36 | 'all bands and just draw a filled in chromosome.'], | ||
| 37 | bgfallback => [ | ||
| 38 | 'color', | ||
| 39 | 'yellow', | ||
| 40 | 'Color to use when no bands are present.'], | ||
| 41 | pattern => [ | ||
| 42 | 'boolean', | ||
| 43 | undef, | ||
| 44 | 'Enable drawing a vertical line pattern for centromeres and "var" regions.', | ||
| 45 | 'This is off by default due to an intermittent gd2 library crash on certain 64-bit platforms.'], | ||
| 46 | } | ||
| 47 | } | ||
| 48 | sub demo_feature { | ||
| 49 | my $self = shift; | ||
| 50 | my $data = <<END; | ||
| 51 | ##gff-version 3 | ||
| 52 | 22 ensembl chromosome 1 500 . . . ID=22;Name=Chr22 | ||
| 53 | 22 ensembl chromosome_band 1 30 . . . Parent=22;Name=p13;Alias=22p13;Stain=gvar | ||
| 54 | 22 ensembl chromosome_band 31 66 . . . Parent=22;Name=p12;Alias=22p12;Stain=stalk | ||
| 55 | 22 ensembl chromosome_band 67 97 . . . Parent=22;Name=p11.2;Alias=22p11.2;Stain=gvar | ||
| 56 | 22 ensembl centromere 98 164 . . . Parent=22;Name=22_cent;Alias=2222_cent | ||
| 57 | 22 ensembl chromosome_band 165 206 . . . Parent=22;Name=q11.21;Alias=22q11.21;Stain=gneg | ||
| 58 | 22 ensembl chromosome_band 207 220 . . . Parent=22;Name=q11.22;Alias=22q11.22;Stain=gpos25 | ||
| 59 | 22 ensembl chromosome_band 221 245 . . . Parent=22;Name=q11.23;Alias=22q11.23;Stain=gneg | ||
| 60 | 22 ensembl chromosome_band 246 281 . . . Parent=22;Name=q12.1;Alias=22q12.1;Stain=gpos50 | ||
| 61 | 22 ensembl chromosome_band 282 307 . . . Parent=22;Name=q12.2;Alias=22q12.2;Stain=gneg | ||
| 62 | 22 ensembl chromosome_band 308 361 . . . Parent=22;Name=q12.3;Alias=22q12.3;Stain=gpos50 | ||
| 63 | 22 ensembl chromosome_band 362 396 . . . Parent=22;Name=q13.1;Alias=22q13.1;Stain=gneg | ||
| 64 | 22 ensembl chromosome_band 397 430 . . . Parent=22;Name=q13.2;Alias=22q13.2;Stain=gpos50 | ||
| 65 | 22 ensembl chromosome_band 431 472 . . . Parent=22;Name=q13.31;Alias=22q13.31;Stain=gneg | ||
| 66 | 22 ensembl chromosome_band 473 482 . . . Parent=22;Name=q13.32;Alias=22q13.32;Stain=gpos50 | ||
| 67 | 22 ensembl chromosome_band 483 500 . . . Parent=22;Name=q13.33;Alias=22q13.33;Stain=gneg | ||
| 68 | END | ||
| 69 | ; | ||
| 70 | eval "require Bio::Graphics::FeatureFile" | ||
| 71 | unless Bio::Graphics::FeatureFile->can('new'); | ||
| 72 | my $db = Bio::Graphics::FeatureFile->new(-text=>$data) or die; | ||
| 73 | return $db->get_features_by_name('Chr22'); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub bgfallback { | ||
| 77 | my $self = shift; | ||
| 78 | return $self->option('bgfallback') || 'yellow'; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub bgcolor { | ||
| 82 | my $self = shift; | ||
| 83 | my $bgcolor = $self->option('bgcolor'); | ||
| 84 | return $bgcolor if defined $bgcolor; | ||
| 85 | return 'gneg:white gpos25:silver gpos50:gray gpos:gray gpos75:darkgray gpos100:black acen:cen gvar:var'; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub can_pattern { | ||
| 89 | my $self = shift; | ||
| 90 | return unless $self->option('pattern'); | ||
| 91 | return $self->panel->image_class !~ /svg/i; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub draw { | ||
| 95 | my $self = shift; | ||
| 96 | my ($gd,$left,$top,$partno,$total_parts) = @_; | ||
| 97 | |||
| 98 | my $fstart = $self->feature->start; | ||
| 99 | my $fstop = $self->feature->end; | ||
| 100 | |||
| 101 | my @parts = $self->parts; | ||
| 102 | |||
| 103 | # Draw the sides for the whole chromosome (in case | ||
| 104 | # there are missing data). | ||
| 105 | $self->draw_component(@_) if $self->level == 0; | ||
| 106 | |||
| 107 | if (@parts) { | ||
| 108 | $left += $self->left + $self->pad_left; | ||
| 109 | $top += $self->top + $self->pad_top; | ||
| 110 | } else { | ||
| 111 | @parts = ($self); | ||
| 112 | } | ||
| 113 | |||
| 114 | # Make unaggregated bands invisible if requested. | ||
| 115 | # This is for making image maps for individual | ||
| 116 | # bands of whole aggregate chromosomes. | ||
| 117 | $self->{invisible} ||= $self->option('invisible') | ||
| 118 | unless @parts > 1; | ||
| 119 | |||
| 120 | $parts[0]->{single}++ if @parts == 1; | ||
| 121 | |||
| 122 | # if the bands are subfeatures of an aggregate chromosome, | ||
| 123 | # we can draw the centomere and telomeres last to improve | ||
| 124 | # the appearance | ||
| 125 | |||
| 126 | my @last; | ||
| 127 | for my $part (@parts) { | ||
| 128 | push @last, $part and next if | ||
| 129 | $part->feature->primary_tag =~ /centromere/i || | ||
| 130 | $part->feature->start <= $fstart || | ||
| 131 | $part->feature->end >= $fstop; | ||
| 132 | my $tile = $part->create_tile('left'); | ||
| 133 | $part->draw_component($gd,$left,$top); | ||
| 134 | } | ||
| 135 | |||
| 136 | for my $part (@last) { | ||
| 137 | my $tile; | ||
| 138 | if ($part->feature->method =~ /centromere/) { | ||
| 139 | $tile = $self->create_tile('right'); | ||
| 140 | } | ||
| 141 | |||
| 142 | else { | ||
| 143 | $tile = $part->create_tile('left'); | ||
| 144 | } | ||
| 145 | my $status = $part->{single} ? 'single' | ||
| 146 | : $part->feature->method =~ /centromere/ ? 'centromere' | ||
| 147 | : $part->feature->start <= $fstart ? 'left telomere' | ||
| 148 | : $part->feature->end >= $fstop ? 'right telomere' | ||
| 149 | : undef; | ||
| 150 | $part->draw_component($gd,$left,$top,$status); | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | $self->draw_label(@_) if $self->option('label'); | ||
| 155 | $self->draw_description(@_) if $self->option('description'); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub draw_component { | ||
| 159 | my $self = shift; | ||
| 160 | my $gd = shift; | ||
| 161 | my ($x,$y,$status) = @_; | ||
| 162 | |||
| 163 | my $feat = $self->feature; | ||
| 164 | |||
| 165 | my $arcradius = $self->option('arcradius') || 7; | ||
| 166 | my ($x1, $y1, $x2, $y2 ) = $self->bounds(@_); | ||
| 167 | |||
| 168 | return if $x2 <= $self->panel->left; | ||
| 169 | return if $x1 >= $self->panel->right; | ||
| 170 | |||
| 171 | $x2 = $self->panel->right if $x2 > $self->panel->right; | ||
| 172 | |||
| 173 | # force odd width so telomere arcs are centered | ||
| 174 | $y2 ++ if ($y2 - $y1) % 2; | ||
| 175 | |||
| 176 | my ($stain) = $feat->get_tag_values('stain'); | ||
| 177 | ($stain) = $feat->get_tag_values('Stain') unless $stain; | ||
| 178 | |||
| 179 | # Some genome sequences don't contain substantial telomere sequence (i.e. Arabidopsis) | ||
| 180 | # We can suggest their presence at the tips of the chromosomes by setting fake_telomeres = 1 | ||
| 181 | # in the configuration file, resulting in the tips of the chromosome being painted black. | ||
| 182 | my $fake_telomeres = $self->option('fake_telomeres') || 0; | ||
| 183 | |||
| 184 | my $bgcolor_index = $self->bgcolor; | ||
| 185 | |||
| 186 | if ((my $fallback = $self->bgfallback) && !$stain) { | ||
| 187 | $bgcolor_index = $fallback; | ||
| 188 | } | ||
| 189 | elsif ($bgcolor_index =~ /\w+:/) { | ||
| 190 | ($bgcolor_index) = $self->bgcolor =~ /$stain:(\S+)/ if $stain; | ||
| 191 | ($bgcolor_index,$stain) = qw/white none/ if !$stain; | ||
| 192 | } | ||
| 193 | |||
| 194 | my $black = $gd->colorAllocate( 0, 0, 0 ); | ||
| 195 | my $cm_color = $self->{cm_color} ||= $self->cm_color; | ||
| 196 | my $var_color = $self->{var_color} ||= $self->var_color; | ||
| 197 | |||
| 198 | my $bgcolor = $self->factory->translate_color($bgcolor_index); | ||
| 199 | my $fgcolor = $self->fgcolor; | ||
| 200 | |||
| 201 | # special color for gvar bands | ||
| 202 | if ( $bgcolor_index =~ /var/) { | ||
| 203 | $bgcolor = $self->can_pattern ? gdTiled : $var_color; | ||
| 204 | } | ||
| 205 | |||
| 206 | if ( $feat->method !~ /centromere/i && $stain ne 'acen') { | ||
| 207 | |||
| 208 | # are we at the end of the chromosome? | ||
| 209 | if (($status eq 'single' || $status eq 'left telomere') && $stain ne 'tip') { | ||
| 210 | |||
| 211 | # left telomere | ||
| 212 | my $state = $status eq 'single' ? -1 | ||
| 213 | : $self->panel->flip ? 0 : 1; | ||
| 214 | |||
| 215 | $bgcolor = $black if $fake_telomeres; | ||
| 216 | $self->draw_telomere( $gd, $x1, $y1, $x2, $y2, | ||
| 217 | $bgcolor, $fgcolor, | ||
| 218 | $arcradius, $state ); | ||
| 219 | } | ||
| 220 | elsif ($status eq 'right telomere' && $stain ne 'tip') { | ||
| 221 | # right telomere | ||
| 222 | my $state = $self->panel->flip ? 1 : 0; | ||
| 223 | $bgcolor = $black if $fake_telomeres; | ||
| 224 | $self->draw_telomere( $gd, $x1, $y1, $x2, $y2, $bgcolor, $fgcolor, | ||
| 225 | $arcradius, $state ); | ||
| 226 | } | ||
| 227 | |||
| 228 | # or a stalk? | ||
| 229 | elsif ( $stain eq 'stalk') { | ||
| 230 | $self->draw_stalk( $gd, $x1, $y1, $x2, $y2, $bgcolor, $fgcolor ); | ||
| 231 | } | ||
| 232 | |||
| 233 | # or a regular band? | ||
| 234 | else { | ||
| 235 | $self->draw_cytoband( $gd, $x1, $y1, $x2, $y2, $bgcolor, $fgcolor ); | ||
| 236 | $self->draw_outline( $gd,$x1,$y1,$x2,$y2,$bgcolor,$fgcolor) if $bgcolor_index =~ /var/i; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | # or a centromere? | ||
| 241 | else { | ||
| 242 | if ( $self->can_pattern ) { | ||
| 243 | my $tile = $self->create_tile('right'); | ||
| 244 | $self->draw_centromere( $gd, $x1, $y1, $x2, $y2, gdTiled, $fgcolor ); | ||
| 245 | } | ||
| 246 | else { | ||
| 247 | $self->draw_centromere( $gd, $x1, $y1, $x2, $y2, $cm_color, $fgcolor ); | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | } | ||
| 252 | |||
| 253 | sub draw_cytoband { | ||
| 254 | my $self = shift; | ||
| 255 | my ( $gd, $x1, $y1, $x2, $y2, $bgcolor, $fgcolor) = @_; | ||
| 256 | |||
| 257 | # draw the filled box | ||
| 258 | $self->filled_box($gd,$x1,$y1,$x2,$y2,$bgcolor,$bgcolor); | ||
| 259 | # outer border | ||
| 260 | $gd->line($x1,$y1,$x2,$y1,$fgcolor); | ||
| 261 | $gd->line($x1,$y2,$x2,$y2,$fgcolor); | ||
| 262 | } | ||
| 263 | |||
| 264 | sub draw_outline { | ||
| 265 | my $self = shift; | ||
| 266 | my ( $gd, $x1, $y1, $x2, $y2, $bgcolor, $fgcolor) = @_; | ||
| 267 | |||
| 268 | # side borders | ||
| 269 | $gd->line($x1,$y1,$x1,$y2,$fgcolor); | ||
| 270 | $gd->line($x2,$y1,$x2,$y2,$fgcolor); | ||
| 271 | } | ||
| 272 | |||
| 273 | sub draw_centromere { | ||
| 274 | my $self = shift; | ||
| 275 | my ( $gd, $x1, $y1, $x2, $y2, $bgcolor, $fgcolor ) = @_; | ||
| 276 | |||
| 277 | # blank slate | ||
| 278 | $self->wipe(@_); | ||
| 279 | |||
| 280 | # draw a sort of hour-glass shape to represent the centromere | ||
| 281 | my $poly = GD::Polygon->new; | ||
| 282 | $poly->addPt( $x1, $y1 ); | ||
| 283 | $poly->addPt( $x1, $y2 ); | ||
| 284 | $poly->addPt( $x2, $y1 ); | ||
| 285 | $poly->addPt( $x2, $y2 ); | ||
| 286 | |||
| 287 | $gd->filledPolygon( $poly, $bgcolor ); # filled | ||
| 288 | $gd->line( $x2 - 1, $y1 + 1, $x2 - 1, $y2 - 1, $fgcolor ); | ||
| 289 | $gd->polygon( $poly, $fgcolor ); # outline | ||
| 290 | } | ||
| 291 | |||
| 292 | sub draw_telomere { | ||
| 293 | my $self = shift; | ||
| 294 | my ($gd, $x1, $y1, $x2, $y2, | ||
| 295 | $bgcolor, $fgcolor, $arcradius, $state ) = @_; | ||
| 296 | |||
| 297 | # blank slate | ||
| 298 | $self->wipe(@_); | ||
| 299 | |||
| 300 | # For single, unaggregated bands, make the terminal band | ||
| 301 | # a bit wider to accomodate the arc | ||
| 302 | if ($self->{single}) { | ||
| 303 | $x1 -= 5 if $state == 1; | ||
| 304 | $x2 += 5 if $state == 0; | ||
| 305 | } | ||
| 306 | |||
| 307 | # state should be one of: | ||
| 308 | # 0 right telomere | ||
| 309 | # 1 left telomere | ||
| 310 | # -1 round at both ends (whole chromosome) | ||
| 311 | my $outline++ if $state == -1; | ||
| 312 | |||
| 313 | my $arcsize = $y2 - $y1; | ||
| 314 | my $bwidth = $x2 - $x1; | ||
| 315 | my $new_x1 = $x1 + $arcradius - 1; | ||
| 316 | my $new_x2 = $x2 - $arcradius; | ||
| 317 | my $new_y = $y1 + int($arcsize/2 + 0.5); | ||
| 318 | |||
| 319 | my $orange = $self->panel->translate_color('lemonchiffon'); | ||
| 320 | my $bg = $self->panel->bgcolor; | ||
| 321 | |||
| 322 | $self->draw_cytoband( $gd, $x1, $y1, $x2, $y2, $bgcolor, $fgcolor ); | ||
| 323 | $self->draw_outline( $gd, $x1, $y1, $x2, $y2, $bgcolor, $fgcolor ); | ||
| 324 | if ( $state ) { # left telomere | ||
| 325 | my $x = $new_x1; | ||
| 326 | my $y = $new_y; | ||
| 327 | |||
| 328 | # erase extra stuff | ||
| 329 | $gd->line($x1,$y1,$x1+5,$y1,$bg); | ||
| 330 | $gd->line($x1,$y1,$x1,$y2,$bg); | ||
| 331 | $gd->line($x1,$y2,$x1+5,$y2,$bg); | ||
| 332 | |||
| 333 | $gd->arc( $x, $y, $arcradius * 2, | ||
| 334 | $arcsize, 90, 270, $fgcolor); | ||
| 335 | |||
| 336 | # erase off-target colors | ||
| 337 | $gd->fill($x1+1,$y1+1,$bg); | ||
| 338 | $gd->fill($x1+1,$y2-1,$bg); | ||
| 339 | } | ||
| 340 | |||
| 341 | if ( $state < 1 ) { # right telomere | ||
| 342 | my $x = $new_x2; | ||
| 343 | my $y = $new_y; | ||
| 344 | |||
| 345 | # erase extra stuff | ||
| 346 | $gd->line($x2-5,$y1,$x2,$y1,$bg); | ||
| 347 | $gd->line($x2,$y1,$x2,$y2,$bg); | ||
| 348 | $gd->line($x2-5,$y2,$x2,$y2,$bg); | ||
| 349 | |||
| 350 | $gd->arc( $x, $y, $arcradius * 2, | ||
| 351 | $arcsize, 270, 90, $fgcolor); | ||
| 352 | |||
| 353 | # erase off-target colors | ||
| 354 | $gd->fill($x2-1,$y1+1,$bg); | ||
| 355 | $gd->fill($x2-1,$y2-1,$bg); | ||
| 356 | } | ||
| 357 | |||
| 358 | unless ( $self->can_pattern ) { | ||
| 359 | $self->draw_cytoband( $gd, $new_x1 - 1, $y1 + 2, | ||
| 360 | $new_x1 + 1, $y2 - 2, | ||
| 361 | $bgcolor, $bgcolor ); | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 | # for acrocentric stalk structure, draw a narrower cytoband | ||
| 366 | sub draw_stalk { | ||
| 367 | my $self = shift; | ||
| 368 | my ( $gd, $x1, $y1, $x2, $y2, $bgcolor, $fgcolor, $inset ) = @_; | ||
| 369 | |||
| 370 | # blank slate | ||
| 371 | $self->wipe(@_); | ||
| 372 | |||
| 373 | my $height = $self->height; | ||
| 374 | $inset ||= $height > 10 ? int( $height / 10 + 0.5 ) : 2; | ||
| 375 | $_[2] += $inset; | ||
| 376 | $_[4] -= $inset; | ||
| 377 | $self->draw_cytoband(@_); | ||
| 378 | |||
| 379 | $gd->line( $x1, $y1, $x1, $y2, $fgcolor ); | ||
| 380 | $gd->line( $x2, $y1, $x2, $y2, $fgcolor ); | ||
| 381 | } | ||
| 382 | |||
| 383 | sub create_tile { | ||
| 384 | my $self = shift; | ||
| 385 | my $direction = shift; | ||
| 386 | |||
| 387 | my $gd = $self->panel->gd; | ||
| 388 | return unless $gd->can('setTile'); | ||
| 389 | |||
| 390 | # Prepare tile to use for filling an area | ||
| 391 | my $tile; | ||
| 392 | if ( $direction eq 'right' ) { | ||
| 393 | $tile = GD::Image->new(3,3); | ||
| 394 | my $black = $tile->colorAllocate(0,0,0); | ||
| 395 | my $white = $tile->colorAllocate(255,255,255); | ||
| 396 | $tile->filledRectangle(0, 0, 3, 3, $white); | ||
| 397 | $tile->line( 0, 0, 3, 3, $black); | ||
| 398 | } | ||
| 399 | elsif ( $direction eq 'left' ) { | ||
| 400 | $tile = GD::Image->new(4,4); | ||
| 401 | my $black = $tile->colorAllocate(0,0,0); | ||
| 402 | my $white = $tile->colorAllocate(255,255,255); | ||
| 403 | $tile->filledRectangle(0,0,4,4, $white); | ||
| 404 | $tile->line( 4, 0, 0, 4, $black); | ||
| 405 | } | ||
| 406 | |||
| 407 | $gd->setTile($tile); | ||
| 408 | return $tile; | ||
| 409 | } | ||
| 410 | |||
| 411 | # This overrides the Glyph::parts method until I | ||
| 412 | # can figure out how the bands get mangled there | ||
| 413 | sub parts { | ||
| 414 | my $self = shift; | ||
| 415 | my $f = $self->feature; | ||
| 416 | my $level = $self->level + 1; | ||
| 417 | my @subf = sort {$a->start <=> $b->start} $f->get_SeqFeatures; | ||
| 418 | return $self->factory->make_glyph($level,@subf); | ||
| 419 | } | ||
| 420 | |||
| 421 | # erase anthing that might collide. This is for | ||
| 422 | # clean telomeres, centromeres and stalks | ||
| 423 | sub wipe { | ||
| 424 | my $self = shift; | ||
| 425 | my $whitewash = $self->panel->bgcolor; | ||
| 426 | $self->filled_box(@_[0..4],$whitewash,$whitewash); | ||
| 427 | } | ||
| 428 | |||
| 429 | # Disable bumping entirely, since it messes up the ideogram | ||
| 430 | sub bump { return 0; } | ||
| 431 | |||
| 432 | sub cm_color { | ||
| 433 | my $self = shift; | ||
| 434 | my $bgcolor = $self->bgcolor; | ||
| 435 | my ($c) = $bgcolor =~ /cen:(\S+)/; | ||
| 436 | $c ||= 'lightgrey'; | ||
| 437 | return $self->translate_color($c); | ||
| 438 | } | ||
| 439 | |||
| 440 | sub var_color { | ||
| 441 | my $self = shift; | ||
| 442 | my $bgcolor = $self->bgcolor; | ||
| 443 | my ($c) = $bgcolor =~ /var:(\S+)/; | ||
| 444 | $c ||= '#805080'; | ||
| 445 | return $self->translate_color($c); | ||
| 446 | } | ||
| 447 | |||
| 448 | 1; | ||
| 449 | |||
| 450 | __END__ |