2 # BioPerl module for Bio::Index::Blast
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Jason Stajich <jason@bioperl.org>
8 # Copyright Jason Stajich
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
16 Bio::Index::Blast - Indexes Blast reports and supports retrieval
17 based on query accession(s)
22 use Bio::Index::Blast;
24 my ($indexfile,$file1,$file2,$query);
25 my $index = Bio::Index::Blast->new(-filename => $indexfile,
27 $index->make_index($file1,$file2);
29 my $fh = $index->get_stream($query);
31 my $blast_report = Bio::SearchIO->new(-noclose => 1,
34 my $result = $blast_report->next_result;
35 print $result->algorithm, "\n";
36 my $hit = $result->next_hit;
37 print $hit->description, "\n";
38 my $hsp = $hit->next_hsp;
39 print $hsp->bits, "\n";
43 This object allows one to build an index on a blast file (or files)
44 and provide quick access to the blast report for that accession.
46 This also allows for ID parsing using a callback:
48 $inx->id_parser(\&get_id);
50 $inx->make_index($file_name);
52 # here is where the retrieval key is specified
55 $line =~ /^gi\|(\d+)/;
59 The indexer is capable of indexing based on multiple IDs passed back from the
60 callback; this is assuming of course all IDs are unique.
62 Note: for best results 'use strict'.
68 User feedback is an integral part of the evolution of this and other
69 Bioperl modules. Send your comments and suggestions preferably to
70 the Bioperl mailing list. Your participation is much appreciated.
72 bioperl-l@bioperl.org - General discussion
73 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
77 Please direct usage questions or support issues to the mailing list:
79 I<bioperl-l@bioperl.org>
81 rather than to the module maintainer directly. Many experienced and
82 reponsive experts will be able look at the problem and quickly
83 address it. Please include a thorough description of the problem
84 with code and data examples if at all possible.
88 Report bugs to the Bioperl bug tracking system to help us keep track
89 of the bugs and their resolution. Bug reports can be submitted via the
92 https://github.com/bioperl/bioperl-live/issues
94 =head1 AUTHOR - Jason Stajich
96 Email jason-at-bioperl-dot-org
100 The rest of the documentation details each of the object methods.
101 Internal methods are usually preceded with a _
105 # Let the code begin...
107 package Bio
::Index
::Blast
;
112 use base
qw(Bio::Index::Abstract Bio::Root::Root);
115 return ${Bio
::Root
::Version
::VERSION
};
120 Usage : $index = Bio::Index::Abstract->new(
121 -filename => $dbm_file,
123 -dbm_package => 'DB_File',
126 Function: Returns a new index object. If filename is
127 specified, then open_dbm() is immediately called.
128 Bio::Index::Abstract->new() will usually be called
129 directly only when opening an existing index.
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
142 my($class,@args) = @_;
143 my $self = $class->SUPER::new
(@args);
144 my ($type) = $self->_rearrange([qw(PARSER)], @args);
145 $type && $self->blast_parser_type($type);
149 =head2 Bio::Index::Blast implemented methods
156 Usage : my $blastreport = $idx->fetch_report($id);
157 Function: Returns a Bio::SearchIO report object
158 for a specific blast report
159 Returns : Bio::SearchIO
166 my $fh = $self->get_stream($id);
167 my $report = Bio
::SearchIO
->new(-noclose
=> 1,
168 -format
=> $self->blast_parser_type,
170 return $report->next_result;
176 Usage : my $blastreport = $idx->fetch_result($id);
177 Function: Returns a Bio::SearchIO report object
178 for a specific blast report
179 Returns : Bio::SearchIO
181 Note : alias of fetch_report()
185 *fetch_result
= \
&fetch_report
;
187 =head2 Require methods from Bio::Index::Abstract
194 Usage : $index->_index_file( $file_name, $i )
195 Function: Specialist function to index BLAST report file(s).
196 Is provided with a filename and an integer
197 by make_index in its SUPER class.
207 $i, # Index-number of file being indexed
210 my( $begin, # Offset from start of file of the start
211 # of the last found record.
214 open my $BLAST, '<', $file or $self->throw("Could not read file '$file': $!");
216 my (@data, @records);
221 # In Windows, text files have '\r\n' as line separator, but when reading in
222 # text mode Perl will only show the '\n'. This means that for a line "ABC\r\n",
223 # "length $_" will report 4 although the line is 5 bytes in length.
224 # We assume that all lines have the same line separator and only read current line.
225 my $init_pos = tell($BLAST);
226 my $curr_line = <$BLAST>;
227 my $pos_diff = tell($BLAST) - $init_pos;
228 my $correction = $pos_diff - length $curr_line;
229 seek $BLAST, $init_pos, 0; # Rewind position to proceed to read the file
231 # fencepost problem: we basically just find the top and the query
232 while( my $line = <$BLAST> ) {
234 # in recent RPS-BLAST output the only delimiter between result
235 # sections is '^Query=' - in other BLAST outputs you
236 # can use '^(RPS-|T?)BLAST(P?|N?|X?)'
238 if ( $line =~ /^(RPS-|T?)BLAST(P?|N?|X?)/ ) {
240 $indexpoint = tell($BLAST) - length($line) - $correction;
242 if ( $line =~ /^Query=\s*([^\n]+)$/ ) {
244 $indexpoint = tell($BLAST) - length($line) - $correction if ( $prefix eq 'RPS-' );
246 foreach my $id ($self->id_parser()->($1)) {
247 $self->debug("id is $id, begin is $indexpoint\n");
248 $self->add_record($id, $i, $indexpoint);
254 # shamelessly stolen from Bio::Index::Fasta
259 Usage : $index->id_parser( CODE )
260 Function: Stores or returns the code used by record_id to
261 parse the ID for record from a string. Useful
262 for (for instance) specifying a different
263 parser for different flavours of blast dbs.
264 Returns \&default_id_parser (see below) if not
265 set. If you supply your own id_parser
266 subroutine, then it should expect a fasta
267 description line. An entry will be added to
268 the index for each string in the list returned.
269 Example : $index->id_parser( \&my_id_parser )
270 Returns : ref to CODE if called without arguments
276 my( $self, $code ) =@_;
279 $self->{'_id_parser'} = $code;
281 return $self->{'_id_parser'} || \
&default_id_parser
;
284 =head2 default_id_parser
286 Title : default_id_parser
287 Usage : $id = default_id_parser( $header )
288 Function: The default Blast Query ID parser for Bio::Index::Blast.pm
289 Returns $1 from applying the regexp /^>\s*(\S+)/
292 Args : a header line string
296 sub default_id_parser
{
297 if ($_[0] =~ /^\s*(\S+)/) {
304 =head2 blast_parser_type
306 Title : blast_parser_type
307 Usage : $index->blast_parser_type() # returns
308 Function: Get/Set SearchIO-based text (-m0) BLAST parser. Only values in
309 local %VALID_PARSERS hash allowed.
312 Note : This only allows simple text-based parsing options; tabular, XML,
313 or others are not supported (see Bio::Index::BlastTable for tab
318 my %VALID_PARSERS = map {$_ =>1} qw(blast blast_pull);
320 sub blast_parser_type
{
321 my ($self, $type) = @_;
323 $self->throw("$type is not a supported BLAST text parser") unless
324 exists $VALID_PARSERS{$type};
325 $self->{_blast_parser_type
} = $type;
327 return $self->{_blast_parser_type
} || 'blast';
330 =head2 Bio::Index::Abstract methods
337 Usage : $value = $self->filename();
338 $self->filename($value);
339 Function: Gets or sets the name of the dbm index file.
340 Returns : The current value of filename
341 Args : Value of filename if setting, or none if
347 Usage : $value = $self->write_flag();
348 $self->write_flag($value);
349 Function: Gets or sets the value of write_flag, which
350 is whether the dbm file should be opened with
352 Returns : The current value of write_flag (default 0)
353 Args : Value of write_flag if setting, or none if
358 Usage : $value = $self->dbm_package();
359 $self->dbm_package($value);
361 Function: Gets or sets the name of the Perl dbm module used.
362 If the value is unset, then it returns the value of
363 the package variable $USE_DBM_TYPE or if that is
364 unset, then it chooses the best available dbm type,
365 choosing 'DB_File' in preference to 'SDBM_File'.
366 Bio::Abstract::Index may work with other dbm file
369 Returns : The current value of dbm_package
370 Args : Value of dbm_package if setting, or none if
377 Usage : $stream = $index->get_stream( $id );
378 Function: Returns a file handle with the file pointer
379 at the approprite place
381 This provides for a way to get the actual
382 file contents and not an object
384 WARNING: you must parse the record deliminter
385 *yourself*. Abstract won't do this for you
388 $fh = $index->get_stream($myid);
392 will parse the entire file if you do not put in
393 a last statement in, like
396 /^\/\// && last; # end of record
400 Returns : A filehandle object
401 Args : string represents the accession number
402 Notes : This method should not be used without forethought
407 Usage : $index->open_dbm()
408 Function: Opens the dbm file associated with the index
409 object. Write access is only given if explicitly
410 asked for by calling new(-write => 1) or having set
411 the write_flag(1) on the index object. The type of
412 dbm file opened is that returned by dbm_package().
413 The name of the file to be is opened is obtained by
414 calling the filename() method.
416 Example : $index->_open_dbm()
417 Returns : 1 on success
423 Usage : $type = $index->_version()
424 Function: Returns a string which identifes the version of an
425 index module. Used to permanently identify an index
426 file as having been created by a particular version
427 of the index module. Must be provided by the sub class
435 Usage : $index->_filename( FILE INT )
436 Function: Indexes the file
444 Usage : $fh = $index->_file_handle( INT )
445 Function: Returns an open filehandle for the file
446 index INT. On opening a new filehandle it
447 caches it in the @{$index->_filehandle} array.
448 If the requested filehandle is already open,
449 it simply returns it from the array.
450 Example : $fist_file_indexed = $index->_file_handle( 0 );
451 Returns : ref to a filehandle
457 Usage : $index->_file_count( INT )
458 Function: Used by the index building sub in a sub class to
459 track the number of files indexed. Sets or gets
460 the number of files indexed when called with or
470 Usage : $index->add_record( $id, @stuff );
471 Function: Calls pack_record on @stuff, and adds the result
472 of pack_record to the index database under key $id.
473 If $id is a reference to an array, then a new entry
474 is added under a key corresponding to each element
476 Example : $index->add_record( $id, $fileNumber, $begin, $end )
477 Returns : TRUE on success or FALSE on failure
483 Usage : $packed_string = $index->pack_record( LIST )
484 Function: Packs an array of scalars into a single string
485 joined by ASCII 034 (which is unlikely to be used
486 in any of the strings), and returns it.
487 Example : $packed_string = $index->pack_record( $fileNumber, $begin, $end )
488 Returns : STRING or undef
493 Title : unpack_record
494 Usage : $index->unpack_record( STRING )
495 Function: Splits the sting provided into an array,
496 splitting on ASCII 034.
497 Example : ( $fileNumber, $begin, $end ) = $index->unpack_record( $self->db->{$id} )
498 Returns : A 3 element ARRAY
499 Args : STRING containing ASCII 034
504 Usage : Called automatically when index goes out of scope
505 Function: Closes connection to database and handles to