« blib/lib/Bio/Graphics/Glyph/whiskerplot.pm
↑ Summary
blib/lib/Bio/Graphics/Glyph/repeating_shape.pm »
| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Getopt::Long; | ||
| 5 | use File::Spec; | ||
| 6 | use IO::Dir; | ||
| 7 | use Bio::Graphics::Panel; | ||
| 8 | use Bio::Graphics::Feature; | ||
| 9 | use File::Temp 'tempfile'; | ||
| 10 | use Bio::Graphics::Wiggle; | ||
| 11 | |||
| 12 | my $MANUAL = 0; | ||
| 13 | my $POD = 0; | ||
| 14 | my $LIST = 0; | ||
| 15 | my $PICT = 0; | ||
| 16 | my $VIEW = 0; | ||
| 17 | my $BOXES = 0; | ||
| 18 | my $SVG = 0; | ||
| 19 | |||
| 20 | my $usage = <<USAGE; | ||
| 21 | Usage: $0 [options] glyph_type | ||
| 22 | |||
| 23 | Give usage information about Bio::Graphics glyphs. | ||
| 24 | |||
| 25 | Options: | ||
| 26 | -m --manual Print the full manual page for the glyph, followed | ||
| 27 | by a summary of its options. | ||
| 28 | -r --raw Print the quick summary of the glyph\'s options in raw POD | ||
| 29 | format. | ||
| 30 | -l --list List all glyphs that are available for use. | ||
| 31 | -p --picture Create a PNG picture of what the indicated glyph looks like. | ||
| 32 | The PNG will be written to stdout | ||
| 33 | -v --view Launch a viewer ("xv", "display" or "firefox") to show the | ||
| 34 | glyph. | ||
| 35 | -b --boxes Outline the boxes around each glyph | ||
| 36 | --svg When used in conjunction with --picture, will create | ||
| 37 | an SVG rather than a png using GD::SVG | ||
| 38 | |||
| 39 | If neither -m nor -l are specified, the default is to print a summary | ||
| 40 | of the glyph\'s options. | ||
| 41 | |||
| 42 | To experiment with glyph options, invoke $0 this way: | ||
| 43 | |||
| 44 | $0 -v glyph_name -- -option1 value1 -option2 value2 | ||
| 45 | |||
| 46 | example: | ||
| 47 | |||
| 48 | $0 -v christmas_arrow -- -radius 5 -fgcolor green | ||
| 49 | USAGE | ||
| 50 | |||
| 51 | GetOptions ('manual' => \$MANUAL, | ||
| 52 | 'raw' => \$POD, | ||
| 53 | 'list' => \$LIST, | ||
| 54 | 'picture' => \$PICT, | ||
| 55 | 'view' => \$VIEW, | ||
| 56 | 'boxes' => \$BOXES, | ||
| 57 | 'svg' => \$SVG, | ||
| 58 | ) or die $usage; | ||
| 59 | |||
| 60 | my $glyph = shift; | ||
| 61 | $glyph || $LIST or die $usage; | ||
| 62 | |||
| 63 | if ($LIST) { | ||
| 64 | print_list(); | ||
| 65 | exit 0; | ||
| 66 | } | ||
| 67 | |||
| 68 | my $class = "Bio::Graphics::Glyph::$glyph"; | ||
| 69 | unless (eval "require $class;1") { | ||
| 70 | my $mesg = $@; | ||
| 71 | $mesg =~ s/\(.+$//; | ||
| 72 | $mesg =~ s/at \(eval.+$//; | ||
| 73 | $mesg =~ s/\s+$//s; | ||
| 74 | warn $mesg,".\n"; | ||
| 75 | die "Please run $0 -l for a list of valid glyphs.\n"; | ||
| 76 | } | ||
| 77 | |||
| 78 | if ($PICT || $VIEW) { | ||
| 79 | print_picture($glyph,$VIEW,$SVG); | ||
| 80 | exit 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | if ($MANUAL) { | ||
| 84 | system "perldoc",$class; | ||
| 85 | } elsif ($POD) { | ||
| 86 | print $class->options_pod(); | ||
| 87 | } else { | ||
| 88 | print $class->options_man(); | ||
| 89 | } | ||
| 90 | exit 0; | ||
| 91 | |||
| 92 | sub print_list { | ||
| 93 | my %glyphs; | ||
| 94 | for my $inc (@INC) { | ||
| 95 | my $dir = File::Spec->catfile($inc,'Bio','Graphics','Glyph'); | ||
| 96 | next unless -d $dir; | ||
| 97 | my $d = IO::Dir->new($dir) or die "Couldn't open $dir for reading: $!"; | ||
| 98 | while (defined(my $entry = $d->read)) { | ||
| 99 | next unless $entry =~ /\.pm$/; | ||
| 100 | (my $base = $entry) =~ s/\.pm$//; | ||
| 101 | eval "use Bio::Graphics::Glyph::$base"; | ||
| 102 | next unless "Bio::Graphics::Glyph::$base"->isa('Bio::Graphics::Glyph'); | ||
| 103 | my $f = File::Spec->catfile($dir,$entry); | ||
| 104 | my $io = IO::File->new($f) or next; | ||
| 105 | while (<$io>) { | ||
| 106 | chomp; | ||
| 107 | next unless /^=head1 NAME/../=head1 (SYNOPSIS|DESCRIPTION)/; | ||
| 108 | my ($name,$description) = /^Bio::Graphics::Glyph::(\w+)\s+(.+)/ or next; | ||
| 109 | $description =~ s/^[\s-]+//; | ||
| 110 | next if $description =~ /base class/; | ||
| 111 | $glyphs{$name} = $description; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | } | ||
| 115 | for my $name (sort keys %glyphs) { | ||
| 116 | my $description = $glyphs{$name}; | ||
| 117 | printf "%-20s %s\n",$name,$description; | ||
| 118 | } | ||
| 119 | |||
| 120 | exit 0; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub print_picture { | ||
| 124 | my $glyph = shift; | ||
| 125 | my $viewit = shift; | ||
| 126 | my $svg = shift; | ||
| 127 | |||
| 128 | my $panel = Bio::Graphics::Panel->new(-length => 500, | ||
| 129 | -width => 250, | ||
| 130 | -pad_left => 20, | ||
| 131 | -pad_right => 20, | ||
| 132 | -pad_top => 10, | ||
| 133 | -pad_bottom => 10, | ||
| 134 | -key_style => 'between', | ||
| 135 | -truecolor => 1, | ||
| 136 | -image_class => $svg ? | ||
| 137 | 'GD::SVG' : 'GD' | ||
| 138 | ); | ||
| 139 | |||
| 140 | |||
| 141 | my @additional_args = @ARGV; | ||
| 142 | |||
| 143 | my $sort_order = sub ($$) { | ||
| 144 | my ($g1,$g2) = @_; | ||
| 145 | return $g1->feature->display_name cmp $g2->feature->display_name; | ||
| 146 | }; | ||
| 147 | |||
| 148 | my @track_args = ( | ||
| 149 | -glyph => $glyph, | ||
| 150 | -label => 1, | ||
| 151 | -description => 1, | ||
| 152 | -height => 16, | ||
| 153 | -sort_order => $sort_order, | ||
| 154 | @additional_args | ||
| 155 | ); | ||
| 156 | |||
| 157 | my $class = "Bio::Graphics::Glyph::$glyph"; | ||
| 158 | eval "require $class"; | ||
| 159 | warn $@ if $@; | ||
| 160 | |||
| 161 | my @example_features = eval{$class->demo_feature}; | ||
| 162 | warn $@ if $@; | ||
| 163 | |||
| 164 | if (@example_features) { | ||
| 165 | $panel->add_track( | ||
| 166 | \@example_features, | ||
| 167 | @track_args, | ||
| 168 | -key => 'Demo feature provided by glyph', | ||
| 169 | ); | ||
| 170 | } else { | ||
| 171 | create_tracks($panel,$glyph,\@track_args); | ||
| 172 | } | ||
| 173 | |||
| 174 | if ($BOXES) { | ||
| 175 | my $gd = $panel->gd; | ||
| 176 | my $boxes = $panel->boxes; | ||
| 177 | for my $box (@$boxes) { | ||
| 178 | my ($f,$x1,$y1,$x2,$y2) = @$box; | ||
| 179 | my $red = $panel->translate_color('red'); | ||
| 180 | $gd->rectangle($x1,$y1,$x2,$y2,$red); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | my $png = $svg ? $panel->svg : $panel->png; | ||
| 185 | unless ($viewit) { | ||
| 186 | print $png; | ||
| 187 | return; | ||
| 188 | } | ||
| 189 | |||
| 190 | # special stuff for displaying on linux systems | ||
| 191 | for my $viewer (qw(xv display)) { # can read from stdin | ||
| 192 | $ENV{SHELL} && `which $viewer` or next; | ||
| 193 | my $child = open my $fh,"|-"; | ||
| 194 | if ($child) { | ||
| 195 | print $fh $png; | ||
| 196 | close $fh; | ||
| 197 | return; | ||
| 198 | } else { | ||
| 199 | fork() && exit 0; | ||
| 200 | exec $viewer,'-'; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | # if we get here, then launch firefox | ||
| 205 | my ($fh,$filename) = tempfile(SUFFIX=>'.png', | ||
| 206 | UNLINK=>1, | ||
| 207 | ); | ||
| 208 | print $fh $png; | ||
| 209 | close $fh; | ||
| 210 | my $child = fork() && sleep 2 && exit 0; | ||
| 211 | exec 'firefox',$filename; | ||
| 212 | } | ||
| 213 | |||
| 214 | sub create_tracks { | ||
| 215 | my ($panel,$glyph,$args) = @_; | ||
| 216 | |||
| 217 | # the next bit of code is here to manufacture some wiggle data for demo purposes | ||
| 218 | my $wig = Bio::Graphics::Wiggle->new(undef, | ||
| 219 | 1, | ||
| 220 | {seqid=>'chr1', | ||
| 221 | start=>1, | ||
| 222 | end =>500} | ||
| 223 | ); | ||
| 224 | my @values = map { | ||
| 225 | (sin($_/60)+sin($_/12))*100+rand(100) | ||
| 226 | } 1..500; | ||
| 227 | if (eval "require Statistics::Descriptive; 1") { | ||
| 228 | my $stat = Statistics::Descriptive::Sparse->new; | ||
| 229 | $stat->add_data(@values); | ||
| 230 | $wig->min($stat->min); | ||
| 231 | $wig->max($stat->max); | ||
| 232 | $wig->mean($stat->mean); | ||
| 233 | $wig->stdev($stat->standard_deviation); | ||
| 234 | } else { | ||
| 235 | my $min = $values[0]; | ||
| 236 | my $max = $values[0]; | ||
| 237 | my $tot = 0; | ||
| 238 | for (@values) {$min = $_ if $min > $_; | ||
| 239 | $max = $_ if $max < $_; | ||
| 240 | $tot += $_; | ||
| 241 | } | ||
| 242 | $wig->min($min); | ||
| 243 | $wig->max($max); | ||
| 244 | $wig->stdev(120); # just make it up | ||
| 245 | $wig->mean($tot/@values); | ||
| 246 | } | ||
| 247 | $wig->set_value($_=>$values[$_-1]) for(1..500); | ||
| 248 | |||
| 249 | my $f0 = Bio::Graphics::Feature->new(-start => 1, | ||
| 250 | -end => 30, | ||
| 251 | -score => 10, | ||
| 252 | -strand=> +1, | ||
| 253 | -source => 'confirmed', | ||
| 254 | -type => 'UTR', | ||
| 255 | ); | ||
| 256 | my $f1 = Bio::Graphics::Feature->new(-start => 31, | ||
| 257 | -end => 100, | ||
| 258 | -score => 20, | ||
| 259 | -strand=> +1, | ||
| 260 | -source => 'confirmed', | ||
| 261 | -type => 'CDS', | ||
| 262 | ); | ||
| 263 | my $f2 = Bio::Graphics::Feature->new(-start => 200, | ||
| 264 | -end => 300, | ||
| 265 | -score => 30, | ||
| 266 | -strand=> +1, | ||
| 267 | -source=> 'unconfirmed', | ||
| 268 | -type => 'CDS', | ||
| 269 | ); | ||
| 270 | my $f3 = Bio::Graphics::Feature->new(-start => 400, | ||
| 271 | -end => 450, | ||
| 272 | -score => 40, | ||
| 273 | -strand=> +1, | ||
| 274 | -source=> 'unconfirmed', | ||
| 275 | -type => 'CDS', | ||
| 276 | ); | ||
| 277 | my $f4 = Bio::Graphics::Feature->new(-start => 451, | ||
| 278 | -end => 500, | ||
| 279 | -score => 50, | ||
| 280 | -strand=> +1, | ||
| 281 | -source=>'confirmed', | ||
| 282 | -type => 'UTR', | ||
| 283 | ); | ||
| 284 | |||
| 285 | my $feature1 = Bio::Graphics::Feature->new(-type=>'mRNA', | ||
| 286 | -name=>'f1', | ||
| 287 | -desc=>'This is a one-level feature', | ||
| 288 | -strand=>+1, | ||
| 289 | -start=>1, | ||
| 290 | -end=>500, | ||
| 291 | -attributes=>{ | ||
| 292 | wigfile=>$wig, | ||
| 293 | wigfileA=>$wig, | ||
| 294 | wigfileB=>$wig, | ||
| 295 | } | ||
| 296 | ); | ||
| 297 | my $feature2 = Bio::Graphics::Feature->new(-type=>'mRNA', | ||
| 298 | -name=>'f2', | ||
| 299 | -desc=>'This is a two-level feature', | ||
| 300 | -strand=>+1, | ||
| 301 | -attributes=>{ | ||
| 302 | wigfile=>$wig, | ||
| 303 | wigfileA=>$wig, | ||
| 304 | wigfileB=>$wig, | ||
| 305 | } | ||
| 306 | ); | ||
| 307 | my $feature3 = Bio::Graphics::Feature->new(-type=>'mRNA', | ||
| 308 | -name=>'f3', | ||
| 309 | -desc=>'This is a two-level feature', | ||
| 310 | -strand=>+1, | ||
| 311 | -attributes=>{ | ||
| 312 | wigfile=>$wig, | ||
| 313 | wigfileA=>$wig, | ||
| 314 | wigfileB=>$wig, | ||
| 315 | } | ||
| 316 | ); | ||
| 317 | |||
| 318 | $feature2->add_SeqFeature($_) foreach ($f0,$f1,$f2,$f3,$f4); | ||
| 319 | $feature3->add_SeqFeature($_) foreach ($f0,$f1,$f3,$f4); | ||
| 320 | |||
| 321 | my $feature4 = Bio::Graphics::Feature->new(-type=>'gene', | ||
| 322 | -name=>'f4', | ||
| 323 | -desc=>'This is a three-level feature', | ||
| 324 | -attributes=>{ | ||
| 325 | wigfile=>$wig, | ||
| 326 | wigfileA=>$wig, | ||
| 327 | wigfileB=>$wig, | ||
| 328 | } | ||
| 329 | ); | ||
| 330 | $feature4->add_SeqFeature($feature2,$feature3); | ||
| 331 | |||
| 332 | $panel->add_track([$feature1,$feature2,$feature4], | ||
| 333 | @$args, | ||
| 334 | -key => 'No connector', | ||
| 335 | ); | ||
| 336 | |||
| 337 | $panel->add_track([$feature1,$feature2,$feature4], | ||
| 338 | @$args, | ||
| 339 | -connector => 'dashed', | ||
| 340 | -key => 'Dashed connector', | ||
| 341 | ); | ||
| 342 | } | ||
| 343 | |||
| 344 | |||
| 345 | 1; | ||
| 346 | |||
| 347 |