comparison variant_effect_predictor/Bio/SearchIO/FastHitEventBuilder.pm @ 0:1f6dce3d34e0

Uploaded
author mahtabm
date Thu, 11 Apr 2013 02:01:53 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1f6dce3d34e0
1 # $Id: FastHitEventBuilder.pm,v 1.6 2002/12/05 13:46:35 heikki Exp $
2 #
3 # BioPerl module for Bio::SearchIO::FastHitEventBuilder
4 #
5 # Cared for by Jason Stajich <jason@bioperl.org>
6 #
7 # Copyright Jason Stajich
8 #
9 # You may distribute this module under the same terms as perl itself
10
11 # POD documentation - main docs before the code
12
13 =head1 NAME
14
15 Bio::SearchIO::FastHitEventBuilder - Event Handler for SearchIO events.
16
17 =head1 SYNOPSIS
18
19 # Do not use this object directly, this object is part of the SearchIO
20 # event based parsing system.
21
22 # to use the FastHitEventBuilder do this
23
24 use Bio::SearchIO::FastHitEventBuilder;
25
26 my $searchio = new Bio::SearchIO(-format => $format, -file => $file);
27
28 $searchio->attach_EventHandler(new Bio::SearchIO::FastHitEventBuilder);
29
30 while( my $r = $searchio->next_result ) {
31 while( my $h = $r->next_hit ) {
32 # note that Hits will NOT have HSPs
33 }
34 }
35
36 =head1 DESCRIPTION
37
38 This object handles Search Events generated by the SearchIO classes
39 and build appropriate Bio::Search::* objects from them. This object
40 is intended for lightweight parsers which only want Hits and not deal
41 with the overhead of HSPs. It is a lot faster than the standard
42 parser event handler but of course you are getting less information
43 and less objects out.
44
45
46 =head1 FEEDBACK
47
48 =head2 Mailing Lists
49
50 User feedback is an integral part of the evolution of this and other
51 Bioperl modules. Send your comments and suggestions preferably to
52 the Bioperl mailing list. Your participation is much appreciated.
53
54 bioperl-l@bioperl.org - General discussion
55 http://bioperl.org/MailList.shtml - About the mailing lists
56
57 =head2 Reporting Bugs
58
59 Report bugs to the Bioperl bug tracking system to help us keep track
60 of the bugs and their resolution. Bug reports can be submitted via
61 email or the web:
62
63 bioperl-bugs@bioperl.org
64 http://bugzilla.bioperl.org/
65
66 =head1 AUTHOR - Jason Stajich
67
68 Email jason@bioperl.org
69
70 Describe contact details here
71
72 =head1 CONTRIBUTORS
73
74 Additional contributors names and emails here
75
76 =head1 APPENDIX
77
78 The rest of the documentation details each of the object methods.
79 Internal methods are usually preceded with a _
80
81 =cut
82
83
84 # Let the code begin...
85
86
87 package Bio::SearchIO::FastHitEventBuilder;
88 use vars qw(@ISA %KNOWNEVENTS);
89 use strict;
90
91 use Bio::Root::Root;
92 use Bio::SearchIO::EventHandlerI;
93 use Bio::Search::HSP::HSPFactory;
94 use Bio::Search::Hit::HitFactory;
95 use Bio::Search::Result::ResultFactory;
96
97 @ISA = qw(Bio::Root::Root Bio::SearchIO::EventHandlerI);
98
99 =head2 new
100
101 Title : new
102 Usage : my $obj = new Bio::SearchIO::FastHitEventBuilder();
103 Function: Builds a new Bio::SearchIO::FastHitEventBuilder object
104 Returns : Bio::SearchIO::FastHitEventBuilder
105 Args : -hit_factory => Bio::Factory::ObjectFactoryI
106 -result_factory => Bio::Factory::ObjectFactoryI
107
108 See L<Bio::Factory::ObjectFactoryI> for more information
109
110 =cut
111
112 sub new {
113 my ($class,@args) = @_;
114 my $self = $class->SUPER::new(@args);
115 my ($hspF,$hitF,$resultF) = $self->_rearrange([qw(HIT_FACTORY
116 RESULT_FACTORY)],@args);
117 $self->register_factory('hit', $hitF || Bio::Search::Hit::HitFactory->new());
118 $self->register_factory('result', $resultF || Bio::Search::Result::ResultFactory->new());
119
120 return $self;
121 }
122
123 # new comes from the superclass
124
125 =head2 will_handle
126
127 Title : will_handle
128 Usage : if( $handler->will_handle($event_type) ) { ... }
129 Function: Tests if this event builder knows how to process a specific event
130 Returns : boolean
131 Args : event type name
132
133
134 =cut
135
136 sub will_handle{
137 my ($self,$type) = @_;
138 # these are the events we recognize
139 return ( $type eq 'hit' || $type eq 'result' );
140 }
141
142 =head2 SAX methods
143
144 =cut
145
146 =head2 start_result
147
148 Title : start_result
149 Usage : $handler->start_result($resulttype)
150 Function: Begins a result event cycle
151 Returns : none
152 Args : Type of Report
153
154 =cut
155
156 sub start_result {
157 my ($self,$type) = @_;
158 $self->{'_resulttype'} = $type;
159 $self->{'_hits'} = [];
160 return;
161 }
162
163 =head2 end_result
164
165 Title : end_result
166 Usage : my @results = $parser->end_result
167 Function: Finishes a result handler cycle Returns : A Bio::Search::Result::ResultI
168 Args : none
169
170 =cut
171
172 sub end_result {
173 my ($self,$type,$data) = @_;
174 if( defined $data->{'runid'} &&
175 $data->{'runid'} !~ /^\s+$/ ) {
176
177 if( $data->{'runid'} !~ /^lcl\|/) {
178 $data->{"RESULT-query_name"}= $data->{'runid'};
179 } else {
180 ($data->{"RESULT-query_name"},$data->{"RESULT-query_description"}) = split(/\s+/,$data->{"RESULT-query_description"},2);
181 }
182
183 if( my @a = split(/\|/,$data->{'RESULT-query_name'}) ) {
184 my $acc = pop @a ; # this is for accession |1234|gb|AAABB1.1|AAABB1
185 # this is for |123|gb|ABC1.1|
186 $acc = pop @a if( ! defined $acc || $acc =~ /^\s+$/);
187 $data->{"RESULT-query_accession"}= $acc;
188 }
189 delete $data->{'runid'};
190 }
191 my %args = map { my $v = $data->{$_}; s/RESULT//; ($_ => $v); }
192 grep { /^RESULT/ } keys %{$data};
193
194 $args{'-algorithm'} = uc( $args{'-algorithm_name'} || $type);
195 $args{'-hits'} = $self->{'_hits'};
196 my $result = $self->factory('result')->create(%args);
197 $self->{'_hits'} = [];
198 return $result;
199 }
200
201 =head2 start_hit
202
203 Title : start_hit
204 Usage : $handler->start_hit()
205 Function: Starts a Hit event cycle
206 Returns : none
207 Args : type of event and associated hashref
208
209
210 =cut
211
212 sub start_hit{
213 my ($self,$type) = @_;
214 return;
215 }
216
217
218 =head2 end_hit
219
220 Title : end_hit
221 Usage : $handler->end_hit()
222 Function: Ends a Hit event cycle
223 Returns : Bio::Search::Hit::HitI object
224 Args : type of event and associated hashref
225
226
227 =cut
228
229 sub end_hit{
230 my ($self,$type,$data) = @_;
231 my %args = map { my $v = $data->{$_}; s/HIT//; ($_ => $v); } grep { /^HIT/ } keys %{$data};
232 $args{'-algorithm'} = uc( $args{'-algorithm_name'} || $type);
233 $args{'-query_len'} = $data->{'RESULT-query_length'};
234 my ($hitrank) = scalar @{$self->{'_hits'}} + 1;
235 $args{'-rank'} = $hitrank;
236 my $hit = $self->factory('hit')->create(%args);
237 push @{$self->{'_hits'}}, $hit;
238 $self->{'_hsps'} = [];
239 return $hit;
240 }
241
242 =head2 Factory methods
243
244 =cut
245
246 =head2 register_factory
247
248 Title : register_factory
249 Usage : $handler->register_factory('TYPE',$factory);
250 Function: Register a specific factory for a object type class
251 Returns : none
252 Args : string representing the class and
253 Bio::Factory::ObjectFactoryI
254
255 See L<Bio::Factory::ObjectFactoryI> for more information
256
257 =cut
258
259 sub register_factory{
260 my ($self, $type,$f) = @_;
261 if( ! defined $f || ! ref($f) ||
262 ! $f->isa('Bio::Factory::ObjectFactoryI') ) {
263 $self->throw("Cannot set factory to value $f".ref($f)."\n");
264 }
265 $self->{'_factories'}->{lc($type)} = $f;
266 }
267
268
269 =head2 factory
270
271 Title : factory
272 Usage : my $f = $handler->factory('TYPE');
273 Function: Retrieves the associated factory for requested 'TYPE'
274 Returns : a Bio::Factory::ObjectFactoryI or undef if none registered
275 Args : name of factory class to retrieve
276
277 See L<Bio::Factory::ObjectFactoryI> for more information
278
279 =cut
280
281 sub factory{
282 my ($self,$type) = @_;
283 return $self->{'_factories'}->{lc($type)} || $self->throw("No factory registered for $type");
284 }
285
286 1;