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