Bio::DB::TFBS namespace has been moved to its own distribution named after itself
[bioperl-live.git] / Bio / SearchIO / hmmer_pull.pm
blob812243094d8944549a514ac162e81eef6c697a6a
2 # BioPerl module for Bio::SearchIO::hmmer_pull
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Sendu Bala <bix@sendu.me.uk>
8 # Copyright Sendu Bala
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
14 =head1 NAME
16 Bio::SearchIO::hmmer_pull - A parser for HMMER output
18 =head1 SYNOPSIS
20 # do not use this class directly it is available through Bio::SearchIO
21 use Bio::SearchIO;
22 my $in = Bio::SearchIO->new(-format => 'hmmer_pull',
23 -file => 't/data/hmmpfam.bigout');
24 while (my $result = $in->next_result) {
25 # this is a Bio::Search::Result::HmmpfamResult object
26 print $result->query_name(), " for HMM ", $result->hmm_name(), "\n";
27 while (my $hit = $result->next_hit) {
28 print $hit->name(), "\n";
29 while (my $hsp = $hit->next_hsp) {
30 print "length is ", $hsp->length(), "\n";
35 =head1 DESCRIPTION
37 This object implements a pull-parser for HMMER output. It is fast since it
38 only does work on request (hence 'pull').
40 =head1 FEEDBACK
42 =head2 Mailing Lists
44 User feedback is an integral part of the evolution of this and other
45 Bioperl modules. Send your comments and suggestions preferably to
46 the Bioperl mailing list. Your participation is much appreciated.
48 bioperl-l@bioperl.org - General discussion
49 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
51 =head2 Support
53 Please direct usage questions or support issues to the mailing list:
55 I<bioperl-l@bioperl.org>
57 rather than to the module maintainer directly. Many experienced and
58 reponsive experts will be able look at the problem and quickly
59 address it. Please include a thorough description of the problem
60 with code and data examples if at all possible.
62 =head2 Reporting Bugs
64 Report bugs to the Bioperl bug tracking system to help us keep track
65 of the bugs and their resolution. Bug reports can be submitted via the
66 web:
68 https://github.com/bioperl/bioperl-live/issues
70 =head1 AUTHOR - Sendu Bala
72 Email bix@sendu.me.uk
74 =head1 APPENDIX
76 The rest of the documentation details each of the object methods.
77 Internal methods are usually preceded with a _
79 =cut
81 # Let the code begin...
83 package Bio::SearchIO::hmmer_pull;
85 use strict;
88 use base qw(Bio::SearchIO Bio::PullParserI);
90 =head2 new
92 Title : new
93 Usage : my $obj = Bio::SearchIO::hmmer_pull->new();
94 Function: Builds a new Bio::SearchIO::hmmer_pull object
95 Returns : Bio::SearchIO::hmmer_pull
96 Args : -fh/-file => HMMER output filename
97 -format => 'hmmer_pull'
98 -evalue => float or scientific notation number to be used
99 as an evalue cutoff for hits
100 -score => integer or scientific notation number to be used
101 as a score value cutoff for hits
102 -hsps => integer minimum number of hsps (domains) a hit must have
103 -piped_behaviour => 'temp_file'|'memory'|'sequential_read'
105 -piped_behaviour defines what the parser should do if the input is
106 an unseekable filehandle (eg. piped input), see
107 Bio::PullParserI::chunk for details. Default is 'sequential_read'.
109 =cut
111 sub _initialize {
112 my ($self, @args) = @_;
114 # don't do normal SearchIO initialization
116 my ($writer, $file, $fh, $piped_behaviour, $evalue, $score, $hsps) =
117 $self->_rearrange([qw(WRITER
118 FILE FH
119 PIPED_BEHAVIOUR
120 EVALUE
121 SCORE
122 HSPS)], @args);
123 $self->writer($writer) if $writer;
125 $self->_fields( { ( header => undef,
126 algorithm => undef,
127 algorithm_version => undef,
128 algorithm_reference => '',
129 hmm_file => undef,
130 hmm_name => undef,
131 sequence_file => undef,
132 sequence_database => undef,
133 database_name => undef,
134 database_letters => undef,
135 database_entries => undef,
136 next_result => undef,
137 evalue_cutoff => '[unset]',
138 score_cutoff => '[unset]',
139 hsps_cutoff => '[unset]' ) } );
141 $self->_fields->{evalue_cutoff} = $evalue if $evalue;
142 $self->_fields->{score_cutoff} = $score if $score;
143 $self->_fields->{hsps_cutoff} = $hsps if $hsps;
145 $self->_dependencies( { ( algorithm => 'header',
146 algorithm_version => 'header',
147 hmm_file => 'header',
148 hmm_name => 'header',
149 sequence_file => 'header',
150 sequence_database => 'header' ) } );
152 $self->chunk($file || $fh || $self->throw("-file or -fh must be supplied"),
153 -piped_behaviour => $piped_behaviour || 'sequential_read');
156 sub _discover_header {
157 my $self = shift;
158 $self->_chunk_seek(0);
159 my $header = $self->_get_chunk_by_nol(8);
160 $self->{_after_header} = $self->_chunk_tell;
162 my ($algo) = $header =~ /^(hmm\S+) - search/m;
163 $self->_fields->{algorithm} = uc $algo;
165 ($self->_fields->{algorithm_version}) = $header =~ /^HMMER\s+?(\S+)/m;
167 ($self->_fields->{hmm_file}) = $header =~ /^HMM file:\s.+?(\S+)$/m;
168 $self->_fields->{hmm_name} = $self->_fields->{hmm_file};
170 ($self->_fields->{sequence_file}) = $header =~ /^Sequence (?:file|database):\s.+?(\S+)$/m;
171 $self->_fields->{sequence_database} = $self->_fields->{sequence_file};
173 $self->_fields->{header} = 1;
176 sub _discover_database_name {
177 my $self = shift;
178 my $type = $self->get_field('algorithm');
180 if ($type eq 'HMMPFAM') {
181 $self->_fields->{database_name} = $self->get_field('hmm_file');
183 elsif ($type eq 'HMMSEARCH') {
184 $self->_fields->{database_name} = $self->get_field('sequence_file');
188 sub _discover_next_result {
189 my $self = shift;
190 my $type = $self->get_field('algorithm'); # also sets _after_header if not set
192 if ($type eq 'HMMPFAM') {
193 use Bio::Search::Result::HmmpfamResult;
195 unless ($self->_sequential) {
196 $self->_chunk_seek($self->{_end_of_previous_result} || $self->{_after_header});
198 my ($start, $end) = $self->_find_chunk_by_end("//\n");
199 return if $start == $end;
200 $self->_fields->{next_result} = Bio::Search::Result::HmmpfamResult->new(-chunk => [($self->chunk, $start, $end)],
201 -parent => $self);
203 $self->{_end_of_previous_result} = $end;
205 else {
206 # deliberatly don't cache these, which means rewind won't work;
207 # if we cached we may as well have used 'memory' option to
208 # -piped_behaviour
209 my $chunk = $self->_get_chunk_by_end("//\n");
210 $chunk || return;
211 $self->_fields->{next_result} = Bio::Search::Result::HmmpfamResult->new(-chunk => [$chunk],
212 -parent => $self);
215 elsif ($type eq 'HMMSEARCH') {
216 $self->throw("Can't handle hmmsearch yet\n");
218 else {
219 $self->throw("Unknown report type");
223 =head2 next_result
225 Title : next_result
226 Usage : my $hit = $searchio->next_result;
227 Function: Returns the next Result from a search
228 Returns : Bio::Search::Result::ResultI object
229 Args : none
231 =cut
233 sub next_result {
234 my $self = shift;
235 my $result = $self->get_field('next_result') || return;
237 undef $self->_fields->{next_result};
239 $self->{'_result_count'}++;
240 return $result;
243 =head2 result_count
245 Title : result_count
246 Usage : my $count = $searchio->result_count
247 Function: Returns the number of results we have processed.
248 Returns : integer
249 Args : none
251 =cut
253 sub result_count {
254 my $self = shift;
255 return $self->{'_result_count'};
258 =head2 rewind
260 Title : rewind
261 Usage : $searchio->rewind;
262 Function: Allow one to reset the Result iterator to the beginning, so that
263 next_result() will subsequently return the first result and so on.
265 NB: result objects are not cached, so you will get new result objects
266 each time you rewind. Also, note that result_count() counts the
267 number of times you have called next_result(), so will not be able
268 tell you how many results there were in the file if you use rewind().
270 Returns : n/a
271 Args : none
273 =cut
275 sub rewind {
276 my $self = shift;
277 if ($self->_sequential) {
278 $self->warn("rewind has no effect on piped input when you have chosen 'sequential_read' mode");
280 delete $self->{_end_of_previous_result};