[Bioperl-l] RE: Passing extra arguments to method references inBio::Graphics::Panel::add_track

Crabtree, Jonathan crabtree at tigr.org
Mon May 9 11:54:19 EDT 2005


Michael-

What you're trying to do is known as "currying" in functional
programming parlance.  Perl (5, at least) doesn't support currying
directly, but you can implement it yourself using a simple closure.  For
example, instead of this:

> -label      => \&gene_description($start)

you could use something like the following (assuming that
&gene_description expects the $start argument, followed by the usual
arguments to a Bioperl track callback subroutine):

  -label  => sub { my @args = @_;  return &gene_description($start,
@args); }

Or if you need to repeat this trick a number of times, with different
$start values, you could create yourself a subroutine-generating
subroutine, like so (in this example I've made the outer subroutine
anonymous, but you don't have to):

my $funMaker = sub {
	my $start = shift;
	return sub {
		my @args = @_;
		return &gene_description($start, @args);
 	}
};

and then in the add_track method you'd say:

  -label => &$funMaker($my_start_value),

If you google some combination of "currying", "perl", and "closure"
you'll find a bunch of pages that discuss these and similar techniques.
Hope this helps,

Jonathan

> -----Original Message-----
> From: bioperl-l-bounces at portal.open-bio.org 
> [mailto:bioperl-l-bounces at portal.open-bio.org] On Behalf Of 
> michael watson (IAH-C)
> Sent: Monday, May 09, 2005 11:31 AM
> To: Bioperl
> Subject: [Bioperl-l] RE: Passing extra arguments to method 
> references inBio::Graphics::Panel::add_track
> 
> 
> Hi
> 
> Sorry, a bit hasty on the trigger....
> 
> I am on bioperl-1.5.
> 
> I'm using the following code to create some rather tasty images:
> 
> $panel->add_track(transcript2  => \@includeCDS,
> 		  -bgcolor    =>  'blue',
> 		  -fgcolor    =>  'black',
> 		  -key        => 'CDS',
> 		  -bump       =>  0,
> 		  -height     =>  10,
> 		  -label      => \&gene_description,
> 		  -description=> \&gene_label,
> 		 );
> 
> This is fairly standard, and @includeCDS is a bunch of 
> feature objects.
> 
> What I want to do is pass extra arguments to 
> &gene_description, and then within &gene_description check to 
> see if the feature start is greater than a certain value (the 
> extra argument).  If it is, then I want to return an empty 
> string, if it isn't I want to return the gene description.  
> Something like:
> 
> -label      => \&gene_description($start)
> 
> But when I tried that it didn't work ;-(.
> 
> So is it possible to pass extra arguments to those functions 
> I am referencing above?
> 
> Many thanks
> 
> Mick
> 
> _______________________________________________
> Bioperl-l mailing list
> Bioperl-l at portal.open-bio.org 
> http://portal.open-> bio.org/mailman/listinfo/bioperl-l
> 



More information about the Bioperl-l mailing list