1
# $Id: blast_pull.pm 11480 2007-06-14 14:16:21Z sendu $
3
# BioPerl module for Bio::SearchIO::blast_pull
5
# Cared for by Sendu Bala <bix@sendu.me.uk>
9
# You may distribute this module under the same terms as perl itself
11
# POD documentation - main docs before the code
15
Bio::SearchIO::blast_pull - A parser for BLAST output
19
# do not use this class directly it is available through Bio::SearchIO
21
my $in = Bio::SearchIO->new(-format => 'blast_pull',
22
-file => 't/data/new_blastn.txt');
23
while (my $result = $in->next_result) {
24
# this is a Bio::Search::Result::BlastPullResult object
25
print "Results for ", $result->query_name(), "\n";
26
while (my $hit = $result->next_hit) {
27
print $hit->name(), "\n";
28
while (my $hsp = $hit->next_hsp) {
29
print "length is ", $hsp->length(), "\n";
36
This object implements a pull-parser for BLAST output. It is fast since it
37
only does work on request (hence 'pull').
39
Currently only NCBI BLASTN and BLASTP are supported.
45
User feedback is an integral part of the evolution of this and other
46
Bioperl modules. Send your comments and suggestions preferably to
47
the Bioperl mailing list. Your participation is much appreciated.
49
bioperl-l@bioperl.org - General discussion
50
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
54
Report bugs to the Bioperl bug tracking system to help us keep track
55
of the bugs and their resolution. Bug reports can be submitted via the
58
http://bugzilla.open-bio.org/
60
=head1 AUTHOR - Sendu Bala
66
The rest of the documentation details each of the object methods.
67
Internal methods are usually preceded with a _
71
# Let the code begin...
73
package Bio::SearchIO::blast_pull;
76
use Bio::Search::Result::BlastPullResult;
78
use base qw(Bio::SearchIO Bio::PullParserI);
83
Usage : my $obj = Bio::SearchIO::blast_pull->new();
84
Function: Builds a new Bio::SearchIO::blast_pull object
85
Returns : Bio::SearchIO::blast_pull
86
Args : -fh/-file => BLAST output filename
87
-format => 'blast_pull'
88
-evalue => float or scientific notation number to be used
89
as an evalue cutoff for hits
90
-score => integer or scientific notation number to be used
91
as a score value cutoff for hits
92
-piped_behaviour => 'temp_file'|'memory'|'sequential_read'
94
-piped_behaviour defines what the parser should do if the input is
95
an unseekable filehandle (eg. piped input), see
96
Bio::PullParserI::chunk for details. Default is 'memory'.
101
my ($self, @args) = @_;
103
# don't do normal SearchIO initialization
105
my ($writer, $file, $fh, $piped_behaviour, $evalue, $score) =
106
$self->_rearrange([qw(WRITER
111
$self->writer($writer) if $writer;
113
$self->_fields( { ( header => undef,
115
algorithm_version => undef,
116
algorithm_reference => '',
117
database_name => undef,
118
database_letters => undef,
119
database_entries => undef,
120
next_result => undef,
121
evalue_cutoff => '[unset]',
122
score_cutoff => '[unset]' ) } );
124
$self->_fields->{evalue_cutoff} = $evalue if $evalue;
125
$self->_fields->{score_cutoff} = $score if $score;
127
$self->_dependencies( { ( algorithm => 'header',
128
algorithm_version => 'header',
129
database_name => 'header',
130
database_letters => 'header',
131
database_entries => 'header' ) } );
133
$self->chunk($file || $fh || $self->throw("-file or -fh must be supplied"),
134
-piped_behaviour => $piped_behaviour || 'memory');
137
sub _discover_header {
139
$self->_chunk_seek(0);
140
my $header = $self->_get_chunk_by_end("\nQuery=");
141
$self->{_after_header} = $self->_chunk_tell;
143
#*** won't catch all types? only support blastn/p now anyway
144
$header =~ /^(\S+) (\S+\s+\S+)/;
145
$self->_fields->{algorithm} = $1;
146
$self->_fields->{algorithm_version} = $2;
148
my ($database) = $header =~ /^Database: (.+)/sm;
151
# earlier versions put query before database?
152
my $header2 = $self->_get_chunk_by_end(".done\n");
153
($database) = $header2 =~ /^Database: (.+)/sm;
156
$database =~ s/\s+(\d\S+) sequences; (\d\S+) total letters.*//s;
159
$database =~ s/\n//g;
162
$self->_fields->{database_name} = $database;
163
$self->_fields->{database_entries} = $entries;
164
$self->_fields->{database_letters} = $letters;
166
$self->_fields->{header} = 1;
169
sub _discover_next_result {
171
return if $self->{_after_results};
172
my $type = $self->get_field('algorithm'); # also sets _after_header if not set
174
if ($type eq 'BLASTN' || $type eq 'BLASTP') {
175
unless ($self->_sequential) {
176
$self->_chunk_seek($self->{_end_of_previous_result} || $self->{_after_header});
178
my ($start, $end) = $self->_find_chunk_by_end("\nQuery=");
179
return if ($start == $end);
182
$start = $self->{_end_of_previous_result} || $self->{_after_header};
186
$self->_fields->{next_result} = Bio::Search::Result::BlastPullResult->new(-chunk => [($self->chunk, $start, $end)],
189
$self->{_end_of_previous_result} = $end;
192
#*** doesn't work for the last result, needs fixing - try getting the database end chunk on failure?...
193
$self->throw("sequential mode not yet implemented");
194
my $chunk = $self->_get_chunk_by_end("\nQuery=");
196
$self->_fields->{next_result} = Bio::Search::Result::BlastPullResult->new(-chunk => [$chunk],
201
$self->throw("Can only handle NCBI BLASTN and BLASTP right now");
208
Usage : my $hit = $searchio->next_result;
209
Function: Returns the next Result from a search
210
Returns : Bio::Search::Result::ResultI object
217
my $result = $self->get_field('next_result') || return;
219
undef $self->_fields->{next_result};
221
$self->{'_result_count'}++;
228
Usage : my $count = $searchio->result_count
229
Function: Returns the number of results we have processed.
237
return $self->{'_result_count'};
243
Usage : $searchio->rewind;
244
Function: Allow one to reset the Result iterator to the beginning, so that
245
next_result() will subsequently return the first result and so on.
247
NB: result objects are not cached, so you will get new result objects
248
each time you rewind. Also, note that result_count() counts the
249
number of times you have called next_result(), so will not be able
250
tell you how many results there were in the file if you use rewind().
259
delete $self->{_end_of_previous_result};