GFF/Adaptor/biofetch.pm

filter: Statementstmt 15.1% 21 / 139 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 37.5% 6 / 16 Podpod 42.8% 3 / 7 totaltotal 10.2% 30 / 292 CC 58 SCAR 62.6
CC
58
Cov
8%
_load_embl
1122.0
segment
72.0
new
42.0
CRAP 523.4
↑ Summary GFF/Aggregator/clone.pm »
1 package Bio::DB::GFF::Adaptor::biofetch;
2 1 $Bio::DB::GFF::Adaptor::biofetch::VERSION = '1.7.4';
3 =head1 NAME
4  
5 Bio::DB::GFF::Adaptor::biofetch -- Cache BioFetch objects in a Bio::DB::GFF database
6  
7 =head1 SYNOPSIS
8  
9 Proof of principle. Not for production use.
10  
11 =head1 DESCRIPTION
12  
13 This adaptor is a proof-of-principle. It is used to fetch BioFetch
14 sequences into a Bio::DB::GFF database (currently uses a hard-coded
15 EMBL database) as needed. This allows the Generic Genome Browser to
16 be used as a Genbank/EMBL browser.
17  
18 =head1 AUTHOR
19  
20 Lincoln Stein E<lt>lstein@cshl.orgE<gt>.
21  
22 Copyright 2002 Cold Spring Harbor Laboratory.
23  
24 This library is free software; you can redistribute it and/or modify
25 it under the same terms as Perl itself.
26  
27 =cut
28  
29 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Bio::DB::GFF::Util::Rearrange; # for rearrange()
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Bio::DB::BioFetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use Bio::SeqIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 ▶use vars qw(%preferred_tags);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # THIS IS WRONG: biofetch should delegate to an underlying
37 # database adaptor, and not inherit from one.
38 1 ▶use base qw(Bio::DB::GFF::Adaptor::dbi::mysql);
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # priority for choosing names of CDS tags, higher is higher priority
41 1 %preferred_tags = (
42 strain => 10,
43 organism => 20,
44 protein_id => 40,
45 locus_tag => 50,
46 locus => 60,
47 gene => 70,
48 standard_name => 80,
49 );
50  
51 =head2 new
52  
53 Title : new
54 Usage : $db = Bio::DB::GFF->new(-adaptor=>'biofetch',@args)
55 Function: create a new adaptor
56 Returns : a Bio::DB::GFF object
57 Args : -adaptor : required. Which adaptor to use; biofetch for mysql, biofetch_oracle for Oracle
58 -preferred_tags : optional. A hash of {classname => weight,...}
59 used to determine the class and name of the feature
60 when a choice of possible feature classes is available
61 (e.g. a feature has both a 'gene' and a 'locus' tag).
62 Common defaults are provided that work well for eukaryotic
63 features (but not well for viral/prokaryotic)
64 see below for additional arguments.
65 Status : Public
66  
67 This is the constructor for the adaptor. It is called automatically
68 by Bio::DB::GFF-E<gt>new. In addition to arguments that are common among
69 all adaptors, the following class-specific arguments are recgonized:
70  
71 Argument Description
72 -------- -----------
73  
74 -dsn the DBI data source, e.g. 'dbi:mysql:ens0040'
75  
76 -user username for authentication
77  
78 -pass the password for authentication
79  
80 -proxy [['http','ftp'],'http://proxy:8080']
81  
82 -source source to use for loaded features ('EMBL')
83  
84 -dsn,-user and -pass indicate the local database to cache results in,
85 and as are per Bio::DB::GFF::Adaptor::dbi. The -proxy argument allows
86 you to set the biofetch web proxy, and uses the same syntax described
87 for the proxy() method of L<Bio::DB::WebDBSeqI>, except that the
88 argument must be passed as an array reference.
89  
90 =cut
91  
92 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
93 0 my $class = shift;
94 0 my $self = $class->SUPER::new(@_);
95 0 my ($preferred,$proxy,$source) = rearrange(['PREFERRED_TAGS','PROXY','SOURCE'],@_);
96  
97 # if the caller sent their own preferences, then use these, otherwise use defaults.
98 0 ▶ $self->_preferred_tags($preferred ? $preferred : \%preferred_tags);
Branch: 1 branch
BranchTrueFalseTotal
$preferred ? : 0 0 0
99 0 ▶ $self->_source($source || 'EMBL');
Condition0%0/2
$source || 'EMBL'
l!l
00
MC/DC0%0/1
$source || 'EMBL'
$source
Truth table0%0/2
A
A= $source
Aresult
11
00
100  
101 0 ▶ if ($proxy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proxy) 0 0 0
102 0 ▶ my @args = ref($proxy) ? @$proxy : eval $proxy;
Branch: 1 branch
BranchTrueFalseTotal
ref $proxy ? : 0 0 0
103 0 ▶ $self->{_proxy} = \@args if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 0 0
104 }
105 0 $self;
106 }
107  
108 0 ▶sub segment {
Subroutine segment: not called CC=8 SCAR=42.8
Pod documented
109 0 my $self = shift;
110 0 my @segments = $self->SUPER::segment(@_);
111  
112 0 ▶ if (!@segments) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@segments) 0 0 0
113 0 my $refclass = $self->refclass;
114  
115 0 my %args = $self->setup_segment_args(@_);
116 0 ▶ if ($args{-class} && $args{-class} =~ /$refclass/oi) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'-class'} and $args{'-class'} =~ /$refclass/io) { } 0 0 0
elsif ($args{'-class'} and $args{'-class'} =~ /refseq|swall|embl/i) { } 0 0 0
Condition0%0/3
$args{"-class"} and $args{"-class"} =~ /$refclass/io
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"-class"} and $args{"-class"} =~ /$refclass/io
$args{"-class"} $args{"-class"} =~ /$refclass/io
Truth table0%0/3
A and B
A= $args{"-class"}B= $args{"-class"} =~ /$refclass/io
ABresult
0X0
100
111
117 0 ▶ return unless $self->load_from_embl('embl'=>$args{-name});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->load_from_embl("embl", $args{"-name"}) 0 0 0
118 0 @segments = $self->SUPER::segment(@_);
119 ▶ } elsif ($args{-class} && $args{-class} =~ /refseq|swall|embl/i) { #hack to get refseq names
Condition0%0/3
$args{"-class"} and $args{"-class"} =~ /refseq|swall|embl/i
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"-class"} and $args{"-class"} =~ /refseq|swall|embl/i
$args{"-class"} $args{"-class"} =~ /refseq|swall|embl/i
Truth table0%0/3
A and B
A= $args{"-class"}B= $args{"-class"} =~ /refseq|swall|embl/i
ABresult
0X0
100
111
120 0 ▶ return unless $self->load_from_embl(lc($args{-class})=>$args{-name});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->load_from_embl(lc $args{"-class"}, $args{"-name"}) 0 0 0
121 0 $args{-class} = $self->refclass;
122 0 @segments = $self->SUPER::segment(%args);
123 }
124 }
125  
126 0 $self->_multiple_return_args(@segments);
127 }
128  
129 # default is to return 'Sequence' as the class of all references
130 0 ▶sub refclass {
Subroutine refclass: not called CC=1 SCAR=6.9
Pod documented
131 0 my $self = shift;
132 0 my $refname = shift;
133 0 'Sequence';
134 }
135  
136 0 ▶sub load_from_embl {
Subroutine load_from_embl: not called CC=5 SCAR=34.0
Pod undocumented
137 0 my $self = shift;
138 0 my $db = shift;
139 0 ▶ my $acc = shift or $self->throw('Must provide an accession ID');
Branch: 1 branch
BranchTrueFalseTotal
my $acc = shift() or $self->throw("Must provide an accession ID") 0 0 0
140  
141 0 my $biofetch;
142 0 ▶ if ($self->{_biofetch}{$db}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_biofetch'}{$db}) { } 0 0 0
143 0 $biofetch = $self->{_biofetch}{$db};
144 } else {
145 0 $biofetch = $self->{_biofetch}{$db} = Bio::DB::BioFetch->new(-db=>$db);
146 0 $biofetch->retrieval_type('tempfile');
147 0 ▶ $biofetch->proxy(@{$self->{_proxy}}) if $self->{_proxy};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_proxy"} 0 0 0
148 }
149  
150 0 ▶ my $seq = eval {$biofetch->get_Seq_by_id($acc)} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $seq = eval { do { $biofetch->get_Seq_by_id($acc) } } or return 0 0 0
151 0 $self->_load_embl($acc,$seq);
152 0 1;
153 }
154  
155 0 ▶sub load_from_file {
Subroutine load_from_file: not called CC=2 SCAR=17.9
Pod undocumented
156 0 my $self = shift;
157 0 my $file = shift;
158  
159 0 ▶ my $format = $file =~ /\.(gb|genbank|gbk)$/i ? 'genbank' : 'embl';
Branch: 1 branch
BranchTrueFalseTotal
$file =~ /\.(gb|genbank|gbk)$/i ? : 0 0 0
160  
161 0 my $seqio = Bio::SeqIO->new( '-format' => $format, -file => $file);
162 0 my $seq = $seqio->next_seq;
163  
164 0 $self->_load_embl($seq->accession,$seq);
165 0 1;
166 }
167  
168 0 ▶sub _load_embl {
Subroutine _load_embl: not called CC=33 SCAR=70.2
169 0 my $self = shift;
170 0 my $acc = shift;
171 0 my $seq = shift;
172 0 my $refclass = $self->refclass;
173 0 my $locus = $seq->id;
174 0 my $source = $self->_source;
175  
176 # begin loading
177 0 $self->setup_load();
178  
179 # first synthesize the entry for the top-level feature
180 0 my @aliases;
181 0 foreach ($seq->accession,$seq->get_secondary_accessions) {
182 0 ▶ next if lc($_) eq lc($acc);
Branch: 1 branch
BranchTrueFalseTotal
if lc $_ eq lc $acc 0 0 0
183 0 push @aliases,[Alias => $_];
184 }
185 $self->load_gff_line(
186 {
187 0 ref => $acc,
188 class => $refclass,
189 source => $source,
190 # method => 'origin',
191 method => 'region',
192 start => 1,
193 stop => $seq->length,
194 score => undef,
195 strand => '.',
196 phase => '.',
197 gclass => $self->refclass,
198 gname => $acc,
199 tstart => undef,
200 tstop => undef,
201 attributes => [[Note => $seq->desc],@aliases],
202 }
203 );
204 # now load each feature in turn
205 0 my ($transcript_version,$mRNA_version) = (0,0);
206 0 for my $feat ($seq->all_SeqFeatures) {
207 0 my $attributes = $self->get_attributes($feat);
208 0 my $name = $self->guess_name($attributes);
209  
210 0 my $location = $feat->location;
211 0 ▶ my @segments = map {[$_->start,$_->end,$_->seq_id]}
Branch: 1 branch
BranchTrueFalseTotal
$location->can('sub_Location') ? : 0 0 0
212 $location->can('sub_Location') ? $location->sub_Location : $location;
213  
214 # this changed CDS to coding, but that is the wrong thing to do, since
215 # CDS is in SOFA and coding is not
216 # my $type = $feat->primary_tag eq 'CDS' ? 'coding'
217 # : $feat->primary_tag;
218 0 my $type= $feat->primary_tag;
219 0 ▶ next if (lc($type) eq 'contig');
Branch: 1 branch
BranchTrueFalseTotal
if lc $type eq "contig" 0 0 0
220 # next if (lc($type) eq 'variation');
221  
222 0 ▶ if (lc($type) eq 'variation' and $feat->length == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $type eq 'variation' and $feat->length == 1) { } 0 0 0
elsif (lc $type eq 'variation') { } 0 0 0
Condition0%0/3
lc $type eq "variation" and $feat->length == 1
!ll&&!rl&&r
000
MC/DC0%0/2
lc $type eq "variation" and $feat->length == 1
lc $type eq "variation" $feat->length == 1
Truth table0%0/3
A and B
A= lc $type eq "variation"B= $feat->length == 1
ABresult
0X0
100
111
223 0 $type = 'SNP';
224 } elsif (lc($type) eq 'variation' ) {
225 0 $type = 'chromosome_variation';
226 }
227  
228 0 ▶ if ($type eq 'source') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "source") 0 0 0
229 0 $type = 'region';
230 }
231  
232 0 ▶ if ($type =~ /misc.*RNA/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /misc.*RNA/i) 0 0 0
233 0 $type = 'RNA';
234 }
235  
236 0 ▶ if ($type eq 'misc_feature' and $name->[1] =~ /similar/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'misc_feature' and $name->[1] =~ /similar/i) { } 0 0 0
elsif ($type eq 'misc_feature') { } 0 0 0
Condition0%0/3
$type eq "misc_feature" and $name->[1] =~ /similar/i
!ll&&!rl&&r
000
MC/DC0%0/2
$type eq "misc_feature" and $name->[1] =~ /similar/i
$type eq "misc_feature" $name->[1] =~ /similar/i
Truth table0%0/3
A and B
A= $type eq "misc_feature"B= $name->[1] =~ /similar/i
ABresult
0X0
100
111
237 0 $type = 'computed_feature_by_similarity';
238 } elsif ($type eq 'misc_feature') {
239 0 warn "skipping a misc_feature\n";
240 0 next;
241 }
242  
243 0 ▶ my $parttype = $feat->primary_tag eq 'mRNA' ? 'exon' : $feat->primary_tag;
Branch: 1 branch
BranchTrueFalseTotal
$feat->primary_tag eq 'mRNA' ? : 0 0 0
244  
245 0 ▶ if ($type eq 'gene') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'gene') { } 0 0 0
elsif ($type eq 'mRNA') { } 0 0 0
elsif ($type eq 'CDS') { } 0 0 0
246 0 $transcript_version = 0;
247 0 $mRNA_version = 0;
248 } elsif ($type eq 'mRNA') {
249 0 $name->[1] = sprintf("%s.t%02d",$name->[1],++$transcript_version);
250 } elsif ($type eq 'CDS') {
251 0 $name->[0] = 'mRNA';
252 0 $name->[1] = sprintf("%s.t%02d",$name->[1],$transcript_version);
253 }
254  
255 0 my $strand = $feat->strand;
256 0 ▶ my $str = defined $strand ?
Branch: 2 branches
BranchTrueFalseTotal
defined $strand ? : 0 0 0
$strand > 0 ? : 0 0 0
257 ($strand > 0 ? '+' : '-')
258 : '.';
259 0 ▶ $self->load_gff_line( {
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type ne "CDS" || $type eq "CDS" && @segments == 1 0 0 0
Condition0%0/13
$type and $type ne "CDS" || $type eq "CDS" && @segments == 1
!ll&&!rl&&r
000
$type ne "CDS" or $type eq "CDS" and @segments == 1
l!l&&r!l&&!r
000
$type eq "CDS" and @segments == 1
!ll&&!rl&&r
000
$feat->score || undef
l!l
00
$feat->frame || '.'
l!l
00
MC/DC0%0/4
$type and $type ne "CDS" || $type eq "CDS" && @segments == 1
$type $type ne "CDS" $type eq "CDS" @segments == 1
MC/DC0%0/1
$feat->score || undef
$feat->score
MC/DC0%0/1
$feat->frame || '.'
$feat->frame
Truth table0%0/5
A and B or C and D
A= $typeB= $type ne "CDS"C= $type eq "CDS"D= @segments == 1
ABCDresult
0XXX0
100X0
10100
11XX1
10111
Truth table0%0/2
A
A= $feat->score
Aresult
11
00
Truth table0%0/2
A
A= $feat->frame
Aresult
11
00
260 ref => $acc,
261 class => $refclass,
262 source => $source,
263 method => $type,
264 start => $location->start,
265 stop => $location->end,
266 score => $feat->score || undef,
267 strand => $str,
268 phase => $feat->frame || '.',
269 gclass => $name->[0],
270 gname => $name->[1],
271 tstart => undef,
272 tstop => undef,
273 attributes => $attributes,
274 }
275 ) if ($type &&
276 ($type ne 'CDS'||($type eq 'CDS'&&@segments==1) ) );
277  
278 0 @$attributes = ();
279  
280 0 ▶ next if @segments == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @segments == 1 0 0 0
281 0 for my $segment (@segments) {
282  
283 0 my $strand = $feat->strand;
284 0 ▶ my $str = defined $strand ?
Branch: 2 branches
BranchTrueFalseTotal
defined $strand ? : 0 0 0
$strand > 0 ? : 0 0 0
285 ($strand > 0 ? '+' : '-')
286 : '.';
287 0 ▶ $self->load_gff_line( {
Branch: 1 branch
BranchTrueFalseTotal
$segment->[2] eq $locus ? : 0 0 0
Condition0%0/4
$feat->score || undef
l!l
00
$feat->frame || '.'
l!l
00
MC/DC0%0/1
$feat->score || undef
$feat->score
MC/DC0%0/1
$feat->frame || '.'
$feat->frame
Truth table0%0/2
A
A= $feat->score
Aresult
11
00
Truth table0%0/2
A
A= $feat->frame
Aresult
11
00
288 ref => $segment->[2] eq $locus ? $acc : $segment->[2],
289 class => $refclass,
290 source => $source,
291 method => $parttype,
292 start => $segment->[0],
293 stop => $segment->[1],
294 score => $feat->score || undef,
295 strand => $str,
296 phase => $feat->frame || '.',
297 gclass => $name->[0],
298 gname => $name->[1],
299 tstart => undef,
300 tstop => undef,
301 attributes => $attributes,
302 }
303 );
304 }
305  
306 }
307  
308 # finish loading
309 0 $self->finish_load();
310  
311 # now load the DNA
312 0 $self->load_sequence_string($acc,$seq->seq);
313  
314 0 1;
315 }
316  
317 0 ▶sub get_attributes {
Subroutine get_attributes: not called CC=4 SCAR=30.0
Pod undocumented
318 0 my $self = shift;
319 0 my $seq = shift;
320  
321 0 ▶ my @tags = $seq->all_tags or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@tags) = $seq->all_tags or return 0 0 0
322 0 my @result;
323 0 foreach my $tag (@tags) {
324 0 foreach my $value ($seq->each_tag_value($tag)) {
325 0 push @result,[$tag=>$value];
326 }
327 }
328 0 \@result;
329 }
330  
331 0 ▶sub guess_name {
Subroutine guess_name: not called CC=4 SCAR=30.0
Pod undocumented
332 0 my $self = shift;
333 0 my $attributes = shift;
334 # remove this fix when Lincoln fixes it properly
335 0 ▶ return ["Misc" => "Misc"] unless ($attributes); # these are arbitrary, and possibly destructive defaults
Branch: 1 branch
BranchTrueFalseTotal
unless $attributes 0 0 0
336 0 ▶ my @ordered_attributes = sort {($self->_preferred_tags->{$a->[0]} || 0) <=> ($self->_preferred_tags->{$b->[0]} || 0)} @$attributes;
Condition0%0/4
$self->_preferred_tags->{$Bio::DB::GFF::Adaptor::biofetch::a->[0]} || 0
l!l
00
$self->_preferred_tags->{$Bio::DB::GFF::Adaptor::biofetch::b->[0]} || 0
l!l
00
MC/DC0%0/1
$self->_preferred_tags->{$Bio::DB::GFF::Adaptor::biofetch::a->[0]} || 0
$self->_preferred_tags->{$Bio::DB::GFF::Adaptor::biofetch::a->[0]}
MC/DC0%0/1
$self->_preferred_tags->{$Bio::DB::GFF::Adaptor::biofetch::b->[0]} || 0
$self->_preferred_tags->{$Bio::DB::GFF::Adaptor::biofetch::b->[0]}
Truth table0%0/2
A
A= $self->_preferred_tags->{$Bio::DB::GFF::Adaptor::biofetch::a->[0]}
Aresult
11
00
Truth table0%0/2
A
A= $self->_preferred_tags->{$Bio::DB::GFF::Adaptor::biofetch::b->[0]}
Aresult
11
00
337 0 my $best = pop @ordered_attributes;
338 0 @$attributes = @ordered_attributes;
339 0 return $best;
340 }
341  
342  
343 0 ▶sub _preferred_tags {
Subroutine _preferred_tags: not called CC=2 SCAR=17.9
344 0 my $self = shift;
345 0 ▶ $self->{preferred_tags} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
346 0 return $self->{preferred_tags};
347 }
348  
349 0 ▶sub _source {
Subroutine _source: not called CC=2 SCAR=17.9
350 0 my $self = shift;
351 0 ▶ $self->{source} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
352 0 $self->{source};
353 }
354  
355 1 1;
↑ Summary GFF/Aggregator/clone.pm »