1 package Sequence::File;
8 use base qw(Root Read Write);
10 =head2 $file->get_entries
12 Returns all records so far found in the file.
19 if ( exists $self->{ __PACKAGE__ . "entries" } ) {
20 return @{ $self->{ __PACKAGE__ . "entries" } };
26 =head2 $file->get_entry(@positions);
28 Retrieves the @position's'th record found in the file or undef if there is no such sequence.
33 my ( $self, @offsets ) = @_;
35 if ( exists $self->{ __PACKAGE__ . "entries" } and @offsets ) {
36 return @{ $self->{ __PACKAGE__ . "entries" } }[@offsets];
42 =head2 $self->add_entries(@entries)
44 Adds more entries to the object. Returns the number of entries added to the object. This may be less than those passed if set_max_entries() has been called.
49 my ( $self, @entries ) = @_;
50 return unless @entries;
53 croak "Adding non Sequence object" unless isa $_, "Sequence";
54 $self->_ids( $_->id );
55 push @{ $self->{ __PACKAGE__ . "entries" } }, $_;
58 # my $max = $self->get_max_entries;
60 # my $exist_size = @{ $self->{__PACKAGE__."entries"} };
61 # if ($exist_size > $max) { return 0 }
62 # elsif ($exist_size + @entries > $max) {
63 # return push @{ $self->{__PACKAGE__."entries"} }, @entries[0..$max - $exist_size];
66 # return push @{ $self->{__PACKAGE__."entries"} }, @entries;
70 # return push @{ $self->{__PACKAGE__."entries"} }, @entries;
74 =head2 $file->get_entry_by_id(/regex/);
76 Returns all of those entries which have id's that match the regex.
80 # Cache of sequence IDs for fast grepping
82 my ( $self, $id ) = @_;
83 if ($id) { push @{ $self->{ __PACKAGE__ . "ids" } }, $id }
84 else { return @{ $self->{ __PACKAGE__ . "ids" } } }
88 my ( $self, $id ) = @_;
89 croak "No id passed" unless defined $id;
91 #return grep { $_->id =~ /$id/ } $self->get_entries;
94 my @ids = $self->_ids;
95 my @indices = grep { $ids[$_] =~ /$id/ } 0 .. $#ids;
96 return $self->get_entry(@indices);
100 =head2 $file->set_max_entries($size);
102 Limits the storing of $size records. Will prevent the addition of more records,
103 but won't delete existing records in the object if there are already more than
108 sub set_max_entries {
109 my ( $self, $size ) = @_;
111 $self->{ __PACKAGE__ . "max_size" } = $size;
115 =head2 $file->get_max_entries
117 Accessor for set_max_entries().
121 sub get_max_entries {
123 return $self->{ __PACKAGE__ . "max_size" };
131 my ( $self, $start, $end ) = @_;
133 croak "Not passed start and end arguments" unless 2 == grep { defined } $start, $end;
135 # Produce a new version of myself, in the right namespace
136 my ($new_self) = ( ref $self )->new;
138 for ( $self->get_entries ) {
139 my ($seq) = $_->sub_seq( $start, $end );
140 $new_self->add_entries($seq);