File Coverage

blib/lib/Bio/Das/TypeHandler.pm
Criterion Covered Total %
statement 8 34 23.5
branch 1 16 6.2
condition 1 8 12.5
subroutine 3 6 50.0
pod 3 4 75.0
total 16 68 23.5


line stmt bran cond sub pod time code
1             package Bio::Das::TypeHandler;
2 1     1   7 use strict;
  1         2  
  1         681  
3              
4             =head1 NAME
5              
6             Bio::Das::TypeHandler -- Utilities for handling types
7              
8             =head1 SYNOPSIS
9              
10             This is to be replaced by ontology-based types very soon.
11              
12             =cut
13              
14             =head1 METHODS
15              
16             =head2 new
17              
18             Title : new
19             Usage : $typehandle = Bio::Das::TypeHandler->new;
20             Function: create new typehandler
21             Returns : a typehandler
22             Args : a verbose/debug flag (optional)
23              
24             =cut
25              
26             sub new {
27 2     2 1 4 my $class = shift;
28 2         3 my $verbose = shift;
29 2         9 return bless {verbose=>$verbose},$class;
30             }
31              
32             sub debug {
33 0     0 0 0 my $self = shift;
34 0         0 my $d = $self->{verbose};
35 0 0       0 $self->{verbose} = shift if @_;
36 0         0 $d;
37             }
38              
39             =head2 parse_types
40              
41             Title : parse_types
42             Usage : $db->parse_types(@args)
43             Function: parses list of types
44             Returns : an array ref containing ['method','source'] pairs
45             Args : a list of types in 'method:source' form
46             Status : internal
47              
48             This method takes an array of type names in the format "method:source"
49             and returns an array reference of ['method','source'] pairs. It will
50             also accept a single argument consisting of an array reference with
51             the list of type names.
52              
53             =cut
54              
55             # turn feature types in the format "method:source" into a list of [method,source] refs
56             sub parse_types {
57 2     2 1 3 my $self = shift;
58 2 50 33     12 return [] if !@_ or !defined($_[0]);
59              
60 0 0         my @types = ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_;
  0            
61 0           my @type_list = map { [split(':',$_,2)] } @types;
  0            
62 0           return \@type_list;
63             }
64              
65             =head2 make_match_sub
66              
67             Title : make_match_sub
68             Usage : $db->make_match_sub($types)
69             Function: creates a subroutine used for filtering features
70             Returns : a code reference
71             Args : a list of parsed type names
72             Status : protected
73              
74             This method is used internally to generate a code subroutine that will
75             accept or reject a feature based on its method and source. It takes
76             an array of parsed type names in the format returned by parse_types(),
77             and generates an anonymous subroutine. The subroutine takes a single
78             Bio::DB::GFF::Feature object and returns true if the feature matches
79             one of the desired feature types, and false otherwise.
80              
81             =cut
82              
83             sub make_match_sub {
84 0     0 1   my $self = shift;
85 0           my $types = shift;
86              
87 0 0 0 0     return sub { 1 } unless ref $types && @$types;
  0            
88              
89 0           my @expr;
90 0           for my $type (@$types) {
91 0           my ($method,$source) = @$type;
92 0   0       $method ||= '.*';
93 0 0         $source = $source ? ":$source" : "(?::.+)?";
94 0           push @expr,"${method}${source}";
95             }
96 0           my $expr = join '|',@expr;
97 0 0         return $self->{match_subs}{$expr} if $self->{match_subs}{$expr};
98              
99 0           my $sub =<<END;
100             sub {
101             my \$feature = shift or return;
102             return \$feature->type =~ /^($expr)\$/i;
103             }
104             END
105 0 0         warn "match sub: $sub\n" if $self->debug;
106 0           my $compiled_sub = eval $sub;
107 0 0         $self->throw($@) if $@;
108 0           return $self->{match_subs}{$expr} = $compiled_sub;
109             }
110              
111              
112             1;
113              
114             =head1 SEE ALSO
115              
116             L<Bio::Das>
117              
118             =head1 AUTHOR
119              
120             Lincoln Stein E<lt>lstein@cshl.orgE<gt>.
121              
122             Copyright (c) 2001 Cold Spring Harbor Laboratory.
123              
124             This library is free software; you can redistribute it and/or modify
125             it under the same terms as Perl itself.
126              
127             =cut
128