Index/Stockholm.pm

filter: Statementstmt 97.9% 47 / 48 Branchbran 75.0% 12 / 16 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 91.2% 73 / 80 CC 11 SCAR 15.0
CC
11
Cov
91%
_index_file
8.1
id_parser
3.5
default_id_parser
2.0
CRAP 4.5
« AlignIO/stockholm.pm ↑ Summary
1 #
2 # BioPerl module for Bio::Index::Stockholm
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Chris Fields <cjfields@uiuc.edu>
7 #
8 # Copyright Chris Fields
9 #
10 # You may distribute this module under the same terms as perl itself
11  
12 # POD documentation - main docs before the code
13  
14 =head1 NAME
15  
16 Bio::Index::Stockholm - Interface for indexing Stockholm files
17  
18 =head1 SYNOPSIS
19  
20 use strict;
21 use Bio::Index::Stockholm;
22 my ($indexfile,$file1,$file2,$query);
23 my $index = Bio::Index::Stockholm->new(-filename => $indexfile,
24 -write_flag => 1);
25 $index->make_index($file1,$file2);
26  
27 # get raw data stream starting at alignment position
28 my $fh = $index->get_stream($query);
29  
30 # fetch individual alignment
31 my $align = $index->fetch_aln($query); # alias for fetch_report
32 my $align = $index->fetch_report($query); # same as above
33 print "query is ", $align->display_id, "\n";
34  
35 =head1 DESCRIPTION
36  
37 Indexes Stockholm format alignments such as those from Pfam and Rfam.
38 Returns raw stream data using the ID or a Bio::SimpleAlign object
39 (via Bio::AlignIO).
40  
41 This module also allows for ID parsing using a callback:
42  
43 $inx->id_parser(\&get_id);
44 # make the index
45 $inx->make_index($file_name);
46  
47 # here is where the retrieval key is specified
48 sub get_id {
49 my $line = shift;
50 $line =~ /^>.+gi\|(\d+)/;
51 $1;
52 }
53  
54 The indexer is capable of indexing based on multiple IDs passed back from the
55 callback; this is assuming of course all IDs are unique. The default is to use
56 the alignment ID provided for Rfam/Pfam output.
57  
58 Note: for best results 'use strict'.
59  
60 =head1 TODO
61  
62 - allow using an alternative regex for indexing (for instance, the ID instead of AC)
63  
64 =head1 FEEDBACK
65  
66 =head2 Mailing Lists
67  
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.
71  
72 bioperl-l@bioperl.org - General discussion
73 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
74  
75 =head2 Support
76  
77 Please direct usage questions or support issues to the mailing list:
78  
79 I<bioperl-l@bioperl.org>
80  
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.
85  
86 =head2 Reporting Bugs
87  
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
90 web:
91  
92 https://github.com/bioperl/bioperl-live/issues
93  
94 =head1 AUTHOR - Chris Fields
95  
96 Email cjfields-at-bioperl-dot-org
97  
98 =head1 APPENDIX
99  
100 The rest of the documentation details each of the object methods.
101 Internal methods are usually preceded with a _
102  
103 =cut
104  
105 # Let the code begin...
106  
107 package Bio::Index::Stockholm;
108 2 $Bio::Index::Stockholm::VERSION = '1.7.3';
109 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 2 ▶use Bio::AlignIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112 2 ▶use base qw(Bio::Index::Abstract Bio::Root::Root);
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 1 ▶sub _version {
Subroutine _version: called CC=1 SCAR=0.0
115 1 return ${Bio::Root::Version::VERSION};
116 }
117  
118 =head2 new
119  
120 Usage : $index = Bio::Index::Abstract->new(
121 -filename => $dbm_file,
122 -write_flag => 0,
123 -dbm_package => 'DB_File',
124 -verbose => 0);
125  
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
133 needed.
134 -dbm_package The Perl dbm module to use for the
135 index.
136 -verbose Print debugging output to STDERR if
137 TRUE.
138  
139 =cut
140  
141 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
142  
143 1 my($class,@args) = @_;
144  
145 1 my $self = $class->SUPER::new(@args);
146  
147 }
148  
149 =head2 Bio::Index::Stockholm implemented methods
150  
151 =cut
152  
153 =head2 fetch_report
154  
155 Title : fetch_report
156 Usage : my $align = $idx->fetch_report($id);
157 Function: Returns a Bio::SimpleAlign object
158 for a specific alignment
159 Returns : Bio::SimpleAlign
160 Args : valid id
161  
162 =cut
163  
164 1 ▶sub fetch_report{
Subroutine fetch_report: called CC=1 SCAR=0.0
Pod documented
165 1 my ($self,$id) = @_;
166 1 my $fh = $self->get_stream($id);
167 1 my $report = Bio::AlignIO->new(-noclose => 1,
168 -format => 'stockholm',
169 -fh => $fh);
170 1 return $report->next_aln;
171 }
172  
173 =head2 fetch_aln
174  
175 Title : fetch_aln
176 Usage : my $align = $idx->fetch_aln($id);
177 Function: Returns a Bio::SimpleAlign object
178 for a specific alignment
179 Returns : Bio::SimpleAlign
180 Args : valid id
181 Note : alias for fetch_report
182  
183 =cut
184  
185 2 *fetch_aln = \&fetch_report;
186  
187 =head2 Require methods from Bio::Index::Abstract
188  
189 =cut
190  
191 =head2 _index_file
192  
193 Title : _index_file
194 Usage : $index->_index_file( $file_name, $i )
195 Function: Specialist function to index report file(s).
196 Is provided with a filename and an integer
197 by make_index in its SUPER class.
198 Example :
199 Returns :
200 Args :
201  
202 =cut
203  
204 1 ▶sub _index_file {
Subroutine _index_file: called CC=8 SCAR=20.9
205 1 my( $self,
206 $file, # File name
207 $i # Index-number of file being indexed
208 ) = @_;
209  
210 1 my $begin = 0;
211  
212 1 my $id_parser = $self->id_parser;
213  
214 1 ▶ open my $STOCKHOLM, '<', $file or $self->throw("Could not read file '$file': $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $STOCKHOLM, "<", $file or $self->throw("Could not read file '${file}': $!") 1 0 1
215  
216 1 my %done_ids;
217  
218 # In Windows, text files have '\r\n' as line separator, but when reading in
219 # text mode Perl will only show the '\n'. This means that for a line "ABC\r\n",
220 # "length $_" will report 4 although the line is 5 bytes in length.
221 # We assume that all lines have the same line separator and only read current line.
222 1 my $init_pos = tell($STOCKHOLM);
223 1 my $curr_line = <$STOCKHOLM>;
224 1 my $pos_diff = tell($STOCKHOLM) - $init_pos;
225 1 my $correction = $pos_diff - length $curr_line;
226 1 seek $STOCKHOLM, $init_pos, 0; # Rewind position to proceed to read the file
227  
228 1 ▶ while (<$STOCKHOLM>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $STOCKHOLM)) 264 1 265
229 264 ▶ if ( /^#\sSTOCKHOLM/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^#\sSTOCKHOLM/) 3 261 264
230 3 $begin = tell($STOCKHOLM) - length($_) - $correction;
231 }
232  
233 264 for my $id ( &$id_parser($_) ) {
234 3 ▶ next if exists $done_ids{$id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $done_ids{$id} 0 3 3
235 3 ▶ $self->add_record($id, $i, $begin) if $id;
Branch: 1 branch
BranchTrueFalseTotal
if $id 3 0 3
236 3 $done_ids{$id} = 1;
237 }
238  
239 264 ▶ %done_ids = () if ( m{//} );
Branch: 1 branch
BranchTrueFalseTotal
if m[//] 3 261 264
240 }
241 1 close $STOCKHOLM;
242  
243 1 1;
244 }
245  
246  
247 =head2 id_parser
248  
249 Title : id_parser
250 Usage : $index->id_parser( CODE )
251 Function: Stores or returns the code used by record_id to
252 parse the ID for record from a string.
253 Returns \&default_id_parser (see below) if not
254 set. An entry will be added to
255 the index for each string in the list returned.
256 Example : $index->id_parser( \&my_id_parser )
257 Returns : reference to CODE if called without arguments
258 Args : CODE
259  
260 =cut
261  
262 1 ▶sub id_parser {
Subroutine id_parser: called CC=3 SCAR=12.5
Pod documented
263 1 my ($self,$code) = @_;
264  
265 1 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 0 1 1
266 0 $self->{'_id_parser'} = $code;
267 }
268 1 ▶ return $self->{'_id_parser'} || \&default_id_parser;
Condition50%1/2
$self->{'_id_parser'} || \&Bio::Index::Stockholm::default_id_parser
l!l
01
MC/DC0%0/1
$self->{'_id_parser'} || \&Bio::Index::Stockholm::default_id_parser
$self->{'_id_parser'}
Truth table50%1/2
A
A= $self->{'_id_parser'}
Aresult
11
00
269 }
270  
271 =head2 default_id_parser
272  
273 Title : default_id_parser
274 Usage : $id = default_id_parser($line)
275 Function: The default parser for Stockholm.pm
276 Returns : Array of specified ids
277 Args : a line string
278  
279 =cut
280  
281 264 ▶sub default_id_parser {
Subroutine default_id_parser: called CC=2 SCAR=6.9
Pod documented
282 264 my $line = shift;
283 264 my %accs;
284  
285 264 ▶ if ( $line =~ /^#=GF AC\s+(\S+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#=GF AC\s+(\S+)/) 3 261 264
286 3 $accs{$1}++;
287 }
288 264 keys %accs;
289 }
290  
291 =head2 Bio::Index::Abstract methods
292  
293 =cut
294  
295 =head2 filename
296  
297 Title : filename
298 Usage : $value = $self->filename();
299 $self->filename($value);
300 Function: Gets or sets the name of the dbm index file.
301 Returns : The current value of filename
302 Args : Value of filename if setting, or none if
303 getting the value.
304  
305 =head2 write_flag
306  
307 Title : write_flag
308 Usage : $value = $self->write_flag();
309 $self->write_flag($value);
310 Function: Gets or sets the value of write_flag, which
311 is whether the dbm file should be opened with
312 write access.
313 Returns : The current value of write_flag (default 0)
314 Args : Value of write_flag if setting, or none if
315 getting the value.
316  
317 =head2 dbm_package
318  
319 Usage : $value = $self->dbm_package();
320 $self->dbm_package($value);
321  
322 Function: Gets or sets the name of the Perl dbm module used.
323 If the value is unset, then it returns the value of
324 the package variable $USE_DBM_TYPE or if that is
325 unset, then it chooses the best available dbm type,
326 choosing 'DB_File' in preference to 'SDBM_File'.
327 Bio::Abstract::Index may work with other dbm file
328 types.
329  
330 Returns : The current value of dbm_package
331 Args : Value of dbm_package if setting, or none if
332 getting the value.
333  
334  
335 =head2 get_stream
336  
337 Title : get_stream
338 Usage : $stream = $index->get_stream( $id );
339 Function: Returns a file handle with the file pointer
340 at the approprite place
341  
342 This provides for a way to get the actual
343 file contents and not an object
344  
345 WARNING: you must parse the record deliminter
346 *yourself*. Abstract won't do this for you
347 So this code
348  
349 $fh = $index->get_stream($myid);
350 while( <$fh> ) {
351 # do something
352 }
353 will parse the entire file if you do not put in
354 a last statement in, like
355  
356 while( <$fh> ) {
357 /^\/\// && last; # end of record
358 # do something
359 }
360  
361 Returns : A filehandle object
362 Args : string represents the accession number
363 Notes : This method should not be used without forethought
364  
365  
366 =head2 open_dbm
367  
368 Usage : $index->open_dbm()
369 Function: Opens the dbm file associated with the index
370 object. Write access is only given if explicitly
371 asked for by calling new(-write => 1) or having set
372 the write_flag(1) on the index object. The type of
373 dbm file opened is that returned by dbm_package().
374 The name of the file to be is opened is obtained by
375 calling the filename() method.
376  
377 Example : $index->_open_dbm()
378 Returns : 1 on success
379  
380  
381 =head2 _version
382  
383 Title : _version
384 Usage : $type = $index->_version()
385 Function: Returns a string which identifes the version of an
386 index module. Used to permanently identify an index
387 file as having been created by a particular version
388 of the index module. Must be provided by the sub class
389 Example :
390 Returns :
391 Args : none
392  
393 =head2 _filename
394  
395 Title : _filename
396 Usage : $index->_filename( FILE INT )
397 Function: Indexes the file
398 Example :
399 Returns :
400 Args :
401  
402 =head2 _file_handle
403  
404 Title : _file_handle
405 Usage : $fh = $index->_file_handle( INT )
406 Function: Returns an open filehandle for the file
407 index INT. On opening a new filehandle it
408 caches it in the @{$index->_filehandle} array.
409 If the requested filehandle is already open,
410 it simply returns it from the array.
411 Example : $fist_file_indexed = $index->_file_handle( 0 );
412 Returns : ref to a filehandle
413 Args : INT
414  
415 =head2 _file_count
416  
417 Title : _file_count
418 Usage : $index->_file_count( INT )
419 Function: Used by the index building sub in a sub class to
420 track the number of files indexed. Sets or gets
421 the number of files indexed when called with or
422 without an argument.
423 Example :
424 Returns : INT
425 Args : INT
426  
427  
428 =head2 add_record
429  
430 Title : add_record
431 Usage : $index->add_record( $id, @stuff );
432 Function: Calls pack_record on @stuff, and adds the result
433 of pack_record to the index database under key $id.
434 If $id is a reference to an array, then a new entry
435 is added under a key corresponding to each element
436 of the array.
437 Example : $index->add_record( $id, $fileNumber, $begin, $end )
438 Returns : TRUE on success or FALSE on failure
439 Args : ID LIST
440  
441 =head2 pack_record
442  
443 Title : pack_record
444 Usage : $packed_string = $index->pack_record( LIST )
445 Function: Packs an array of scalars into a single string
446 joined by ASCII 034 (which is unlikely to be used
447 in any of the strings), and returns it.
448 Example : $packed_string = $index->pack_record( $fileNumber, $begin, $end )
449 Returns : STRING or undef
450 Args : LIST
451  
452 =head2 unpack_record
453  
454 Title : unpack_record
455 Usage : $index->unpack_record( STRING )
456 Function: Splits the sting provided into an array,
457 splitting on ASCII 034.
458 Example : ( $fileNumber, $begin, $end ) = $index->unpack_record( $self->db->{$id} )
459 Returns : A 3 element ARRAY
460 Args : STRING containing ASCII 034
461  
462 =head2 DESTROY
463  
464 Title : DESTROY
465 Usage : Called automatically when index goes out of scope
466 Function: Closes connection to database and handles to
467 sequence files
468 Returns : NEVER
469 Args : NONE
470  
471  
472 =cut
473  
474 2 1;
« AlignIO/stockholm.pm ↑ Summary