| 1 | package Bio::Graphics::Glyph::ruler_arrow; | ||
| 2 | # package to use for drawing an arrow as ruler (5' and 3' are marked as label) | ||
| 3 | |||
| 4 | # Non object-oriented utilities used here-and-there in Bio::Graphics modules | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Bio::Graphics::Glyph::ruler_arrow - glyph for drawing an arrow as ruler (5' and 3' are marked as label) | ||
| 9 | |||
| 10 | =cut | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use base qw(Bio::Graphics::Glyph::generic); | ||
| 14 | |||
| 15 | my %UNITS = (K => 1000, | ||
| 16 | M => 1_000_000, | ||
| 17 | G => 1_000_000_000); | ||
| 18 | |||
| 19 | sub pad_bottom { | ||
| 20 | my $self = shift; | ||
| 21 | my $val = $self->SUPER::pad_bottom(@_); | ||
| 22 | $val += $self->font_height if $self->option('tick'); | ||
| 23 | $val; | ||
| 24 | } | ||
| 25 | |||
| 26 | # override draw method | ||
| 27 | sub draw { | ||
| 28 | my $self = shift; | ||
| 29 | my $parallel = $self->option('parallel'); | ||
| 30 | $parallel = 1 unless defined $parallel; | ||
| 31 | $self->draw_parallel(@_) if $parallel; | ||
| 32 | $self->draw_perpendicular(@_) unless $parallel; | ||
| 33 | $self->draw_label(@_) if ($self->option('label')); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub draw_perpendicular { | ||
| 37 | my $self = shift; | ||
| 38 | my $gd = shift; | ||
| 39 | my ($dx,$dy) = @_; | ||
| 40 | my ($x1,$y1,$x2,$y2) = $self->bounds(@_); | ||
| 41 | |||
| 42 | my $ne = $self->option('northeast'); | ||
| 43 | my $sw = $self->option('southwest'); | ||
| 44 | $ne = $sw = 1 unless defined($ne) || defined($sw); | ||
| 45 | |||
| 46 | # draw a perpendicular arrow at position indicated by $x1 | ||
| 47 | my $fg = $self->set_pen; | ||
| 48 | my $a2 = ($y2-$y1)/4; | ||
| 49 | |||
| 50 | my @positions = $x1 == $x2 ? ($x1) : ($x1,$x2); | ||
| 51 | for my $x (@positions) { | ||
| 52 | if ($ne) { | ||
| 53 | $gd->line($x,$y1,$x,$y2,$fg); | ||
| 54 | $gd->line($x-$a2,$y1+$a2,$x,$y1,$fg); | ||
| 55 | $gd->line($x+$a2,$y1+$a2,$x,$y1,$fg); | ||
| 56 | } | ||
| 57 | if ($sw) { | ||
| 58 | $gd->line($x,$y1,$x,$y2,$fg); | ||
| 59 | $gd->line($x-$a2,$y2-$a2,$x,$y2,$fg); | ||
| 60 | $gd->line($x+$a2,$y2-$a2,$x,$y2,$fg); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | # add a label if requested | ||
| 65 | # $self->draw_label($gd,$dx,$dy) if ($self->option('label') && !$self->option('ruler')); | ||
| 66 | # this draws the label aligned to the left | ||
| 67 | } | ||
| 68 | |||
| 69 | sub draw_parallel { | ||
| 70 | my $self = shift; | ||
| 71 | my $gd = shift; | ||
| 72 | my ($dx,$dy) = @_; | ||
| 73 | my ($x1,$y1,$x2,$y2) = $self->bounds(@_); | ||
| 74 | |||
| 75 | my $fg = $self->set_pen; | ||
| 76 | my $a2 = ($self->height)/2; | ||
| 77 | my $center = $y1+$a2; | ||
| 78 | |||
| 79 | $x1 = $self->panel->left if $x1 < $self->panel->left; | ||
| 80 | $x2 = $self->panel->right if $x2 > $self->panel->right; | ||
| 81 | |||
| 82 | my ($sw,$ne,$base_w,$base_e) = $self->arrowheads; | ||
| 83 | $gd->line($x1,$center,$x2,$center,$fg); | ||
| 84 | $self->arrowhead($gd,$x1,$center,$a2,-1) if $sw; # west arrow | ||
| 85 | $self->arrowhead($gd,$x2,$center,$a2,+1) if $ne; # east arrow | ||
| 86 | $gd->line($x2,$center-$a2,$x2,$center+$a2,$fg) if $base_e; #east base | ||
| 87 | $gd->line($x1,$center-$a2,$x1,$center+$a2,$fg) if $base_w; #west base | ||
| 88 | |||
| 89 | # turn on ticks | ||
| 90 | if ($self->option('tick')) { | ||
| 91 | local $^W = 0; # dumb uninitialized variable warning | ||
| 92 | my $font = $self->font; | ||
| 93 | my $font_color = $self->fontcolor; | ||
| 94 | my $height = $self->height; | ||
| 95 | |||
| 96 | my $relative = $self->option('relative_coords'); | ||
| 97 | my $start = $relative ? 1 : $self->feature->start; | ||
| 98 | my $stop = $start + $self->feature->length - 1; | ||
| 99 | |||
| 100 | my $offset = $relative ? $self->feature->start-1 : 0; | ||
| 101 | my $reversed = $self->feature->strand < 0; | ||
| 102 | |||
| 103 | my $units = $self->option('units') || ''; | ||
| 104 | my $divisor = $UNITS{$units} || 1 if $units; | ||
| 105 | |||
| 106 | my ($major_ticks,$minor_ticks) = $self->panel->ticks($start,$stop,$font,$divisor); | ||
| 107 | |||
| 108 | ## Does the user want to override the internal scale? | ||
| 109 | my $scale = $self->option('scale'); | ||
| 110 | |||
| 111 | my $left = $sw ? $x1+$height : $x1; | ||
| 112 | my $right = $ne ? $x2-$height : $x2; | ||
| 113 | |||
| 114 | my $format = ($major_ticks->[1]-$major_ticks->[0])/($divisor||1) < 1 ? "%.1f$units" : "%d$units"; | ||
| 115 | |||
| 116 | for my $i (@$major_ticks) { | ||
| 117 | my $tickpos = $dx + ($reversed ? $self->map_pt($stop - $i + $offset) | ||
| 118 | : $self->map_pt($i + $offset)); | ||
| 119 | next if $tickpos < $left or $tickpos > $right; | ||
| 120 | $gd->line($tickpos,$center-$a2,$tickpos,$center+$a2,$fg); | ||
| 121 | my $label = $scale ? $i / $scale : $i; | ||
| 122 | if ($units) { | ||
| 123 | my $scaled = $label/$divisor; | ||
| 124 | $label = sprintf($format,$scaled); | ||
| 125 | } | ||
| 126 | my $middle = $tickpos - $self->string_width($label)/2; | ||
| 127 | $gd->string($font,$middle,$center+$a2-1,$label,$font_color) | ||
| 128 | unless ($self->option('no_tick_label')); | ||
| 129 | } | ||
| 130 | |||
| 131 | if ($self->option('tick') >= 2) { | ||
| 132 | my $a4 = $self->height/4; | ||
| 133 | for my $i (@$minor_ticks) { | ||
| 134 | my $tickpos = $dx + ($reversed ? $self->map_pt($stop - $i + $offset) | ||
| 135 | : $self->map_pt($i + $offset)); | ||
| 136 | next if $tickpos < $left or $tickpos > $right; | ||
| 137 | $gd->line($tickpos,$center-$a4,$tickpos,$center+$a4,$fg); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | # add a label if requested | ||
| 143 | # $self->draw_label($gd,$dx,$dy) if ($self->option('label'); | ||
| 144 | # $self->draw_description($gd,$dx,$dy) if $self->option('description'); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub arrowheads { | ||
| 148 | my $self = shift; | ||
| 149 | my ($ne,$sw,$base_e,$base_w); | ||
| 150 | if ($self->option('double')) { | ||
| 151 | $ne = $sw = 1; | ||
| 152 | } else { | ||
| 153 | $ne = $self->option('northeast') || $self->option('east'); | ||
| 154 | $sw = $self->option('southwest') || $self->option('west'); | ||
| 155 | } | ||
| 156 | # otherwise use strandedness to define the arrow | ||
| 157 | unless (defined($ne) || defined($sw)) { | ||
| 158 | # turn on both if neither specified | ||
| 159 | $ne = 1 if $self->feature->strand > 0; | ||
| 160 | $sw = 1 if $self->feature->strand < 0; | ||
| 161 | } | ||
| 162 | return ($sw,$ne,0,0) unless $self->option('base'); | ||
| 163 | return ($sw,$ne,!$sw,!$ne); | ||
| 164 | } | ||
| 165 | |||
| 166 | sub draw_label { | ||
| 167 | my $self = shift; | ||
| 168 | my ($gd,$left,$top) = @_; | ||
| 169 | |||
| 170 | my $label5 = "5'"; | ||
| 171 | my $label3 = "3'"; | ||
| 172 | my $relative = $self->option('relative_coords'); | ||
| 173 | my $start = $relative ? 1 : $self->feature->start; | ||
| 174 | my $stop = $start + $self->feature->length - 1; | ||
| 175 | |||
| 176 | my $offset = $relative ? $self->feature->start-1 : 0; | ||
| 177 | my $reversed = $self->feature->strand < 0; | ||
| 178 | |||
| 179 | my $units = $self->option('units') || ''; | ||
| 180 | my $divisor = $UNITS{$units} || 1 if $units; | ||
| 181 | |||
| 182 | my ($major_ticks,$minor_ticks) = $self->panel->ticks($start,$stop,$self->font,$divisor); | ||
| 183 | my $tick_scale = " ($major_ticks bp/"; | ||
| 184 | $tick_scale .= ($self->option('tick') >= 2)?"major tick)":"tick)"; | ||
| 185 | |||
| 186 | my $top_left_label = $label5; | ||
| 187 | $top_left_label .= $tick_scale if ($self->option('no_tick_label') && $self->option('tick')); | ||
| 188 | #-1 direction mean lower end is 3' (minus strand on top) | ||
| 189 | ($label5, $label3) = ($label3, $label5) if ($self->option('direction') == -1); | ||
| 190 | my $x = $self->left + $left; | ||
| 191 | $x = $self->panel->left + 1 if $x <= $self->panel->left; | ||
| 192 | my $font = $self->option('labelfont') || $self->font; | ||
| 193 | $gd->string($font, | ||
| 194 | $x, | ||
| 195 | $self->top + $top, | ||
| 196 | $top_left_label, | ||
| 197 | $self->fontcolor); | ||
| 198 | my $x1 = $left + $self->panel->right - $self->string_width($label3); | ||
| 199 | $gd->string($font, | ||
| 200 | $x1, | ||
| 201 | $self->top + $top, | ||
| 202 | $label3, | ||
| 203 | $self->fontcolor); | ||
| 204 | if ($self->option('both')) {#minus strand as well | ||
| 205 | $gd->string($font, | ||
| 206 | $x, | ||
| 207 | $self->bottom - $self->pad_bottom + $top, | ||
| 208 | $label3, | ||
| 209 | $self->fontcolor); | ||
| 210 | my $x1 = $left + $self->panel->right - $self->string_width($label5); | ||
| 211 | $gd->string($font, | ||
| 212 | $x1, | ||
| 213 | $self->bottom - $self->pad_bottom + $top, | ||
| 214 | $label5, | ||
| 215 | $self->fontcolor); | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | |||
| 220 | 1; | ||
| 221 | |||
| 222 | |||
| 223 | __END__ |