Online Book Reader

Home Category

Beautiful Code [179]

By Root 5007 0
'orange',

18 -font2color => 'red',

19 -height =>20,

20 -label => 1,

21 -description => 1,

22 );

23 print $panel->png;

Figure 12-2. The output from Example 12-4

We load the Bio::Graphics library and one of BioPerl's standard Bio::SeqFeatureI classes, Bio::SeqFeature::Generic (lines 3–4). In order to avoid repeatedly typing out the full name of the feature class, we store it in a variable (line 5).

We then create three Bio::SeqFeature::Generic objects. One feature starts at position 1 and ends at position 1000, and will be used to draw a track containing the scale for the image (line 6). Two others will be features in a second track (lines 7–11).

We create the panel, passing it options that specify its width in pixels, its length in base pairs, and additional whitespace to pad the image with on the left and right (lines 12–13).

Next, we create a track for the image scale (line 14). It consists of a single feature, contained in the variable $span, and options that select the arrow glyph, make the arrow double-headed (-double=>1), and print both major and minor tick marks on the arrow (-tick=>2).

Now it's time to create a track for the two features, $test1_feat and $test2_feat. We add a second track, this time specifying options to use the box glyph with a background color of orange, a description font color of red, and a height of 20 pixels. We also selectively turn on the printing of the feature name and description (lines 15–22).

The last step is to call the panel object's png() method to convert it into a PNG graphic, and to print the graphic to standard output where it can be saved to a file or piped to a graphics display program (line 23).

12.2.6. Dynamic Options

The original Bio::Graphics::Glyph::Factory design was based around the idea of simple static option values. However, as I started working with the first version of Bio::Graphics, I realized that it would be handy to give the developer the ability to compute some options dynamically.

For example, scattered along the genome are sites on the DNA where regulatory proteins attach. When a regulatory protein attaches to a specific site of the DNA (a process called "binding"), a nearby gene is typically turned on or off. Some binding sites are strong, while others are weak, and the strength of the DNA/protein binding interaction is often of great importance to understanding how the regulatory interaction works.

To create a track showing the positions and relative strengths of DNA/protein binding site features, a developer might want to show a series of rectangles. The start and end of each rectangle would show the span of the feature, and its background (interior) color would show the strength of binding: white for weak, pink for medium, and red for strong. Under the original design the developer could specify the background color for all features in the track like this:

@features = get_features_somehow();

$panel->add_track(\@features,

-glyph => 'generic',

-bgcolor => 'pink');

However, this offered no way to set the color on a feature-by-feature basis.

When I realized this limitation, I went back and extended the API to allow the values of options to be CODE references. These are anonymous subroutines that Perl programmers can define in a variety of ways and are used in much the same way that function pointers are used in C. Here is a revised add_track() call that takes advantage of this facility:

$panel->add_track(\@features,

-glyph => 'box',

-bgcolor => sub {

my $feature = shift;

my $score = $feature->score;

return 'white' if $score < 0.25;

return 'pink' if $score < 0.75;

return 'red';

}

);

This works as follows: the value of -bgcolor is an anonymous CODE reference created using the sub keyword without a subroutine name. The code is invoked at runtime each time the glyph wants to access the value of its bgcolor option. The subroutine receives the corresponding feature on its argument array and calls its score() method to get the binding-site strength. Assuming that the binding-site strength

Return Main Page Previous Page Next Page

®Online Book Reader