Commit d4be2146 authored by Kieron Taylor's avatar Kieron Taylor 😠
Browse files

Introducing a proxy Feature class to allow custom limitation of the boundaries...

Introducing a proxy Feature class to allow custom limitation of the boundaries of the feature. This implementation influences the get_all_ methods while leaving the standard methods unaffected.
parent 43fceee6
=head1 LICENSE
Copyright (c) 1999-2013 The European Bioinformatics Institute and
Genome Research Limited. All rights reserved.
This software is distributed under a modified Apache license.
For license details, please see
http://www.ensembl.org/info/about/code_licence.html
=head1 CONTACT
Please email comments or questions to the public Ensembl
developers list at <dev@ensembl.org>.
Questions may also be sent to the Ensembl help desk at
<helpdesk@ensembl.org>.
=cut
=head1 NAME
Bio::EnsEMBL::SubSlicedFeature
=head1 SYNOPSIS
my $truncated_gene = Bio::EnsEMBL::Utils::SubSlicedFeature->new(
-start => 300,
-end => 10000,
-feature => $gene);
my $transcripts = $truncated_gene->get_all_Transcripts();
# list of transcripts is limited to those within the coordinates, rather
# than the original feature Slice.
=head1 DESCRIPTION
Alters the behaviour of a normal Feature object to act within a user-specified
sub-slice of of its boundaries. As it stands, this only affects get_all_*
methods, meaning that seq_region_start() and project() will work on original
coordinates.
=cut
package Bio::EnsEMBL::SubSlicedFeature;
use Bio::EnsEMBL::Utils::Argument qw/rearrange/;
use base qw/Bio::EnsEMBL::Utils::Proxy/;
sub new {
my ($class, @args) = @_;
my ($start,$end,$original_feature) = rearrange([qw/start end feature/], @args);
my $self = $class->SUPER::new($original_feature);
$self->{'start'} = $start;
$self->{'end'} = $end;
return $self;
}
# Required by Proxy to control scope of the autoloaded methods.
# Also intercepts calls to get methods that would access Slice
sub __resolver {
my ($self, $package_name, $method) = @_;
if ($method =~ /^get_all_/) {
# Call original method and filter results to Proxy coordinates
return sub {
my ($local_self, @args) = @_;
my $feature_list = $local_self->__proxy()->$method(@args);
my @short_list;
foreach my $feature (@$feature_list) {
if ($feature->start > $local_self->{'start'}
&& $feature->end < $local_self->{'end'}) {
push @short_list,$feature;
}
}
return \@short_list;
}
} else {
# No intervention required, call original object method
return sub {
my ($local_self, @args) = @_;
return $local_self->__proxy()->$method(@args);
};
}
}
1;
\ No newline at end of file
use strict;
use warnings;
use Test::More;
use Bio::EnsEMBL::Test::MultiTestDB;
use Bio::EnsEMBL::SubSlicedFeature;
my $multi = Bio::EnsEMBL::Test::MultiTestDB->new();
my $dba = $multi->get_DBAdaptor('core');
my $ga = $dba->get_GeneAdaptor();
my $gene = $ga->fetch_by_stable_id('ENSG00000125964');
diag($gene->stable_id);
diag($gene->start);
diag($gene->end);
my $transcript_list = $gene->get_all_Transcripts;
diag(scalar(@$transcript_list));
foreach (@$transcript_list) {
diag($_->stable_id);
diag($_->start);
diag($_->end);
}
my $fake_gene = Bio::EnsEMBL::SubSlicedFeature->new(-feature => $gene, -start => 30840810, -end => 30859270);
$transcript_list = $fake_gene->get_all_Transcripts;
diag (scalar(@$transcript_list));
foreach (@$transcript_list) {
diag($_->stable_id);
diag($_->start);
diag($_->end);
}
is ($transcript_list->[0]->stable_id,"ENST00000216932", "Only one transcript found in subsliced Gene");
my $exon_list = $fake_gene->get_all_Exons;
foreach (@$exon_list) {
diag($_->stable_id);
diag($_->start);
diag($_->end);
}
ok(scalar(@$exon_list) == 4, "Correct Exons for subsliced Gene");
is($exon_list->[0]->stable_id,'ENSE00001048819', "Correct Exon returned for subsliced Gene");
$fake_gene = Bio::EnsEMBL::SubSlicedFeature->new(-feature => $gene, -start => 1, -end => 2);
$transcript_list = $fake_gene->get_all_Transcripts;
ok (scalar(@$transcript_list) == 0, "Out of bounds search for features");
# Check normal feature functions are unaffected.
ok($fake_gene->start == 30822726, "Normal feature function behaves through proxy");
done_testing;
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment