JPRED-2 Move Jpred 3.0.1 to public Git
[jpred.git] / jpred / lib / Sequence / File.pm
1 package Sequence::File;
2
3 use strict;
4 use warnings;
5 use Carp;
6
7 use UNIVERSAL qw(isa);
8 use base qw(Root Read Write);
9
10 =head2 $file->get_entries
11
12 Returns all records so far found in the file.
13
14 =cut
15
16 sub get_entries {
17         my ($self) = @_;
18
19         if (exists $self->{__PACKAGE__."entries"}) {
20                 return @{ $self->{__PACKAGE__."entries"} };
21         }
22         else { return undef; }
23 }
24
25 =head2 $file->get_entry(@positions);
26
27 Retrieves the @position's'th record found in the file or undef if there is no such sequence.
28
29 =cut
30
31 sub get_entry {
32         my ($self, @offsets) = @_;
33
34         if (exists $self->{__PACKAGE__."entries"} and @offsets) {
35                 return @{ $self->{__PACKAGE__."entries"} }[@offsets];
36         }
37         else { return undef }
38 }
39
40 =head2 $self->add_entries(@entries)
41
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.
43
44 =cut
45
46 sub add_entries {
47         my ($self, @entries) = @_;
48         return unless @entries;
49
50         for (@entries) {
51                 croak "Adding non Sequence object" unless isa $_, "Sequence";
52                 $self->_ids($_->id);
53                 push @{ $self->{__PACKAGE__."entries"} }, $_;
54         }
55
56 #       my $max = $self->get_max_entries;
57 #       if (defined $max) {
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];
62 #               }
63 #               else {
64 #                       return push @{ $self->{__PACKAGE__."entries"} }, @entries;
65 #               }
66 #       }
67 #       else {
68 #               return push @{ $self->{__PACKAGE__."entries"} }, @entries;
69 #       }
70 }
71
72 =head2 $file->get_entry_by_id(/regex/);
73
74 Returns all of those entries which have id's that match the regex.
75
76 =cut
77
78 # Cache of sequence IDs for fast grepping
79 sub _ids {
80         my ($self, $id) = @_;
81         if ($id) { push @{ $self->{__PACKAGE__."ids"} }, $id }
82         else { return @{ $self->{__PACKAGE__."ids"} } }
83 }
84
85 sub get_entry_by_id {
86         my ($self, $id) = @_;
87         croak "No id passed" unless defined $id;
88
89         #return grep { $_->id =~ /$id/ } $self->get_entries;
90
91         {
92                 my @ids = $self->_ids;
93                 my @indices = grep { $ids[$_] =~ /$id/ } 0..$#ids;
94                 return $self->get_entry(@indices);
95         }
96 }
97
98 =head2 $file->set_max_entries($size);
99
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.
101
102 =cut
103
104 sub set_max_entries {
105         my ($self, $size) = @_;
106
107         $self->{__PACKAGE__."max_size"} = $size;
108         return $size;
109 }
110
111 =head2 $file->get_max_entries
112
113 Accessor for set_max_entries().
114
115 =cut
116
117 sub get_max_entries {
118         my ($self) = @_;
119         return $self->{__PACKAGE__."max_size"};
120 }
121
122 =head2 
123
124 =cut
125
126 sub sub_seq {
127         my ($self, $start, $end) = @_;
128
129         croak "Not passed start and end arguments" unless 2 == grep { defined } $start, $end;
130
131         # Produce a new version of myself, in the right namespace
132         my ($new_self) = (ref $self)->new;
133
134         for ($self->get_entries) {
135                 my ($seq) = $_->sub_seq($start, $end);
136                 $new_self->add_entries($seq);
137         }
138
139         return $new_self;
140 }
141
142 1;