Online Book Reader

Home Category

Beautiful Code [182]

By Root 5218 0
easy to do as I hoped it would be, judging from the number of user-support requests I received. So, after some experience writing my own Bio:: Graphics-based genome browser, I added an image_and_map() method to Bio::Graphics:: Panel. Here is a code fragment that illustrates how to use this method:

Code View: Scroll / Show All

$panel = Bio::Graphics::Panel->new(@args);

$panel->add_track(@args);

$panel->add_track(@args);

...

($url,$map,$mapname) = $panel->image_and_map(

-root => '/var/www/html',

-url => '/images',

-link => sub {

my $feature = shift;

my $name = $feature->display_name;

return "http://www.google.com/search?q=$name";

}

print "

My Genome

";

print "";

print $map;

We set up a Panel and add tracks to it as before. We then call image_and_map() with three argument/value pairs. The -root argument gives the physical location of the web server's document root—the place where the tree of HTML files starts. -url indicates where, relative to the document root, Bio::Graphics-generated images should be stored. The -link argument is a callback that Bio::Graphics invokes to attach a clickable link to a glyph. In this case, we recover the feature object from the callback's argument list, get the feature's human-readable name by calling display_name(), and generate a Google search link. Several other image_and_map() options can be used to customize and extend the resulting image map.

The method generates the image and stores it into the filesystem at the location indicated by -root and -url—in this case, /var/www/html/images. It then returns a three-member result list consisting of a URL for the generated image, the HTML for the image map, and the name of the image map for use in the tag. We then simply print the appropriate fragments of HTML to use the image and its map.

To date there are two web-based genome browsers based on Bio::Graphics. The one that I wrote, called GBrowse (http://www.gmod.org/gbrowse), is now widely used to display a large number of genomes ranging from bacteria to man. However, it was written in 2002 before Ajax-based asynchronous page refreshes were invented; one moves along the genome by clicking arrow buttons and waiting for the screen to reload. A new browser that is currently in prototype stage, the Ajax Generic Genome Browser (http://genome.biowiki.org/gbrowse), provides Google Maps-style functionality for the genome. To navigate, one simply grabs the view and slides it.

12.3.2. Supporting Publication-Quality Images

Another original requirement was support for multiple graphics formats. To satisfy this, I designed Bio::Graphics to use the Perl GD library for its low-level graphics calls. This library, which is based on Tom Boutell's libgd (http://www.libgd.org), generates pixmap-based images in a variety of formats, including PNG, JPEG, and GIF.

The Panel object creates and maintains a GD graphics object internally and passes this object to each of its tracks' draw() routines. The tracks, in turn, pass the GD object to their glyphs, and the glyphs to their subglyphs.

The Bio::Graphics::Panel->png() method is simply a pass-through to GD's png() method:

# in Bio::Graphics::Panel

sub png {

my $self = shift;

my $gd = $self->gd;

return $gd->png;

}

The jpeg() and gif() methods are similar. The developer also has the option of recovering the raw GD object and calling its png() method:

$gd = $panel->gd;

print $gd->png;

The advantage of making the internal GD object available to the public interface is that the developer can do additional things with the GD object, such as embedding it in a larger picture or manipulating its colors.

One consequence of my choice to use GD was that Bio::Graphics was originally limited to the generation of pixmap images. This problem was solved by Todd Harris when he wrote the Perl GD:::SVG module (http://toddot.net/projects/GD-SVG). GD::SVG has the same API as GD, but generates Scalable Vector Graphics (SVG) images, which can be printed

Return Main Page Previous Page Next Page

®Online Book Reader