blib/lib/Bio/DB/SeqFeature/Store/Alias.pm untested

filter: Statementstmt 0.0% 0 / 204 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 98 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 429 CC 34 SCAR 33.2
CC
34
Cov
0%
_parse_metadb
90.0
next_seq
56.0
_filter_ids_by_attribute
42.0
CRAP 27.6
1 package Bio::DB::SeqFeature::Store::Alias;
2  
3 use strict;
4 use Carp 'croak';
5 use IO::File;
6 use Bio::DB::SeqFeature::Store;
7  
8 =head1 SYNOPSIS
9  
10 # override feature type, method, source and attribute fields
11 # with the contents of a simple text index file
12  
13 use Bio::DB::SeqFeature::Store::Alias;
14 use Bio::DB::SeqFeature::Store;
15 my $db = Bio::DB::SeqFeature::Store->new(-adaptor=>'DBI::mysql',
16 -dsn =>'testdb');
17 my $meta = Bio::DB::SeqFeature::Store::Alias->new(-store => $db,
18 -index => '/usr/local/testdb/meta.index',
19 -default_type => 'example_feature',
20 );
21 my @features = $meta->get_seq_stream(-seq_id => 'I',
22 -attributes => {foo => 'bar'});
23
24 meta.index has the following structure
25  
26 [feature_name_1]
27 :dbid = f101
28 :selected = 1
29 display_name = My First Feature
30 type = some_type1
31 method = my_method1
32 source = my_source1
33 some_attribute = value1
34 another_attribute = value2
35  
36 [feature_name_2]
37 :dbid = f102
38 :selected = 1
39 display_name = My Second Feature
40 type = some_type2
41 method = my_method2
42 source = my_source2
43 some_attribute = value3
44 another_attribute = value4
45  
46 [feature_name_3]
47 :dbid = f103
48 type = some_type2
49 method = my_method2
50 source = my_source2
51 some_attribute = value5
52 another_attribute = value6
53  
54 =head1 DESCRIPTION
55  
56 This module can be used as a wrapper around any BioPerl database that
57 supports the get_feature_by_name() method. It allows you to
58 dynamically override the type, source, method and display name of each
59 feature, as well as to assign new metadata attributes to each one.
60  
61 It is used by GBrowse to create subtracks for tracks that have a small
62 number of named features, primarily wiggle tracks.
63  
64 =head2 Metadata Format
65  
66 The wrapper is associated with a text file that contains metadata
67 about each feature. You can use any name for the text file, but it
68 must follow this format:
69  
70 [feature_name_1]
71 :dbid = f101
72 :selected = 1
73 type = some_type1
74 method = my_method1
75 source = my_source1
76 some_attribute = value1
77 another_attribute = value2
78  
79 [feature_name_2]
80 :dbid = f102
81 :selected = 1
82 type = some_type2
83 method = my_method2
84 source = my_source2
85 some_attribute = value3
86 another_attribute = value4
87  
88 [feature_name_3]
89 :dbid = f103
90 type = some_type2
91 method = my_method2
92 source = my_source2
93 some_attribute = value5
94 another_attribute = value6
95  
96 Each [stanza] begins with the name of a feature as it is represented
97 in the underlying database. A call to the database's
98 get_feature_by_name() method with the [stanza] heading contents
99 (e.g. "feature_name_2") and a genomic position should return one and
100 only one feature.
101  
102 Below each [stanza] heading are a series of tag=value pairs. The
103 following tag names have special meaning:
104  
105 :dbid Unique identifier for the feature, used by GBrowse to process
106 clicks on the feature.
107 :selected If true, this subtrack is selected by default when the
108 containing track is turned on.
109 display_name Display name for the feature. If not present, will
110 default to the feature's native display name
111 (i.e. the one in the [stanza]).
112 type What is returned by calling the feature's type()
113 method.
114 method What is returned by calling the feature's method()
115 method.
116 source What is returned by calling the feature's source()
117 method.
118 score What is returned by calling the feature's score() method.
119  
120 Any other tags become sortable attributes which are displayed by the
121 GBrowse subtrack selection dialog box. For this to work properly, each
122 tag must be present in each stanza. Tags that are present in some
123 stanzas and not others are ignored.
124  
125 =head1 METHODS
126  
127 =cut
128  
129 our $AUTOLOAD;
130 my %CACHE;
131  
132 #sub AUTOLOAD {
133 # my($pack,$func_name) = $AUTOLOAD=~/(.+)::([^:]+)$/;
134 # return if $func_name eq 'DESTROY';
135 # my $self = shift or die;
136 # warn "AUTOLOAD: $self->$func_name";
137 # $self->{store}->$func_name(@_);
138 #}
139  
140 sub isa {
141 my $self = shift;
142 return ref $self ? $self->store->isa(@_)
143 : Bio::DB::SeqFeature::Store->isa(@_);
144 }
145  
146 =head2 $aliasdb = Bio::DB::SeqFeature::Store::Alias->new(-metadata=>$path,-store=>$db,-type=>$type)
147  
148 new() creates the Alias database. The arguments are:
149  
150 -metadata Full path to the metadata file.
151 -store Previously opened handle to the feature database
152 (e.g. Bio::SeqFeature::Store, Bio::DB::GFF,
153 Bio::DB::Chado)
154 -type Default type to use for features in this database.
155  
156 =cut
157  
158 sub new {
159 my $self = shift;
160 my %args = @_;
161 my $index = $args{-metadata} or croak __PACKAGE__.'->new(): -metadata argument required';
162 my $store = $args{-store} or croak __PACKAGE__.'->new(): -store argument required';
163 my $default_type = $args{-type}||$args{-default_type}||'';
164 return $CACHE{$index,$store,$default_type} ||=
165 bless {
166 store => $store,
167 index => $index,
168 default_type => $default_type
169 };
170 }
171  
172 =head2 $type = $aliasdb->default_type([$type])
173  
174 Gets or sets the default type.
175  
176 =cut
177  
178 sub feature_type { shift->default_type(@_) }
179  
180 sub default_type {
181 my $self = shift;
182 my $d = $self->{default_type};
183 $self->{default_type} = shift if @_;
184 return $d;
185 }
186  
187 =head2 $store = $aliasdb->store
188  
189 Gets the underlying feature database specified during new() (immutable).
190  
191 =cut
192  
193 sub store {shift->{store}}
194  
195 =head2 $index = $aliasdb->index
196  
197 Returns the path to the metadata description file.
198  
199 =cut
200  
201 sub index {shift->{index}}
202 sub parse_metadb {
203 my $self = shift;
204 return $self->{metadb} ||= $self->_parse_metadb;
205 }
206  
207 =head2 $meta = $aliasdb->metadata
208  
209 Return a hashref in which the keys are the feature IDs (or an
210 arbitrary numeric value if :dbid was not specified), and the values
211 are a hashref of attribute/value pairs.
212  
213 =cut
214  
215 sub metadata {
216 my $self = shift;
217  
218 $self->parse_metadb;
219 my $att = $self->{attributes};
220  
221 # obscure file names
222 my @indices = sort {
223 $att->{$a}{display_name} cmp $att->{$b}{display_name}
224 } keys %$att;
225 my @values = @{$att}{@indices};
226 my @ids = map {$att->{$indices[$_]}{':dbid'}||($_+1)} (0..@indices-1);
227  
228 my %result;
229 @result{@ids} = @values;
230  
231 return \%result;
232 }
233  
234 =head2 $f = $aliasdb->get_feature($name)
235  
236 Calls the underlying database's get_feature_by_name() method and
237 returns a list of response.
238  
239 =cut
240  
241 sub get_feature {
242 my $self = shift;
243 my $name = shift;
244 return ($self->store->get_feature_by_name($name))[0];
245 }
246  
247 =head2 @f = $aliasdb->features(@args)
248  
249 Same as calling the underlying database's features() method, except
250 that the values you specified in the metadata file for display_name,
251 type, method, source, score and other attributes will override the
252 original values.
253  
254 =cut
255  
256 sub features {
257 my $self = shift;
258 my @args = @_;
259 my %options = $args[0]=~/^-/ ? @args : (-type=>$_[0]);
260  
261 my $iterator = $self->get_seq_stream(@args);
262 return $iterator if $options{-iterator};
263
264 my @result;
265 while (my $f = $iterator->next_seq) {
266 push @result,$f;
267 }
268  
269 return @result;
270 }
271  
272 =head2 $segment = $aliasdb->segment(@args)
273  
274 Creates a segment object (see Bio::DB::SeqFeature::Store->segment()),
275 which records a genomic range for use in further query refinement:
276  
277 $chr1 = $aliasdb->segment(-seqid=>'chr1');
278 @f = $chr1->features;
279  
280 =cut
281  
282  
283 sub segment {
284 my $self = shift;
285 my ($seqid,$start,$end) = @_;
286  
287 if ($_[0] =~ /^-/) {
288 my %args = @_;
289 $seqid = $args{-seq_id} || $args{-name};
290 $start = $args{-start};
291 $end = $args{-stop} || $args{-end};
292 } else {
293 ($seqid,$start,$end) = @_;
294 }
295  
296 return Bio::DB::SeqFeature::Store::Alias::Segment->new(-aliasdb => $self,
297 -seq_id => $seqid,
298 -start => $start,
299 -end => $end);
300 }
301  
302 =head2 $iterator = $aliasdb->get_seq_stream(@args)
303  
304 This method is similar to the underlying database's get_seq_stream()
305 method, except that it wraps each feature. Example:
306  
307 $iterator = $aliasdb->get_seq_stream(-type=>'some_type2');
308 while (my $seq = $iterator->next_seq) {
309 # do something with the seq object.
310 }
311  
312 =cut
313  
314  
315 sub get_seq_stream {
316 my $self = shift;
317 my %options;
318  
319 if (@_ && $_[0] !~ /^-/) {
320 %options = (-type => $_[0]);
321 } else {
322 %options = @_;
323 }
324 $options{-type} ||= $options{-types};
325 $self->parse_metadb; # load metadata
326  
327 my @ids = keys %{$self->{features}};
328 @ids = $self->_filter_ids_by_type($options{-type}, \@ids) if $options{-type};
329 @ids = $self->_filter_ids_by_attribute($options{-attributes},\@ids) if $options{-attributes};
330 @ids = $self->_filter_ids_by_name($options{-name}, \@ids) if $options{-name};
331  
332 my %search_opts;
333 $search_opts{$_} = $options{$_} foreach qw(-seq_id -start -end);
334  
335 return Bio::DB::SeqFeature::Store::Alias::Iterator->new($self,\@ids,\%search_opts);
336 }
337  
338  
339  
340 =head2 @f = $aliasdb->get_features_by_location(@args)
341  
342 This method is similar to the underlying database's get_features_by_location()
343 method.
344  
345 $iterator = $aliasdb->get_seq_stream(-type=>'some_type2');
346 while (my $seq = $iterator->next_seq) {
347 # do something with the seq object.
348 }
349  
350 =cut
351  
352 sub get_features_by_location {
353 my $self = shift;
354 my ($seqid,$start,$end) = @_;
355 return $self->features(-seq_id=> $seqid,
356 -start => $start,
357 -end => $end);
358 }
359  
360 =head2 @f = $aliasdb->get_features_by_name(@args)
361  
362 This method is similar to the underlying database's get_features_by_location()
363 method.
364  
365 =cut
366  
367 sub get_features_by_name {
368 my $self = shift;
369 my $name = shift;
370 return $self->features(-name => $name);
371 }
372  
373 sub get_feature_by_name { shift->get_features_by_name(@_) }
374  
375 =head2 @f = $aliasdb->get_features_by_name(@args)
376  
377 This method is similar to the underlying database's get_features_by_location()
378 method.
379  
380 =cut
381  
382 =head2 @f = $aliasdb->get_features_by_alias(@args);
383  
384 This method is similar to the underlying database's get_features_by_alias()
385 method.
386  
387 =cut
388  
389 sub get_features_by_alias {shift->get_features_by_attribute(@_) }
390  
391 =head2 @f = $aliasdb->get_features_by_attribute(@args);
392  
393 This method is similar to the underlying database's get_features_by_attribute()
394 method.
395  
396 =cut
397  
398 sub get_features_by_attribute {
399 my $self = shift;
400 my $att = shift;
401 $self->features(-attributes=>$att);
402 }
403 sub _filter_ids_by_type {
404 my $self = shift;
405 my ($type,$ids) = @_;
406  
407 my %ids;
408 my @types = ref $type ? @$type : $type;
409  
410 my %ok_types = map {lc $_=>1} @types;
411 ID:
412 for my $id (@$ids) {
413 my $att = $self->{attributes}{$id};
414 my $type = lc ($att->{type} || $att->{method} || $att->{primary_tag} || $self->default_type);
415 my ($method,$source) = split(':',$type||'');
416 $method ||= $att->{method};
417 $source ||= $att->{source};
418 {
419 no warnings;
420 my $type_extended = lc "$method:$source" if $method;
421 next ID unless $ok_types{$method} || $ok_types{$type_extended};
422 }
423 $ids{$id}++;
424 }
425  
426 return keys %ids;
427 }
428  
429 sub _filter_ids_by_name {
430 my $self = shift;
431 my ($name,$ids) = @_;
432 my $atts = $self->{attributes};
433 my @result = grep {($atts->{$_}{display_name} || $atts->{$_}{name}) eq $name} @$ids;
434 return @result;
435 }
436  
437 sub _filter_ids_by_attribute {
438 my $self = shift;
439 my ($attributes,$ids) = @_;
440  
441 my @result;
442 my %ids = map {$_=>1} @$ids;
443 for my $att_name (keys %$attributes) {
444 my @search_terms = ref($attributes->{$att_name}) && ref($attributes->{$att_name}) eq 'ARRAY'
445 ? @{$attributes->{$att_name}} : $attributes->{$att_name};
446 for my $id (keys %ids) {
447 my $ok;
448
449 for my $v (@search_terms) {
450 my $att = $self->{attributes}{$id} or next;
451 my $val = $att->{lc $att_name} or next;
452 if (my $regexp = $self->glob_match($v)) {
453 $ok++ if $val =~ /$regexp/i;
454 } else {
455 $ok++ if lc $val eq lc $v;
456 }
457 }
458 delete $ids{$id} unless $ok;
459 }
460 }
461 return keys %ids;
462 }
463  
464 sub glob_match {
465 my $self = shift;
466 my $term = shift;
467 return unless $term =~ /(?:^|[^\\])[*?]/;
468 $term =~ s/(^|[^\\])([+\[\]^{}\$|\(\).])/$1\\$2/g;
469 $term =~ s/(^|[^\\])\*/$1.*/g;
470 $term =~ s/(^|[^\\])\?/$1./g;
471 return $term;
472 }
473  
474 sub _parse_metadb {
475 my $self = shift;
476 my $file = $self->index;
477  
478 my $f;
479 if ($file =~ /^(ftp|http):/i) {
480 eval "require LWP::UserAgent; 1"
481 or die "LWP::UserAgent module is required for remote metadata indexes"
482 unless LWP::UserAgent->can('new');
483 my $ua = LWP::UserAgent->new;
484 my $r = $ua->get($file);
485 die "Couldn't read $file: ",$r->status_line unless $r->is_success;
486 eval "require IO::String; 1"
487 or die "IO::String module is required for remote directories"
488 unless IO::String->can('new');
489 $f = IO::String->new($r->decoded_content);
490 }
491 else {
492 $f = IO::File->new($file) or die "$file: $!";
493 }
494 my ($current_feature,%features);
495  
496 while (<$f>) {
497 chomp;
498 s/\s+$//; # strip whitespace at ends of lines
499 # strip right-column comments unless they look like colors or html fragments
500 s/\s*\#.*$// unless /\#[0-9a-f]{6,8}\s*$/i || /\w+\#\w+/ || /\w+\"*\s*\#\d+$/;
501 if (/^\[([^\]]+)\]/) { # beginning of a configuration section
502 $current_feature = $1;
503 }
504  
505 elsif ($current_feature && /^([\w: -]+?)\s*=\s*(.*)/) { # key value pair
506 my $tag = lc $1;
507 my $value = defined $2 ? $2 : '';
508 $features{$current_feature}{$tag}=$value;
509 }
510 }
511  
512 for my $f (keys %features) {
513 my $attributes = $features{$f};
514 $self->set_feature_attributes($f,$attributes);
515 }
516  
517 return $self->{attributes};
518 }
519  
520 sub set_feature_attributes {
521 my $self = shift;
522 my ($feature,$attributes) = @_;
523 if (my $old = $self->{attributes}{$feature}) {
524 %$attributes = (%$old,%$attributes); # merge
525 }
526 $self->{features}{$feature} ||= undef;
527 $self->{attributes}{$feature} = $attributes;
528 }
529  
530 package Bio::DB::SeqFeature::Store::Alias::Iterator;
531  
532 sub new {
533 my $class = shift;
534 my ($db,$ids,$search_opts) = @_;
535 return bless {db => $db,
536 ids => $ids,
537 search_opts => $search_opts,
538 },ref $class || $class;
539 }
540  
541 sub next_seq {
542 my $self = shift;
543 my $db = $self->{db};
544 my $opts = $self->{search_opts};
545 while (1) {
546 return unless @{$self->{ids}};
547 my $name = shift @{$self->{ids}};
548
549 my ($next) = $db->store->get_features_by_alias($name) or next;
550 if (my $att = $db->{attributes}{$name}) {
551 my ($method,$source) = split(':',$att->{type}||'');
552 $next->primary_tag($method || $att->{primary_tag}) if $method || $att->{primary_tag};
553 $next->source_tag ($source || $att->{source} ) if $source || $att->{source};
554 for my $key (keys %$att) {
555 if ($key =~ /(display_)?name/) {
556 $next->name($att->{$key});
557 }
558 else {
559 $next->add_tag_value($key=>$att->{$key})
560 unless $key =~ /^(display_name|name|type|method|source)/;
561 }
562 }
563 }
564 return $next;
565 }
566 }
567  
568 package Bio::DB::SeqFeature::Store::Alias::Segment;
569  
570 sub new {
571 my $self = shift;
572 my %args = @_;
573 return bless {
574 seq_id => $args{-seq_id},
575 start => $args{-start},
576 end => $args{-end},
577 aliasdb=> $args{-aliasdb},
578 },ref $self || $self;
579 }
580  
581 sub features {
582 my $self = shift;
583 return $self->{aliasdb}->features(-seq_id => $self->{seq_id},
584 -start => $self->{start},
585 -end => $self->{end},
586 -type => $_[0]);
587 }
588  
589 sub get_seq_stream {
590 my $self = shift;
591 return $self->{aliasdb}->get_seq_stream(-seq_id => $self->{seq_id},
592 -start => $self->{start},
593 -end => $self->{end},
594 -type => $_[0]);
595 }
596  
597 =head1 BUGS
598  
599 Please report them.
600  
601 =head1 SEE ALSO
602  
603 L<Bio::SeqFeature::Store>,
604 L<Bio::DB::GFF>,
605 L<Bio::SeqI>,
606 L<Bio::SeqFeatureI>,
607 L<Bio::Das>,
608 L<Bio::Graphics::Browser2>
609  
610 =head1 AUTHOR
611  
612 Copyright (c) 2012 Ontario Institute for Cancer Research
613  
614 This library is free software; you can redistribute it and/or modify
615 it under the same terms as Perl itself. See DISCLAIMER.txt for
616 disclaimers of warranty.
617  
618 =cut
619  
620 1;
621