lib/Bio/Index/Hmmer.pm

filter: Statementstmt 96.3% 53 / 55 Branchbran 68.1% 15 / 22 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 88.4% 84 / 95 CC 14 SCAR 13.7
CC
14
Cov
87%
fetch_report
6.1
_index_file
6.0
id_parser
3.5
CRAP 3.9
« lib/Bio/Search/HSP/HmmpfamHSP.pm ↑ Summary lib/Bio/Search/Hit/HmmpfamHit.pm »
1 #
2 # BioPerl module for Bio::Index::Hmmer
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Josh Lauricha <laurichj@bioinfo.ucr.edu>
7 #
8 # Copyright Josh Lauricha
9 # Unless otherwise noted, this was shamelessly ripped from
10 # Bio::Index::Blast
11 #
12 # You may distribute this module under the terms of perl itself
13  
14 # POD documentation - main docs before the code
15  
16 =head1 NAME
17  
18 Bio::Index::Hmmer - indexes HMMER reports and supports retreival based on query
19  
20 =head1 SYNOPSIS
21  
22 # Complete Code for indexing a set of report files
23 #!/usr/bin/perl -w
24 use strict;
25 use Bio::Index::Hmmer;
26 my $indexfile = shift;
27 my $index = Bio::Index::Hmmer->new(
28 -filename => $indexfile,
29 -write_flag => 1
30 );
31 $index->make_index(@ARGV);
32  
33  
34 # Complete code for fetching a report
35 use strict;
36 use Bio::Index::Hmmer;
37 my $indexfile = shift;
38 my $index = Bio::Index::Hmmer->new(
39 -filename => $indexfile,
40 -write_flag => 0
41 );
42  
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";
50 }
51 }
52 }
53  
54 =head1 DESCRIPTION
55  
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'.
59  
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.
62 For example:
63  
64 $inx->id_parser(\&get_id);
65 # make the index
66 $inx->make_index($file_name);
67  
68 # here is where the retrieval key is specified
69 sub get_id {
70 my $line = shift;
71 $line =~ /^KW\s+([A-Z]+)/i;
72 $1;
73 }
74  
75  
76 =head1 FEEDBACK
77  
78 =head2 Mailing Lists
79  
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.
83  
84 bioperl-l@bioperl.org - General discussion
85 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
86  
87 =head2 Support
88  
89 Please direct usage questions or support issues to the mailing list:
90  
91 I<bioperl-l@bioperl.org>
92  
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.
97  
98 =head2 Reporting Bugs
99  
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
102 web:
103  
104 https://github.com/bioperl/bioperl-live/issues
105  
106 =head1 AUTHOR - Josh Lauricha
107  
108 Email laurichj@bioinfo.ucr.edu
109  
110 =head1 APPENDIX
111  
112 The rest of the documentation details each of the object methods.
113 Internal methods are usually preceded with a _
114  
115 =cut
116  
117 # Let the code begin...
118  
119 package Bio::Index::Hmmer;
120 2 $Bio::Index::Hmmer::VERSION = '1.7.3';
121 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 2 ▶use Bio::SearchIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 2 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 2 ▶use Bio::Root::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
126  
127 2 ▶use base qw(Bio::Index::Abstract Bio::Root::Root);
Subroutine BEGIN: called CC=1 SCAR=0.0
128  
129 sub _version
130 1 ▶{
Subroutine _version: called CC=1 SCAR=0.0
131 1 return ${Bio::Root::Version::VERSION};
132 }
133  
134 =head2 new
135  
136 Usage : $index = Bio::Index::Hmmer->new(
137 -filename => $dbm_file,
138 -write_flag => 0,
139 -dbm_package => 'DB_File',
140 -verbose => 0
141 );
142 Function: Returns a new index object. If filename is
143 specified, then open_dbm() is immediately called.
144 Returns : A new index object
145 Args : -filename The name of the dbm index file.
146 -write_flag TRUE if write access to the dbm file is
147 needed.
148 -dbm_package The Perl dbm module to use for the
149 index.
150 -verbose Print debugging output to STDERR if
151 TRUE.
152  
153 =cut
154  
155 sub new
156 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
157 1 my($class, @args) = @_;
158 1 my $self = $class->SUPER::new(@args);
159 }
160  
161 =head2 Bio::Index::Hmmer implemented methods
162  
163 =cut
164  
165 =head2 fetch_report
166  
167 Title : fetch_report
168 Usage : my $report = $idx->fetch_report($id);
169 Function: Returns a Bio::Search::Result::HMMERResult report object
170 for a specific HMMER report
171 Returns : Bio::Search::Result::HMMERResult
172 Args : valid id
173  
174 =cut
175  
176 sub fetch_report
177 1 ▶{
Subroutine fetch_report: called CC=6 SCAR=18.0
Pod documented
178 1 my ($self, $id) = @_;
179 1 my (@header, @data, $line);
180 1 my $fh = $self->get_stream($id);
181 1 my $pos = tell($fh);
182  
183 1 seek($fh, 0, 0); # The HMMER SearchIO wants the header, so we fetch it
184 1 ▶ while($line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 10 0 10
185 10 push @header, $line;
186 10 ▶ last if $line =~ /Query sequence:/o;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /Query sequence:/o 1 9 10
187 }
188 1 seek($fh, $pos, 0);
189  
190 # Then the data
191 1 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 64 0 64
192 64 ▶ push @data, $_ if defined;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 64 0 64
193 64 ▶ last if m{//}o;
Branch: 1 branch
BranchTrueFalseTotal
if m[//]o 1 63 64
194 }
195  
196 # Then join them and send
197 1 my $rfh = IO::String->new(join('', @header, @data));
198 1 my $report = Bio::SearchIO->new(
199 -noclose => 1,
200 -format => 'hmmer',
201 -fh => $rfh
202 );
203 1 return $report->next_result();
204 }
205  
206 # shamelessly stolen from Bio::Index::Fasta
207  
208 =head2 id_parser
209  
210 Title : id_parser
211 Usage : $index->id_parser( CODE )
212 Function: Stores or returns the code used by record_id to
213 parse the ID for record from a string. Useful
214 for (for instance) specifying a different
215 parser for different flavours of blast dbs.
216 Returns \&default_id_parser (see below) if not
217 set. If you supply your own id_parser
218 subroutine, then it should expect a fasta
219 description line. An entry will be added to
220 the index for each string in the list returned.
221 Example : $index->id_parser( \&my_id_parser )
222 Returns : ref to CODE if called without arguments
223 Args : CODE
224  
225 =cut
226  
227 sub id_parser
228 2 ▶{
Subroutine id_parser: called CC=3 SCAR=12.5
Pod documented
229 2 my( $self, $code ) =@_;
230  
231 2 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 0 2 2
232 0 $self->{'_id_parser'} = $code;
233 }
234 2 ▶ return $self->{'_id_parser'} || \&default_id_parser;
Condition50%1/2
$self->{'_id_parser'} || \&Bio::Index::Hmmer::default_id_parser
l!l
02
MC/DC0%0/1
$self->{'_id_parser'} || \&Bio::Index::Hmmer::default_id_parser
$self->{'_id_parser'}
Truth table50%1/2
A
A= $self->{'_id_parser'}
Aresult
11
00
235 }
236  
237 =head2 default_id_parser
238  
239 Title : default_id_parser
240 Usage : $id = default_id_parser( $header )
241 Function: The default Blast Query ID parser for Bio::Index::Blast.pm
242 Returns $1 from applying the regexp /^>\s*(\S+)/
243 to $header.
244 Returns : ID string
245 Args : a header line string
246  
247 =cut
248  
249 sub default_id_parser
250 2 ▶{
Subroutine default_id_parser: called CC=2 SCAR=8.1
Pod documented
251 2 ▶ if ($_[0] =~ /^\s*(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\s*(\S+)/) { } 2 0 2
252 2 return $1;
253 } else {
254 0 return;
255 }
256 }
257  
258 =head2 Require methods from Bio::Index::Abstract
259  
260 =cut
261  
262 =head2 _index_file
263  
264 Title : _index_file
265 Usage : $index->_index_file( $file_name, $i )
266 Function: Specialist function to index HMMER report file(s).
267 Is provided with a filename and an integer
268 by make_index in its SUPER class.
269 Example :
270 Returns :
271 Args :
272  
273 =cut
274  
275  
276 1 ▶sub _index_file {
Subroutine _index_file: called CC=6 SCAR=18.0
277 1 my($self, $file, $i) = @_;
278 1 my($begin);
279  
280 1 ▶ open my $HMMER, '<', $file or $self->throw("Could not read file '$file': $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $HMMER, "<", $file or $self->throw("Could not read file '${file}': $!") 1 0 1
281  
282 1 my $id;
283 1 my $indexpoint = 0;
284  
285 1 ▶ while(<$HMMER>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $HMMER)) 106 1 107
286 106 ▶ if( /Query sequence: ([^\s]+)/o ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/Query sequence: ([^\s]+)/o) 2 104 106
287 2 $indexpoint = tell($HMMER);
288 2 foreach my $id ($self->id_parser()->($1)) {
289 2 ▶ print "id is $id, begin is $indexpoint\n" if $self->verbose() > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 0 0 2 2
290 2 $self->add_record($id, $i, $indexpoint);
291 }
292 }
293 }
294 1 close $HMMER;
295 1 return 1;
296 }
297  
298 =head2 Bio::Index::Abstract methods
299  
300 =cut
301  
302 =head2 filename
303  
304 Title : filename
305 Usage : $value = $self->filename();
306 $self->filename($value);
307 Function: Gets or sets the name of the dbm index file.
308 Returns : The current value of filename
309 Args : Value of filename if setting, or none if
310 getting the value.
311  
312 =head2 write_flag
313  
314 Title : write_flag
315 Usage : $value = $self->write_flag();
316 $self->write_flag($value);
317 Function: Gets or sets the value of write_flag, which
318 is whether the dbm file should be opened with
319 write access.
320 Returns : The current value of write_flag (default 0)
321 Args : Value of write_flag if setting, or none if
322 getting the value.
323  
324 =head2 dbm_package
325  
326 Usage : $value = $self->dbm_package();
327 $self->dbm_package($value);
328  
329 Function: Gets or sets the name of the Perl dbm module used.
330 If the value is unset, then it returns the value of
331 the package variable $USE_DBM_TYPE or if that is
332 unset, then it chooses the best available dbm type,
333 choosing 'DB_File' in preference to 'SDBM_File'.
334 Bio::Abstract::Index may work with other dbm file
335 types.
336  
337 Returns : The current value of dbm_package
338 Args : Value of dbm_package if setting, or none if
339 getting the value.
340  
341  
342 =head2 get_stream
343  
344 Title : get_stream
345 Usage : $stream = $index->get_stream( $id );
346 Function: Returns a file handle with the file pointer
347 at the approprite place
348  
349 This provides for a way to get the actual
350 file contents and not an object
351  
352 WARNING: you must parse the record deliminter
353 *yourself*. Abstract won't do this for you
354 So this code
355  
356 $fh = $index->get_stream($myid);
357 while( <$fh> ) {
358 # do something
359 }
360 will parse the entire file if you don't put in
361 a last statement in, like
362  
363 while( <$fh> ) {
364 /^\/\// && last; # end of record
365 # do something
366 }
367  
368 Returns : A filehandle object
369 Args : string represents the accession number
370 Notes : This method should not be used without forethought
371  
372  
373 =head2 open_dbm
374  
375 Usage : $index->open_dbm()
376 Function: Opens the dbm file associated with the index
377 object. Write access is only given if explicitly
378 asked for by calling new(-write => 1) or having set
379 the write_flag(1) on the index object. The type of
380 dbm file opened is that returned by dbm_package().
381 The name of the file to be is opened is obtained by
382 calling the filename() method.
383  
384 Example : $index->_open_dbm()
385 Returns : 1 on success
386  
387  
388 =head2 _version
389  
390 Title : _version
391 Usage : $type = $index->_version()
392 Function: Returns a string which identifes the version of an
393 index module. Used to permanently identify an index
394 file as having been created by a particular version
395 of the index module. Must be provided by the sub class
396 Example :
397 Returns :
398 Args : none
399  
400 =head2 _filename
401  
402 Title : _filename
403 Usage : $index->_filename( FILE INT )
404 Function: Indexes the file
405 Example :
406 Returns :
407 Args :
408  
409 =head2 _file_handle
410  
411 Title : _file_handle
412 Usage : $fh = $index->_file_handle( INT )
413 Function: Returns an open filehandle for the file
414 index INT. On opening a new filehandle it
415 caches it in the @{$index->_filehandle} array.
416 If the requested filehandle is already open,
417 it simply returns it from the array.
418 Example : $fist_file_indexed = $index->_file_handle( 0 );
419 Returns : ref to a filehandle
420 Args : INT
421  
422 =head2 _file_count
423  
424 Title : _file_count
425 Usage : $index->_file_count( INT )
426 Function: Used by the index building sub in a sub class to
427 track the number of files indexed. Sets or gets
428 the number of files indexed when called with or
429 without an argument.
430 Example :
431 Returns : INT
432 Args : INT
433  
434  
435 =head2 add_record
436  
437 Title : add_record
438 Usage : $index->add_record( $id, @stuff );
439 Function: Calls pack_record on @stuff, and adds the result
440 of pack_record to the index database under key $id.
441 If $id is a reference to an array, then a new entry
442 is added under a key corresponding to each element
443 of the array.
444 Example : $index->add_record( $id, $fileNumber, $begin, $end )
445 Returns : TRUE on success or FALSE on failure
446 Args : ID LIST
447  
448 =head2 pack_record
449  
450 Title : pack_record
451 Usage : $packed_string = $index->pack_record( LIST )
452 Function: Packs an array of scalars into a single string
453 joined by ASCII 034 (which is unlikely to be used
454 in any of the strings), and returns it.
455 Example : $packed_string = $index->pack_record( $fileNumber, $begin, $end )
456 Returns : STRING or undef
457 Args : LIST
458  
459 =head2 unpack_record
460  
461 Title : unpack_record
462 Usage : $index->unpack_record( STRING )
463 Function: Splits the sting provided into an array,
464 splitting on ASCII 034.
465 Example : ( $fileNumber, $begin, $end ) = $index->unpack_record( $self->db->{$id} )
466 Returns : A 3 element ARRAY
467 Args : STRING containing ASCII 034
468  
469 =head2 DESTROY
470  
471 Title : DESTROY
472 Usage : Called automatically when index goes out of scope
473 Function: Closes connection to database and handles to
474 sequence files
475 Returns : NEVER
476 Args : NONE
477  
478  
479 =cut
480  
481 2 1;
« lib/Bio/Search/HSP/HmmpfamHSP.pm ↑ Summary lib/Bio/Search/Hit/HmmpfamHit.pm »