[Bioperl-l] Generalized _setparams proposal

Sendu Bala bix at sendu.me.uk
Wed Jan 10 10:49:31 EST 2007

Bioperl-run wrappers (and some other modules) typically store parameters 
from the user intended for the program they wrap via methods of similar 
name to the parameter. The wrappers then implement their own way of 
taking the values of those methods and generating a parameter string 
suitable for passing to the program.

Typically this is done in a _setparams() method with code along the 
lines of:

$param_string .= ' --param_x '.$self->param_x if $self->param_x;

or a number of modules have their own variation on:

# clustalw's _setparam method
my $param_string = "";
for  $attr ( @CLUSTALW_PARAMS ) {
   $value = $self->$attr();
   next unless (defined $value);
   my $attr_key = lc $attr; #put params in format expected by clustalw
   $attr_key = ' -'.$attr_key;
   $param_string .= $attr_key.'='.$value;

I propose generalizing this into a _setparams method in 
Bio::Tools::Run::WrapperBase, eg. such that the above could be 
re-implemented as:

# clustalw's _setparam method
my $param_string = $self->SUPER::_setparam(
   -methods => \@CLUSTALW_PARAMS,
   -dash => 1,
   -lc => 1,
   -join => '=');

Proposed code follows:

=head2  _setparams()

  Title   : _setparams
  Usage   : $params = $self->_setparams(-methods => [qw(window 
  Function: For internal use by wrapper modules to build parameter strings
            suitable for sending to the program being wrapped. For each 
            name supplied, calls the method and adds the method name (as 
            by optional things) along with its value (unless a switch) 
to the
            parameter string
  Example : $params = $self->_setparams(-methods => [qw(window 
                                        -double_dash => 1,
                                        -underscore_to_dash => 1);
            If window() had not been previously called, but 
            had been called, $params would be ' --evalue-cutoff 0.5'

            A separate call should be used for methods that store values 
            are intended to be valueless switches:

            $params .= $self->_setparams(-methods => [qw(simple large all)],
                                         -switches => 1,
                                         -double_dash => 1,
                                         -underscore_to_dash => 1);
            If simple() had not been called, large(1) had been called 
and all(0)
            had been called, $params would now be ' --evalue-cutoff 0.5 

  Returns : parameter string
  Args    : -methods => [] or {} # REQUIRED, array ref of method names 
to call,
                                   or hash ref where keys are method 
names and
                                   values are how those names should be 
                                   in the params string
            -join => string      # define how parameters and their 
values are
                                   joined, default ' '. (eg. could be 
'=' for
            -switches => boolean # all supplied methods are treated as 
            -lc => boolean       # lc() method names prior to output in 
            -dash => boolean     # prefix all method names with a single 
            -double_dash => bool # prefix all method names with a double 
            -underscore_to_dash => boolean # convert all underscores in 
                                             names to dashes


sub _setparams {
     my ($self, @args) = @_;

     my ($methods, $switch, $join, $lc, $d, $dd, $utd) =
                               UNDERSCORE_TO_DASH)], @args);
     $self->throw('-methods is required') unless $methods;
     $self->throw("-dash and -double_dash are mutually exclusive") if 
($d && $dd);
     $join ||= ' ';

     my %methods = ref($methods) eq 'HASH' ? %{$methods} : map { $_ => 
$_ } @{$methods};

     my $param_string = '';
     while (my ($method, $method_out) = each %methods) {
         my $value = $self->$method();
         next unless (defined $value);
         next if ($switch && ! $value);

         $method_out = lc($method_out) if $lc;
         $method_out = '-'.$method_out if $d;
         $method_out = '--'.$method_out if $dd;
         $method_out =~ s/_/-/g if $utd;

         $param_string .= ' '.$method_out.($switch ? '' : $join.$value);

     return $param_string;

More information about the Bioperl-l mailing list