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"} };
22 else { return undef; }
25 =head2 $file->get_entry(@positions);
27 Retrieves the @position's'th record found in the file or undef if there is no such sequence.
32 my ($self, @offsets) = @_;
34 if (exists $self->{__PACKAGE__."entries"} and @offsets) {
35 return @{ $self->{__PACKAGE__."entries"} }[@offsets];
40 =head2 $self->add_entries(@entries)
42 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.
47 my ($self, @entries) = @_;
48 return unless @entries;
51 croak "Adding non Sequence object" unless isa $_, "Sequence";
53 push @{ $self->{__PACKAGE__."entries"} }, $_;
56 # my $max = $self->get_max_entries;
58 # my $exist_size = @{ $self->{__PACKAGE__."entries"} };
59 # if ($exist_size > $max) { return 0 }
60 # elsif ($exist_size + @entries > $max) {
61 # return push @{ $self->{__PACKAGE__."entries"} }, @entries[0..$max - $exist_size];
64 # return push @{ $self->{__PACKAGE__."entries"} }, @entries;
68 # return push @{ $self->{__PACKAGE__."entries"} }, @entries;
72 =head2 $file->get_entry_by_id(/regex/);
74 Returns all of those entries which have id's that match the regex.
78 # Cache of sequence IDs for fast grepping
81 if ($id) { push @{ $self->{__PACKAGE__."ids"} }, $id }
82 else { return @{ $self->{__PACKAGE__."ids"} } }
87 croak "No id passed" unless defined $id;
89 #return grep { $_->id =~ /$id/ } $self->get_entries;
92 my @ids = $self->_ids;
93 my @indices = grep { $ids[$_] =~ /$id/ } 0..$#ids;
94 return $self->get_entry(@indices);
98 =head2 $file->set_max_entries($size);
100 Limits the storing of $size records. Will prevent the addition of more records, but won't delete existing records in the object if there are already more than $size entries.
104 sub set_max_entries {
105 my ($self, $size) = @_;
107 $self->{__PACKAGE__."max_size"} = $size;
111 =head2 $file->get_max_entries
113 Accessor for set_max_entries().
117 sub get_max_entries {
119 return $self->{__PACKAGE__."max_size"};
127 my ($self, $start, $end) = @_;
129 croak "Not passed start and end arguments" unless 2 == grep { defined } $start, $end;
131 # Produce a new version of myself, in the right namespace
132 my ($new_self) = (ref $self)->new;
134 for ($self->get_entries) {
135 my ($seq) = $_->sub_seq($start, $end);
136 $new_self->add_entries($seq);