Skip to content

Commit

Permalink
Merge branch 'master' of github.com:bioperl/bioperl-live
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Fields committed Oct 24, 2011
2 parents f20b8fd + 416a408 commit f3c6e04
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 36 deletions.
65 changes: 37 additions & 28 deletions Bio/FeatureIO/gff.pm
Expand Up @@ -208,50 +208,49 @@ sub next_feature_group {
$self->{group_not_done} = 1;

while ($self->{group_not_done} && ($feat = $self->next_feature()) && defined($feat)) {
# we start by collecting all features in the group and
# memorizing those which have an ID attribute
# we start by collecting all features in the group and
# memorizing those which have an ID attribute
my $anno_ID = $feat->get_Annotations('ID');
if(ref($anno_ID)) {
if(ref($anno_ID)) {
my $attr_ID = $anno_ID->value;
$self->throw("Oops! ID $attr_ID exists more than once in your file!")
if (exists($seen_ids{$attr_ID}));
if (exists($seen_ids{$attr_ID}));
$seen_ids{$attr_ID} = $feat;
}
push(@all_feats, $feat);
}
push(@all_feats, $feat);
}

# assemble the top-level features
foreach $feat (@all_feats) {
my @parents = $feat->get_Annotations('Parent');
if (@parents) {
my @parents = $feat->get_Annotations('Parent');
if (@parents) {
foreach my $parent (@parents) {
my $parent_id = $parent->value;
$self->throw("Parent with ID $parent_id not found!") unless (exists($seen_ids{$parent_id}));
$seen_ids{$parent_id}->add_SeqFeature($feat);
}
} else {
push(@toplevel_feats, $feat);
my $parent_id = $parent->value;
$self->throw("Parent with ID $parent_id not found!") unless (exists($seen_ids{$parent_id}));
$seen_ids{$parent_id}->add_SeqFeature($feat);
}
} else {
push(@toplevel_feats, $feat);
}
}

return @toplevel_feats;
}

=head2 next_seq()
access the FASTA section (if any) at the end of the GFF stream. note that this method
will return undef if not all features in the stream have been handled
Usage : $featureio->next_seq( );
Function: access the FASTA section (if any) at the end of the GFF stream. Note
that this method will return undef before all the features in the GFF
stream have been handled.
Returns : a Bio::SeqI object or undef
Args : none
=cut

sub next_seq() {
my $self = shift;
return unless $self->fasta_mode();

#first time next_seq has been called. initialize Bio::SeqIO instance
if(!$self->seqio){
$self->seqio( Bio::SeqIO->new(-format => 'fasta', -fh => $self->_fh()) );
}
return undef unless $self->fasta_mode();
return $self->seqio->next_seq();
}

Expand Down Expand Up @@ -295,7 +294,8 @@ sub write_feature {
=head2 fasta_mode()
Usage : $obj->fasta_mode($newval)
Function:
Function: Returns whether or not we're currently in a FASTA section of the GFF
stream
Example :
Returns : value of fasta_mode (a scalar)
Args : on set, new value (a scalar or undef, optional)
Expand All @@ -311,7 +311,7 @@ sub fasta_mode {
$self->{'fasta_mode'} = $val if defined($val);

if ($val && $val == 1) {
# seek $self->_fh(), -1, 1; #rewind 1 byte to get the previous line's \n
#seek $self->_fh(), -1, 1; #rewind 1 byte to get the previous line's \n
$self->_pushback("\n");
}

Expand All @@ -321,19 +321,28 @@ sub fasta_mode {
=head2 seqio()
Usage : $obj->seqio($newval)
Function: holds a Bio::SeqIO instance for handling the GFF3 ##FASTA section.
Returns : value of seqio (a scalar)
Function: get/set a Bio::SeqIO instance to handle the GFF3 ##FASTA section.
Returns : a Bio::SeqIO object or undef
Args : on set, new value (a scalar or undef, optional)
=cut

sub seqio {
my($self,$val) = @_;
$self->{'seqio'} = $val if defined($val);
if (defined $val) {
$self->{'seqio'} = $val;
} else {
# Cannot get seqio before we've reached the ##FASTA section
return undef unless $self->fasta_mode();
if (not defined $self->{'seqio'}) {
# Initialize Bio::SeqIO instance
$self->{'seqio'} = Bio::SeqIO->new(-format => 'fasta', -fh => $self->_fh());
}
}
return $self->{'seqio'};
}


=head2 sequence_region()
Usage :
Expand Down
2 changes: 1 addition & 1 deletion Bio/Seq/SimulatedRead.pm
Expand Up @@ -248,7 +248,7 @@ sub _create_desc {
}
# Description of the original sequence
my $ref_desc = $self->reference->desc;
if (defined $self->reference->desc) {
if ( (defined $self->reference->desc) && ($self->reference->desc !~ m/^\s*$/) ) {
$ref_desc =~ s/"/\\"/g; # escape double-quotes to \"
$desc_str .= 'description="'.$ref_desc.'" ';
}
Expand Down
2 changes: 1 addition & 1 deletion Bio/SeqFeature/Annotated.pm
Expand Up @@ -719,7 +719,7 @@ sub attach_seq {
my ($self, $seq) = @_;

if ( ! ($seq && ref($seq) && $seq->isa("Bio::PrimarySeqI")) ) {
$self->throw("Must attach Bio::PrimarySeqI objects to SeqFeatures");
$self->throw("Must attach Bio::PrimarySeqI objects to SeqFeatures but got '".ref($seq)."'");
}

$self->{'seq'} = $seq;
Expand Down
2 changes: 1 addition & 1 deletion Bio/SeqFeature/Generic.pm
Expand Up @@ -586,7 +586,7 @@ sub attach_seq {
my ($self, $seq) = @_;

if ( ! ($seq && ref($seq) && $seq->isa("Bio::PrimarySeqI")) ) {
$self->throw("Must attach Bio::PrimarySeqI objects to SeqFeatures");
$self->throw("Must attach Bio::PrimarySeqI objects to SeqFeatures but got '".ref($seq)."'");
}

$self->{'_gsf_seq'} = $seq;
Expand Down
23 changes: 23 additions & 0 deletions Bio/SimpleAlign.pm
Expand Up @@ -1064,6 +1064,29 @@ sub select_noncont {
return $aln;
}

=head2 select_noncont_by_name
Title : select_noncont_by_name
Usage : my $aln2 = $aln->select_noncont_by_name('A123', 'B456');
Function : Creates a new alignment from a subset of sequences which are
selected by name (sequence ID).
Returns : a Bio::SimpleAlign object
Args : array of names (i.e., identifiers) for the sequences.
=cut

sub select_noncont_by_name {
my ($self, @names) = @_;

my $aln = $self->new;
foreach my $name (@names) {
$aln->add_seq($self->get_seq_by_id($name));
}
$aln->id($self->id);

return $aln;
}

=head2 slice
Title : slice
Expand Down
15 changes: 12 additions & 3 deletions t/Align/SimpleAlign.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {
use lib '.';
use Bio::Root::Test;

test_begin( -tests => 201 );
test_begin( -tests => 198 );

use_ok('Bio::SimpleAlign');
use_ok('Bio::AlignIO');
Expand Down Expand Up @@ -38,7 +38,7 @@ my $aln2 = $aln->select( 1, 3 );
isa_ok( $aln2, 'Bio::Align::AlignI' );
is( $aln2->num_sequences, 3, 'num_sequences' );

# test select non continuous-sorted by default
# test select non contiguous-sorted by default
$aln2 = $aln->select_noncont( 6, 7, 8, 9, 10, 1, 2, 3, 4, 5 );
is( $aln2->num_sequences, 10, 'num_sequences' );
is(
Expand All @@ -52,7 +52,7 @@ is(
'select_noncont'
);

# test select non continuous-nosort option
# test select non contiguous-nosort option
$aln2 = $aln->select_noncont( 'nosort', 6, 7, 8, 9, 10, 1, 2, 3, 4, 5 );
is( $aln2->num_sequences, 10, 'num_sequences' );
is(
Expand All @@ -66,6 +66,15 @@ is(
'select_noncont'
);

# test select non contiguous by name
my $aln3 = $aln->select_noncont_by_name('1433_LYCES','BMH1_YEAST','143T_HUMAN');
is( $aln3->num_sequences, 3, 'select_noncont_by_name' );
my @seqs3 = $aln3->each_seq();
is $seqs3[0]->id, '1433_LYCES', 'select_noncont_by_name';
is $seqs3[1]->id, 'BMH1_YEAST', 'select_noncont_by_name';
is $seqs3[2]->id, '143T_HUMAN', 'select_noncont_by_name';


@seqs = $aln->each_seq();
is scalar @seqs, 16, 'each_seq';
is $seqs[0]->get_nse, '1433_LYCES/9-246', 'get_nse';
Expand Down
8 changes: 6 additions & 2 deletions t/SeqFeature/FeatureIO.t
Expand Up @@ -7,8 +7,8 @@ BEGIN {
use lib '.';
use Bio::Root::Test;

test_begin(-tests => 47,
-requires_module => 'Graph');
test_begin(-tests => 50,
-requires_module => 'Graph');

use_ok('Bio::FeatureIO');
}
Expand All @@ -35,6 +35,7 @@ while($f = $io->next_feature()){
}
is($fcount, 0);


#then try to read sequences again. should get seqs now
while($s = $io->next_seq()){
$scount++;
Expand Down Expand Up @@ -84,6 +85,7 @@ $scount = 0;
ok( $io = Bio::FeatureIO->new( -file => test_input_file('hybrid1.gff3') ) );

#try to read sequences first. should be undef
is $io->seqio, undef;
while($s = $io->next_seq()){
$scount++;
}
Expand All @@ -96,8 +98,10 @@ while($f = $io->next_feature()){
is($fcount , 6);

#then try to read sequences again.
isa_ok $io->seqio, 'Bio::SeqIO';
while($s = $io->next_seq()){
$scount++;
isa_ok $s, 'Bio::Seq';
TODO: {
local $TODO = 'How did this ever work?!?';
if ($scount == 1) {
Expand Down

0 comments on commit f3c6e04

Please sign in to comment.