From 07b57d02148b3cd29569cccd9d6c8f5c99e7c910 Mon Sep 17 00:00:00 2001 From: Brian Osborne Date: Mon, 15 Sep 2014 11:48:30 -0400 Subject: [PATCH] Add ability to remove specific features, using primary_tag --- Bio/Seq.pm | 36 +++++++++++++++++++++--------------- t/SeqIO/genbank.t | 16 +++++++++++++++- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/Bio/Seq.pm b/Bio/Seq.pm index e83974103..92ed49a1b 100644 --- a/Bio/Seq.pm +++ b/Bio/Seq.pm @@ -1163,25 +1163,31 @@ sub add_SeqFeature { Title : remove_SeqFeatures Usage : $seq->remove_SeqFeatures(); - Function: Flushes all attached SeqFeatureI objects. - - To remove individual feature objects, delete those from the returned - array and re-add the rest. - Example : - Returns : The array of Bio::SeqFeatureI objects removed from this seq. - Args : None + Function: Removes all attached SeqFeatureI objects or those with the + specified primary tag + Example : my @gene_feats = $seq->remove_seqFeatures('gene') or + my @feats = $seq->remove_seqFeatures() + Returns : The array of Bio::SeqFeatureI objects removed from the sequence + Args : None, or a feature primary tag =cut sub remove_SeqFeatures { - my $self = shift; - + my ( $self, $type ) = @_; return () unless $self->{'_as_feat'}; - my @feats = @{$self->{'_as_feat'}}; - $self->{'_as_feat'} = []; - return @feats; -} + if ( $type ) { + my @selected_feats = grep { $_->primary_tag eq $type } @{ $self->{'_as_feat'} }; + my @unselected_feats = grep { $_->primary_tag ne $type } @{ $self->{'_as_feat'} }; + $self->{'_as_feat'} = \@unselected_feats; + return @selected_feats; + } + else { + my @all_feats = @{ $self->{'_as_feat'} }; + $self->{'_as_feat'} = []; + return @all_feats; + } +} =head1 Methods provided in the Bio::PrimarySeqI interface @@ -1191,8 +1197,8 @@ or other information as expected. See L for more information. Sequence Features are B transferred to the new objects. -This is possibly a mistake. Anyone who feels the urge in -dealing with this is welcome to give it a go. +To reverse complement and include the features use +L. =head2 revcom diff --git a/t/SeqIO/genbank.t b/t/SeqIO/genbank.t index b9fa2ef37..3c37dfc62 100644 --- a/t/SeqIO/genbank.t +++ b/t/SeqIO/genbank.t @@ -6,7 +6,7 @@ use strict; BEGIN { use lib '.'; use Bio::Root::Test; - test_begin(-tests => 292); + test_begin(-tests => 296); use_ok('Bio::SeqIO::genbank'); } @@ -679,3 +679,17 @@ is($anns[0]->value, 'join(WP_015639704.1:1..205)'); is($seq->seq, 'MENRKFGYIRVSSKDQNEGRQLEAMRKIGITERDIYLDKQSGKNFERANYQLLKRIIRKGDI' . 'LYIHSLDRFGRNKEEILQEWNDLTKNIEADIVVLDMPLLDTTQYKDSMGTFIADLVLQILSWMAEEERERIRK' . 'RQREGIDLALQNGIQFGRSPVVVSDEFKEVYRKWKAKELTAVEAMQEAGVKKTSFYKLVKAHENSIKVNS'); + +$seq = Bio::SeqIO->new(-format => 'genbank', + -file => test_input_file('YP_007988852.gp') )->next_seq; +@features = $seq->remove_SeqFeatures; +is $#features, 10, 'Got 11 features'; + +$seq = Bio::SeqIO->new(-format => 'genbank', + -file => test_input_file('YP_007988852.gp') )->next_seq; +@features = $seq->remove_SeqFeatures('CDS'); +is $#features, 0, 'Got 1 feature'; +is $features[0]->primary_tag, 'CDS', 'Correct primary tag for feature'; +@features = $seq->remove_SeqFeatures; +is $#features, 9, 'Got 10 features'; + -- 2.11.4.GIT