2 # BioPerl module for Bio::Index::Hmmer
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Josh Lauricha <laurichj@bioinfo.ucr.edu>
8 # Copyright Josh Lauricha
9 # Unless otherwise noted, this was shamelessly ripped from
12 # You may distribute this module under the terms of perl itself
14 # POD documentation - main docs before the code
18 Bio::Index::Hmmer - indexes HMMER reports and supports retreival based on query
22 # Complete Code for indexing a set of report files
25 use Bio::Index::Hmmer;
26 my $indexfile = shift;
27 my $index = Bio::Index::Hmmer->new(
28 -filename => $indexfile,
31 $index->make_index(@ARGV);
34 # Complete code for fetching a report
36 use Bio::Index::Hmmer;
37 my $indexfile = shift;
38 my $index = Bio::Index::Hmmer->new(
39 -filename => $indexfile,
43 foreach my $id (@ARGV) {
44 my $report = $index->fetch_report($id);
45 print "Query: ", $report->query_name(), "\n";
46 while( my $hit = $report->next_hit() ) {
47 print "\tHit Name: ", $hit->name(), "\n";
48 while( my $hsp = $hit->next_domain() ) {
49 print "\t\tE-Value: ", $hsp->evalue(), "\n";
56 This object allows one to build an index on a HMMER file (or files)
57 and provide quick access to the HMMER report for that accession.
58 For best results 'use strict'.
60 You can also set or customize the unique key used to retrieve by
61 writing your own function and calling the id_parser() method.
64 $inx->id_parser(\&get_id);
66 $inx->make_index($file_name);
68 # here is where the retrieval key is specified
71 $line =~ /^KW\s+([A-Z]+)/i;
80 User feedback is an integral part of the evolution of this and other
81 Bioperl modules. Send your comments and suggestions preferably to
82 the Bioperl mailing list. Your participation is much appreciated.
84 bioperl-l@bioperl.org - General discussion
85 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
89 Please direct usage questions or support issues to the mailing list:
91 I<bioperl-l@bioperl.org>
93 rather than to the module maintainer directly. Many experienced and
94 reponsive experts will be able look at the problem and quickly
95 address it. Please include a thorough description of the problem
96 with code and data examples if at all possible.
100 Report bugs to the Bioperl bug tracking system to help us keep track
101 of the bugs and their resolution. Bug reports can be submitted via the
104 https://github.com/bioperl/bioperl-live/issues
106 =head1 AUTHOR - Josh Lauricha
108 Email laurichj@bioinfo.ucr.edu
112 The rest of the documentation details each of the object methods.
113 Internal methods are usually preceded with a _
117 # Let the code begin...
119 package Bio
::Index
::Hmmer
;
124 use Bio
::Root
::Version
;
126 use base
qw(Bio::Index::Abstract Bio::Root::Root);
130 return ${Bio
::Root
::Version
::VERSION
};
135 Usage : $index = Bio::Index::Hmmer->new(
136 -filename => $dbm_file,
138 -dbm_package => 'DB_File',
141 Function: Returns a new index object. If filename is
142 specified, then open_dbm() is immediately called.
143 Returns : A new index object
144 Args : -filename The name of the dbm index file.
145 -write_flag TRUE if write access to the dbm file is
147 -dbm_package The Perl dbm module to use for the
149 -verbose Print debugging output to STDERR if
156 my($class, @args) = @_;
157 my $self = $class->SUPER::new
(@args);
160 =head2 Bio::Index::Hmmer implemented methods
167 Usage : my $report = $idx->fetch_report($id);
168 Function: Returns a Bio::Search::Result::HMMERResult report object
169 for a specific HMMER report
170 Returns : Bio::Search::Result::HMMERResult
177 my ($self, $id) = @_;
178 my (@header, @data, $line);
179 my $fh = $self->get_stream($id);
182 seek($fh, 0, 0); # The HMMER SearchIO wants the header, so we fetch it
183 while($line = <$fh>) {
185 last if $line =~ /Query sequence:/o;
191 push @data, $_ if defined;
195 # Then join them and send
196 my $rfh = IO
::String
->new(join('', @header, @data));
197 my $report = Bio
::SearchIO
->new(
202 return $report->next_result();
205 # shamelessly stolen from Bio::Index::Fasta
210 Usage : $index->id_parser( CODE )
211 Function: Stores or returns the code used by record_id to
212 parse the ID for record from a string. Useful
213 for (for instance) specifying a different
214 parser for different flavours of blast dbs.
215 Returns \&default_id_parser (see below) if not
216 set. If you supply your own id_parser
217 subroutine, then it should expect a fasta
218 description line. An entry will be added to
219 the index for each string in the list returned.
220 Example : $index->id_parser( \&my_id_parser )
221 Returns : ref to CODE if called without arguments
228 my( $self, $code ) =@_;
231 $self->{'_id_parser'} = $code;
233 return $self->{'_id_parser'} || \
&default_id_parser
;
236 =head2 default_id_parser
238 Title : default_id_parser
239 Usage : $id = default_id_parser( $header )
240 Function: The default Blast Query ID parser for Bio::Index::Blast.pm
241 Returns $1 from applying the regexp /^>\s*(\S+)/
244 Args : a header line string
248 sub default_id_parser
250 if ($_[0] =~ /^\s*(\S+)/) {
257 =head2 Require methods from Bio::Index::Abstract
264 Usage : $index->_index_file( $file_name, $i )
265 Function: Specialist function to index HMMER report file(s).
266 Is provided with a filename and an integer
267 by make_index in its SUPER class.
276 my($self, $file, $i) = @_;
279 open my $HMMER, '<', $file or $self->throw("Could not read file '$file': $!");
285 if( /Query sequence: ([^\s]+)/o ) {
286 $indexpoint = tell($HMMER);
287 foreach my $id ($self->id_parser()->($1)) {
288 print "id is $id, begin is $indexpoint\n" if $self->verbose() > 0;
289 $self->add_record($id, $i, $indexpoint);
297 =head2 Bio::Index::Abstract methods
304 Usage : $value = $self->filename();
305 $self->filename($value);
306 Function: Gets or sets the name of the dbm index file.
307 Returns : The current value of filename
308 Args : Value of filename if setting, or none if
314 Usage : $value = $self->write_flag();
315 $self->write_flag($value);
316 Function: Gets or sets the value of write_flag, which
317 is whether the dbm file should be opened with
319 Returns : The current value of write_flag (default 0)
320 Args : Value of write_flag if setting, or none if
325 Usage : $value = $self->dbm_package();
326 $self->dbm_package($value);
328 Function: Gets or sets the name of the Perl dbm module used.
329 If the value is unset, then it returns the value of
330 the package variable $USE_DBM_TYPE or if that is
331 unset, then it chooses the best available dbm type,
332 choosing 'DB_File' in preference to 'SDBM_File'.
333 Bio::Abstract::Index may work with other dbm file
336 Returns : The current value of dbm_package
337 Args : Value of dbm_package if setting, or none if
344 Usage : $stream = $index->get_stream( $id );
345 Function: Returns a file handle with the file pointer
346 at the approprite place
348 This provides for a way to get the actual
349 file contents and not an object
351 WARNING: you must parse the record deliminter
352 *yourself*. Abstract won't do this for you
355 $fh = $index->get_stream($myid);
359 will parse the entire file if you don't put in
360 a last statement in, like
363 /^\/\// && last; # end of record
367 Returns : A filehandle object
368 Args : string represents the accession number
369 Notes : This method should not be used without forethought
374 Usage : $index->open_dbm()
375 Function: Opens the dbm file associated with the index
376 object. Write access is only given if explicitly
377 asked for by calling new(-write => 1) or having set
378 the write_flag(1) on the index object. The type of
379 dbm file opened is that returned by dbm_package().
380 The name of the file to be is opened is obtained by
381 calling the filename() method.
383 Example : $index->_open_dbm()
384 Returns : 1 on success
390 Usage : $type = $index->_version()
391 Function: Returns a string which identifes the version of an
392 index module. Used to permanently identify an index
393 file as having been created by a particular version
394 of the index module. Must be provided by the sub class
402 Usage : $index->_filename( FILE INT )
403 Function: Indexes the file
411 Usage : $fh = $index->_file_handle( INT )
412 Function: Returns an open filehandle for the file
413 index INT. On opening a new filehandle it
414 caches it in the @{$index->_filehandle} array.
415 If the requested filehandle is already open,
416 it simply returns it from the array.
417 Example : $fist_file_indexed = $index->_file_handle( 0 );
418 Returns : ref to a filehandle
424 Usage : $index->_file_count( INT )
425 Function: Used by the index building sub in a sub class to
426 track the number of files indexed. Sets or gets
427 the number of files indexed when called with or
437 Usage : $index->add_record( $id, @stuff );
438 Function: Calls pack_record on @stuff, and adds the result
439 of pack_record to the index database under key $id.
440 If $id is a reference to an array, then a new entry
441 is added under a key corresponding to each element
443 Example : $index->add_record( $id, $fileNumber, $begin, $end )
444 Returns : TRUE on success or FALSE on failure
450 Usage : $packed_string = $index->pack_record( LIST )
451 Function: Packs an array of scalars into a single string
452 joined by ASCII 034 (which is unlikely to be used
453 in any of the strings), and returns it.
454 Example : $packed_string = $index->pack_record( $fileNumber, $begin, $end )
455 Returns : STRING or undef
460 Title : unpack_record
461 Usage : $index->unpack_record( STRING )
462 Function: Splits the sting provided into an array,
463 splitting on ASCII 034.
464 Example : ( $fileNumber, $begin, $end ) = $index->unpack_record( $self->db->{$id} )
465 Returns : A 3 element ARRAY
466 Args : STRING containing ASCII 034
471 Usage : Called automatically when index goes out of scope
472 Function: Closes connection to database and handles to