2 # BioPerl module for Bio::Index::Hmmer
4 # Cared for by Josh Lauricha <laurichj@bioinfo.ucr.edu>
6 # Copyright Josh Lauricha
7 # Unless otherwise noted, this was shamelessly ripped from
10 # You may distribute this module under the terms of perl itself
12 # POD documentation - main docs before the code
16 Bio::Index::Hmmer Indexes HMMER reports and supports retreival based on query
20 # Complete Code for indexing a set off report files
23 use Bio::Index::Hmmer;
24 my $indexfile = shift;
25 my $index = Bio::Index::Hmmer->new(
26 -filename => $indexfile,
29 $index->make_index(@ARGV);
32 # Complete code for fetching a report
34 use Bio::Index::Hmmer;
35 my $indexfile = shift;
36 my $index = Bio::Index::Hmmer->new(
37 -filename => $indexfile,
41 foreach my $id (@ARGV) {
42 my $report = $index->fetch_report($id);
43 print "Query: ", $report->query_name(), "\n";
44 while( my $hit = $report->next_hit() ) {
45 print "\tHit Name: ", $hit->name(), "\n";
46 while( my $hsp = $hit->next_domain() ) {
47 print "\t\tE-Value: ", $hsp->evalue(), "\n";
54 This object allows one to build an index on a HMMER file (or files)
55 and provide quick access to the HMMER report for that accession.
56 For best results 'use strict'.
58 You can also set or customize the unique key used to retrieve by
59 writing your own function and calling the id_parser() method.
62 $inx->id_parser(\&get_id);
64 $inx->make_index($file_name);
66 # here is where the retrieval key is specified
69 $line =~ /^KW\s+([A-Z]+)/i;
78 User feedback is an integral part of the evolution of this and other
79 Bioperl modules. Send your comments and suggestions preferably to
80 the Bioperl mailing list. Your participation is much appreciated.
82 bioperl-l@bioperl.org - General discussion
83 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
87 Report bugs to the Bioperl bug tracking system to help us keep track
88 of the bugs and their resolution. Bug reports can be submitted via the
91 http://bugzilla.open-bio.org/
93 =head1 AUTHOR - Josh Lauricha
95 Email laurichj@bioinfo.ucr.edu
99 The rest of the documentation details each of the object methods.
100 Internal methods are usually preceded with a _
104 # Let the code begin...
106 package Bio
::Index
::Hmmer
;
111 use Bio
::Root
::Version
;
113 use base
qw(Bio::Index::Abstract Bio::Root::Root);
117 return ${Bio
::Root
::Version
::VERSION
};
122 Usage : $index = Bio::Index::Hmmer->new(
123 -filename => $dbm_file,
125 -dbm_package => 'DB_File',
128 Function: Returns a new index object. If filename is
129 specified, then open_dbm() is immediately called.
130 Returns : A new index object
131 Args : -filename The name of the dbm index file.
132 -write_flag TRUE if write access to the dbm file is
134 -dbm_package The Perl dbm module to use for the
136 -verbose Print debugging output to STDERR if
143 my($class, @args) = @_;
144 my $self = $class->SUPER::new
(@args);
147 =head2 Bio::Index::Hmmer implemented methods
154 Usage : my $report = $idx->fetch_report($id);
155 Function: Returns a Bio::Search::Result::HMMERResult report object
156 for a specific HMMER report
157 Returns : Bio::Search::Result::HMMERResult
164 my ($self, $id) = @_;
165 my (@header, @data, $line);
166 my $fh = $self->get_stream($id);
169 seek($fh, 0, 0); # The HMMER SearchIO wants the header, so we fetch it
170 while($line = <$fh>) {
172 last if $line =~ /Query sequence:/o;
178 push @data, $_ if defined;
182 # Then join them and send
183 my $rfh = new IO
::String
(join('', @header, @data));
184 my $report = Bio
::SearchIO
->new(
189 return $report->next_result();
192 # shamelessly stolen from Bio::Index::Fasta
197 Usage : $index->id_parser( CODE )
198 Function: Stores or returns the code used by record_id to
199 parse the ID for record from a string. Useful
200 for (for instance) specifying a different
201 parser for different flavours of blast dbs.
202 Returns \&default_id_parser (see below) if not
203 set. If you supply your own id_parser
204 subroutine, then it should expect a fasta
205 description line. An entry will be added to
206 the index for each string in the list returned.
207 Example : $index->id_parser( \&my_id_parser )
208 Returns : ref to CODE if called without arguments
215 my( $self, $code ) =@_;
218 $self->{'_id_parser'} = $code;
220 return $self->{'_id_parser'} || \
&default_id_parser
;
223 =head2 default_id_parser
225 Title : default_id_parser
226 Usage : $id = default_id_parser( $header )
227 Function: The default Blast Query ID parser for Bio::Index::Blast.pm
228 Returns $1 from applying the regexp /^>\s*(\S+)/
231 Args : a header line string
235 sub default_id_parser
237 if ($_[0] =~ /^\s*(\S+)/) {
244 =head2 Require methods from Bio::Index::Abstract
251 Usage : $index->_index_file( $file_name, $i )
252 Function: Specialist function to index HMMER report file(s).
253 Is provided with a filename and an integer
254 by make_index in its SUPER class.
263 my($self, $file, $i) = @_;
266 open(my $HMMER, '<', $file) or $self->throw("cannot open file $file");
272 if( /Query sequence: ([^\s]+)/o ) {
273 $indexpoint = tell($HMMER);
274 foreach my $id ($self->id_parser()->($1)) {
275 print "id is $id, begin is $indexpoint\n" if $self->verbose() > 0;
276 $self->add_record($id, $i, $indexpoint);
284 =head2 Bio::Index::Abstract methods
291 Usage : $value = $self->filename();
292 $self->filename($value);
293 Function: Gets or sets the name of the dbm index file.
294 Returns : The current value of filename
295 Args : Value of filename if setting, or none if
301 Usage : $value = $self->write_flag();
302 $self->write_flag($value);
303 Function: Gets or sets the value of write_flag, which
304 is wether the dbm file should be opened with
306 Returns : The current value of write_flag (default 0)
307 Args : Value of write_flag if setting, or none if
312 Usage : $value = $self->dbm_package();
313 $self->dbm_package($value);
315 Function: Gets or sets the name of the Perl dbm module used.
316 If the value is unset, then it returns the value of
317 the package variable $USE_DBM_TYPE or if that is
318 unset, then it chooses the best available dbm type,
319 choosing 'DB_File' in preference to 'SDBM_File'.
320 Bio::Abstract::Index may work with other dbm file
323 Returns : The current value of dbm_package
324 Args : Value of dbm_package if setting, or none if
331 Usage : $stream = $index->get_stream( $id );
332 Function: Returns a file handle with the file pointer
333 at the approprite place
335 This provides for a way to get the actual
336 file contents and not an object
338 WARNING: you must parse the record deliminter
339 *yourself*. Abstract wont do this for you
342 $fh = $index->get_stream($myid);
346 will parse the entire file if you don't put in
347 a last statement in, like
350 /^\/\// && last; # end of record
354 Returns : A filehandle object
355 Args : string represents the accession number
356 Notes : This method should not be used without forethought
361 Usage : $index->open_dbm()
362 Function: Opens the dbm file associated with the index
363 object. Write access is only given if explicitly
364 asked for by calling new(-write => 1) or having set
365 the write_flag(1) on the index object. The type of
366 dbm file opened is that returned by dbm_package().
367 The name of the file to be is opened is obtained by
368 calling the filename() method.
370 Example : $index->_open_dbm()
371 Returns : 1 on success
377 Usage : $type = $index->_version()
378 Function: Returns a string which identifes the version of an
379 index module. Used to permanently identify an index
380 file as having been created by a particular version
381 of the index module. Must be provided by the sub class
389 Usage : $index->_filename( FILE INT )
390 Function: Indexes the file
398 Usage : $fh = $index->_file_handle( INT )
399 Function: Returns an open filehandle for the file
400 index INT. On opening a new filehandle it
401 caches it in the @{$index->_filehandle} array.
402 If the requested filehandle is already open,
403 it simply returns it from the array.
404 Example : $fist_file_indexed = $index->_file_handle( 0 );
405 Returns : ref to a filehandle
411 Usage : $index->_file_count( INT )
412 Function: Used by the index building sub in a sub class to
413 track the number of files indexed. Sets or gets
414 the number of files indexed when called with or
424 Usage : $index->add_record( $id, @stuff );
425 Function: Calls pack_record on @stuff, and adds the result
426 of pack_record to the index database under key $id.
427 If $id is a reference to an array, then a new entry
428 is added under a key corresponding to each element
430 Example : $index->add_record( $id, $fileNumber, $begin, $end )
431 Returns : TRUE on success or FALSE on failure
437 Usage : $packed_string = $index->pack_record( LIST )
438 Function: Packs an array of scalars into a single string
439 joined by ASCII 034 (which is unlikely to be used
440 in any of the strings), and returns it.
441 Example : $packed_string = $index->pack_record( $fileNumber, $begin, $end )
442 Returns : STRING or undef
447 Title : unpack_record
448 Usage : $index->unpack_record( STRING )
449 Function: Splits the sting provided into an array,
450 splitting on ASCII 034.
451 Example : ( $fileNumber, $begin, $end ) = $index->unpack_record( $self->db->{$id} )
452 Returns : A 3 element ARRAY
453 Args : STRING containing ASCII 034
458 Usage : Called automatically when index goes out of scope
459 Function: Closes connection to database and handles to