| 1 | package Bio::Graphics::Glyph::allele_tower; | ||
| 2 | |||
| 3 | # Glyph for drawing each allele found at a SNP position in a column. | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use vars '@ISA'; | ||
| 7 | @ISA = 'Bio::Graphics::Glyph::generic'; | ||
| 8 | use Bio::Graphics::Glyph::generic; | ||
| 9 | |||
| 10 | # Give enough height to fit in the alleles | ||
| 11 | sub height { | ||
| 12 | my $self = shift; | ||
| 13 | my @alleles = eval{$self->feature->get_tag_values('Alleles')}; | ||
| 14 | @alleles = split /\//,$self->option('alleles') unless @alleles >= 2; | ||
| 15 | my $size = 2 + 10 * ($#alleles +1); | ||
| 16 | return $size; | ||
| 17 | } | ||
| 18 | |||
| 19 | # Need to make room for the allele bars if there is room | ||
| 20 | sub pad_right { | ||
| 21 | my $self = shift; | ||
| 22 | my $right = $self->SUPER::pad_right; | ||
| 23 | return $right > 55 ? $right : 55 if $self->label; | ||
| 24 | my $width = GD::Font->Small->width * 2.5; | ||
| 25 | return $width > $right ? $width : $right; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub draw_component { | ||
| 29 | my $self = shift; | ||
| 30 | my $gd = shift; | ||
| 31 | my $fg = $self->fgcolor; | ||
| 32 | |||
| 33 | # find the center and vertices | ||
| 34 | my ($x1,$y1,$x2,$y2) = $self->calculate_boundaries(@_); | ||
| 35 | |||
| 36 | my $feature = $self->feature; | ||
| 37 | my @alleles = eval{$feature->get_tag_values('Alleles')}; | ||
| 38 | @alleles = split /\//,$self->option('alleles') unless @alleles == 2; | ||
| 39 | |||
| 40 | if (@alleles) { | ||
| 41 | # If it is on the minus strand | ||
| 42 | if (my $strand = $self->option('ref_strand') <0){ | ||
| 43 | foreach (@alleles) { | ||
| 44 | tr/ACTG/TGAC/ if $self->option('complement'); | ||
| 45 | } | ||
| 46 | $fg = $self->bgcolor if $self->bgcolor; | ||
| 47 | } | ||
| 48 | |||
| 49 | for (my $i=0;$i<@alleles;$i++) { | ||
| 50 | my $position = -2+ $i * 10; # Space out each allele | ||
| 51 | |||
| 52 | # for the allele frequency horizontal bars (maf lines) | ||
| 53 | # x1, x2 are the same, y2 is bigger than y1 | ||
| 54 | my $maf = defined ($self->option('maf'))? $self->option('maf') : "NO"; | ||
| 55 | |||
| 56 | # If the MAF freq = 0, the major allele will be length 44 + 6 | ||
| 57 | my $bar_length = $maf*44 +6 unless $maf eq "NO"; | ||
| 58 | my $y_delta = ($y2- $y1)/(2 * ($#alleles +1)); # correct for height | ||
| 59 | |||
| 60 | if (my $minor_allele = $self->option('minor_allele')){ | ||
| 61 | if ($alleles[$i] eq $minor_allele) { | ||
| 62 | # Print the letter | ||
| 63 | $gd->string(GD::Font->Small,$x1-1, | ||
| 64 | $position + $y1, $alleles[$i], $fg); | ||
| 65 | } | ||
| 66 | else { | ||
| 67 | # If this is the major allele, the bar length must be 44 +6 - maf length | ||
| 68 | $bar_length = 44-($maf*44) +6 unless $maf eq "NO"; | ||
| 69 | # Print the letter | ||
| 70 | $gd->string(GD::Font->MediumBold,$x1-1, | ||
| 71 | $position + $y1, $alleles[$i], $fg); | ||
| 72 | |||
| 73 | } | ||
| 74 | # Print the line for the allele freq. bar | ||
| 75 | if ($self->label){ | ||
| 76 | $gd->line($x1+6, $y1 + (2*$i +1)*$y_delta, | ||
| 77 | $x1+$bar_length, $y1 + (2*$i +1)*$y_delta, $fg) | ||
| 78 | unless $maf eq "NO"; | ||
| 79 | } | ||
| 80 | |||
| 81 | } | ||
| 82 | # if no minor allele is defined, use the small fonts for both | ||
| 83 | else { | ||
| 84 | $gd->string(GD::Font->Small,$x1-1, $position + $y1, $alleles[$i], $fg); | ||
| 85 | } | ||
| 86 | } # end of for | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | ; | ||
| 91 | |||
| 92 | __END__ |