[Bioperl-l] code reuse with moose
Siddhartha Basu
sidd.basu at gmail.com
Tue Aug 18 11:01:03 UTC 2009
Putting it in the bioperl list, makes more sense here,
On Wed, 12 Aug 2009, Chris Fields wrote:
> (BTW, this is re: the reimplementation of major chunks of BioPerl using
> Moose, Biome: http://github.com/cjfields/biome/tree/)
>
> Locations should use a Role (specifically, Biome::Role::Range), so
> start/end/strand should be attributes, not methods. With attributes the
> best way to do this is probably with a builder, and lazily (start
> requires end, and vice versa). Factor out the common code as Tomas
> indicates. BTW, the $self->throw() is akin to BioPerl's $self->throw()
> exception handling; it simply catches any exceptions and passes them to
> the metaclass exception handling.
>
> I've been thinking about making the Range role abstract for this very
> reason (or defining very basic attributes); something like:
>
> ----------------------------
>
> package Bio::Role::Range;
>
> requires qw(_build_start _build_end _build_strand);
>
> # also require other methods which need to be defined in implementation
>
> has 'start' => (
> isa => 'Int',
> is => 'rw',
> builder => '_build_start',
> lazy => 1
> );
>
> # same for end, strand (except strand has a different isa via
> MooseX::Types)
> ....
>
> package Bio::Location::Foo;
>
> with 'Bio::Role::Range';
>
> sub _build_start {
> # for location-specific start
> }
>
> sub _build_end {
> # for location-specific end
> }
>
> sub _build_strand {
> # for location-specific strand
> }
>
> sub _common_build_method {
> # factor out common code here, call from other builders
> }
>
> ----------------------------
This plan makes things much clearer. Currently the BioMe::Role::Location has a 'requires' keyword and rest of the
location modules consume that role to have its own implementation. At
this point on BioMe::Location::Atomic has attribute based 'start' and
'end' implememtation. I got a bit confused because in current bioperl
'Bio::Location::Simple' inherits from 'Bio::Location::Atomic' and when
i am trying to follow that path in BioMe it has to override that method.
So, my question is do all the location modules really needs to inherits
from each other. I am totally aware about the origianl design ideas but
it would be better to have a flatten hierarchy if possible.
One more thing, what about putting the 'start', 'end' and the other
common base attributes in BioMe::Role::Location instead of
BioMe::Role::Range. I am not sure which would be correct from bioperl
stand of view, just throwing out an idea.
>
> Also, I think the Coordinate-related stuff should be simplified down to a
> trait or an attribute; they bring in way too much overhead in bioperl w/o
> much added value.
You mean instead of having 'builder' method, having a specialized
traits handling those. That sounds like even better.
-siddhartha
>
> And now back to your regular Moose-related broadcast...
>
> chris
>
> On Aug 11, 2009, at 9:27 PM, Siddhartha Basu wrote:
>
> > Hi,
> > In one my classes i have this boilerplate code block that is repeated
> > all
> > over ....
> >
> > sub start {
> > my ( $self, $value ) = @_;
> > $self->{'_start'} = $value if defined $value;
> >
> > ## -- from here
> > $self->throw( "Only adjacent residues when location type "
> > . "is IN-BETWEEN. Not ["
> > . $self->{'_start'}
> > . "] and ["
> > . $self->{'_end'}
> > . "]" )
> > if defined $self->{'_start'}
> > && defined $self->{'_end'}
> > && $self->location_type eq 'IN-BETWEEN'
> > && ( $self->{'_end'} - 1 != $self->{'_start'} );
> > return $self->{'_start'};
> > ## -- here
> >
> > }
> >
> > then again ....
> >
> > sub end {
> > my ( $self, $value ) = @_;
> >
> > $self->{'_end'} = $value if defined $value;
> >
> > #assume end is the same as start if not defined
> > if ( !defined $self->{'_end'} ) {
> > if ( !defined $self->{'_start'} ) {
> > $self->warn('Calling end without a defined start
> > position');
> > return;
> > }
> > $self->warn('Setting start equal to end');
> > $self->{'_end'} = $self->{'_start'};
> > }
> >
> > ## ----
> >
> > $self->throw( "Only adjacent residues when location type "
> > . "is IN-BETWEEN. Not ["
> > . $self->{'_start'}
> > . "] and ["
> > . $self->{'_end'}
> > . "]" )
> > if defined $self->{'_start'}
> > && defined $self->{'_end'}
> > && $self->location_type eq 'IN-BETWEEN'
> > && ( $self->{'_end'} - 1 != $self->{'_start'} );
> >
> > return $self->{'_end'};
> > #---------
> > }
> >
> >
> > Is there any way moose can be used here for more code resuage. I
> > thought
> > about converted it to a type but still couldn't figure out how that
> > can
> > be done.
> >
> >
> > thanks,
> > -siddhartha
>
More information about the Bioperl-l
mailing list