Stone/GB_Sequence.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 194 CC 21 SCAR 45.6
CC
21
Cov
0%
_overlap_filter
182.0
features
72.0
_feature_filter
6.0
CRAP 95.8
1 package Stone::GB_Sequence;
2  
3 use strict;
4 use Carp;
5 use vars '@ISA';
6  
7 =head1 NAME
8  
9 Stone::GB_Sequence - Specialized Access to GenBank Records
10  
11 =head1 SYNOPSIS
12  
13 use Boulder::Genbank; # No need to use Stone::GB_Sequence directly
14 $gb = Boulder::Genbank->newFh qw(M57939 M28274 L36028);
15  
16 while ($entry = <$gb>) {
17 print "Entry's length is ",$entry->length,"\n";
18 @cds = $entry->match_features(-type=>'CDS');
19 @exons = $entry->match_features(-type=>'Exon',-start=>100,-end=>300);
20 }
21 }
22  
23 =head1 DESCRIPTION
24  
25 Stone::GB_Sequence provides several specialized access methods to the
26 various fields in a GenBank flat file record. You can return the
27 sequence as a Bio::Seq object, or query the sequence for features that
28 match positional or descriptional criteria that you provide.
29  
30 =head1 CONSTRUCTORS
31  
32 This class is not intended to be created directly, but via a
33 L<Boulder::Genbank> stream.
34  
35 =head1 METHODS
36  
37 In addition to the standard L<Stone> methods and accessors, the
38 following methods are provided. In the synopses, the variable
39 C<$entry> refers to a previously-created Stone::GB_Sequence object.
40  
41 =head2 $length = $entry->length
42  
43 Get the length of the sequence.
44  
45 =head2 $start = $entry->start
46  
47 Get the start position of the sequence, currently always "1".
48  
49 =head2 $end = $entry->end
50  
51 Get the end position of the sequence, currently always the same as the
52 length.
53  
54 =head2 @feature_list = $entry->features(-pos=>[50,450],-type=>['CDS','Exon'])
55  
56 features() will search the entry feature list for those features that
57 meet certain criteria. The criteria are specified using the B<-pos>
58 and/or B<-type> argument names, as shown below.
59  
60 =over 4
61  
62 =item -pos
63  
64 Provide a position or range of positions which the feature must
65 B<overlap>. A single position is specified in this way:
66  
67 -pos => 1500; # feature must overlap postion 1500
68  
69 or a range of positions in this way:
70  
71 -pos => [1000,1500]; # 1000 to 1500 inclusive
72  
73 If no criteria are provided, then features() returns all the features,
74 and is equivalent to calling the Features() accessor.
75  
76 =item -type, -types
77  
78 Filter the list of features by type or a set of types. Matches are
79 case-insensitive, so "exon", "Exon" and "EXON" are all equivalent.
80 You may call with a single type as in:
81  
82 -type => 'Exon'
83  
84 or with a list of types, as in
85  
86 -types => ['Exon','CDS']
87  
88 The names "-type" and "-types" can be used interchangeably.
89  
90 =head2 $seqObj = $entry->bioSeq;
91  
92 Returns a L<Bio::Seq> object from the Bioperl project. Dies with an
93 error message unless the Bio::Seq module is installed.
94  
95 =back
96  
97 =head1 AUTHOR
98  
99 Lincoln D. Stein <lstein@cshl.org>.
100  
101 =head1 COPYRIGHT
102  
103 Copyright 1997-1999, Cold Spring Harbor Laboratory, Cold Spring Harbor
104 NY. This module can be used and distributed on the same terms as Perl
105 itself.
106  
107 =head1 SEE ALSO
108  
109 L<Boulder>, L<Boulder:Genbank>, L<Stone>
110  
111 =cut
112  
113 @ISA = 'Stone';
114  
115 # -------------------- h'mmmmm, bogus alert! --------------------
116 # Return list of all features that overlap a particular range
117 #
118 # Example:
119 # @features = $gb->grab_features(-pos=>[50,450],-types=>['CDS','Exon'])
120 #
121 sub features {
122 my $self = shift;
123 my %param = @_;
124 my %p;
125 @p{map {s/^-//; s/s$//; lc $_} keys %param} = values %param;
126  
127 my $f = $self->Features;
128  
129 # regularize coordinates
130 my $pos = $p{po};
131 my ($left,$right) = ref($pos) ? @$pos : $pos;
132 $left ||= 0;
133 $right ||= $left;
134 ($left,$right) = ($right,$left) if $left > $right;
135  
136 # regularize types
137 my @types = ref $p{type} ? @{$p{type}} : $p{type} if $p{type};
138 @types = $f->tags unless @types;
139
140 # flatten into a list of all features of the specified type(s)
141 my @features;
142 for my $t (@types) {
143 my @f = $f->get("\u\L$t");
144 foreach (@f) { $_->insert(Type=>$t) unless $_->get('Type'); }
145 push @features,@f;
146 }
147  
148
149 @features = grep { _overlap_filter($_,$left,$right) } @features
150 if $left > 0 || $right > 0;
151  
152 return @features;
153 }
154  
155 sub length {
156 my $self = shift;
157 return length $self->Sequence;
158 }
159  
160 sub start { 1; }
161  
162 sub end { $_[0]->length }
163  
164 sub bioSeq {
165 my $self = shift;
166 my $id = $self->Accession;
167 my $seq = $self->Sequence;
168 my $desc = $self->Definition;
169  
170 eval { require Bio::Seq } || croak "Bio::Seq module not installed";
171 return Bio::Seq->new(-id=>$id,-sequence=>$seq,-desc=>$desc);
172 }
173  
174 sub _feature_filter {
175 my $f = shift;
176 my $types = shift;
177 foreach (@$types) {
178 return 1 if $f->get("\u\L$_");
179 }
180 return;
181 }
182  
183 sub _overlap_filter { # assumes left and right are numerically sorted
184 my $f = shift;
185 my ($left,$right) = @_;
186 return unless my $p = $f->Position;
187  
188 # simplest case -- single position
189 if ($p =~ /^(\d+)$/) {
190 return 1 if $1 >= $left and $1 <= $right;
191 }
192  
193 # another simple case -- either/or
194 if ($p =~ /^(\d+)[.^](\d+)$/) {
195 return 1 if $1 == $left || $2 == $left || $1 == $right || $2 == $right;
196 }
197  
198 # next simplest case -- a range
199 if ($p =~ /^<?(\d+)\.\.>?(\d+)$/) {
200 ($2,$1) = ($1,$2) if $1 > $2;
201 return 1 if ($left >= $1 and $left <= $2) or ($right >= $1 and $left <= $2);
202 }
203
204 # complex case, a join(), order() or group()
205 # not sure this is handled correctly in all the crazy combos
206 if ($p =~ /^(?:join|order|group|complement)/) {
207  
208 $p =~ s/\((\d)+\.\d+\)\.\./$1../g; # (1.10).. => 1..
209 $p =~ s/\.\.\(\d+\.(\d+)\)/..$1/g; # ..(1.10) => ..10
210  
211 my @ranges = $p =~ /[(),](<?\d+\.\.>?\d+)/g;
212 foreach (@ranges) {
213 next unless /<?(\d+)\.\.>?(\d+)/;
214 ($2,$1) = ($1,$2) if $1 > $2;
215 return 1 if ($left >= $1 and $left <= $2) or ($right >= $1 and $left <= $2);
216 }
217 }
218  
219 return;
220 }
221  
222 1;
223  
224 __END__