Bio/DB/IndexedBase.pm

filter: Statementstmt 89.8% 293 / 326 Branchbran 67.9% 91 / 134 Conditioncond 63.0% 41 / 65 MC/DCmcdc 28.5% 10 / 35 Subroutinesub 86.4% 64 / 74 Podpod 100% 20 / 20 totaltotal 79.3% 519 / 654 CC 97 SCAR 18.0
CC
97
Cov
81%
new
16.9
_index_files
14.5
linelen
12.0
CRAP 6.1
« Bio/Tools/IUPAC.pm ↑ Summary Bio/Seq/RichSeq.pm »
1 #
2 # BioPerl module for Bio::DB::IndexedBase
3 #
4 # You may distribute this module under the same terms as perl itself
5 #
6  
7 =head1 NAME
8  
9 Bio::DB::IndexedBase - Base class for modules using indexed sequence files
10  
11 =head1 SYNOPSIS
12  
13 use Bio::DB::XXX; # a made-up class that uses Bio::IndexedBase
14  
15 # 1/ Bio::SeqIO-style access
16  
17 # Index some sequence files
18 my $db = Bio::DB::XXX->new('/path/to/file'); # from a single file
19 my $db = Bio::DB::XXX->new(['file1', 'file2']); # from multiple files
20 my $db = Bio::DB::XXX->new('/path/to/files/'); # from a directory
21  
22 # Get IDs of all the sequences in the database
23 my @ids = $db->get_all_primary_ids;
24  
25 # Get a specific sequence
26 my $seq = $db->get_Seq_by_id('CHROMOSOME_I');
27  
28 # Loop through all sequences
29 my $stream = $db->get_PrimarySeq_stream;
30 while (my $seq = $stream->next_seq) {
31 # Do something...
32 }
33  
34  
35 # 2/ Access via filehandle
36 my $fh = Bio::DB::XXX->newFh('/path/to/file');
37 while (my $seq = <$fh>) {
38 # Do something...
39 }
40  
41  
42 # 3/ Tied-hash access
43 tie %sequences, 'Bio::DB::XXX', '/path/to/file';
44 print $sequences{'CHROMOSOME_I:1,20000'};
45  
46 =head1 DESCRIPTION
47  
48 Bio::DB::IndexedBase provides a base class for modules that want to index
49 and read sequence files and provides persistent, random access to each sequence
50 entry, without bringing the entire file into memory. This module is compliant
51 with the Bio::SeqI interface and both. Bio::DB::Fasta and Bio::DB::Qual both use
52 Bio::DB::IndexedBase.
53  
54 When you initialize the module, you point it at a single file, several files, or
55 a directory of files. The first time it is run, the module generates an index
56 of the content of the files using the AnyDBM_File module (BerkeleyDB preferred,
57 followed by GDBM_File, NDBM_File, and SDBM_File). Subsequently, it uses the
58 index file to find the sequence file and offset for any requested sequence. If
59 one of the source files is updated, the module reindexes just that one file. You
60 can also force reindexing manually at any time. For improved performance, the
61 module keeps a cache of open filehandles, closing less-recently used ones when
62 the cache is full.
63  
64 Entries may have any line length up to 65,536 characters, and different line
65 lengths are allowed in the same file. However, within a sequence entry, all
66 lines must be the same length except for the last. An error will be thrown if
67 this is not the case!
68  
69 This module was developed for use with the C. elegans and human genomes, and has
70 been tested with sequence segments as large as 20 megabases. Indexing the C.
71 elegans genome (100 megabases of genomic sequence plus 100,000 ESTs) takes ~5
72 minutes on my 300 MHz pentium laptop. On the same system, average access time
73 for any 200-mer within the C. elegans genome was E<lt>0.02s.
74  
75 =head1 DATABASE CREATION AND INDEXING
76  
77 The two constructors for this class are new() and newFh(). The former creates a
78 Bio::DB::IndexedBase object which is accessed via method calls. The latter
79 creates a tied filehandle which can be used Bio::SeqIO style to fetch sequence
80 objects in a stream fashion. There is also a tied hash interface.
81  
82 =over
83  
84 =item $db = Bio::DB::IndexedBase-E<gt>new($path [,%options])
85  
86 Create a new Bio::DB::IndexedBase object from the files designated by $path
87 $path may be a single file, an arrayref of files, or a directory containing
88 such files.
89  
90 After the database is created, you can use methods like get_all_primary_ids()
91 and get_Seq_by_id() to retrieve sequence objects.
92  
93 =item $fh = Bio::DB::IndexedBase-E<gt>newFh($path [,%options])
94  
95 Create a tied filehandle opened on a Bio::DB::IndexedBase object. Reading
96 from this filehandle with E<lt>E<gt> will return a stream of sequence objects,
97 Bio::SeqIO style. The path and the options should be specified as for new().
98  
99 =item $obj = tie %db,'Bio::DB::IndexedBase', '/path/to/file' [,@args]
100  
101 Create a tied-hash by tieing %db to Bio::DB::IndexedBase using the indicated
102 path to the files. The optional @args list is the same set used by new(). If
103 successful, tie() returns the tied object, undef otherwise.
104  
105 Once tied, you can use the hash to retrieve an individual sequence by
106 its ID, like this:
107  
108 my $seq = $db{CHROMOSOME_I};
109  
110 The keys() and values() functions will return the sequence IDs and their
111 sequences, respectively. In addition, each() can be used to iterate over the
112 entire data set:
113  
114 while (my ($id,$sequence) = each %db) {
115 print "$id => $sequence\n";
116 }
117  
118  
119 When dealing with very large sequences, you can avoid bringing them into memory
120 by calling each() in a scalar context. This returns the key only. You can then
121 use tied(%db) to recover the Bio::DB::IndexedBase object and call its methods.
122  
123 while (my $id = each %db) {
124 print "$id: $db{$sequence:1,100}\n";
125 print "$id: ".tied(%db)->length($id)."\n";
126 }
127  
128 In addition, you may invoke the FIRSTKEY and NEXTKEY tied hash methods directly
129 to retrieve the first and next ID in the database, respectively. This allows one to
130 write the following iterative loop using just the object-oriented interface:
131  
132 my $db = Bio::DB::IndexedBase->new('/path/to/file');
133 for (my $id=$db->FIRSTKEY; $id; $id=$db->NEXTKEY($id)) {
134 # do something with sequence
135 }
136  
137 =back
138  
139 =head1 INDEX CONTENT
140  
141 Several attributes of each sequence are stored in the index file. Given a
142 sequence ID, these attributes can be retrieved using the following methods:
143  
144 =over
145  
146 =item offset($id)
147  
148 Get the offset of the indicated sequence from the beginning of the file in which
149 it is located. The offset points to the beginning of the sequence, not the
150 beginning of the header line.
151  
152 =item strlen($id)
153  
154 Get the number of characters in the sequence string.
155  
156 =item length($id)
157  
158 Get the number of residues of the sequence.
159  
160 =item linelen($id)
161  
162 Get the length of the line for this sequence. If the sequence is wrapped, then
163 linelen() is likely to be much shorter than strlen().
164  
165 =item headerlen($id)
166  
167 Get the length of the header line for the indicated sequence.
168  
169 =item header_offset
170  
171 Get the offset of the header line for the indicated sequence from the beginning
172 of the file in which it is located. This attribute is not stored. It is
173 calculated from offset() and headerlen().
174  
175 =item alphabet($id)
176  
177 Get the molecular type (alphabet) of the indicated sequence. This method handles
178 residues according to the IUPAC convention.
179  
180 =item file($id)
181  
182 Get the the name of the file in which the indicated sequence can be found.
183  
184 =back
185  
186 =head1 INTERFACE COMPLIANCE NOTES
187  
188 Bio::DB::IndexedBase is compliant with the Bio::DB::SeqI and hence with the
189 Bio::RandomAccessI interfaces.
190  
191 Database do not necessarily provide any meaningful internal primary ID for the
192 sequences they store. However, Bio::DB::IndexedBase's internal primary IDs are
193 the IDs of the sequences. This means that the same ID passed to get_Seq_by_id()
194 and get_Seq_by_primary_id() will return the same sequence.
195  
196 Since this database index has no notion of sequence version or namespace, the
197 get_Seq_by_id(), get_Seq_by_acc() and get_Seq_by_version() are identical.
198  
199 =head1 BUGS
200  
201 When a sequence is deleted from one of the files, this deletion is not detected
202 by the module and removed from the index. As a result, a "ghost" entry will
203 remain in the index and will return garbage results if accessed.
204  
205 Also, if you are indexing a directory, it is wise to not add or remove files
206 from it.
207  
208 In case you have changed the files in a directory, or the sequences in a file,
209 you can to rebuild the entire index, either by deleting it manually, or by
210 passing -reindex=E<gt>1 to new() when initializing the module.
211  
212 =head1 SEE ALSO
213  
214 L<DB_File>
215  
216 L<Bio::DB::Fasta>
217  
218 L<Bio::DB::Qual>
219  
220 =head1 AUTHOR
221  
222 Lincoln Stein E<lt>lstein@cshl.orgE<gt>.
223  
224 Copyright (c) 2001 Cold Spring Harbor Laboratory.
225  
226 Florent Angly (for the modularization)
227  
228 This library is free software; you can redistribute it and/or modify
229 it under the same terms as Perl itself. See DISCLAIMER.txt for
230 disclaimers of warranty.
231  
232 =head1 APPENDIX
233  
234 The rest of the documentation details each of the object
235 methods. Internal methods are usually preceded with a _
236  
237 =cut
238  
239  
240 package Bio::DB::IndexedBase;
241 5 $Bio::DB::IndexedBase::VERSION = '1.7.8';
242  
243 5 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
244 @AnyDBM_File::ISA = qw(DB_File GDBM_File NDBM_File SDBM_File)
245 5 ▶ if(!$INC{'AnyDBM_File.pm'});
Branch: 1 branch
BranchTrueFalseTotal
if not $INC{"AnyDBM_File.pm"} 5 0 5
246 }
247  
248 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
249 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
250 5 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
251 5 ▶use AnyDBM_File;
Subroutine BEGIN: called CC=1 SCAR=0.0
252 5 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
253 5 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
254 5 ▶use File::Basename qw(basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
255 5 ▶use Bio::PrimarySeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
256  
257 5 ▶use base qw(Bio::DB::SeqI);
Subroutine BEGIN: called CC=1 SCAR=0.0
258  
259 # Store offset, strlen, linelen, headerlen, type and fileno
260 5 ▶use constant STRUCT => 'NNNnnCa*'; # 32-bit file offset and seq length
Subroutine BEGIN: called CC=1 SCAR=0.0
261 5 ▶use constant STRUCTBIG => 'QQQnnCa*'; # 64-bit
Subroutine BEGIN: called CC=1 SCAR=0.0
262  
263 5 ▶use constant NA => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
264 5 ▶use constant DNA => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
265 5 ▶use constant RNA => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
266 5 ▶use constant PROTEIN => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
267  
268 # You can avoid dying if you want but you may get incorrect results
269 5 ▶use constant DIE_ON_MISSMATCHED_LINES => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
270  
271 =head2 new
272  
273 Title : new
274 Usage : my $db = Bio::DB::IndexedBase->new($path, -reindex => 1);
275 Function: Initialize a new database object
276 Returns : A Bio::DB::IndexedBase object
277 Args : A single file, or path to dir, or arrayref of files
278 Optional arguments:
279  
280 Option Description Default
281 ----------- ----------- -------
282 -glob Glob expression to search for files in directories *
283 -makeid A code subroutine for transforming IDs None
284 -maxopen Maximum size of filehandle cache 32
285 -debug Turn on status messages 0
286 -reindex Force the index to be rebuilt 0
287 -dbmargs Additional arguments to pass to the DBM routine None
288 -index_name Name of the file that will hold the indices
289 -clean Remove the index file when finished 0
290  
291 The -dbmargs option can be used to control the format of the index. For example,
292 you can pass $DB_BTREE to this argument so as to force the IDs to be sorted and
293 retrieved alphabetically. Note that you must use the same arguments every time
294 you open the index!
295  
296 The -makeid option gives you a chance to modify sequence IDs during indexing.
297 For example, you may wish to extract a portion of the gi|gb|abc|xyz nonsense
298 that GenBank Fasta files use. The original header line can be recovered later.
299 The option value for -makeid should be a code reference that takes a scalar
300 argument (the full header line) and returns a scalar or an array of scalars (the
301 ID or IDs you want to assign). For example:
302  
303 $db = Bio::DB::IndexedBase->new('file.fa', -makeid => \&extract_gi);
304  
305 sub extract_gi {
306 # Extract GI from GenBank
307 my $header = shift;
308 my ($id) = ($header =~ /gi\|(\d+)/m);
309 return $id || '';
310 }
311  
312 extract_gi() will be called with the full header line, e.g. a Fasta line would
313 include the "E<gt>", the ID and the description:
314  
315 >gi|352962132|ref|NG_030353.1| Homo sapiens sal-like 3 (Drosophila) (SALL3)
316  
317 In the database, this sequence can now be retrieved by its GI instead of its
318 complete ID:
319  
320 my $seq = $db->get_Seq_by_id(352962132);
321  
322 The -makeid option is ignored after the index is constructed.
323  
324 =cut
325  
326 18 ▶sub new {
Subroutine new: called CC=14 SCAR=28.3
Pod documented
327 18 my ($class, $path, %opts) = @_;
328  
329 my $self = bless {
330 debug => $opts{-debug} || 0,
331 makeid => $opts{-makeid},
332 glob => $opts{-glob} || eval '$'.$class.'::file_glob' || '*',
333 maxopen => $opts{-maxopen} || 32,
334 clean => $opts{-clean} || 0,
335 dbmargs => $opts{-dbmargs} || undef,
336 fhcache => {},
337 cacheseq => {},
338 curopen => 0,
339 openseq => 1,
340 dirname => undef,
341 offsets => undef,
342 index_name => $opts{-index_name},
343 obj_class => eval '$'.$class.'::obj_class',
344 18 ▶ offset_meth => \&{$class.'::_calculate_offsets'},
Condition53%7/13
$opts{'-debug'} || 0
l!l
018
$opts{'-glob'} || eval '$' . $class . '::file_glob' || '*'
l!l
180
$opts{"-glob"} or eval '$' . $class . '::file_glob'
l!l&&r!l&&!r
0180
$opts{'-maxopen'} || 32
l!l
018
$opts{'-clean'} || 0
l!l
315
$opts{'-dbmargs'} || undef
l!l
018
MC/DC0%0/1
$opts{'-debug'} || 0
$opts{'-debug'}
MC/DC0%0/2
$opts{'-glob'} || eval '$' . $class . '::file_glob' || '*'
$opts{"-glob"} eval '$' . $class . '::file_glob'
MC/DC0%0/1
$opts{'-maxopen'} || 32
$opts{'-maxopen'}
MC/DC100%1/1
$opts{'-clean'} || 0
$opts{'-clean'}
MC/DC0%0/1
$opts{'-dbmargs'} || undef
$opts{'-dbmargs'}
Truth table50%1/2
A
A= $opts{'-debug'}
Aresult
11
00
Truth table33%1/3
A or B
A= $opts{"-glob"}B= eval '$' . $class . '::file_glob'
ABresult
1X1
011
000
Truth table50%1/2
A
A= $opts{'-maxopen'}
Aresult
11
00
Truth table100%2/2
A
A= $opts{'-clean'}
Aresult
11
00
Truth table50%1/2
A
A= $opts{'-dbmargs'}
Aresult
11
00
345 fileno2path => [],
346 filepath2no => {},
347 }, $class;
348  
349 18 my ($offsets, $dirname);
350 18 ▶ my $ref = ref $path || '';
Condition100%2/2
ref $path || ''
l!l
117
MC/DC100%1/1
ref $path || ''
ref $path
Truth table100%2/2
A
A= ref $path
Aresult
11
00
351 18 ▶ if ( $ref eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 1 17 18
352 1 $offsets = $self->index_files($path, $opts{-reindex});
353 1 require Cwd;
354 1 $dirname = Cwd::getcwd();
355 } else {
356 17 ▶ $self->{index_name} ||= $self->_default_index_name($path);
Condition66%2/3
$self->{"index_name"} ||= $self->_default_index_name($path)
l!l&&r!l&&!r
1160
MC/DC0%0/2
$self->{"index_name"} ||= $self->_default_index_name($path)
$self->{"index_name"} $self->_default_index_name($path)
Truth table66%2/3
A ||= B
A= $self->{"index_name"}B= $self->_default_index_name($path)
ABresult
1X1
011
000
357 17 ▶ if (-d $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $path) { } 9 8 17
elsif (-f _) { } 8 0 8
358 # because Win32 glob() is broken with respect to long file names
359 # that contain whitespace.
360 9 ▶ $path = Win32::GetShortPathName($path)
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /^MSWin/i and eval "use Win32; 1" 0 9 9
Condition33%1/3
$^O =~ /^MSWin/i and eval "use Win32; 1"
!ll&&!rl&&r
900
MC/DC0%0/2
$^O =~ /^MSWin/i and eval "use Win32; 1"
$^O =~ /^MSWin/i eval "use Win32; 1"
Truth table33%1/3
A and B
A= $^O =~ /^MSWin/iB= eval "use Win32; 1"
ABresult
0X0
100
111
361 if $^O =~ /^MSWin/i && eval 'use Win32; 1';
362 9 $offsets = $self->index_dir($path, $opts{-reindex});
363 8 $dirname = $path;
364 } elsif (-f _) {
365 8 $offsets = $self->index_file($path, $opts{-reindex});
366 8 $dirname = dirname($path);
367 } else {
368 0 $self->throw( "No file or directory called '$path'");
369 }
370 }
371 17 @{$self}{qw(dirname offsets)} = ($dirname, $offsets);
372  
373 17 return $self;
374 }
375  
376  
377 =head2 newFh
378  
379 Title : newFh
380 Usage : my $fh = Bio::DB::IndexedBase->newFh('/path/to/files/', %options);
381 Function: Index and get a new Fh for a single file, several files or a directory
382 Returns : Filehandle object
383 Args : Same as new()
384  
385 =cut
386  
387 1 ▶sub newFh {
Subroutine newFh: called CC=2 SCAR=7.0
Pod documented
388 1 my ($class, @args) = @_;
389 1 my $self = $class->new(@args);
390 1 require Symbol;
391 1 my $fh = Symbol::gensym;
392 1 ▶ tie $$fh, 'Bio::DB::Indexed::Stream', $self
Branch: 1 branch
BranchTrueFalseTotal
tie $$fh, "Bio::DB::Indexed::Stream", $self or $self->throw("Could not tie filehandle: $!") 1 0 1
393 or $self->throw("Could not tie filehandle: $!");
394 1 return $fh;
395 }
396  
397  
398 =head2 dbmargs
399  
400 Title : dbmargs
401 Usage : my @args = $db->dbmargs;
402 Function: Get stored dbm arguments
403 Returns : Array
404 Args : None
405  
406 =cut
407  
408 35 ▶sub dbmargs {
Subroutine dbmargs: called CC=3 SCAR=15.4
Pod documented
409 35 my $self = shift;
410 35 ▶ my $args = $self->{dbmargs} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $args = $self->{"dbmargs"} or return 0 35 35
411 0 ▶ return ref($args) eq 'ARRAY' ? @$args : $args;
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'ARRAY' ? : 0 0 0
412 }
413  
414  
415 =head2 glob
416  
417 Title : glob
418 Usage : my $glob = $db->glob;
419 Function: Get the expression used to match files in directories
420 Returns : String
421 Args : None
422  
423 =cut
424  
425 2 ▶sub glob {
Subroutine glob: called CC=1 SCAR=0.0
Pod documented
426 2 my $self = shift;
427 2 return $self->{glob};
428 }
429  
430  
431 =head2 index_dir
432  
433 Title : index_dir
434 Usage : $db->index_dir($dir);
435 Function: Index the files that match -glob in the given directory
436 Returns : Hashref of offsets
437 Args : Dirname
438 Boolean to force a reindexing the directory
439  
440 =cut
441  
442 9 ▶sub index_dir {
Subroutine index_dir: called CC=3 SCAR=11.3
Pod documented
443 9 my ($self, $dir, $force_reindex) = @_;
444 5 ▶ my @files = glob( File::Spec->catfile($dir, $self->{glob}) );
Subroutine BEGIN: called CC=1 SCAR=0.0
445 9 ▶ return if scalar @files == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @files == 0 0 9 9
446 9 ▶ $self->{index_name} ||= $self->_default_index_name($dir);
Condition33%1/3
$self->{"index_name"} ||= $self->_default_index_name($dir)
l!l&&r!l&&!r
900
MC/DC0%0/2
$self->{"index_name"} ||= $self->_default_index_name($dir)
$self->{"index_name"} $self->_default_index_name($dir)
Truth table33%1/3
A ||= B
A= $self->{"index_name"}B= $self->_default_index_name($dir)
ABresult
1X1
011
000
447 9 my $offsets = $self->_index_files(\@files, $force_reindex);
448 8 return $offsets;
449 }
450  
451  
452 =head2 get_all_primary_ids
453  
454 Title : get_all_primary_ids, get_all_ids, ids
455 Usage : my @ids = $db->get_all_primary_ids;
456 Function: Get the IDs stored in all indexes. This is a Bio::DB::SeqI method
457 implementation. Note that in this implementation, the internal
458 database primary IDs are also the sequence IDs.
459 Returns : List of ids
460 Args : None
461  
462 =cut
463  
464 6 ▶sub get_all_primary_ids {
Subroutine get_all_primary_ids: called CC=1 SCAR=0.0
Pod documented
465 6 return keys %{shift->{offsets}};
466 }
467  
468 {
469 5 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
470 5 *ids = *get_all_ids = \&get_all_primary_ids;
471 }
472  
473  
474 =head2 index_file
475  
476 Title : index_file
477 Usage : $db->index_file($filename);
478 Function: Index the given file
479 Returns : Hashref of offsets
480 Args : Filename
481 Boolean to force reindexing the file
482  
483 =cut
484  
485 8 ▶sub index_file {
Subroutine index_file: called CC=2 SCAR=7.4
Pod documented
486 8 my ($self, $file, $force_reindex) = @_;
487 8 ▶ $self->{index_name} ||= $self->_default_index_name($file);
Condition33%1/3
$self->{"index_name"} ||= $self->_default_index_name($file)
l!l&&r!l&&!r
800
MC/DC0%0/2
$self->{"index_name"} ||= $self->_default_index_name($file)
$self->{"index_name"} $self->_default_index_name($file)
Truth table33%1/3
A ||= B
A= $self->{"index_name"}B= $self->_default_index_name($file)
ABresult
1X1
011
000
488 8 my $offsets = $self->_index_files([$file], $force_reindex);
489 8 return $offsets;
490 }
491  
492 16 ▶sub _default_index_name {
Subroutine _default_index_name: called CC=2 SCAR=6.9
493 16 my ($self,$path) = @_;
494 16 ▶ return File::Spec->catfile($path,'directory.index') if -d $path;
Branch: 1 branch
BranchTrueFalseTotal
if -d $path 9 7 16
495 7 return "$path.index";
496 }
497  
498 =head2 index_files
499  
500 Title : index_files
501 Usage : $db->index_files(\@files);
502 Function: Index the given files
503 Returns : Hashref of offsets
504 Args : Arrayref of filenames
505 Boolean to force reindexing the files
506  
507 =cut
508  
509 1 ▶sub index_files {
Subroutine index_files: called CC=2 SCAR=7.0
Pod documented
510 1 my ($self, $files, $force_reindex) = @_;
511 1 my @paths = map { File::Spec->rel2abs($_) } @$files;
512 1 require Digest::MD5;
513 1 my $digest = Digest::MD5::md5_hex( join('', sort @paths) );
514 1 ▶ $self->{index_name} ||= "fileset_$digest.index"; # unique name for the given files
Condition50%1/2
$self->{"index_name"} ||= "fileset_$digest.index"
l!l
01
MC/DC0%0/1
$self->{"index_name"} ||= "fileset_$digest.index"
$self->{"index_name"}
Truth table50%1/2
A
A= $self->{"index_name"}
Aresult
11
00
515 1 my $offsets = $self->_index_files($files, $force_reindex);
516 1 return $offsets;
517 }
518  
519  
520 =head2 index_name
521  
522 Title : index_name
523 Usage : my $indexname = $db->index_name($path);
524 Function: Get the full name of the index file
525 Returns : String
526 Args : None
527  
528 =cut
529  
530 36 ▶sub index_name {
Subroutine index_name: called CC=1 SCAR=0.0
Pod documented
531 36 return shift->{index_name};
532 }
533  
534  
535 =head2 path
536  
537 Title : path
538 Usage : my $path = $db->path($path);
539 Function: When a single file or a directory of files is indexed, this returns
540 the file directory. When indexing an arbitrary list of files, the
541 return value is the path of the current working directory.
542 Returns : String
543 Args : None
544  
545 =cut
546  
547 0 ▶sub path {
Subroutine path: not called CC=1 SCAR=6.9
Pod documented
548 0 return shift->{dirname};
549 }
550  
551  
552 =head2 get_PrimarySeq_stream
553  
554 Title : get_PrimarySeq_stream
555 Usage : my $stream = $db->get_PrimarySeq_stream();
556 Function: Get a SeqIO-like stream of sequence objects. The stream supports a
557 single method, next_seq(). Each call to next_seq() returns a new
558 PrimarySeqI compliant sequence object, until no more sequences remain.
559 This is a Bio::DB::SeqI method implementation.
560 Returns : A Bio::DB::Indexed::Stream object
561 Args : None
562  
563 =cut
564  
565 3 ▶sub get_PrimarySeq_stream {
Subroutine get_PrimarySeq_stream: called CC=1 SCAR=0.0
Pod documented
566 3 my $self = shift;
567 3 return Bio::DB::Indexed::Stream->new($self);
568 }
569  
570  
571 =head2 get_Seq_by_id
572  
573 Title : get_Seq_by_id, get_Seq_by_acc, get_Seq_by_version, get_Seq_by_primary_id
574 Usage : my $seq = $db->get_Seq_by_id($id);
575 Function: Given an ID, fetch the corresponding sequence from the database.
576 This is a Bio::DB::SeqI and Bio::DB::RandomAccessI method implementation.
577 Returns : A sequence object
578 Args : ID
579  
580 =cut
581  
582 22 ▶sub get_Seq_by_id {
Subroutine get_Seq_by_id: called CC=3 SCAR=11.0
Pod documented
583 22 my ($self, $id) = @_;
584 22 ▶ $self->throw('Need to provide a sequence ID') if not defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 22 22
585 22 ▶ return if not exists $self->{offsets}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"offsets"}{$id} 4 18 22
586 18 return $self->{obj_class}->new($self, $id);
587 }
588  
589 {
590 5 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
591 5 *get_Seq_by_version = *get_Seq_by_primary_id = *get_Seq_by_acc = \&get_Seq_by_id;
592 }
593  
594  
595 =head2 _calculate_offsets
596  
597 Title : _calculate_offsets
598 Usage : $db->_calculate_offsets($filename, $offsets);
599 Function: This method calculates the sequence offsets in a file based on ID and
600 should be implemented by classes that use Bio::DB::IndexedBase.
601 Returns : Hash of offsets
602 Args : File to process
603 Hashref of file offsets keyed by IDs.
604  
605 =cut
606  
607 0 ▶sub _calculate_offsets {
Subroutine _calculate_offsets: not called CC=1 SCAR=6.9
608 0 my $self = shift;
609 0 $self->throw_not_implemented();
610 }
611  
612  
613 18 ▶sub _index_files {
Subroutine _index_files: called CC=14 SCAR=26.7
614 # Do the indexing of the given files using the index file on record
615 18 my ($self, $files, $force_reindex) = @_;
616  
617 18 $self->_set_pack_method( @$files );
618  
619 # Get name of index file
620 18 my $index = $self->index_name;
621  
622 # If caller has requested reindexing, unlink the index file.
623 18 ▶ if ($force_reindex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_reindex) 10 8 18
624 # Tied-hash in Strawberry Perl creates "$file.index"
625 10 ▶ unlink $index if -e $index;
Branch: 1 branch
BranchTrueFalseTotal
if -e $index 0 10 10
626 # Tied-hash in ActivePerl creates "$file.index.pag" and "$file.index.dir"
627 10 ▶ unlink "$index.dir" if -e "$index.dir";
Branch: 1 branch
BranchTrueFalseTotal
if -e "$index.dir" 2 8 10
628 10 ▶ unlink "$index.pag" if -e "$index.pag";
Branch: 1 branch
BranchTrueFalseTotal
if -e "$index.pag" 2 8 10
629 }
630  
631 # Get the modification time of the index
632 18 ▶ my $indextime = (stat $index)[9] || 0;
Condition50%1/2
(stat $index)[9] || 0
l!l
018
MC/DC0%0/1
(stat $index)[9] || 0
(stat $index)[9]
Truth table50%1/2
A
A= (stat $index)[9]
Aresult
11
00
633  
634 # Register files and find if there has been any update
635 18 my $modtime = 0;
636 18 my @updated;
637 18 for my $file (@$files) {
638 # Register file
639 61 $self->_path2fileno(basename($file));
640 # Any update?
641 61 ▶ my $m = (stat $file)[9] || 0;
Condition50%1/2
(stat $file)[9] || 0
l!l
610
MC/DC0%0/1
(stat $file)[9] || 0
(stat $file)[9]
Truth table50%1/2
A
A= (stat $file)[9]
Aresult
11
00
642 61 ▶ if ($m > $modtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m > $modtime) 18 43 61
643 18 $modtime = $m;
644 }
645 61 ▶ if ($m > $indextime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m > $indextime) 61 0 61
646 61 push @updated, $file;
647 }
648 }
649  
650 # Get termination length from first file
651 18 $self->{termination_length} = $self->_calc_termination_length( $files->[0] );
652  
653 # Reindex contents of changed files if needed
654 18 ▶ my $reindex = $force_reindex || (scalar @updated > 0);
Condition66%2/3
$force_reindex || scalar @updated > 0
l!l&&r!l&&!r
1080
MC/DC0%0/2
$force_reindex || scalar @updated > 0
$force_reindex scalar @updated > 0
Truth table66%2/3
A || B
A= $force_reindexB= scalar @updated > 0
ABresult
1X1
011
000
655 18 ▶ $self->{offsets} = $self->_open_index($index, $reindex) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"offsets"} = $self->_open_index($index, $reindex) or return 18 0 18
656 18 ▶ if ($reindex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reindex) 18 0 18
657 18 $self->{indexing} = $index;
658 18 for my $file (@updated) {
659 60 my $fileno = $self->_path2fileno(basename($file));
660 60 &{$self->{offset_meth}}($self, $fileno, $file, $self->{offsets});
661 }
662 17 delete $self->{indexing};
663 }
664  
665 # Closing and reopening might help corrupted index file problem on Windows
666 17 $self->_close_index($self->{offsets});
667  
668 17 return $self->{offsets} = $self->_open_index($index);
669 }
670  
671  
672 35 ▶sub _open_index {
Subroutine _open_index: called CC=3 SCAR=11.0
673 # Open index file in read-only or write mode
674 35 my ($self, $index_file, $write) = @_;
675 35 my %offsets;
676 35 ▶ my $flags = $write ? O_CREAT|O_RDWR : O_RDONLY;
Branch: 1 branch
BranchTrueFalseTotal
$write ? : 18 17 35
677 35 my @dbmargs = $self->dbmargs;
678 35 ▶ tie %offsets, 'AnyDBM_File', $index_file, $flags, 0644, @dbmargs
Branch: 1 branch
BranchTrueFalseTotal
tie %offsets, "AnyDBM_File", $index_file, $flags, 420, @dbmargs or $self->throw("Could not open index file ${index_file}: $!") 35 0 35
679 or $self->throw( "Could not open index file $index_file: $!");
680 35 return \%offsets;
681 }
682  
683  
684 41 ▶sub _close_index {
Subroutine _close_index: called CC=1 SCAR=0.0
685 # Close index file
686 41 my ($self, $index) = @_;
687 41 untie %$index;
688 41 return 1;
689 }
690  
691 # Compiling the below regular expression speeds up _parse_compound_id
692 5 my $compound_id = qr/^ (.+?) (?:\:([\d_]+)(?:,|-|\.\.)([\d_]+))? (?:\/(.+))? $/x;
693  
694 41 ▶sub _parse_compound_id {
Subroutine _parse_compound_id: called CC=11 SCAR=24.0
695 # Handle compound IDs:
696 # $db->seq($id)
697 # $db->seq($id, $start, $stop, $strand)
698 # $db->seq("$id:$start,$stop")
699 # $db->seq("$id:$start..$stop")
700 # $db->seq("$id:$start-$stop")
701 # $db->seq("$id:$start,$stop/$strand")
702 # $db->seq("$id:$start..$stop/$strand")
703 # $db->seq("$id:$start-$stop/$strand")
704 # $db->seq("$id/$strand")
705 41 my ($self, $id, $start, $stop, $strand) = @_;
706  
707 41 ▶ if ( (not defined $start ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $start || defined $stop || defined $strand and $id =~ /$compound_id/) 18 23 41
Condition66%6/9
not defined $start || defined $stop || defined $strand and $id =~ /$compound_id/
!ll&&!rl&&r
23018
defined $start || defined $stop || defined $strand
l!l&&r!l&&!r
23018
defined $start or defined $stop
l!l&&r!l&&!r
23018
MC/DC25%1/4
not defined $start || defined $stop || defined $strand and $id =~ /$compound_id/
defined $start defined $stop defined $strand $id =~ /$compound_id/
Truth table40%2/5
not(A or B || C) and D
A= defined $startB= defined $stopC= defined $strandD= $id =~ /$compound_id/
ABCDresult
1XXX0
01XX0
001X0
00000
00011
708 (not defined $stop ) &&
709 (not defined $strand) &&
710 ($id =~ m{$compound_id}) ) {
711 # Start, stop and strand not provided and ID looks like a compound ID
712 18 ($id, $start, $stop, $strand) = ($1, $2, $3, $4);
713 }
714  
715 # Start, stop and strand defaults
716 41 ▶ $stop ||= $self->length($id) || 0; # 0 if sequence not found in database
Condition100%5/5
$stop ||= $self->length($id) || 0
l!l&&r!l&&!r
3452
$self->length($id) || 0
l!l
52
MC/DC100%2/2
$stop ||= $self->length($id) || 0
$stop $self->length($id)
Truth table100%3/3
A ||= B
A= $stopB= $self->length($id)
ABresult
1X1
011
000
717 41 ▶ $start ||= ($stop > 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$stop > 0 ? : 5 2 7
Condition100%3/3
$start ||= $stop > 0 ? 1 : 0
l!l&&r!l&&!r
3452
MC/DC100%2/2
$start ||= $stop > 0 ? 1 : 0
$start $stop > 0 ? 1 : 0
Truth table100%3/3
A ||= B
A= $startB= $stop > 0 ? 1 : 0
ABresult
1X1
011
000
718 41 ▶ $strand ||= 1;
Condition100%2/2
$strand ||= 1
l!l
1328
MC/DC100%1/1
$strand ||= 1
$strand
Truth table100%2/2
A
A= $strand
Aresult
11
00
719  
720 # Convert numbers such as 1_000_000 to 1000000
721 41 $start =~ s/_//g;
722 41 $stop =~ s/_//g;
723  
724 41 ▶ if ($start > $stop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start > $stop) 8 33 41
725 # Change the strand
726 8 ($start, $stop) = ($stop, $start);
727 8 $strand *= -1;
728 }
729  
730 41 return $id, $start, $stop, $strand;
731 }
732  
733  
734 2958 ▶sub _guess_alphabet {
Subroutine _guess_alphabet: called CC=4 SCAR=13.9
735 # Determine the molecular type of the given sequence string:
736 # 'dna', 'rna', 'protein' or '' (unknown/empty)
737 2958 my ($self, $string) = @_;
738 # Handle IUPAC residues like PrimarySeq does
739 2958 my $alphabet = Bio::PrimarySeq::_guess_alphabet_from_string($self, $string, 1);
740 2958 ▶ return $alphabet eq 'dna' ? DNA
Branch: 3 branches
BranchTrueFalseTotal
$alphabet eq 'dna' ? : 2903 55 2958
$alphabet eq 'rna' ? : 11 44 55
$alphabet eq 'protein' ? : 44 0 44
741 : $alphabet eq 'rna' ? RNA
742 : $alphabet eq 'protein' ? PROTEIN
743 : NA;
744 }
745  
746  
747 3014 ▶sub _makeid {
Subroutine _makeid: called CC=2 SCAR=6.9
748 # Process the header line by applying any transformation given in -makeid
749 3014 my ($self, $header_line) = @_;
750 3014 ▶ return ref($self->{makeid}) eq 'CODE' ? $self->{makeid}->($header_line) : $1;
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'makeid'} eq 'CODE' ? : 14 3000 3014
751 }
752  
753  
754 104 ▶sub _check_linelength {
Subroutine _check_linelength: called CC=3 SCAR=11.1
755 # Check that the line length is valid. Generate an error otherwise.
756 104 my ($self, $linelength) = @_;
757 104 ▶ return if not defined $linelength;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $linelength 9 95 104
758 95 ▶ $self->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $linelength > 65535 0 95 95
759 "Each line of the file must be less than 65,536 characters. Line ".
760 "$. is $linelength chars."
761 ) if $linelength > 65535;
762 }
763  
764  
765 18 ▶sub _calc_termination_length {
Subroutine _calc_termination_length: called CC=3 SCAR=11.1
766 # Try the beginning of the file to determine termination length
767 # Account for crlf-terminated Windows and Mac files
768 18 my ($self, $file) = @_;
769 18 ▶ my $fh = IO::File->new($file) or $self->throw( "Could not open $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($file) or $self->throw("Could not open ${file}: $!") 18 0 18
770  
771 # In Windows, text files have '\r\n' as line separator, but when reading in
772 # text mode Perl will only show the '\n'. This means that for a line "ABC\r\n",
773 # "length $_" will report 4 although the line is 5 bytes in length.
774 # We assume that all lines have the same line separator and only read current line.
775 18 my $init_pos = tell($fh);
776 18 my $curr_line = <$fh>;
777 18 my $pos_diff = tell($fh) - $init_pos;
778 18 my $correction = $pos_diff - length $curr_line;
779 18 close $fh;
780  
781 18 ▶ $self->{termination_length} = ($curr_line =~ /\r\n$/) ? 2 : 1+$correction;
Branch: 1 branch
BranchTrueFalseTotal
$curr_line =~ /\r\n$/ ? : 0 18 18
782 18 return $self->{termination_length};
783 }
784  
785  
786 80 ▶sub _calc_offset {
Subroutine _calc_offset: called CC=3 SCAR=11.0
787 # Get the offset of the n-th residue of the sequence with the given ID
788 # and termination length (tl)
789 80 my ($self, $id, $n) = @_;
790 80 my $tl = $self->{termination_length};
791 80 $n--;
792 80 my ($offset, $seqlen, $linelen) = (&{$self->{unpackmeth}}($self->{offsets}{$id}))[0,1,3];
793 80 ▶ $n = 0 if $n < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 0 2 78 80
794 80 ▶ $n = $seqlen-1 if $n >= $seqlen;
Branch: 1 branch
BranchTrueFalseTotal
if $n >= $seqlen 2 78 80
795 80 return $offset + $linelen * int($n/($linelen-$tl)) + $n % ($linelen-$tl);
796 }
797  
798  
799 45 ▶sub _fh {
Subroutine _fh: called CC=4 SCAR=14.5
800 # Given a sequence ID, return the filehandle on which to find this sequence
801 45 my ($self, $id) = @_;
802 45 ▶ $self->throw('Need to provide a sequence ID') if not defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 45 45
803 45 ▶ my $file = $self->file($id) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->file($id) or return 44 1 45
804 44 ▶ return eval {
Condition33%1/3
eval { do { $self->_fhcache('File::Spec'->catfile($self->{'dirname'}, $file)) } } || $self->throw("Can't open file $file")
l!l&&r!l&&!r
4400
MC/DC0%0/2
eval { do { $self->_fhcache('File::Spec'->catfile($self->{'dirname'}, $file)) } } || $self->throw("Can't open file $file")
eval { do { $self->_fhcache('File::Spec'->catfile($self->{'dirname'}, $file)) } } $self->throw("Can't open file $file")
Truth table33%1/3
A || B
A= eval { do { $self->_fhcache('File::Spec'->catfile($self->{'dirname'}, $file)) } }B= $self->throw("Can't open file $file")
ABresult
1X1
011
000
805 44 $self->_fhcache( File::Spec->catfile($self->{dirname}, $file));
806 } || $self->throw( "Can't open file $file" );
807 }
808  
809  
810 44 ▶sub _fhcache {
Subroutine _fhcache: called CC=5 SCAR=19.4
811 44 my ($self, $path) = @_;
812 44 ▶ if (!$self->{fhcache}{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"fhcache"}{$path}) 8 36 44
813 8 ▶ if ($self->{curopen} >= $self->{maxopen}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"curopen"} >= $self->{"maxopen"}) 0 8 8
814 0 my @lru = sort {$self->{cacheseq}{$a} <=> $self->{cacheseq}{$b};}
815 0 keys %{$self->{fhcache}};
816 0 splice(@lru, $self->{maxopen} / 3);
817 0 $self->{curopen} -= @lru;
818 0 for (@lru) {
819 0 delete $self->{fhcache}{$_};
820 }
821 }
822 8 ▶ $self->{fhcache}{$path} = IO::File->new($path) || return;
Condition50%1/2
'IO::File'->new($path) || (return)
l!l
80
MC/DC0%0/1
'IO::File'->new($path) || (return)
'IO::File'->new($path)
Truth table50%1/2
A
A= 'IO::File'->new($path)
Aresult
11
00
823 8 binmode $self->{fhcache}{$path};
824 8 $self->{curopen}++;
825 }
826 44 $self->{cacheseq}{$path}++;
827 44 return $self->{fhcache}{$path};
828 }
829  
830  
831 #-------------------------------------------------------------
832 # Methods to store and retrieve data from indexed file
833 #
834  
835 =head2 offset
836  
837 Title : offset
838 Usage : my $offset = $db->offset($id);
839 Function: Get the offset of the indicated sequence from the beginning of the
840 file in which it is located. The offset points to the beginning of
841 the sequence, not the beginning of the header line.
842 Returns : String
843 Args : ID of sequence
844  
845 =cut
846  
847 1 ▶sub offset {
Subroutine offset: called CC=3 SCAR=11.3
Pod documented
848 1 my ($self, $id) = @_;
849 1 ▶ $self->throw('Need to provide a sequence ID') if not defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 1 1
850 1 ▶ my $offset = $self->{offsets}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $offset = $self->{"offsets"}{$id} or return 1 0 1
851 1 return (&{$self->{unpackmeth}}($offset))[0];
852 }
853  
854  
855 =head2 strlen
856  
857 Title : strlen
858 Usage : my $length = $db->strlen($id);
859 Function: Get the number of characters in the sequence string.
860 Returns : Integer
861 Args : ID of sequence
862  
863 =cut
864  
865 16 ▶sub strlen {
Subroutine strlen: called CC=3 SCAR=11.3
Pod documented
866 16 my ($self, $id) = @_;
867 16 ▶ $self->throw('Need to provide a sequence ID') if not defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 16 16
868 16 ▶ my $offset = $self->{offsets}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $offset = $self->{"offsets"}{$id} or return 16 0 16
869 16 return (&{$self->{unpackmeth}}($offset))[1];
870 }
871  
872  
873 =head2 length
874  
875 Title : length
876 Usage : my $length = $db->length($id);
877 Function: Get the number of residues of the sequence.
878 Returns : Integer
879 Args : ID of sequence
880  
881 =cut
882  
883 28 ▶sub length {
Subroutine length: called CC=3 SCAR=11.0
Pod documented
884 28 my ($self, $id) = @_;
885 28 ▶ $self->throw('Need to provide a sequence ID') if not defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 28 28
886 28 ▶ my $offset = $self->{offsets}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $offset = $self->{"offsets"}{$id} or return 27 1 28
887 27 return (&{$self->{unpackmeth}}($offset))[2];
888 }
889  
890  
891 =head2 linelen
892  
893 Title : linelen
894 Usage : my $linelen = $db->linelen($id);
895 Function: Get the length of the line for this sequence.
896 Returns : Integer
897 Args : ID of sequence
898  
899 =cut
900  
901 0 ▶sub linelen {
Subroutine linelen: not called CC=3 SCAR=24.8
Pod documented
902 0 my ($self, $id) = @_;
903 0 ▶ $self->throw('Need to provide a sequence ID') if not defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
904 0 ▶ my $offset = $self->{offsets}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $offset = $self->{"offsets"}{$id} or return 0 0 0
905 0 return (&{$self->{unpackmeth}}($offset))[3];
906 }
907  
908  
909 =head2 headerlen
910  
911 Title : headerlen
912 Usage : my $length = $db->headerlen($id);
913 Function: Get the length of the header line for the indicated sequence.
914 Returns : Integer
915 Args : ID of sequence
916  
917 =cut
918  
919 0 ▶sub headerlen {
Subroutine headerlen: not called CC=3 SCAR=24.8
Pod documented
920 0 my ($self, $id) = @_;
921 0 ▶ $self->throw('Need to provide a sequence ID') if not defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
922 0 ▶ my $offset = $self->{offsets}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $offset = $self->{"offsets"}{$id} or return 0 0 0
923 0 return (&{$self->{unpackmeth}}($offset))[4];
924 }
925  
926  
927 =head2 header_offset
928  
929 Title : header_offset
930 Usage : my $offset = $db->header_offset($id);
931 Function: Get the offset of the header line for the indicated sequence from
932 the beginning of the file in which it is located.
933 Returns : String
934 Args : ID of sequence
935  
936 =cut
937  
938 0 ▶sub header_offset {
Subroutine header_offset: not called CC=3 SCAR=24.8
Pod documented
939 0 my ($self, $id) = @_;
940 0 ▶ $self->throw('Need to provide a sequence ID') if not defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
941 0 ▶ return if not $self->{offsets}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"offsets"}{$id} 0 0 0
942 0 return $self->offset($id) - $self->headerlen($id);
943 }
944  
945  
946 =head2 alphabet
947  
948 Title : alphabet
949 Usage : my $alphabet = $db->alphabet($id);
950 Function: Get the molecular type of the indicated sequence: dna, rna or protein
951 Returns : String
952 Args : ID of sequence
953  
954 =cut
955  
956 13 ▶sub alphabet {
Subroutine alphabet: called CC=6 SCAR=18.0
Pod documented
957 13 my ($self, $id) = @_;
958 13 ▶ $self->throw('Need to provide a sequence ID') if not defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 13 13
959 13 ▶ my $offset = $self->{offsets}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $offset = $self->{"offsets"}{$id} or return 13 0 13
960 13 my $alphabet = (&{$self->{unpackmeth}}($offset))[5];
961 13 ▶ return : $alphabet == Bio::DB::IndexedBase::DNA ? 'dna'
Branch: 3 branches
BranchTrueFalseTotal
$alphabet == 1 ? : 8 5 13
$alphabet == 2 ? : 2 3 5
$alphabet == 3 ? : 2 1 3
962 : $alphabet == Bio::DB::IndexedBase::RNA ? 'rna'
963 : $alphabet == Bio::DB::IndexedBase::PROTEIN ? 'protein'
964 : '';
965 }
966  
967  
968 =head2 file
969  
970 Title : file
971 Usage : my $file = $db->file($id);
972 Function: Get the the name of the file in which the indicated sequence can be
973 found.
974 Returns : String
975 Args : ID of sequence
976  
977 =cut
978  
979 55 ▶sub file {
Subroutine file: called CC=3 SCAR=11.0
Pod documented
980 55 my ($self, $id) = @_;
981 55 ▶ $self->throw('Need to provide a sequence ID') if not defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 55 55
982 55 ▶ my $offset = $self->{offsets}{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $offset = $self->{"offsets"}{$id} or return 54 1 55
983 54 return $self->_fileno2path((&{$self->{unpackmeth}}($offset))[6]);
984 }
985  
986  
987 54 ▶sub _fileno2path {
Subroutine _fileno2path: called CC=1 SCAR=0.0
988 54 my ($self, $fileno) = @_;
989 54 return $self->{fileno2path}->[$fileno];
990 }
991  
992  
993 121 ▶sub _path2fileno {
Subroutine _path2fileno: called CC=2 SCAR=6.9
994 121 my ($self, $path) = @_;
995 121 ▶ if ( not exists $self->{filepath2no}->{$path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"filepath2no"}{$path}) 61 60 121
996 61 my $fileno = ($self->{filepath2no}->{$path} = 0+ $self->{fileno}++);
997 61 $self->{fileno2path}->[$fileno] = $path; # Save path
998 }
999 121 return $self->{filepath2no}->{$path};
1000  
1001 }
1002  
1003  
1004 3014 ▶sub _packSmall {
Subroutine _packSmall: called CC=1 SCAR=0.0
1005 3014 return pack STRUCT, @_;
1006 }
1007  
1008  
1009 0 ▶sub _packBig {
Subroutine _packBig: not called CC=1 SCAR=6.9
1010 0 return pack STRUCTBIG, @_;
1011 }
1012  
1013  
1014 195 ▶sub _unpackSmall {
Subroutine _unpackSmall: called CC=1 SCAR=0.0
1015 195 return unpack STRUCT, shift;
1016 }
1017  
1018  
1019 0 ▶sub _unpackBig {
Subroutine _unpackBig: not called CC=1 SCAR=6.9
1020 0 return unpack STRUCTBIG, shift;
1021 }
1022  
1023  
1024 18 ▶sub _set_pack_method {
Subroutine _set_pack_method: called CC=2 SCAR=7.2
1025 # Determine whether to use 32 or 64 bit integers for the given files.
1026 18 my $self = shift;
1027 # Find the maximum file size:
1028 18 my ($maxsize) = sort { $b <=> $a } map { -s $_ } @_;
1029 18 my $fourGB = (2 ** 32) - 1;
1030  
1031 18 ▶ if ($maxsize > $fourGB) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxsize > $fourGB) { } 0 18 18
1032 # At least one file exceeds 4Gb - we will need to use 64 bit ints
1033 0 $self->{packmeth} = \&_packBig;
1034 0 $self->{unpackmeth} = \&_unpackBig;
1035 } else {
1036 18 $self->{packmeth} = \&_packSmall;
1037 18 $self->{unpackmeth} = \&_unpackSmall;
1038 }
1039 18 return 1;
1040 }
1041  
1042  
1043 #-------------------------------------------------------------
1044 # Tied hash logic
1045 #
1046  
1047 2 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=1 SCAR=0.0
1048 2 return shift->new(@_);
1049 }
1050  
1051  
1052 3 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
1053 3 return shift->subseq(@_);
1054 }
1055  
1056  
1057 0 ▶sub STORE {
Subroutine STORE: not called CC=1 SCAR=6.9
1058 0 shift->throw("Read-only database");
1059 }
1060  
1061  
1062 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
1063 0 shift->throw("Read-only database");
1064 }
1065  
1066  
1067 0 ▶sub CLEAR {
Subroutine CLEAR: not called CC=1 SCAR=6.9
1068 0 shift->throw("Read-only database");
1069 }
1070  
1071  
1072 1 ▶sub EXISTS {
Subroutine EXISTS: called CC=1 SCAR=0.0
1073 1 return defined shift->offset(@_);
1074 }
1075  
1076  
1077 4 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
1078 4 return tied(%{shift->{offsets}})->FIRSTKEY(@_);
1079 }
1080  
1081  
1082 9 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=1 SCAR=0.0
1083 9 return tied(%{shift->{offsets}})->NEXTKEY(@_);
1084 }
1085  
1086  
1087 24 ▶sub DESTROY {
Subroutine DESTROY: called CC=8 SCAR=20.8
1088 24 my $self = shift;
1089  
1090 # Close filehandles
1091 24 ▶ while (my ($file, $fh) = each %{ $self->{fhcache} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"fhcache"};}) 8 24 32
1092 8 ▶ if (defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) 8 0 8
1093 8 $fh->close;
1094 }
1095 }
1096 24 $self->_close_index($self->{offsets});
1097  
1098 24 ▶ if ( $self->{clean} || $self->{indexing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"clean"} or $self->{"indexing"}) 5 19 24
Condition100%3/3
$self->{"clean"} or $self->{"indexing"}
l!l&&r!l&&!r
4119
MC/DC100%2/2
$self->{"clean"} or $self->{"indexing"}
$self->{"clean"} $self->{"indexing"}
Truth table100%3/3
A or B
A= $self->{"clean"}B= $self->{"indexing"}
ABresult
1X1
011
000
1099 # Indexing aborted or cleaning requested. Delete the index file.
1100 5 my $index = $self->{index_name};
1101  
1102 # Tied-hash in Strawberry Perl creates "$file.index"
1103 5 ▶ unlink $index if -e $index;
Branch: 1 branch
BranchTrueFalseTotal
if -e $index 0 5 5
1104 # Tied-hash in ActivePerl creates "$file.index.pag" and "$file.index.dir"
1105 5 ▶ unlink "$index.dir" if -e "$index.dir";
Branch: 1 branch
BranchTrueFalseTotal
if -e "$index.dir" 4 1 5
1106 5 ▶ unlink "$index.pag" if -e "$index.pag";
Branch: 1 branch
BranchTrueFalseTotal
if -e "$index.pag" 4 1 5
1107 }
1108 24 return 1;
1109 }
1110  
1111  
1112 #-------------------------------------------------------------
1113 # stream-based access to the database
1114 #
1115  
1116 package Bio::DB::Indexed::Stream;
1117 5 $Bio::DB::Indexed::Stream::VERSION = '1.7.8';
1118 5 ▶use base qw(Tie::Handle Bio::DB::SeqI);
Subroutine BEGIN: called CC=1 SCAR=0.0
1119  
1120  
1121 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
1122 4 my ($class, $db) = @_;
1123 4 my $key = $db->FIRSTKEY;
1124 4 return bless {
1125 db => $db,
1126 key => $key
1127 }, $class;
1128 }
1129  
1130 10 ▶sub next_seq {
Subroutine next_seq: called CC=2 SCAR=6.9
1131 10 my $self = shift;
1132 10 my ($key, $db) = @{$self}{'key', 'db'};
1133 10 ▶ return if not defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 1 9 10
1134 9 my $value = $db->get_Seq_by_id($key);
1135 9 $self->{key} = $db->NEXTKEY($key);
1136 9 return $value;
1137 }
1138  
1139 1 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
1140 1 my ($class, $db) = @_;
1141 1 return $class->new($db);
1142 }
1143  
1144 1 ▶sub READLINE {
Subroutine READLINE: called CC=2 SCAR=7.2
1145 1 my $self = shift;
1146 1 ▶ return $self->next_seq || undef;
Condition50%1/2
$self->next_seq || undef
l!l
10
MC/DC0%0/1
$self->next_seq || undef
$self->next_seq
Truth table50%1/2
A
A= $self->next_seq
Aresult
11
00
1147 }
1148  
1149  
1150 5 1;
« Bio/Tools/IUPAC.pm ↑ Summary Bio/Seq/RichSeq.pm »